1 /* Copyright (c) 2006 Russ Cox */
4 #include <sys/select.h>
12 int chattydrawclient = 0;
14 static int drawgettag(Mux *mux, void *vmsg);
15 static void* drawrecv(Mux *mux);
16 static int drawnbrecv(Mux *mux, void**);
17 static int drawsend(Mux *mux, void *vmsg);
18 static int drawsettag(Mux *mux, void *vmsg, uint tag);
19 static int canreadfd(int);
22 _displayconnect(Display *d)
26 fmtinstall('W', drawfcallfmt);
27 fmtinstall('H', encodefmt);
40 /* execl("strace", "strace", "-o", "drawsrv.out", "drawsrv", nil); */
42 * The argv0 has no meaning to devdraw.
43 * Pass it along only so that the various
44 * devdraws in psu -a can be distinguished.
45 * The NOLIBTHREADDAEMONIZE keeps devdraw from
46 * forking before threadmain. OS X hates it when
49 putenv("NOLIBTHREADDAEMONIZE", "1");
50 execl("devdraw", argv0, argv0, "(devdraw)", nil);
51 sysfatal("exec devdraw: %r");
59 _displaymux(Display *d)
61 if((d->mux = mallocz(sizeof(*d->mux), 1)) == nil)
66 d->mux->send = drawsend;
67 d->mux->recv = drawrecv;
68 d->mux->nbrecv = drawnbrecv;
69 d->mux->gettag = drawgettag;
70 d->mux->settag = drawsettag;
78 drawsend(Mux *mux, void *vmsg)
87 return write(d->srvfd, msg, n);
91 _drawrecv(Mux *mux, int canblock, void **vp)
99 if(!canblock && !canreadfd(d->srvfd))
101 if((n=readn(d->srvfd, buf, 4)) != 4)
106 fprint(2, "out of memory allocating %d in drawrecv\n", n);
110 if(readn(d->srvfd, p+4, n-4) != n-4){
122 _drawrecv(mux, 1, &p);
127 drawnbrecv(Mux *mux, void **vp)
129 return _drawrecv(mux, 0, vp);
133 drawgettag(Mux *mux, void *vmsg)
143 drawsettag(Mux *mux, void *vmsg, uint tag)
154 displayrpc(Display *d, Wsysmsg *tx, Wsysmsg *rx, void **freep)
167 fprint(2, "<- %W\n", tx);
168 nn = convW2M(tx, tpkt, n);
171 werrstr("drawclient: sizeW2M convW2M mismatch");
176 * This is the only point where we might reschedule.
177 * Muxrpc might need to acquire d->mux->lk, which could
178 * be held by some other proc (e.g., the one reading from
179 * the keyboard via Trdkbd messages). If we need to wait
180 * for the lock, don't let other threads from this proc
181 * run. This keeps up the appearance that writes to /dev/draw
182 * don't cause rescheduling. If you *do* allow rescheduling
183 * here, then flushimage(display, 1) happening in two different
184 * threads in the same proc can cause a buffer of commands
185 * to be written out twice, leading to interesting results
188 * Threadpin and threadunpin were added to the thread library
189 * to solve exactly this problem. Be careful! They are dangerous.
191 * _pin and _unpin are aliases for threadpin and threadunpin
192 * in a threaded program and are no-ops in unthreaded programs.
195 rpkt = muxrpc(d->mux, tpkt);
199 werrstr("muxrpc: %r");
202 GET((uchar*)rpkt, n);
203 nn = convM2W(rpkt, n, rx);
206 werrstr("drawclient: convM2W packet size mismatch %d %d %.*H", n, nn, n, rpkt);
211 fprint(2, "-> %W\n", rx);
212 if(rx->type == Rerror){
213 werrstr("%s", rx->error);
217 if(rx->type != tx->type+1){
218 werrstr("packet type mismatch -- tx %d rx %d",
231 _displayinit(Display *d, char *label, char *winsize)
237 tx.winsize = winsize;
238 return displayrpc(d, &tx, &rx, nil);
242 _displayrdmouse(Display *d, Mouse *m, int *resized)
247 if(displayrpc(d, &tx, &rx, nil) < 0)
250 *resized = rx.resized;
255 _displayrdkbd(Display *d, Rune *r)
260 if(displayrpc(d, &tx, &rx, nil) < 0)
267 _displaymoveto(Display *d, Point p)
273 return displayrpc(d, &tx, &rx, nil);
277 _displaycursor(Display *d, Cursor *c)
283 memset(&tx.cursor, 0, sizeof tx.cursor);
289 return displayrpc(d, &tx, &rx, nil);
293 _displaybouncemouse(Display *d, Mouse *m)
297 tx.type = Tbouncemouse;
299 return displayrpc(d, &tx, &rx, nil);
303 _displaylabel(Display *d, char *label)
309 return displayrpc(d, &tx, &rx, nil);
313 _displayrdsnarf(Display *d)
320 if(displayrpc(d, &tx, &rx, &p) < 0)
322 s = strdup(rx.snarf);
328 _displaywrsnarf(Display *d, char *snarf)
334 return displayrpc(d, &tx, &rx, nil);
338 _displayrddraw(Display *d, void *v, int n)
345 if(displayrpc(d, &tx, &rx, &p) < 0)
347 memmove(v, rx.data, rx.count);
353 _displaywrdraw(Display *d, void *v, int n)
360 if(displayrpc(d, &tx, &rx, nil) < 0)
366 _displaytop(Display *d)
371 return displayrpc(d, &tx, &rx, nil);
375 _displayresize(Display *d, Rectangle r)
381 return displayrpc(d, &tx, &rx, nil);
397 if(select(fd+1, &rs, &ws, &xs, &tv) < 0)
399 if(FD_ISSET(fd, &rs) || FD_ISSET(fd, &xs))