-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathTerminalView.cs
413 lines (374 loc) · 10.3 KB
/
TerminalView.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using System;
using Terminal.Gui;
using XtermSharp;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace GuiCsHost {
public class TerminalView : View, ITerminalDelegate {
internal XtermSharp.Terminal terminal;
bool cursesDriver = Application.Driver.GetType ().Name.IndexOf ("CursesDriver") != -1;
bool terminalSupportsUtf8;
public TerminalView ()
{
terminal = new XtermSharp.Terminal (this, new TerminalOptions () { Cols = 80, Rows = 25 });
CanFocus = true;
if (cursesDriver)
terminalSupportsUtf8 = Environment.GetEnvironmentVariable ("LANG").IndexOf ("UTF-8", StringComparison.OrdinalIgnoreCase) != -1;
else
terminalSupportsUtf8 = true;
}
public override Rect Frame {
get => base.Frame;
set {
base.Frame = value;
SetNeedsDisplay ();
if (value.Width != terminal.Cols || value.Height != terminal.Rows) {
terminal.Resize (value.Width, value.Height);
}
TerminalSizeChanged?.Invoke (value.Width, value.Height);
}
}
/// <summary>
/// This event is raised when the terminal size has change, due to a Gui.CS frame changed.
/// </summary>
public event Action<int, int> TerminalSizeChanged;
public override bool ProcessKey (KeyEvent keyEvent)
{
switch (keyEvent.Key) {
case Key.Esc:
Send (0x1b);
break;
case Key.Space:
Send (0x20);
break;
case Key.DeleteChar:
Send (EscapeSequences.CmdDelKey);
break;
case Key.Backspace:
Send (0x7f);
break;
case Key.CursorUp:
Send (terminal.ApplicationCursor ? EscapeSequences.MoveUpApp : EscapeSequences.MoveUpNormal);
break;
case Key.CursorDown:
Send (terminal.ApplicationCursor ? EscapeSequences.MoveDownApp : EscapeSequences.MoveDownNormal);
break;
case Key.CursorLeft:
Send (terminal.ApplicationCursor ? EscapeSequences.MoveLeftApp : EscapeSequences.MoveLeftNormal);
break;
case Key.CursorRight:
Send (terminal.ApplicationCursor ? EscapeSequences.MoveRightApp : EscapeSequences.MoveRightNormal);
break;
case Key.PageUp:
if (terminal.ApplicationCursor)
Send (EscapeSequences.CmdPageUp);
else {
// TODO: view should scroll one page up.
}
break;
case Key.PageDown:
if (terminal.ApplicationCursor)
Send (EscapeSequences.CmdPageDown);
else {
// TODO: view should scroll one page down
}
break;
case Key.Home:
Send (terminal.ApplicationCursor ? EscapeSequences.MoveHomeApp : EscapeSequences.MoveHomeNormal);
break;
case Key.End:
Send (terminal.ApplicationCursor ? EscapeSequences.MoveEndApp : EscapeSequences.MoveEndNormal);
break;
case Key.InsertChar:
break;
case Key.F1:
Send (EscapeSequences.CmdF [0]);
break;
case Key.F2:
Send (EscapeSequences.CmdF [1]);
break;
case Key.F3:
Send (EscapeSequences.CmdF [2]);
break;
case Key.F4:
Send (EscapeSequences.CmdF [3]);
break;
case Key.F5:
Send (EscapeSequences.CmdF [4]);
break;
case Key.F6:
Send (EscapeSequences.CmdF [5]);
break;
case Key.F7:
Send (EscapeSequences.CmdF [6]);
break;
case Key.F8:
Send (EscapeSequences.CmdF [7]);
break;
case Key.F9:
Send (EscapeSequences.CmdF [8]);
break;
case Key.F10:
Send (EscapeSequences.CmdF [9]);
break;
case Key.BackTab:
Send (EscapeSequences.CmdBackTab);
break;
default:
if (keyEvent.Key >= Key.ControlA && keyEvent.Key <= Key.ControlZ) {
Send ((byte)keyEvent.Key);
break;
}
if (keyEvent.IsAlt) {
Send (0x1b);
}
var rune = (Rune)(uint)keyEvent.Key;
var len = Rune.RuneLen (rune);
if (len > 0) {
var buff = new byte [len];
var n = Rune.EncodeRune (rune, buff);
Send (buff);
} else {
Send ((byte)keyEvent.Key);
}
break;
}
return true;
}
void SetAttribute (int attribute)
{
int bg = attribute & 0x1ff;
int fg = (attribute >> 9) & 0x1ff;
var flags = (FLAGS)(attribute >> 18);
if (cursesDriver) {
var cattr = 0;
if (fg == Renderer.DefaultColor && bg == Renderer.DefaultColor)
cattr = 0;
else {
if (fg == Renderer.DefaultColor)
fg = (short)ConsoleColor.Gray;
if (bg == Renderer.DefaultColor)
bg = (short)ConsoleColor.Black;
Driver.SetColors ((ConsoleColor)fg, (ConsoleColor)bg);
return;
}
if (flags.HasFlag (FLAGS.BOLD))
cattr |= 0x200000; // A_BOLD
if (flags.HasFlag (FLAGS.INVERSE))
cattr |= 0x40000; // A_REVERSE
if (flags.HasFlag (FLAGS.BLINK))
cattr |= 0x80000; // A_BLINK
if (flags.HasFlag (FLAGS.DIM))
cattr |= 0x100000; // A_DIM
if (flags.HasFlag (FLAGS.UNDERLINE))
cattr |= 0x20000; // A_UNDERLINE
if (flags.HasFlag (FLAGS.ITALIC))
cattr |= 0x10000; // A_STANDOUT
Driver.SetAttribute (new Terminal.Gui.Attribute (cattr));
} else {
Driver.SetAttribute (ColorScheme.Normal);
}
}
public override void Redraw (Rect region)
{
Driver.SetAttribute (ColorScheme.Normal);
Clear ();
var maxCol = Frame.Width;
var maxRow = Frame.Height;
var yDisp = terminal.Buffer.YDisp;
for (int row = 0; row < maxRow; row++) {
Move (Frame.X, Frame.Y + row);
if (row >= terminal.Rows)
continue;
var line = terminal.Buffer.Lines [row+yDisp];
for (int col = 0; col < maxCol; col++) {
var ch = line [col];
SetAttribute (ch.Attribute);
Rune r;
if (ch.Code == 0)
r = ' ';
else {
if (terminalSupportsUtf8)
r = ch.Rune;
else {
switch (ch.Code) {
case 0:
r = ' ';
break;
case 0x2518: // '┘'
r = 0x40006a; // ACS_LRCORNER;
break;
case 0x2510: // '┐'
r = 0x40006b; // ACS_URCORNER;
break;
case 0x250c: // '┌'
r = 0x40006c; // ACS_ULCORNER;
break;
case 0x2514: // '└'
r = 0x40006d; // ACS_LLCORNER;
break;
case 0x253c: // '┼'
r = 0x40006e; // ACS_PLUS;
break;
case 0x23ba: // '⎺'
case 0x23bb: // '⎻'
case 0x2500: // '─'
case 0x23bc: // '⎼'
case 0x23bd: // '⎽'
r = 0x400071; // ACS_VLINE
break;
case 0x251c: // '├'
r = 0x400074; // ACS_LTEE
break;
case 0x2524: // '┤'
r = 0x400075; // ACS_RTEE
break;
case 0x2534: // '┴'
r = 0x400076; // ACS_BTEE
break;
case 0x252c: // '┬'
r = 0x400077; // ACS_TTEE
break;
case 0x2502: // '│'
r = 0x400078; // ACS_VLINE
break;
default:
r = ch.Rune;
break;
}
}
}
AddRune (col, row, r);
}
}
PositionCursor ();
}
public override bool MouseEvent (MouseEvent mouseEvent)
{
if (terminal.MouseEvents) {
var f = mouseEvent.Flags;
int button = -1;
if (f.HasFlag (MouseFlags.Button1Clicked))
button = 0;
if (f.HasFlag (MouseFlags.Button2Clicked))
button = 1;
if (f.HasFlag (MouseFlags.Button3Clicked))
button = 2;
if (button != -1){
var e = terminal.EncodeButton (button, release: false, shift: false, meta: false, control: false);
terminal.SendEvent (e, mouseEvent.X, mouseEvent.Y);
if (terminal.MouseSendsRelease) {
e = terminal.EncodeButton (button, release: true, shift: false, meta: false, control: false);
terminal.SendEvent (e, mouseEvent.X, mouseEvent.Y);
}
return true;
}
} else {
// Not currently handled
}
return false;
}
public Action<byte []> UserInput;
byte [] miniBuf = new byte [1];
void Send (byte b)
{
miniBuf [0] = b;
Send (miniBuf);
}
public void Send (byte [] data)
{
UserInput?.Invoke (data);
}
public void SetTerminalTitle (XtermSharp.Terminal source, string title)
{
//
}
public void SetTerminalIconTitle (XtermSharp.Terminal source, string title) { }
public void ShowCursor (XtermSharp.Terminal source)
{
//
}
public void SizeChanged (XtermSharp.Terminal source)
{
// Triggered by the terminal
}
public string WindowCommand (XtermSharp.Terminal source, WindowManipulationCommand command, params int [] args)
{
return null;
}
public override void PositionCursor ()
{
Move (terminal.Buffer.X, terminal.Buffer.Y);
}
bool UpdateDisplay (Mono.Terminal.MainLoop mainloop)
{
terminal.GetUpdateRange (out var rowStart, out var rowEnd);
terminal.ClearUpdateRange ();
var cols = terminal.Cols;
var tb = terminal.Buffer;
SetNeedsDisplay (new Rect (0, rowStart, Frame.Width, rowEnd+1));
//SetNeedsDisplay ();
pendingDisplay = false;
return false;
}
bool pendingDisplay;
void QueuePendingDisplay ()
{
// throttle
if (!pendingDisplay) {
pendingDisplay = true;
Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (1), UpdateDisplay);
}
}
public void Feed (byte [] buffer, int n)
{
terminal.Feed (buffer, n);
QueuePendingDisplay ();
}
}
public class SubprocessTerminalView : TerminalView {
int ptyFd;
int childPid;
void SendDataToChild (byte [] data)
{
unsafe {
fixed (byte* p = &data [0]) {
var n = Mono.Unix.Native.Syscall.write (ptyFd, (void*)((IntPtr)p), (ulong)data.Length);
}
}
}
void NotifyPtySizeChanged (int cols, int rows)
{
UnixWindowSize nz = new UnixWindowSize ();
nz.col = (short) Frame.Width;
nz.row = (short) Frame.Height;
var res = Pty.SetWinSize (ptyFd, ref nz);
}
public SubprocessTerminalView ()
{
var size = new UnixWindowSize () {
col = (short) terminal.Cols,
row = (short) terminal.Rows,
};
childPid = Pty.ForkAndExec ("/bin/bash", new string [] { "/bin/bash" }, XtermSharp.Terminal.GetEnvironmentVariables ("xterm"), out ptyFd, size);
var unixMainLoop = Application.MainLoop.Driver as Mono.Terminal.UnixMainLoop;
unixMainLoop.AddWatch (ptyFd, Mono.Terminal.UnixMainLoop.Condition.PollIn, PtyReady);
this.UserInput = SendDataToChild;
this.TerminalSizeChanged += NotifyPtySizeChanged;
}
byte [] buffer = new byte [8192];
bool PtyReady (Mono.Terminal.MainLoop mainloop)
{
unsafe {
long n;
fixed (byte* p = &buffer[0]) {
n = Mono.Unix.Native.Syscall.read (ptyFd, (void*)((IntPtr)p), (ulong)buffer.Length);
Debug.Print(System.Text.Encoding.UTF8.GetString (buffer, 0, (int) n));
Feed (buffer, (int)n);
}
}
return true;
}
}
}