1 /* Copyright (c) 2006 Russ Cox */
4 #include <sys/select.h>
12 extern Mouse _drawmouse;
13 int chattydrawclient = 0;
15 static int drawgettag(Mux *mux, void *vmsg);
16 static void* drawrecv(Mux *mux);
17 static int drawnbrecv(Mux *mux, void**);
18 static int drawsend(Mux *mux, void *vmsg);
19 static int drawsettag(Mux *mux, void *vmsg, uint tag);
20 static int canreadfd(int);
23 _displayconnect(Display *d)
25 int pid, p[2], fd, nbuf, n;
26 char *wsysid, *addr, *id;
30 fmtinstall('W', drawfcallfmt);
31 fmtinstall('H', encodefmt);
33 wsysid = getenv("wsysid");
35 // Connect to running devdraw service.
36 // wsysid=devdrawname/id
37 id = strchr(wsysid, '/');
39 werrstr("invalid $wsysid");
43 addr = smprint("unix!%s/%s", getns(), wsysid);
46 fd = dial(addr, 0, 0, 0);
50 nbuf = strlen(id) + 500;
56 memset(&w, 0, sizeof w);
59 n = convW2M(&w, buf, nbuf);
60 if(write(fd, buf, n) != n) {
62 werrstr("wsys short write: %r");
65 n = readwsysmsg(fd, buf, nbuf);
68 werrstr("wsys short read: %r");
71 if(convM2W(buf, n, &w) <= 0) {
73 werrstr("wsys decode error");
79 werrstr("%s", w.error);
81 werrstr("wsys rpc phase error (%d)", w.type);
98 devdraw = getenv("DEVDRAW");
104 /* execl("strace", "strace", "-o", "drawsrv.out", "drawsrv", nil); */
106 * The argv0 has no meaning to devdraw.
107 * Pass it along only so that the various
108 * devdraws in psu -a can be distinguished.
109 * The NOLIBTHREADDAEMONIZE keeps devdraw from
110 * forking before threadmain. OS X hates it when
113 * If client didn't use ARGBEGIN, argv0 == nil.
114 * Can't send nil through because OS X expects
115 * argv[0] to be non-nil. Also, OS X apparently
116 * expects argv[0] to be a valid executable name,
117 * so "(argv0)" is not okay. Use "devdraw"
120 putenv("NOLIBTHREADDAEMONIZE", "1");
121 devdraw = getenv("DEVDRAW");
126 execl(devdraw, argv0, argv0, "(devdraw)", nil);
127 sysfatal("exec devdraw: %r");
135 _displaymux(Display *d)
137 if((d->mux = mallocz(sizeof(*d->mux), 1)) == nil)
141 d->mux->maxtag = 255;
142 d->mux->send = drawsend;
143 d->mux->recv = drawrecv;
144 d->mux->nbrecv = drawnbrecv;
145 d->mux->gettag = drawgettag;
146 d->mux->settag = drawsettag;
154 drawsend(Mux *mux, void *vmsg)
163 return write(d->srvfd, msg, n);
167 _drawrecv(Mux *mux, int canblock, void **vp)
175 if(!canblock && !canreadfd(d->srvfd))
177 if((n=readn(d->srvfd, buf, 4)) != 4)
182 fprint(2, "out of memory allocating %d in drawrecv\n", n);
186 if(readn(d->srvfd, p+4, n-4) != n-4){
198 _drawrecv(mux, 1, &p);
203 drawnbrecv(Mux *mux, void **vp)
205 return _drawrecv(mux, 0, vp);
209 drawgettag(Mux *mux, void *vmsg)
219 drawsettag(Mux *mux, void *vmsg, uint tag)
230 displayrpc(Display *d, Wsysmsg *tx, Wsysmsg *rx, void **freep)
243 fprint(2, "<- %W\n", tx);
244 nn = convW2M(tx, tpkt, n);
247 werrstr("drawclient: sizeW2M convW2M mismatch");
252 * This is the only point where we might reschedule.
253 * Muxrpc might need to acquire d->mux->lk, which could
254 * be held by some other proc (e.g., the one reading from
255 * the keyboard via Trdkbd messages). If we need to wait
256 * for the lock, don't let other threads from this proc
257 * run. This keeps up the appearance that writes to /dev/draw
258 * don't cause rescheduling. If you *do* allow rescheduling
259 * here, then flushimage(display, 1) happening in two different
260 * threads in the same proc can cause a buffer of commands
261 * to be written out twice, leading to interesting results
264 * Threadpin and threadunpin were added to the thread library
265 * to solve exactly this problem. Be careful! They are dangerous.
267 * _pin and _unpin are aliases for threadpin and threadunpin
268 * in a threaded program and are no-ops in unthreaded programs.
271 rpkt = muxrpc(d->mux, tpkt);
275 werrstr("muxrpc: %r");
278 GET((uchar*)rpkt, n);
279 nn = convM2W(rpkt, n, rx);
282 werrstr("drawclient: convM2W packet size mismatch %d %d %.*H", n, nn, n, rpkt);
287 fprint(2, "-> %W\n", rx);
288 if(rx->type == Rerror){
289 werrstr("%s", rx->error);
293 if(rx->type != tx->type+1){
294 werrstr("packet type mismatch -- tx %d rx %d",
307 _displayinit(Display *d, char *label, char *winsize)
313 tx.winsize = winsize;
314 return displayrpc(d, &tx, &rx, nil);
318 _displayrdmouse(Display *d, Mouse *m, int *resized)
323 if(displayrpc(d, &tx, &rx, nil) < 0)
325 _drawmouse = rx.mouse;
327 *resized = rx.resized;
332 _displayrdkbd(Display *d, Rune *r)
337 if(displayrpc(d, &tx, &rx, nil) < 0)
344 _displaymoveto(Display *d, Point p)
350 if(displayrpc(d, &tx, &rx, nil) < 0)
353 return flushimage(d, 1);
357 _displaycursor(Display *d, Cursor *c, Cursor2 *c2)
363 memset(&tx.cursor, 0, sizeof tx.cursor);
364 memset(&tx.cursor2, 0, sizeof tx.cursor2);
372 scalecursor(&tx.cursor2, c);
374 return displayrpc(d, &tx, &rx, nil);
378 _displaybouncemouse(Display *d, Mouse *m)
382 tx.type = Tbouncemouse;
384 return displayrpc(d, &tx, &rx, nil);
388 _displaylabel(Display *d, char *label)
394 return displayrpc(d, &tx, &rx, nil);
398 _displayrdsnarf(Display *d)
405 if(displayrpc(d, &tx, &rx, &p) < 0)
407 s = strdup(rx.snarf);
413 _displaywrsnarf(Display *d, char *snarf)
419 return displayrpc(d, &tx, &rx, nil);
423 _displayrddraw(Display *d, void *v, int n)
430 if(displayrpc(d, &tx, &rx, &p) < 0)
432 memmove(v, rx.data, rx.count);
438 _displaywrdraw(Display *d, void *v, int n)
445 if(displayrpc(d, &tx, &rx, nil) < 0)
451 _displaytop(Display *d)
456 return displayrpc(d, &tx, &rx, nil);
460 _displayresize(Display *d, Rectangle r)
466 return displayrpc(d, &tx, &rx, nil);
482 if(select(fd+1, &rs, &ws, &xs, &tv) < 0)
484 if(FD_ISSET(fd, &rs) || FD_ISSET(fd, &xs))