Blob


1 /*
2 * /dev/draw simulator -- handles the messages prepared by the draw library.
3 * Doesn't simulate the file system part, just the messages.
4 */
6 #include <u.h>
7 #include <libc.h>
8 #include <draw.h>
9 #include <memdraw.h>
10 #include <memlayer.h>
11 #include "devdraw.h"
13 extern void _flushmemscreen(Rectangle);
14 int forcedpi = 0;
15 int displaydpi = 100;
17 #define NHASH (1<<5)
18 #define HASHMASK (NHASH-1)
20 typedef struct Client Client;
21 typedef struct Draw Draw;
22 typedef struct DImage DImage;
23 typedef struct DScreen DScreen;
24 typedef struct CScreen CScreen;
25 typedef struct FChar FChar;
26 typedef struct Refresh Refresh;
27 typedef struct Refx Refx;
28 typedef struct DName DName;
30 struct Draw
31 {
32 QLock lk;
33 int clientid;
34 int nclient;
35 Client* client[1];
36 int nname;
37 DName* name;
38 int vers;
39 int softscreen;
40 };
42 struct Client
43 {
44 /*Ref r;*/
45 DImage* dimage[NHASH];
46 CScreen* cscreen;
47 Refresh* refresh;
48 Rendez refrend;
49 uchar* readdata;
50 int nreaddata;
51 int busy;
52 int clientid;
53 int slot;
54 int refreshme;
55 int infoid;
56 int op;
57 };
59 struct Refresh
60 {
61 DImage* dimage;
62 Rectangle r;
63 Refresh* next;
64 };
66 struct Refx
67 {
68 Client* client;
69 DImage* dimage;
70 };
72 struct DName
73 {
74 char *name;
75 Client *client;
76 DImage* dimage;
77 int vers;
78 };
80 struct FChar
81 {
82 int minx; /* left edge of bits */
83 int maxx; /* right edge of bits */
84 uchar miny; /* first non-zero scan-line */
85 uchar maxy; /* last non-zero scan-line + 1 */
86 schar left; /* offset of baseline */
87 uchar width; /* width of baseline */
88 };
90 /*
91 * Reference counts in DImages:
92 * one per open by original client
93 * one per screen image or fill
94 * one per image derived from this one by name
95 */
96 struct DImage
97 {
98 int id;
99 int ref;
100 char *name;
101 int vers;
102 Memimage* image;
103 int ascent;
104 int nfchar;
105 FChar* fchar;
106 DScreen* dscreen; /* 0 if not a window */
107 DImage* fromname; /* image this one is derived from, by name */
108 DImage* next;
109 };
111 struct CScreen
113 DScreen* dscreen;
114 CScreen* next;
115 };
117 struct DScreen
119 int id;
120 int public;
121 int ref;
122 DImage *dimage;
123 DImage *dfill;
124 Memscreen* screen;
125 Client* owner;
126 DScreen* next;
127 };
129 static Draw sdraw;
130 static Client *client0;
131 static Memimage *screenimage;
132 static Rectangle flushrect;
133 static int waste;
134 static DScreen* dscreen;
135 static int drawuninstall(Client*, int);
136 static Memimage* drawinstall(Client*, int, Memimage*, DScreen*);
137 static void drawfreedimage(DImage*);
139 void
140 _initdisplaymemimage(Memimage *m)
142 screenimage = m;
143 m->screenref = 1;
144 client0 = mallocz(sizeof(Client), 1);
145 if(client0 == nil){
146 fprint(2, "initdraw: allocating client0: out of memory");
147 abort();
149 client0->slot = 0;
150 client0->clientid = ++sdraw.clientid;
151 client0->op = SoverD;
152 sdraw.client[0] = client0;
153 sdraw.nclient = 1;
154 sdraw.softscreen = 1;
157 void
158 _drawreplacescreenimage(Memimage *m)
160 /*
161 * Replace the screen image because the screen
162 * was resized.
164 * In theory there should only be one reference
165 * to the current screen image, and that's through
166 * client0's image 0, installed a few lines above.
167 * Once the client drops the image, the underlying backing
168 * store freed properly. The client is being notified
169 * about the resize through external means, so all we
170 * need to do is this assignment.
171 */
172 Memimage *om;
174 qlock(&sdraw.lk);
175 om = screenimage;
176 screenimage = m;
177 m->screenref = 1;
178 if(om && --om->screenref == 0){
179 _freememimage(om);
181 qunlock(&sdraw.lk);
184 static
185 void
186 drawrefreshscreen(DImage *l, Client *client)
188 while(l != nil && l->dscreen == nil)
189 l = l->fromname;
190 if(l != nil && l->dscreen->owner != client)
191 l->dscreen->owner->refreshme = 1;
194 static
195 void
196 drawrefresh(Memimage *m, Rectangle r, void *v)
198 Refx *x;
199 DImage *d;
200 Client *c;
201 Refresh *ref;
203 USED(m);
205 if(v == 0)
206 return;
207 x = v;
208 c = x->client;
209 d = x->dimage;
210 for(ref=c->refresh; ref; ref=ref->next)
211 if(ref->dimage == d){
212 combinerect(&ref->r, r);
213 return;
215 ref = mallocz(sizeof(Refresh), 1);
216 if(ref){
217 ref->dimage = d;
218 ref->r = r;
219 ref->next = c->refresh;
220 c->refresh = ref;
224 static void
225 addflush(Rectangle r)
227 int abb, ar, anbb;
228 Rectangle nbb;
230 if(sdraw.softscreen==0 || !rectclip(&r, screenimage->r))
231 return;
233 if(flushrect.min.x >= flushrect.max.x){
234 flushrect = r;
235 waste = 0;
236 return;
238 nbb = flushrect;
239 combinerect(&nbb, r);
240 ar = Dx(r)*Dy(r);
241 abb = Dx(flushrect)*Dy(flushrect);
242 anbb = Dx(nbb)*Dy(nbb);
243 /*
244 * Area of new waste is area of new bb minus area of old bb,
245 * less the area of the new segment, which we assume is not waste.
246 * This could be negative, but that's OK.
247 */
248 waste += anbb-abb - ar;
249 if(waste < 0)
250 waste = 0;
251 /*
252 * absorb if:
253 * total area is small
254 * waste is less than half total area
255 * rectangles touch
256 */
257 if(anbb<=1024 || waste*2<anbb || rectXrect(flushrect, r)){
258 flushrect = nbb;
259 return;
261 /* emit current state */
262 if(flushrect.min.x < flushrect.max.x)
263 _flushmemscreen(flushrect);
264 flushrect = r;
265 waste = 0;
268 static
269 void
270 dstflush(int dstid, Memimage *dst, Rectangle r)
272 Memlayer *l;
274 if(dstid == 0){
275 combinerect(&flushrect, r);
276 return;
278 /* how can this happen? -rsc, dec 12 2002 */
279 if(dst == 0){
280 fprint(2, "nil dstflush\n");
281 return;
283 l = dst->layer;
284 if(l == nil)
285 return;
286 do{
287 if(l->screen->image->data != screenimage->data)
288 return;
289 r = rectaddpt(r, l->delta);
290 l = l->screen->image->layer;
291 }while(l);
292 addflush(r);
295 static
296 void
297 drawflush(void)
299 if(flushrect.min.x < flushrect.max.x)
300 _flushmemscreen(flushrect);
301 flushrect = Rect(10000, 10000, -10000, -10000);
304 static
305 int
306 drawcmp(char *a, char *b, int n)
308 if(strlen(a) != n)
309 return 1;
310 return memcmp(a, b, n);
313 static
314 DName*
315 drawlookupname(int n, char *str)
317 DName *name, *ename;
319 name = sdraw.name;
320 ename = &name[sdraw.nname];
321 for(; name<ename; name++)
322 if(drawcmp(name->name, str, n) == 0)
323 return name;
324 return 0;
327 static
328 int
329 drawgoodname(DImage *d)
331 DName *n;
333 /* if window, validate the screen's own images */
334 if(d->dscreen)
335 if(drawgoodname(d->dscreen->dimage) == 0
336 || drawgoodname(d->dscreen->dfill) == 0)
337 return 0;
338 if(d->name == nil)
339 return 1;
340 n = drawlookupname(strlen(d->name), d->name);
341 if(n==nil || n->vers!=d->vers)
342 return 0;
343 return 1;
346 static
347 DImage*
348 drawlookup(Client *client, int id, int checkname)
350 DImage *d;
352 d = client->dimage[id&HASHMASK];
353 while(d){
354 if(d->id == id){
355 /*
356 * BUG: should error out but too hard.
357 * Return 0 instead.
358 */
359 if(checkname && !drawgoodname(d))
360 return 0;
361 return d;
363 d = d->next;
365 return 0;
368 static
369 DScreen*
370 drawlookupdscreen(int id)
372 DScreen *s;
374 s = dscreen;
375 while(s){
376 if(s->id == id)
377 return s;
378 s = s->next;
380 return 0;
383 static
384 DScreen*
385 drawlookupscreen(Client *client, int id, CScreen **cs)
387 CScreen *s;
389 s = client->cscreen;
390 while(s){
391 if(s->dscreen->id == id){
392 *cs = s;
393 return s->dscreen;
395 s = s->next;
397 /* caller must check! */
398 return 0;
401 static
402 Memimage*
403 drawinstall(Client *client, int id, Memimage *i, DScreen *dscreen)
405 DImage *d;
407 d = mallocz(sizeof(DImage), 1);
408 if(d == 0)
409 return 0;
410 d->id = id;
411 d->ref = 1;
412 d->name = 0;
413 d->vers = 0;
414 d->image = i;
415 if(i->screenref)
416 ++i->screenref;
417 d->nfchar = 0;
418 d->fchar = 0;
419 d->fromname = 0;
420 d->dscreen = dscreen;
421 d->next = client->dimage[id&HASHMASK];
422 client->dimage[id&HASHMASK] = d;
423 return i;
426 static
427 Memscreen*
428 drawinstallscreen(Client *client, DScreen *d, int id, DImage *dimage, DImage *dfill, int public)
430 Memscreen *s;
431 CScreen *c;
433 c = mallocz(sizeof(CScreen), 1);
434 if(dimage && dimage->image && dimage->image->chan == 0){
435 fprint(2, "bad image %p in drawinstallscreen", dimage->image);
436 abort();
439 if(c == 0)
440 return 0;
441 if(d == 0){
442 d = mallocz(sizeof(DScreen), 1);
443 if(d == 0){
444 free(c);
445 return 0;
447 s = mallocz(sizeof(Memscreen), 1);
448 if(s == 0){
449 free(c);
450 free(d);
451 return 0;
453 s->frontmost = 0;
454 s->rearmost = 0;
455 d->dimage = dimage;
456 if(dimage){
457 s->image = dimage->image;
458 dimage->ref++;
460 d->dfill = dfill;
461 if(dfill){
462 s->fill = dfill->image;
463 dfill->ref++;
465 d->ref = 0;
466 d->id = id;
467 d->screen = s;
468 d->public = public;
469 d->next = dscreen;
470 d->owner = client;
471 dscreen = d;
473 c->dscreen = d;
474 d->ref++;
475 c->next = client->cscreen;
476 client->cscreen = c;
477 return d->screen;
480 static
481 void
482 drawdelname(DName *name)
484 int i;
486 i = name-sdraw.name;
487 memmove(name, name+1, (sdraw.nname-(i+1))*sizeof(DName));
488 sdraw.nname--;
491 static
492 void
493 drawfreedscreen(DScreen *this)
495 DScreen *ds, *next;
497 this->ref--;
498 if(this->ref < 0)
499 fprint(2, "negative ref in drawfreedscreen\n");
500 if(this->ref > 0)
501 return;
502 ds = dscreen;
503 if(ds == this){
504 dscreen = this->next;
505 goto Found;
507 while(next = ds->next){ /* assign = */
508 if(next == this){
509 ds->next = this->next;
510 goto Found;
512 ds = next;
514 /*
515 * Should signal Enodrawimage, but too hard.
516 */
517 return;
519 Found:
520 if(this->dimage)
521 drawfreedimage(this->dimage);
522 if(this->dfill)
523 drawfreedimage(this->dfill);
524 free(this->screen);
525 free(this);
528 static
529 void
530 drawfreedimage(DImage *dimage)
532 int i;
533 Memimage *l;
534 DScreen *ds;
536 dimage->ref--;
537 if(dimage->ref < 0)
538 fprint(2, "negative ref in drawfreedimage\n");
539 if(dimage->ref > 0)
540 return;
542 /* any names? */
543 for(i=0; i<sdraw.nname; )
544 if(sdraw.name[i].dimage == dimage)
545 drawdelname(sdraw.name+i);
546 else
547 i++;
548 if(dimage->fromname){ /* acquired by name; owned by someone else*/
549 drawfreedimage(dimage->fromname);
550 goto Return;
552 ds = dimage->dscreen;
553 l = dimage->image;
554 dimage->dscreen = nil; /* paranoia */
555 dimage->image = nil;
556 if(ds){
557 if(l->data == screenimage->data)
558 addflush(l->layer->screenr);
559 if(l->layer->refreshfn == drawrefresh) /* else true owner will clean up */
560 free(l->layer->refreshptr);
561 l->layer->refreshptr = nil;
562 if(drawgoodname(dimage))
563 memldelete(l);
564 else
565 memlfree(l);
566 drawfreedscreen(ds);
567 }else{
568 if(l->screenref==0)
569 freememimage(l);
570 else if(--l->screenref==0)
571 _freememimage(l);
573 Return:
574 free(dimage->fchar);
575 free(dimage);
578 static
579 void
580 drawuninstallscreen(Client *client, CScreen *this)
582 CScreen *cs, *next;
584 cs = client->cscreen;
585 if(cs == this){
586 client->cscreen = this->next;
587 drawfreedscreen(this->dscreen);
588 free(this);
589 return;
591 while(next = cs->next){ /* assign = */
592 if(next == this){
593 cs->next = this->next;
594 drawfreedscreen(this->dscreen);
595 free(this);
596 return;
598 cs = next;
602 static
603 int
604 drawuninstall(Client *client, int id)
606 DImage *d, **l;
608 for(l=&client->dimage[id&HASHMASK]; (d=*l) != nil; l=&d->next){
609 if(d->id == id){
610 *l = d->next;
611 drawfreedimage(d);
612 return 0;
615 return -1;
618 static
619 int
620 drawaddname(Client *client, DImage *di, int n, char *str, char **err)
622 DName *name, *ename, *new, *t;
623 char *ns;
625 name = sdraw.name;
626 ename = &name[sdraw.nname];
627 for(; name<ename; name++)
628 if(drawcmp(name->name, str, n) == 0){
629 *err = "image name in use";
630 return -1;
632 t = mallocz((sdraw.nname+1)*sizeof(DName), 1);
633 ns = malloc(n+1);
634 if(t == nil || ns == nil){
635 free(t);
636 free(ns);
637 *err = "out of memory";
638 return -1;
640 memmove(t, sdraw.name, sdraw.nname*sizeof(DName));
641 free(sdraw.name);
642 sdraw.name = t;
643 new = &sdraw.name[sdraw.nname++];
644 new->name = ns;
645 memmove(new->name, str, n);
646 new->name[n] = 0;
647 new->dimage = di;
648 new->client = client;
649 new->vers = ++sdraw.vers;
650 return 0;
653 static int
654 drawclientop(Client *cl)
656 int op;
658 op = cl->op;
659 cl->op = SoverD;
660 return op;
663 static
664 Memimage*
665 drawimage(Client *client, uchar *a)
667 DImage *d;
669 d = drawlookup(client, BGLONG(a), 1);
670 if(d == nil)
671 return nil; /* caller must check! */
672 return d->image;
675 static
676 void
677 drawrectangle(Rectangle *r, uchar *a)
679 r->min.x = BGLONG(a+0*4);
680 r->min.y = BGLONG(a+1*4);
681 r->max.x = BGLONG(a+2*4);
682 r->max.y = BGLONG(a+3*4);
685 static
686 void
687 drawpoint(Point *p, uchar *a)
689 p->x = BGLONG(a+0*4);
690 p->y = BGLONG(a+1*4);
693 static
694 Point
695 drawchar(Memimage *dst, Point p, Memimage *src, Point *sp, DImage *font, int index, int op)
697 FChar *fc;
698 Rectangle r;
699 Point sp1;
701 fc = &font->fchar[index];
702 r.min.x = p.x+fc->left;
703 r.min.y = p.y-(font->ascent-fc->miny);
704 r.max.x = r.min.x+(fc->maxx-fc->minx);
705 r.max.y = r.min.y+(fc->maxy-fc->miny);
706 sp1.x = sp->x+fc->left;
707 sp1.y = sp->y+fc->miny;
708 memdraw(dst, r, src, sp1, font->image, Pt(fc->minx, fc->miny), op);
709 p.x += fc->width;
710 sp->x += fc->width;
711 return p;
714 static
715 uchar*
716 drawcoord(uchar *p, uchar *maxp, int oldx, int *newx)
718 int b, x;
720 if(p >= maxp)
721 return nil;
722 b = *p++;
723 x = b & 0x7F;
724 if(b & 0x80){
725 if(p+1 >= maxp)
726 return nil;
727 x |= *p++ << 7;
728 x |= *p++ << 15;
729 if(x & (1<<22))
730 x |= ~0U<<23;
731 }else{
732 if(b & 0x40)
733 x |= ~0U<<7;
734 x += oldx;
736 *newx = x;
737 return p;
740 int
741 _drawmsgread(void *a, int n)
743 Client *cl;
745 qlock(&sdraw.lk);
746 cl = client0;
747 if(cl->readdata == nil){
748 werrstr("no draw data");
749 goto err;
751 if(n < cl->nreaddata){
752 werrstr("short read");
753 goto err;
755 n = cl->nreaddata;
756 memmove(a, cl->readdata, cl->nreaddata);
757 free(cl->readdata);
758 cl->readdata = nil;
759 qunlock(&sdraw.lk);
760 return n;
762 err:
763 qunlock(&sdraw.lk);
764 return -1;
767 int
768 _drawmsgwrite(void *v, int n)
770 char cbuf[40], *err, ibuf[12*12+1], *s;
771 int c, ci, doflush, dstid, e0, e1, esize, j, m;
772 int ni, nw, oesize, oldn, op, ox, oy, repl, scrnid, y;
773 uchar *a, refresh, *u;
774 u32int chan, value;
775 Client *client;
776 CScreen *cs;
777 DImage *di, *ddst, *dsrc, *font, *ll;
778 DName *dn;
779 DScreen *dscrn;
780 FChar *fc;
781 Fmt fmt;
782 Memimage *dst, *i, *l, **lp, *mask, *src;
783 Memscreen *scrn;
784 Point p, *pp, q, sp;
785 Rectangle clipr, r;
786 Refreshfn reffn;
787 Refx *refx;
789 qlock(&sdraw.lk);
790 a = v;
791 m = 0;
792 oldn = n;
793 client = client0;
795 while((n-=m) > 0){
796 a += m;
797 /*fprint(2, "msgwrite %d(%d)...", n, *a); */
798 switch(*a){
799 default:
800 /*fprint(2, "bad command %d\n", *a); */
801 err = "bad draw command";
802 goto error;
804 /* allocate: 'b' id[4] screenid[4] refresh[1] chan[4] repl[1]
805 R[4*4] clipR[4*4] rrggbbaa[4]
806 */
807 case 'b':
808 m = 1+4+4+1+4+1+4*4+4*4+4;
809 if(n < m)
810 goto Eshortdraw;
811 dstid = BGLONG(a+1);
812 scrnid = BGSHORT(a+5);
813 refresh = a[9];
814 chan = BGLONG(a+10);
815 repl = a[14];
816 drawrectangle(&r, a+15);
817 drawrectangle(&clipr, a+31);
818 value = BGLONG(a+47);
819 if(drawlookup(client, dstid, 0))
820 goto Eimageexists;
821 if(scrnid){
822 dscrn = drawlookupscreen(client, scrnid, &cs);
823 if(!dscrn)
824 goto Enodrawscreen;
825 scrn = dscrn->screen;
826 if(repl || chan!=scrn->image->chan){
827 err = "image parameters incompatibile with screen";
828 goto error;
830 reffn = 0;
831 switch(refresh){
832 case Refbackup:
833 break;
834 case Refnone:
835 reffn = memlnorefresh;
836 break;
837 case Refmesg:
838 reffn = drawrefresh;
839 break;
840 default:
841 err = "unknown refresh method";
842 goto error;
844 l = memlalloc(scrn, r, reffn, 0, value);
845 if(l == 0)
846 goto Edrawmem;
847 addflush(l->layer->screenr);
848 l->clipr = clipr;
849 rectclip(&l->clipr, r);
850 if(drawinstall(client, dstid, l, dscrn) == 0){
851 memldelete(l);
852 goto Edrawmem;
854 dscrn->ref++;
855 if(reffn){
856 refx = nil;
857 if(reffn == drawrefresh){
858 refx = mallocz(sizeof(Refx), 1);
859 if(refx == 0){
860 if(drawuninstall(client, dstid) < 0)
861 goto Enodrawimage;
862 goto Edrawmem;
864 refx->client = client;
865 refx->dimage = drawlookup(client, dstid, 1);
867 memlsetrefresh(l, reffn, refx);
869 continue;
871 i = allocmemimage(r, chan);
872 if(i == 0)
873 goto Edrawmem;
874 if(repl)
875 i->flags |= Frepl;
876 i->clipr = clipr;
877 if(!repl)
878 rectclip(&i->clipr, r);
879 if(drawinstall(client, dstid, i, 0) == 0){
880 freememimage(i);
881 goto Edrawmem;
883 memfillcolor(i, value);
884 continue;
886 /* allocate screen: 'A' id[4] imageid[4] fillid[4] public[1] */
887 case 'A':
888 m = 1+4+4+4+1;
889 if(n < m)
890 goto Eshortdraw;
891 dstid = BGLONG(a+1);
892 if(dstid == 0)
893 goto Ebadarg;
894 if(drawlookupdscreen(dstid))
895 goto Escreenexists;
896 ddst = drawlookup(client, BGLONG(a+5), 1);
897 dsrc = drawlookup(client, BGLONG(a+9), 1);
898 if(ddst==0 || dsrc==0)
899 goto Enodrawimage;
900 if(drawinstallscreen(client, 0, dstid, ddst, dsrc, a[13]) == 0)
901 goto Edrawmem;
902 continue;
904 /* set repl and clip: 'c' dstid[4] repl[1] clipR[4*4] */
905 case 'c':
906 m = 1+4+1+4*4;
907 if(n < m)
908 goto Eshortdraw;
909 ddst = drawlookup(client, BGLONG(a+1), 1);
910 if(ddst == nil)
911 goto Enodrawimage;
912 if(ddst->name){
913 err = "can't change repl/clipr of shared image";
914 goto error;
916 dst = ddst->image;
917 if(a[5])
918 dst->flags |= Frepl;
919 drawrectangle(&dst->clipr, a+6);
920 continue;
922 /* draw: 'd' dstid[4] srcid[4] maskid[4] R[4*4] P[2*4] P[2*4] */
923 case 'd':
924 m = 1+4+4+4+4*4+2*4+2*4;
925 if(n < m)
926 goto Eshortdraw;
927 dst = drawimage(client, a+1);
928 dstid = BGLONG(a+1);
929 src = drawimage(client, a+5);
930 mask = drawimage(client, a+9);
931 if(!dst || !src || !mask)
932 goto Enodrawimage;
933 drawrectangle(&r, a+13);
934 drawpoint(&p, a+29);
935 drawpoint(&q, a+37);
936 op = drawclientop(client);
937 memdraw(dst, r, src, p, mask, q, op);
938 dstflush(dstid, dst, r);
939 continue;
941 /* toggle debugging: 'D' val[1] */
942 case 'D':
943 m = 1+1;
944 if(n < m)
945 goto Eshortdraw;
946 drawdebug = a[1];
947 continue;
949 /* ellipse: 'e' dstid[4] srcid[4] center[2*4] a[4] b[4] thick[4] sp[2*4] alpha[4] phi[4]*/
950 case 'e':
951 case 'E':
952 m = 1+4+4+2*4+4+4+4+2*4+2*4;
953 if(n < m)
954 goto Eshortdraw;
955 dst = drawimage(client, a+1);
956 dstid = BGLONG(a+1);
957 src = drawimage(client, a+5);
958 if(!dst || !src)
959 goto Enodrawimage;
960 drawpoint(&p, a+9);
961 e0 = BGLONG(a+17);
962 e1 = BGLONG(a+21);
963 if(e0<0 || e1<0){
964 err = "invalid ellipse semidiameter";
965 goto error;
967 j = BGLONG(a+25);
968 if(j < 0){
969 err = "negative ellipse thickness";
970 goto error;
973 drawpoint(&sp, a+29);
974 c = j;
975 if(*a == 'E')
976 c = -1;
977 ox = BGLONG(a+37);
978 oy = BGLONG(a+41);
979 op = drawclientop(client);
980 /* high bit indicates arc angles are present */
981 if(ox & ((ulong)1<<31)){
982 if((ox & ((ulong)1<<30)) == 0)
983 ox &= ~((ulong)1<<31);
984 memarc(dst, p, e0, e1, c, src, sp, ox, oy, op);
985 }else
986 memellipse(dst, p, e0, e1, c, src, sp, op);
987 dstflush(dstid, dst, Rect(p.x-e0-j, p.y-e1-j, p.x+e0+j+1, p.y+e1+j+1));
988 continue;
990 /* free: 'f' id[4] */
991 case 'f':
992 m = 1+4;
993 if(n < m)
994 goto Eshortdraw;
995 ll = drawlookup(client, BGLONG(a+1), 0);
996 if(ll && ll->dscreen && ll->dscreen->owner != client)
997 ll->dscreen->owner->refreshme = 1;
998 if(drawuninstall(client, BGLONG(a+1)) < 0)
999 goto Enodrawimage;
1000 continue;
1002 /* free screen: 'F' id[4] */
1003 case 'F':
1004 m = 1+4;
1005 if(n < m)
1006 goto Eshortdraw;
1007 if(!drawlookupscreen(client, BGLONG(a+1), &cs))
1008 goto Enodrawscreen;
1009 drawuninstallscreen(client, cs);
1010 continue;
1012 /* initialize font: 'i' fontid[4] nchars[4] ascent[1] */
1013 case 'i':
1014 m = 1+4+4+1;
1015 if(n < m)
1016 goto Eshortdraw;
1017 dstid = BGLONG(a+1);
1018 if(dstid == 0){
1019 err = "can't use display as font";
1020 goto error;
1022 font = drawlookup(client, dstid, 1);
1023 if(font == 0)
1024 goto Enodrawimage;
1025 if(font->image->layer){
1026 err = "can't use window as font";
1027 goto error;
1029 ni = BGLONG(a+5);
1030 if(ni<=0 || ni>4096){
1031 err = "bad font size (4096 chars max)";
1032 goto error;
1034 free(font->fchar); /* should we complain if non-zero? */
1035 font->fchar = mallocz(ni*sizeof(FChar), 1);
1036 if(font->fchar == 0){
1037 err = "no memory for font";
1038 goto error;
1040 memset(font->fchar, 0, ni*sizeof(FChar));
1041 font->nfchar = ni;
1042 font->ascent = a[9];
1043 continue;
1045 /* set image 0 to screen image */
1046 case 'J':
1047 m = 1;
1048 if(n < m)
1049 goto Eshortdraw;
1050 if(drawlookup(client, 0, 0))
1051 goto Eimageexists;
1052 drawinstall(client, 0, screenimage, 0);
1053 client->infoid = 0;
1054 continue;
1056 /* get image info: 'I' */
1057 case 'I':
1058 m = 1;
1059 if(n < m)
1060 goto Eshortdraw;
1061 if(client->infoid < 0)
1062 goto Enodrawimage;
1063 if(client->infoid == 0){
1064 i = screenimage;
1065 if(i == nil)
1066 goto Enodrawimage;
1067 }else{
1068 di = drawlookup(client, client->infoid, 1);
1069 if(di == nil)
1070 goto Enodrawimage;
1071 i = di->image;
1073 ni = sprint(ibuf, "%11d %11d %11s %11d %11d %11d %11d %11d"
1074 " %11d %11d %11d %11d ",
1075 client->clientid,
1076 client->infoid,
1077 chantostr(cbuf, i->chan),
1078 (i->flags&Frepl)==Frepl,
1079 i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y,
1080 i->clipr.min.x, i->clipr.min.y,
1081 i->clipr.max.x, i->clipr.max.y);
1082 free(client->readdata);
1083 client->readdata = malloc(ni);
1084 if(client->readdata == nil)
1085 goto Enomem;
1086 memmove(client->readdata, ibuf, ni);
1087 client->nreaddata = ni;
1088 client->infoid = -1;
1089 continue;
1091 /* query: 'Q' n[1] queryspec[n] */
1092 case 'q':
1093 if(n < 2)
1094 goto Eshortdraw;
1095 m = 1+1+a[1];
1096 if(n < m)
1097 goto Eshortdraw;
1098 fmtstrinit(&fmt);
1099 for(c=0; c<a[1]; c++) {
1100 switch(a[2+c]) {
1101 default:
1102 err = "unknown query";
1103 goto error;
1104 case 'd': /* dpi */
1105 if(forcedpi)
1106 fmtprint(&fmt, "%11d ", forcedpi);
1107 else
1108 fmtprint(&fmt, "%11d ", displaydpi);
1109 break;
1112 client->readdata = (uchar*)fmtstrflush(&fmt);
1113 if(client->readdata == nil)
1114 goto Enomem;
1115 client->nreaddata = strlen((char*)client->readdata);
1116 continue;
1118 /* load character: 'l' fontid[4] srcid[4] index[2] R[4*4] P[2*4] left[1] width[1] */
1119 case 'l':
1120 m = 1+4+4+2+4*4+2*4+1+1;
1121 if(n < m)
1122 goto Eshortdraw;
1123 font = drawlookup(client, BGLONG(a+1), 1);
1124 if(font == 0)
1125 goto Enodrawimage;
1126 if(font->nfchar == 0)
1127 goto Enotfont;
1128 src = drawimage(client, a+5);
1129 if(!src)
1130 goto Enodrawimage;
1131 ci = BGSHORT(a+9);
1132 if(ci >= font->nfchar)
1133 goto Eindex;
1134 drawrectangle(&r, a+11);
1135 drawpoint(&p, a+27);
1136 memdraw(font->image, r, src, p, memopaque, p, S);
1137 fc = &font->fchar[ci];
1138 fc->minx = r.min.x;
1139 fc->maxx = r.max.x;
1140 fc->miny = r.min.y;
1141 fc->maxy = r.max.y;
1142 fc->left = a[35];
1143 fc->width = a[36];
1144 continue;
1146 /* draw line: 'L' dstid[4] p0[2*4] p1[2*4] end0[4] end1[4] radius[4] srcid[4] sp[2*4] */
1147 case 'L':
1148 m = 1+4+2*4+2*4+4+4+4+4+2*4;
1149 if(n < m)
1150 goto Eshortdraw;
1151 dst = drawimage(client, a+1);
1152 dstid = BGLONG(a+1);
1153 drawpoint(&p, a+5);
1154 drawpoint(&q, a+13);
1155 e0 = BGLONG(a+21);
1156 e1 = BGLONG(a+25);
1157 j = BGLONG(a+29);
1158 if(j < 0){
1159 err = "negative line width";
1160 goto error;
1162 src = drawimage(client, a+33);
1163 if(!dst || !src)
1164 goto Enodrawimage;
1165 drawpoint(&sp, a+37);
1166 op = drawclientop(client);
1167 memline(dst, p, q, e0, e1, j, src, sp, op);
1168 /* avoid memlinebbox if possible */
1169 if(dstid==0 || dst->layer!=nil){
1170 /* BUG: this is terribly inefficient: update maximal containing rect*/
1171 r = memlinebbox(p, q, e0, e1, j);
1172 dstflush(dstid, dst, insetrect(r, -(1+1+j)));
1174 continue;
1176 /* create image mask: 'm' newid[4] id[4] */
1179 case 'm':
1180 m = 4+4;
1181 if(n < m)
1182 goto Eshortdraw;
1183 break;
1187 /* attach to a named image: 'n' dstid[4] j[1] name[j] */
1188 case 'n':
1189 m = 1+4+1;
1190 if(n < m)
1191 goto Eshortdraw;
1192 j = a[5];
1193 if(j == 0) /* give me a non-empty name please */
1194 goto Eshortdraw;
1195 m += j;
1196 if(n < m)
1197 goto Eshortdraw;
1198 dstid = BGLONG(a+1);
1199 if(drawlookup(client, dstid, 0))
1200 goto Eimageexists;
1201 dn = drawlookupname(j, (char*)a+6);
1202 if(dn == nil)
1203 goto Enoname;
1204 s = malloc(j+1);
1205 if(s == nil)
1206 goto Enomem;
1207 if(drawinstall(client, dstid, dn->dimage->image, 0) == 0)
1208 goto Edrawmem;
1209 di = drawlookup(client, dstid, 0);
1210 if(di == 0)
1211 goto Eoldname;
1212 di->vers = dn->vers;
1213 di->name = s;
1214 di->fromname = dn->dimage;
1215 di->fromname->ref++;
1216 memmove(di->name, a+6, j);
1217 di->name[j] = 0;
1218 client->infoid = dstid;
1219 continue;
1221 /* name an image: 'N' dstid[4] in[1] j[1] name[j] */
1222 case 'N':
1223 m = 1+4+1+1;
1224 if(n < m)
1225 goto Eshortdraw;
1226 c = a[5];
1227 j = a[6];
1228 if(j == 0) /* give me a non-empty name please */
1229 goto Eshortdraw;
1230 m += j;
1231 if(n < m)
1232 goto Eshortdraw;
1233 di = drawlookup(client, BGLONG(a+1), 0);
1234 if(di == 0)
1235 goto Enodrawimage;
1236 if(di->name)
1237 goto Enamed;
1238 if(c)
1239 if(drawaddname(client, di, j, (char*)a+7, &err) < 0)
1240 goto error;
1241 else{
1242 dn = drawlookupname(j, (char*)a+7);
1243 if(dn == nil)
1244 goto Enoname;
1245 if(dn->dimage != di)
1246 goto Ewrongname;
1247 drawdelname(dn);
1249 continue;
1251 /* position window: 'o' id[4] r.min [2*4] screenr.min [2*4] */
1252 case 'o':
1253 m = 1+4+2*4+2*4;
1254 if(n < m)
1255 goto Eshortdraw;
1256 dst = drawimage(client, a+1);
1257 if(!dst)
1258 goto Enodrawimage;
1259 if(dst->layer){
1260 drawpoint(&p, a+5);
1261 drawpoint(&q, a+13);
1262 r = dst->layer->screenr;
1263 ni = memlorigin(dst, p, q);
1264 if(ni < 0){
1265 err = "image origin failed";
1266 goto error;
1268 if(ni > 0){
1269 addflush(r);
1270 addflush(dst->layer->screenr);
1271 ll = drawlookup(client, BGLONG(a+1), 1);
1272 drawrefreshscreen(ll, client);
1275 continue;
1277 /* set compositing operator for next draw operation: 'O' op */
1278 case 'O':
1279 m = 1+1;
1280 if(n < m)
1281 goto Eshortdraw;
1282 client->op = a[1];
1283 continue;
1285 /* filled polygon: 'P' dstid[4] n[2] wind[4] ignore[2*4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1286 /* polygon: 'p' dstid[4] n[2] end0[4] end1[4] radius[4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1287 case 'p':
1288 case 'P':
1289 m = 1+4+2+4+4+4+4+2*4;
1290 if(n < m)
1291 goto Eshortdraw;
1292 dstid = BGLONG(a+1);
1293 dst = drawimage(client, a+1);
1294 ni = BGSHORT(a+5);
1295 if(ni < 0){
1296 err = "negative cout in polygon";
1297 goto error;
1299 e0 = BGLONG(a+7);
1300 e1 = BGLONG(a+11);
1301 j = 0;
1302 if(*a == 'p'){
1303 j = BGLONG(a+15);
1304 if(j < 0){
1305 err = "negative polygon line width";
1306 goto error;
1309 src = drawimage(client, a+19);
1310 if(!dst || !src)
1311 goto Enodrawimage;
1312 drawpoint(&sp, a+23);
1313 drawpoint(&p, a+31);
1314 ni++;
1315 pp = mallocz(ni*sizeof(Point), 1);
1316 if(pp == nil)
1317 goto Enomem;
1318 doflush = 0;
1319 if(dstid==0 || (dst->layer && dst->layer->screen->image->data == screenimage->data))
1320 doflush = 1; /* simplify test in loop */
1321 ox = oy = 0;
1322 esize = 0;
1323 u = a+m;
1324 for(y=0; y<ni; y++){
1325 q = p;
1326 oesize = esize;
1327 u = drawcoord(u, a+n, ox, &p.x);
1328 if(!u)
1329 goto Eshortdraw;
1330 u = drawcoord(u, a+n, oy, &p.y);
1331 if(!u)
1332 goto Eshortdraw;
1333 ox = p.x;
1334 oy = p.y;
1335 if(doflush){
1336 esize = j;
1337 if(*a == 'p'){
1338 if(y == 0){
1339 c = memlineendsize(e0);
1340 if(c > esize)
1341 esize = c;
1343 if(y == ni-1){
1344 c = memlineendsize(e1);
1345 if(c > esize)
1346 esize = c;
1349 if(*a=='P' && e0!=1 && e0 !=~0)
1350 r = dst->clipr;
1351 else if(y > 0){
1352 r = Rect(q.x-oesize, q.y-oesize, q.x+oesize+1, q.y+oesize+1);
1353 combinerect(&r, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1355 if(rectclip(&r, dst->clipr)) /* should perhaps be an arg to dstflush */
1356 dstflush(dstid, dst, r);
1358 pp[y] = p;
1360 if(y == 1)
1361 dstflush(dstid, dst, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1362 op = drawclientop(client);
1363 if(*a == 'p')
1364 mempoly(dst, pp, ni, e0, e1, j, src, sp, op);
1365 else
1366 memfillpoly(dst, pp, ni, e0, src, sp, op);
1367 free(pp);
1368 m = u-a;
1369 continue;
1371 /* read: 'r' id[4] R[4*4] */
1372 case 'r':
1373 m = 1+4+4*4;
1374 if(n < m)
1375 goto Eshortdraw;
1376 i = drawimage(client, a+1);
1377 if(!i)
1378 goto Enodrawimage;
1379 drawrectangle(&r, a+5);
1380 if(!rectinrect(r, i->r))
1381 goto Ereadoutside;
1382 c = bytesperline(r, i->depth);
1383 c *= Dy(r);
1384 free(client->readdata);
1385 client->readdata = mallocz(c, 0);
1386 if(client->readdata == nil){
1387 err = "readimage malloc failed";
1388 goto error;
1390 client->nreaddata = memunload(i, r, client->readdata, c);
1391 if(client->nreaddata < 0){
1392 free(client->readdata);
1393 client->readdata = nil;
1394 err = "bad readimage call";
1395 goto error;
1397 continue;
1399 /* string: 's' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] ni*(index[2]) */
1400 /* 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]) */
1401 case 's':
1402 case 'x':
1403 m = 1+4+4+4+2*4+4*4+2*4+2;
1404 if(*a == 'x')
1405 m += 4+2*4;
1406 if(n < m)
1407 goto Eshortdraw;
1409 dst = drawimage(client, a+1);
1410 dstid = BGLONG(a+1);
1411 src = drawimage(client, a+5);
1412 if(!dst || !src)
1413 goto Enodrawimage;
1414 font = drawlookup(client, BGLONG(a+9), 1);
1415 if(font == 0)
1416 goto Enodrawimage;
1417 if(font->nfchar == 0)
1418 goto Enotfont;
1419 drawpoint(&p, a+13);
1420 drawrectangle(&r, a+21);
1421 drawpoint(&sp, a+37);
1422 ni = BGSHORT(a+45);
1423 u = a+m;
1424 m += ni*2;
1425 if(n < m)
1426 goto Eshortdraw;
1427 clipr = dst->clipr;
1428 dst->clipr = r;
1429 op = drawclientop(client);
1430 if(*a == 'x'){
1431 /* paint background */
1432 l = drawimage(client, a+47);
1433 if(!l)
1434 goto Enodrawimage;
1435 drawpoint(&q, a+51);
1436 r.min.x = p.x;
1437 r.min.y = p.y-font->ascent;
1438 r.max.x = p.x;
1439 r.max.y = r.min.y+Dy(font->image->r);
1440 j = ni;
1441 while(--j >= 0){
1442 ci = BGSHORT(u);
1443 if(ci<0 || ci>=font->nfchar){
1444 dst->clipr = clipr;
1445 goto Eindex;
1447 r.max.x += font->fchar[ci].width;
1448 u += 2;
1450 memdraw(dst, r, l, q, memopaque, ZP, op);
1451 u -= 2*ni;
1453 q = p;
1454 while(--ni >= 0){
1455 ci = BGSHORT(u);
1456 if(ci<0 || ci>=font->nfchar){
1457 dst->clipr = clipr;
1458 goto Eindex;
1460 q = drawchar(dst, q, src, &sp, font, ci, op);
1461 u += 2;
1463 dst->clipr = clipr;
1464 p.y -= font->ascent;
1465 dstflush(dstid, dst, Rect(p.x, p.y, q.x, p.y+Dy(font->image->r)));
1466 continue;
1468 /* use public screen: 'S' id[4] chan[4] */
1469 case 'S':
1470 m = 1+4+4;
1471 if(n < m)
1472 goto Eshortdraw;
1473 dstid = BGLONG(a+1);
1474 if(dstid == 0)
1475 goto Ebadarg;
1476 dscrn = drawlookupdscreen(dstid);
1477 if(dscrn==0 || (dscrn->public==0 && dscrn->owner!=client))
1478 goto Enodrawscreen;
1479 if(dscrn->screen->image->chan != BGLONG(a+5)){
1480 err = "inconsistent chan";
1481 goto error;
1483 if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0)
1484 goto Edrawmem;
1485 continue;
1487 /* top or bottom windows: 't' top[1] nw[2] n*id[4] */
1488 case 't':
1489 m = 1+1+2;
1490 if(n < m)
1491 goto Eshortdraw;
1492 nw = BGSHORT(a+2);
1493 if(nw < 0)
1494 goto Ebadarg;
1495 if(nw == 0)
1496 continue;
1497 m += nw*4;
1498 if(n < m)
1499 goto Eshortdraw;
1500 lp = mallocz(nw*sizeof(Memimage*), 1);
1501 if(lp == 0)
1502 goto Enomem;
1503 for(j=0; j<nw; j++){
1504 lp[j] = drawimage(client, a+1+1+2+j*4);
1505 if(lp[j] == nil){
1506 free(lp);
1507 goto Enodrawimage;
1510 if(lp[0]->layer == 0){
1511 err = "images are not windows";
1512 free(lp);
1513 goto error;
1515 for(j=1; j<nw; j++)
1516 if(lp[j]->layer->screen != lp[0]->layer->screen){
1517 err = "images not on same screen";
1518 free(lp);
1519 goto error;
1521 if(a[1])
1522 memltofrontn(lp, nw);
1523 else
1524 memltorearn(lp, nw);
1525 if(lp[0]->layer->screen->image->data == screenimage->data)
1526 for(j=0; j<nw; j++)
1527 addflush(lp[j]->layer->screenr);
1528 free(lp);
1529 ll = drawlookup(client, BGLONG(a+1+1+2), 1);
1530 drawrefreshscreen(ll, client);
1531 continue;
1533 /* visible: 'v' */
1534 case 'v':
1535 m = 1;
1536 drawflush();
1537 continue;
1539 /* write: 'y' id[4] R[4*4] data[x*1] */
1540 /* write from compressed data: 'Y' id[4] R[4*4] data[x*1] */
1541 case 'y':
1542 case 'Y':
1543 m = 1+4+4*4;
1544 if(n < m)
1545 goto Eshortdraw;
1546 dstid = BGLONG(a+1);
1547 dst = drawimage(client, a+1);
1548 if(!dst)
1549 goto Enodrawimage;
1550 drawrectangle(&r, a+5);
1551 if(!rectinrect(r, dst->r))
1552 goto Ewriteoutside;
1553 y = memload(dst, r, a+m, n-m, *a=='Y');
1554 if(y < 0){
1555 err = "bad writeimage call";
1556 goto error;
1558 dstflush(dstid, dst, r);
1559 m += y;
1560 continue;
1563 qunlock(&sdraw.lk);
1564 return oldn - n;
1566 Enodrawimage:
1567 err = "unknown id for draw image";
1568 goto error;
1569 Enodrawscreen:
1570 err = "unknown id for draw screen";
1571 goto error;
1572 Eshortdraw:
1573 err = "short draw message";
1574 goto error;
1576 Eshortread:
1577 err = "draw read too short";
1578 goto error;
1580 Eimageexists:
1581 err = "image id in use";
1582 goto error;
1583 Escreenexists:
1584 err = "screen id in use";
1585 goto error;
1586 Edrawmem:
1587 err = "image memory allocation failed";
1588 goto error;
1589 Ereadoutside:
1590 err = "readimage outside image";
1591 goto error;
1592 Ewriteoutside:
1593 err = "writeimage outside image";
1594 goto error;
1595 Enotfont:
1596 err = "image not a font";
1597 goto error;
1598 Eindex:
1599 err = "character index out of range";
1600 goto error;
1602 Enoclient:
1603 err = "no such draw client";
1604 goto error;
1605 Edepth:
1606 err = "image has bad depth";
1607 goto error;
1608 Enameused:
1609 err = "image name in use";
1610 goto error;
1612 Enoname:
1613 err = "no image with that name";
1614 goto error;
1615 Eoldname:
1616 err = "named image no longer valid";
1617 goto error;
1618 Enamed:
1619 err = "image already has name";
1620 goto error;
1621 Ewrongname:
1622 err = "wrong name for image";
1623 goto error;
1624 Enomem:
1625 err = "out of memory";
1626 goto error;
1627 Ebadarg:
1628 err = "bad argument in draw message";
1629 goto error;
1631 error:
1632 werrstr("%s", err);
1633 qunlock(&sdraw.lk);
1634 return -1;