forked from gui-cs/Terminal.Gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnixMainLoop.cs
216 lines (184 loc) · 5.16 KB
/
UnixMainLoop.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// mainloop.cs: Simple managed mainloop implementation.
//
using System;
using System.Collections.Generic;
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 {
public UnixMainLoop (ConsoleDriver consoleDriver = null)
{
// UnixDriver doesn't use the consoleDriver parameter, but the WindowsDriver does.
}
public const int KEY_RESIZE = unchecked((int)0xffffffffffffffff);
[StructLayout (LayoutKind.Sequential)]
struct Pollfd {
public int fd;
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 {
public int File;
public Condition Condition;
public Func<MainLoop, bool> Callback;
}
Dictionary<int, Watch> descriptorWatchers = new Dictionary<int, Watch> ();
[DllImport ("libc")]
extern static int poll ([In, Out] Pollfd [] ufds, uint nfds, int timeout);
[DllImport ("libc")]
extern static int pipe ([In, Out] int [] pipes);
[DllImport ("libc")]
extern static int read (int fd, IntPtr buf, IntPtr n);
[DllImport ("libc")]
extern static int write (int fd, IntPtr buf, IntPtr n);
Pollfd [] pollmap;
bool poll_dirty = true;
int [] wakeupPipes = new int [2];
static IntPtr ignore = Marshal.AllocHGlobal (1);
static IntPtr readHandle = Marshal.AllocHGlobal (1);
MainLoop mainLoop;
bool winChanged;
public Action WinChanged;
void IMainLoopDriver.Wakeup ()
{
write (wakeupPipes [1], ignore, (IntPtr)1);
}
void IMainLoopDriver.Setup (MainLoop mainLoop)
{
this.mainLoop = mainLoop;
pipe (wakeupPipes);
AddWatch (wakeupPipes [0], Condition.PollIn, ml => {
var result = read (wakeupPipes [0], ignore, readHandle);
return true;
});
}
/// <summary>
/// Removes an active watch from the mainloop.
/// </summary>
/// <remarks>
/// The token parameter is the value returned from AddWatch
/// </remarks>
public void RemoveWatch (object token)
{
var watch = token as Watch;
if (watch == null)
return;
descriptorWatchers.Remove (watch.File);
}
/// <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>
public object AddWatch (int fileDescriptor, Condition condition, Func<MainLoop, bool> callback)
{
if (callback == null)
throw new ArgumentNullException (nameof (callback));
var watch = new Watch () { Condition = condition, Callback = callback, File = fileDescriptor };
descriptorWatchers [fileDescriptor] = watch;
poll_dirty = true;
return watch;
}
void UpdatePollMap ()
{
if (!poll_dirty)
return;
poll_dirty = false;
pollmap = new Pollfd [descriptorWatchers.Count];
int i = 0;
foreach (var fd in descriptorWatchers.Keys) {
pollmap [i].fd = fd;
pollmap [i].events = (short)descriptorWatchers [fd].Condition;
i++;
}
}
bool IMainLoopDriver.EventsPending (bool wait)
{
UpdatePollMap ();
bool checkTimersResult = CheckTimers (wait, out var pollTimeout);
var n = poll (pollmap, (uint)pollmap.Length, pollTimeout);
if (n == KEY_RESIZE) {
winChanged = true;
}
return checkTimersResult || n >= KEY_RESIZE;
}
bool CheckTimers (bool wait, out int pollTimeout)
{
long now = DateTime.UtcNow.Ticks;
if (mainLoop.timeouts.Count > 0) {
pollTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
if (pollTimeout < 0) {
return true;
}
} else
pollTimeout = -1;
if (!wait)
pollTimeout = 0;
int ic;
lock (mainLoop.idleHandlers) {
ic = mainLoop.idleHandlers.Count;
}
return ic > 0;
}
void IMainLoopDriver.MainIteration ()
{
if (winChanged) {
winChanged = false;
WinChanged?.Invoke ();
}
if (pollmap != null) {
foreach (var p in pollmap) {
Watch watch;
if (p.revents == 0)
continue;
if (!descriptorWatchers.TryGetValue (p.fd, out watch))
continue;
if (!watch.Callback (this.mainLoop))
descriptorWatchers.Remove (p.fd);
}
}
}
}
}