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
|
implement Wmclient;
#
# Copyright © 2003 Vita Nuova Holdings Limited
#
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
Display, Image, Screen, Rect, Point, Pointer, Wmcontext, Context: import draw;
include "tk.m";
tk: Tk;
Toplevel: import tk;
include "wmlib.m";
wmlib: Wmlib;
qword, splitqword, s2r: import wmlib;
include "titlebar.m";
titlebar: Titlebar;
include "wmclient.m";
Focusnone, Focusimage, Focustitle: con iota;
Bdup: con int 16rffffffff;
Bddown: con int 16radadadff;
init()
{
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
tk = load Tk Tk->PATH;
wmlib = load Wmlib Wmlib->PATH;
if(wmlib == nil){
sys->fprint(sys->fildes(2), "wmclient: cannot load %s: %r\n", Wmlib->PATH);
raise "fail:bad module";
}
wmlib->init();
titlebar = load Titlebar Titlebar->PATH;
if(titlebar == nil){
sys->fprint(sys->fildes(2), "wmclient: cannot load %s: %r\n", Titlebar->PATH);
raise "fail:bad module";
}
titlebar->init();
}
makedrawcontext(): ref Draw->Context
{
return wmlib->makedrawcontext();
}
cursorspec(img: ref Draw->Image): string
{
Hex: con "0123456789abcdef";
if(img == nil || img.depth != 1)
return "cursor";
display := img.display;
hot := img.r.min;
if(img.r.min.x != 0 || img.r.min.y != 0){
n := display.newimage(((0, 0), img.r.size()), Draw->GREY1, 0, Draw->Nofill);
n.draw(n.r, img, nil, img.r.min);
img = n;
}
s := sys->sprint("cursor %d %d %d %d ", hot.x, hot.y, img.r.dx(), img.r.dy());
nb := img.r.dy() * draw->bytesperline(img.r, img.depth);
buf := array[nb] of byte;
if(img.readpixels(img.r, buf) == -1)
return "cursor";
for(i := 0; i < nb; i++){
c := int buf[i];
s[len s] = Hex[c >> 4];
s[len s] = Hex[c & 16rf];
}
return s;
}
blankwin: Window;
window(ctxt: ref Draw->Context, title: string, buts: int): ref Window
{
w := ref blankwin;
w.ctxt = wmlib->connect(ctxt);
w.display = ctxt.display;
w.ctl = chan of string;
readscreenrect(w);
if(buts & Plain)
return w;
if(ctxt.wm == nil)
buts &= ~(Resize|Hide);
w.bd = 1;
w.titlebar = tk->toplevel(ctxt.display, nil);
top := w.titlebar;
top.wreq = nil;
w.ctl = titlebar->new(top, buts);
titlebar->settitle(top, title);
sizetb(w);
w.wmctl("fixedorigin");
return w;
}
Window.pointer(w: self ref Window, p: Draw->Pointer): int
{
if(w.screen == nil || w.titlebar == nil)
return 0;
if(p.buttons && (w.ptrfocus == Focusnone || w.buttons == 0)){
if(p.xy.in(w.tbrect))
w.ptrfocus = Focustitle;
else
w.ptrfocus = Focusimage;
}
w.buttons = p.buttons;
if(w.ptrfocus == Focustitle){
tk->pointer(w.titlebar, p);
return 1;
}
return 0;
}
# titlebar requested size might have changed:
# find out what size it's requesting.
sizetb(w: ref Window)
{
if(w.titlebar == nil)
return;
w.tbsize = tk->rect(w.titlebar, ".", Tk->Border|Tk->Required).size();
}
# reshape the image; the space needed for the
# titlebar is added to r.
Window.reshape(w: self ref Window, r: Rect)
{
w.r = w.screenr(r);
if(w.screen == nil)
return;
w.wmctl(sys->sprint("!reshape . -1 %s", r2s(w.r)));
}
putimage(w: ref Window, i: ref Image)
{
if(w.screen != nil && i == w.screen.image)
return;
w.screen = Screen.allocate(i, w.display.color(Draw->White), 0);
ir := i.r.inset(w.bd);
if(ir.dx() < 0)
ir.max.x = ir.min.x;
if(ir.dy() < 0)
ir.max.y = ir.min.y;
if(w.titlebar != nil){
w.tbrect = Rect(ir.min, (ir.max.x, ir.min.y + w.tbsize.y));
tbimage := w.screen.newwindow(w.tbrect, Draw->Refnone, Draw->Nofill);
tk->putimage(w.titlebar, ".", tbimage, nil);
ir.min.y = w.tbrect.max.y;
}
if(ir.dy() < 0)
ir.max.y = ir.min.y;
w.image = w.screen.newwindow(ir, Draw->Refnone, Draw->Nofill);
drawborder(w);
w.r = i.r;
}
# return a rectangle suitable to hold image r when the
# titlebar and border are included.
Window.screenr(w: self ref Window, r: Rect): Rect
{
if(w.titlebar != nil){
if(r.dx() < w.tbsize.x)
r.max.x = r.min.x + w.tbsize.x;
r.min.y -= w.tbsize.y;
}
return r.inset(-w.bd);
}
# return the available space inside r when space for
# border and titlebar is taken away.
Window.imager(w: self ref Window, r: Rect): Rect
{
r = r.inset(w.bd);
if(r.dx() < 0)
r.max.x = r.min.x;
if(r.dy() < 0)
r.max.y = r.min.y;
if(w.titlebar != nil){
r.min.y += w.tbsize.y;
if(r.dy() < 0)
r.max.y = r.min.y;
}
return r;
}
# draw an imitation tk border.
drawborder(w: ref Window)
{
if(w.screen == nil)
return;
col := w.display.color(Bdup);
i := w.screen.image;
r := w.screen.image.r;
i.draw((r.min, (r.min.x+w.bd, r.max.y)), col, nil, (0, 0));
i.draw(((r.min.x+w.bd, r.min.y), (r.max.x, r.min.y+w.bd)), col, nil, (0, 0));
col = w.display.color(Bddown);
i.draw(((r.max.x-w.bd, r.min.y+w.bd), r.max), col, nil, (0, 0));
i.draw(((r.min.x+w.bd, r.max.y-w.bd), (r.max.x-w.bd, r.max.y)), col, nil, (0, 0));
}
readscreenrect(w: ref Window)
{
if((fd := sys->open("/chan/wmrect", Sys->OREAD)) != nil){
buf := array[12*4] of byte;
n := sys->read(fd, buf, len buf);
if(n > 0){
(w.displayr, nil) = s2r(string buf[0:n], 0);
return;
}
}
w.displayr = w.display.image.r;
}
Window.onscreen(w: self ref Window, how: string)
{
if(how == nil)
how = "place";
w.wmctl(sys->sprint("!reshape . -1 %s %q", r2s(w.r), how));
}
Window.startinput(w: self ref Window, devs: list of string)
{
for(; devs != nil; devs = tl devs)
w.wmctl(sys->sprint("start %q", hd devs));
}
# commands originating both from tkclient and wm (via ctl)
Window.wmctl(w: self ref Window, req: string): string
{
(c, next) := qword(req, 0);
case c {
"exit" =>
sys->fprint(sys->open("/prog/" + string sys->pctl(0, nil) + "/ctl", Sys->OWRITE), "killgrp");
exit;
# old-style requests: pass them back around in proper form.
"move" =>
# move x y
if(w.titlebar != nil)
titlebar->sendctl(w.titlebar, "!move . -1 " + req[next:]);
"size" =>
if(w.titlebar != nil){
minsz := titlebar->minsize(w.titlebar);
titlebar->sendctl(w.titlebar, "!size . -1 " + string minsz.x + " " + string minsz.y);
}
"ok" or
"help" =>
;
"rect" =>
(w.displayr, nil) = s2r(req, next);
"haskbdfocus" =>
w.focused = int qword(req, next).t0;
if(w.titlebar != nil){
tk->cmd(w.titlebar, "focus -global " + string w.focused);
tk->cmd(w.titlebar, "update");
}
drawborder(w);
"task" =>
title := "";
if(w.titlebar != nil)
title = titlebar->title(w.titlebar);
wmreq(w, sys->sprint("task %q", title), next);
w.saved = w.r.min;
# send window out of the way
# XXX oops, can't do this for plain windows...
titlebar->sendctl(w.titlebar, "!reshape . -1 " + r2s((w.displayr.max, w.displayr.max.add(w.r.size()))));
"untask" =>
wmreq(w, req, next);
# put window back where it was before.
# XXX what do we we do if the window manager window has been reshape in the meantime...?
titlebar->sendctl(w.titlebar, "!reshape . -1 " + r2s((w.saved, w.saved.add(w.r.size()))));
* =>
return wmreq(w, req, next);
}
return nil;
}
wmreq(w: ref Window, req: string, e: int): string
{
name: string;
if(req != nil && req[0] == '!'){
(name, e) = qword(req, e);
if(name != ".")
return "invalid window name";
}
if(w.ctxt.connfd != nil){
if(sys->fprint(w.ctxt.connfd, "%s", req) == -1)
return sys->sprint("%r");
if(req[0] == '!')
recvimage(w);
return nil;
}
# if we're getting an image and there's no window manager,
# then there's only one image to get...
if(req[0] == '!')
putimage(w, w.ctxt.ctxt.display.image);
else{
(nil, nil, err) := wmlib->wmctl(w.ctxt, req);
return err;
}
return nil;
}
recvimage(w: ref Window)
{
i := <-w.ctxt.images;
if(i == nil)
i = <-w.ctxt.images;
putimage(w, i);
}
Window.settitle(w: self ref Window, title: string): string
{
if(w.titlebar == nil)
return nil;
oldr := w.imager(w.r);
old := titlebar->settitle(w.titlebar, title);
sizetb(w);
if(w.tbsize.x < w.r.dx())
tk->putimage(w.titlebar, ".", w.titlebar.image, nil); # unsuspend the window
else
w.wmctl("!reshape . -1 " + r2s(w.screenr(oldr)));
return old;
}
snarfget(): string
{
return wmlib->snarfget();
}
snarfput(buf: string)
{
return wmlib->snarfput(buf);
}
r2s(r: Rect): string
{
return sys->sprint("%d %d %d %d", r.min.x, r.min.y, r.max.x, r.max.y);
}
|