123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- //
- // mainloop.cs: Linux/Curses MainLoop implementation.
- //
- using System.Runtime.InteropServices;
- namespace Terminal.Gui;
- /// <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>
- internal class UnixMainLoop : IMainLoopDriver
- {
- /// <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
- }
- public const int KEY_RESIZE = unchecked ((int)0xffffffffffffffff);
- private static readonly nint _ignore = Marshal.AllocHGlobal (1);
- private readonly CursesDriver _cursesDriver;
- private readonly Dictionary<int, Watch> _descriptorWatchers = new ();
- private readonly int [] _wakeUpPipes = new int [2];
- private MainLoop _mainLoop;
- private bool _pollDirty = true;
- private Pollfd [] _pollMap;
- private bool _winChanged;
- private readonly ManualResetEventSlim _eventReady = new (false);
- internal readonly ManualResetEventSlim _waitForInput = new (false);
- private readonly CancellationTokenSource _eventReadyTokenSource = new ();
- private readonly CancellationTokenSource _inputHandlerTokenSource = new ();
- public UnixMainLoop (ConsoleDriver consoleDriver = null)
- {
- // UnixDriver doesn't use the consoleDriver parameter, but the WindowsDriver does.
- _cursesDriver = (CursesDriver)Application.Driver;
- }
- void IMainLoopDriver.Wakeup ()
- {
- if (!ConsoleDriver.RunningUnitTests)
- {
- write (_wakeUpPipes [1], _ignore, 1);
- }
- }
- void IMainLoopDriver.Setup (MainLoop mainLoop)
- {
- _mainLoop = mainLoop;
- if (ConsoleDriver.RunningUnitTests)
- {
- return;
- }
- try
- {
- pipe (_wakeUpPipes);
- AddWatch (
- _wakeUpPipes [0],
- Condition.PollIn,
- ml =>
- {
- read (_wakeUpPipes [0], _ignore, 1);
- return true;
- }
- );
- }
- catch (DllNotFoundException e)
- {
- throw new NotSupportedException ("libc not found", e);
- }
- Task.Run (CursesInputHandler, _inputHandlerTokenSource.Token);
- }
- internal bool _forceRead;
- internal bool _suspendRead;
- private int n;
- private void CursesInputHandler ()
- {
- while (_mainLoop is { })
- {
- try
- {
- UpdatePollMap ();
- if (!_inputHandlerTokenSource.IsCancellationRequested && !_forceRead)
- {
- _waitForInput.Wait (_inputHandlerTokenSource.Token);
- }
- }
- catch (OperationCanceledException)
- {
- return;
- }
- finally
- {
- if (!_inputHandlerTokenSource.IsCancellationRequested)
- {
- _waitForInput.Reset ();
- }
- }
- while (!_inputHandlerTokenSource.IsCancellationRequested)
- {
- if (!_suspendRead)
- {
- n = poll (_pollMap, (uint)_pollMap.Length, 0);
- if (n == KEY_RESIZE)
- {
- _winChanged = true;
- break;
- }
- if (n > 0)
- {
- break;
- }
- }
- if (!_forceRead)
- {
- Task.Delay (100, _inputHandlerTokenSource.Token).Wait (_inputHandlerTokenSource.Token);
- }
- }
- _eventReady.Set ();
- }
- }
- bool IMainLoopDriver.EventsPending ()
- {
- _waitForInput.Set ();
- if (_mainLoop.CheckTimersAndIdleHandlers (out int waitTimeout))
- {
- return true;
- }
- try
- {
- if (!_eventReadyTokenSource.IsCancellationRequested)
- {
- _eventReady.Wait (waitTimeout, _eventReadyTokenSource.Token);
- }
- }
- catch (OperationCanceledException)
- {
- return true;
- }
- finally
- {
- _eventReady.Reset ();
- }
- if (!_eventReadyTokenSource.IsCancellationRequested)
- {
- return n > 0 || _mainLoop.CheckTimersAndIdleHandlers (out _) || _winChanged;
- }
- return true;
- }
- void IMainLoopDriver.Iteration ()
- {
- if (_winChanged)
- {
- _winChanged = false;
- _cursesDriver.ProcessInput ();
- // This is needed on the mac. See https://github.com/gui-cs/Terminal.Gui/pull/2922#discussion_r1365992426
- _cursesDriver.ProcessWinChange ();
- }
- n = 0;
- if (_pollMap is null)
- {
- return;
- }
- foreach (Pollfd p in _pollMap)
- {
- Watch watch;
- if (p.revents == 0)
- {
- continue;
- }
- if (!_descriptorWatchers.TryGetValue (p.fd, out watch))
- {
- continue;
- }
- if (!watch.Callback (_mainLoop))
- {
- _descriptorWatchers.Remove (p.fd);
- }
- }
- }
- void IMainLoopDriver.TearDown ()
- {
- _descriptorWatchers?.Clear ();
- _inputHandlerTokenSource?.Cancel ();
- _inputHandlerTokenSource?.Dispose ();
- _waitForInput?.Dispose ();
- _eventReadyTokenSource?.Cancel ();
- _eventReadyTokenSource?.Dispose ();
- _eventReady?.Dispose ();
- _mainLoop = null;
- }
- /// <summary>Watches a file descriptor for activity.</summary>
- /// <remarks>
- /// When the condition is met, the provided callback is invoked. If the callback returns false, the watch is
- /// automatically removed. The return value is a token that represents this watch, you can use this token to remove the
- /// watch by calling RemoveWatch.
- /// </remarks>
- internal object AddWatch (int fileDescriptor, Condition condition, Func<MainLoop, bool> callback)
- {
- if (callback is null)
- {
- throw new ArgumentNullException (nameof (callback));
- }
- var watch = new Watch { Condition = condition, Callback = callback, File = fileDescriptor };
- _descriptorWatchers [fileDescriptor] = watch;
- _pollDirty = true;
- return watch;
- }
- /// <summary>Removes an active watch from the mainloop.</summary>
- /// <remarks>The token parameter is the value returned from AddWatch</remarks>
- internal void RemoveWatch (object token)
- {
- if (!ConsoleDriver.RunningUnitTests)
- {
- if (token is not Watch watch)
- {
- return;
- }
- _descriptorWatchers.Remove (watch.File);
- }
- }
- [DllImport ("libc")]
- private static extern int pipe ([In] [Out] int [] pipes);
- [DllImport ("libc")]
- private static extern int poll ([In] [Out] Pollfd [] ufds, uint nfds, int timeout);
- [DllImport ("libc")]
- private static extern int read (int fd, nint buf, nint n);
- private void UpdatePollMap ()
- {
- if (!_pollDirty)
- {
- return;
- }
- _pollDirty = false;
- _pollMap = new Pollfd [_descriptorWatchers.Count];
- var i = 0;
- foreach (int fd in _descriptorWatchers.Keys)
- {
- _pollMap [i].fd = fd;
- _pollMap [i].events = (short)_descriptorWatchers [fd].Condition;
- i++;
- }
- }
- [DllImport ("libc")]
- private static extern int write (int fd, nint buf, nint n);
- [StructLayout (LayoutKind.Sequential)]
- private struct Pollfd
- {
- public int fd;
- public short events;
- public readonly short revents;
- }
- private class Watch
- {
- public Func<MainLoop, bool> Callback;
- public Condition Condition;
- public int File;
- }
- }
|