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
|
implement Wmsrv;
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
Display, Image, Point, Rect, Screen, Pointer, Context, Wmcontext: import draw;
include "wmsrv.m";
zorder: ref Client; # top of z-order list, linked by znext.
ZR: con Rect((0, 0), (0, 0));
Iqueue: adt {
h, t: list of int;
n: int;
put: fn(q: self ref Iqueue, s: int);
get: fn(q: self ref Iqueue): int;
peek: fn(q: self ref Iqueue): int;
nonempty: fn(q: self ref Iqueue): int;
};
Squeue: adt {
h, t: list of string;
n: int;
put: fn(q: self ref Squeue, s: string);
get: fn(q: self ref Squeue): string;
peek: fn(q: self ref Squeue): string;
nonempty: fn(q: self ref Squeue): int;
};
# Ptrqueue is the same as the other queues except it merges events
# that have the same button state.
Ptrqueue: adt {
last: ref Pointer;
h, t: list of ref Pointer;
put: fn(q: self ref Ptrqueue, s: ref Pointer);
get: fn(q: self ref Ptrqueue): ref Pointer;
peek: fn(q: self ref Ptrqueue): ref Pointer;
nonempty: fn(q: self ref Ptrqueue): int;
flush: fn(q: self ref Ptrqueue);
};
init(): (chan of (string, chan of (string, ref Wmcontext)),
chan of (ref Client, chan of string),
chan of (ref Client, array of byte, Sys->Rwrite))
{
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
sys->bind("#s", "/chan", Sys->MBEFORE);
ctlio := sys->file2chan("/chan", "wmctl");
if(ctlio == nil){
sys->werrstr(sys->sprint("can't create /chan/wmctl: %r"));
return (nil, nil, nil);
}
wmreq := chan of (string, chan of (string, ref Wmcontext));
join := chan of (ref Client, chan of string);
req := chan of (ref Client, array of byte, Sys->Rwrite);
spawn wm(ctlio, wmreq, join, req);
return (wmreq, join, req);
}
wm(ctlio: ref Sys->FileIO,
wmreq: chan of (string, chan of (string, ref Wmcontext)),
join: chan of (ref Client, chan of string),
req: chan of (ref Client, array of byte, Sys->Rwrite))
{
clients: array of ref Client;
for(;;)alt{
(cmd, rc) := <-wmreq =>
token := int cmd;
for(i := 0; i < len clients; i++)
if(clients[i] != nil && clients[i].token == token)
break;
if(i == len clients){
spawn senderror(rc, "not found");
break;
}
c := clients[i];
if(c.stop != nil){
spawn senderror(rc, "already started");
break;
}
ok := chan of string;
join <-= (c, ok);
if((e := <-ok) != nil){
spawn senderror(rc, e);
break;
}
c.stop = chan of int;
spawn childminder(c, rc);
(nil, nbytes, fid, rc) := <-ctlio.read =>
if(rc == nil)
break;
c := findfid(clients, fid);
if(c == nil){
c = ref Client(
chan of int,
chan of ref Draw->Pointer,
chan of string,
nil,
0,
nil,
nil,
nil,
chan of (ref Point, ref Image, chan of int),
-1,
fid,
fid, # token; XXX could be random integer + fid
newwmcontext()
);
clients = addclient(clients, c);
}
alt{
rc <-= (sys->aprint("%d", c.token), nil) => ;
* => ;
}
(nil, data, fid, wc) := <-ctlio.write =>
c := findfid(clients, fid);
if(wc != nil){
if(c == nil){
alt{
wc <-= (0, "must read first") => ;
* => ;
}
break;
}
req <-= (c, data, wc);
}else if(c != nil){
req <-= (c, nil, nil);
delclient(clients, c);
}
}
}
# buffer all events between a window manager and
# a client, so that one recalcitrant child can't
# clog the whole system.
childminder(c: ref Client, rc: chan of (string, ref Wmcontext))
{
wmctxt := c.wmctxt;
dummykbd := chan of int;
dummyptr := chan of ref Pointer;
dummyimg := chan of ref Image;
dummyctl := chan of string;
kbdq := ref Iqueue;
ptrq := ref Ptrqueue;
ctlq := ref Squeue;
Imgnone, Imgsend, Imgsendnil1, Imgsendnil2, Imgorigin: con iota;
img, sendimg: ref Image;
imgorigin: Point;
imgstate := Imgnone;
# send reply to client, but make sure we don't block.
Reply:
for(;;) alt{
rc <-= (nil, ref *wmctxt) =>
break Reply;
<-c.stop =>
exit;
key := <-c.kbd =>
kbdq.put(key);
ptr := <-c.ptr =>
ptrq.put(ptr);
ctl := <-c.ctl =>
ctlq.put(ctl);
}
for(;;){
outkbd := dummykbd;
key := -1;
if(kbdq.nonempty()){
key = kbdq.peek();
outkbd = wmctxt.kbd;
}
outptr := dummyptr;
ptr: ref Pointer;
if(ptrq.nonempty()){
ptr = ptrq.peek();
outptr = wmctxt.ptr;
}
outctl := dummyctl;
ctl: string;
if(ctlq.nonempty()){
ctl = ctlq.peek();
outctl = wmctxt.ctl;
}
outimg := dummyimg;
case imgstate{
Imgsend =>
outimg = wmctxt.images;
sendimg = img;
Imgsendnil1 or
Imgsendnil2 or
Imgorigin =>
outimg = wmctxt.images;
sendimg = nil;
}
alt{
outkbd <-= key =>
kbdq.get();
outptr <-= ptr =>
ptrq.get();
outctl <-= ctl =>
ctlq.get();
outimg <-= sendimg =>
case imgstate{
Imgsend =>
imgstate = Imgnone;
img = sendimg = nil;
Imgsendnil1 =>
imgstate = Imgsendnil2;
Imgsendnil2 =>
imgstate = Imgnone;
Imgorigin =>
if(img.origin(imgorigin, imgorigin) == -1){
# XXX what can we do about this? there's no way at the moment
# of getting the information about the origin failure back to the wm,
# so we end up with an inconsistent window position.
# if the window manager blocks while we got the sync from
# the client, then a client could block the whole window manager
# which is what we're trying to avoid.
# but there's no other time we could set the origin of the window,
# and not risk mucking up the window contents.
# the short answer is that running out of image space is Bad News.
}
imgstate = Imgsend;
}
# XXX could mark the application as unresponding if any of these queues
# start growing too much.
ch := <-c.kbd =>
kbdq.put(ch);
p := <-c.ptr =>
if(p == nil)
ptrq.flush();
else
ptrq.put(p);
e := <-c.ctl =>
ctlq.put(e);
(o, i, reply) := <-c.images =>
# can't queue multiple image requests.
if(imgstate != Imgnone)
reply <-= -1;
else {
# if the origin is being set, then we first send a nil image
# to indicate that this is happening, and then the
# image itself (reorigined).
# if a nil image is being set, then we
# send nil twice.
if(o != nil){
imgorigin = *o;
imgstate = Imgorigin;
img = i;
}else if(i != nil){
img = i;
imgstate = Imgsend;
}else
imgstate = Imgsendnil1;
reply <-= 0;
}
<-c.stop =>
# XXX do we need to unblock channels, kill, etc.?
# we should perhaps drain the ctl output channel here
# if possible, exiting if it times out.
exit;
}
}
}
findfid(clients: array of ref Client, fid: int): ref Client
{
for(i := 0; i < len clients; i++)
if(clients[i] != nil && clients[i].fid == fid)
return clients[i];
return nil;
}
addclient(clients: array of ref Client, c: ref Client): array of ref Client
{
for(i := 0; i < len clients; i++)
if(clients[i] == nil){
clients[i] = c;
c.id = i;
return clients;
}
nc := array[len clients + 4] of ref Client;
nc[0:] = clients;
nc[len clients] = c;
c.id = len clients;
return nc;
}
delclient(clients: array of ref Client, c: ref Client)
{
clients[c.id] = nil;
}
senderror(rc: chan of (string, ref Wmcontext), e: string)
{
rc <-= (e, nil);
}
Client.window(c: self ref Client, tag: string): ref Window
{
for (w := c.wins; w != nil; w = tl w)
if((hd w).tag == tag)
return hd w;
return nil;
}
Client.image(c: self ref Client, tag: string): ref Draw->Image
{
w := c.window(tag);
if(w != nil)
return w.img;
return nil;
}
Client.setimage(c: self ref Client, tag: string, img: ref Draw->Image): int
{
# if img is nil, remove window from list.
if(img == nil){
# usual case:
if(c.wins != nil && (hd c.wins).tag == tag){
c.wins = tl c.wins;
return -1;
}
nw: list of ref Window;
for (w := c.wins; w != nil; w = tl w)
if((hd w).tag != tag)
nw = hd w :: nw;
c.wins = nil;
for(; nw != nil; nw = tl nw)
c.wins = hd nw :: c.wins;
return -1;
}
for(w := c.wins; w != nil; w = tl w)
if((hd w).tag == tag)
break;
win: ref Window;
if(w != nil)
win = hd w;
else{
win = ref Window(tag, ZR, nil);
c.wins = win :: c.wins;
}
win.img = img;
win.r = img.r; # save so clients can set logical origin
rc := chan of int;
c.images <-= (nil, img, rc);
return <-rc;
}
# tell a client about a window that's moved to screen coord o.
Client.setorigin(c: self ref Client, tag: string, o: Draw->Point): int
{
w := c.window(tag);
if(w == nil)
return -1;
img := w.img;
if(img == nil)
return -1;
rc := chan of int;
c.images <-= (ref o, w.img, rc);
if(<-rc != -1){
w.r = (o, o.add(img.r.size()));
return 0;
}
return -1;
}
clientimages(c: ref Client): array of ref Image
{
a := array[len c.wins] of ref Draw->Image;
i := 0;
for(w := c.wins; w != nil; w = tl w)
if((hd w).img != nil)
a[i++] = (hd w).img;
return a[0:i];
}
Client.top(c: self ref Client)
{
imgs := clientimages(c);
if(len imgs > 0)
imgs[0].screen.top(imgs);
if(zorder == c)
return;
prev: ref Client;
for(z := zorder; z != nil; (prev, z) = (z, z.znext))
if(z == c)
break;
if(prev != nil)
prev.znext = c.znext;
c.znext = zorder;
zorder = c;
}
Client.bottom(c: self ref Client)
{
if(c.znext == nil)
return;
imgs := clientimages(c);
if(len imgs > 0)
imgs[0].screen.bottom(imgs);
prev: ref Client;
for(z := zorder; z != nil; (prev, z) = (z, z.znext))
if(z == c)
break;
if(prev != nil)
prev.znext = c.znext;
else
zorder = c.znext;
z = c.znext;
c.znext = nil;
for(; z != nil; (prev, z) = (z, z.znext))
;
if(prev != nil)
prev.znext = c;
else
zorder = c;
}
Client.hide(nil: self ref Client)
{
}
Client.unhide(nil: self ref Client)
{
}
Client.remove(c: self ref Client)
{
prev: ref Client;
for(z := zorder; z != nil; (prev, z) = (z, z.znext))
if(z == c)
break;
if(z == nil)
return;
if(prev != nil)
prev.znext = z.znext;
else if(z != nil)
zorder = zorder.znext;
}
find(p: Draw->Point): ref Client
{
for(z := zorder; z != nil; z = z.znext)
if(z.contains(p))
return z;
return nil;
}
top(): ref Client
{
return zorder;
}
Client.contains(c: self ref Client, p: Point): int
{
for(w := c.wins; w != nil; w = tl w)
if((hd w).r.contains(p))
return 1;
return 0;
}
r2s(r: Rect): string
{
return string r.min.x + " " + string r.min.y + " " +
string r.max.x + " " + string r.max.y;
}
newwmcontext(): ref Wmcontext
{
return ref Wmcontext(
chan of int,
chan of ref Pointer,
chan of string,
nil,
chan of ref Image,
nil,
nil
);
}
Iqueue.put(q: self ref Iqueue, s: int)
{
q.t = s :: q.t;
}
Iqueue.get(q: self ref Iqueue): int
{
s := -1;
if(q.h == nil){
for(t := q.t; t != nil; t = tl t)
q.h = hd t :: q.h;
q.t = nil;
}
if(q.h != nil){
s = hd q.h;
q.h = tl q.h;
}
return s;
}
Iqueue.peek(q: self ref Iqueue): int
{
s := -1;
if (q.h == nil && q.t == nil)
return s;
s = q.get();
q.h = s :: q.h;
return s;
}
Iqueue.nonempty(q: self ref Iqueue): int
{
return q.h != nil || q.t != nil;
}
Squeue.put(q: self ref Squeue, s: string)
{
q.t = s :: q.t;
}
Squeue.get(q: self ref Squeue): string
{
s: string;
if(q.h == nil){
for(t := q.t; t != nil; t = tl t)
q.h = hd t :: q.h;
q.t = nil;
}
if(q.h != nil){
s = hd q.h;
q.h = tl q.h;
}
return s;
}
Squeue.peek(q: self ref Squeue): string
{
s: string;
if (q.h == nil && q.t == nil)
return s;
s = q.get();
q.h = s :: q.h;
return s;
}
Squeue.nonempty(q: self ref Squeue): int
{
return q.h != nil || q.t != nil;
}
Ptrqueue.put(q: self ref Ptrqueue, s: ref Pointer)
{
if(q.last != nil && s.buttons == q.last.buttons)
*q.last = *s;
else{
q.t = s :: q.t;
q.last = s;
}
}
Ptrqueue.get(q: self ref Ptrqueue): ref Pointer
{
s: ref Pointer;
h := q.h;
if(h == nil){
for(t := q.t; t != nil; t = tl t)
h = hd t :: h;
q.t = nil;
}
if(h != nil){
s = hd h;
h = tl h;
if(h == nil)
q.last = nil;
}
q.h = h;
return s;
}
Ptrqueue.peek(q: self ref Ptrqueue): ref Pointer
{
s: ref Pointer;
if (q.h == nil && q.t == nil)
return s;
t := q.last;
s = q.get();
q.h = s :: q.h;
q.last = t;
return s;
}
Ptrqueue.nonempty(q: self ref Ptrqueue): int
{
return q.h != nil || q.t != nil;
}
Ptrqueue.flush(q: self ref Ptrqueue)
{
q.h = q.t = nil;
}
|