-
Notifications
You must be signed in to change notification settings - Fork 1
/
mgui.b
3790 lines (3163 loc) · 82.1 KB
/
mgui.b
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
implement Mgui;
include "sys.m";
include "draw.m";
include "math.m";
include "freetype.m";
include "pgui.m";
include "string.m";
include "keyring.m";
include "security.m";
include "styxpersist.m";
include "tk.m";
include "wmsrv.m";
include "wmclient.m";
include "imagefile.m";
include "bufio.m";
include "readdir.m";
include "lock.m";
include "timers.m";
include "cache.m";
include "mgui.m";
include "timedio.m";
include "dialog.m";
include "selectfile.m";
include "tkclient.m";
include "winplace.m";
winplace : Winplace;
dialog : Dialog;
selectfile : Selectfile;
tk : Tk;
tkclient : Tkclient;
str : String;
lock : Lock;
sys : Sys;
wmclient : Wmclient;
wmsrv : Wmsrv;
bufio : Bufio;
pgui : Pgui;
timedio : TimedIO;
draw : Draw;
cache : Cache;
random : Random;
PFont,
ZP,
PTextEntry,
PScrollableText,
PScrollableList,
PScrollBar,
PButton,
PFileDialog,
PG_LTARROW,
PG_RTARROW : import pgui;
Context,
Display,
Font,
Image,
Point,
Pointer,
Rect,
Screen,
Wmcontext : import draw;
timedopen,
timedread,
timedwrite,
timedmount,
timedunmount,
timedreaddir,
timedauclient,
timeddial : import timedio;
StrCache : import cache;
Window,
Client : import wmsrv;
Iobuf : import bufio;
Semaphore : import lock;
M : MguiState;
DEBUG : con 0;
# Stuff from /appl/wm/wm.b.
# Needs major cleanup:
ptrfocus: ref Client;
kbdfocus: ref Client;
controller: ref Client;
allowcontrol := 1;
fakekbd: chan of string;
fakekbdin: chan of string;
buttons := 0;
lastrect: Rect;
Ptrstarted, Kbdstarted, Controlstarted, Controller, Fixedorigin: con 1<<iota;
Minx, Miny, Maxx, Maxy: con 1<<iota;
Sminx, Sminy, Smaxx, Smaxy: con iota;
Bdwidth: con 3;
init(ctxt: ref Context, args: list of string)
{
buf := array [Sys->ATOMICIO] of byte;
sys = load Sys Sys->PATH;
str = load String String->PATH;
bufio = load Bufio Bufio->PATH;
lock = load Lock Lock->PATH;
timedio = load TimedIO TimedIO->PATH;
cache = load Cache Cache->PATH;
random = load Random Random->PATH;
if (str == nil || bufio == nil || lock == nil || timedio == nil || cache == nil || random == nil)
{
fatal(sys->sprint(
"Could not load String/Bufio/Readdir/Lock/TimedIO/Cache/Random modules: %r."));
}
if ((err := timedio->init()) != nil)
{
fatal(sys->sprint("Could not initialize TimedIO module: %s.", err));
}
#
# Fork pgrp+ns ASAP. Will also fork name space again
# to be separate from export later in services()
#
M.savedargs = args;
M.pgrp = sys->pctl(Sys->NEWPGRP|Sys->FORKNS, nil);
M.stderr = sys->fildes(2);
M.gui = 1;
M.engineid = random->randomint(Random->ReallyRandom);
tmpfd := sys->open("#e/emuroot", Sys->OREAD);
if (tmpfd == nil)
{
fatal(sys->sprint("Could not open #e/emuroot: %r"));
}
n := sys->read(tmpfd, buf, len buf);
if (n < 0)
{
fatal(sys->sprint("Could not read #e/emuroot: %r"));
}
status(sys->sprint("Root directory is \"%s\"", string buf[:n]));
tmpfd = sys->open("#e/emuhost", Sys->OREAD);
if (tmpfd == nil)
{
fatal(sys->sprint("Could not open #e/emuhost: %r"));
}
n = sys->read(tmpfd, buf, len buf);
if (n < 0)
{
fatal(sys->sprint("Could not read #e/emuhost: %r"));
}
status(sys->sprint("Host architecture is \"%s\"", string buf[:n]));
tmpfd = sys->open("#e/emuargs", Sys->OREAD);
if (tmpfd == nil)
{
fatal(sys->sprint("Could not open #e/emuargs: %r"));
}
n = sys->read(tmpfd, buf, len buf);
if (n < 0)
{
fatal(sys->sprint("Could not read #e/emuargs: %r"));
}
status(sys->sprint("Run with host args \"%s\"", string buf[:n]));
# Get rid of program name
(nil, emuargs) := str->splitstrl(string buf[:n], " ");
(l1, r1) := str->splitstrl(emuargs, "-I");
(l2, r2) := str->splitstrl(emuargs, "-d");
if (r1 != nil || r2 != nil)
{
M.daemonized = 1;
M.gui = 0;
status("Running in daemonized console mode");
}
if ((len args == 2) && (hd tl args == "-nogui"))
{
status("Running in console mode...");
M.gui = 0;
}
else if (len args >= 2)
{
usage();
cleanexit();
}
(there, nil) := sys->stat("/net/cs");
if (there == -1)
{
cs := load Command "/dis/ndb/cs.dis";
if (cs == nil)
{
fatal("Could not load /dis/ndb/cs.dis");
}
cs->init(nil, "cs"::nil);
}
(there, nil) = sys->stat("/net/dns");
if (there == -1)
{
dns := load Command "/dis/ndb/dns.dis";
if (dns == nil)
{
fatal("Could not load /dis/ndb/dns.dis");
}
dns->init(nil, "dns"::nil);
}
M.sem_cachedhosts = Semaphore.new();
M.sem_cachednodes = Semaphore.new();
M.sem_curhost = Semaphore.new();
M.sem_curnode = Semaphore.new();
M.mntcache = StrCache.new();
if (M.mntcache == nil)
{
fatal("Could not initialize a StrCache.");
}
hostname := sysname();
status(sys->sprint("Local host name is \"%s\"", hostname));
status("Attaching local simulation engine...");
if (sys->bind("#*sunflower!"+hostname+sys->sprint(".%uX", M.engineid), MG_MNTDIR, sys->MBEFORE) < 0)
{
fatal("Could not attach local simulation engine:"+
sys->sprint("%r"));
}
status("Unioning local filesystem into / ...");
ok := sys->bind("#U*", "/", Sys->MAFTER);
if (ok < 0)
{
error(sys->sprint("Could not bind host filesystem ('#U*'): %r"));
}
ls := load Command "/dis/ls.dis";
ls->init(nil, "ls"::"-ls"::MG_MNTDIR::nil);
sync := chan of int;
spawn enginectl(MG_MNTDIR+"sunflower."+hostname+sys->sprint(".%uX", M.engineid), sync);
<- sync;
# Walk the device to pick up the node and lists
M.setcachedhosts(getdevhostlist());
# Don't call setcachednodes though, because it needs gui init to be done (easier that way)
# M.setcachednodes(getdevnodelist());
tmp := M.getcachedhosts();
if (tmp == nil)
{
fatal("No usable local engine (getdevnodelist() returned nil).");
}
M.localhost = hd tmp;
status(sys->sprint("Local simulation engine @ %s", M.localhost.mntpt));
M.curhost = M.localhost;
#
# Need to spawn this off so we can detach export
# from name space, but only after enginectl is made
#
spawn services();
if (M.gui)
{
spawn guiinit(ctxt, sync);
<-sync;
}
}
allocmsgwin() : ref PScrollableText
{
mr := get_msgswinrect();
pmr := Rect (mr.min.add((MG_INPUTRECT_HOFFSET, 0)), mr.max);
pmr = pmr.inset(MG_TEXTBOX_INSET);
(msgwin, e) := PScrollableText.new(M.screen,
pmr, MG_MSGS_FONT, MG_DFLT_TXTBUFLEN, Draw->White, Draw->White);
## pmr, "/appl/sfgui/fonts/ttf/LUCON.TTF", MG_DFLT_TXTBUFLEN,
## Draw->White, Draw->White);
fatal(e);
msgwin.setlinespacing(MG_DEFAULT_LINESPACING);
msgwin.autoscroll = 1;
msgwin.settxtimg(M.display.color(Draw->Black));
return msgwin;
}
guiinit(ctxt : ref Draw->Context, guiinitsync : chan of int)
{
draw = load Draw Draw->PATH;
wmsrv = load Wmsrv Wmsrv->PATH;
pgui = load Pgui Pgui->PATH;
tk = load Tk Tk->PATH;
tkclient = load Tkclient Tkclient->PATH;
selectfile = load Selectfile Selectfile->PATH;
dialog = load Dialog Dialog->PATH;
wmclient = load Wmclient Wmclient->PATH;
winplace = load Winplace Winplace->PATH;
# So that fatal() etc. dont try to use an un-init graphics subsystem:
M.gui = 0;
if (draw == nil || wmsrv == nil || pgui == nil || tk == nil ||
tkclient == nil || wmclient == nil || winplace == nil ||
selectfile == nil || dialog == nil)
{
fatal(sys->sprint("Could not load %s modules : %r\n",
"Draw/Wmsrv/Pgui/Tk/Tkclient/Slectfile/Dialog/Wmclient/Winplace"));
}
tkclient->init();
selectfile->init();
dialog->init();
wmclient->init();
winplace->init();
(there, dir) := sys->stat("/dev/draw");
if ((there == -1) || (dir.qid.qtype != Sys->QTDIR) || (dir.dtype != 'i'))
{
ok := sys->bind("#i", "/dev", Sys->MBEFORE);
if (ok < 0)
{
fatal(sys->sprint("Could not bind draw module: %r"));
}
}
if (ctxt == nil)
{
ctxt = wmclient->makedrawcontext();
}
if (ctxt == nil)
{
fatal(sys->sprint("Could not make or inherit a graphics context: %r"));
}
M.display = ctxt.display;
M.ctxt = ctxt;
(ok, err) := pgui->init();
if (ok < 0)
{
fatal(sys->sprint("Initialization of PGui failed: %s", err));
}
buts := Wmclient->Appl;
if (ctxt.wm == nil)
{
buts = Wmclient->Plain;
}
M.win = wmclient->window(ctxt, "Sgui : Sunflower Simulator Control Interface", buts);
wmclient->M.win.reshape(((0, 0), (900, 700)));
wmclient->M.win.onscreen(nil);
if (M.win.image == nil)
{
fatal("Mgui: cannot get image to draw on.");
}
wmclient->M.win.startinput("kbd" :: "ptr" :: nil);
(clientwm, join, req) := wmsrv->init();
clientctxt := ref Draw->Context(ctxt.display, ctxt.screen, clientwm);
spawn toolbar(clientctxt);
bg := M.display.color(MG_DEFAULT_BGCOLOR);
M.screen = Screen.allocate(M.win.image, bg, 0);
M.win.image.draw(M.win.image.r, M.screen.fill, nil, M.screen.fill.r.min);
#
# Most of this drawing should go into Pgui
#
e := "";
# Global msgs window reference is pmsg. It is updated when we set the current node
mr := get_msgswinrect();
msgsbgwin = M.screen.newwindow(mr, Draw->Refbackup, Draw->White);
M.display.image.draw(mr, msgsbgwin, nil, mr.min);
mrborder := pgui->getrectborder(mr);
M.display.image.poly(mrborder, Draw->Enddisc, Draw->Enddisc,
MG_WINBORDER_PIXELS, M.display.color(MG_DEFAULT_BORDERCOLOR), mr.min);
pmsgwin = allocmsgwin();
#pmsgwin = M.getcurnode().msgwin;
#
# Scrollbar for msgs window
#
smr := get_msgswinscrollrect();
(pscrollmsgwin, e) = PScrollBar.new(M.screen, smr, 0, MG_DFLT_TXTBUFLEN,
MG_COLOR_LIGHTORANGE, Draw->White);
fatal(e);
# Input box
ir := get_inputwinrect();
irbgwin := M.screen.newwindow(ir, Draw->Refbackup, Draw->White);
M.display.image.draw(ir, irbgwin, nil, ir.min);
irborder := pgui->getrectborder(ir);
M.display.image.poly(irborder, Draw->Enddisc, Draw->Enddisc,
MG_WINBORDER_PIXELS, M.display.color(MG_DEFAULT_BORDERCOLOR), ir.min);
(pinputwin, e) = PTextEntry.new(MG_PROMPT, M.screen,
ir.inset(MG_INPUTBOX_INSET),
MG_INPUT_FONT, MG_INPUT_HISTORYLEN, Draw->White, Draw->White);
fatal(e);
pinputwin.s.settxtimg(M.display.color(Draw->Black));
pinputwin.s.append(MG_PROMPT);
# Remote hosts
rr := get_remotewinrect();
rrbgwin := M.screen.newwindow(rr, Draw->Refbackup, Draw->White);
M.display.image.draw(rr, rrbgwin, nil, rr.min);
rrborder := pgui->getrectborder(rr);
M.display.image.poly(rrborder, Draw->Enddisc, Draw->Enddisc,
MG_WINBORDER_PIXELS, M.display.color(MG_DEFAULT_BORDERCOLOR), rr.min);
prr := Rect (rr.min.add((MG_INPUTRECT_HOFFSET, 0)), rr.max);
prr = prr.inset(MG_TEXTBOX_INSET);
(prmtwin, e) = PScrollableText.new(M.screen,
prr, MG_REMOTEHOSTS_FONT, MG_DFLT_REMOTEBUFLEN,
Draw->White, Draw->White);
fatal(e);
prmtwin.setlinespacing(MG_DEFAULT_LINESPACING);
prmtwin.autoscroll = 1;
prmtwin.settxtimg(M.display.color(MG_COLOR_DARKORANGE));
# Node info
nr := get_nodeinforect();
nrbgwin := M.screen.newwindow(nr, Draw->Refbackup, Draw->White);
M.display.image.draw(nr, nrbgwin, nil, nr.min);
nrborder := pgui->getrectborder(nr);
M.display.image.poly(nrborder, Draw->Enddisc, Draw->Enddisc,
MG_WINBORDER_PIXELS, M.display.color(MG_DEFAULT_BORDERCOLOR), nr.min);
pnr := nr.inset(3);
(pnodeinfowin, e) = PScrollableText.new(M.screen,
pnr, MG_NODEINFO_FONT, MG_DFLT_REMOTEBUFLEN, Draw->White, Draw->White);
fatal(e);
pnodeinfowin.setlinespacing(1.0);
pnodeinfowin.autoscroll = 0;
pnodeinfowin.settxtimg(M.display.color(Draw->Black));
# Errors
er := get_errwinrect();
erbgwin := M.screen.newwindow(er, Draw->Refbackup, Draw->White);
M.display.image.draw(er, erbgwin, nil, er.min);
erborder := pgui->getrectborder(er);
M.display.image.poly(erborder, Draw->Enddisc, Draw->Enddisc,
MG_WINBORDER_PIXELS, M.display.color(MG_DEFAULT_BORDERCOLOR), er.min);
per := Rect (er.min.add((3*MG_INPUTRECT_HOFFSET/4, 0)), er.max);
per = per.inset(MG_TEXTBOX_INSET);
(perrwin, e) = PScrollableText.new(M.screen,
per, MG_ALERT_FONT, MG_DFLT_ERRBUFLEN, Draw->White, Draw->White);
fatal(e);
perrwin.setlinespacing(MG_DEFAULT_LINESPACING);
perrwin.autoscroll = 1;
perrwin.settxtimg(M.display.color(Draw->Red));
# Warnings
sr := get_sanitywinrect();
srbgwin := M.screen.newwindow(sr, Draw->Refbackup, Draw->White);
M.display.image.draw(sr, srbgwin, nil, sr.min);
srborder := pgui->getrectborder(sr);
M.display.image.poly(srborder, Draw->Enddisc, Draw->Enddisc,
MG_WINBORDER_PIXELS, M.display.color(MG_DEFAULT_BORDERCOLOR), sr.min);
psr := Rect (sr.min.add((3*MG_INPUTRECT_HOFFSET/4, 0)), sr.max);
psr = psr.inset(MG_TEXTBOX_INSET);
(psanitywin, e) = PScrollableText.new(M.screen,
psr, MG_ALERT_FONT, MG_DFLT_ERRBUFLEN, Draw->White, Draw->White);
fatal(e);
psanitywin.setlinespacing(MG_DEFAULT_LINESPACING);
psanitywin.autoscroll = 1;
psanitywin.settxtimg(M.display.color(Draw->Black));
# Author and PGP key stuff
ar := get_authorwinrect();
(pauthorswin, e) = PScrollableText.new(M.screen, ar,
MG_AUTHORS_FONT, MG_DFLT_AUTHNLINES,
MG_DEFAULT_BGCOLOR, MG_DEFAULT_BGCOLOR);
fatal(e);
pauthorswin.setlinespacing(1.0);
pauthorswin.autoscroll = 0;
pauthorswin.settxtimg(M.display.color(MG_DEFAULT_AUTHORSCOLOR));
pauthorswin.setbgimg(M.display.color(Draw->Transparent));
pauthorswin.append(
"Sunflower simulator GUI. Authored, 2004-2008, by phillip stanley-marbell\n");
pauthorswin.append(
"Public key fingerprint 62A1 E95D 304D 9876 D5B1 1FB2 BF7E B65F BD89 20AB\n");
pauthorswin.append(
"This software is provided with ABSOLUTELY NO WARRANTY. Read LICENSE.txt\n");
# Icons and other graphics
#br := get_bannerwinrect();
#bannerwin := M.screen.newwindow(br, Draw->Refbackup, MG_DEFAULT_BGCOLOR);
#pgui->drawimgfromfile(MG_BANNERIMG, br, bannerwin);
pgui->drawimgfromfile(MG_REMOTEIMG,
get_remotewinrect().inset(MG_TEXTBOX_INSET), M.display.image);
pgui->drawimgfromfile(MG_KEYBOARDIMG,
get_inputwinrect().inset(MG_INPUTBOX_INSET).subpt((MG_INPUTRECT_HOFFSET, 0)),
M.display.image);
pgui->drawimgfromfile(MG_WARNIMG,
get_sanitywinrect().inset(MG_TEXTBOX_INSET), M.display.image);
pgui->drawimgfromfile(MG_ERRORIMG,
get_errwinrect().inset(MG_TEXTBOX_INSET), M.display.image);
# For now, leave out the attempt to get a transparent banner...
#M.display.image.draw(br, bannerwin, nil, br.min);
M.gui = 1;
M.kbdchan = chan of int;
M.msgschan = chan of string;
M.tpgymousechan = chan of Pointer;
M.updatetopology= chan of int;
M.tpgyfont = Font.open(M.screen.display, MG_TPGY_FONT);
if (M.tpgyfont == nil)
{
M.tpgyfont = Font.open(M.screen.display, "*default*");
}
if (M.tpgyfont == nil)
{
fatal(sys->sprint("Could not open *default* font: %r"));
}
sync := chan of int;
spawn kbd(M.kbdchan, sync);
<- sync;
spawn remotedisplay();
spawn nodeinfodisplay();
spawn topologydisplay();
spawn fastrefreshproc();
spawn tpgymouse(M.tpgymousechan);
spawn stdioproc();
spawn splash();
guiinitsync <-= 0;
M.guiinit = 1;
for (;;)
alt
{
c := <-M.win.ctl or c = <-M.win.ctxt.ctl =>
M.splashactive = 0;
if (c == "exit")
{
cleanexit();
}
wmclient->M.win.wmctl(c);
if(M.win.image != M.screen.image)
{
reshaped(M.win);
}
c := <-M.win.ctxt.kbd =>
M.splashactive = 0;
M.kbdchan <-= c;
#
# TODO: a copy of this pointer event should be sent to Pwm, a
# window manager that is built on Pgui and monitors which window
# is active and updates thats window's border.
#
p := <-M.win.ctxt.ptr =>
#
# Pointer events are first passed to titlebar
# then if not consumed, handled by us
#
if(wmclient->M.win.pointer(*p))
{
break;
}
if (p.buttons)
{
M.splashactive = 0;
M.tpgymousechan <-= *p;
}
#####
if(p.buttons && (ptrfocus == nil || buttons == 0))
{
c := wmsrv->find(p.xy);
if(c != nil){
ptrfocus = c;
c.ctl <-= "raise";
setkbdfocus(c);
}
}
if(ptrfocus != nil && (ptrfocus.flags & Ptrstarted) != 0)
{
buttons = p.buttons;
ptrfocus.ptr <-= p;
break;
}
buttons = 0;
####
(c, rc) := <-join =>
rc <-= nil;
# new client; inform it of the available screen rectangle.
# XXX do we need to do this now we've got wmrect?
c.ctl <-= "rect " + r2s(M.screen.image.r);
if(allowcontrol){
controller = c;
c.flags |= Controller;
allowcontrol = 0;
}else
controlevent("newclient " + string c.id);
(c, data, rc) := <-req =>
# if client leaving
if(rc == nil){
c.remove();
if(c == ptrfocus)
ptrfocus = nil;
if(c == kbdfocus)
kbdfocus = nil;
if(c == controller)
controller = nil;
controlevent("delclient " + string c.id);
for(z := wmsrv->top(); z != nil; z = z.znext)
if(z.flags & Kbdstarted)
break;
setkbdfocus(z);
c.stop <-= 1;
break;
}
reqerr := handlerequest(M.win, M.win.ctxt, c, string data);
n := len data;
if(reqerr != nil)
n = -1;
alt{
rc <-= (n, reqerr) =>;
* =>;
}
####
}
}
# From wm/wm.b
controlevent(e: string)
{
if (controller != nil && (controller.flags & Controlstarted))
{
controller.ctl <-= e;
}
}
# From wm/wm.b
sweep(ptr: chan of ref Pointer, r: Rect, offset: Point, borders: array of ref Image,
move, show: int, min: Point): Rect
{
while ((p := <-ptr).buttons != 0)
{
xy := p.xy.sub(offset);
if(move&Minx)
r.min.x = xy.x;
if(move&Miny)
r.min.y = xy.y;
if(move&Maxx)
r.max.x = xy.x;
if(move&Maxy)
r.max.y = xy.y;
showborders(borders, r, show);
}
r = r.canon();
if (r.min.y < M.screen.image.r.min.y)
{
r.min.y = M.screen.image.r.min.y;
r = r.canon();
}
if(r.dx() < min.x)
{
if(move & Maxx)
r.max.x = r.min.x + min.x;
else
r.min.x = r.max.x - min.x;
}
if(r.dy() < min.y)
{
if(move & Maxy)
{
r.max.y = r.min.y + min.y;
}
else
{
r.min.y = r.max.y - min.y;
if(r.min.y < M.screen.image.r.min.y)
{
r.min.y = M.screen.image.r.min.y;
r.max.y = r.min.y + min.y;
}
}
}
return r;
}
# From wm/wm.b
dragwin(ptr: chan of ref Pointer, c: ref Client, w: ref Window, off: Point): string
{
if(buttons == 0)
return "too late";
p: ref Pointer;
do{
p = <-ptr;
w.img.origin(w.img.r.min, p.xy.sub(off));
} while (p.buttons != 0);
c.ptr <-= p;
buttons = 0;
r: Rect;
r.min = p.xy.sub(off);
r.max = r.min.add(w.r.size());
if(r.eq(w.r))
return "not moved";
reshape(c, w.tag, r);
return nil;
}
# From wm/wm.b
bufferproc(in, out: chan of string)
{
h, t: list of string;
dummyout := chan of string;
for(;;){
outc := dummyout;
s: string;
if(h != nil || t != nil){
outc = out;
if(h == nil)
for(; t != nil; t = tl t)
h = hd t :: h;
s = hd h;
}
alt{
x := <-in =>
t = x :: t;
outc <-= s =>
h = tl h;
}
}
}
# From wm/wm.b
showborders(b: array of ref Image, r: Rect, show: int)
{
r = r.canon();
b[Sminx] = showborder(b[Sminx], show&Minx,
(r.min, (r.min.x+Bdwidth, r.max.y)));
b[Sminy] = showborder(b[Sminy], show&Miny,
((r.min.x+Bdwidth, r.min.y), (r.max.x-Bdwidth, r.min.y+Bdwidth)));
b[Smaxx] = showborder(b[Smaxx], show&Maxx,
((r.max.x-Bdwidth, r.min.y), (r.max.x, r.max.y)));
b[Smaxy] = showborder(b[Smaxy], show&Maxy,
((r.min.x+Bdwidth, r.max.y-Bdwidth), (r.max.x-Bdwidth, r.max.y)));
}
# From wm/wm.b
showborder(b: ref Image, show: int, r: Rect): ref Image
{
if(!show)
return nil;
if(b != nil && b.r.size().eq(r.size()))
b.origin(r.min, r.min);
else
b = M.screen.newwindow(r, Draw->Refbackup, Draw->Red);
return b;
}
# From wm/wm.b
fitrect(w, r: Rect): Rect
{
if(w.dx() > r.dx())
w.max.x = w.min.x + r.dx();
if(w.dy() > r.dy())
w.max.y = w.min.y + r.dy();
size := w.size();
if (w.max.x > r.max.x)
(w.min.x, w.max.x) = (r.min.x - size.x, r.max.x - size.x);
if (w.max.y > r.max.y)
(w.min.y, w.max.y) = (r.min.y - size.y, r.max.y - size.y);
if (w.min.x < r.min.x)
(w.min.x, w.max.x) = (r.min.x, r.min.x + size.x);
if (w.min.y < r.min.y)
(w.min.y, w.max.y) = (r.min.y, r.min.y + size.y);
return w;
}
# From wm/wm.b: find an suitable area for a window
newrect(w, r: Rect): Rect
{
rl: list of Rect;
for(z := wmsrv->top(); z != nil; z = z.znext)
for(wl := z.wins; wl != nil; wl = tl wl)
rl = (hd wl).r :: rl;
lastrect = winplace->place(rl, r, lastrect, w.size());
return lastrect;
}
# From wm/wm.b
sizewin(ptrc: chan of ref Pointer, c: ref Client, w: ref Window, minsize: Point): string
{
borders := array[4] of ref Image;
showborders(borders, w.r, Minx|Maxx|Miny|Maxy);
M.screen.image.flush(Draw->Flushnow);
while((ptr := <-ptrc).buttons == 0)
;
xy := ptr.xy;
move, show: int;
offset := Point(0, 0);
r := w.r;
show = Minx|Miny|Maxx|Maxy;
if(xy.in(w.r) == 0){
r = (xy, xy);
move = Maxx|Maxy;
}else {
if(xy.x < (r.min.x+r.max.x)/2){
move=Minx;
offset.x = xy.x - r.min.x;
}else{
move=Maxx;
offset.x = xy.x - r.max.x;
}
if(xy.y < (r.min.y+r.max.y)/2){
move |= Miny;
offset.y = xy.y - r.min.y;
}else{
move |= Maxy;
offset.y = xy.y - r.max.y;
}
}
return reshape(c, w.tag, sweep(ptrc, r, offset, borders, move, show, minsize));
}
# From wm/wm.b
handlerequest(win: ref Wmclient->Window, wmctxt: ref Wmcontext, c: ref Client, req: string): string
{
#sys->print("%d: %s\n", c.id, req);
args := str->unquoted(req);
if(args == nil)
return "no request";
n := len args;
if(req[0] == '!' && n < 3)
return "bad arg count";
case hd args {
"key" =>
# XXX should we think about restricting this capability to certain clients only?
if(n != 2)
return "bad arg count";
if(fakekbdin == nil){
fakekbdin = chan of string;
spawn bufferproc(fakekbdin, fakekbd);
}
fakekbdin <-= hd tl args;
"ptr" =>
# ptr x y
if(n != 3)
return "bad arg count";
if(ptrfocus != c)
return "cannot move pointer";
e := wmclient->win.wmctl(req);
if(e == nil){
c.ptr <-= nil; # flush queue
c.ptr <-= ref Pointer(buttons, (int hd tl args, int hd tl tl args), sys->millisec());
}
"start" =>
if(n != 2)
return "bad arg count";
case hd tl args {
"mouse" or
"ptr" =>
c.flags |= Ptrstarted;
"kbd" =>
c.flags |= Kbdstarted;
# XXX this means that any new window grabs the focus from the current
# application, but usually you want this to happen... how can we distinguish
# the two cases?
setkbdfocus(c);
"control" =>
if((c.flags & Controller) == 0)
return "control not available";
c.flags |= Controlstarted;
* =>
return "unknown input source";
}
"!reshape" =>
# reshape tag reqid rect [how]
# XXX allow "how" to specify that the origin of the window is never
# changed - a new window will be created instead.
if(n < 7)
return "bad arg count";
args = tl args;
tag := hd args; args = tl args;
args = tl args; # skip reqid
r: Rect;
r.min.x = int hd args; args = tl args;
r.min.y = int hd args; args = tl args;
r.max.x = int hd args; args = tl args;
r.max.y = int hd args; args = tl args;
if(args != nil){
case hd args{
"onscreen" =>
r = fitrect(r, M.screen.image.r);
"place" =>
r = fitrect(r, M.screen.image.r);
r = newrect(r, M.screen.image.r);
"exact" =>
;
"max" =>
r = M.screen.image.r; # XXX don't obscure toolbar?
* =>
return "unkown placement method";
}
}
return reshape(c, tag, r);
"delete" =>
# delete tag
if(tl args == nil)
return "tag required";
c.setimage(hd tl args, nil);
if(c.wins == nil && c == kbdfocus)
setkbdfocus(nil);
"raise" =>
c.top();
"lower" =>
c.bottom();
"!move" or
"!size" =>
# !move tag reqid startx starty
# !size tag reqid mindx mindy
ismove := hd args == "!move";
if(n < 3)
return "bad arg count";
args = tl args;
tag := hd args; args = tl args;
args = tl args; # skip reqid
w := c.window(tag);
if(w == nil)
return "no such tag";
if(ismove){
if(n != 5)
return "bad arg count";
return dragwin(wmctxt.ptr, c, w, Point(int hd args, int hd tl args).sub(w.r.min));
}else{
if(n != 5)
return "bad arg count";
sizewin(wmctxt.ptr, c, w, Point(int hd args, int hd tl args));
}
"fixedorigin" =>
c.flags |= Fixedorigin;
"rect" =>
;
"kbdfocus" =>
if(n != 2)
return "bad arg count";
if(int hd tl args)
setkbdfocus(c);
else if(c == kbdfocus)
setkbdfocus(nil);
# controller specific messages: