-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnestedterminalemulator.d
453 lines (377 loc) · 12.7 KB
/
nestedterminalemulator.d
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// to compile the magic d demangler:
// dmd nestedterminalemulator.d terminalemulator.d ~/arsd/terminal.d ~/arsd/color.d ~/arsd/eventloop.d -version=with_eventloop -version=d_demangle
/**
This application does our terminal emulation inside another terminal, or the
Windows console.
Extended functions [s]are[/s] should be gracefully degraded as possible.
Linux compile:
dmd nestedterminalemulator.d terminalemulator.d arsd/terminal.d arsd/color.d arsd/eventloop.d -version=with_eventloop
Windows compile:
dmd nestedterminalemulator.d terminalemulator.d arsd\terminal.d arsd\color.d
FIXME: underlining
*/
import arsd.terminal;
//import arsd.extendedterminalemulator;
import arsd.terminalemulator;
import core.stdc.stdio;
import arsd.color;
version(Windows)
import core.sys.windows.windows;
version(Windows) extern(Windows) DWORD WaitForSingleObjectEx( HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable);
version(Windows) extern(Windows) DWORD WaitForMultipleObjectsEx(DWORD nCount,const HANDLE *lpHandles,BOOL bWaitAll,DWORD dwMilliseconds,BOOL bAlertable);
version(use_libssh2)
void main(string[] args) {
import arsd.libssh2;
import std.socket;
void startup(Socket socket, LIBSSH2_SESSION* sshSession, LIBSSH2_CHANNEL* sshChannel) {
// FIXME: finish this
}
if(args.length < 6) {
import std.format : format;
import std.string : toStringz;
auto msg = format("Provide a list of arguments like:\n%s font_size host port username keyfile\nSo for example: %s example.com 0 22 root /path/to/id_rsa\n(font size of 0 means use a system font)\nOn Windows, it might be helpful to create a shortcut with your options specified in the properties sheet command line option.", args[0], args[0]);
import std.stdio;
writeln(msg);
return;
}
string host = args[2];
import std.conv : to;
short port = to!short(args[3]);
string username = args[4];
string keyfile = args[5];
string expectedFingerprint = null;
if(args.length > 6)
expectedFingerprint = args[6];
startChild!startup(host, port, username, keyfile, expectedFingerprint);
}
else version(Windows)
void main(string[] args) {
if(args.length < 2) {
import std.stdio;
writeln("Give a command line to run like: plink.exe [email protected] -i keyfile /opt/serverside");
return;
}
void startup(HANDLE inwritePipe, HANDLE outreadPipe) {
auto terminal = Terminal(ConsoleOutputType.cellular);
auto input = RealTimeConsoleInput(&terminal, ConsoleInputFlags.raw | ConsoleInputFlags.allInputEvents);
SetConsoleMode(terminal.hConsole, ENABLE_PROCESSED_OUTPUT); // no more line wrapping so our last line outputted is cool
SetConsoleMode(input.inputHandle, 0x80 /*ENABLE_EXTENDED_FLAGS*/ | ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT); // disabling processed input so ctrl+c comes through to us
terminal._wrapAround = false;
auto te = new NestedTerminalEmulator(inwritePipe, outreadPipe, &terminal);
loop: while(true) {
if(WaitForSingleObjectEx(input.inputHandle, INFINITE, true) == 0) {
auto event = input.nextEvent();
te.handleEvent(event);
} else {
// apc; the read handler will have already called so we don't have to worry about it
}
if(childDead)
break;
}
}
import std.string;
startChild!startup(null, args[1..$].join(" "));
}
else version(Posix)
void main(string[] args) {
void startup(int master) {
auto terminal = Terminal(ConsoleOutputType.cellular);
auto input = RealTimeConsoleInput(&terminal, ConsoleInputFlags.raw | ConsoleInputFlags.allInputEvents);
terminal._wrapAround = false;
auto te = new NestedTerminalEmulator(master, &terminal);
/*
import core.sys.posix.unistd;
import core.sys.posix.fcntl;
int nullRead = open("/dev/pts/13", O_RDONLY);
int nullWrite = open("/dev/pts/13", O_WRONLY);
assert(nullRead > 0);
assert(nullWrite > 0);
close(0);
close(1);
dup2(nullRead, 0);
dup2(nullWrite, 1);
*/
//te.detach();
import arsd.eventloop;
addListener(&te.handleEvent);
loop();
}
import std.process;
auto cmd = environment.get("SHELL", "/bin/bash");
startChild!startup(args.length > 1 ? args[1] : cmd, args.length > 1 ? args[1 .. $] : [cmd]);
}
class NestedTerminalEmulator : TerminalEmulator {
Terminal* terminal;
//RealTimeConsoleInput* rtInput;
version(Posix)
void detach() {
import core.sys.posix.unistd;
if(fork()) {
// the parent exits, leaving the parent terminal back to normal
static import arsd.eventloop;
arsd.eventloop.exit();
} else {
// while the child kinda floats in limbo
//terminal = null;
}
}
version(Windows)
import core.sys.windows.windows;
version(use_libssh2)
import arsd.libssh2;
version(use_libssh2)
this(LIBSSH2_CHANNEL* sshChannel, Terminal* terminal) {
this.sshChannel = sshChannel;
this.terminal = terminal;
super(terminal.width, terminal.height);
}
else version(Windows)
this(HANDLE stdin, HANDLE stdout, Terminal* terminal) {
this.stdin = stdin;
this.stdout = stdout;
this.terminal = terminal;
//this.rtInput = rtInput;
//this.rtInput = rtInput;
super(terminal.width, terminal.height);
version(Windows) {
overlapped = new OVERLAPPED();
overlapped.hEvent = cast(void*) this;
//window.handleNativeEvent = &windowsRead;
readyToReadWindows(0, 0, overlapped);
redraw();
}
}
else version(Posix)
this(int master, Terminal* terminal) {
this.master = master;
this.terminal = terminal;
//this.rtInput = rtInput;
makeNonBlocking(master);
addFileEventListeners(master, &readyToRead, null, null);
if(terminal)
super(terminal.width, terminal.height);
else
super(80, 25);
}
version(Windows)
override TextAttributes defaultTextAttributes() {
TextAttributes da;
da.foregroundIndex = 256; // terminal.d's Color.DEFAULT
da.backgroundIndex = 256;
import arsd.color;
da.foreground = Color(200, 200, 200);
da.background = Color(0, 0, 0);
return da;
}
void handleEvent(InputEvent event) {
import std.conv;
auto te = this;
final switch(event.type) {
// FIXME: what about Ctrl+Z? maybe terminal.d should catch that signal too. SIGSTOP i think tho could be SIGTSTP
case InputEvent.Type.HangupEvent:
version(with_eventloop)
exit();
break;
case InputEvent.Type.CharacterEvent:
auto ce = event.get!(InputEvent.Type.CharacterEvent);
if(ce.eventType == CharacterEvent.Type.Released)
return;
endScrollback();
char[4] str;
import std.utf;
auto data = str[0 .. encode(str, ce.character)];
te.sendToApplication(data);
break;
case InputEvent.Type.SizeChangedEvent:
auto ce = event.get!(InputEvent.Type.SizeChangedEvent);
te.resizeTerminal(ce.newWidth, ce.newHeight);
break;
case InputEvent.Type.EndOfFileEvent:
// FIXME: is this correct?
te.sendToApplication("\004");
break;
case InputEvent.Type.UserInterruptionEvent:
te.sendToApplication("\003");
break;
case InputEvent.Type.NonCharacterKeyEvent:
auto ev = event.get!(InputEvent.Type.NonCharacterKeyEvent);
if(ev.eventType == NonCharacterKeyEvent.Type.Pressed) {
with(NonCharacterKeyEvent.Key)
if(ev.key == escape)
te.sendToApplication("\033");
else
// this is guaranteed to work since the enum values are the same by design
if(te.sendKeyToApplication(cast(TerminalKey) ev.key,
(ev.modifierState & ModifierState.shift)?true:false,
(ev.modifierState & ModifierState.alt)?true:false,
(ev.modifierState & ModifierState.control)?true:false))
redraw();
}
break;
case InputEvent.Type.PasteEvent:
auto ev = event.get!(InputEvent.Type.PasteEvent);
sendPasteData(ev.pastedText);
break;
case InputEvent.Type.MouseEvent:
auto me = event.get!(InputEvent.Type.MouseEvent);
if(sendMouseInputToApplication(me.x, me.y,
cast(arsd.terminalemulator.MouseEventType) me.eventType,
cast(arsd.terminalemulator.MouseButton) me.buttons,
(me.modifierState & ModifierState.shift) ? true : false,
(me.modifierState & ModifierState.control) ? true : false,
(me.modifierState & ModifierState.alt) ? true : false
))
redraw();
break;
case InputEvent.Type.CustomEvent:
break;
}
}
version(Windows) {
protected override void changeWindowIcon(IndexedImage t) {
if(t !is null) {
// FIXME: i might be able to change this with GetConsoleWindow
}
}
protected override void changeIconTitle(string) {} // doesn't matter
protected override void changeTextAttributes(TextAttributes) {} // ditto
protected override void soundBell() {
if(terminal)
terminal.writeStringRaw("\007");
}
protected override void copyToClipboard(string text) {
arsd.simpledisplay.setClipboardText(simpleWindowConsole, text);
}
protected override void pasteFromClipboard(void delegate(in char[]) dg) {
arsd.simpledisplay.getClipboardText(simpleWindowConsole, dg);
}
protected override void changeCursorStyle(CursorStyle s) {
}
} else {
void writeRaw(in char[] s) {
if(terminal)
terminal.writeStringRaw(s);
}
mixin ForwardVirtuals!writeRaw;
}
protected override void changeWindowTitle(string t) {
if(terminal && t.length)
terminal.setTitle(t);
}
version(Windows) {
static import arsd.simpledisplay; // this is for copy/paste
private arsd.simpledisplay.SimpleWindow _simpleWindowConsole;
protected arsd.simpledisplay.SimpleWindow simpleWindowConsole() {
if(_simpleWindowConsole is null) {
auto handle = arsd.simpledisplay.GetConsoleWindow();
_simpleWindowConsole = new arsd.simpledisplay.SimpleWindow(handle);
}
return _simpleWindowConsole;
}
}
bool debugMode;
mixin PtySupport!(doNothing);
version(d_demangle) {
void readyToRead(int fd) {
import core.sys.posix.unistd;
ubyte[4096] buffer;
int len = read(fd, buffer.ptr, 4096);
if(len < 0)
throw new Exception("read failed");
auto data = buffer[0 .. len];
import std.regex;
char[] dem(Captures!(char[], uint) m) {
import core.demangle;
return "\033[1m" ~ demangle(m.hit) ~ "\033[22m";
}
data = cast(typeof(data)) replace!dem(cast(char[]) data, regex("_D[a-zA-Z0-9_]+", "g"));
super.sendRawInput(data);
redraw();
}
}
version(Posix)
import arsd.eventloop;
bool lastDrawAlternativeScreen;
// FIXME: a lot of code duplication between this and detachable
void redraw(bool forceRedraw = false) {
int x, y;
if(terminal is null)
return;
if(cursorShowing)
terminal.autoHideCursor();
else
terminal.hideCursor();
foreach(idx, ref cell; alternateScreenActive ? alternateScreen : normalScreen) {
ushort tfg, tbg;
if(!forceRedraw && !cell.invalidated && lastDrawAlternativeScreen == alternateScreenActive) {
goto skipDrawing;
}
cell.invalidated = false;
//auto bg = (cell.attributes.inverse != reverseVideo) ? cell.attributes.foreground : cell.attributes.background;
//auto fg = (cell.attributes.inverse != reverseVideo) ? cell.attributes.background : cell.attributes.foreground;
{
import t = terminal;
// we always work with indexes, so the fallback flag is irrelevant here
tbg = cell.attributes.backgroundIndex & ~0x8000;
tfg = cell.attributes.foregroundIndex & ~0x8000;
version(Windows) {
ushort b, r;
if(tfg != 256) {
b = tfg & 1;
r = (tfg & 4) >> 2;
tfg &= 0b0000_0010;
tfg |= b << 2;
tfg |= r;
}
if(tbg != 256) {
b = tbg & 1;
r = (tbg & 4) >> 2;
tbg &= 0b0000_0010;
tbg |= b << 2;
tbg |= r;
}
}
if(cell.attributes.bold)
tfg |= t.Bright;
}
if(cell.ch != dchar.init) {
char[4] str;
import std.utf;
try {
auto stride = encode(str, cell.ch);
// on Windows, we hacked the mode above so terminal.d doesn't track the console correctly
// see also for a potential improvement:
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms687404%28v=vs.85%29.aspx
terminal.moveTo(x, y);
bool reverse = cell.attributes.inverse != reverseVideo; /* != == ^ btw */
if(cell.selected)
reverse = !reverse;
// reducing it to 16 color
// FIXME: this sucks, it should do something more sane for palette support like findNearestColor()
// or even reducing our palette and changing the console palette in Windows for best results
// and xterm 256 color too can just forward it. and of course if we're nested in ourselves, we can just use
// a 24 bit extension command.
tfg &= 0xff0f;
terminal.color(tfg, tbg, ForceOption.automatic, reverse);
terminal.underline = cell.attributes.underlined;
terminal.write(cast(immutable) str[0 .. stride]);
} catch(Exception e) {
}
} else if(cell.nonCharacterData !is null) {
// something maybe
}
skipDrawing:
x++;
if(x == screenWidth) {
x = 0;
y++;
}
}
if(cursorShowing) {
terminal.moveTo(cursorX, cursorY);
terminal.autoShowCursor();
}
lastDrawAlternativeScreen = alternateScreenActive;
terminal.flush();
}
}