1 /* Copyright (c) 2006 Russ Cox */
4 #include <sys/select.h>
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.
46 execl("devdraw", "devdraw", argv0, nil);
47 sysfatal("exec devdraw: %r");
55 _displaymux(Display *d)
57 if((d->mux = mallocz(sizeof(*d->mux), 1)) == nil)
62 d->mux->send = drawsend;
63 d->mux->recv = drawrecv;
64 d->mux->nbrecv = drawnbrecv;
65 d->mux->gettag = drawgettag;
66 d->mux->settag = drawsettag;
74 drawsend(Mux *mux, void *vmsg)
83 return write(d->srvfd, msg, n);
87 _drawrecv(Mux *mux, int canblock, void **vp)
95 if(!canblock && !canreadfd(d->srvfd))
97 if((n=readn(d->srvfd, buf, 4)) != 4)
102 fprint(2, "out of memory allocating %d in drawrecv\n", n);
106 if(readn(d->srvfd, p+4, n-4) != n-4){
118 _drawrecv(mux, 1, &p);
123 drawnbrecv(Mux *mux, void **vp)
125 return _drawrecv(mux, 0, vp);
129 drawgettag(Mux *mux, void *vmsg)
139 drawsettag(Mux *mux, void *vmsg, uint tag)
150 displayrpc(Display *d, Wsysmsg *tx, Wsysmsg *rx, void **freep)
163 fprint(2, "<- %W\n", tx);
164 nn = convW2M(tx, tpkt, n);
167 werrstr("drawclient: sizeW2M convW2M mismatch");
172 * This is the only point where we might reschedule.
173 * Muxrpc might need to acquire d->mux->lk, which could
174 * be held by some other proc (e.g., the one reading from
175 * the keyboard via Trdkbd messages). If we need to wait
176 * for the lock, don't let other threads from this proc
177 * run. This keeps up the appearance that writes to /dev/draw
178 * don't cause rescheduling. If you *do* allow rescheduling
179 * here, then flushimage(display, 1) happening in two different
180 * threads in the same proc can cause a buffer of commands
181 * to be written out twice, leading to interesting results
184 * Threadpin and threadunpin were added to the thread library
185 * to solve exactly this problem. Be careful! They are dangerous.
187 * _pin and _unpin are aliases for threadpin and threadunpin
188 * in a threaded program and are no-ops in unthreaded programs.
191 rpkt = muxrpc(d->mux, tpkt);
195 werrstr("muxrpc: %r");
198 GET((uchar*)rpkt, n);
199 nn = convM2W(rpkt, n, rx);
202 werrstr("drawclient: convM2W packet size mismatch %d %d %.*H", n, nn, n, rpkt);
207 fprint(2, "-> %W\n", rx);
208 if(rx->type == Rerror){
209 werrstr("%s", rx->error);
213 if(rx->type != tx->type+1){
214 werrstr("packet type mismatch -- tx %d rx %d",
227 _displayinit(Display *d, char *label, char *winsize)
233 tx.winsize = winsize;
234 return displayrpc(d, &tx, &rx, nil);
238 _displayrdmouse(Display *d, Mouse *m, int *resized)
243 if(displayrpc(d, &tx, &rx, nil) < 0)
246 *resized = rx.resized;
251 _displayrdkbd(Display *d, Rune *r)
256 if(displayrpc(d, &tx, &rx, nil) < 0)
263 _displaymoveto(Display *d, Point p)
269 return displayrpc(d, &tx, &rx, nil);
273 _displaycursor(Display *d, Cursor *c)
279 memset(&tx.cursor, 0, sizeof tx.cursor);
285 return displayrpc(d, &tx, &rx, nil);
289 _displaybouncemouse(Display *d, Mouse *m)
293 tx.type = Tbouncemouse;
295 return displayrpc(d, &tx, &rx, nil);
299 _displaylabel(Display *d, char *label)
305 return displayrpc(d, &tx, &rx, nil);
309 _displayrdsnarf(Display *d)
316 if(displayrpc(d, &tx, &rx, &p) < 0)
318 s = strdup(rx.snarf);
324 _displaywrsnarf(Display *d, char *snarf)
330 return displayrpc(d, &tx, &rx, nil);
334 _displayrddraw(Display *d, void *v, int n)
341 if(displayrpc(d, &tx, &rx, &p) < 0)
343 memmove(v, rx.data, rx.count);
349 _displaywrdraw(Display *d, void *v, int n)
356 if(displayrpc(d, &tx, &rx, nil) < 0)
362 _displaytop(Display *d)
367 return displayrpc(d, &tx, &rx, nil);
371 _displayresize(Display *d, Rectangle r)
377 return displayrpc(d, &tx, &rx, nil);
393 if(select(fd+1, &rs, &ws, &xs, &tv) < 0)
395 if(FD_ISSET(fd, &rs) || FD_ISSET(fd, &xs))