Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying fixing #518. Almost functions work on both Windows and Unix with the NetDriver. #1014

Merged
merged 3 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Example/Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace Terminal.Gui {
/// This is the Curses driver for the gui.cs/Terminal framework.
/// </summary>
internal class CursesDriver : ConsoleDriver {
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public override int Cols => Curses.Cols;
public override int Rows => Curses.Lines;
public override int Top => 0;

// Current row, and current col, tracked by Move/AddRune only
int ccol, crow;
Expand Down Expand Up @@ -907,7 +907,5 @@ static public bool Suspend ()
killpg (0, signal);
return true;
}
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
}

}
86 changes: 4 additions & 82 deletions Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ namespace Terminal.Gui {
/// Implements a mock ConsoleDriver for unit testing
/// </summary>
public class FakeDriver : ConsoleDriver {
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
int cols, rows;
/// <summary>
///
/// </summary>
public override int Cols => cols;
/// <summary>
///
/// </summary>
public override int Rows => rows;
public override int Top => 0;

// The format is rows, columns and 3 values on the last column: Rune, Attribute and Dirty Flag
int [,,] contents;
Expand All @@ -49,9 +45,6 @@ void UpdateOffscreen ()

static bool sync = false;

/// <summary>
///
/// </summary>
public FakeDriver ()
{
cols = FakeConsole.WindowWidth;
Expand All @@ -62,11 +55,6 @@ public FakeDriver ()
bool needMove;
// Current row, and current col, tracked by Move/AddCh only
int ccol, crow;
/// <summary>
///
/// </summary>
/// <param name="col"></param>
/// <param name="row"></param>
public override void Move (int col, int row)
{
ccol = col;
Expand All @@ -84,10 +72,6 @@ public override void Move (int col, int row)

}

/// <summary>
///
/// </summary>
/// <param name="rune"></param>
public override void AddRune (Rune rune)
{
rune = MakePrintable (rune);
Expand All @@ -113,19 +97,12 @@ public override void AddRune (Rune rune)
UpdateScreen ();
}

/// <summary>
///
/// </summary>
/// <param name="str"></param>
public override void AddStr (ustring str)
{
foreach (var rune in str)
AddRune (rune);
}

/// <summary>
///
/// </summary>
public override void End ()
{
FakeConsole.ResetColor ();
Expand All @@ -138,10 +115,6 @@ static Attribute MakeColor (ConsoleColor f, ConsoleColor b)
return new Attribute () { value = ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff) };
}

/// <summary>
///
/// </summary>
/// <param name="terminalResized"></param>
public override void Init (Action terminalResized)
{
Colors.TopLevel = new ColorScheme ();
Expand Down Expand Up @@ -185,12 +158,6 @@ public override void Init (Action terminalResized)
//MockConsole.Clear ();
}

/// <summary>
///
/// </summary>
/// <param name="fore"></param>
/// <param name="back"></param>
/// <returns></returns>
public override Attribute MakeAttribute (Color fore, Color back)
{
return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
Expand All @@ -211,9 +178,6 @@ void SetColor (int color)
}
}

/// <summary>
///
/// </summary>
public override void UpdateScreen ()
{
int rows = Rows;
Expand All @@ -233,9 +197,6 @@ public override void UpdateScreen ()
}
}

/// <summary>
///
/// </summary>
public override void Refresh ()
{
int rows = Rows;
Expand Down Expand Up @@ -267,40 +228,24 @@ public override void Refresh ()
FakeConsole.CursorLeft = savedCol;
}

/// <summary>
///
/// </summary>
public override void UpdateCursor ()
{
//
}

/// <summary>
///
/// </summary>
public override void StartReportingMouseMoves ()
{
}

/// <summary>
///
/// </summary>
public override void StopReportingMouseMoves ()
{
}

/// <summary>
///
/// </summary>
public override void Suspend ()
{
}

int currentAttribute;
/// <summary>
///
/// </summary>
/// <param name="c"></param>
public override void SetAttribute (Attribute c)
{
currentAttribute = c.value;
Expand Down Expand Up @@ -417,18 +362,10 @@ private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key)
return keyMod != Key.Null ? keyMod | key : key;
}

