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);
15 #define NHASH (1<<5)
16 #define HASHMASK (NHASH-1)
18 typedef struct Client Client;
19 typedef struct Draw Draw;
20 typedef struct DImage DImage;
21 typedef struct DScreen DScreen;
22 typedef struct CScreen CScreen;
23 typedef struct FChar FChar;
24 typedef struct Refresh Refresh;
25 typedef struct Refx Refx;
26 typedef struct DName DName;
28 struct Draw
29 {
30 QLock lk;
31 int clientid;
32 int nclient;
33 Client* client[1];
34 int nname;
35 DName* name;
36 int vers;
37 int softscreen;
38 };
40 struct Client
41 {
42 /*Ref r;*/
43 DImage* dimage[NHASH];
44 CScreen* cscreen;
45 Refresh* refresh;
46 Rendez refrend;
47 uchar* readdata;
48 int nreaddata;
49 int busy;
50 int clientid;
51 int slot;
52 int refreshme;
53 int infoid;
54 int op;
55 };
57 struct Refresh
58 {
59 DImage* dimage;
60 Rectangle r;
61 Refresh* next;
62 };
64 struct Refx
65 {
66 Client* client;
67 DImage* dimage;
68 };
70 struct DName
71 {
72 char *name;
73 Client *client;
74 DImage* dimage;
75 int vers;
76 };
78 struct FChar
79 {
80 int minx; /* left edge of bits */
81 int maxx; /* right edge of bits */
82 uchar miny; /* first non-zero scan-line */
83 uchar maxy; /* last non-zero scan-line + 1 */
84 schar left; /* offset of baseline */
85 uchar width; /* width of baseline */
86 };
88 /*
89 * Reference counts in DImages:
90 * one per open by original client
91 * one per screen image or fill
92 * one per image derived from this one by name
93 */
94 struct DImage
95 {
96 int id;
97 int ref;
98 char *name;
99 int vers;
100 Memimage* image;
101 int ascent;
102 int nfchar;
103 FChar* fchar;
104 DScreen* dscreen; /* 0 if not a window */
105 DImage* fromname; /* image this one is derived from, by name */
106 DImage* next;
107 };
109 struct CScreen
111 DScreen* dscreen;
112 CScreen* next;
113 };
115 struct DScreen
117 int id;
118 int public;
119 int ref;
120 DImage *dimage;
121 DImage *dfill;
122 Memscreen* screen;
123 Client* owner;
124 DScreen* next;
125 };
127 static Draw sdraw;
128 static Client *client0;
129 static Memimage *screenimage;
130 static Rectangle flushrect;
131 static int waste;
132 static DScreen* dscreen;
133 static int drawuninstall(Client*, int);
134 static Memimage* drawinstall(Client*, int, Memimage*, DScreen*);
135 static void drawfreedimage(DImage*);
137 void
138 _initdisplaymemimage(Memimage *m)
140 screenimage = m;
141 m->screenref = 1;
142 client0 = mallocz(sizeof(Client), 1);
143 if(client0 == nil){
144 fprint(2, "initdraw: allocating client0: out of memory");
145 abort();
147 client0->slot = 0;
148 client0->clientid = ++sdraw.clientid;
149 client0->op = SoverD;
150 sdraw.client[0] = client0;
151 sdraw.nclient = 1;
152 sdraw.softscreen = 1;
155 void
156 _drawreplacescreenimage(Memimage *m)
158 /*
159 * Replace the screen image because the screen
160 * was resized.
162 * In theory there should only be one reference
163 * to the current screen image, and that's through
164 * client0's image 0, installed a few lines above.
165 * Once the client drops the image, the underlying backing
166 * store freed properly. The client is being notified
167 * about the resize through external means, so all we
168 * need to do is this assignment.
169 */
170 Memimage *om;
172 qlock(&sdraw.lk);
173 om = screenimage;
174 screenimage = m;
175 m->screenref = 1;
176 if(om && --om->screenref == 0){
177 _freememimage(om);
179 qunlock(&sdraw.lk);
182 static
183 void
184 drawrefreshscreen(DImage *l, Client *client)
186 while(l != nil && l->dscreen == nil)
187 l = l->fromname;
188 if(l != nil && l->dscreen->owner != client)
189 l->dscreen->owner->refreshme = 1;
192 static
193 void
194 drawrefresh(Memimage *m, Rectangle r, void *v)
196 Refx *x;
197 DImage *d;
198 Client *c;
199 Refresh *ref;
201 USED(m);
203 if(v == 0)
204 return;
205 x = v;
206 c = x->client;
207 d = x->dimage;
208 for(ref=c->refresh; ref; ref=ref->next)
209 if(ref->dimage == d){
210 combinerect(&ref->r, r);
211 return;
213 ref = mallocz(sizeof(Refresh), 1);
214 if(ref){
215 ref->dimage = d;
216 ref->r = r;
217 ref->next = c->refresh;
218 c->refresh = ref;
222 static void
223 addflush(Rectangle r)
225 int abb, ar, anbb;
226 Rectangle nbb;
228 if(sdraw.softscreen==0 || !rectclip(&r, screenimage->r))
229 return;
231 if(flushrect.min.x >= flushrect.max.x){
232 flushrect = r;
233 waste = 0;
234 return;
236 nbb = flushrect;
237 combinerect(&nbb, r);
238 ar = Dx(r)*Dy(r);
239 abb = Dx(flushrect)*Dy(flushrect);
240 anbb = Dx(nbb)*Dy(nbb);
241 /*
242 * Area of new waste is area of new bb minus area of old bb,
243 * less the area of the new segment, which we assume is not waste.
244 * This could be negative, but that's OK.
245 */
246 waste += anbb-abb - ar;
247 if(waste < 0)
248 waste = 0;
249 /*
250 * absorb if:
251 * total area is small
252 * waste is less than half total area
253 * rectangles touch
254 */
255 if(anbb<=1024 || waste*2<anbb || rectXrect(flushrect, r)){
256 flushrect = nbb;
257 return;
259 /* emit current state */
260 if(flushrect.min.x < flushrect.max.x)
261 _flushmemscreen(flushrect);
262 flushrect = r;
263 waste = 0;
266 static
267 void
268 dstflush(int dstid, Memimage *dst, Rectangle r)
270 Memlayer *l;
272 if(dstid == 0){
273 combinerect(&flushrect, r);
274 return;
276 /* how can this happen? -rsc, dec 12 2002 */
277 if(dst == 0){
278 fprint(2, "nil dstflush\n");
279 return;
281 l = dst->layer;
282 if(l == nil)
283 return;
284 do{
285 if(l->screen->image->data != screenimage->data)
286 return;
287 r = rectaddpt(r, l->delta);
288 l = l->screen->image->layer;
289 }while(l);
290 addflush(r);
293 static
294 void
295 drawflush(void)
297 _flushmemscreen(flushrect);
298 flushrect = Rect(10000, 10000, -10000, -10000);
301 void
302 xdrawflush(void)
304 qlock(&sdraw.lk);
305 drawflush();
306 qunlock(&sdraw.lk);
309 static
310 int
311 drawcmp(char *a, char *b, int n)
313 if(strlen(a) != n)
314 return 1;
315 return memcmp(a, b, n);
318 static
319 DName*
320 drawlookupname(int n, char *str)
322 DName *name, *ename;
324 name = sdraw.name;
325 ename = &name[sdraw.nname];
326 for(; name<ename; name++)
327 if(drawcmp(name->name, str, n) == 0)
328 return name;
329 return 0;
332 static
333 int
334 drawgoodname(DImage *d)
336 DName *n;
338 /* if window, validate the screen's own images */
339 if(d->dscreen)
340 if(drawgoodname(d->dscreen->dimage) == 0
341 || drawgoodname(d->dscreen->dfill) == 0)
342 return 0;
343 if(d->name == nil)
344 return 1;
345 n = drawlookupname(strlen(d->name), d->name);
346 if(n==nil || n->vers!=d->vers)
347 return 0;
348 return 1;
351 static
352 DImage*
353 drawlookup(Client *client, int id, int checkname)
355 DImage *d;
357 d = client->dimage[id&HASHMASK];
358 while(d){
359 if(d->id == id){
360 /*
361 * BUG: should error out but too hard.
362 * Return 0 instead.
363 */
364 if(checkname && !drawgoodname(d))
365 return 0;
366 return d;
368 d = d->next;
370 return 0;
373 static
374 DScreen*
375 drawlookupdscreen(int id)
377 DScreen *s;
379 s = dscreen;
380 while(s){
381 if(s->id == id)
382 return s;
383 s = s->next;
385 return 0;
388 static
389 DScreen*
390 drawlookupscreen(Client *client, int id, CScreen **cs)
392 CScreen *s;
394 s = client->cscreen;
395 while(s){
396 if(s->dscreen->id == id){
397 *cs = s;
398 return s->dscreen;
400 s = s->next;
402 /* caller must check! */
403 return 0;
406 static
407 Memimage*
408 drawinstall(Client *client, int id, Memimage *i, DScreen *dscreen)
410 DImage *d;
412 d = mallocz(sizeof(DImage), 1);
413 if(d == 0)
414 return 0;
415 d->id = id;
416 d->ref = 1;
417 d->name = 0;
418 d->vers = 0;
419 d->image = i;
420 if(i->screenref)
421 ++i->screenref;
422 d->nfchar = 0;
423 d->fchar = 0;
424 d->fromname = 0;
425 d->dscreen = dscreen;
426 d->next = client->dimage[id&HASHMASK];
427 client->dimage[id&HASHMASK] = d;
428 return i;
431 static
432 Memscreen*
433 drawinstallscreen(Client *client, DScreen *d, int id, DImage *dimage, DImage *dfill, int public)
435 Memscreen *s;
436 CScreen *c;
438 c = mallocz(sizeof(CScreen), 1);
439 if(dimage && dimage->image && dimage->image->chan == 0){
440 fprint(2, "bad image %p in drawinstallscreen", dimage->image);
441 abort();
444 if(c == 0)
445 return 0;
446 if(d == 0){
447 d = mallocz(sizeof(DScreen), 1);
448 if(d == 0){
449 free(c);
450 return 0;
452 s = mallocz(sizeof(Memscreen), 1);
453 if(s == 0){
454 free(c);
455 free(d);
456 return 0;
458 s->frontmost = 0;
459 s->rearmost = 0;
460 d->dimage = dimage;
461 if(dimage){
462 s->image = dimage->image;
463 dimage->ref++;
465 d->dfill = dfill;
466 if(dfill){
467 s->fill = dfill->image;
468 dfill->ref++;
470 d->ref = 0;
471 d->id = id;
472 d->screen = s;
473 d->public = public;
474 d->next = dscreen;
475 d->owner = client;
476 dscreen = d;
478 c->dscreen = d;
479 d->ref++;
480 c->next = client->cscreen;
481 client->cscreen = c;
482 return d->screen;
485 static
486 void
487 drawdelname(DName *name)
489 int i;
491 i = name-sdraw.name;
492 memmove(name, name+1, (sdraw.nname-(i+1))*sizeof(DName));
493 sdraw.nname--;
496 static
497 void
498 drawfreedscreen(DScreen *this)
500 DScreen *ds, *next;
502 this->ref--;
503 if(this->ref < 0)
504 fprint(2, "negative ref in drawfreedscreen\n");
505 if(this->ref > 0)
506 return;
507 ds = dscreen;
508 if(ds == this){
509 dscreen = this->next;
510 goto Found;
512 while(next = ds->next){ /* assign = */
513 if(next == this){
514 ds->next = this->next;
515 goto Found;
517 ds = next;
519 /*
520 * Should signal Enodrawimage, but too hard.
521 */
522 return;
524 Found:
525 if(this->dimage)
526 drawfreedimage(this->dimage);
527 if(this->dfill)
528 drawfreedimage(this->dfill);
529 free(this->screen);
530 free(this);
533 static
534 void
535 drawfreedimage(DImage *dimage)
537 int i;
538 Memimage *l;
539 DScreen *ds;
541 dimage->ref--;
542 if(dimage->ref < 0)
543 fprint(2, "negative ref in drawfreedimage\n");
544 if(dimage->ref > 0)
545 return;
547 /* any names? */
548 for(i=0; i<sdraw.nname; )
549 if(sdraw.name[i].dimage == dimage)
550 drawdelname(sdraw.name+i);
551 else
552 i++;
553 if(dimage->fromname){ /* acquired by name; owned by someone else*/
554 drawfreedimage(dimage->fromname);
555 goto Return;
557 ds = dimage->dscreen;
558 l = dimage->image;
559 dimage->dscreen = nil; /* paranoia */
560 dimage->image = nil;
561 if(ds){
562 if(l->data == screenimage->data)
563 addflush(l->layer->screenr);
564 if(l->layer->refreshfn == drawrefresh) /* else true owner will clean up */
565 free(l->layer->refreshptr);
566 l->layer->refreshptr = nil;
567 if(drawgoodname(dimage))
568 memldelete(l);
569 else
570 memlfree(l);
571 drawfreedscreen(ds);
572 }else{
573 if(l->screenref==0)
574 freememimage(l);
575 else if(--l->screenref==0)
576 _freememimage(l);
578 Return:
579 free(dimage->fchar);
580 free(dimage);
583 static
584 void
585 drawuninstallscreen(Client *client, CScreen *this)
587 CScreen *cs, *next;
589 cs = client->cscreen;
590 if(cs == this){
591 client->cscreen = this->next;
592 drawfreedscreen(this->dscreen);
593 free(this);
594 return;
596 while(next = cs->next){ /* assign = */
597 if(next == this){
598 cs->next = this->next;
599 drawfreedscreen(this->dscreen);
600 free(this);
601 return;
603 cs = next;
607 static
608 int
609 drawuninstall(Client *client, int id)
611 DImage *d, **l;
613 for(l=&client->dimage[id&HASHMASK]; (d=*l) != nil; l=&d->next){
614 if(d->id == id){
615 *l = d->next;
616 drawfreedimage(d);
617 return 0;
620 return -1;
623 static
624 int
625 drawaddname(Client *client, DImage *di, int n, char *str, char **err)
627 DName *name, *ename, *new, *t;
628 char *ns;
630 name = sdraw.name;
631 ename = &name[sdraw.nname];
632 for(; name<ename; name++)
633 if(drawcmp(name->name, str, n) == 0){
634 *err = "image name in use";
635 return -1;
637 t = mallocz((sdraw.nname+1)*sizeof(DName), 1);
638 ns = malloc(n+1);
639 if(t == nil || ns == nil){
640 free(t);
641 free(ns);
642 *err = "out of memory";
643 return -1;
645 memmove(t, sdraw.name, sdraw.nname*sizeof(DName));
646 free(sdraw.name);
647 sdraw.name = t;
648 new = &sdraw.name[sdraw.nname++];
649 new->name = ns;
650 memmove(new->name, str, n);
651 new->name[n] = 0;
652 new->dimage = di;
653 new->client = client;
654 new->vers = ++sdraw.vers;
655 return 0;
658 static int
659 drawclientop(Client *cl)
661 int op;
663 op = cl->op;
664 cl->op = SoverD;
665 return op;
668 static
669 Memimage*
670 drawimage(Client *client, uchar *a)
672 DImage *d;
674 d = drawlookup(client, BGLONG(a), 1);
675 if(d == nil)
676 return nil; /* caller must check! */
677 return d->image;
680 static
681 void
682 drawrectangle(Rectangle *r, uchar *a)
684 r->min.x = BGLONG(a+0*4);
685 r->min.y = BGLONG(a+1*4);
686 r->max.x = BGLONG(a+2*4);
687 r->max.y = BGLONG(a+3*4);
690 static
691 void
692 drawpoint(Point *p, uchar *a)
694 p->x = BGLONG(a+0*4);
695 p->y = BGLONG(a+1*4);
698 static
699 Point
700 drawchar(Memimage *dst, Point p, Memimage *src, Point *sp, DImage *font, int index, int op)
702 FChar *fc;
703 Rectangle r;
704 Point sp1;
706 fc = &font->fchar[index];
707 r.min.x = p.x+fc->left;
708 r.min.y = p.y-(font->ascent-fc->miny);
709 r.max.x = r.min.x+(fc->maxx-fc->minx);
710 r.max.y = r.min.y+(fc->maxy-fc->miny);
711 sp1.x = sp->x+fc->left;
712 sp1.y = sp->y+fc->miny;
713 memdraw(dst, r, src, sp1, font->image, Pt(fc->minx, fc->miny), op);
714 p.x += fc->width;
715 sp->x += fc->width;
716 return p;
719 static
720 uchar*
721 drawcoord(uchar *p, uchar *maxp, int oldx, int *newx)
723 int b, x;
725 if(p >= maxp)
726 return nil;
727 b = *p++;
728 x = b & 0x7F;
729 if(b & 0x80){
730 if(p+1 >= maxp)
731 return nil;
732 x |= *p++ << 7;
733 x |= *p++ << 15;
734 if(x & (1<<22))
735 x |= ~0<<23;
736 }else{
737 if(b & 0x40)
738 x |= ~0<<7;
739 x += oldx;
741 *newx = x;
742 return p;
745 int
746 _drawmsgread(void *a, int n)
748 Client *cl;
750 qlock(&sdraw.lk);
751 cl = client0;
752 if(cl->readdata == nil){
753 werrstr("no draw data");
754 goto err;
756 if(n < cl->nreaddata){
757 werrstr("short read");
758 goto err;
760 n = cl->nreaddata;
761 memmove(a, cl->readdata, cl->nreaddata);
762 free(cl->readdata);
763 cl->readdata = nil;
764 qunlock(&sdraw.lk);
765 return n;
767 err:
768 qunlock(&sdraw.lk);
769 return -1;
772 int
773 _drawmsgwrite(void *v, int n)
775 char cbuf[40], *err, ibuf[12*12+1], *s;
776 int c, ci, doflush, dstid, e0, e1, esize, j, m;
777 int ni, nw, oesize, oldn, op, ox, oy, repl, scrnid, y;
778 uchar *a, refresh, *u;
779 u32int chan, value;
780 Client *client;
781 CScreen *cs;
782 DImage *di, *ddst, *dsrc, *font, *ll;
783 DName *dn;
784 DScreen *dscrn;
785 FChar *fc;
786 Memimage *dst, *i, *l, **lp, *mask, *src;
787 Memscreen *scrn;
788 Point p, *pp, q, sp;
789 Rectangle clipr, r;
790 Refreshfn reffn;
791 Refx *refx;
793 qlock(&sdraw.lk);
794 a = v;
795 m = 0;
796 oldn = n;
797 client = client0;
799 while((n-=m) > 0){
800 a += m;
801 /* print("msgwrite %d(%c)...", n, *a); */
802 switch(*a){
803 default:
804 /*fprint(2, "bad command %d\n", *a); */
805 err = "bad draw command";
806 goto error;
808 /* allocate: 'b' id[4] screenid[4] refresh[1] chan[4] repl[1]
809 R[4*4] clipR[4*4] rrggbbaa[4]
810 */
811 case 'b':
812 m = 1+4+4+1+4+1+4*4+4*4+4;
813 if(n < m)
814 goto Eshortdraw;
815 dstid = BGLONG(a+1);
816 scrnid = BGSHORT(a+5);
817 refresh = a[9];
818 chan = BGLONG(a+10);
819 repl = a[14];
820 drawrectangle(&r, a+15);
821 drawrectangle(&clipr, a+31);
822 value = BGLONG(a+47);
823 if(drawlookup(client, dstid, 0))
824 goto Eimageexists;
825 if(scrnid){
826 dscrn = drawlookupscreen(client, scrnid, &cs);
827 if(!dscrn)
828 goto Enodrawscreen;
829 scrn = dscrn->screen;
830 if(repl || chan!=scrn->image->chan){
831 err = "image parameters incompatibile with screen";
832 goto error;
834 reffn = 0;
835 switch(refresh){
836 case Refbackup:
837 break;
838 case Refnone:
839 reffn = memlnorefresh;
840 break;
841 case Refmesg:
842 reffn = drawrefresh;
843 break;
844 default:
845 err = "unknown refresh method";
846 goto error;
848 l = memlalloc(scrn, r, reffn, 0, value);
849 if(l == 0)
850 goto Edrawmem;
851 addflush(l->layer->screenr);
852 l->clipr = clipr;
853 rectclip(&l->clipr, r);
854 if(drawinstall(client, dstid, l, dscrn) == 0){
855 memldelete(l);
856 goto Edrawmem;
858 dscrn->ref++;
859 if(reffn){
860 refx = nil;
861 if(reffn == drawrefresh){
862 refx = mallocz(sizeof(Refx), 1);
863 if(refx == 0){
864 if(drawuninstall(client, dstid) < 0)
865 goto Enodrawimage;
866 goto Edrawmem;
868 refx->client = client;
869 refx->dimage = drawlookup(client, dstid, 1);
871 memlsetrefresh(l, reffn, refx);
873 continue;
875 i = allocmemimage(r, chan);
876 if(i == 0)
877 goto Edrawmem;
878 if(repl)
879 i->flags |= Frepl;
880 i->clipr = clipr;
881 if(!repl)
882 rectclip(&i->clipr, r);
883 if(drawinstall(client, dstid, i, 0) == 0){
884 freememimage(i);
885 goto Edrawmem;
887 memfillcolor(i, value);
888 continue;
890 /* allocate screen: 'A' id[4] imageid[4] fillid[4] public[1] */
891 case 'A':
892 m = 1+4+4+4+1;
893 if(n < m)
894 goto Eshortdraw;
895 dstid = BGLONG(a+1);
896 if(dstid == 0)
897 goto Ebadarg;
898 if(drawlookupdscreen(dstid))
899 goto Escreenexists;
900 ddst = drawlookup(client, BGLONG(a+5), 1);
901 dsrc = drawlookup(client, BGLONG(a+9), 1);
902 if(ddst==0 || dsrc==0)
903 goto Enodrawimage;
904 if(drawinstallscreen(client, 0, dstid, ddst, dsrc, a[13]) == 0)
905 goto Edrawmem;
906 continue;
908 /* set repl and clip: 'c' dstid[4] repl[1] clipR[4*4] */
909 case 'c':
910 m = 1+4+1+4*4;
911 if(n < m)
912 goto Eshortdraw;
913 ddst = drawlookup(client, BGLONG(a+1), 1);
914 if(ddst == nil)
915 goto Enodrawimage;
916 if(ddst->name){
917 err = "can't change repl/clipr of shared image";
918 goto error;
920 dst = ddst->image;
921 if(a[5])
922 dst->flags |= Frepl;
923 drawrectangle(&dst->clipr, a+6);
924 continue;
926 /* draw: 'd' dstid[4] srcid[4] maskid[4] R[4*4] P[2*4] P[2*4] */
927 case 'd':
928 m = 1+4+4+4+4*4+2*4+2*4;
929 if(n < m)
930 goto Eshortdraw;
931 dst = drawimage(client, a+1);
932 dstid = BGLONG(a+1);
933 src = drawimage(client, a+5);
934 mask = drawimage(client, a+9);
935 if(!dst || !src || !mask)
936 goto Enodrawimage;
937 drawrectangle(&r, a+13);
938 drawpoint(&p, a+29);
939 drawpoint(&q, a+37);
940 op = drawclientop(client);
941 memdraw(dst, r, src, p, mask, q, op);
942 dstflush(dstid, dst, r);
943 continue;
945 /* toggle debugging: 'D' val[1] */
946 case 'D':
947 m = 1+1;
948 if(n < m)
949 goto Eshortdraw;
950 drawdebug = a[1];
951 continue;
953 /* ellipse: 'e' dstid[4] srcid[4] center[2*4] a[4] b[4] thick[4] sp[2*4] alpha[4] phi[4]*/
954 case 'e':
955 case 'E':
956 m = 1+4+4+2*4+4+4+4+2*4+2*4;
957 if(n < m)
958 goto Eshortdraw;
959 dst = drawimage(client, a+1);
960 dstid = BGLONG(a+1);
961 src = drawimage(client, a+5);
962 if(!dst || !src)
963 goto Enodrawimage;
964 drawpoint(&p, a+9);
965 e0 = BGLONG(a+17);
966 e1 = BGLONG(a+21);
967 if(e0<0 || e1<0){
968 err = "invalid ellipse semidiameter";
969 goto error;
971 j = BGLONG(a+25);
972 if(j < 0){
973 err = "negative ellipse thickness";
974 goto error;
977 drawpoint(&sp, a+29);
978 c = j;
979 if(*a == 'E')
980 c = -1;
981 ox = BGLONG(a+37);
982 oy = BGLONG(a+41);
983 op = drawclientop(client);
984 /* high bit indicates arc angles are present */
985 if(ox & ((ulong)1<<31)){
986 if((ox & ((ulong)1<<30)) == 0)
987 ox &= ~((ulong)1<<31);
988 memarc(dst, p, e0, e1, c, src, sp, ox, oy, op);
989 }else
990 memellipse(dst, p, e0, e1, c, src, sp, op);
991 dstflush(dstid, dst, Rect(p.x-e0-j, p.y-e1-j, p.x+e0+j+1, p.y+e1+j+1));
992 continue;
994 /* free: 'f' id[4] */
995 case 'f':
996 m = 1+4;
997 if(n < m)
998 goto Eshortdraw;
999 ll = drawlookup(client, BGLONG(a+1), 0);
1000 if(ll && ll->dscreen && ll->dscreen->owner != client)
1001 ll->dscreen->owner->refreshme = 1;
1002 if(drawuninstall(client, BGLONG(a+1)) < 0)
1003 goto Enodrawimage;
1004 continue;
1006 /* free screen: 'F' id[4] */
1007 case 'F':
1008 m = 1+4;
1009 if(n < m)
1010 goto Eshortdraw;
1011 if(!drawlookupscreen(client, BGLONG(a+1), &cs))
1012 goto Enodrawscreen;
1013 drawuninstallscreen(client, cs);
1014 continue;
1016 /* initialize font: 'i' fontid[4] nchars[4] ascent[1] */
1017 case 'i':
1018 m = 1+4+4+1;
1019 if(n < m)
1020 goto Eshortdraw;
1021 dstid = BGLONG(a+1);
1022 if(dstid == 0){
1023 err = "can't use display as font";
1024 goto error;
1026 font = drawlookup(client, dstid, 1);
1027 if(font == 0)
1028 goto Enodrawimage;
1029 if(font->image->layer){
1030 err = "can't use window as font";
1031 goto error;
1033 ni = BGLONG(a+5);
1034 if(ni<=0 || ni>4096){
1035 err = "bad font size (4096 chars max)";
1036 goto error;
1038 free(font->fchar); /* should we complain if non-zero? */
1039 font->fchar = mallocz(ni*sizeof(FChar), 1);
1040 if(font->fchar == 0){
1041 err = "no memory for font";
1042 goto error;
1044 memset(font->fchar, 0, ni*sizeof(FChar));
1045 font->nfchar = ni;
1046 font->ascent = a[9];
1047 continue;
1049 /* set image 0 to screen image */
1050 case 'J':
1051 m = 1;
1052 if(n < m)
1053 goto Eshortdraw;
1054 if(drawlookup(client, 0, 0))
1055 goto Eimageexists;
1056 drawinstall(client, 0, screenimage, 0);
1057 client->infoid = 0;
1058 continue;
1060 /* get image info: 'I' */
1061 case 'I':
1062 m = 1;
1063 if(n < m)
1064 goto Eshortdraw;
1065 if(client->infoid < 0)
1066 goto Enodrawimage;
1067 if(client->infoid == 0){
1068 i = screenimage;
1069 if(i == nil)
1070 goto Enodrawimage;
1071 }else{
1072 di = drawlookup(client, client->infoid, 1);
1073 if(di == nil)
1074 goto Enodrawimage;
1075 i = di->image;
1077 ni = sprint(ibuf, "%11d %11d %11s %11d %11d %11d %11d %11d"
1078 " %11d %11d %11d %11d ",
1079 client->clientid,
1080 client->infoid,
1081 chantostr(cbuf, i->chan),
1082 (i->flags&Frepl)==Frepl,
1083 i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y,
1084 i->clipr.min.x, i->clipr.min.y,
1085 i->clipr.max.x, i->clipr.max.y);
1086 free(client->readdata);
1087 client->readdata = malloc(ni);
1088 if(client->readdata == nil)
1089 goto Enomem;
1090 memmove(client->readdata, ibuf, ni);
1091 client->nreaddata = ni;
1092 client->infoid = -1;
1093 continue;
1095 /* load character: 'l' fontid[4] srcid[4] index[2] R[4*4] P[2*4] left[1] width[1] */
1096 case 'l':
1097 m = 1+4+4+2+4*4+2*4+1+1;
1098 if(n < m)
1099 goto Eshortdraw;
1100 font = drawlookup(client, BGLONG(a+1), 1);
1101 if(font == 0)
1102 goto Enodrawimage;
1103 if(font->nfchar == 0)
1104 goto Enotfont;
1105 src = drawimage(client, a+5);
1106 if(!src)
1107 goto Enodrawimage;
1108 ci = BGSHORT(a+9);
1109 if(ci >= font->nfchar)
1110 goto Eindex;
1111 drawrectangle(&r, a+11);
1112 drawpoint(&p, a+27);
1113 memdraw(font->image, r, src, p, memopaque, p, S);
1114 fc = &font->fchar[ci];
1115 fc->minx = r.min.x;
1116 fc->maxx = r.max.x;
1117 fc->miny = r.min.y;
1118 fc->maxy = r.max.y;
1119 fc->left = a[35];
1120 fc->width = a[36];
1121 continue;
1123 /* draw line: 'L' dstid[4] p0[2*4] p1[2*4] end0[4] end1[4] radius[4] srcid[4] sp[2*4] */
1124 case 'L':
1125 m = 1+4+2*4+2*4+4+4+4+4+2*4;
1126 if(n < m)
1127 goto Eshortdraw;
1128 dst = drawimage(client, a+1);
1129 dstid = BGLONG(a+1);
1130 drawpoint(&p, a+5);
1131 drawpoint(&q, a+13);
1132 e0 = BGLONG(a+21);
1133 e1 = BGLONG(a+25);
1134 j = BGLONG(a+29);
1135 if(j < 0){
1136 err = "negative line width";
1137 goto error;
1139 src = drawimage(client, a+33);
1140 if(!dst || !src)
1141 goto Enodrawimage;
1142 drawpoint(&sp, a+37);
1143 op = drawclientop(client);
1144 memline(dst, p, q, e0, e1, j, src, sp, op);
1145 /* avoid memlinebbox if possible */
1146 if(dstid==0 || dst->layer!=nil){
1147 /* BUG: this is terribly inefficient: update maximal containing rect*/
1148 r = memlinebbox(p, q, e0, e1, j);
1149 dstflush(dstid, dst, insetrect(r, -(1+1+j)));
1151 continue;
1153 /* create image mask: 'm' newid[4] id[4] */
1156 case 'm':
1157 m = 4+4;
1158 if(n < m)
1159 goto Eshortdraw;
1160 break;
1164 /* attach to a named image: 'n' dstid[4] j[1] name[j] */
1165 case 'n':
1166 m = 1+4+1;
1167 if(n < m)
1168 goto Eshortdraw;
1169 j = a[5];
1170 if(j == 0) /* give me a non-empty name please */
1171 goto Eshortdraw;
1172 m += j;
1173 if(n < m)
1174 goto Eshortdraw;
1175 dstid = BGLONG(a+1);
1176 if(drawlookup(client, dstid, 0))
1177 goto Eimageexists;
1178 dn = drawlookupname(j, (char*)a+6);
1179 if(dn == nil)
1180 goto Enoname;
1181 s = malloc(j+1);
1182 if(s == nil)
1183 goto Enomem;
1184 if(drawinstall(client, dstid, dn->dimage->image, 0) == 0)
1185 goto Edrawmem;
1186 di = drawlookup(client, dstid, 0);
1187 if(di == 0)
1188 goto Eoldname;
1189 di->vers = dn->vers;
1190 di->name = s;
1191 di->fromname = dn->dimage;
1192 di->fromname->ref++;
1193 memmove(di->name, a+6, j);
1194 di->name[j] = 0;
1195 client->infoid = dstid;
1196 continue;
1198 /* name an image: 'N' dstid[4] in[1] j[1] name[j] */
1199 case 'N':
1200 m = 1+4+1+1;
1201 if(n < m)
1202 goto Eshortdraw;
1203 c = a[5];
1204 j = a[6];
1205 if(j == 0) /* give me a non-empty name please */
1206 goto Eshortdraw;
1207 m += j;
1208 if(n < m)
1209 goto Eshortdraw;
1210 di = drawlookup(client, BGLONG(a+1), 0);
1211 if(di == 0)
1212 goto Enodrawimage;
1213 if(di->name)
1214 goto Enamed;
1215 if(c)
1216 if(drawaddname(client, di, j, (char*)a+7, &err) < 0)
1217 goto error;
1218 else{
1219 dn = drawlookupname(j, (char*)a+7);
1220 if(dn == nil)
1221 goto Enoname;
1222 if(dn->dimage != di)
1223 goto Ewrongname;
1224 drawdelname(dn);
1226 continue;
1228 /* position window: 'o' id[4] r.min [2*4] screenr.min [2*4] */
1229 case 'o':
1230 m = 1+4+2*4+2*4;
1231 if(n < m)
1232 goto Eshortdraw;
1233 dst = drawimage(client, a+1);
1234 if(!dst)
1235 goto Enodrawimage;
1236 if(dst->layer){
1237 drawpoint(&p, a+5);
1238 drawpoint(&q, a+13);
1239 r = dst->layer->screenr;
1240 ni = memlorigin(dst, p, q);
1241 if(ni < 0){
1242 err = "image origin failed";
1243 goto error;
1245 if(ni > 0){
1246 addflush(r);
1247 addflush(dst->layer->screenr);
1248 ll = drawlookup(client, BGLONG(a+1), 1);
1249 drawrefreshscreen(ll, client);
1252 continue;
1254 /* set compositing operator for next draw operation: 'O' op */
1255 case 'O':
1256 m = 1+1;
1257 if(n < m)
1258 goto Eshortdraw;
1259 client->op = a[1];
1260 continue;
1262 /* filled polygon: 'P' dstid[4] n[2] wind[4] ignore[2*4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1263 /* polygon: 'p' dstid[4] n[2] end0[4] end1[4] radius[4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1264 case 'p':
1265 case 'P':
1266 m = 1+4+2+4+4+4+4+2*4;
1267 if(n < m)
1268 goto Eshortdraw;
1269 dstid = BGLONG(a+1);
1270 dst = drawimage(client, a+1);
1271 ni = BGSHORT(a+5);
1272 if(ni < 0){
1273 err = "negative cout in polygon";
1274 goto error;
1276 e0 = BGLONG(a+7);
1277 e1 = BGLONG(a+11);
1278 j = 0;
1279 if(*a == 'p'){
1280 j = BGLONG(a+15);
1281 if(j < 0){
1282 err = "negative polygon line width";
1283 goto error;
1286 src = drawimage(client, a+19);
1287 if(!dst || !src)
1288 goto Enodrawimage;
1289 drawpoint(&sp, a+23);
1290 drawpoint(&p, a+31);
1291 ni++;
1292 pp = mallocz(ni*sizeof(Point), 1);
1293 if(pp == nil)
1294 goto Enomem;
1295 doflush = 0;
1296 if(dstid==0 || (dst->layer && dst->layer->screen->image->data == screenimage->data))
1297 doflush = 1; /* simplify test in loop */
1298 ox = oy = 0;
1299 esize = 0;
1300 u = a+m;
1301 for(y=0; y<ni; y++){
1302 q = p;
1303 oesize = esize;
1304 u = drawcoord(u, a+n, ox, &p.x);
1305 if(!u)
1306 goto Eshortdraw;
1307 u = drawcoord(u, a+n, oy, &p.y);
1308 if(!u)
1309 goto Eshortdraw;
1310 ox = p.x;
1311 oy = p.y;
1312 if(doflush){
1313 esize = j;
1314 if(*a == 'p'){
1315 if(y == 0){
1316 c = memlineendsize(e0);
1317 if(c > esize)
1318 esize = c;
1320 if(y == ni-1){
1321 c = memlineendsize(e1);
1322 if(c > esize)
1323 esize = c;
1326 if(*a=='P' && e0!=1 && e0 !=~0)
1327 r = dst->clipr;
1328 else if(y > 0){
1329 r = Rect(q.x-oesize, q.y-oesize, q.x+oesize+1, q.y+oesize+1);
1330 combinerect(&r, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1332 if(rectclip(&r, dst->clipr)) /* should perhaps be an arg to dstflush */
1333 dstflush(dstid, dst, r);
1335 pp[y] = p;
1337 if(y == 1)
1338 dstflush(dstid, dst, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1339 op = drawclientop(client);
1340 if(*a == 'p')
1341 mempoly(dst, pp, ni, e0, e1, j, src, sp, op);
1342 else
1343 memfillpoly(dst, pp, ni, e0, src, sp, op);
1344 free(pp);
1345 m = u-a;
1346 continue;
1348 /* read: 'r' id[4] R[4*4] */
1349 case 'r':
1350 m = 1+4+4*4;
1351 if(n < m)
1352 goto Eshortdraw;
1353 i = drawimage(client, a+1);
1354 if(!i)
1355 goto Enodrawimage;
1356 drawrectangle(&r, a+5);
1357 if(!rectinrect(r, i->r))
1358 goto Ereadoutside;
1359 c = bytesperline(r, i->depth);
1360 c *= Dy(r);
1361 free(client->readdata);
1362 client->readdata = mallocz(c, 0);
1363 if(client->readdata == nil){
1364 err = "readimage malloc failed";
1365 goto error;
1367 client->nreaddata = memunload(i, r, client->readdata, c);
1368 if(client->nreaddata < 0){
1369 free(client->readdata);
1370 client->readdata = nil;
1371 err = "bad readimage call";
1372 goto error;
1374 continue;
1376 /* string: 's' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] ni*(index[2]) */
1377 /* 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]) */
1378 case 's':
1379 case 'x':
1380 m = 1+4+4+4+2*4+4*4+2*4+2;
1381 if(*a == 'x')
1382 m += 4+2*4;
1383 if(n < m)
1384 goto Eshortdraw;
1386 dst = drawimage(client, a+1);
1387 dstid = BGLONG(a+1);
1388 src = drawimage(client, a+5);
1389 if(!dst || !src)
1390 goto Enodrawimage;
1391 font = drawlookup(client, BGLONG(a+9), 1);
1392 if(font == 0)
1393 goto Enodrawimage;
1394 if(font->nfchar == 0)
1395 goto Enotfont;
1396 drawpoint(&p, a+13);
1397 drawrectangle(&r, a+21);
1398 drawpoint(&sp, a+37);
1399 ni = BGSHORT(a+45);
1400 u = a+m;
1401 m += ni*2;
1402 if(n < m)
1403 goto Eshortdraw;
1404 clipr = dst->clipr;
1405 dst->clipr = r;
1406 op = drawclientop(client);
1407 if(*a == 'x'){
1408 /* paint background */
1409 l = drawimage(client, a+47);
1410 if(!l)
1411 goto Enodrawimage;
1412 drawpoint(&q, a+51);
1413 r.min.x = p.x;
1414 r.min.y = p.y-font->ascent;
1415 r.max.x = p.x;
1416 r.max.y = r.min.y+Dy(font->image->r);
1417 j = ni;
1418 while(--j >= 0){
1419 ci = BGSHORT(u);
1420 if(ci<0 || ci>=font->nfchar){
1421 dst->clipr = clipr;
1422 goto Eindex;
1424 r.max.x += font->fchar[ci].width;
1425 u += 2;
1427 memdraw(dst, r, l, q, memopaque, ZP, op);
1428 u -= 2*ni;
1430 q = p;
1431 while(--ni >= 0){
1432 ci = BGSHORT(u);
1433 if(ci<0 || ci>=font->nfchar){
1434 dst->clipr = clipr;
1435 goto Eindex;
1437 q = drawchar(dst, q, src, &sp, font, ci, op);
1438 u += 2;
1440 dst->clipr = clipr;
1441 p.y -= font->ascent;
1442 dstflush(dstid, dst, Rect(p.x, p.y, q.x, p.y+Dy(font->image->r)));
1443 continue;
1445 /* use public screen: 'S' id[4] chan[4] */
1446 case 'S':
1447 m = 1+4+4;
1448 if(n < m)
1449 goto Eshortdraw;
1450 dstid = BGLONG(a+1);
1451 if(dstid == 0)
1452 goto Ebadarg;
1453 dscrn = drawlookupdscreen(dstid);
1454 if(dscrn==0 || (dscrn->public==0 && dscrn->owner!=client))
1455 goto Enodrawscreen;
1456 if(dscrn->screen->image->chan != BGLONG(a+5)){
1457 err = "inconsistent chan";
1458 goto error;
1460 if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0)
1461 goto Edrawmem;
1462 continue;
1464 /* top or bottom windows: 't' top[1] nw[2] n*id[4] */
1465 case 't':
1466 m = 1+1+2;
1467 if(n < m)
1468 goto Eshortdraw;
1469 nw = BGSHORT(a+2);
1470 if(nw < 0)
1471 goto Ebadarg;
1472 if(nw == 0)
1473 continue;
1474 m += nw*4;
1475 if(n < m)
1476 goto Eshortdraw;
1477 lp = mallocz(nw*sizeof(Memimage*), 1);
1478 if(lp == 0)
1479 goto Enomem;
1480 for(j=0; j<nw; j++){
1481 lp[j] = drawimage(client, a+1+1+2+j*4);
1482 if(lp[j] == nil){
1483 free(lp);
1484 goto Enodrawimage;
1487 if(lp[0]->layer == 0){
1488 err = "images are not windows";
1489 free(lp);
1490 goto error;
1492 for(j=1; j<nw; j++)
1493 if(lp[j]->layer->screen != lp[0]->layer->screen){
1494 err = "images not on same screen";
1495 free(lp);
1496 goto error;
1498 if(a[1])
1499 memltofrontn(lp, nw);
1500 else
1501 memltorearn(lp, nw);
1502 if(lp[0]->layer->screen->image->data == screenimage->data)
1503 for(j=0; j<nw; j++)
1504 addflush(lp[j]->layer->screenr);
1505 free(lp);
1506 ll = drawlookup(client, BGLONG(a+1+1+2), 1);
1507 drawrefreshscreen(ll, client);
1508 continue;
1510 /* visible: 'v' */
1511 case 'v':
1512 m = 1;
1513 drawflush();
1514 continue;
1516 /* write: 'y' id[4] R[4*4] data[x*1] */
1517 /* write from compressed data: 'Y' id[4] R[4*4] data[x*1] */
1518 case 'y':
1519 case 'Y':
1520 m = 1+4+4*4;
1521 if(n < m)
1522 goto Eshortdraw;
1523 dstid = BGLONG(a+1);
1524 dst = drawimage(client, a+1);
1525 if(!dst)
1526 goto Enodrawimage;
1527 drawrectangle(&r, a+5);
1528 if(!rectinrect(r, dst->r))
1529 goto Ewriteoutside;
1530 y = memload(dst, r, a+m, n-m, *a=='Y');
1531 if(y < 0){
1532 err = "bad writeimage call";
1533 goto error;
1535 dstflush(dstid, dst, r);
1536 m += y;
1537 continue;
1540 qunlock(&sdraw.lk);
1541 return oldn - n;
1543 Enodrawimage:
1544 err = "unknown id for draw image";
1545 goto error;
1546 Enodrawscreen:
1547 err = "unknown id for draw screen";
1548 goto error;
1549 Eshortdraw:
1550 err = "short draw message";
1551 goto error;
1553 Eshortread:
1554 err = "draw read too short";
1555 goto error;
1557 Eimageexists:
1558 err = "image id in use";
1559 goto error;
1560 Escreenexists:
1561 err = "screen id in use";
1562 goto error;
1563 Edrawmem:
1564 err = "image memory allocation failed";
1565 goto error;
1566 Ereadoutside:
1567 err = "readimage outside image";
1568 goto error;
1569 Ewriteoutside:
1570 err = "writeimage outside image";
1571 goto error;
1572 Enotfont:
1573 err = "image not a font";
1574 goto error;
1575 Eindex:
1576 err = "character index out of range";
1577 goto error;
1579 Enoclient:
1580 err = "no such draw client";
1581 goto error;
1582 Edepth:
1583 err = "image has bad depth";
1584 goto error;
1585 Enameused:
1586 err = "image name in use";
1587 goto error;
1589 Enoname:
1590 err = "no image with that name";
1591 goto error;
1592 Eoldname:
1593 err = "named image no longer valid";
1594 goto error;
1595 Enamed:
1596 err = "image already has name";
1597 goto error;
1598 Ewrongname:
1599 err = "wrong name for image";
1600 goto error;
1601 Enomem:
1602 err = "out of memory";
1603 goto error;
1604 Ebadarg:
1605 err = "bad argument in draw message";
1606 goto error;
1608 error:
1609 werrstr("%s", err);
1610 qunlock(&sdraw.lk);
1611 return -1;