summaryrefslogtreecommitdiff
path: root/appl/cmd/rioimport.b
blob: 499800004fa93ac01341646607835a8176b4717c (plain)
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
implement Rioimport;
include "sys.m";
	sys: Sys;
include "draw.m";
	draw: Draw;
	Image, Point, Rect, Display, Screen: import draw;
include "wmsrv.m";
	wmsrv: Wmsrv;
include "sh.m";
	sh: Sh;
include "string.m";
	str: String;

Rioimport: module{
	init: fn(nil: ref Draw->Context, argv: list of string);
};

Client: adt{
	ptrstarted:	int;
	kbdstarted:	int;
	state:		int;		# Hidden|Current
	req:		chan of (array of byte, Sys->Rwrite);
	resize:	chan of ref Riowin;
	ptr:		chan of ref Draw->Pointer;
	riowctl:	chan of (ref Riowin, int);
	wins:	list of ref Riowin;
	winfd:	ref Sys->FD;
	sc: 		ref Wmsrv->Client;
};

Riowin: adt {
	tag:		string;
	img:		ref Image;
	dir:		string;
	state:	int;
	ptrpid:	int;
	kbdpid:	int;
	ctlpid:	int;
	ptrfd:	ref Sys->FD;
	ctlfd:		ref Sys->FD;
};

Hidden, Current: con 1<<iota;
Ptrsize: con 1+4*12;		# 'm' plus 4 12-byte decimal integers
P9PATH: con "/n/local";
Borderwidth: con 4;		# defined in /sys/include/draw.h

display: ref Display;
wsysseq := 0;
screenr := Rect((0, 0), (640, 480));	# no way of getting this reliably from rio

Minwinsize: con Point(100, 42);

init(nil: ref Draw->Context, argv: list of string)
{
	sys = load Sys Sys->PATH;
	draw = load Draw Draw->PATH;
	sh = load Sh Sh->PATH;
	sh->initialise();
	str = load String String->PATH;
	wmsrv = load Wmsrv Wmsrv->PATH;

	wc := chan of (ref Draw->Context, string);
	spawn rioproxy(wc);
	(ctxt, err) := <-wc;
	if(err != nil){
		sys->fprint(sys->fildes(2), "rioimport: %s\n", err);
		raise "fail:no display";
	}
	sh->run(ctxt, tl argv);
}

ebind(a, b: string, flag: int)
{
	if(sys->bind(a, b, flag) == -1){
		sys->fprint(sys->fildes(2), "rioimport: cannot bind %q onto %q: %r\n", a, b);
		raise "fail:error";
	}
}

rioproxy(wc: chan of (ref Draw->Context, string))
{
	{
		rioproxy1(wc);
	} exception e {
	"fail:*" =>
		wc <-= (nil, e[5:]);
	}
}

rioproxy1(wc: chan of (ref Draw->Context, string))
{
	sys->pctl(Sys->NEWFD, 0 :: 1 :: 2 :: nil);

	ebind("#U*", P9PATH, Sys->MREPL);
	display = Display.allocate(P9PATH + "/dev");
	if(display == nil)
		raise sys->sprint("fail:cannot allocate display: %r");


	(wm, join, req) := wmsrv->init();
	if(wm == nil){
		wc <-= (nil, sys->sprint("%r"));
		return;
	}
	readscreenr();
	wc <-= (ref Draw->Context(display, nil, wm), nil);

	sys->pctl(Sys->FORKNS, nil);
	ebind("#₪", "/srv", Sys->MREPL|Sys->MCREATE);
	if(sys->bind(P9PATH+"/dev/draw", "/dev/draw", Sys->MREPL) == -1)
		ebind(P9PATH+"/dev", "/dev", Sys->MAFTER);
	sh->run(nil, "mount" :: "{mntgen}" :: "/mnt" :: nil);

	clients: array of ref Client;
	nc := 0;
	for(;;) alt{
	(sc, rc) := <-join =>
		if(nc != 0)
			rc <-= "only one client available";
		sync := chan of (ref Client, string);
		spawn clientproc(sc,sync);
		(c, err) := <-sync;
		rc <-= err;
		if(c != nil){
			if(sc.id >= len clients)
				clients = (array[sc.id + 1] of ref Client)[0:] = clients;
			clients[sc.id] = c;
		}
	(sc, data, rc) := <-req =>
		clients[sc.id].req <-= (data, rc);
		if(rc == nil)
			clients[sc.id] = nil;
	}
}
zclient: Client;
clientproc(sc: ref Wmsrv->Client, rc: chan of (ref Client, string))
{
	c := ref zclient;
	c.req = chan of (array of byte, Sys->Rwrite);
	c.resize = chan of ref Riowin;
	c.ptr = chan of ref Draw->Pointer;
	c.riowctl = chan of (ref Riowin, int);
	c.sc = sc;
	rc <-= (c, nil);

loop:
	for(;;) alt{
	(data, drc) := <-c.req =>
		if(drc == nil)
			break loop;
		err := handlerequest(c, data);
		n := len data;
		if(err != nil)
			n = -1;
		alt{
		drc <-= (n, err) =>;
		* =>;
		}
	p := <-c.ptr =>
		sc.ptr <-= p;
	w := <-c.resize =>
		if((c.state & Hidden) == 0)
			sc.ctl <-= sys->sprint("!reshape %q -1 0 0 0 0 getwin", w.tag);
	(w, state) := <-c.riowctl =>
		if((c.state^state)&Current)
			sc.ctl <-= "haskbdfocus " + string ((state & Current)!=0);
		if((c.state^state)&Hidden){
			s := "unhide";
			if(state&Hidden)
				s = "hide";
			for(wl := c.wins; wl != nil; wl = tl wl){
				if(hd wl != w)
					rioctl(hd wl, s);
				if(c.state&Hidden)
					sc.ctl <-= sys->sprint("!reshape %q -1 0 0 0 0 getwin", (hd wl).tag);
			}
		}
		c.state = state;
		w.state = state;
	}
	sc.stop <-= 1;
	for(wl := c.wins; wl != nil; wl = tl wl)
		delwin(hd wl);
}

