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, *ns, *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 if((ns = getns()) == nil)
45 addr = smprint("unix!%s/%s", ns, wsysid);
49 fd = dial(addr, 0, 0, 0);
53 nbuf = strlen(id) + 500;
59 memset(&w, 0, sizeof w);
62 n = convW2M(&w, buf, nbuf);
63 if(write(fd, buf, n) != n) {
65 werrstr("wsys short write: %r");
68 n = readwsysmsg(fd, buf, nbuf);
71 werrstr("wsys short read: %r");
74 if(convM2W(buf, n, &w) <= 0) {
76 werrstr("wsys decode error");
82 werrstr("%s", w.error);
84 werrstr("wsys rpc phase error (%d)", w.type);
101 devdraw = getenv("DEVDRAW");
107 /* execl("strace", "strace", "-o", "drawsrv.out", "drawsrv", nil); */
109 * The argv0 has no meaning to devdraw.
110 * Pass it along only so that the various
111 * devdraws in psu -a can be distinguished.
112 * The NOLIBTHREADDAEMONIZE keeps devdraw from
113 * forking before threadmain. OS X hates it when
116 * If client didn't use ARGBEGIN, argv0 == nil.
117 * Can't send nil through because OS X expects
118 * argv[0] to be non-nil. Also, OS X apparently
119 * expects argv[0] to be a valid executable name,
120 * so "(argv0)" is not okay. Use "devdraw"
123 putenv("NOLIBTHREADDAEMONIZE", "1");
124 devdraw = getenv("DEVDRAW");
129 execl(devdraw, argv0, argv0, "(devdraw)", nil);
130 sysfatal("exec devdraw: %r");
138 _displaymux(Display *d)
140 if((d->mux = mallocz(sizeof(*d->mux), 1)) == nil)
144 d->mux->maxtag = 255;
145 d->mux->send = drawsend;
146 d->mux->recv = drawrecv;
147 d->mux->nbrecv = drawnbrecv;
148 d->mux->gettag = drawgettag;
149 d->mux->settag = drawsettag;
157 drawsend(Mux *mux, void *vmsg)
166 return write(d->srvfd, msg, n);
170 _drawrecv(Mux *mux, int canblock, void **vp)
178 if(!canblock && !canreadfd(d->srvfd))
180 if((n=readn(d->srvfd, buf, 4)) != 4)
185 fprint(2, "out of memory allocating %d in drawrecv\n", n);
189 if(readn(d->srvfd, p+4, n-4) != n-4){
201 _drawrecv(mux, 1, &p);
206 drawnbrecv(Mux *mux, void **vp)
208 return _drawrecv(mux, 0, vp);
212 drawgettag(Mux *mux, void *vmsg)
222 drawsettag(Mux *mux, void *vmsg, uint tag)
233 displayrpc(Display *d, Wsysmsg *tx, Wsysmsg *rx, void **freep)
246 fprint(2, "<- %W\n", tx);
247 nn = convW2M(tx, tpkt, n);
250 werrstr("drawclient: sizeW2M convW2M mismatch");
255 * This is the only point where we might reschedule.
256 * Muxrpc might need to acquire d->mux->lk, which could
257 * be held by some other proc (e.g., the one reading from
258 * the keyboard via Trdkbd messages). If we need to wait
259 * for the lock, don't let other threads from this proc
260 * run. This keeps up the appearance that writes to /dev/draw
261 * don't cause rescheduling. If you *do* allow rescheduling
262 * here, then flushimage(display, 1) happening in two different
263 * threads in the same proc can cause a buffer of commands
264 * to be written out twice, leading to interesting results
267 * Threadpin and threadunpin were added to the thread library
268 * to solve exactly this problem. Be careful! They are dangerous.
270 * _pin and _unpin are aliases for threadpin and threadunpin
271 * in a threaded program and are no-ops in unthreaded programs.
274 rpkt = muxrpc(d->mux, tpkt);
278 werrstr("muxrpc: %r");
281 GET((uchar*)rpkt, n);
282 nn = convM2W(rpkt, n, rx);
285 werrstr("drawclient: convM2W packet size mismatch %d %d %.*H", n, nn, n, rpkt);
290 fprint(2, "-> %W\n", rx);
291 if(rx->type == Rerror){
292 werrstr("%s", rx->error);
296 if(rx->type != tx->type+1){
297 werrstr("packet type mismatch -- tx %d rx %d",
310 _displayinit(Display *d, char *label, char *winsize)
316 tx.winsize = winsize;
317 return displayrpc(d, &tx, &rx, nil);
321 _displayrdmouse(Display *d, Mouse *m, int *resized)
326 if(displayrpc(d, &tx, &rx, nil) < 0)
328 _drawmouse = rx.mouse;
330 *resized = rx.resized;
335 _displayrdkbd(Display *d, Rune *r)
340 if(displayrpc(d, &tx, &rx, nil) < 0)
347 _displaymoveto(Display *d, Point p)
353 if(displayrpc(d, &tx, &rx, nil) < 0)
356 return flushimage(d, 1);
360 _displaycursor(Display *d, Cursor *c, Cursor2 *c2)
366 memset(&tx.cursor, 0, sizeof tx.cursor);
367 memset(&tx.cursor2, 0, sizeof tx.cursor2);
375 scalecursor(&tx.cursor2, c);
377 return displayrpc(d, &tx, &rx, nil);
381 _displaybouncemouse(Display *d, Mouse *m)
385 tx.type = Tbouncemouse;
387 return displayrpc(d, &tx, &rx, nil);
391 _displaylabel(Display *d, char *label)
397 return displayrpc(d, &tx, &rx, nil);
401 _displayrdsnarf(Display *d)
408 if(displayrpc(d, &tx, &rx, &p) < 0)
410 s = strdup(rx.snarf);
416 _displaywrsnarf(Display *d, char *snarf)
422 return displayrpc(d, &tx, &rx, nil);
426 _displayrddraw(Display *d, void *v, int n)
433 if(displayrpc(d, &tx, &rx, &p) < 0)
435 memmove(v, rx.data, rx.count);
441 _displaywrdraw(Display *d, void *v, int n)
448 if(displayrpc(d, &tx, &rx, nil) < 0)
454 _displaytop(Display *d)
459 return displayrpc(d, &tx, &rx, nil);
463 _displayresize(Display *d, Rectangle r)
469 return displayrpc(d, &tx, &rx, nil);
485 if(select(fd+1, &rs, &ws, &xs, &tv) < 0)
487 if(FD_ISSET(fd, &rs) || FD_ISSET(fd, &xs))