/// <summary>
///
/// </summary>
/// <param name="mainLoop"></param>
/// <param name="keyHandler"></param>
/// <param name="keyDownHandler"></param>
/// <param name="keyUpHandler"></param>
/// <param name="mouseHandler"></param>
public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
{
// Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
(mainLoop.Driver as NetMainLoop).KeyPressed = delegate (ConsoleKeyInfo consoleKey) {
(mainLoop.Driver as FakeMainLoop).KeyPressed = delegate (ConsoleKeyInfo consoleKey) {
var map = MapKey (consoleKey);
if (map == (Key)0xffffffff)
return;
Expand All @@ -452,38 +389,23 @@ public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandle
};
}

/// <summary>
///
/// </summary>
/// <param name="foreground"></param>
/// <param name="background"></param>
public override void SetColors (ConsoleColor foreground, ConsoleColor background)
{
throw new NotImplementedException ();
}

/// <summary>
///
/// </summary>
/// <param name="foregroundColorId"></param>
/// <param name="backgroundColorId"></param>
public override void SetColors (short foregroundColorId, short backgroundColorId)
{
throw new NotImplementedException ();
}

/// <summary>
///
/// </summary>
public override void CookMouse ()
{
}

/// <summary>
///
/// </summary>
public override void UncookMouse ()
{
}
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
}
}
89 changes: 89 additions & 0 deletions Terminal.Gui/ConsoleDrivers/FakeDriver/FakeMainLoop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Threading;

namespace Terminal.Gui {
/// <summary>
/// Mainloop intended to be used with the .NET System.Console API, and can
/// be used on Windows and Unix, it is cross platform but lacks things like
/// file descriptor monitoring.
/// </summary>
/// <remarks>
/// This implementation is used for FakeDriver.
/// </remarks>
public class FakeMainLoop : IMainLoopDriver {
AutoResetEvent keyReady = new AutoResetEvent (false);
AutoResetEvent waitForProbe = new AutoResetEvent (false);
ConsoleKeyInfo? keyResult = null;
MainLoop mainLoop;
Func<ConsoleKeyInfo> consoleKeyReaderFn = null;

/// <summary>
/// Invoked when a Key is pressed.
/// </summary>
public Action<ConsoleKeyInfo> KeyPressed;

/// <summary>
/// Initializes the class.
/// </summary>
/// <remarks>
/// Passing a consoleKeyReaderfn is provided to support unit test scenarios.
/// </remarks>
/// <param name="consoleKeyReaderFn">The method to be called to get a key from the console.</param>
public FakeMainLoop (Func<ConsoleKeyInfo> consoleKeyReaderFn = null)
{
if (consoleKeyReaderFn == null) {
throw new ArgumentNullException ("key reader function must be provided.");
}
this.consoleKeyReaderFn = consoleKeyReaderFn;
}

void WindowsKeyReader ()
{
while (true) {
waitForProbe.WaitOne ();
keyResult = consoleKeyReaderFn ();
keyReady.Set ();
}
}

void IMainLoopDriver.Setup (MainLoop mainLoop)
{
this.mainLoop = mainLoop;
Thread readThread = new Thread (WindowsKeyReader);
readThread.Start ();
}

void IMainLoopDriver.Wakeup ()
{
}

bool IMainLoopDriver.EventsPending (bool wait)
{
long now = DateTime.UtcNow.Ticks;

int waitTimeout;
if (mainLoop.timeouts.Count > 0) {
waitTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
if (waitTimeout < 0)
return true;
} else
waitTimeout = -1;

if (!wait)
waitTimeout = 0;

keyResult = null;
waitForProbe.Set ();
keyReady.WaitOne (waitTimeout);
return keyResult.HasValue;
}

void IMainLoopDriver.MainIteration ()
{
if (keyResult.HasValue) {
KeyPressed?.Invoke (keyResult.Value);
keyResult = null;
}
}
}
}
Loading