handlerequest(c: ref Client, data: array of byte): string
{
	req := string data;
#sys->print("%d: %s\n", c.sc.id, req);
	if(req == nil)
		return "no request";
	args := str->unquoted(req);
	n := len args;
	case hd args {
	"key" =>
		return "permission denied";
	"ptr" =>
		# ptr x y
		if(n != 3)
			return "bad arg count";
		if(c.ptrstarted == 0)
			return "pointer not active";
		for(w := c.wins; w != nil; w = tl w){
			if((hd w).ptrfd != nil){
				sys->fprint((hd w).ptrfd, "m%11d %11d", int hd tl args, int hd tl tl args);
				return nil;
			}
		}
		return "no windows";
	"start" =>
		if(n != 2)
			return "bad arg count";
		case hd tl args {
		"ptr" or
		"mouse" =>
			if(c.ptrstarted == -1)
				return "already started";
			sync := chan of int;
			for(w := c.wins; w != nil; w = tl w){
				spawn ptrproc(hd w, c.ptr, c.resize, sync);
				(hd w).ptrpid = <-sync;
			}
			c.ptrstarted = 1;
			return nil;
		"kbd" =>
			if(c.kbdstarted == -1)
				return "already started";
			sync := chan of int;
			for(w := c.wins; w != nil; w = tl w){
				spawn kbdproc(hd w, c.sc.kbd, sync);
				(hd w).kbdpid = <-sync;
			}
			return nil;
		* =>
			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(r.dx() < Minwinsize.x)
			r.max.x = r.min.x + Minwinsize.x;
		if(r.dy() < Minwinsize.y)
			r.max.y = r.min.y + Minwinsize.y;

		spec := "";
		if(args != nil){
			case hd args{
			"onscreen" =>
				r = fitrect(r, screenr).inset(-Borderwidth);
				spec = "-r " + r2s(r);
			"place" =>
				r = fitrect(r, screenr).inset(-Borderwidth);
				spec = "-dx " + string r.dx() + " -dy " + string r.dy();
			"exact" =>
				spec = "-r " + r2s(r.inset(-Borderwidth));
			"max" =>
				r = screenr;			# XXX don't obscure toolbar?
				spec = "-r " + r2s(r.inset(Borderwidth));
			"getwin" =>
				;						# just get the new image
			* =>
				return "unkown placement method";
			}
		}else
			spec = "-r " + r2s(r.inset(-Borderwidth));
		return reshape(c, tag, spec);
	"delete" =>
		# delete tag
		if(tl args == nil)
			return "tag required";
		tag := hd tl args;
		nw: list of ref Riowin;
		for(w := c.wins; w != nil; w = tl w){
			if((hd w).tag == tag){
				delwin(hd w);
				wmsrv->c.sc.setimage(tag, nil);
			}else
				nw = hd w :: nw;
		}
		c.wins = nil;
		for(; nw != nil; nw = tl nw)
			c.wins = hd nw :: c.wins;
	"label" =>
		if(n != 2)
			return "bad arg count";
		for(w := c.wins; w != nil; w = tl w)
			setlabel(hd w, hd tl args);
	"raise" =>
		for(w := c.wins; w != nil; w = tl w){
			rioctl(hd w, "top");
			if(tl w == nil)
				rioctl(hd w, "current");
		}
	"lower" =>
		for(w := c.wins; w != nil; w = tl w)
			rioctl(hd w, "bottom");
	"task" =>
		if(n != 2)
			return "bad arg count";
		c.state |= Hidden;
		for(w := c.wins; w != nil; w = tl w){
			setlabel(hd w, hd tl args);
			rioctl(hd w, "hide");
		}
	"untask" =>
		wins: list of ref Riowin;
		for(w := c.wins; w != nil; w = tl w)
			wins = hd w :: wins;
		for(; wins != nil; wins = tl wins)
			rioctl(hd wins, "unhide");
	"!move" =>
		# !move tag reqid startx starty
		if(n != 5)
			return "bad arg count";
		args = tl args;
		tag := hd args; args = tl args;
		args = tl args;
		w := wmsrv->c.sc.window(tag);
		if(w == nil)
			return "no such tag";
		return dragwin(c.ptr, c, w, Point(int hd args, int hd tl args));
	"!size" =>
		return "nope";
	"kbdfocus" =>
		if(n != 2)
			return "bad arg count";
		if(int hd tl args){
			if(c.wins != nil)
				return rioctl(hd c.wins, "current");
		}
		return nil;
	* =>
		return "unknown request";
	}
	return nil;
}

dragwin(ptr: chan of ref Draw->Pointer, c: ref Client, w: ref Wmsrv->Window, click: Point): string
{
#	if(buttons == 0)
#		return "too late";
	p: ref Draw->Pointer;
	img := w.img.screen.image;
	r := img.r;
	off := click.sub(r.min);
	do{
		p = <-ptr;
		img.origin(r.min, p.xy.sub(off));
	} while (p.buttons != 0);
	c.sc.ptr <-= p;
#	buttons = 0;
	nr: Rect;
	nr.min = p.xy.sub(off);
	nr.max = nr.min.add(r.size());
	if(nr.eq(r))
		return "not moved";
	reshape(c, w.tag, "-r " + r2s(nr));
	return nil;
}

rioctl(w: ref Riowin, req: string): string
{
	if(sys->fprint(w.ctlfd, "%s", req) == -1){
#sys->print("rioctl fail %s: %s: %r\n", w.dir, req);
		return sys->sprint("%r");
}
#sys->print("rioctl %s: %s\n", w.dir, req);
	return nil;
}

reshape(c: ref Client, tag: string, spec: string): string
{
	for(wl := c.wins; wl != nil; wl = tl wl)
		if((hd wl).tag == tag)
			break;
	if(wl == nil){
		(w, e) := newwin(c, tag, spec);
		if(w == nil){
sys->print("can't make new win (spec %q): %s\n", spec, e);
			return e;
		}
		c.wins = w :: c.wins;
		wmsrv->c.sc.setimage(tag, w.img);
		sync := chan of int;
		if(c.kbdstarted){
			spawn kbdproc(w, c.sc.kbd, sync);
			w.kbdpid = <-sync;
		}
		if(c.ptrstarted){
			spawn ptrproc(w, c.ptr, c.resize, sync);
			w.ptrpid = <-sync;
		}
		return nil;
	}
	w := hd wl;
	if(spec != nil){
		e := rioctl(w, "resize " + spec);
		if(e != nil)
			return e;
	}
	getwin(w);
	if(w.img == nil)
		return "getwin failed";
	wmsrv->c.sc.setimage(tag, w.img);
	return nil;
}

zriowin: Riowin;
newwin(c: ref Client, tag, spec: string): (ref Riowin, string)
{
	wsys := readfile(P9PATH + "/env/wsys");
	if(wsys == nil)
		return (nil, "no $wsys");
	
	d := "/mnt/"+string wsysseq++;
	fd := sys->open(wsys, Sys->ORDWR);
	if(fd == nil)
		return (nil, sys->sprint("cannot open %q: %r\n", wsys));
	# XXX this won't multiplex properly - srv9 should export attach files (actually that's what plan 9 should do)
	if(sys->mount(fd, nil, d, Sys->MREPL, "new "+spec) == -1)
		return (nil, sys->sprint("mount %q failed: %r", wsys));
	(ok, nil) := sys->stat(d + "/winname");
	if(ok == -1)
		return (nil, "could not make window");
	w := ref zriowin;
	w.tag = tag;
	w.dir = d;
	getwin(w);
	w.ctlfd = sys->open(d + "/wctl", Sys->ORDWR);
	setlabel(w, "inferno "+string sys->pctl(0, nil)+"."+tag);
	sync := chan of int;
	spawn ctlproc(w, c.riowctl, sync);
	w.ctlpid = <-sync;
	return (w, nil);
}

setlabel(w: ref Riowin, s: string)
{
	fd := sys->open(w.dir + "/label", Sys->OWRITE);
	if(fd != nil)
		sys->fprint(fd, "%s", s);
}

ctlproc(w: ref Riowin, wctl: chan of (ref Riowin, int), sync: chan of int)
{
	sync <-= sys->pctl(0, nil);
	buf := array[1024] of byte;
	for(;;){
		n := sys->read(w.ctlfd, buf, len buf);
		if(n <= 0)
			break;
		if(n > 4*12){
			state := 0;
			(nil, toks) := sys->tokenize(string buf[4*12:], " ");
			if(hd toks == "current")
				state |= Current;
			if(hd tl toks == "hidden")
				state |= Hidden;
			wctl <-= (w, state);
		}
	}
#sys->print("riowctl eof\n");
}

delwin(w: ref Riowin)
{
	sys->unmount(nil, w.dir);
	kill(w.ptrpid, "kill");
	kill(w.kbdpid, "kill");
	kill(w.ctlpid, "kill");
}

getwin(w: ref Riowin): int
{
	s := readfile(w.dir + "/winname");
#sys->print("getwin %s\n", s);
	i := display.namedimage(s);
	if(i == nil)
		return -1;
	scr := Screen.allocate(i, display.white, 0);
	if(scr == nil)
		return -1;
	wi := scr.newwindow(i.r.inset(Borderwidth), Draw->Refnone, Draw->Nofill);
	if(wi == nil)
		return -1;
	w.img = wi;
	return 0;
}

kbdproc(w: ref Riowin, keys: chan of int, sync: chan of int)
{
	sys->pctl(Sys->NEWFD, nil);
	cctl := sys->open(w.dir + "/consctl", Sys->OWRITE);
	sys->fprint(cctl, "rawon");
	fd := sys->open(w.dir + "/cons", Sys->OREAD);
	if(fd == nil){
		sync <-= -1;
		return;
	}
	sync <-= sys->pctl(0, nil);
	buf := array[12] of byte;
	while((n := sys->read(fd, buf, len buf)) > 0){
		s := string buf[0:n];
		for(j := 0; j < len s; j++)
			keys <-= int s[j];
	}
#sys->print("eof on kbdproc\n");
}

# 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;
}

