-
Notifications
You must be signed in to change notification settings - Fork 10
/
attach.d
1303 lines (1112 loc) · 37 KB
/
attach.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// FIXME: catch the suspend signal (ctrl+z) and forward it inside
// FIXME: i should be able to change the title from the outside too
//
// FIXME:some condition can make it spin 100% cpu and allocate a bunch of memory and idk what.
/++
This is a GNU Screen style program to multiplex and provide remote attach/detach
support to a terminal emulator backend.
It works with two pieces: sessions and screens. A session is a collection of screens
and a screen is one backend terminal emulator.
attach must be run inside a terminal emulator itself. For best results, use the GUI
frontend provided in this package, but it will also work on most others like the linux
console or xterm. You can also use a nested terminal emulator with it if you want.
Controls by default are based on screen. Differences are:
C-a D: detach the current screen from the session
C-a C: attach a specific screen to the session
C-a t: toggle taskbar. The taskbar will show a tab in green if it has a beep.
C-a <colon>: start command line
attach socket_name
You can edit session files in ~/.detachable-terminals with a text editor.
+/
module arsd.terminalemulatorattachutility;
import arsd.detachableterminalemulatormessage;
import arsd.terminal;
static import std.file;
static import core.stdc.stdlib;
import std.conv;
import std.socket;
/*
FIXME: error messages aren't displayed when you attach and the socket cannot be opened
FIXME: activity/silence watching should make sense out of the box so default on doesn't annoy me
FIXME: when a session is remote attached, it steals all the screens. this causes the existing one
to write out a blank session file, meaning it won't be stolen back easily. This should change
so it is aware of the remote detach and just leaves things, or gracefully exits upon request.
fun facts:
SIGTSTP is the one we catch
SIGCONT is the one to send to make it continue
Session file example:
title: foo
icon: bar.png
cwd: /home/me
screens: foo bar baz
FIXME: support icon changing and title changing and DEMANDS_ATTENTION support
FIXME: if a screen detaches because it is being attached somewhere else, we should note that somehow
FIXME: support the " key
FIXME: watch for change in silence automatically
FIXME: allow suppression of beeps
so what do i want for activity noticing...
if it has been receiving user input, give it a bigger time out
if there's been no output for a minute, consider it silent
if there has been output in the last minute, it is not silent
if it is less than a minute old, it is neither silent nor loud yet
*/
struct Session {
import core.sys.posix.sys.types;
pid_t pid; // a hacky way to keep track of who has this open right now for remote detaching
// preserved in the file
string title;
string icon;
string cwd;
string[] screens;
string[] screensTitlePrefixes;
string autoCommand;
dchar escapeCharacter = 1;
bool showingTaskbar = true;
/// if set to true, screens are counted from zero when jumping with the number keys (like GNU screen)
/// otherwise, screens count from one. I like one because it is more convenient with the left hand
/// and the numbers match up visually with the tabs, but the zero based gnu screen habit is hard to break.
bool zeroBasedCounting;
// the filename
string sname;
bool mouseTrackingOn;
// not preserved in the file
ChildTerminal[] children;
int activeScreen;
void saveToFile() {
import std.stdio;
import std.string;
if(sname.length == 0) return;
auto file = File(socketDirectoryName() ~ "/" ~ sname ~ ".session", "wt");
file.writeln("title: ", title);
file.writeln("icon: ", icon);
file.writeln("cwd: ", cwd);
file.writeln("autoCommand: ", autoCommand);
file.writeln("escapeCharacter: ", cast(dchar) (escapeCharacter + 'a' - 1));
file.writeln("showingTaskbar: ", showingTaskbar);
file.writeln("zeroBasedCounting: ", zeroBasedCounting);
file.writeln("pid: ", pid);
file.writeln("activeScreen: ", activeScreen);
file.writeln("screens: ", join(screens, " "));
file.writeln("screensTitlePrefixes: ", join(screensTitlePrefixes, "; "));
}
void readFromFile() {
import std.stdio;
import std.string;
import std.file;
if(sname.length == 0) return;
if(!std.file.exists(socketDirectoryName() ~ "/" ~ sname ~ ".session"))
return;
auto file = File(socketDirectoryName() ~ "/" ~ sname ~ ".session", "rt");
foreach(line; file.byLine) {
auto idx = indexOf(line, ":");
if(idx == -1)
continue;
auto lhs = strip(line[0 .. idx]);
auto rhs = strip(line[idx + 1 .. $]);
switch(lhs) {
case "title": title = rhs.idup; break;
case "cwd": cwd = rhs.idup; break;
case "autoCommand": autoCommand = rhs.idup; break;
case "icon": icon = rhs.idup; break;
case "escapeCharacter":
import std.utf;
escapeCharacter = decodeFront(rhs) + 1 - 'a';
break;
case "showingTaskbar": showingTaskbar = rhs == "true"; break;
case "zeroBasedCounting": zeroBasedCounting = rhs == "true"; break;
case "pid": pid = to!int(rhs); break;
case "activeScreen": activeScreen = to!int(rhs); break;
case "screens": screens = split(rhs.idup, " "); break;
case "screensTitlePrefixes": screensTitlePrefixes = split(line[idx + 1 .. $].stripLeft.idup, "; "); break;
default: continue;
}
}
}
void saveUpdatedSessionToFile() {
if(this.sname !is null) {
this.screens = null;
this.screensTitlePrefixes = null;
foreach(child; this.children) {
if(child.socket !is null) {
this.screens ~= child.socketName;
} else {
this.screens ~= "[vacant]";
}
this.screensTitlePrefixes ~= child.titlePrefix;
}
this.saveToFile();
}
}
}
import core.stdc.time;
struct ChildTerminal {
Socket socket;
string title;
string socketName;
// tab image
string titlePrefix;
bool demandsAttention;
// for mouse click detection
int x;
int x2;
// for detecting changes in output
time_t lastActivity;
bool lastWasSilent;
}
extern(C) nothrow static @nogc
void detachable_child_dead(int) {
import core.sys.posix.sys.wait;
wait(null);
}
bool stopRequested;
extern(C) nothrow static @nogc
void stop_requested(int) {
stopRequested = true;
}
bool debugMode;
bool outputPaused;
int previousScreen = 0;
bool running = true;
Socket socket;
void main(string[] args) {
Session session;
if(args.length > 1 && args[1] != "--list" && args[1] != "--cleanup") {
import std.algorithm : endsWith;
if(args.length == 2 && !endsWith(args[1], ".socket")) {
// load the given argument as a session
session.sname = args[1];
} else {
// make an anonymous session with the listed screens as sockets
foreach(arg; args[1 .. $])
session.screens ~= arg;
}
} else {
// list the available sockets and sessions...
import std.file, std.process, std.stdio;
bool found = false;
auto dirName = socketDirectoryName();
string[] sessions;
string[] sockets;
foreach(string name; dirEntries(dirName, SpanMode.shallow)) {
name = name[dirName.length + 1 .. $];
if(name[$-1] == 'n')
sessions ~= name[0 .. $ - ".session".length];
else
sockets ~= name[0 .. $ - ".socket".length];
found = true;
}
if(found) {
string[string] associations;
foreach(sessionName; sessions) {
auto sess = Session();
sess.sname = sessionName;
sess.readFromFile();
foreach(s; sess.screens)
associations[s] = sessionName;
if(args.length == 2 && args[1] == "--cleanup") {
} else
writefln("%20s\t%d\t%d", sessionName, sess.pid, sess.screens.length);
}
foreach(socketName; sockets) {
if(args.length == 2 && args[1] == "--cleanup") {
if(socketName !in associations) {
import core.stdc.stdlib;
static import std.file;
if(std.file.exists("/proc/" ~ socketName))
system(("attach " ~ socketName ~ ".socket" ~ "\0").ptr);
else
std.file.remove(socketDirectoryName() ~ "/" ~ socketName ~ ".socket");
}
} else {
writefln("%s.socket\t\t%s", socketName, (socketName in associations) ? associations[socketName] : "[detached]");
static import std.file;
if(std.file.exists("/proc/" ~ socketName)) {
auto newSocket = connectTo(socketName, false);
if(newSocket) {
sendSimpleMessage(newSocket, InputMessage.Type.RequestStatus);
char[1024] buffer;
auto read = newSocket.receive(buffer[]);
while(read > 0) {
writef("%s", buffer[0 .. read]);
read = newSocket.receive(buffer[]);
}
newSocket.close();
}
}
}
}
} else {
writeln("No screens found");
}
return;
}
import core.sys.posix.signal;
signal(SIGPIPE, SIG_IGN);
signal(SIGCHLD, &detachable_child_dead);
signal(SIGTSTP, &stop_requested);
import std.process;
// FIXME: set up a FIFO or something so we can receive commands
// pertaining to the whole session like detach... or something.
// if we do that it will need a way to find it by session name
// or by pid. Maybe a symlink.
session.cwd = std.file.getcwd();
session.title = session.sname;
session.readFromFile();
if(session.pid) {
// detach the old session
kill(session.pid, SIGHUP);
import core.sys.posix.unistd;
usleep(1_000_000); // give the old process a chance to die
session.readFromFile();
}
session.pid = thisProcessID();
if(session.cwd.length)
std.file.chdir(session.cwd);
bool good = false;
foreach(idx, sname; session.screens) {
if(sname == "[vacant]") {
session.children ~= ChildTerminal(null, sname, sname, idx < session.screensTitlePrefixes.length ? session.screensTitlePrefixes[idx] : null);
continue;
}
auto socket = connectTo(sname);
if(socket is null)
sname = "[failed]";
else {
good = true;
sendSimpleMessage(socket, InputMessage.Type.Attach);
}
session.children ~= ChildTerminal(socket, sname, sname, idx < session.screensTitlePrefixes.length ? session.screensTitlePrefixes[idx] : null);
// we should scan inactive sockets for:
// 1) a bell
// 2) a title change
// 3) a window icon change
// as these can all be reflected in the tab listing
}
if(session.children.length == 0)
session.children ~= ChildTerminal(null, null, null, null);
assert(session.children.length);
if(!good) {
if(session.children[0].socketName == "[vacant]" || session.children[0].socketName == "[failed]")
session.children[0].socketName = null;
session.children[0].socket = connectTo(session.children[0].socketName);
if(auto socket = session.children[0].socket) {
sendSimpleMessage(socket, InputMessage.Type.Attach);
if(session.autoCommand.length)
sendSimulatedInput(socket, session.autoCommand);
}
}
session.saveUpdatedSessionToFile(); // saves the new PID
// doing these just to get it in the state i want
auto terminal = Terminal(ConsoleOutputType.cellular);
scope(exit) terminal.cursor(TerminalCursor.DEFAULT, ForceOption.alwaysSend);
auto input = RealTimeConsoleInput(&terminal, ConsoleInputFlags.raw | ConsoleInputFlags.allInputEvents);
try {
// We got a bit beyond what Terminal does and also disable kernel flow
// control, allowing us to capture ^S and ^Q too.
// There's no need to reset at end of scope btw because RealTimeConsoleInput's dtor
// resets to original state, saved before we make this change anyway.
{
import core.sys.posix.termios;
// padding because I'm not sure druntime's termios is the correct size
ubyte[128] padding;
termios old;
ubyte[128] padding2;
tcgetattr(0 /* terminal.fdIn */, &old);
old.c_iflag &= ~(IXON | IXOFF | ISIG);
old.c_cc[VQUIT] = 0; // disable the ctrl+\ signal so it can be handled on the inner layer too
tcsetattr(0, TCSANOW, &old);
}
// then all we do is forward data to/from stdin to the pipe
import core.stdc.errno;
import core.sys.posix.unistd;
import core.sys.posix.sys.select;
if(session.activeScreen < session.children.length && session.children[session.activeScreen].socket !is null) {
setActiveScreen(&terminal, session, cast(int) session.activeScreen, true);
} else {
foreach(idx, child; session.children)
if(child.socket !is null) {
setActiveScreen(&terminal, session, cast(int) idx, true);
break;
}
}
if(socket is null)
return;
// sets the icon, if set
if(session.icon.length) {
import arsd.terminalextensions;
changeWindowIcon(&terminal, session.icon);
}
while(running) {
if(stopRequested) {
stopRequested = false;
InputMessage im;
im.type = InputMessage.Type.CharacterPressed;
im.characterEvent.character = 26; // ctrl+z
im.eventLength = im.sizeof;
write(socket.handle, &im, im.eventLength);
}
terminal.flush();
ubyte[4096] buffer;
fd_set rdfs;
FD_ZERO(&rdfs);
FD_SET(0, &rdfs);
int maxFd = 0;
foreach(child; session.children) {
if(child.socket !is null) {
FD_SET(child.socket.handle, &rdfs);
if(child.socket.handle > maxFd)
maxFd = child.socket.handle;
}
}
timeval timeout;
timeout.tv_sec = 10;
auto ret = select(maxFd + 1, &rdfs, null, null, &timeout);
if(ret == -1) {
if(errno == 4) { // EAGAIN
while(running && (interrupted || windowSizeChanged || hangedUp))
handleEvent(&terminal, session, input.nextEvent(), socket);
continue; // EINTR
}
else throw new Exception("select");
}
bool redrawTaskbar = false;
if(ret) {
if(FD_ISSET(0, &rdfs)) {
// the terminal is ready, we'll call next event here
while(running && input.anyInput_internal())
handleEvent(&terminal, session, input.nextEvent(), socket);
}
foreach(ref child; session.children) if(child.socket !is null) {
if(FD_ISSET(child.socket.handle, &rdfs)) {
// data from the pty should be forwarded straight out
auto len = read(child.socket.handle, buffer.ptr, cast(int) 2);
if(len <= 0) {
// probably end of file or something cuz the child exited
// we should switch to the next possible screen
//throw new Exception("closing cuz of bad read " ~ to!string(errno) ~ " " ~ to!string(len));
closeSocket(&terminal, session, child.socket);
continue;
}
assert(len == 2); // should be a frame
// unpack the frame
OutputMessageType messageType = cast(OutputMessageType) buffer[0];
ubyte messageLength = buffer[1];
if(messageLength) {
// unpack the message
int where = 0;
while(where < messageLength) {
len = read(child.socket.handle, buffer.ptr + where, messageLength - where);
if(len <= 0) assert(0);
where += len;
}
assert(where == messageLength);
}
void handleDataFromTerminal() {
/* read just for stuff in the background like bell or title change */
int lastEsc = -1;
int cut1 = 0, cut2 = 0;
foreach(bidx, b; buffer[0 .. messageLength]) {
if(b == '\033')
lastEsc = cast(int) bidx;
if(b == '\007') {
if(lastEsc != -1) {
auto pieces = cast(char[]) buffer[lastEsc .. bidx];
cut1 = lastEsc;
cut2 = 0;
lastEsc = -1;
// anything longer is just unreasonable
if(pieces.length > 4 && pieces.length < 120)
if(pieces[1] == ']' && pieces[2] == '0' && pieces[3] == ';') {
child.title = pieces[4 .. $].idup;
redrawTaskbar = true;
cut2 = cast(int) bidx;
}
}
if(child.socket !is socket) {
child.demandsAttention = true;
redrawTaskbar = true;
}
}
}
// activity on the active screen needs to be forwarded
// to the actual terminal so the user can see it too
if(!outputPaused && child.socket is socket) {
void writeOut(ubyte[] toWrite) {
int len = cast(int) toWrite.length;
while(len > 0) {
if(!debugMode) {
auto wrote = write(1, toWrite.ptr, len);
if(wrote <= 0)
throw new Exception("write");
toWrite = toWrite[wrote .. $];
len -= wrote;
} else {import std.stdio; writeln(to!string(buffer[0..len])); len = 0;}
}
}
// FIXME
if(false && cut2 > cut1) {
// cut1 .. cut2 should be sliced out of the final output
// a title change isn't necessarily desirable directly since
// we do it in the session
writeOut(buffer[0 .. cut1]);
writeOut(buffer[cut2 + 1 .. messageLength]);
} else {
writeOut(buffer[0 .. messageLength]);
}
}
/+
/* there's still new activity here */
if(child.lastWasSilent && child.lastActivity) {
child.demandsAttention = true;
redrawTaskbar = true;
child.lastWasSilent = false;
}
child.lastActivity = time(null);
+/
}
final switch(messageType) {
case OutputMessageType.NULL:
// should never happen
assert(0);
//break;
case OutputMessageType.dataFromTerminal:
handleDataFromTerminal();
break;
case OutputMessageType.remoteDetached:
// FIXME: this should be done on a session level
// but the idea is if one is remote detached, they all are,
// so we should just terminate immediately as to not write a new file
return;
//break;
case OutputMessageType.mouseTrackingOn:
session.mouseTrackingOn = true;
break;
case OutputMessageType.mouseTrackingOff:
session.mouseTrackingOn = false;
break;
}
} else {
/+
/* there was not any new activity, see if it has become silent */
if(child.lastActivity && !child.lastWasSilent && time(null) - child.lastActivity > 10) {
child.demandsAttention = true;
child.lastWasSilent = true;
redrawTaskbar = true;
}
+/
}
}
} else {
/+
// it timed out, everybody is silent now
foreach(ref child; session.children) {
if(!child.lastWasSilent && child.lastActivity) {
child.demandsAttention = true;
child.lastWasSilent = true;
redrawTaskbar = true;
}
}
+/
}
if(redrawTaskbar)
drawTaskbar(&terminal, session);
}
session.pid = 0; // we're terminating, don't keep the pid anymore
session.saveUpdatedSessionToFile();
} catch(Throwable t) {
terminal.writeln("\n\n\n", t);
input.getch();
input.getch();
input.getch();
input.getch();
input.getch();
}
}
Socket connectTo(ref string sname, in bool spawn = true) {
Socket socket;
if(sname.length) {
try {
socket = new Socket(AddressFamily.UNIX, SocketType.STREAM);
socket.connect(new UnixAddress(socketFileName(sname)));
} catch(Exception e) {
socket = null;
}
}
if(socket is null && spawn) {
// if it can't connect, we'll spawn a new backend to control it
import core.sys.posix.unistd;
if(auto pid = fork()) {
import std.conv;
if(sname.length == 0)
sname = to!string(pid);
int tries = 3;
while(tries > 0 && socket is null) {
// give the child a chance to get started...
import core.thread;
Thread.sleep(dur!"msecs"(25));
// and try to connect
socket = connectTo(sname, false);
tries--;
}
} else {
// child
import core.sys.posix.fcntl;
auto n = open("/dev/null", O_RDONLY);
auto n2 = open("/dev/null", O_WRONLY);
assert(n >= 0);
import core.stdc.errno;
assert(n2 >= 0, to!string(errno));
dup2(n, 0);
dup2(n2, 1);
close(n);
close(n2);
// also detach from the calling foreground process group
// because otherwise SIGINT will be sent to this too and kill
// it instead of just going to the parent and being translated
// into a ctrl+c input for the child.
setpgid(0, 0);
if(true) {
// and run the detachable backend now
{
// If we don't do this, events will get mixed up because
// the pipes handles would be inherited across fork.
import arsd.eventloop;
openNewEventPipes();
}
// also changing the command line as seen in the shell ps command
import core.runtime;
auto cArgs = Runtime.cArgs;
if(cArgs.argc) {
import core.stdc.string;
auto toOverwritePtr = cArgs.argv[0];
auto toOverwrite = toOverwritePtr[0 .. strlen(toOverwritePtr) + 1];
toOverwrite[] = 0;
auto newName = "ATTACH";
if(newName.length > toOverwrite.length - 1)
newName = newName[0 .. toOverwrite.length - 1]; // leave room for a 0 terminator
toOverwrite[0 .. newName.length] = newName[];
}
// and calling main
import arsd.detachableterminalemulator;
try {
detachableMain(["ATTACH", sname]);
} catch(Throwable t) {
}
core.stdc.stdlib.exit(0); // we want this process dead like it would be with exec()
}
// alternatively, but this requires a separate binary:
// compile it separately with -version=standalone_detachable if you want it (is better for debugging btw) then use this code
/*
auto proggie = "/home/me/program/terminal-emulator/detachable";
if(execl(proggie.ptr, proggie.ptr, (sname ~ "\0").ptr, 0))
throw new Exception("wtf " ~ to!string(errno));
*/
}
}
return socket;
}
void drawTaskbar(Terminal* terminal, ref Session session) {
static string lastTitleDrawn;
bool anyDemandAttention = false;
if(session.showingTaskbar) {
terminal.writeStringRaw("\0337"); // save cursor
scope(exit) {
terminal.writeStringRaw("\0338"); // restore cursor
}
int spaceRemaining = terminal.width;
terminal.moveTo(0, terminal.height - 1);
//terminal.writeStringRaw("\033[K"); // clear line
terminal.color(Color.blue, Color.white, ForceOption.alwaysSend, true);
terminal.write(" "); //"+ ");
spaceRemaining-=2;
spaceRemaining--; // save space for the close button on the end
foreach(idx, ref child; session.children) {
child.x = terminal.width - spaceRemaining - 1;
terminal.color(Color.blue, child.demandsAttention ? Color.green : Color.white, ForceOption.automatic, idx != session.activeScreen);
terminal.write(" ");
spaceRemaining--;
anyDemandAttention = anyDemandAttention || child.demandsAttention;
int size = 10;
if(spaceRemaining < size)
size = spaceRemaining;
if(size <= 2)
break;
if(child.title.length == 0)
child.title = "Screen " ~ to!string(idx + 1);
auto dispTitle = child.titlePrefix.length ? (child.titlePrefix ~ child.title) : child.title;
if(dispTitle.length <= size-2) {
terminal.write(dispTitle);
foreach(i; dispTitle.length .. size-2)
terminal.write(" ");
} else {
terminal.write(dispTitle[0 .. size-2]);
}
terminal.write(" ");
spaceRemaining -= size;
child.x2 = terminal.width - spaceRemaining - 1;
if(spaceRemaining == 0)
break;
}
terminal.color(Color.blue, Color.white, ForceOption.automatic, true);
foreach(i; 0 .. spaceRemaining)
terminal.write(" ");
terminal.write(" ");//"X");
}
if(anyDemandAttention) {
import std.process;
if(environment["TERM"] != "linux")
terminal.writeStringRaw("\033]5001;1\007");
}
string titleToDraw;
if(session.title.length)
titleToDraw = session.title ~ " - " ~ session.children[session.activeScreen].title;
else
titleToDraw = session.children[session.activeScreen].title;
// FIXME
if(true || lastTitleDrawn != titleToDraw) {
lastTitleDrawn = titleToDraw;
terminal.setTitle(titleToDraw);
}
}
int nextScreen(ref Session session) {
foreach(i; session.activeScreen + 1 .. session.children.length)
if(session.children[i].socket !is null)
return cast(int) i;
foreach(i; 0 .. session.activeScreen)
if(session.children[i].socket !is null)
return cast(int) i;
return session.activeScreen;
}
int nextScreenBackwards(ref Session session) {
foreach_reverse(i; 0 .. session.activeScreen)
if(session.children[i].socket !is null)
return cast(int) i;
foreach_reverse(i; session.activeScreen + 1 .. session.children.length)
if(session.children[i].socket !is null)
return cast(int) i;
return session.activeScreen;
}
void attach(Terminal* terminal, ref Session session, string sname) {
int position = -1;
foreach(idx, child; session.children)
if(child.socket is null) {
position = cast(int) idx;
break;
}
if(position == -1) {
position = cast(int) session.children.length;
session.children ~= ChildTerminal();
}
// spin off the child process
auto newSocket = connectTo(sname, true);
if(newSocket) {
sendSimpleMessage(newSocket, InputMessage.Type.Attach);
session.children[position] = ChildTerminal(newSocket, sname, sname);
setActiveScreen(terminal, session, position);
if(session.autoCommand.length)
sendSimulatedInput(newSocket, session.autoCommand);
}
}
void sendSimpleMessage(Socket socket, InputMessage.Type type) {
InputMessage im;
im.eventLength = InputMessage.type.offsetof + InputMessage.type.sizeof;
im.type = type;
auto len = socket.send((cast(ubyte*)&im)[0 .. im.eventLength]);
if(len <= 0) {
throw new Exception("wtf");
}
}
void handleEvent(Terminal* terminal, ref Session session, InputEvent event, Socket socket) {
// FIXME: UI stuff
static bool escaping;
static bool gettingCommandLine;
static bool gettingListSelection;
static LineGetter lineGetter;
InputMessage im;
im.eventLength = im.sizeof;
InputMessage* eventToSend;
if(gettingCommandLine) {
if(!lineGetter.workOnLine(event)) {
gettingCommandLine = false;
auto cmdLine = lineGetter.finishGettingLine();
import std.string;
auto args = split(cmdLine, " ");
if(args.length)
switch(args[0]) {
case "attach":
attach(terminal, session, args.length > 1 ? args[1] : null);
break;
case "title":
session.children[session.activeScreen].titlePrefix = join(args[1..$], " ");
break;
default:
}
outputPaused = false;
forceRedraw(terminal, session);
}
return;
}
if(gettingListSelection) {
if(event.type == InputEvent.Type.CharacterEvent) {
auto ce = event.get!(InputEvent.Type.CharacterEvent);
if(ce.eventType == CharacterEvent.Type.Released)
return;
switch(ce.character) {
case '0':
..
case '9':
int num = cast(int) (ce.character - '0');
if(!session.zeroBasedCounting) {
if(num == 0)
num = 9;
else
num--;
}
setActiveScreen(terminal, session, num);
goto case;
case '\n':
gettingListSelection = false;
outputPaused = false;
forceRedraw(terminal, session);
return;
default:
}
}
}
void triggerCommandLine(string text = "") {
terminal.moveTo(0, terminal.height - 1);
terminal.color(Color.DEFAULT, Color.DEFAULT, ForceOption.alwaysSend, true);
terminal.write(":");
foreach(i; 1 .. terminal.width)
terminal.write(" ");
terminal.moveTo(1, terminal.height - 1);
gettingCommandLine = true;
if(lineGetter is null)
lineGetter = new LineGetter(terminal);
lineGetter.startGettingLine();
lineGetter.addString(text);
outputPaused = true;
if(text.length)
lineGetter.redraw();
}
final switch(event.type) {
case InputEvent.Type.LinkEvent:
break;
case InputEvent.Type.EndOfFileEvent:
// assert(0);
// FIXME: is this right too?
running = false;
break;
case InputEvent.Type.HangupEvent:
running = false;
break;
case InputEvent.Type.CharacterEvent:
case InputEvent.Type.NonCharacterKeyEvent:
break; // ignore deprecated things
case InputEvent.Type.KeyboardEvent:
auto ce = event.get!(InputEvent.Type.KeyboardEvent);
if(!ce.pressed)
return;
if(escaping) {
// C-a C-a toggles active screens quickly
if(session.escapeCharacter != dchar.init && ce.character == session.escapeCharacter) {
if(previousScreen != session.activeScreen && previousScreen < session.children.length && session.children[previousScreen].socket !is null)
setActiveScreen(terminal, session, previousScreen);
else {
setActiveScreen(terminal, session, nextScreen(session));
}
// C-a a sends C-a to the child.
} else if(session.escapeCharacter != dchar.init && ce.character == session.escapeCharacter + 'a' - 1) {
im.type = InputMessage.Type.CharacterPressed;
im.characterEvent.character = 1;
im.eventLength = im.characterEvent.offsetof + im.CharacterEvent.sizeof;
eventToSend = &im;
} else switch(ce.character) {
case 'q': debugMode = !debugMode; break;
case 't':
session.showingTaskbar = !session.showingTaskbar;
forceRedraw(terminal, session); // redraw full because the height changes
break;
case ' ':
setActiveScreen(terminal, session, nextScreen(session));
break;
case 'd':
// detach the session
running = false;
break;
case 'D':
// detach only the screen
// closeSocket(terminal, session); // don't really like this
break;
case 'i':
// request information
terminal.writeln(session.children[session.activeScreen].socketName);
break;
case 12: // ^L
forceRedraw(terminal, session);
break;
case '"':
// list everything and give ui to choose