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)
611 memmove(name, name+1, (sdraw.nname-(i+1))*sizeof(DName));
616 drawfreedscreen(DScreen *this)
622 print("negative ref in drawfreedscreen\n");
627 dscreen = this->next;
630 while(next = ds->next){ /* assign = */
632 ds->next = this->next;
641 drawfreedimage(this->dimage);
643 drawfreedimage(this->dfill);
649 drawfreedimage(DImage *dimage)
657 print("negative ref in drawfreedimage\n");
662 for(i=0; i<sdraw.nname; )
663 if(sdraw.name[i].dimage == dimage)
664 drawdelname(sdraw.name+i);
667 if(dimage->fromname){ /* acquired by name; owned by someone else*/
668 drawfreedimage(dimage->fromname);
671 ds = dimage->dscreen;
674 if(screenimage && l->data == screenimage->data)
675 addflush(l->layer->screenr);
676 if(l->layer->refreshfn == drawrefresh) /* else true owner will clean up */
677 free(l->layer->refreshptr);
678 l->layer->refreshptr = nil;
679 if(drawgoodname(dimage))
685 freememimage(dimage->image);
692 drawuninstallscreen(Client *client, CScreen *this)
696 cs = client->cscreen;
698 client->cscreen = this->next;
699 drawfreedscreen(this->dscreen);
703 while(next = cs->next){ /* assign = */
705 cs->next = this->next;
706 drawfreedscreen(this->dscreen);
715 drawuninstall(Client *client, int id)
719 d = client->dimage[id&HASHMASK];
723 client->dimage[id&HASHMASK] = d->next;
727 while(next = d->next){ /* assign = */
729 d->next = next->next;
730 drawfreedimage(next);
739 drawaddname(Client *client, DImage *di, int n, char *str)
741 DName *name, *ename, *new, *t;
744 ename = &name[sdraw.nname];
745 for(; name<ename; name++)
746 if(drawcmp(name->name, str, n) == 0)
748 t = smalloc((sdraw.nname+1)*sizeof(DName));
749 memmove(t, sdraw.name, sdraw.nname*sizeof(DName));
752 new = &sdraw.name[sdraw.nname++];
753 new->name = smalloc(n+1);
754 memmove(new->name, str, n);
757 new->client = client;
758 new->vers = ++sdraw.vers;
767 for(i=0; i<sdraw.nclient; i++){
768 cl = sdraw.client[i];
772 if(i == sdraw.nclient){
773 cp = malloc((sdraw.nclient+1)*sizeof(Client*));
776 memmove(cp, sdraw.client, sdraw.nclient*sizeof(Client*));
782 cl = malloc(sizeof(Client));
785 memset(cl, 0, sizeof(Client));
787 cl->clientid = ++sdraw.clientid;
789 sdraw.client[i] = cl;
794 drawclientop(Client *cl)
807 * if draw has ever been used, we can't resize the frame buffer,
808 * even if all clients have exited (nclients is cumulative); it's too
811 return sdraw.nclient != 0;
815 drawclientofpath(ulong path)
820 slot = CLIENTPATH(path);
823 cl = sdraw.client[slot-1];
824 if(cl==0 || cl->clientid==0)
835 client = drawclientofpath(c->qid.path);
842 drawimage(Client *client, uchar *a)
846 d = drawlookup(client, BGLONG(a), 1);
853 drawrectangle(Rectangle *r, uchar *a)
855 r->min.x = BGLONG(a+0*4);
856 r->min.y = BGLONG(a+1*4);
857 r->max.x = BGLONG(a+2*4);
858 r->max.y = BGLONG(a+3*4);
862 drawpoint(Point *p, uchar *a)
864 p->x = BGLONG(a+0*4);
865 p->y = BGLONG(a+1*4);
869 drawchar(Memimage *dst, Memimage *rdst, Point p, Memimage *src, Point *sp, DImage *font, int index, int op)
874 static Memimage *tmp;
876 fc = &font->fchar[index];
877 r.min.x = p.x+fc->left;
878 r.min.y = p.y-(font->ascent-fc->miny);
879 r.max.x = r.min.x+(fc->maxx-fc->minx);
880 r.max.y = r.min.y+(fc->maxy-fc->miny);
881 sp1.x = sp->x+fc->left;
882 sp1.y = sp->y+fc->miny;
885 * If we're drawing greyscale fonts onto a VGA screen,
886 * it's very costly to read the screen memory to do the
887 * alpha blending inside memdraw. If this is really a stringbg,
888 * then rdst is the bg image (in main memory) which we can
889 * refer to for the underlying dst pixels instead of reading dst
892 if(ishwimage(dst) && !ishwimage(rdst) && font->image->depth > 1){
893 if(tmp == nil || tmp->chan != dst->chan || Dx(tmp->r) < Dx(r) || Dy(tmp->r) < Dy(r)){
896 tmp = allocmemimage(Rect(0,0,Dx(r),Dy(r)), dst->chan);
900 memdraw(tmp, Rect(0,0,Dx(r),Dy(r)), rdst, r.min, memopaque, ZP, S);
901 memdraw(tmp, Rect(0,0,Dx(r),Dy(r)), src, sp1, font->image, Pt(fc->minx, fc->miny), op);
902 memdraw(dst, r, tmp, ZP, memopaque, ZP, S);
905 memdraw(dst, r, src, sp1, font->image, Pt(fc->minx, fc->miny), op);
914 makescreenimage(void)
924 if((data = attachscreen(&r, &chan, &depth, &width, &sdraw.softscreen)) == nil)
926 if(sdraw.softscreen == 0xa110c){
927 /* hack: softscreen is memimage. */
928 md = *((Memdata**)(data - sizeof(ulong) - sizeof(Memdata*)));
930 assert(md->bdata == data);
934 if((i = allocmemimaged(r, chan, md)) == nil){
939 if((md = malloc(sizeof *md)) == nil)
945 if((i = allocmemimaged(r, chan, md)) == nil){
954 freememimage(i); /* frees md */
958 snprint(screenname, sizeof screenname, "noborder.screen.%d", ++screennameid);
959 drawaddname(nil, di, strlen(screenname), screenname);
966 initscreenimage(void)
968 if(screenimage != nil)
971 screendimage = makescreenimage();
972 if(screendimage == nil)
974 screenimage = screendimage->image;
975 // iprint("initscreenimage %p %p\n", screendimage, screenimage);
981 deletescreenimage(void)
985 /* will be freed via screendimage; disable */
986 screenimage->clipr = ZR;
990 drawfreedimage(screendimage);
997 resetscreenimage(void)
1005 drawattach(char *spec)
1008 if(!initscreenimage()){
1010 error("no frame buffer");
1013 return devattach('i', spec);
1017 drawwalk(Chan *c, Chan *nc, char **name, int nname)
1019 if(screenimage == nil)
1020 error("no frame buffer");
1021 return devwalk(c, nc, name, nname, 0, 0, drawgen);
1025 drawstat(Chan *c, uchar *db, int n)
1027 return devstat(c, db, n, 0, 0, drawgen);
1031 drawopen(Chan *c, int omode)
1037 if(c->qid.type & QTDIR){
1038 c = devopen(c, omode, 0, 0, drawgen);
1048 if(QID(c->qid) == Qnew){
1049 cl = drawnewclient();
1052 c->qid.path = Qctl|((cl->slot+1)<<QSHIFT);
1055 switch(QID(c->qid)){
1067 flushrect = Rect(10000, 10000, -10000, -10000);
1068 dn = drawlookupname(strlen(screenname), screenname);
1070 error("draw: cannot happen 2");
1071 if(drawinstall(cl, 0, dn->dimage->image, 0) == 0)
1073 di = drawlookup(cl, 0, 0);
1075 error("draw: cannot happen 1");
1076 di->vers = dn->vers;
1077 di->name = smalloc(strlen(screenname)+1);
1078 strcpy(di->name, screenname);
1079 di->fromname = dn->dimage;
1080 di->fromname->ref++;
1093 c->mode = openmode(omode);
1108 if(QID(c->qid) < Qcolormap) /* Qtopdir, Qnew, Q3rd, Q2nd have no client */
1117 if(QID(c->qid) == Qctl)
1119 if((c->flag&COPEN) && (decref(&cl->r)==0)){
1120 while(r = cl->refresh){ /* assign = */
1121 cl->refresh = r->next;
1125 for(i=0; i<sdraw.nname; )
1126 if(sdraw.name[i].client == cl)
1127 drawdelname(sdraw.name+i);
1131 drawuninstallscreen(cl, cl->cscreen);
1132 /* all screens are freed, so now we can free images */
1134 for(i=0; i<NHASH; i++){
1135 while((d = *dp) != nil){
1141 sdraw.client[cl->slot] = 0;
1142 drawflush(); /* to erase visible, now dead windows */
1150 drawread(Chan *c, void *a, long n, vlong off)
1153 ulong red, green, blue;
1162 if(c->qid.type & QTDIR)
1163 return devdirread(c, a, n, 0, 0, drawgen);
1164 if(QID(c->qid) == Qwinname)
1165 return readstr(off, a, n, screenname);
1173 switch(QID(c->qid)){
1178 error(Enodrawimage);
1179 if(cl->infoid == 0){
1182 error(Enodrawimage);
1184 di = drawlookup(cl, cl->infoid, 1);
1186 error(Enodrawimage);
1189 n = sprint(a, "%11d %11d %11s %11d %11d %11d %11d %11d %11d %11d %11d %11d ",
1190 cl->clientid, cl->infoid, chantostr(buf, i->chan), (i->flags&Frepl)==Frepl,
1191 i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y,
1192 i->clipr.min.x, i->clipr.min.y, i->clipr.max.x, i->clipr.max.y);
1197 drawactive(1); /* to restore map from backup */
1198 p = malloc(4*12*256+1);
1202 for(index = 0; index < 256; index++){
1203 getcolor(index, &red, &green, &blue);
1204 m += sprint((char*)p+m, "%11d %11lud %11lud %11lud\n", index, red>>24, green>>24, blue>>24);
1206 n = readstr(offset, a, n, (char*)p);
1211 if(cl->readdata == nil)
1212 error("no draw data");
1213 if(n < cl->nreaddata)
1216 memmove(a, cl->readdata, cl->nreaddata);
1225 if(cl->refreshme || cl->refresh)
1237 sleep(&cl->refrend, drawrefactive, cl);
1244 while(cl->refresh && n>=5*4){
1246 BPLONG(p+0*4, r->dimage->id);
1247 BPLONG(p+1*4, r->r.min.x);
1248 BPLONG(p+2*4, r->r.min.y);
1249 BPLONG(p+3*4, r->r.max.x);
1250 BPLONG(p+4*4, r->r.max.y);
1251 cl->refresh = r->next;
1271 for(i=0; i<sdraw.nclient; i++){
1272 cl = sdraw.client[i];
1273 if(cl && (cl->refreshme || cl->refresh))
1274 wakeup(&cl->refrend);
1279 drawwrite(Chan *c, void *a, long n, vlong)
1281 char buf[128], *fields[4], *q;
1283 int i, m, red, green, blue, x;
1285 if(c->qid.type & QTDIR)
1294 switch(QID(c->qid)){
1297 error("unknown draw control request");
1298 cl->infoid = BGLONG((uchar*)a);
1302 drawactive(1); /* to restore map from backup */
1307 if(x > sizeof(buf)-1)
1309 q = memccpy(buf, a, '\n', x);
1317 if(tokenize(buf, fields, nelem(fields)) != 4)
1319 i = strtoul(fields[0], 0, 0);
1320 red = strtoul(fields[1], 0, 0);
1321 green = strtoul(fields[2], 0, 0);
1322 blue = strtoul(fields[3], &q, 0);
1325 if(red>255 || green>255 || blue>255 || i<0 || i>255)
1333 setcolor(i, red, green, blue);
1351 drawcoord(uchar *p, uchar *maxp, int oldx, int *newx)
1376 printmesg(char *fmt, uchar *a, int plsprnt)
1384 USED(fmt, a, buf, p, q, s);
1389 for(p=fmt; *p; p++){
1392 q += sprint(q, " %ld", (long)BGLONG(a));
1396 q += sprint(q, " %.8lux", (ulong)BGLONG(a));
1400 q += sprint(q, " [%d %d %d %d]", BGLONG(a), BGLONG(a+4), BGLONG(a+8), BGLONG(a+12));
1404 q += sprint(q, " [%d %d]", BGLONG(a), BGLONG(a+4));
1408 q += sprint(q, " %d", *a++);
1411 q += sprint(q, " %d", BGSHORT(a));
1415 q += sprint(q, " %.4ux", BGSHORT(a));
1422 iprint("%.*s", (int)(q-buf), buf);
1426 drawmesg(Client *client, void *av, int n)
1428 int c, repl, m, y, dstid, scrnid, ni, ci, j, nw, e0, e1, op, ox, oy, oesize, esize, doflush;
1429 uchar *u, *a, refresh;
1433 Point p, q, *pp, sp;
1434 Memimage *i, *bg, *dst, *src, *mask;
1437 DImage *font, *ll, *di, *ddst, *dsrc;
1449 if(fmt) printmesg(fmt, a, 1);
1450 /* iprint("error: %s\n", up->errstr); */
1458 error("bad draw command");
1459 /* new allocate: 'b' id[4] screenid[4] refresh[1] chan[4] repl[1] R[4*4] clipR[4*4] rrggbbaa[4] */
1461 printmesg(fmt="LLbLbRRL", a, 0);
1462 m = 1+4+4+1+4+1+4*4+4*4+4;
1465 dstid = BGLONG(a+1);
1466 scrnid = BGSHORT(a+5);
1468 chan = BGLONG(a+10);
1470 drawrectangle(&r, a+15);
1471 drawrectangle(&clipr, a+31);
1472 value = BGLONG(a+47);
1473 if(drawlookup(client, dstid, 0))
1474 error(Eimageexists);
1476 dscrn = drawlookupscreen(client, scrnid, &cs);
1477 scrn = dscrn->screen;
1478 if(repl || chan!=scrn->image->chan)
1479 error("image parameters incompatible with screen");
1485 reffn = memlnorefresh;
1488 reffn = drawrefresh;
1491 error("unknown refresh method");
1493 l = memlalloc(scrn, r, reffn, 0, value);
1496 addflush(l->layer->screenr);
1498 rectclip(&l->clipr, r);
1499 if(drawinstall(client, dstid, l, dscrn) == 0){
1506 if(reffn == drawrefresh){
1507 refx = malloc(sizeof(Refx));
1509 drawuninstall(client, dstid);
1512 refx->client = client;
1513 refx->dimage = drawlookup(client, dstid, 1);
1515 memlsetrefresh(l, reffn, refx);
1519 i = allocmemimage(r, chan);
1526 rectclip(&i->clipr, r);
1527 if(drawinstall(client, dstid, i, 0) == 0){
1531 memfillcolor(i, value);
1534 /* allocate screen: 'A' id[4] imageid[4] fillid[4] public[1] */
1536 printmesg(fmt="LLLb", a, 1);
1540 dstid = BGLONG(a+1);
1543 if(drawlookupdscreen(dstid))
1544 error(Escreenexists);
1545 ddst = drawlookup(client, BGLONG(a+5), 1);
1546 dsrc = drawlookup(client, BGLONG(a+9), 1);
1547 if(ddst==0 || dsrc==0)
1548 error(Enodrawimage);
1549 if(drawinstallscreen(client, 0, dstid, ddst, dsrc, a[13]) == 0)
1553 /* set repl and clip: 'c' dstid[4] repl[1] clipR[4*4] */
1555 printmesg(fmt="LbR", a, 0);
1559 ddst = drawlookup(client, BGLONG(a+1), 1);
1561 error(Enodrawimage);
1563 error("cannot change repl/clipr of shared image");
1566 dst->flags |= Frepl;
1567 drawrectangle(&dst->clipr, a+6);
1570 /* draw: 'd' dstid[4] srcid[4] maskid[4] R[4*4] P[2*4] P[2*4] */
1572 printmesg(fmt="LLLRPP", a, 0);
1573 m = 1+4+4+4+4*4+2*4+2*4;
1576 dst = drawimage(client, a+1);
1577 dstid = BGLONG(a+1);
1578 src = drawimage(client, a+5);
1579 mask = drawimage(client, a+9);
1580 drawrectangle(&r, a+13);
1581 drawpoint(&p, a+29);
1582 drawpoint(&q, a+37);
1583 op = drawclientop(client);
1584 memdraw(dst, r, src, p, mask, q, op);
1585 dstflush(dstid, dst, r);
1588 /* toggle debugging: 'D' val[1] */
1590 printmesg(fmt="b", a, 0);
1597 /* ellipse: 'e' dstid[4] srcid[4] center[2*4] a[4] b[4] thick[4] sp[2*4] alpha[4] phi[4]*/
1600 printmesg(fmt="LLPlllPll", a, 0);
1601 m = 1+4+4+2*4+4+4+4+2*4+2*4;
1604 dst = drawimage(client, a+1);
1605 dstid = BGLONG(a+1);
1606 src = drawimage(client, a+5);
1611 error("invalid ellipse semidiameter");
1614 error("negative ellipse thickness");
1615 drawpoint(&sp, a+29);
1621 op = drawclientop(client);
1622 /* high bit indicates arc angles are present */
1624 if((ox & (1<<30)) == 0)
1626 memarc(dst, p, e0, e1, c, src, sp, ox, oy, op);
1628 memellipse(dst, p, e0, e1, c, src, sp, op);
1629 dstflush(dstid, dst, Rect(p.x-e0-j, p.y-e1-j, p.x+e0+j+1, p.y+e1+j+1));
1632 /* free: 'f' id[4] */
1634 printmesg(fmt="L", a, 1);
1638 ll = drawlookup(client, BGLONG(a+1), 0);
1639 if(ll && ll->dscreen && ll->dscreen->owner != client)
1640 ll->dscreen->owner->refreshme = 1;
1641 drawuninstall(client, BGLONG(a+1));
1644 /* free screen: 'F' id[4] */
1646 printmesg(fmt="L", a, 1);
1650 drawlookupscreen(client, BGLONG(a+1), &cs);
1651 drawuninstallscreen(client, cs);
1654 /* initialize font: 'i' fontid[4] nchars[4] ascent[1] */
1656 printmesg(fmt="Llb", a, 1);
1660 dstid = BGLONG(a+1);
1662 error("cannot use display as font");
1663 font = drawlookup(client, dstid, 1);
1665 error(Enodrawimage);
1666 if(font->image->layer)
1667 error("cannot use window as font");
1669 if(ni<=0 || ni>4096)
1670 error("bad font size (4096 chars max)");
1671 free(font->fchar); /* should we complain if non-zero? */
1672 font->fchar = malloc(ni*sizeof(FChar));
1673 if(font->fchar == 0)
1674 error("no memory for font");
1675 memset(font->fchar, 0, ni*sizeof(FChar));
1677 font->ascent = a[9];
1680 /* load character: 'l' fontid[4] srcid[4] index[2] R[4*4] P[2*4] left[1] width[1] */
1682 printmesg(fmt="LLSRPbb", a, 0);
1683 m = 1+4+4+2+4*4+2*4+1+1;
1686 font = drawlookup(client, BGLONG(a+1), 1);
1688 error(Enodrawimage);
1689 if(font->nfchar == 0)
1691 src = drawimage(client, a+5);
1693 if(ci >= font->nfchar)
1695 drawrectangle(&r, a+11);
1696 drawpoint(&p, a+27);
1697 memdraw(font->image, r, src, p, memopaque, p, S);
1698 fc = &font->fchar[ci];
1707 /* draw line: 'L' dstid[4] p0[2*4] p1[2*4] end0[4] end1[4] radius[4] srcid[4] sp[2*4] */
1709 printmesg(fmt="LPPlllLP", a, 0);
1710 m = 1+4+2*4+2*4+4+4+4+4+2*4;
1713 dst = drawimage(client, a+1);
1714 dstid = BGLONG(a+1);
1716 drawpoint(&q, a+13);
1721 error("negative line width");
1722 src = drawimage(client, a+33);
1723 drawpoint(&sp, a+37);
1724 op = drawclientop(client);
1725 memline(dst, p, q, e0, e1, j, src, sp, op);
1726 /* avoid memlinebbox if possible */
1727 if(dstid==0 || dst->layer!=nil){
1728 /* BUG: this is terribly inefficient: update maximal containing rect*/
1729 r = memlinebbox(p, q, e0, e1, j);
1730 dstflush(dstid, dst, insetrect(r, -(1+1+j)));
1734 /* create image mask: 'm' newid[4] id[4] */
1738 printmesg("LL", a, 0);
1746 /* attach to a named image: 'n' dstid[4] j[1] name[j] */
1748 printmesg(fmt="Lz", a, 0);
1753 if(j == 0) /* give me a non-empty name please */
1758 dstid = BGLONG(a+1);
1759 if(drawlookup(client, dstid, 0))
1760 error(Eimageexists);
1761 dn = drawlookupname(j, (char*)a+6);
1764 if(drawinstall(client, dstid, dn->dimage->image, 0) == 0)
1766 di = drawlookup(client, dstid, 0);
1768 error("draw: cannot happen");
1769 di->vers = dn->vers;
1770 di->name = smalloc(j+1);
1771 di->fromname = dn->dimage;
1772 di->fromname->ref++;
1773 memmove(di->name, a+6, j);
1775 client->infoid = dstid;
1778 /* name an image: 'N' dstid[4] in[1] j[1] name[j] */
1780 printmesg(fmt="Lbz", a, 0);
1786 if(j == 0) /* give me a non-empty name please */
1791 di = drawlookup(client, BGLONG(a+1), 0);
1793 error(Enodrawimage);
1797 drawaddname(client, di, j, (char*)a+7);
1799 dn = drawlookupname(j, (char*)a+7);
1802 if(dn->dimage != di)
1808 /* position window: 'o' id[4] r.min [2*4] screenr.min [2*4] */
1810 printmesg(fmt="LPP", a, 0);
1814 dst = drawimage(client, a+1);
1817 drawpoint(&q, a+13);
1818 r = dst->layer->screenr;
1819 ni = memlorigin(dst, p, q);
1821 error("image origin failed");
1824 addflush(dst->layer->screenr);
1825 ll = drawlookup(client, BGLONG(a+1), 1);
1826 drawrefreshscreen(ll, client);
1831 /* set compositing operator for next draw operation: 'O' op */
1833 printmesg(fmt="b", a, 0);
1840 /* filled polygon: 'P' dstid[4] n[2] wind[4] ignore[2*4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1841 /* polygon: 'p' dstid[4] n[2] end0[4] end1[4] radius[4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1844 printmesg(fmt="LslllLPP", a, 0);
1845 m = 1+4+2+4+4+4+4+2*4;
1848 dstid = BGLONG(a+1);
1849 dst = drawimage(client, a+1);
1852 error("negative count in polygon");
1859 error("negative polygon line width");
1861 src = drawimage(client, a+19);
1862 drawpoint(&sp, a+23);
1863 drawpoint(&p, a+31);
1865 pp = malloc(ni*sizeof(Point));
1869 if(dstid==0 || (screenimage && dst->layer && dst->layer->screen->image->data == screenimage->data))
1870 doflush = 1; /* simplify test in loop */
1874 for(y=0; y<ni; y++){
1877 u = drawcoord(u, a+n, ox, &p.x);
1878 u = drawcoord(u, a+n, oy, &p.y);
1885 c = memlineendsize(e0);
1890 c = memlineendsize(e1);
1895 if(*a=='P' && e0!=1 && e0 !=~0)
1898 r = Rect(q.x-oesize, q.y-oesize, q.x+oesize+1, q.y+oesize+1);
1899 combinerect(&r, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1901 if(rectclip(&r, dst->clipr)) /* should perhaps be an arg to dstflush */
1902 dstflush(dstid, dst, r);
1907 dstflush(dstid, dst, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1908 op = drawclientop(client);
1910 mempoly(dst, pp, ni, e0, e1, j, src, sp, op);
1912 memfillpoly(dst, pp, ni, e0, src, sp, op);
1917 /* read: 'r' id[4] R[4*4] */
1919 printmesg(fmt="LR", a, 0);
1923 i = drawimage(client, a+1);
1924 drawrectangle(&r, a+5);
1925 if(!rectinrect(r, i->r))
1926 error(Ereadoutside);
1927 c = bytesperline(r, i->depth);
1929 free(client->readdata);
1930 client->readdata = mallocz(c, 0);
1931 if(client->readdata == nil)
1932 error("readimage malloc failed");
1933 client->nreaddata = memunload(i, r, client->readdata, c);
1934 if(client->nreaddata < 0){
1935 free(client->readdata);
1936 client->readdata = nil;
1937 error("bad readimage call");
1941 /* string: 's' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] ni*(index[2]) */
1942 /* 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]) */
1945 printmesg(fmt="LLLPRPs", a, 0);
1946 m = 1+4+4+4+2*4+4*4+2*4+2;
1952 dst = drawimage(client, a+1);
1953 dstid = BGLONG(a+1);
1954 src = drawimage(client, a+5);
1955 font = drawlookup(client, BGLONG(a+9), 1);
1957 error(Enodrawimage);
1958 if(font->nfchar == 0)
1960 drawpoint(&p, a+13);
1961 drawrectangle(&r, a+21);
1962 drawpoint(&sp, a+37);
1970 op = drawclientop(client);
1973 /* paint background */
1974 bg = drawimage(client, a+47);
1975 drawpoint(&q, a+51);
1977 r.min.y = p.y-font->ascent;
1979 r.max.y = r.min.y+Dy(font->image->r);
1983 if(ci<0 || ci>=font->nfchar){
1987 r.max.x += font->fchar[ci].width;
1990 memdraw(dst, r, bg, q, memopaque, ZP, op);
1996 if(ci<0 || ci>=font->nfchar){
2000 q = drawchar(dst, bg, q, src, &sp, font, ci, op);
2004 p.y -= font->ascent;
2005 dstflush(dstid, dst, Rect(p.x, p.y, q.x, p.y+Dy(font->image->r)));
2008 /* use public screen: 'S' id[4] chan[4] */
2010 printmesg(fmt="Ll", a, 0);
2014 dstid = BGLONG(a+1);
2017 dscrn = drawlookupdscreen(dstid);
2018 if(dscrn==0 || (dscrn->public==0 && dscrn->owner!=client))
2019 error(Enodrawscreen);
2020 if(dscrn->screen->image->chan != BGLONG(a+5))
2021 error("inconsistent chan");
2022 if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0)
2026 /* top or bottom windows: 't' top[1] nw[2] n*id[4] */
2028 printmesg(fmt="bsL", a, 0);
2040 lp = malloc(nw*sizeof(Memimage*));
2048 lp[j] = drawimage(client, a+1+1+2+j*4);
2049 if(lp[0]->layer == 0)
2050 error("images are not windows");
2052 if(lp[j]->layer->screen != lp[0]->layer->screen)
2053 error("images not on same screen");
2055 memltofrontn(lp, nw);
2057 memltorearn(lp, nw);
2058 if(screenimage && lp[0]->layer->screen->image->data == screenimage->data)
2060 addflush(lp[j]->layer->screenr);
2061 ll = drawlookup(client, BGLONG(a+1+1+2), 1);
2062 drawrefreshscreen(ll, client);
2069 printmesg(fmt="", a, 0);
2074 /* write: 'y' id[4] R[4*4] data[x*1] */
2075 /* write from compressed data: 'Y' id[4] R[4*4] data[x*1] */
2078 printmesg(fmt="LR", a, 0);
2079 // iprint("load %c\n", *a);
2083 dstid = BGLONG(a+1);
2084 dst = drawimage(client, a+1);
2085 drawrectangle(&r, a+5);
2086 if(!rectinrect(r, dst->r))
2087 error(Ewriteoutside);
2088 y = memload(dst, r, a+m, n-m, *a=='Y');
2090 error("bad writeimage call");
2091 dstflush(dstid, dst, r);
2121 * On 8 bit displays, load the default color map
2126 int r, g, b, cr, cg, cb, v;
2130 drawactive(1); /* to restore map from backup */
2131 for(r=0,i=0; r!=4; r++)
2132 for(v=0; v!=4; v++,i+=16){
2133 for(g=0,j=v-r; g!=4; g++)
2134 for(b=0;b!=4;b++,j++){
2140 if(den == 0) /* divide check -- pick grey shades */
2141 cr = cg = cb = v*17;
2149 cr*0x01010101, cg*0x01010101, cb*0x01010101);
2155 drawblankscreen(int blank)
2160 if(blank == sdraw.blanked)
2164 if(screenimage == nil){
2169 nc = screenimage->depth > 8 ? 256 : 1<<screenimage->depth;
2172 * blankscreen uses the hardware to blank the screen
2173 * when possible. to help in cases when it is not possible,
2174 * we set the color map to be all black.
2176 if(blank == 0){ /* turn screen on */
2177 for(i=0; i<nc; i++, p+=3)
2178 setcolor(i, p[0], p[1], p[2]);
2180 }else{ /* turn screen off */
2182 for(i=0; i<nc; i++, p+=3){
2183 getcolor(i, &p[0], &p[1], &p[2]);
2184 setcolor(i, 0, 0, 0);
2187 sdraw.blanked = blank;
2192 * record activity on screen, changing blanking as appropriate
2195 drawactive(int active)
2199 sdraw.blanktime = MACHP(0)->ticks;
2201 if(blanktime && sdraw.blanktime && TK2SEC(MACHP(0)->ticks - sdraw.blanktime)/60 >= blanktime)
2209 return TK2SEC(MACHP(0)->ticks - sdraw.blanktime)/60;