|
@@ -32,20 +32,75 @@ using System.Threading;
|
|
|
|
|
|
namespace Mono.Terminal {
|
|
namespace Mono.Terminal {
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Public interface to create your own platform specific main loop driver.
|
|
|
|
+ /// </summary>
|
|
public interface IMainLoopDriver {
|
|
public interface IMainLoopDriver {
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Initializes the main loop driver, gets the calling main loop for the initialization.
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="mainLoop">Main loop.</param>
|
|
void Setup (MainLoop mainLoop);
|
|
void Setup (MainLoop mainLoop);
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Wakes up the mainloop that might be waiting on input, must be thread safe.
|
|
|
|
+ /// </summary>
|
|
void Wakeup ();
|
|
void Wakeup ();
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Must report whether there are any events pending, or even block waiting for events.
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns><c>true</c>, if there were pending events, <c>false</c> otherwise.</returns>
|
|
|
|
+ /// <param name="wait">If set to <c>true</c> wait until an event is available, otherwise return immediately.</param>
|
|
bool EventsPending (bool wait);
|
|
bool EventsPending (bool wait);
|
|
void MainIteration ();
|
|
void MainIteration ();
|
|
}
|
|
}
|
|
|
|
|
|
- internal class UnixMainLoop : IMainLoopDriver {
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Unix main loop, suitable for using on Posix systems
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <remarks>
|
|
|
|
+ /// In addition to the general functions of the mainloop, the Unix version
|
|
|
|
+ /// can watch file descriptors using the AddWatch methods.
|
|
|
|
+ /// </remarks>
|
|
|
|
+ public class UnixMainLoop : IMainLoopDriver {
|
|
[StructLayout (LayoutKind.Sequential)]
|
|
[StructLayout (LayoutKind.Sequential)]
|
|
struct Pollfd {
|
|
struct Pollfd {
|
|
public int fd;
|
|
public int fd;
|
|
public short events, revents;
|
|
public short events, revents;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Condition on which to wake up from file descriptor activity. These match the Linux/BSD poll definitions.
|
|
|
|
+ /// </summary>
|
|
|
|
+ [Flags]
|
|
|
|
+ public enum Condition : short {
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// There is data to read
|
|
|
|
+ /// </summary>
|
|
|
|
+ PollIn = 1,
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Writing to the specified descriptor will not block
|
|
|
|
+ /// </summary>
|
|
|
|
+ PollOut = 4,
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// There is urgent data to read
|
|
|
|
+ /// </summary>
|
|
|
|
+ PollPri = 2,
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Error condition on output
|
|
|
|
+ /// </summary>
|
|
|
|
+ PollErr = 8,
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Hang-up on output
|
|
|
|
+ /// </summary>
|
|
|
|
+ PollHup = 16,
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// File descriptor is not open.
|
|
|
|
+ /// </summary>
|
|
|
|
+ PollNval = 32
|
|
|
|
+ }
|
|
|
|
+
|
|
class Watch {
|
|
class Watch {
|
|
public int File;
|
|
public int File;
|
|
public Condition Condition;
|
|
public Condition Condition;
|
|
@@ -70,6 +125,7 @@ namespace Mono.Terminal {
|
|
bool poll_dirty = true;
|
|
bool poll_dirty = true;
|
|
int [] wakeupPipes = new int [2];
|
|
int [] wakeupPipes = new int [2];
|
|
static IntPtr ignore = Marshal.AllocHGlobal (1);
|
|
static IntPtr ignore = Marshal.AllocHGlobal (1);
|
|
|
|
+ MainLoop mainLoop;
|
|
|
|
|
|
void IMainLoopDriver.Wakeup ()
|
|
void IMainLoopDriver.Wakeup ()
|
|
{
|
|
{
|
|
@@ -77,8 +133,9 @@ namespace Mono.Terminal {
|
|
}
|
|
}
|
|
|
|
|
|
void IMainLoopDriver.Setup (MainLoop mainLoop) {
|
|
void IMainLoopDriver.Setup (MainLoop mainLoop) {
|
|
|
|
+ this.mainLoop = mainLoop;
|
|
pipe (wakeupPipes);
|
|
pipe (wakeupPipes);
|
|
- mainLoop.AddWatch (wakeupPipes [0], MainLoop.Condition.PollIn, ml => {
|
|
|
|
|
|
+ AddWatch (wakeupPipes [0], Condition.PollIn, ml => {
|
|
read (wakeupPipes [0], ignore, (IntPtr)1);
|
|
read (wakeupPipes [0], ignore, (IntPtr)1);
|
|
return true;
|
|
return true;
|
|
});
|
|
});
|
|
@@ -109,7 +166,7 @@ namespace Mono.Terminal {
|
|
/// The return value is a token that represents this watch, you can
|
|
/// The return value is a token that represents this watch, you can
|
|
/// use this token to remove the watch by calling RemoveWatch.
|
|
/// use this token to remove the watch by calling RemoveWatch.
|
|
/// </remarks>
|
|
/// </remarks>
|
|
- public object AddWatch (int fileDescriptor, MainLoop.Condition condition, Func<MainLoop, bool> callback)
|
|
|
|
|
|
+ public object AddWatch (int fileDescriptor, Condition condition, Func<MainLoop, bool> callback)
|
|
{
|
|
{
|
|
if (callback == null)
|
|
if (callback == null)
|
|
throw new ArgumentNullException (nameof(callback));
|
|
throw new ArgumentNullException (nameof(callback));
|
|
@@ -140,8 +197,8 @@ namespace Mono.Terminal {
|
|
long now = DateTime.UtcNow.Ticks;
|
|
long now = DateTime.UtcNow.Ticks;
|
|
|
|
|
|
int pollTimeout, n;
|
|
int pollTimeout, n;
|
|
- if (timeouts.Count > 0) {
|
|
|
|
- pollTimeout = (int)((timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
|
|
|
|
|
|
+ if (mainLoop.timeouts.Count > 0) {
|
|
|
|
+ pollTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
|
|
if (pollTimeout < 0)
|
|
if (pollTimeout < 0)
|
|
return true;
|
|
return true;
|
|
|
|
|
|
@@ -155,9 +212,9 @@ namespace Mono.Terminal {
|
|
|
|
|
|
n = poll (pollmap, (uint)pollmap.Length, pollTimeout);
|
|
n = poll (pollmap, (uint)pollmap.Length, pollTimeout);
|
|
int ic;
|
|
int ic;
|
|
- lock (idleHandlers)
|
|
|
|
- ic = idleHandlers.Count;
|
|
|
|
- return n > 0 || timeouts.Count > 0 && ((timeouts.Keys [0] - DateTime.UtcNow.Ticks) < 0) || ic > 0;
|
|
|
|
|
|
+ lock (mainLoop.idleHandlers)
|
|
|
|
+ ic = mainLoop.idleHandlers.Count;
|
|
|
|
+ return n > 0 || mainLoop.timeouts.Count > 0 && ((mainLoop.timeouts.Keys [0] - DateTime.UtcNow.Ticks) < 0) || ic > 0;
|
|
}
|
|
}
|
|
|
|
|
|
void IMainLoopDriver.MainIteration ()
|
|
void IMainLoopDriver.MainIteration ()
|
|
@@ -171,22 +228,27 @@ namespace Mono.Terminal {
|
|
|
|
|
|
if (!descriptorWatchers.TryGetValue (p.fd, out watch))
|
|
if (!descriptorWatchers.TryGetValue (p.fd, out watch))
|
|
continue;
|
|
continue;
|
|
- if (!watch.Callback (this))
|
|
|
|
|
|
+ if (!watch.Callback (this.mainLoop))
|
|
descriptorWatchers.Remove (p.fd);
|
|
descriptorWatchers.Remove (p.fd);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- internal class NetMainLoop : IMainLoopDriver {
|
|
|
|
|
|
+ /// <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>
|
|
|
|
+ class NetMainLoop : IMainLoopDriver {
|
|
AutoResetEvent keyReady = new AutoResetEvent (false);
|
|
AutoResetEvent keyReady = new AutoResetEvent (false);
|
|
AutoResetEvent waitForProbe = new AutoResetEvent (false);
|
|
AutoResetEvent waitForProbe = new AutoResetEvent (false);
|
|
ConsoleKeyInfo? windowsKeyResult = null;
|
|
ConsoleKeyInfo? windowsKeyResult = null;
|
|
- Action<ConsoleKeyInfo> keyCallback;
|
|
|
|
|
|
+ public Action<ConsoleKeyInfo> WindowsKeyPressed;
|
|
|
|
+ MainLoop mainLoop;
|
|
|
|
|
|
- public NetMainLoop (Action<ConsoleKeyInfo> keyCallback)
|
|
|
|
|
|
+ public NetMainLoop ()
|
|
{
|
|
{
|
|
- this.keyCallback = keyCallback;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void WindowsKeyReader ()
|
|
void WindowsKeyReader ()
|
|
@@ -200,6 +262,7 @@ namespace Mono.Terminal {
|
|
|
|
|
|
void IMainLoopDriver.Setup (MainLoop mainLoop)
|
|
void IMainLoopDriver.Setup (MainLoop mainLoop)
|
|
{
|
|
{
|
|
|
|
+ this.mainLoop = mainLoop;
|
|
Thread readThread = new Thread (WindowsKeyReader);
|
|
Thread readThread = new Thread (WindowsKeyReader);
|
|
readThread.Start ();
|
|
readThread.Start ();
|
|
}
|
|
}
|
|
@@ -213,8 +276,8 @@ namespace Mono.Terminal {
|
|
long now = DateTime.UtcNow.Ticks;
|
|
long now = DateTime.UtcNow.Ticks;
|
|
|
|
|
|
int waitTimeout;
|
|
int waitTimeout;
|
|
- if (timeouts.Count > 0) {
|
|
|
|
- waitTimeout = (int)((timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
|
|
|
|
|
|
+ if (mainLoop.timeouts.Count > 0) {
|
|
|
|
+ waitTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
|
|
if (waitTimeout < 0)
|
|
if (waitTimeout < 0)
|
|
return true;
|
|
return true;
|
|
} else
|
|
} else
|
|
@@ -232,32 +295,13 @@ namespace Mono.Terminal {
|
|
void IMainLoopDriver.MainIteration ()
|
|
void IMainLoopDriver.MainIteration ()
|
|
{
|
|
{
|
|
if (windowsKeyResult.HasValue) {
|
|
if (windowsKeyResult.HasValue) {
|
|
- if (keyCallback != null)
|
|
|
|
- keyCallback (windowsKeyResult.Value);
|
|
|
|
|
|
+ if (WindowsKeyPressed!= null)
|
|
|
|
+ WindowsKeyPressed (windowsKeyResult.Value);
|
|
windowsKeyResult = null;
|
|
windowsKeyResult = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- internal class WinConsoleLoop : IMainLoopDriver {
|
|
|
|
- void IMainLoopDriver.Setup (MainLoop mainLoop)
|
|
|
|
- {
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- void IMainLoopDriver.Wakeup ()
|
|
|
|
- {
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- bool IMainLoopDriver.EventsPending (bool wait)
|
|
|
|
- {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- void IMainLoopDriver.MainIteration ()
|
|
|
|
- {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Simple main loop implementation that can be used to monitor
|
|
/// Simple main loop implementation that can be used to monitor
|
|
/// file descriptor, run timers and idle handlers.
|
|
/// file descriptor, run timers and idle handlers.
|
|
@@ -266,54 +310,21 @@ namespace Mono.Terminal {
|
|
/// Monitoring of file descriptors is only available on Unix, there
|
|
/// Monitoring of file descriptors is only available on Unix, there
|
|
/// does not seem to be a way of supporting this on Windows.
|
|
/// does not seem to be a way of supporting this on Windows.
|
|
/// </remarks>
|
|
/// </remarks>
|
|
- public class MainLoop {
|
|
|
|
- bool useUnix = true;
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// Condition on which to wake up from file descriptor activity. These match the Linux/BSD poll definitions.
|
|
|
|
- /// </summary>
|
|
|
|
- [Flags]
|
|
|
|
- public enum Condition : short {
|
|
|
|
- /// <summary>
|
|
|
|
- /// There is data to read
|
|
|
|
- /// </summary>
|
|
|
|
- PollIn = 1,
|
|
|
|
- /// <summary>
|
|
|
|
- /// Writing to the specified descriptor will not block
|
|
|
|
- /// </summary>
|
|
|
|
- PollOut = 4,
|
|
|
|
- /// <summary>
|
|
|
|
- /// There is urgent data to read
|
|
|
|
- /// </summary>
|
|
|
|
- PollPri = 2,
|
|
|
|
- /// <summary>
|
|
|
|
- /// Error condition on output
|
|
|
|
- /// </summary>
|
|
|
|
- PollErr = 8,
|
|
|
|
- /// <summary>
|
|
|
|
- /// Hang-up on output
|
|
|
|
- /// </summary>
|
|
|
|
- PollHup = 16,
|
|
|
|
- /// <summary>
|
|
|
|
- /// File descriptor is not open.
|
|
|
|
- /// </summary>
|
|
|
|
- PollNval = 32
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- class Timeout {
|
|
|
|
|
|
+ public class MainLoop {
|
|
|
|
+ internal class Timeout {
|
|
public TimeSpan Span;
|
|
public TimeSpan Span;
|
|
public Func<MainLoop,bool> Callback;
|
|
public Func<MainLoop,bool> Callback;
|
|
}
|
|
}
|
|
|
|
|
|
- SortedList <long, Timeout> timeouts = new SortedList<long,Timeout> ();
|
|
|
|
- List<Func<bool>> idleHandlers = new List<Func<bool>> ();
|
|
|
|
|
|
+ internal SortedList <long, Timeout> timeouts = new SortedList<long,Timeout> ();
|
|
|
|
+ internal List<Func<bool>> idleHandlers = new List<Func<bool>> ();
|
|
|
|
|
|
IMainLoopDriver driver;
|
|
IMainLoopDriver driver;
|
|
- IMainLoopDriver Driver => driver;
|
|
|
|
|
|
+ public IMainLoopDriver Driver => driver;
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// Default constructor
|
|
|
|
|
|
+ /// Creates a new Mainloop, to run it you must provide a driver, and choose
|
|
|
|
+ /// one of the implementations UnixMainLoop, NetMainLoop or WindowsMainLoop.
|
|
/// </summary>
|
|
/// </summary>
|
|
public MainLoop (IMainLoopDriver driver)
|
|
public MainLoop (IMainLoopDriver driver)
|
|
{
|
|
{
|