27 * 4 bits of file type (qids above)
28 * 24 bits of mux slot number +1; 0 means not attached to client
30 #define QSHIFT 4 /* location in qid of client # */
32 #define QID(q) ((((ulong)(q).path)&0x0000000F)>>0)
33 #define CLIENTPATH(q) ((((ulong)q)&0x7FFFFFF0)>>QSHIFT)
34 #define CLIENT(q) CLIENTPATH((q).path)
37 #define HASHMASK (NHASH-1)
39 typedef struct Client Client;
40 typedef struct Draw Draw;
41 typedef struct DImage DImage;
42 typedef struct DScreen DScreen;
43 typedef struct CScreen CScreen;
44 typedef struct FChar FChar;
45 typedef struct Refresh Refresh;
46 typedef struct Refx Refx;
47 typedef struct DName DName;
49 ulong blanktime = 30; /* in minutes; a half hour */
61 int blanked; /* screen turned off */
62 ulong blanktime; /* time of last operation */
69 DImage* dimage[NHASH];
106 int minx; /* left edge of bits */
107 int maxx; /* right edge of bits */
108 uchar miny; /* first non-zero scan-line */
109 uchar maxy; /* last non-zero scan-line + 1 */
110 schar left; /* offset of baseline */
111 uchar width; /* width of baseline */
115 * Reference counts in DImages:
116 * one per open by original client
117 * one per screen image or fill
118 * one per image derived from this one by name
130 DScreen* dscreen; /* 0 if not a window */
131 DImage* fromname; /* image this one is derived from, by name */
154 static Memimage *screenimage;
155 static Memdata screendata;
156 static Rectangle flushrect;
158 static DScreen* dscreen;
159 extern void flushmemscreen(Rectangle);
160 void drawmesg(Client*, void*, int);
161 void drawuninstall(Client*, int);
162 void drawfreedimage(DImage*);
163 Client* drawclientofpath(ulong);
165 static char Enodrawimage[] = "unknown id for draw image";
166 static char Enodrawscreen[] = "unknown id for draw screen";
167 static char Eshortdraw[] = "short draw message";
168 static char Eshortread[] = "draw read too short";
169 static char Eimageexists[] = "image id in use";
170 static char Escreenexists[] = "screen id in use";
171 static char Edrawmem[] = "image memory allocation failed";
172 static char Ereadoutside[] = "readimage outside image";
173 static char Ewriteoutside[] = "writeimage outside image";
174 static char Enotfont[] = "image not a font";
175 static char Eindex[] = "character index out of range";
176 static char Enoclient[] = "no such draw client";
177 static char Edepth[] = "image has bad depth";
178 static char Enameused[] = "image name in use";
179 static char Enoname[] = "no image with that name";
180 static char Eoldname[] = "named image no longer valid";
181 static char Enamed[] = "image already has name";
182 static char Ewrongname[] = "wrong name for image";
199 return canqlock(&sdraw);
203 drawgen(Chan *c, Dirtab*, int, int s, Dir *dp)
216 mkqid(&q, Qtopdir, 0, QTDIR);
217 devdir(c, q, "#i", 0, eve, 0500, dp);
220 cl = drawclientofpath(c->qid.path);
222 strcpy(up->genbuf, "??");
224 sprint(up->genbuf, "%d", cl->clientid);
225 mkqid(&q, Q2nd, 0, QTDIR);
226 devdir(c, q, up->genbuf, 0, eve, 0500, dp);
229 panic("drawwalk %#llux", c->qid.path);
235 * Top level directory contains the name of the device.
241 mkqid(&q, Q2nd, 0, QTDIR);
242 devdir(c, q, "draw", 0, eve, 0555, dp);
251 * Second level contains "new" plus all the clients.
253 if(t == Q2nd || t == Qnew){
255 mkqid(&q, Qnew, 0, QTFILE);
256 devdir(c, q, "new", 0, eve, 0666, dp);
258 else if(s <= sdraw.nclient){
259 cl = sdraw.client[s-1];
262 sprint(up->genbuf, "%d", cl->clientid);
263 mkqid(&q, (s<<QSHIFT)|Q3rd, 0, QTDIR);
264 devdir(c, q, up->genbuf, 0, eve, 0555, dp);
275 path = c->qid.path&~(((1<<QSHIFT)-1)); /* slot component */
276 q.vers = c->qid.vers;
280 q.path = path|Qcolormap;
281 devdir(c, q, "colormap", 0, eve, 0600, dp);
285 devdir(c, q, "ctl", 0, eve, 0600, dp);
289 devdir(c, q, "data", 0, eve, 0600, dp);
292 q.path = path|Qrefresh;
293 devdir(c, q, "refresh", 0, eve, 0400, dp);
303 drawrefactive(void *a)
308 return c->refreshme || c->refresh!=0;
313 drawrefreshscreen(DImage *l, Client *client)
315 while(l != nil && l->dscreen == nil)
317 if(l != nil && l->dscreen->owner != client)
318 l->dscreen->owner->refreshme = 1;
323 drawrefresh(Memimage *l, Rectangle r, void *v)
336 for(ref=c->refresh; ref; ref=ref->next)
337 if(ref->dimage == d){
338 combinerect(&ref->r, r);
341 ref = malloc(sizeof(Refresh));
345 ref->next = c->refresh;
351 addflush(Rectangle r)
356 if(sdraw.softscreen==0 || !rectclip(&r, screenimage->r))
359 if(flushrect.min.x >= flushrect.max.x){
364 /* VNC uses a region to compute the minimum bounding area.
365 * The waste is far less than that of a bounding box. see region.c
368 flushmemscreen(flushrect);
373 combinerect(&nbb, r);
375 abb = Dx(flushrect)*Dy(flushrect);
376 anbb = Dx(nbb)*Dy(nbb);
378 * Area of new waste is area of new bb minus area of old bb,
379 * less the area of the new segment, which we assume is not waste.
380 * This could be negative, but that's OK.
382 waste += anbb-abb - ar;
387 * total area is small
388 * waste is less than half total area
391 if(anbb<=1024 || waste*2<anbb || rectXrect(flushrect, r)){
395 /* emit current state */
396 flushmemscreen(flushrect);
403 dstflush(int dstid, Memimage *dst, Rectangle r)
408 //combinerect(&flushrect, r);
409 addflush(r); // for VNC, see comments in addflush
416 if(l->screen->image->data != screenimage->data)
418 r = rectaddpt(r, l->delta);
419 l = l->screen->image->layer;
428 flushmemscreen(flushrect);
429 flushrect = Rect(10000, 10000, -10000, -10000);
434 drawcmp(char *a, char *b, int n)
438 return memcmp(a, b, n);
442 drawlookupname(int n, char *str)
447 ename = &name[sdraw.nname];
448 for(; name<ename; name++)
449 if(drawcmp(name->name, str, n) == 0)
455 drawgoodname(DImage *d)
459 /* if window, validate the screen's own images */
461 if(drawgoodname(d->dscreen->dimage) == 0
462 || drawgoodname(d->dscreen->dfill) == 0)
466 n = drawlookupname(strlen(d->name), d->name);
467 if(n==nil || n->vers!=d->vers)
473 drawlookup(Client *client, int id, int checkname)
477 d = client->dimage[id&HASHMASK];
480 if(checkname && !drawgoodname(d))
490 drawlookupdscreen(int id)
504 drawlookupscreen(Client *client, int id, CScreen **cs)
510 if(s->dscreen->id == id){
516 error(Enodrawscreen);
521 drawinstall(Client *client, int id, Memimage *i, DScreen *dscreen)
525 d = malloc(sizeof(DImage));
536 d->dscreen = dscreen;
537 d->next = client->dimage[id&HASHMASK];
538 client->dimage[id&HASHMASK] = d;
543 drawinstallscreen(Client *client, DScreen *d, int id, DImage *dimage, DImage *dfill, int public)
548 c = malloc(sizeof(CScreen));
549 if(dimage && dimage->image && dimage->image->chan == 0)
550 panic("bad image %p in drawinstallscreen", dimage->image);
555 d = malloc(sizeof(DScreen));
560 s = malloc(sizeof(Memscreen));
570 s->image = dimage->image;
575 s->fill = dfill->image;
588 c->next = client->cscreen;
594 drawdelname(DName *name)
599 memmove(name, name+1, (sdraw.nname-(i+1))*sizeof(DName));
604 drawfreedscreen(DScreen *this)
610 print("negative ref in drawfreedscreen\n");
615 dscreen = this->next;
618 while(next = ds->next){ /* assign = */
620 ds->next = this->next;
629 drawfreedimage(this->dimage);
631 drawfreedimage(this->dfill);
637 drawfreedimage(DImage *dimage)
645 print("negative ref in drawfreedimage\n");
650 for(i=0; i<sdraw.nname; )
651 if(sdraw.name[i].dimage == dimage)
652 drawdelname(sdraw.name+i);
655 if(dimage->fromname){ /* acquired by name; owned by someone else*/
656 drawfreedimage(dimage->fromname);
659 if(dimage->image == screenimage) /* don't free the display */
661 ds = dimage->dscreen;
664 if(l->data == screenimage->data)
665 addflush(l->layer->screenr);
666 if(l->layer->refreshfn == drawrefresh) /* else true owner will clean up */
667 free(l->layer->refreshptr);
668 l->layer->refreshptr = nil;
669 if(drawgoodname(dimage))
675 freememimage(dimage->image);
682 drawuninstallscreen(Client *client, CScreen *this)
686 cs = client->cscreen;
688 client->cscreen = this->next;
689 drawfreedscreen(this->dscreen);
693 while(next = cs->next){ /* assign = */
695 cs->next = this->next;
696 drawfreedscreen(this->dscreen);
705 drawuninstall(Client *client, int id)
709 d = client->dimage[id&HASHMASK];
713 client->dimage[id&HASHMASK] = d->next;
717 while(next = d->next){ /* assign = */
719 d->next = next->next;
720 drawfreedimage(next);
729 drawaddname(Client *client, DImage *di, int n, char *str)
731 DName *name, *ename, *new, *t;
734 ename = &name[sdraw.nname];
735 for(; name<ename; name++)
736 if(drawcmp(name->name, str, n) == 0)
738 t = smalloc((sdraw.nname+1)*sizeof(DName));
739 memmove(t, sdraw.name, sdraw.nname*sizeof(DName));
742 new = &sdraw.name[sdraw.nname++];
743 new->name = smalloc(n+1);
744 memmove(new->name, str, n);
747 new->client = client;
748 new->vers = ++sdraw.vers;
757 for(i=0; i<sdraw.nclient; i++){
758 cl = sdraw.client[i];
762 if(i == sdraw.nclient){
763 cp = malloc((sdraw.nclient+1)*sizeof(Client*));
766 memmove(cp, sdraw.client, sdraw.nclient*sizeof(Client*));
772 cl = malloc(sizeof(Client));
775 memset(cl, 0, sizeof(Client));
777 cl->clientid = ++sdraw.clientid;
779 sdraw.client[i] = cl;
784 drawclientop(Client *cl)
797 * if draw has ever been used, we can't resize the frame buffer,
798 * even if all clients have exited (nclients is cumulative); it's too
801 return sdraw.nclient != 0;
805 drawclientofpath(ulong path)
810 slot = CLIENTPATH(path);
813 cl = sdraw.client[slot-1];
814 if(cl==0 || cl->clientid==0)
825 client = drawclientofpath(c->qid.path);
832 drawimage(Client *client, uchar *a)
836 d = drawlookup(client, BGLONG(a), 1);
843 drawrectangle(Rectangle *r, uchar *a)
845 r->min.x = BGLONG(a+0*4);
846 r->min.y = BGLONG(a+1*4);
847 r->max.x = BGLONG(a+2*4);
848 r->max.y = BGLONG(a+3*4);
852 drawpoint(Point *p, uchar *a)
854 p->x = BGLONG(a+0*4);
855 p->y = BGLONG(a+1*4);
859 drawchar(Memimage *dst, Point p, Memimage *src, Point *sp, DImage *font, int index, int op)
865 fc = &font->fchar[index];
866 r.min.x = p.x+fc->left;
867 r.min.y = p.y-(font->ascent-fc->miny);
868 r.max.x = r.min.x+(fc->maxx-fc->minx);
869 r.max.y = r.min.y+(fc->maxy-fc->miny);
870 sp1.x = sp->x+fc->left;
871 sp1.y = sp->y+fc->miny;
872 memdraw(dst, r, src, sp1, font->image, Pt(fc->minx, fc->miny), op);
879 initscreenimage(void)
885 if(screenimage != nil)
888 screendata.base = nil;
889 screendata.bdata = attachscreen(&r, &chan, &depth, &width, &sdraw.softscreen);
890 if(screendata.bdata == nil)
891 {fprint(2, "bad bdata\n");
896 screenimage = allocmemimaged(r, chan, &screendata);
897 if(screenimage == nil){
898 fprint(2, "bad memimaged: %r\n");
899 /* RSC: BUG: detach screen */
903 screenimage->width = width;
904 screenimage->clipr = screenimage->r;
909 deletescreenimage(void)
912 /* RSC: BUG: detach screen */
914 freememimage(screenimage);
920 drawattach(char *spec)
923 if(!initscreenimage()){
925 error("no frame buffer");
928 return devattach('i', spec);
932 drawwalk(Chan *c, Chan *nc, char **name, int nname)
934 if(screendata.bdata == nil)
935 error("no frame buffer");
936 return devwalk(c, nc, name, nname, 0, 0, drawgen);
940 drawstat(Chan *c, uchar *db, int n)
942 return devstat(c, db, n, 0, 0, drawgen);
946 drawopen(Chan *c, int omode)
950 if(c->qid.type & QTDIR)
951 return devopen(c, omode, 0, 0, drawgen);
959 if(QID(c->qid) == Qnew){
960 cl = drawnewclient();
963 c->qid.path = Qctl|((cl->slot+1)<<QSHIFT);
975 flushrect = Rect(10000, 10000, -10000, -10000);
976 drawinstall(cl, 0, screenimage, 0);
988 c->mode = openmode(omode);
1002 if(c->qid.type & QTDIR)
1011 if(QID(c->qid) == Qctl)
1013 if((c->flag&COPEN) && (decref(&cl->r)==0)){
1014 while(r = cl->refresh){ /* assign = */
1015 cl->refresh = r->next;
1019 for(i=0; i<sdraw.nname; )
1020 if(sdraw.name[i].client == cl)
1021 drawdelname(sdraw.name+i);
1025 drawuninstallscreen(cl, cl->cscreen);
1026 /* all screens are freed, so now we can free images */
1028 for(i=0; i<NHASH; i++){
1029 while((d = *dp) != nil){
1035 sdraw.client[cl->slot] = 0;
1036 drawflush(); /* to erase visible, now dead windows */
1044 drawread(Chan *c, void *a, long n, vlong off)
1047 ulong red, green, blue;
1057 if(c->qid.type & QTDIR)
1058 return devdirread(c, a, n, 0, 0, drawgen);
1065 switch(QID(c->qid)){
1070 error(Enodrawimage);
1071 if(cl->infoid == 0){
1074 error(Enodrawimage);
1076 di = drawlookup(cl, cl->infoid, 1);
1078 error(Enodrawimage);
1081 n = sprint(a, "%11d %11d %11s %11d %11d %11d %11d %11d %11d %11d %11d %11d ",
1082 cl->clientid, cl->infoid, chantostr(buf, i->chan), (i->flags&Frepl)==Frepl,
1083 i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y,
1084 i->clipr.min.x, i->clipr.min.y, i->clipr.max.x, i->clipr.max.y);
1089 drawactive(1); /* to restore map from backup */
1090 p = malloc(4*12*256+1);
1094 for(index = 0; index < 256; index++){
1095 getcolor(index, &red, &green, &blue);
1096 m += sprint((char*)p+m, "%11d %11lud %11lud %11lud\n", index, red>>24, green>>24, blue>>24);
1098 n = readstr(offset, a, n, (char*)p);
1103 if(cl->readdata == nil)
1104 error("no draw data");
1105 if(n < cl->nreaddata)
1108 memmove(a, cl->readdata, cl->nreaddata);
1117 if(cl->refreshme || cl->refresh)
1121 qlock(&sdraw); /* restore lock for waserror() above */
1124 rendsleep(&cl->refrend, drawrefactive, cl);
1129 while(cl->refresh && n>=5*4){
1131 BPLONG(p+0*4, r->dimage->id);
1132 BPLONG(p+1*4, r->r.min.x);
1133 BPLONG(p+2*4, r->r.min.y);
1134 BPLONG(p+3*4, r->r.max.x);
1135 BPLONG(p+4*4, r->r.max.y);
1136 cl->refresh = r->next;
1155 for(i=0; i<sdraw.nclient; i++){
1156 cl = sdraw.client[i];
1157 if(cl && (cl->refreshme || cl->refresh))
1158 rendwakeup(&cl->refrend);
1163 drawwrite(Chan *c, void *a, long n, vlong off)
1165 char buf[128], *fields[4], *q;
1167 int i, m, red, green, blue, x;
1171 if(c->qid.type & QTDIR)
1180 switch(QID(c->qid)){
1183 error("unknown draw control request");
1184 cl->infoid = BGLONG((uchar*)a);
1188 drawactive(1); /* to restore map from backup */
1193 if(x > sizeof(buf)-1)
1195 q = memccpy(buf, a, '\n', x);
1203 if(getfields(buf, fields, nelem(fields), 1, " ") != 4)
1205 i = strtoul(fields[0], 0, 0);
1206 red = strtoul(fields[1], 0, 0);
1207 green = strtoul(fields[2], 0, 0);
1208 blue = strtoul(fields[3], &q, 0);
1211 if(red>255 || green>255 || blue>255 || i<0 || i>255)
1219 setcolor(i, red, green, blue);
1237 drawcoord(uchar *p, uchar *maxp, int oldx, int *newx)
1262 printmesg(char *fmt, uchar *a, int plsprnt)
1270 USED(fmt, a, buf, p, q, s);
1275 for(p=fmt; *p; p++){
1278 q += sprint(q, " %ld", (long)BGLONG(a));
1282 q += sprint(q, " %.8lux", (ulong)BGLONG(a));
1286 q += sprint(q, " [%d %d %d %d]", BGLONG(a), BGLONG(a+4), BGLONG(a+8), BGLONG(a+12));
1290 q += sprint(q, " [%d %d]", BGLONG(a), BGLONG(a+4));
1294 q += sprint(q, " %d", *a++);
1297 q += sprint(q, " %d", BGSHORT(a));
1301 q += sprint(q, " %.4ux", BGSHORT(a));
1308 iprint("%.*s", (int)(q-buf), buf);
1312 drawmesg(Client *client, void *av, int n)
1314 int c, repl, m, y, dstid, scrnid, ni, ci, j, nw, e0, e1, op, ox, oy, oesize, esize, doflush;
1315 uchar *u, *a, refresh;
1319 Point p, q, *pp, sp;
1320 Memimage *i, *dst, *src, *mask;
1323 DImage *font, *ll, *di, *ddst, *dsrc;
1335 if(fmt) printmesg(fmt, a, 1);
1336 /* iprint("error: %s\n", up->error); */
1344 error("bad draw command");
1345 /* new allocate: 'b' id[4] screenid[4] refresh[1] chan[4] repl[1] R[4*4] clipR[4*4] rrggbbaa[4] */
1347 printmesg(fmt="LLbLbRRL", a, 0);
1348 m = 1+4+4+1+4+1+4*4+4*4+4;
1351 dstid = BGLONG(a+1);
1352 scrnid = BGSHORT(a+5);
1354 chan = BGLONG(a+10);
1356 drawrectangle(&r, a+15);
1357 drawrectangle(&clipr, a+31);
1358 value = BGLONG(a+47);
1359 if(drawlookup(client, dstid, 0))
1360 error(Eimageexists);
1362 dscrn = drawlookupscreen(client, scrnid, &cs);
1363 scrn = dscrn->screen;
1364 if(repl || chan!=scrn->image->chan)
1365 error("image parameters incompatible with screen");
1371 reffn = memlnorefresh;
1374 reffn = drawrefresh;
1377 error("unknown refresh method");
1379 l = memlalloc(scrn, r, reffn, 0, value);
1382 addflush(l->layer->screenr);
1384 rectclip(&l->clipr, r);
1385 if(drawinstall(client, dstid, l, dscrn) == 0){
1392 if(reffn == drawrefresh){
1393 refx = malloc(sizeof(Refx));
1395 drawuninstall(client, dstid);
1398 refx->client = client;
1399 refx->dimage = drawlookup(client, dstid, 1);
1401 memlsetrefresh(l, reffn, refx);
1405 i = allocmemimage(r, chan);
1412 rectclip(&i->clipr, r);
1413 if(drawinstall(client, dstid, i, 0) == 0){
1417 memfillcolor(i, value);
1420 /* allocate screen: 'A' id[4] imageid[4] fillid[4] public[1] */
1422 printmesg(fmt="LLLb", a, 1);
1426 dstid = BGLONG(a+1);
1429 if(drawlookupdscreen(dstid))
1430 error(Escreenexists);
1431 ddst = drawlookup(client, BGLONG(a+5), 1);
1432 dsrc = drawlookup(client, BGLONG(a+9), 1);
1433 if(ddst==0 || dsrc==0)
1434 error(Enodrawimage);
1435 if(drawinstallscreen(client, 0, dstid, ddst, dsrc, a[13]) == 0)
1439 /* set repl and clip: 'c' dstid[4] repl[1] clipR[4*4] */
1441 printmesg(fmt="LbR", a, 0);
1445 ddst = drawlookup(client, BGLONG(a+1), 1);
1447 error(Enodrawimage);
1449 error("can't change repl/clipr of shared image");
1452 dst->flags |= Frepl;
1453 drawrectangle(&dst->clipr, a+6);
1456 /* draw: 'd' dstid[4] srcid[4] maskid[4] R[4*4] P[2*4] P[2*4] */
1458 printmesg(fmt="LLLRPP", a, 0);
1459 m = 1+4+4+4+4*4+2*4+2*4;
1462 dst = drawimage(client, a+1);
1463 dstid = BGLONG(a+1);
1464 src = drawimage(client, a+5);
1465 mask = drawimage(client, a+9);
1466 drawrectangle(&r, a+13);
1467 drawpoint(&p, a+29);
1468 drawpoint(&q, a+37);
1469 op = drawclientop(client);
1470 memdraw(dst, r, src, p, mask, q, op);
1471 dstflush(dstid, dst, r);
1474 /* toggle debugging: 'D' val[1] */
1476 printmesg(fmt="b", a, 0);
1483 /* ellipse: 'e' dstid[4] srcid[4] center[2*4] a[4] b[4] thick[4] sp[2*4] alpha[4] phi[4]*/
1486 printmesg(fmt="LLPlllPll", a, 0);
1487 m = 1+4+4+2*4+4+4+4+2*4+2*4;
1490 dst = drawimage(client, a+1);
1491 dstid = BGLONG(a+1);
1492 src = drawimage(client, a+5);
1497 error("invalid ellipse semidiameter");
1500 error("negative ellipse thickness");
1501 drawpoint(&sp, a+29);
1507 op = drawclientop(client);
1508 /* high bit indicates arc angles are present */
1510 if((ox & (1<<30)) == 0)
1512 memarc(dst, p, e0, e1, c, src, sp, ox, oy, op);
1514 memellipse(dst, p, e0, e1, c, src, sp, op);
1515 dstflush(dstid, dst, Rect(p.x-e0-j, p.y-e1-j, p.x+e0+j+1, p.y+e1+j+1));
1518 /* free: 'f' id[4] */
1520 printmesg(fmt="L", a, 1);
1524 ll = drawlookup(client, BGLONG(a+1), 0);
1525 if(ll && ll->dscreen && ll->dscreen->owner != client)
1526 ll->dscreen->owner->refreshme = 1;
1527 drawuninstall(client, BGLONG(a+1));
1530 /* free screen: 'F' id[4] */
1532 printmesg(fmt="L", a, 1);
1536 drawlookupscreen(client, BGLONG(a+1), &cs);
1537 drawuninstallscreen(client, cs);
1540 /* initialize font: 'i' fontid[4] nchars[4] ascent[1] */
1542 printmesg(fmt="Llb", a, 1);
1546 dstid = BGLONG(a+1);
1548 error("can't use display as font");
1549 font = drawlookup(client, dstid, 1);
1551 error(Enodrawimage);
1552 if(font->image->layer)
1553 error("can't use window as font");
1554 free(font->fchar); /* should we complain if non-zero? */
1556 font->fchar = malloc(ni*sizeof(FChar));
1557 if(font->fchar == 0)
1558 error("no memory for font");
1559 memset(font->fchar, 0, ni*sizeof(FChar));
1561 font->ascent = a[9];
1564 /* load character: 'l' fontid[4] srcid[4] index[2] R[4*4] P[2*4] left[1] width[1] */
1566 printmesg(fmt="LLSRPbb", a, 0);
1567 m = 1+4+4+2+4*4+2*4+1+1;
1570 font = drawlookup(client, BGLONG(a+1), 1);
1572 error(Enodrawimage);
1573 if(font->nfchar == 0)
1575 src = drawimage(client, a+5);
1577 if(ci >= font->nfchar)
1579 drawrectangle(&r, a+11);
1580 drawpoint(&p, a+27);
1581 memdraw(font->image, r, src, p, memopaque, p, S);
1582 fc = &font->fchar[ci];
1591 /* draw line: 'L' dstid[4] p0[2*4] p1[2*4] end0[4] end1[4] radius[4] srcid[4] sp[2*4] */
1593 printmesg(fmt="LPPlllLP", a, 0);
1594 m = 1+4+2*4+2*4+4+4+4+4+2*4;
1597 dst = drawimage(client, a+1);
1598 dstid = BGLONG(a+1);
1600 drawpoint(&q, a+13);
1605 error("negative line width");
1606 src = drawimage(client, a+33);
1607 drawpoint(&sp, a+37);
1608 op = drawclientop(client);
1609 memline(dst, p, q, e0, e1, j, src, sp, op);
1610 /* avoid memlinebbox if possible */
1611 if(dstid==0 || dst->layer!=nil){
1612 /* BUG: this is terribly inefficient: update maximal containing rect*/
1613 r = memlinebbox(p, q, e0, e1, j);
1614 dstflush(dstid, dst, insetrect(r, -(1+1+j)));
1618 /* create image mask: 'm' newid[4] id[4] */
1622 printmesg("LL", a, 0);
1630 /* attach to a named image: 'n' dstid[4] j[1] name[j] */
1632 printmesg(fmt="Lz", a, 0);
1637 if(j == 0) /* give me a non-empty name please */
1642 dstid = BGLONG(a+1);
1643 if(drawlookup(client, dstid, 0))
1644 error(Eimageexists);
1645 dn = drawlookupname(j, (char*)a+6);
1648 if(drawinstall(client, dstid, dn->dimage->image, 0) == 0)
1650 di = drawlookup(client, dstid, 0);
1652 error("draw: can't happen");
1653 di->vers = dn->vers;
1654 di->name = smalloc(j+1);
1655 di->fromname = dn->dimage;
1656 di->fromname->ref++;
1657 memmove(di->name, a+6, j);
1659 client->infoid = dstid;
1662 /* name an image: 'N' dstid[4] in[1] j[1] name[j] */
1664 printmesg(fmt="Lbz", a, 0);
1670 if(j == 0) /* give me a non-empty name please */
1675 di = drawlookup(client, BGLONG(a+1), 0);
1677 error(Enodrawimage);
1681 drawaddname(client, di, j, (char*)a+7);
1683 dn = drawlookupname(j, (char*)a+7);
1686 if(dn->dimage != di)
1692 /* position window: 'o' id[4] r.min [2*4] screenr.min [2*4] */
1694 printmesg(fmt="LPP", a, 0);
1698 dst = drawimage(client, a+1);
1701 drawpoint(&q, a+13);
1702 r = dst->layer->screenr;
1703 ni = memlorigin(dst, p, q);
1705 error("image origin failed");
1708 addflush(dst->layer->screenr);
1709 ll = drawlookup(client, BGLONG(a+1), 1);
1710 drawrefreshscreen(ll, client);
1715 /* set compositing operator for next draw operation: 'O' op */
1717 printmesg(fmt="b", a, 0);
1724 /* filled polygon: 'P' dstid[4] n[2] wind[4] ignore[2*4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1725 /* polygon: 'p' dstid[4] n[2] end0[4] end1[4] radius[4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1728 printmesg(fmt="LslllLPP", a, 0);
1729 m = 1+4+2+4+4+4+4+2*4;
1732 dstid = BGLONG(a+1);
1733 dst = drawimage(client, a+1);
1736 error("negative count in polygon");
1743 error("negative polygon line width");
1745 src = drawimage(client, a+19);
1746 drawpoint(&sp, a+23);
1747 drawpoint(&p, a+31);
1749 pp = malloc(ni*sizeof(Point));
1753 if(dstid==0 || (dst->layer && dst->layer->screen->image->data == screenimage->data))
1754 doflush = 1; /* simplify test in loop */
1758 for(y=0; y<ni; y++){
1761 u = drawcoord(u, a+n, ox, &p.x);
1762 u = drawcoord(u, a+n, oy, &p.y);
1769 c = memlineendsize(e0);
1774 c = memlineendsize(e1);
1779 if(*a=='P' && e0!=1 && e0 !=~0)
1782 r = Rect(q.x-oesize, q.y-oesize, q.x+oesize+1, q.y+oesize+1);
1783 combinerect(&r, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1785 if(rectclip(&r, dst->clipr)) /* should perhaps be an arg to dstflush */
1786 dstflush(dstid, dst, r);
1791 dstflush(dstid, dst, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1792 op = drawclientop(client);
1794 mempoly(dst, pp, ni, e0, e1, j, src, sp, op);
1796 memfillpoly(dst, pp, ni, e0, src, sp, op);
1801 /* read: 'r' id[4] R[4*4] */
1803 printmesg(fmt="LR", a, 0);
1807 i = drawimage(client, a+1);
1809 error("readimage from window unimplemented");
1810 drawrectangle(&r, a+5);
1811 if(!rectinrect(r, i->r))
1812 error(Ereadoutside);
1813 c = bytesperline(r, i->depth);
1815 free(client->readdata);
1816 client->readdata = mallocz(c, 0);
1817 if(client->readdata == nil)
1818 error("readimage malloc failed");
1819 client->nreaddata = unloadmemimage(i, r, client->readdata, c);
1820 if(client->nreaddata < 0){
1821 free(client->readdata);
1822 client->readdata = nil;
1823 error("bad readimage call");
1827 /* string: 's' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] ni*(index[2]) */
1828 /* stringbg: 'x' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] bgid[4] bgpt[2*4] ni*(index[2]) */
1831 printmesg(fmt="LLLPRPs", a, 0);
1832 m = 1+4+4+4+2*4+4*4+2*4+2;
1838 dst = drawimage(client, a+1);
1839 dstid = BGLONG(a+1);
1840 src = drawimage(client, a+5);
1841 font = drawlookup(client, BGLONG(a+9), 1);
1843 error(Enodrawimage);
1844 if(font->nfchar == 0)
1846 drawpoint(&p, a+13);
1847 drawrectangle(&r, a+21);
1848 drawpoint(&sp, a+37);
1856 op = drawclientop(client);
1858 /* paint background */
1859 l = drawimage(client, a+47);
1860 drawpoint(&q, a+51);
1862 r.min.y = p.y-font->ascent;
1864 r.max.y = r.min.y+Dy(font->image->r);
1868 if(ci<0 || ci>=font->nfchar){
1872 r.max.x += font->fchar[ci].width;
1875 memdraw(dst, r, l, q, memopaque, ZP, op);
1881 if(ci<0 || ci>=font->nfchar){
1885 q = drawchar(dst, q, src, &sp, font, ci, op);
1889 p.y -= font->ascent;
1890 dstflush(dstid, dst, Rect(p.x, p.y, q.x, p.y+Dy(font->image->r)));
1893 /* use public screen: 'S' id[4] chan[4] */
1895 printmesg(fmt="Ll", a, 0);
1899 dstid = BGLONG(a+1);
1902 dscrn = drawlookupdscreen(dstid);
1903 if(dscrn==0 || (dscrn->public==0 && dscrn->owner!=client))
1904 error(Enodrawscreen);
1905 if(dscrn->screen->image->chan != BGLONG(a+5))
1906 error("inconsistent chan");
1907 if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0)
1911 /* top or bottom windows: 't' top[1] nw[2] n*id[4] */
1913 printmesg(fmt="bsL", a, 0);
1925 lp = malloc(nw*sizeof(Memimage*));
1933 lp[j] = drawimage(client, a+1+1+2+j*4);
1934 if(lp[0]->layer == 0)
1935 error("images are not windows");
1937 if(lp[j]->layer->screen != lp[0]->layer->screen)
1938 error("images not on same screen");
1940 memltofrontn(lp, nw);
1942 memltorearn(lp, nw);
1943 if(lp[0]->layer->screen->image->data == screenimage->data)
1945 addflush(lp[j]->layer->screenr);
1946 ll = drawlookup(client, BGLONG(a+1+1+2), 1);
1947 drawrefreshscreen(ll, client);
1954 printmesg(fmt="", a, 0);
1959 /* write: 'y' id[4] R[4*4] data[x*1] */
1960 /* write from compressed data: 'Y' id[4] R[4*4] data[x*1] */
1963 printmesg(fmt="LR", a, 0);
1964 // iprint("load %c\n", *a);
1968 dstid = BGLONG(a+1);
1969 dst = drawimage(client, a+1);
1970 drawrectangle(&r, a+5);
1971 if(!rectinrect(r, dst->r))
1972 error(Ewriteoutside);
1973 y = memload(dst, r, a+m, n-m, *a=='Y');
1975 error("bad writeimage call");
1976 dstflush(dstid, dst, r);
2005 * On 8 bit displays, load the default color map
2010 int r, g, b, cr, cg, cb, v;
2014 drawactive(1); /* to restore map from backup */
2015 for(r=0,i=0; r!=4; r++)
2016 for(v=0; v!=4; v++,i+=16){
2017 for(g=0,j=v-r; g!=4; g++)
2018 for(b=0;b!=4;b++,j++){
2024 if(den == 0) /* divide check -- pick grey shades */
2025 cr = cg = cb = v*17;
2033 cr*0x01010101, cg*0x01010101, cb*0x01010101);
2039 drawblankscreen(int blank)
2044 if(blank == sdraw.blanked)
2046 if(!canqlock(&sdraw))
2048 if(!initscreenimage()){
2053 nc = screenimage->depth > 8 ? 256 : 1<<screenimage->depth;
2056 * blankscreen uses the hardware to blank the screen
2057 * when possible. to help in cases when it is not possible,
2058 * we set the color map to be all black.
2060 if(blank == 0){ /* turn screen on */
2061 for(i=0; i<nc; i++, p+=3)
2062 setcolor(i, p[0], p[1], p[2]);
2064 }else{ /* turn screen off */
2066 for(i=0; i<nc; i++, p+=3){
2067 getcolor(i, &p[0], &p[1], &p[2]);
2068 setcolor(i, 0, 0, 0);
2071 sdraw.blanked = blank;
2076 * record activity on screen, changing blanking as appropriate
2079 drawactive(int active)
2083 sdraw.blanktime = 0;