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
|
implement Wm;
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
Screen, Display, Image, Rect, Point, Wmcontext, Pointer: import draw;
include "wmsrv.m";
wmsrv: Wmsrv;
Window, Client: import wmsrv;
include "tk.m";
include "wmclient.m";
wmclient: Wmclient;
include "string.m";
str: String;
include "sh.m";
include "winplace.m";
winplace: Winplace;
Wm: module {
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
Ptrstarted, Kbdstarted, Controlstarted, Controller, Fixedorigin: con 1<<iota;
Bdwidth: con 3;
Sminx, Sminy, Smaxx, Smaxy: con iota;
Minx, Miny, Maxx, Maxy: con 1<<iota;
Background: con int 16r777777FF;
screen: ref Screen;
display: ref Display;
ptrfocus: ref Client;
kbdfocus: ref Client;
controller: ref Client;
allowcontrol := 1;
fakekbd: chan of string;
fakekbdin: chan of string;
buttons := 0;
badmodule(p: string)
{
sys->fprint(sys->fildes(2), "wm: cannot load %s: %r\n", p);
raise "fail:bad module";
}
init(ctxt: ref Draw->Context, argv: list of string)
{
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
if(draw == nil)
badmodule(Draw->PATH);
str = load String String->PATH;
if(str == nil)
badmodule(String->PATH);
wmsrv = load Wmsrv Wmsrv->PATH;
if(wmsrv == nil)
badmodule(Wmsrv->PATH);
wmclient = load Wmclient Wmclient->PATH;
if(wmclient == nil)
badmodule(Wmclient->PATH);
wmclient->init();
winplace = load Winplace Winplace->PATH;
if(winplace == nil)
badmodule(Winplace->PATH);
winplace->init();
sys->pctl(Sys->NEWPGRP|Sys->FORKNS, nil);
if (ctxt == nil)
ctxt = wmclient->makedrawcontext();
display = ctxt.display;
buts := Wmclient->Appl;
if(ctxt.wm == nil)
buts = Wmclient->Plain;
win := wmclient->window(ctxt, "Wm", buts);
wmclient->win.reshape(((0, 0), (100, 100)));
wmclient->win.onscreen("place");
if(win.image == nil){
sys->fprint(sys->fildes(2), "wm: cannot get image to draw on\n");
raise "fail:no image";
}
wmclient->win.startinput("kbd" :: "ptr" :: nil);
wmctxt := win.ctxt;
screen = makescreen(win.image);
(clientwm, join, req) := wmsrv->init();
clientctxt := ref Draw->Context(ctxt.display, nil, clientwm);
wmrectIO := sys->file2chan("/chan", "wmrect");
if(wmrectIO == nil)
fatal(sys->sprint("cannot make /chan/wmrect: %r"));
sync := chan of string;
argv = tl argv;
if(argv == nil)
argv = "wm/toolbar" :: nil;
spawn command(clientctxt, argv, sync);
if((e := <-sync) != nil)
fatal("cannot run command: " + e);
fakekbd = chan of string;
for(;;) alt {
c := <-win.ctl or
c = <-wmctxt.ctl =>
# XXX could implement "pleaseexit" in order that
# applications can raise a warning message before
# they're unceremoniously dumped.
if(c == "exit")
for(z := wmsrv->top(); z != nil; z = z.znext)
z.ctl <-= "exit";
wmclient->win.wmctl(c);
if(win.image != screen.image)
reshaped(win);
c := <-wmctxt.kbd or
c = int <-fakekbd =>
if(kbdfocus != nil)
kbdfocus.kbd <-= c;
p := <-wmctxt.ptr =>
if(wmclient->win.pointer(*p))
break;
if(p.buttons && (ptrfocus == nil || buttons == 0)){
c := wmsrv->find(p.xy);
if(c != nil){
ptrfocus = c;
c.ctl <-= "raise";
setfocus(win, c);
}
}
if(ptrfocus != nil && (ptrfocus.flags & Ptrstarted) != 0){
# inside currently selected client or it had button down last time (might have come up)
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(screen.image.r);
if(allowcontrol){
controller = c;
c.flags |= Controller;
allowcontrol = 0;
}else
controlevent("newclient " + string c.id);
c.cursor = "cursor";
(c, data, rc) := <-req =>
# if client leaving
if(rc == nil){
c.remove();
if(c.stop == nil)
break;
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;
setfocus(win, z);
c.stop <-= 1;
break;
}
err := handlerequest(win, wmctxt, c, string data);
n := len data;
if(err != nil)
n = -1;
alt{
rc <-= (n, err) =>;
* =>;
}
(nil, nil, nil, wc) := <-wmrectIO.write =>
if(wc == nil)
break;
alt{
wc <-= (0, "cannot write") =>;
* =>;
}
(off, nil, nil, rc) := <-wmrectIO.read =>
if(rc == nil)
break;
d := array of byte r2s(screen.image.r);
if(off > len d)
off = len d;
alt{
rc <-= (d[off:], nil) =>;
* =>;
}
}
}
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 restrict 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());
}
"cursor" =>
# cursor hotx hoty dx dy data
if(n != 6 && n != 1)
return "bad arg count";
c.cursor = req;
if(ptrfocus == c || kbdfocus == c)
return wmclient->win.wmctl(c.cursor);
"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?
setfocus(win, 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, screen.image.r);
"place" =>
r = fitrect(r, screen.image.r);
r = newrect(r, screen.image.r);
"exact" =>
;
"max" =>
r = 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)
setfocus(win, 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)
setfocus(win, c);
else if(c == kbdfocus)
setfocus(win, nil);
# controller specific messages:
"request" => # can be used to test for control.
if((c.flags & Controller) == 0)
return "you are not in control";
"ctl" =>
# ctl id msg
if((c.flags & Controlstarted) == 0)
return "invalid request";
if(n < 3)
return "bad arg count";
id := int hd tl args;
for(z := wmsrv->top(); z != nil; z = z.znext)
if(z.id == id)
break;
if(z == nil)
return "no such client";
z.ctl <-= str->quoted(tl tl args);
"endcontrol" =>
if(c != controller)
return "invalid request";
controller = nil;
allowcontrol = 1;
c.flags &= ~(Controlstarted | Controller);
* =>
if(c == controller || controller == nil || (controller.flags & Controlstarted) == 0)
return "unknown control request";
controller.ctl <-= "request " + string c.id + " " + req;
}
return nil;
}
Fix: con 1000;
# the window manager window has been reshaped;
# allocate a new screen, and move all the
reshaped(win: ref Wmclient->Window)
{
oldr := screen.image.r;
newr := win.image.r;
mx := Fix;
if(oldr.dx() > 0)
mx = newr.dx() * Fix / oldr.dx();
my := Fix;
if(oldr.dy() > 0)
my = newr.dy() * Fix / oldr.dy();
screen = makescreen(win.image);
for(z := wmsrv->top(); z != nil; z = z.znext){
for(wl := z.wins; wl != nil; wl = tl wl){
w := hd wl;
w.img = nil;
nr := w.r.subpt(oldr.min);
nr.min.x = nr.min.x * mx / Fix;
nr.min.y = nr.min.y * my / Fix;
nr.max.x = nr.max.x * mx / Fix;
nr.max.y = nr.max.y * my / Fix;
nr = nr.addpt(newr.min);
w.img = screen.newwindow(nr, Draw->Refbackup, Draw->Nofill);
# XXX check for creation failure
w.r = nr;
z.ctl <-= sys->sprint("!reshape %q -1 %s", w.tag, r2s(nr));
z.ctl <-= "rect " + r2s(newr);
}
}
}
controlevent(e: string)
{
if(controller != nil && (controller.flags & Controlstarted))
controller.ctl <-= e;
}
dragwin(ptr: chan of ref Pointer, c: ref Client, w: ref Window, off: Point): string
{
if(buttons == 0)
return "too late";
p: ref Pointer;
scr := screen.image.r;
Margin: con 10;
do{
p = <-ptr;
org := p.xy.sub(off);
if(org.y < scr.min.y)
org.y = scr.min.y;
else if(org.y > scr.max.y - Margin)
org.y = scr.max.y - Margin;
if(org.x < scr.min.x && org.x + w.r.dx() < scr.min.x + Margin)
org.x = scr.min.x + Margin - w.r.dx();
else if(org.x > scr.max.x - Margin)
org.x = scr.max.x - Margin;
w.img.origin(w.img.r.min, org);
} 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;
}
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);
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));
}
reshape(c: ref Client, tag: string, r: Rect): string
{
w := c.window(tag);
# if window hasn't changed size, then just change its origin and use the same image.
if((c.flags & Fixedorigin) == 0 && w != nil && w.r.size().eq(r.size())){
c.setorigin(tag, r.min);
} else {
img := screen.newwindow(r, Draw->Refbackup, Draw->Nofill);
if(img == nil)
return sys->sprint("window creation failed: %r");
if(c.setimage(tag, img) == -1)
return "can't do two at once";
}
c.top();
return nil;
}
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 < screen.image.r.min.y){
r.min.y = 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 < screen.image.r.min.y){
r.min.y = screen.image.r.min.y;
r.max.y = r.min.y + min.y;
}
}
}
return r;
}
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)));
}
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 = screen.newwindow(r, Draw->Refbackup, Draw->Red);
return b;
}
r2s(r: Rect): string
{
return string r.min.x + " " + string r.min.y + " " +
string r.max.x + " " + string r.max.y;
}
# XXX for consideration:
# do not allow applications to grab the keyboard focus
# unless there is currently no keyboard focus...
# but what about launching a new app from the taskbar:
# surely we should allow that to grab the focus?
setfocus(win: ref Wmclient->Window, new: ref Client)
{
old := kbdfocus;
if(old == new)
return;
if(new == nil)
wmclient->win.wmctl("cursor");
else if(old == nil || old.cursor != new.cursor)
wmclient->win.wmctl(new.cursor);
if(new != nil && (new.flags & Kbdstarted) == 0)
return;
if(old != nil)
old.ctl <-= "haskbdfocus 0";
if(new != nil){
new.ctl <-= "raise";
new.ctl <-= "haskbdfocus 1";
kbdfocus = new;
} else
kbdfocus = nil;
}
makescreen(img: ref Image): ref Screen
{
screen = Screen.allocate(img, img.display.color(Background), 0);
img.draw(img.r, screen.fill, nil, screen.fill.r.min);
return screen;
}
kill(pid: int, note: string): int
{
fd := sys->open("/prog/"+string pid+"/ctl", Sys->OWRITE);
if(fd == nil || sys->fprint(fd, "%s", note) < 0)
return -1;
return 0;
}
fatal(s: string)
{
sys->fprint(sys->fildes(2), "wm: %s\n", s);
kill(sys->pctl(0, nil), "killgrp");
raise "fail:error";
}
# fit a window rectangle to the available space.
# try to preserve requested location if possible.
# make sure that the window is no bigger than
# the screen, and that its top and left-hand edges
# will be visible at least.
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;
}
lastrect: Rect;
# 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;
}
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;
}
}
}
command(ctxt: ref Draw->Context, args: list of string, sync: chan of string)
{
if((sh := load Sh Sh->PATH) != nil){
sh->run(ctxt, "{$*&}" :: args);
sync <-= nil;
return;
}
fds := list of {0, 1, 2};
sys->pctl(sys->NEWFD, fds);
cmd := hd args;
file := cmd;
if(len file<4 || file[len file-4:]!=".dis")
file += ".dis";
c := load Wm file;
if(c == nil) {
err := sys->sprint("%r");
if(err != "permission denied" && err != "access permission denied" && file[0]!='/' && file[0:2]!="./"){
c = load Wm "/dis/"+file;
if(c == nil)
err = sys->sprint("%r");
}
if(c == nil){
sync <-= sys->sprint("%s: %s\n", cmd, err);
exit;
}
}
sync <-= nil;
c->init(ctxt, args);
}
|