2 #include "../port/lib.h"
6 #include "../port/error.h"
30 * 4 bits of file type (qids above)
31 * 24 bits of mux slot number +1; 0 means not attached to client
33 #define QSHIFT 4 /* location in qid of client # */
35 #define QID(q) ((((ulong)(q).path)&0x0000000F)>>0)
36 #define CLIENTPATH(q) ((((ulong)q)&0x7FFFFFF0)>>QSHIFT)
37 #define CLIENT(q) CLIENTPATH((q).path)
40 #define HASHMASK (NHASH-1)
41 #define IOUNIT (64*1024)
43 typedef struct Client Client;
44 typedef struct Draw Draw;
45 typedef struct DImage DImage;
46 typedef struct DScreen DScreen;
47 typedef struct CScreen CScreen;
48 typedef struct FChar FChar;
49 typedef struct Refresh Refresh;
50 typedef struct Refx Refx;
51 typedef struct DName DName;
53 ulong blanktime = 30; /* in minutes; a half hour */
64 int blanked; /* screen turned off */
65 ulong blanktime; /* time of last operation */
72 DImage* dimage[NHASH];
110 int minx; /* left edge of bits */
111 int maxx; /* right edge of bits */
112 uchar miny; /* first non-zero scan-line */
113 uchar maxy; /* last non-zero scan-line + 1 */
114 schar left; /* offset of baseline */
115 uchar width; /* width of baseline */
119 * Reference counts in DImages:
120 * one per open by original client
121 * one per screen image or fill
122 * one per image derived from this one by name
134 DScreen* dscreen; /* 0 if not a window */
135 DImage* fromname; /* image this one is derived from, by name */
160 static Memimage *screenimage;
161 static DImage* screendimage;
162 static char screenname[40];
163 static int screennameid;
165 static Rectangle flushrect;
167 static DScreen* dscreen;
168 extern void flushmemscreen(Rectangle);
169 void drawmesg(Client*, void*, int);
170 void drawuninstall(Client*, int);
171 void drawfreedimage(DImage*);
172 Client* drawclientofpath(ulong);
173 DImage* allocdimage(Memimage*);
175 static char Enodrawimage[] = "unknown id for draw image";
176 static char Enodrawscreen[] = "unknown id for draw screen";
177 static char Eshortdraw[] = "short draw message";
178 static char Eshortread[] = "draw read too short";
179 static char Eimageexists[] = "image id in use";
180 static char Escreenexists[] = "screen id in use";
181 static char Edrawmem[] = "image memory allocation failed";
182 static char Ereadoutside[] = "readimage outside image";
183 static char Ewriteoutside[] = "writeimage outside image";
184 static char Enotfont[] = "image not a font";
185 static char Eindex[] = "character index out of range";
186 static char Enoclient[] = "no such draw client";
187 static char Edepth[] = "image has bad depth";
188 static char Enameused[] = "image name in use";
189 static char Enoname[] = "no image with that name";
190 static char Eoldname[] = "named image no longer valid";
191 static char Enamed[] = "image already has name";
192 static char Ewrongname[] = "wrong name for image";
203 return canqlock(&drawlock);
213 drawgen(Chan *c, char*, Dirtab*, int, int s, Dir *dp)
226 mkqid(&q, Qtopdir, 0, QTDIR);
227 devdir(c, q, "#i", 0, eve, 0500, dp);
230 cl = drawclientofpath(c->qid.path);
232 strcpy(up->genbuf, "??");
234 sprint(up->genbuf, "%d", cl->clientid);
235 mkqid(&q, Q2nd, 0, QTDIR);
236 devdir(c, q, up->genbuf, 0, eve, 0500, dp);
239 panic("drawwalk %llux", c->qid.path);
245 * Top level directory contains the name of the device.
248 if(t == Qtopdir || t == Qwinname){
249 if(s == 1 || t == Qwinname){
250 mkqid(&q, Qwinname, 0, QTFILE);
251 devdir(c, q, "winname", 0, eve, 0444, dp);
254 mkqid(&q, Q2nd, 0, QTDIR);
255 devdir(c, q, "draw", 0, eve, 0555, dp);
263 * Second level contains "new" plus all the clients.
265 if(t == Q2nd || t == Qnew){
267 mkqid(&q, Qnew, 0, QTFILE);
268 devdir(c, q, "new", 0, eve, 0666, dp);
270 else if(s <= sdraw.nclient){
271 cl = sdraw.client[s-1];
274 sprint(up->genbuf, "%d", cl->clientid);
275 mkqid(&q, (s<<QSHIFT)|Q3rd, 0, QTDIR);
276 devdir(c, q, up->genbuf, 0, eve, 0555, dp);
287 path = c->qid.path&~((1<<QSHIFT)-1); /* slot component */
288 q.vers = c->qid.vers;
292 q.path = path|Qcolormap;
293 devdir(c, q, "colormap", 0, eve, 0600, dp);
297 devdir(c, q, "ctl", 0, eve, 0600, dp);
301 devdir(c, q, "data", 0, eve, 0600, dp);
304 q.path = path|Qrefresh;
305 devdir(c, q, "refresh", 0, eve, 0400, dp);
315 drawrefactive(void *a)
320 return c->refreshme || c->refresh!=0;
325 drawrefreshscreen(DImage *l, Client *client)
327 while(l != nil && l->dscreen == nil)
329 if(l != nil && l->dscreen->owner != client)
330 l->dscreen->owner->refreshme = 1;
335 drawrefresh(Memimage*, Rectangle r, void *v)
347 for(ref=c->refresh; ref; ref=ref->next)
348 if(ref->dimage == d){
349 combinerect(&ref->r, r);
352 ref = malloc(sizeof(Refresh));
356 ref->next = c->refresh;
362 addflush(Rectangle r)
367 if(sdraw.softscreen==0 || screenimage == nil || !rectclip(&r, screenimage->r))
369 if(flushrect.min.x >= flushrect.max.x){
375 combinerect(&nbb, r);
377 abb = Dx(flushrect)*Dy(flushrect);
378 anbb = Dx(nbb)*Dy(nbb);
380 * Area of new waste is area of new bb minus area of old bb,
381 * less the area of the new segment, which we assume is not waste.
382 * This could be negative, but that's OK.
384 waste += anbb-abb - ar;
389 * total area is small
390 * waste is less than half total area
393 if(anbb<=1024 || waste*2<anbb || rectXrect(flushrect, r)){
397 /* emit current state */
398 if(flushrect.min.x < flushrect.max.x)
399 flushmemscreen(flushrect);
406 dstflush(int dstid, Memimage *dst, Rectangle r)
411 combinerect(&flushrect, r);
414 if(screenimage == nil || dst == nil || (l = dst->layer) == nil)
417 if(l->screen->image->data != screenimage->data)
419 r = rectaddpt(r, l->delta);
420 l = l->screen->image->layer;
428 if(screenimage && flushrect.min.x < flushrect.max.x)
429 flushmemscreen(flushrect);
430 flushrect = Rect(10000, 10000, -10000, -10000);
435 drawcmp(char *a, char *b, int n)
439 return memcmp(a, b, n);
443 drawlookupname(int n, char *str)
448 ename = &name[sdraw.nname];
449 for(; name<ename; name++)
450 if(drawcmp(name->name, str, n) == 0)
456 drawgoodname(DImage *d)
460 /* if window, validate the screen's own images */
462 if(drawgoodname(d->dscreen->dimage) == 0
463 || drawgoodname(d->dscreen->dfill) == 0)
467 n = drawlookupname(strlen(d->name), d->name);
468 if(n==nil || n->vers!=d->vers)
474 drawlookup(Client *client, int id, int checkname)
478 d = client->dimage[id&HASHMASK];
481 if(checkname && !drawgoodname(d))
491 drawlookupdscreen(int id)
505 drawlookupscreen(Client *client, int id, CScreen **cs)
511 if(s->dscreen->id == id){
517 error(Enodrawscreen);
522 allocdimage(Memimage *i)
526 d = malloc(sizeof(DImage));
540 drawinstall(Client *client, int id, Memimage *i, DScreen *dscreen)
548 d->dscreen = dscreen;
549 d->next = client->dimage[id&HASHMASK];
550 client->dimage[id&HASHMASK] = d;
555 drawinstallscreen(Client *client, DScreen *d, int id, DImage *dimage, DImage *dfill, int public)
560 c = malloc(sizeof(CScreen));
561 if(dimage && dimage->image && dimage->image->chan == 0)
562 panic("bad image %p in drawinstallscreen", dimage->image);
567 d = malloc(sizeof(DScreen));
572 s = malloc(sizeof(Memscreen));
582 s->image = dimage->image;
587 s->fill = dfill->image;
600 c->next = client->cscreen;
606 drawdelname(DName *name)
612 memmove(name, name+1, (sdraw.nname-(i+1))*sizeof(DName));
617 drawfreedscreen(DScreen *this)
623 print("negative ref in drawfreedscreen\n");
628 dscreen = this->next;
631 while(next = ds->next){ /* assign = */
633 ds->next = this->next;
642 drawfreedimage(this->dimage);
644 drawfreedimage(this->dfill);
650 drawfreedimage(DImage *dimage)
658 print("negative ref in drawfreedimage\n");
663 for(i=0; i<sdraw.nname; )
664 if(sdraw.name[i].dimage == dimage)
665 drawdelname(sdraw.name+i);
668 if(dimage->fromname){ /* acquired by name; owned by someone else*/
669 drawfreedimage(dimage->fromname);
672 ds = dimage->dscreen;
675 if(screenimage && l->data == screenimage->data)
676 addflush(l->layer->screenr);
677 if(l->layer->refreshfn == drawrefresh) /* else true owner will clean up */
678 free(l->layer->refreshptr);
679 l->layer->refreshptr = nil;
680 if(drawgoodname(dimage))
686 freememimage(dimage->image);
693 drawuninstallscreen(Client *client, CScreen *this)
697 cs = client->cscreen;
699 client->cscreen = this->next;
700 drawfreedscreen(this->dscreen);
704 while(next = cs->next){ /* assign = */
706 cs->next = this->next;
707 drawfreedscreen(this->dscreen);
716 drawuninstall(Client *client, int id)
720 d = client->dimage[id&HASHMASK];
724 client->dimage[id&HASHMASK] = d->next;
728 while(next = d->next){ /* assign = */
730 d->next = next->next;
731 drawfreedimage(next);
740 drawaddname(Client *client, DImage *di, int n, char *str)
742 DName *name, *ename, *new, *t;
745 ename = &name[sdraw.nname];
746 for(; name<ename; name++)
747 if(drawcmp(name->name, str, n) == 0)
749 t = smalloc((sdraw.nname+1)*sizeof(DName));
750 memmove(t, sdraw.name, sdraw.nname*sizeof(DName));
753 new = &sdraw.name[sdraw.nname++];
754 new->name = smalloc(n+1);
755 memmove(new->name, str, n);
758 new->client = client;
759 new->vers = ++sdraw.vers;
768 for(i=0; i<sdraw.nclient; i++){
769 cl = sdraw.client[i];
773 if(i == sdraw.nclient){
774 cp = malloc((sdraw.nclient+1)*sizeof(Client*));
777 memmove(cp, sdraw.client, sdraw.nclient*sizeof(Client*));
783 cl = malloc(sizeof(Client));
786 memset(cl, 0, sizeof(Client));
788 cl->clientid = ++sdraw.clientid;
790 sdraw.client[i] = cl;
795 drawclientop(Client *cl)
808 * if draw has ever been used, we can't resize the frame buffer,
809 * even if all clients have exited (nclients is cumulative); it's too
812 return sdraw.nclient != 0;
816 drawclientofpath(ulong path)
821 slot = CLIENTPATH(path);
824 cl = sdraw.client[slot-1];
825 if(cl==0 || cl->clientid==0)
836 client = drawclientofpath(c->qid.path);
843 drawimage(Client *client, uchar *a)
847 d = drawlookup(client, BGLONG(a), 1);
854 drawrectangle(Rectangle *r, uchar *a)
856 r->min.x = BGLONG(a+0*4);
857 r->min.y = BGLONG(a+1*4);
858 r->max.x = BGLONG(a+2*4);
859 r->max.y = BGLONG(a+3*4);
863 drawpoint(Point *p, uchar *a)
865 p->x = BGLONG(a+0*4);
866 p->y = BGLONG(a+1*4);
870 drawchar(Memimage *dst, Memimage *rdst, Point p, Memimage *src, Point *sp, DImage *font, int index, int op)
875 static Memimage *tmp;
877 fc = &font->fchar[index];
878 r.min.x = p.x+fc->left;
879 r.min.y = p.y-(font->ascent-fc->miny);
880 r.max.x = r.min.x+(fc->maxx-fc->minx);
881 r.max.y = r.min.y+(fc->maxy-fc->miny);
882 sp1.x = sp->x+fc->left;
883 sp1.y = sp->y+fc->miny;
886 * If we're drawing greyscale fonts onto a VGA screen,
887 * it's very costly to read the screen memory to do the
888 * alpha blending inside memdraw. If this is really a stringbg,
889 * then rdst is the bg image (in main memory) which we can
890 * refer to for the underlying dst pixels instead of reading dst
893 if(ishwimage(dst) && !ishwimage(rdst) && font->image->depth > 1){
894 if(tmp == nil || tmp->chan != dst->chan || Dx(tmp->r) < Dx(r) || Dy(tmp->r) < Dy(r)){
897 tmp = allocmemimage(Rect(0,0,Dx(r),Dy(r)), dst->chan);
901 memdraw(tmp, Rect(0,0,Dx(r),Dy(r)), rdst, r.min, memopaque, ZP, S);
902 memdraw(tmp, Rect(0,0,Dx(r),Dy(r)), src, sp1, font->image, Pt(fc->minx, fc->miny), op);
903 memdraw(dst, r, tmp, ZP, memopaque, ZP, S);
906 memdraw(dst, r, src, sp1, font->image, Pt(fc->minx, fc->miny), op);
915 makescreenimage(void)
925 if((data = attachscreen(&r, &chan, &depth, &width, &sdraw.softscreen)) == nil)
927 if(sdraw.softscreen == 0xa110c){
928 /* hack: softscreen is memimage. */
929 md = *((Memdata**)(data - sizeof(ulong) - sizeof(Memdata*)));
931 assert(md->bdata == data);
935 if((i = allocmemimaged(r, chan, md)) == nil){
940 if((md = malloc(sizeof *md)) == nil)
946 if((i = allocmemimaged(r, chan, md)) == nil){
955 freememimage(i); /* frees md */
959 snprint(screenname, sizeof screenname, "noborder.screen.%d", ++screennameid);
960 drawaddname(nil, di, strlen(screenname), screenname);
967 initscreenimage(void)
969 if(screenimage != nil)
972 screendimage = makescreenimage();
973 if(screendimage == nil)
975 screenimage = screendimage->image;
976 // iprint("initscreenimage %p %p\n", screendimage, screenimage);
982 deletescreenimage(void)
986 /* will be freed via screendimage; disable */
987 screenimage->clipr = ZR;
991 drawfreedimage(screendimage);
998 resetscreenimage(void)
1006 drawattach(char *spec)
1009 if(!initscreenimage()){
1011 error("no frame buffer");
1014 return devattach('i', spec);
1018 drawwalk(Chan *c, Chan *nc, char **name, int nname)
1020 if(screenimage == nil)
1021 error("no frame buffer");
1022 return devwalk(c, nc, name, nname, 0, 0, drawgen);
1026 drawstat(Chan *c, uchar *db, int n)
1028 return devstat(c, db, n, 0, 0, drawgen);
1032 drawopen(Chan *c, int omode)
1038 if(c->qid.type & QTDIR){
1039 c = devopen(c, omode, 0, 0, drawgen);
1049 if(QID(c->qid) == Qnew){
1050 cl = drawnewclient();
1053 c->qid.path = Qctl|((cl->slot+1)<<QSHIFT);
1056 switch(QID(c->qid)){
1068 flushrect = Rect(10000, 10000, -10000, -10000);
1069 dn = drawlookupname(strlen(screenname), screenname);
1071 error("draw: cannot happen 2");
1072 if(drawinstall(cl, 0, dn->dimage->image, 0) == 0)
1074 di = drawlookup(cl, 0, 0);
1076 error("draw: cannot happen 1");
1077 di->vers = dn->vers;
1078 di->name = smalloc(strlen(screenname)+1);
1079 strcpy(di->name, screenname);
1080 di->fromname = dn->dimage;
1081 di->fromname->ref++;
1094 c->mode = openmode(omode);
1109 if(QID(c->qid) < Qcolormap) /* Qtopdir, Qnew, Q3rd, Q2nd have no client */
1118 if(QID(c->qid) == Qctl)
1120 if((c->flag&COPEN) && (decref(&cl->r)==0)){
1121 while(r = cl->refresh){ /* assign = */
1122 cl->refresh = r->next;
1126 for(i=0; i<sdraw.nname; )
1127 if(sdraw.name[i].client == cl)
1128 drawdelname(sdraw.name+i);
1132 drawuninstallscreen(cl, cl->cscreen);
1133 /* all screens are freed, so now we can free images */
1135 for(i=0; i<NHASH; i++){
1136 while((d = *dp) != nil){
1142 sdraw.client[cl->slot] = 0;
1143 drawflush(); /* to erase visible, now dead windows */
1151 drawread(Chan *c, void *a, long n, vlong off)
1154 ulong red, green, blue;
1163 if(c->qid.type & QTDIR)
1164 return devdirread(c, a, n, 0, 0, drawgen);
1165 if(QID(c->qid) == Qwinname)
1166 return readstr(off, a, n, screenname);
1174 switch(QID(c->qid)){
1179 error(Enodrawimage);
1180 if(cl->infoid == 0){
1183 error(Enodrawimage);
1185 di = drawlookup(cl, cl->infoid, 1);
1187 error(Enodrawimage);
1190 n = sprint(a, "%11d %11d %11s %11d %11d %11d %11d %11d %11d %11d %11d %11d",
1191 cl->clientid, cl->infoid, chantostr(buf, i->chan), (i->flags&Frepl)==Frepl,
1192 i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y,
1193 i->clipr.min.x, i->clipr.min.y, i->clipr.max.x, i->clipr.max.y);
1194 ((char*)a)[n++] = ' ';
1199 drawactive(1); /* to restore map from backup */
1200 p = malloc(4*12*256+1);
1204 for(index = 0; index < 256; index++){
1205 getcolor(index, &red, &green, &blue);
1206 m += sprint((char*)p+m, "%11d %11lud %11lud %11lud\n", index, red>>24, green>>24, blue>>24);
1208 n = readstr(offset, a, n, (char*)p);
1213 if(cl->readdata == nil)
1214 error("no draw data");
1215 if(n < cl->nreaddata)
1218 memmove(a, cl->readdata, cl->nreaddata);
1227 if(cl->refreshme || cl->refresh)
1239 sleep(&cl->refrend, drawrefactive, cl);
1246 while(cl->refresh && n>=5*4){
1248 BPLONG(p+0*4, r->dimage->id);
1249 BPLONG(p+1*4, r->r.min.x);
1250 BPLONG(p+2*4, r->r.min.y);
1251 BPLONG(p+3*4, r->r.max.x);
1252 BPLONG(p+4*4, r->r.max.y);
1253 cl->refresh = r->next;
1273 for(i=0; i<sdraw.nclient; i++){
1274 cl = sdraw.client[i];
1275 if(cl && (cl->refreshme || cl->refresh))
1276 wakeup(&cl->refrend);
1281 drawwrite(Chan *c, void *a, long n, vlong)
1283 char buf[128], *fields[4], *q;
1285 int i, m, red, green, blue, x;
1287 if(c->qid.type & QTDIR)
1296 switch(QID(c->qid)){
1299 error("unknown draw control request");
1300 cl->infoid = BGLONG((uchar*)a);
1304 drawactive(1); /* to restore map from backup */
1309 if(x > sizeof(buf)-1)
1311 q = memccpy(buf, a, '\n', x);
1319 if(tokenize(buf, fields, nelem(fields)) != 4)
1321 i = strtoul(fields[0], 0, 0);
1322 red = strtoul(fields[1], 0, 0);
1323 green = strtoul(fields[2], 0, 0);
1324 blue = strtoul(fields[3], &q, 0);
1327 if(red>255 || green>255 || blue>255 || i<0 || i>255)
1335 setcolor(i, red, green, blue);
1353 drawcoord(uchar *p, uchar *maxp, int oldx, int *newx)
1378 printmesg(char *fmt, uchar *a, int plsprnt)
1386 USED(fmt, a, buf, p, q, s);
1391 for(p=fmt; *p; p++){
1394 q += sprint(q, " %ld", (long)BGLONG(a));
1398 q += sprint(q, " %.8lux", (ulong)BGLONG(a));
1402 q += sprint(q, " [%d %d %d %d]", BGLONG(a), BGLONG(a+4), BGLONG(a+8), BGLONG(a+12));
1406 q += sprint(q, " [%d %d]", BGLONG(a), BGLONG(a+4));
1410 q += sprint(q, " %d", *a++);
1413 q += sprint(q, " %d", BGSHORT(a));
1417 q += sprint(q, " %.4ux", BGSHORT(a));
1424 iprint("%.*s", (int)(q-buf), buf);
1428 drawmesg(Client *client, void *av, int n)
1430 int c, repl, m, y, dstid, scrnid, ni, ci, j, nw, e0, e1, op, ox, oy, oesize, esize, doflush;
1431 uchar *u, *a, refresh;
1435 Point p, q, *pp, sp;
1436 Memimage *i, *bg, *dst, *src, *mask;
1439 DImage *font, *ll, *di, *ddst, *dsrc;
1451 if(fmt) printmesg(fmt, a, 1);
1452 /* iprint("error: %s\n", up->errstr); */
1460 error("bad draw command");
1461 /* new allocate: 'b' id[4] screenid[4] refresh[1] chan[4] repl[1] R[4*4] clipR[4*4] rrggbbaa[4] */
1463 printmesg(fmt="LLbLbRRL", a, 0);
1464 m = 1+4+4+1+4+1+4*4+4*4+4;
1467 dstid = BGLONG(a+1);
1468 scrnid = BGLONG(a+5);
1470 chan = BGLONG(a+10);
1472 drawrectangle(&r, a+15);
1473 drawrectangle(&clipr, a+31);
1474 value = BGLONG(a+47);
1475 if(drawlookup(client, dstid, 0))
1476 error(Eimageexists);
1478 dscrn = drawlookupscreen(client, scrnid, &cs);
1479 scrn = dscrn->screen;
1480 if(repl || chan!=scrn->image->chan)
1481 error("image parameters incompatible with screen");
1487 reffn = memlnorefresh;
1490 reffn = drawrefresh;
1493 error("unknown refresh method");
1495 l = memlalloc(scrn, r, reffn, 0, value);
1498 addflush(l->layer->screenr);
1500 rectclip(&l->clipr, r);
1501 if(drawinstall(client, dstid, l, dscrn) == 0){
1508 if(reffn == drawrefresh){
1509 refx = malloc(sizeof(Refx));
1511 drawuninstall(client, dstid);
1514 refx->client = client;
1515 refx->dimage = drawlookup(client, dstid, 1);
1517 memlsetrefresh(l, reffn, refx);
1521 i = allocmemimage(r, chan);
1528 rectclip(&i->clipr, r);
1529 if(drawinstall(client, dstid, i, 0) == 0){
1533 memfillcolor(i, value);
1536 /* allocate screen: 'A' id[4] imageid[4] fillid[4] public[1] */
1538 printmesg(fmt="LLLb", a, 1);
1542 dstid = BGLONG(a+1);
1545 if(drawlookupdscreen(dstid))
1546 error(Escreenexists);
1547 ddst = drawlookup(client, BGLONG(a+5), 1);
1548 dsrc = drawlookup(client, BGLONG(a+9), 1);
1549 if(ddst==0 || dsrc==0)
1550 error(Enodrawimage);
1551 if(drawinstallscreen(client, 0, dstid, ddst, dsrc, a[13]) == 0)
1555 /* set repl and clip: 'c' dstid[4] repl[1] clipR[4*4] */
1557 printmesg(fmt="LbR", a, 0);
1561 ddst = drawlookup(client, BGLONG(a+1), 1);
1563 error(Enodrawimage);
1565 error("cannot change repl/clipr of shared image");
1568 dst->flags |= Frepl;
1569 drawrectangle(&dst->clipr, a+6);
1572 /* draw: 'd' dstid[4] srcid[4] maskid[4] R[4*4] P[2*4] P[2*4] */
1574 printmesg(fmt="LLLRPP", a, 0);
1575 m = 1+4+4+4+4*4+2*4+2*4;
1578 dst = drawimage(client, a+1);
1579 dstid = BGLONG(a+1);
1580 src = drawimage(client, a+5);
1581 mask = drawimage(client, a+9);
1582 drawrectangle(&r, a+13);
1583 drawpoint(&p, a+29);
1584 drawpoint(&q, a+37);
1585 op = drawclientop(client);
1586 memdraw(dst, r, src, p, mask, q, op);
1587 dstflush(dstid, dst, r);
1590 /* toggle debugging: 'D' val[1] */
1592 printmesg(fmt="b", a, 0);
1599 /* ellipse: 'e' dstid[4] srcid[4] center[2*4] a[4] b[4] thick[4] sp[2*4] alpha[4] phi[4]*/
1602 printmesg(fmt="LLPlllPll", a, 0);
1603 m = 1+4+4+2*4+4+4+4+2*4+2*4;
1606 dst = drawimage(client, a+1);
1607 dstid = BGLONG(a+1);
1608 src = drawimage(client, a+5);
1613 error("invalid ellipse semidiameter");
1616 error("negative ellipse thickness");
1617 drawpoint(&sp, a+29);
1623 op = drawclientop(client);
1624 /* high bit indicates arc angles are present */
1626 if((ox & (1<<30)) == 0)
1628 memarc(dst, p, e0, e1, c, src, sp, ox, oy, op);
1630 memellipse(dst, p, e0, e1, c, src, sp, op);
1631 dstflush(dstid, dst, Rect(p.x-e0-j, p.y-e1-j, p.x+e0+j+1, p.y+e1+j+1));
1634 /* free: 'f' id[4] */
1636 printmesg(fmt="L", a, 1);
1640 ll = drawlookup(client, BGLONG(a+1), 0);
1641 if(ll && ll->dscreen && ll->dscreen->owner != client)
1642 ll->dscreen->owner->refreshme = 1;
1643 drawuninstall(client, BGLONG(a+1));
1646 /* free screen: 'F' id[4] */
1648 printmesg(fmt="L", a, 1);
1652 drawlookupscreen(client, BGLONG(a+1), &cs);
1653 drawuninstallscreen(client, cs);
1656 /* initialize font: 'i' fontid[4] nchars[4] ascent[1] */
1658 printmesg(fmt="Llb", a, 1);
1662 dstid = BGLONG(a+1);
1664 error("cannot use display as font");
1665 font = drawlookup(client, dstid, 1);
1667 error(Enodrawimage);
1668 if(font->image->layer)
1669 error("cannot use window as font");
1671 if(ni<=0 || ni>4096)
1672 error("bad font size (4096 chars max)");
1673 free(font->fchar); /* should we complain if non-zero? */
1674 font->fchar = malloc(ni*sizeof(FChar));
1675 if(font->fchar == 0)
1676 error("no memory for font");
1677 memset(font->fchar, 0, ni*sizeof(FChar));
1679 font->ascent = a[9];
1682 /* load character: 'l' fontid[4] srcid[4] index[2] R[4*4] P[2*4] left[1] width[1] */
1684 printmesg(fmt="LLSRPbb", a, 0);
1685 m = 1+4+4+2+4*4+2*4+1+1;
1688 font = drawlookup(client, BGLONG(a+1), 1);
1690 error(Enodrawimage);
1691 if(font->nfchar == 0)
1693 src = drawimage(client, a+5);
1695 if(ci >= font->nfchar)
1697 drawrectangle(&r, a+11);
1698 drawpoint(&p, a+27);
1699 memdraw(font->image, r, src, p, memopaque, p, S);
1700 fc = &font->fchar[ci];
1709 /* draw line: 'L' dstid[4] p0[2*4] p1[2*4] end0[4] end1[4] radius[4] srcid[4] sp[2*4] */
1711 printmesg(fmt="LPPlllLP", a, 0);
1712 m = 1+4+2*4+2*4+4+4+4+4+2*4;
1715 dst = drawimage(client, a+1);
1716 dstid = BGLONG(a+1);
1718 drawpoint(&q, a+13);
1723 error("negative line width");
1724 src = drawimage(client, a+33);
1725 drawpoint(&sp, a+37);
1726 op = drawclientop(client);
1727 memline(dst, p, q, e0, e1, j, src, sp, op);
1728 /* avoid memlinebbox if possible */
1729 if(dstid==0 || dst->layer!=nil){
1730 /* BUG: this is terribly inefficient: update maximal containing rect*/
1731 r = memlinebbox(p, q, e0, e1, j);
1732 dstflush(dstid, dst, insetrect(r, -(1+1+j)));
1736 /* create image mask: 'm' newid[4] id[4] */
1740 printmesg("LL", a, 0);
1748 /* attach to a named image: 'n' dstid[4] j[1] name[j] */
1750 printmesg(fmt="Lz", a, 0);
1755 if(j == 0) /* give me a non-empty name please */
1760 dstid = BGLONG(a+1);
1761 if(drawlookup(client, dstid, 0))
1762 error(Eimageexists);
1763 dn = drawlookupname(j, (char*)a+6);
1766 if(drawinstall(client, dstid, dn->dimage->image, 0) == 0)
1768 di = drawlookup(client, dstid, 0);
1770 error("draw: cannot happen");
1771 di->vers = dn->vers;
1772 di->name = smalloc(j+1);
1773 di->fromname = dn->dimage;
1774 di->fromname->ref++;
1775 memmove(di->name, a+6, j);
1777 client->infoid = dstid;
1780 /* name an image: 'N' dstid[4] in[1] j[1] name[j] */
1782 printmesg(fmt="Lbz", a, 0);
1788 if(j == 0) /* give me a non-empty name please */
1793 di = drawlookup(client, BGLONG(a+1), 0);
1795 error(Enodrawimage);
1799 drawaddname(client, di, j, (char*)a+7);
1801 dn = drawlookupname(j, (char*)a+7);
1804 if(dn->dimage != di)
1810 /* position window: 'o' id[4] r.min [2*4] screenr.min [2*4] */
1812 printmesg(fmt="LPP", a, 0);
1816 dst = drawimage(client, a+1);
1819 drawpoint(&q, a+13);
1820 r = dst->layer->screenr;
1821 ni = memlorigin(dst, p, q);
1823 error("image origin failed");
1826 addflush(dst->layer->screenr);
1827 ll = drawlookup(client, BGLONG(a+1), 1);
1828 drawrefreshscreen(ll, client);
1833 /* set compositing operator for next draw operation: 'O' op */
1835 printmesg(fmt="b", a, 0);
1842 /* filled polygon: 'P' dstid[4] n[2] wind[4] ignore[2*4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1843 /* polygon: 'p' dstid[4] n[2] end0[4] end1[4] radius[4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1846 printmesg(fmt="LslllLPP", a, 0);
1847 m = 1+4+2+4+4+4+4+2*4;
1850 dstid = BGLONG(a+1);
1851 dst = drawimage(client, a+1);
1854 error("negative count in polygon");
1861 error("negative polygon line width");
1863 src = drawimage(client, a+19);
1864 drawpoint(&sp, a+23);
1865 drawpoint(&p, a+31);
1867 pp = malloc(ni*sizeof(Point));
1871 if(dstid==0 || (screenimage && dst->layer && dst->layer->screen->image->data == screenimage->data))
1872 doflush = 1; /* simplify test in loop */
1876 for(y=0; y<ni; y++){
1879 u = drawcoord(u, a+n, ox, &p.x);
1880 u = drawcoord(u, a+n, oy, &p.y);
1887 c = memlineendsize(e0);
1892 c = memlineendsize(e1);
1897 if(*a=='P' && e0!=1 && e0 !=~0)
1900 r = Rect(q.x-oesize, q.y-oesize, q.x+oesize+1, q.y+oesize+1);
1901 combinerect(&r, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1903 if(rectclip(&r, dst->clipr)) /* should perhaps be an arg to dstflush */
1904 dstflush(dstid, dst, r);
1909 dstflush(dstid, dst, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1910 op = drawclientop(client);
1912 mempoly(dst, pp, ni, e0, e1, j, src, sp, op);
1914 memfillpoly(dst, pp, ni, e0, src, sp, op);
1919 /* read: 'r' id[4] R[4*4] */
1921 printmesg(fmt="LR", a, 0);
1925 i = drawimage(client, a+1);
1926 drawrectangle(&r, a+5);
1927 if(!rectinrect(r, i->r))
1928 error(Ereadoutside);
1929 c = bytesperline(r, i->depth);
1931 free(client->readdata);
1932 client->readdata = mallocz(c, 0);
1933 if(client->readdata == nil)
1934 error("readimage malloc failed");
1935 client->nreaddata = memunload(i, r, client->readdata, c);
1936 if(client->nreaddata < 0){
1937 free(client->readdata);
1938 client->readdata = nil;
1939 error("bad readimage call");
1943 /* string: 's' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] ni*(index[2]) */
1944 /* 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]) */
1947 printmesg(fmt="LLLPRPs", a, 0);
1948 m = 1+4+4+4+2*4+4*4+2*4+2;
1954 dst = drawimage(client, a+1);
1955 dstid = BGLONG(a+1);
1956 src = drawimage(client, a+5);
1957 font = drawlookup(client, BGLONG(a+9), 1);
1959 error(Enodrawimage);
1960 if(font->nfchar == 0)
1962 drawpoint(&p, a+13);
1963 drawrectangle(&r, a+21);
1964 drawpoint(&sp, a+37);
1972 op = drawclientop(client);
1975 /* paint background */
1976 bg = drawimage(client, a+47);
1977 drawpoint(&q, a+51);
1979 r.min.y = p.y-font->ascent;
1981 r.max.y = r.min.y+Dy(font->image->r);
1985 if(ci<0 || ci>=font->nfchar){
1989 r.max.x += font->fchar[ci].width;
1992 memdraw(dst, r, bg, q, memopaque, ZP, op);
1998 if(ci<0 || ci>=font->nfchar){
2002 q = drawchar(dst, bg, q, src, &sp, font, ci, op);
2006 p.y -= font->ascent;
2007 dstflush(dstid, dst, Rect(p.x, p.y, q.x, p.y+Dy(font->image->r)));
2010 /* use public screen: 'S' id[4] chan[4] */
2012 printmesg(fmt="Ll", a, 0);
2016 dstid = BGLONG(a+1);
2019 dscrn = drawlookupdscreen(dstid);
2020 if(dscrn==0 || (dscrn->public==0 && dscrn->owner!=client))
2021 error(Enodrawscreen);
2022 if(dscrn->screen->image->chan != BGLONG(a+5))
2023 error("inconsistent chan");
2024 if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0)
2028 /* top or bottom windows: 't' top[1] nw[2] n*id[4] */
2030 printmesg(fmt="bsL", a, 0);
2042 lp = malloc(nw*sizeof(Memimage*));
2050 lp[j] = drawimage(client, a+1+1+2+j*4);
2051 if(lp[0]->layer == 0)
2052 error("images are not windows");
2054 if(lp[j]->layer->screen != lp[0]->layer->screen)
2055 error("images not on same screen");
2057 memltofrontn(lp, nw);
2059 memltorearn(lp, nw);
2060 if(screenimage && lp[0]->layer->screen->image->data == screenimage->data)
2062 addflush(lp[j]->layer->screenr);
2063 ll = drawlookup(client, BGLONG(a+1+1+2), 1);
2064 drawrefreshscreen(ll, client);
2071 printmesg(fmt="", a, 0);
2076 /* write: 'y' id[4] R[4*4] data[x*1] */
2077 /* write from compressed data: 'Y' id[4] R[4*4] data[x*1] */
2080 printmesg(fmt="LR", a, 0);
2081 // iprint("load %c\n", *a);
2085 dstid = BGLONG(a+1);
2086 dst = drawimage(client, a+1);
2087 drawrectangle(&r, a+5);
2088 if(!rectinrect(r, dst->r))
2089 error(Ewriteoutside);
2090 y = memload(dst, r, a+m, n-m, *a=='Y');
2092 error("bad writeimage call");
2093 dstflush(dstid, dst, r);
2123 * On 8 bit displays, load the default color map
2128 int r, g, b, cr, cg, cb, v;
2132 drawactive(1); /* to restore map from backup */
2133 for(r=0,i=0; r!=4; r++)
2134 for(v=0; v!=4; v++,i+=16){
2135 for(g=0,j=v-r; g!=4; g++)
2136 for(b=0;b!=4;b++,j++){
2142 if(den == 0) /* divide check -- pick grey shades */
2143 cr = cg = cb = v*17;
2151 cr*0x01010101, cg*0x01010101, cb*0x01010101);
2157 drawblankscreen(int blank)
2162 if(blank == sdraw.blanked)
2166 if(screenimage == nil){
2171 nc = screenimage->depth > 8 ? 256 : 1<<screenimage->depth;
2174 * blankscreen uses the hardware to blank the screen
2175 * when possible. to help in cases when it is not possible,
2176 * we set the color map to be all black.
2178 if(blank == 0){ /* turn screen on */
2179 for(i=0; i<nc; i++, p+=3)
2180 setcolor(i, p[0], p[1], p[2]);
2182 }else{ /* turn screen off */
2184 for(i=0; i<nc; i++, p+=3){
2185 getcolor(i, &p[0], &p[1], &p[2]);
2186 setcolor(i, 0, 0, 0);
2189 sdraw.blanked = blank;
2194 * record activity on screen, changing blanking as appropriate
2197 drawactive(int active)
2201 sdraw.blanktime = MACHP(0)->ticks;
2203 if(blanktime && sdraw.blanktime && TK2SEC(MACHP(0)->ticks - sdraw.blanktime)/60 >= blanktime)
2211 return TK2SEC(MACHP(0)->ticks - sdraw.blanktime)/60;