ptrproc(w: ref Riowin, ptr: chan of ref Draw->Pointer, resize: chan of ref Riowin, sync: chan of int)
{
	w.ptrfd = sys->open(w.dir + "/mouse", Sys->ORDWR);
	if(w.ptrfd == nil){
		sync <-= -1;
		return;
	}
	sync <-= sys->pctl(0, nil);

	b:= array[Ptrsize] of byte;
	while((n := sys->read(w.ptrfd, b, len b)) > 0){
		if(n > 0 && int b[0] == 'r'){
#sys->print("ptrproc got resize: %s\n", string b[0:n]);
			resize <-= w;
		}else{
			p := bytes2ptr(b);
			if(p != nil)
				ptr <-= p;
		}
	}
#sys->print("eof on ptrproc\n");
}

bytes2ptr(b: array of byte): ref Draw->Pointer
{
	if(len b < Ptrsize || int b[0] != 'm')
		return nil;
	x := int string b[1:13];
	y := int string b[13:25];
	but := int string b[25:37];
	msec := int string b[37:49];
	return ref Draw->Pointer (but, (x, y), msec);
}

readfile(f: string): string
{
	fd := sys->open(f, sys->OREAD);
	if(fd == nil)
		return nil;

	buf := array[8192] of byte;
	n := sys->read(fd, buf, len buf);
	if(n < 0)
		return nil;

	return string buf[0:n];	
}

readscreenr()
{
	fd := sys->open(P9PATH + "/dev/screen", Sys->OREAD);
	if(fd == nil)
		return ;
	buf := array[5*12] of byte;
	n := sys->read(fd, buf, len buf);
	if(n <= len buf)
		return;
	screenr.min.x = int string buf[12:23];
	screenr.min.y = int string buf[24:35];
	screenr.max.x = int string buf[36:47];
	screenr.max.y = int string buf[48:];
}

r2s(r: Rect): string
{
	return string r.min.x + " " + string r.min.y + " " +
			string r.max.x + " " + string r.max.y;
}

kill(pid: int, note: string): int
{
	fd := sys->open("#p/"+string pid+"/ctl", Sys->OWRITE);
	if(fd == nil || sys->fprint(fd, "%s", note) < 0)
		return -1;
	return 0;
}