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 if(flushrect.min.x < flushrect.max.x)
298 _flushmemscreen(flushrect);
299 flushrect = Rect(10000, 10000, -10000, -10000);
302 static
303 int
304 drawcmp(char *a, char *b, int n)
306 if(strlen(a) != n)
307 return 1;
308 return memcmp(a, b, n);
311 static
312 DName*
313 drawlookupname(int n, char *str)
315 DName *name, *ename;
317 name = sdraw.name;
318 ename = &name[sdraw.nname];
319 for(; name<ename; name++)
320 if(drawcmp(name->name, str, n) == 0)
321 return name;
322 return 0;
325 static
326 int
327 drawgoodname(DImage *d)
329 DName *n;
331 /* if window, validate the screen's own images */
332 if(d->dscreen)
333 if(drawgoodname(d->dscreen->dimage) == 0
334 || drawgoodname(d->dscreen->dfill) == 0)
335 return 0;
336 if(d->name == nil)
337 return 1;
338 n = drawlookupname(strlen(d->name), d->name);
339 if(n==nil || n->vers!=d->vers)
340 return 0;
341 return 1;
344 static
345 DImage*
346 drawlookup(Client *client, int id, int checkname)
348 DImage *d;
350 d = client->dimage[id&HASHMASK];
351 while(d){
352 if(d->id == id){
353 /*
354 * BUG: should error out but too hard.
355 * Return 0 instead.
356 */
357 if(checkname && !drawgoodname(d))
358 return 0;
359 return d;
361 d = d->next;
363 return 0;
366 static
367 DScreen*
368 drawlookupdscreen(int id)
370 DScreen *s;
372 s = dscreen;
373 while(s){
374 if(s->id == id)
375 return s;
376 s = s->next;
378 return 0;
381 static
382 DScreen*
383 drawlookupscreen(Client *client, int id, CScreen **cs)
385 CScreen *s;
387 s = client->cscreen;
388 while(s){
389 if(s->dscreen->id == id){
390 *cs = s;
391 return s->dscreen;
393 s = s->next;
395 /* caller must check! */
396 return 0;
399 static
400 Memimage*
401 drawinstall(Client *client, int id, Memimage *i, DScreen *dscreen)
403 DImage *d;
405 d = mallocz(sizeof(DImage), 1);
406 if(d == 0)
407 return 0;
408 d->id = id;
409 d->ref = 1;
410 d->name = 0;
411 d->vers = 0;
412 d->image = i;
413 if(i->screenref)
414 ++i->screenref;
415 d->nfchar = 0;
416 d->fchar = 0;
417 d->fromname = 0;
418 d->dscreen = dscreen;
419 d->next = client->dimage[id&HASHMASK];
420 client->dimage[id&HASHMASK] = d;
421 return i;
424 static
425 Memscreen*
426 drawinstallscreen(Client *client, DScreen *d, int id, DImage *dimage, DImage *dfill, int public)
428 Memscreen *s;
429 CScreen *c;
431 c = mallocz(sizeof(CScreen), 1);
432 if(dimage && dimage->image && dimage->image->chan == 0){
433 fprint(2, "bad image %p in drawinstallscreen", dimage->image);
434 abort();
437 if(c == 0)
438 return 0;
439 if(d == 0){
440 d = mallocz(sizeof(DScreen), 1);
441 if(d == 0){
442 free(c);
443 return 0;
445 s = mallocz(sizeof(Memscreen), 1);
446 if(s == 0){
447 free(c);
448 free(d);
449 return 0;
451 s->frontmost = 0;
452 s->rearmost = 0;
453 d->dimage = dimage;
454 if(dimage){
455 s->image = dimage->image;
456 dimage->ref++;
458 d->dfill = dfill;
459 if(dfill){
460 s->fill = dfill->image;
461 dfill->ref++;
463 d->ref = 0;
464 d->id = id;
465 d->screen = s;
466 d->public = public;
467 d->next = dscreen;
468 d->owner = client;
469 dscreen = d;
471 c->dscreen = d;
472 d->ref++;
473 c->next = client->cscreen;
474 client->cscreen = c;
475 return d->screen;
478 static
479 void
480 drawdelname(DName *name)
482 int i;
484 i = name-sdraw.name;
485 memmove(name, name+1, (sdraw.nname-(i+1))*sizeof(DName));
486 sdraw.nname--;
489 static
490 void
491 drawfreedscreen(DScreen *this)
493 DScreen *ds, *next;
495 this->ref--;
496 if(this->ref < 0)
497 fprint(2, "negative ref in drawfreedscreen\n");
498 if(this->ref > 0)
499 return;
500 ds = dscreen;
501 if(ds == this){
502 dscreen = this->next;
503 goto Found;
505 while(next = ds->next){ /* assign = */
506 if(next == this){
507 ds->next = this->next;
508 goto Found;
510 ds = next;
512 /*
513 * Should signal Enodrawimage, but too hard.
514 */
515 return;
517 Found:
518 if(this->dimage)
519 drawfreedimage(this->dimage);
520 if(this->dfill)
521 drawfreedimage(this->dfill);
522 free(this->screen);
523 free(this);
526 static
527 void
528 drawfreedimage(DImage *dimage)
530 int i;
531 Memimage *l;
532 DScreen *ds;
534 dimage->ref--;
535 if(dimage->ref < 0)
536 fprint(2, "negative ref in drawfreedimage\n");
537 if(dimage->ref > 0)
538 return;
540 /* any names? */
541 for(i=0; i<sdraw.nname; )
542 if(sdraw.name[i].dimage == dimage)
543 drawdelname(sdraw.name+i);
544 else
545 i++;
546 if(dimage->fromname){ /* acquired by name; owned by someone else*/
547 drawfreedimage(dimage->fromname);
548 goto Return;
550 ds = dimage->dscreen;
551 l = dimage->image;
552 dimage->dscreen = nil; /* paranoia */
553 dimage->image = nil;
554 if(ds){
555 if(l->data == screenimage->data)
556 addflush(l->layer->screenr);
557 if(l->layer->refreshfn == drawrefresh) /* else true owner will clean up */
558 free(l->layer->refreshptr);
559 l->layer->refreshptr = nil;
560 if(drawgoodname(dimage))
561 memldelete(l);
562 else
563 memlfree(l);
564 drawfreedscreen(ds);
565 }else{
566 if(l->screenref==0)
567 freememimage(l);
568 else if(--l->screenref==0)
569 _freememimage(l);
571 Return:
572 free(dimage->fchar);
573 free(dimage);
576 static
577 void
578 drawuninstallscreen(Client *client, CScreen *this)
580 CScreen *cs, *next;
582 cs = client->cscreen;
583 if(cs == this){
584 client->cscreen = this->next;
585 drawfreedscreen(this->dscreen);
586 free(this);
587 return;
589 while(next = cs->next){ /* assign = */
590 if(next == this){
591 cs->next = this->next;
592 drawfreedscreen(this->dscreen);
593 free(this);
594 return;
596 cs = next;
600 static
601 int
602 drawuninstall(Client *client, int id)
604 DImage *d, **l;
606 for(l=&client->dimage[id&HASHMASK]; (d=*l) != nil; l=&d->next){
607 if(d->id == id){
608 *l = d->next;
609 drawfreedimage(d);
610 return 0;
613 return -1;
616 static
617 int
618 drawaddname(Client *client, DImage *di, int n, char *str, char **err)
620 DName *name, *ename, *new, *t;
621 char *ns;
623 name = sdraw.name;
624 ename = &name[sdraw.nname];
625 for(; name<ename; name++)
626 if(drawcmp(name->name, str, n) == 0){
627 *err = "image name in use";
628 return -1;
630 t = mallocz((sdraw.nname+1)*sizeof(DName), 1);
631 ns = malloc(n+1);
632 if(t == nil || ns == nil){
633 free(t);
634 free(ns);
635 *err = "out of memory";
636 return -1;
638 memmove(t, sdraw.name, sdraw.nname*sizeof(DName));
639 free(sdraw.name);
640 sdraw.name = t;
641 new = &sdraw.name[sdraw.nname++];
642 new->name = ns;
643 memmove(new->name, str, n);
644 new->name[n] = 0;
645 new->dimage = di;
646 new->client = client;
647 new->vers = ++sdraw.vers;
648 return 0;
651 static int
652 drawclientop(Client *cl)
654 int op;
656 op = cl->op;
657 cl->op = SoverD;
658 return op;
661 static
662 Memimage*
663 drawimage(Client *client, uchar *a)
665 DImage *d;
667 d = drawlookup(client, BGLONG(a), 1);
668 if(d == nil)
669 return nil; /* caller must check! */
670 return d->image;
673 static
674 void
675 drawrectangle(Rectangle *r, uchar *a)
677 r->min.x = BGLONG(a+0*4);
678 r->min.y = BGLONG(a+1*4);
679 r->max.x = BGLONG(a+2*4);
680 r->max.y = BGLONG(a+3*4);
683 static
684 void
685 drawpoint(Point *p, uchar *a)
687 p->x = BGLONG(a+0*4);
688 p->y = BGLONG(a+1*4);
691 static
692 Point
693 drawchar(Memimage *dst, Point p, Memimage *src, Point *sp, DImage *font, int index, int op)
695 FChar *fc;
696 Rectangle r;
697 Point sp1;
699 fc = &font->fchar[index];
700 r.min.x = p.x+fc->left;
701 r.min.y = p.y-(font->ascent-fc->miny);
702 r.max.x = r.min.x+(fc->maxx-fc->minx);
703 r.max.y = r.min.y+(fc->maxy-fc->miny);
704 sp1.x = sp->x+fc->left;
705 sp1.y = sp->y+fc->miny;
706 memdraw(dst, r, src, sp1, font->image, Pt(fc->minx, fc->miny), op);
707 p.x += fc->width;
708 sp->x += fc->width;
709 return p;
712 static
713 uchar*
714 drawcoord(uchar *p, uchar *maxp, int oldx, int *newx)
716 int b, x;
718 if(p >= maxp)
719 return nil;
720 b = *p++;
721 x = b & 0x7F;
722 if(b & 0x80){
723 if(p+1 >= maxp)
724 return nil;
725 x |= *p++ << 7;
726 x |= *p++ << 15;
727 if(x & (1<<22))
728 x |= ~0<<23;
729 }else{
730 if(b & 0x40)
731 x |= ~0<<7;
732 x += oldx;
734 *newx = x;
735 return p;
738 int
739 _drawmsgread(void *a, int n)
741 Client *cl;
743 qlock(&sdraw.lk);
744 cl = client0;
745 if(cl->readdata == nil){
746 werrstr("no draw data");
747 goto err;
749 if(n < cl->nreaddata){
750 werrstr("short read");
751 goto err;
753 n = cl->nreaddata;
754 memmove(a, cl->readdata, cl->nreaddata);
755 free(cl->readdata);
756 cl->readdata = nil;
757 qunlock(&sdraw.lk);
758 return n;
760 err:
761 qunlock(&sdraw.lk);
762 return -1;
765 int
766 _drawmsgwrite(void *v, int n)
768 char cbuf[40], *err, ibuf[12*12+1], *s;
769 int c, ci, doflush, dstid, e0, e1, esize, j, m;
770 int ni, nw, oesize, oldn, op, ox, oy, repl, scrnid, y;
771 uchar *a, refresh, *u;
772 u32int chan, value;
773 Client *client;
774 CScreen *cs;
775 DImage *di, *ddst, *dsrc, *font, *ll;
776 DName *dn;
777 DScreen *dscrn;
778 FChar *fc;
779 Memimage *dst, *i, *l, **lp, *mask, *src;
780 Memscreen *scrn;
781 Point p, *pp, q, sp;
782 Rectangle clipr, r;
783 Refreshfn reffn;
784 Refx *refx;
786 qlock(&sdraw.lk);
787 a = v;
788 m = 0;
789 oldn = n;
790 client = client0;
792 while((n-=m) > 0){
793 a += m;
794 /*fprint(2, "msgwrite %d(%d)...", n, *a); */
795 switch(*a){
796 default:
797 /*fprint(2, "bad command %d\n", *a); */
798 err = "bad draw command";
799 goto error;
801 /* allocate: 'b' id[4] screenid[4] refresh[1] chan[4] repl[1]
802 R[4*4] clipR[4*4] rrggbbaa[4]
803 */
804 case 'b':
805 m = 1+4+4+1+4+1+4*4+4*4+4;
806 if(n < m)
807 goto Eshortdraw;
808 dstid = BGLONG(a+1);
809 scrnid = BGSHORT(a+5);
810 refresh = a[9];
811 chan = BGLONG(a+10);
812 repl = a[14];
813 drawrectangle(&r, a+15);
814 drawrectangle(&clipr, a+31);
815 value = BGLONG(a+47);
816 if(drawlookup(client, dstid, 0))
817 goto Eimageexists;
818 if(scrnid){
819 dscrn = drawlookupscreen(client, scrnid, &cs);
820 if(!dscrn)
821 goto Enodrawscreen;
822 scrn = dscrn->screen;
823 if(repl || chan!=scrn->image->chan){
824 err = "image parameters incompatibile with screen";
825 goto error;
827 reffn = 0;
828 switch(refresh){
829 case Refbackup:
830 break;
831 case Refnone:
832 reffn = memlnorefresh;
833 break;
834 case Refmesg:
835 reffn = drawrefresh;
836 break;
837 default:
838 err = "unknown refresh method";
839 goto error;
841 l = memlalloc(scrn, r, reffn, 0, value);
842 if(l == 0)
843 goto Edrawmem;
844 addflush(l->layer->screenr);
845 l->clipr = clipr;
846 rectclip(&l->clipr, r);
847 if(drawinstall(client, dstid, l, dscrn) == 0){
848 memldelete(l);
849 goto Edrawmem;
851 dscrn->ref++;
852 if(reffn){
853 refx = nil;
854 if(reffn == drawrefresh){
855 refx = mallocz(sizeof(Refx), 1);
856 if(refx == 0){
857 if(drawuninstall(client, dstid) < 0)
858 goto Enodrawimage;
859 goto Edrawmem;
861 refx->client = client;
862 refx->dimage = drawlookup(client, dstid, 1);
864 memlsetrefresh(l, reffn, refx);
866 continue;
868 i = allocmemimage(r, chan);
869 if(i == 0)
870 goto Edrawmem;
871 if(repl)
872 i->flags |= Frepl;
873 i->clipr = clipr;
874 if(!repl)
875 rectclip(&i->clipr, r);
876 if(drawinstall(client, dstid, i, 0) == 0){
877 freememimage(i);
878 goto Edrawmem;
880 memfillcolor(i, value);
881 continue;
883 /* allocate screen: 'A' id[4] imageid[4] fillid[4] public[1] */
884 case 'A':
885 m = 1+4+4+4+1;
886 if(n < m)
887 goto Eshortdraw;
888 dstid = BGLONG(a+1);
889 if(dstid == 0)
890 goto Ebadarg;
891 if(drawlookupdscreen(dstid))
892 goto Escreenexists;
893 ddst = drawlookup(client, BGLONG(a+5), 1);
894 dsrc = drawlookup(client, BGLONG(a+9), 1);
895 if(ddst==0 || dsrc==0)
896 goto Enodrawimage;
897 if(drawinstallscreen(client, 0, dstid, ddst, dsrc, a[13]) == 0)
898 goto Edrawmem;
899 continue;
901 /* set repl and clip: 'c' dstid[4] repl[1] clipR[4*4] */
902 case 'c':
903 m = 1+4+1+4*4;
904 if(n < m)
905 goto Eshortdraw;
906 ddst = drawlookup(client, BGLONG(a+1), 1);
907 if(ddst == nil)
908 goto Enodrawimage;
909 if(ddst->name){
910 err = "can't change repl/clipr of shared image";
911 goto error;
913 dst = ddst->image;
914 if(a[5])
915 dst->flags |= Frepl;
916 drawrectangle(&dst->clipr, a+6);
917 continue;
919 /* draw: 'd' dstid[4] srcid[4] maskid[4] R[4*4] P[2*4] P[2*4] */
920 case 'd':
921 m = 1+4+4+4+4*4+2*4+2*4;
922 if(n < m)
923 goto Eshortdraw;
924 dst = drawimage(client, a+1);
925 dstid = BGLONG(a+1);
926 src = drawimage(client, a+5);
927 mask = drawimage(client, a+9);
928 if(!dst || !src || !mask)
929 goto Enodrawimage;
930 drawrectangle(&r, a+13);
931 drawpoint(&p, a+29);
932 drawpoint(&q, a+37);
933 op = drawclientop(client);
934 memdraw(dst, r, src, p, mask, q, op);
935 dstflush(dstid, dst, r);
936 continue;
938 /* toggle debugging: 'D' val[1] */
939 case 'D':
940 m = 1+1;
941 if(n < m)
942 goto Eshortdraw;
943 drawdebug = a[1];
944 continue;
946 /* ellipse: 'e' dstid[4] srcid[4] center[2*4] a[4] b[4] thick[4] sp[2*4] alpha[4] phi[4]*/
947 case 'e':
948 case 'E':
949 m = 1+4+4+2*4+4+4+4+2*4+2*4;
950 if(n < m)
951 goto Eshortdraw;
952 dst = drawimage(client, a+1);
953 dstid = BGLONG(a+1);
954 src = drawimage(client, a+5);
955 if(!dst || !src)
956 goto Enodrawimage;
957 drawpoint(&p, a+9);
958 e0 = BGLONG(a+17);
959 e1 = BGLONG(a+21);
960 if(e0<0 || e1<0){
961 err = "invalid ellipse semidiameter";
962 goto error;
964 j = BGLONG(a+25);
965 if(j < 0){
966 err = "negative ellipse thickness";
967 goto error;
970 drawpoint(&sp, a+29);
971 c = j;
972 if(*a == 'E')
973 c = -1;
974 ox = BGLONG(a+37);
975 oy = BGLONG(a+41);
976 op = drawclientop(client);
977 /* high bit indicates arc angles are present */
978 if(ox & ((ulong)1<<31)){
979 if((ox & ((ulong)1<<30)) == 0)
980 ox &= ~((ulong)1<<31);
981 memarc(dst, p, e0, e1, c, src, sp, ox, oy, op);
982 }else
983 memellipse(dst, p, e0, e1, c, src, sp, op);
984 dstflush(dstid, dst, Rect(p.x-e0-j, p.y-e1-j, p.x+e0+j+1, p.y+e1+j+1));
985 continue;
987 /* free: 'f' id[4] */
988 case 'f':
989 m = 1+4;
990 if(n < m)
991 goto Eshortdraw;
992 ll = drawlookup(client, BGLONG(a+1), 0);
993 if(ll && ll->dscreen && ll->dscreen->owner != client)
994 ll->dscreen->owner->refreshme = 1;
995 if(drawuninstall(client, BGLONG(a+1)) < 0)
996 goto Enodrawimage;
997 continue;
999 /* free screen: 'F' id[4] */
1000 case 'F':
1001 m = 1+4;
1002 if(n < m)
1003 goto Eshortdraw;
1004 if(!drawlookupscreen(client, BGLONG(a+1), &cs))
1005 goto Enodrawscreen;
1006 drawuninstallscreen(client, cs);
1007 continue;
1009 /* initialize font: 'i' fontid[4] nchars[4] ascent[1] */
1010 case 'i':
1011 m = 1+4+4+1;
1012 if(n < m)
1013 goto Eshortdraw;
1014 dstid = BGLONG(a+1);
1015 if(dstid == 0){
1016 err = "can't use display as font";
1017 goto error;
1019 font = drawlookup(client, dstid, 1);
1020 if(font == 0)
1021 goto Enodrawimage;
1022 if(font->image->layer){
1023 err = "can't use window as font";
1024 goto error;
1026 ni = BGLONG(a+5);
1027 if(ni<=0 || ni>4096){
1028 err = "bad font size (4096 chars max)";
1029 goto error;
1031 free(font->fchar); /* should we complain if non-zero? */
1032 font->fchar = mallocz(ni*sizeof(FChar), 1);
1033 if(font->fchar == 0){
1034 err = "no memory for font";
1035 goto error;
1037 memset(font->fchar, 0, ni*sizeof(FChar));
1038 font->nfchar = ni;
1039 font->ascent = a[9];
1040 continue;
1042 /* set image 0 to screen image */
1043 case 'J':
1044 m = 1;
1045 if(n < m)
1046 goto Eshortdraw;
1047 if(drawlookup(client, 0, 0))
1048 goto Eimageexists;
1049 drawinstall(client, 0, screenimage, 0);
1050 client->infoid = 0;
1051 continue;
1053 /* get image info: 'I' */
1054 case 'I':
1055 m = 1;
1056 if(n < m)
1057 goto Eshortdraw;
1058 if(client->infoid < 0)
1059 goto Enodrawimage;
1060 if(client->infoid == 0){
1061 i = screenimage;
1062 if(i == nil)
1063 goto Enodrawimage;
1064 }else{
1065 di = drawlookup(client, client->infoid, 1);
1066 if(di == nil)
1067 goto Enodrawimage;
1068 i = di->image;
1070 ni = sprint(ibuf, "%11d %11d %11s %11d %11d %11d %11d %11d"
1071 " %11d %11d %11d %11d ",
1072 client->clientid,
1073 client->infoid,
1074 chantostr(cbuf, i->chan),
1075 (i->flags&Frepl)==Frepl,
1076 i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y,
1077 i->clipr.min.x, i->clipr.min.y,
1078 i->clipr.max.x, i->clipr.max.y);
1079 free(client->readdata);
1080 client->readdata = malloc(ni);
1081 if(client->readdata == nil)
1082 goto Enomem;
1083 memmove(client->readdata, ibuf, ni);
1084 client->nreaddata = ni;
1085 client->infoid = -1;
1086 continue;
1088 /* load character: 'l' fontid[4] srcid[4] index[2] R[4*4] P[2*4] left[1] width[1] */
1089 case 'l':
1090 m = 1+4+4+2+4*4+2*4+1+1;
1091 if(n < m)
1092 goto Eshortdraw;
1093 font = drawlookup(client, BGLONG(a+1), 1);
1094 if(font == 0)
1095 goto Enodrawimage;
1096 if(font->nfchar == 0)
1097 goto Enotfont;
1098 src = drawimage(client, a+5);
1099 if(!src)
1100 goto Enodrawimage;
1101 ci = BGSHORT(a+9);
1102 if(ci >= font->nfchar)
1103 goto Eindex;
1104 drawrectangle(&r, a+11);
1105 drawpoint(&p, a+27);
1106 memdraw(font->image, r, src, p, memopaque, p, S);
1107 fc = &font->fchar[ci];
1108 fc->minx = r.min.x;
1109 fc->maxx = r.max.x;
1110 fc->miny = r.min.y;
1111 fc->maxy = r.max.y;
1112 fc->left = a[35];
1113 fc->width = a[36];
1114 continue;
1116 /* draw line: 'L' dstid[4] p0[2*4] p1[2*4] end0[4] end1[4] radius[4] srcid[4] sp[2*4] */
1117 case 'L':
1118 m = 1+4+2*4+2*4+4+4+4+4+2*4;
1119 if(n < m)
1120 goto Eshortdraw;
1121 dst = drawimage(client, a+1);
1122 dstid = BGLONG(a+1);
1123 drawpoint(&p, a+5);
1124 drawpoint(&q, a+13);
1125 e0 = BGLONG(a+21);
1126 e1 = BGLONG(a+25);
1127 j = BGLONG(a+29);
1128 if(j < 0){
1129 err = "negative line width";
1130 goto error;
1132 src = drawimage(client, a+33);
1133 if(!dst || !src)
1134 goto Enodrawimage;
1135 drawpoint(&sp, a+37);
1136 op = drawclientop(client);
1137 memline(dst, p, q, e0, e1, j, src, sp, op);
1138 /* avoid memlinebbox if possible */
1139 if(dstid==0 || dst->layer!=nil){
1140 /* BUG: this is terribly inefficient: update maximal containing rect*/
1141 r = memlinebbox(p, q, e0, e1, j);
1142 dstflush(dstid, dst, insetrect(r, -(1+1+j)));
1144 continue;
1146 /* create image mask: 'm' newid[4] id[4] */
1149 case 'm':
1150 m = 4+4;
1151 if(n < m)
1152 goto Eshortdraw;
1153 break;
1157 /* attach to a named image: 'n' dstid[4] j[1] name[j] */
1158 case 'n':
1159 m = 1+4+1;
1160 if(n < m)
1161 goto Eshortdraw;
1162 j = a[5];
1163 if(j == 0) /* give me a non-empty name please */
1164 goto Eshortdraw;
1165 m += j;
1166 if(n < m)
1167 goto Eshortdraw;
1168 dstid = BGLONG(a+1);
1169 if(drawlookup(client, dstid, 0))
1170 goto Eimageexists;
1171 dn = drawlookupname(j, (char*)a+6);
1172 if(dn == nil)
1173 goto Enoname;
1174 s = malloc(j+1);
1175 if(s == nil)
1176 goto Enomem;
1177 if(drawinstall(client, dstid, dn->dimage->image, 0) == 0)
1178 goto Edrawmem;
1179 di = drawlookup(client, dstid, 0);
1180 if(di == 0)
1181 goto Eoldname;
1182 di->vers = dn->vers;
1183 di->name = s;
1184 di->fromname = dn->dimage;
1185 di->fromname->ref++;
1186 memmove(di->name, a+6, j);
1187 di->name[j] = 0;
1188 client->infoid = dstid;
1189 continue;
1191 /* name an image: 'N' dstid[4] in[1] j[1] name[j] */
1192 case 'N':
1193 m = 1+4+1+1;
1194 if(n < m)
1195 goto Eshortdraw;
1196 c = a[5];
1197 j = a[6];
1198 if(j == 0) /* give me a non-empty name please */
1199 goto Eshortdraw;
1200 m += j;
1201 if(n < m)
1202 goto Eshortdraw;
1203 di = drawlookup(client, BGLONG(a+1), 0);
1204 if(di == 0)
1205 goto Enodrawimage;
1206 if(di->name)
1207 goto Enamed;
1208 if(c)
1209 if(drawaddname(client, di, j, (char*)a+7, &err) < 0)
1210 goto error;
1211 else{
1212 dn = drawlookupname(j, (char*)a+7);
1213 if(dn == nil)
1214 goto Enoname;
1215 if(dn->dimage != di)
1216 goto Ewrongname;
1217 drawdelname(dn);
1219 continue;
1221 /* position window: 'o' id[4] r.min [2*4] screenr.min [2*4] */
1222 case 'o':
1223 m = 1+4+2*4+2*4;
1224 if(n < m)
1225 goto Eshortdraw;
1226 dst = drawimage(client, a+1);
1227 if(!dst)
1228 goto Enodrawimage;
1229 if(dst->layer){
1230 drawpoint(&p, a+5);
1231 drawpoint(&q, a+13);
1232 r = dst->layer->screenr;
1233 ni = memlorigin(dst, p, q);
1234 if(ni < 0){
1235 err = "image origin failed";
1236 goto error;
1238 if(ni > 0){
1239 addflush(r);
1240 addflush(dst->layer->screenr);
1241 ll = drawlookup(client, BGLONG(a+1), 1);
1242 drawrefreshscreen(ll, client);
1245 continue;
1247 /* set compositing operator for next draw operation: 'O' op */
1248 case 'O':
1249 m = 1+1;
1250 if(n < m)
1251 goto Eshortdraw;
1252 client->op = a[1];
1253 continue;
1255 /* filled polygon: 'P' dstid[4] n[2] wind[4] ignore[2*4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1256 /* polygon: 'p' dstid[4] n[2] end0[4] end1[4] radius[4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
1257 case 'p':
1258 case 'P':
1259 m = 1+4+2+4+4+4+4+2*4;
1260 if(n < m)
1261 goto Eshortdraw;
1262 dstid = BGLONG(a+1);
1263 dst = drawimage(client, a+1);
1264 ni = BGSHORT(a+5);
1265 if(ni < 0){
1266 err = "negative cout in polygon";
1267 goto error;
1269 e0 = BGLONG(a+7);
1270 e1 = BGLONG(a+11);
1271 j = 0;
1272 if(*a == 'p'){
1273 j = BGLONG(a+15);
1274 if(j < 0){
1275 err = "negative polygon line width";
1276 goto error;
1279 src = drawimage(client, a+19);
1280 if(!dst || !src)
1281 goto Enodrawimage;
1282 drawpoint(&sp, a+23);
1283 drawpoint(&p, a+31);
1284 ni++;
1285 pp = mallocz(ni*sizeof(Point), 1);
1286 if(pp == nil)
1287 goto Enomem;
1288 doflush = 0;
1289 if(dstid==0 || (dst->layer && dst->layer->screen->image->data == screenimage->data))
1290 doflush = 1; /* simplify test in loop */
1291 ox = oy = 0;
1292 esize = 0;
1293 u = a+m;
1294 for(y=0; y<ni; y++){
1295 q = p;
1296 oesize = esize;
1297 u = drawcoord(u, a+n, ox, &p.x);
1298 if(!u)
1299 goto Eshortdraw;
1300 u = drawcoord(u, a+n, oy, &p.y);
1301 if(!u)
1302 goto Eshortdraw;
1303 ox = p.x;
1304 oy = p.y;
1305 if(doflush){
1306 esize = j;
1307 if(*a == 'p'){
1308 if(y == 0){
1309 c = memlineendsize(e0);
1310 if(c > esize)
1311 esize = c;
1313 if(y == ni-1){
1314 c = memlineendsize(e1);
1315 if(c > esize)
1316 esize = c;
1319 if(*a=='P' && e0!=1 && e0 !=~0)
1320 r = dst->clipr;
1321 else if(y > 0){
1322 r = Rect(q.x-oesize, q.y-oesize, q.x+oesize+1, q.y+oesize+1);
1323 combinerect(&r, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1325 if(rectclip(&r, dst->clipr)) /* should perhaps be an arg to dstflush */
1326 dstflush(dstid, dst, r);
1328 pp[y] = p;
1330 if(y == 1)
1331 dstflush(dstid, dst, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
1332 op = drawclientop(client);
1333 if(*a == 'p')
1334 mempoly(dst, pp, ni, e0, e1, j, src, sp, op);
1335 else
1336 memfillpoly(dst, pp, ni, e0, src, sp, op);
1337 free(pp);
1338 m = u-a;
1339 continue;
1341 /* read: 'r' id[4] R[4*4] */
1342 case 'r':
1343 m = 1+4+4*4;
1344 if(n < m)
1345 goto Eshortdraw;
1346 i = drawimage(client, a+1);
1347 if(!i)
1348 goto Enodrawimage;
1349 drawrectangle(&r, a+5);
1350 if(!rectinrect(r, i->r))
1351 goto Ereadoutside;
1352 c = bytesperline(r, i->depth);
1353 c *= Dy(r);
1354 free(client->readdata);
1355 client->readdata = mallocz(c, 0);
1356 if(client->readdata == nil){
1357 err = "readimage malloc failed";
1358 goto error;
1360 client->nreaddata = memunload(i, r, client->readdata, c);
1361 if(client->nreaddata < 0){
1362 free(client->readdata);
1363 client->readdata = nil;
1364 err = "bad readimage call";
1365 goto error;
1367 continue;
1369 /* string: 's' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] ni*(index[2]) */
1370 /* 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]) */
1371 case 's':
1372 case 'x':
1373 m = 1+4+4+4+2*4+4*4+2*4+2;
1374 if(*a == 'x')
1375 m += 4+2*4;
1376 if(n < m)
1377 goto Eshortdraw;
1379 dst = drawimage(client, a+1);
1380 dstid = BGLONG(a+1);
1381 src = drawimage(client, a+5);
1382 if(!dst || !src)
1383 goto Enodrawimage;
1384 font = drawlookup(client, BGLONG(a+9), 1);
1385 if(font == 0)
1386 goto Enodrawimage;
1387 if(font->nfchar == 0)
1388 goto Enotfont;
1389 drawpoint(&p, a+13);
1390 drawrectangle(&r, a+21);
1391 drawpoint(&sp, a+37);
1392 ni = BGSHORT(a+45);
1393 u = a+m;
1394 m += ni*2;
1395 if(n < m)
1396 goto Eshortdraw;
1397 clipr = dst->clipr;
1398 dst->clipr = r;
1399 op = drawclientop(client);
1400 if(*a == 'x'){
1401 /* paint background */
1402 l = drawimage(client, a+47);
1403 if(!l)
1404 goto Enodrawimage;
1405 drawpoint(&q, a+51);
1406 r.min.x = p.x;
1407 r.min.y = p.y-font->ascent;
1408 r.max.x = p.x;
1409 r.max.y = r.min.y+Dy(font->image->r);
1410 j = ni;
1411 while(--j >= 0){
1412 ci = BGSHORT(u);
1413 if(ci<0 || ci>=font->nfchar){
1414 dst->clipr = clipr;
1415 goto Eindex;
1417 r.max.x += font->fchar[ci].width;
1418 u += 2;
1420 memdraw(dst, r, l, q, memopaque, ZP, op);
1421 u -= 2*ni;
1423 q = p;
1424 while(--ni >= 0){
1425 ci = BGSHORT(u);
1426 if(ci<0 || ci>=font->nfchar){
1427 dst->clipr = clipr;
1428 goto Eindex;
1430 q = drawchar(dst, q, src, &sp, font, ci, op);
1431 u += 2;
1433 dst->clipr = clipr;
1434 p.y -= font->ascent;
1435 dstflush(dstid, dst, Rect(p.x, p.y, q.x, p.y+Dy(font->image->r)));
1436 continue;
1438 /* use public screen: 'S' id[4] chan[4] */
1439 case 'S':
1440 m = 1+4+4;
1441 if(n < m)
1442 goto Eshortdraw;
1443 dstid = BGLONG(a+1);
1444 if(dstid == 0)
1445 goto Ebadarg;
1446 dscrn = drawlookupdscreen(dstid);
1447 if(dscrn==0 || (dscrn->public==0 && dscrn->owner!=client))
1448 goto Enodrawscreen;
1449 if(dscrn->screen->image->chan != BGLONG(a+5)){
1450 err = "inconsistent chan";
1451 goto error;
1453 if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0)
1454 goto Edrawmem;
1455 continue;
1457 /* top or bottom windows: 't' top[1] nw[2] n*id[4] */
1458 case 't':
1459 m = 1+1+2;
1460 if(n < m)
1461 goto Eshortdraw;
1462 nw = BGSHORT(a+2);
1463 if(nw < 0)
1464 goto Ebadarg;
1465 if(nw == 0)
1466 continue;
1467 m += nw*4;
1468 if(n < m)
1469 goto Eshortdraw;
1470 lp = mallocz(nw*sizeof(Memimage*), 1);
1471 if(lp == 0)
1472 goto Enomem;
1473 for(j=0; j<nw; j++){
1474 lp[j] = drawimage(client, a+1+1+2+j*4);
1475 if(lp[j] == nil){
1476 free(lp);
1477 goto Enodrawimage;
1480 if(lp[0]->layer == 0){
1481 err = "images are not windows";
1482 free(lp);
1483 goto error;
1485 for(j=1; j<nw; j++)
1486 if(lp[j]->layer->screen != lp[0]->layer->screen){
1487 err = "images not on same screen";
1488 free(lp);
1489 goto error;
1491 if(a[1])
1492 memltofrontn(lp, nw);
1493 else
1494 memltorearn(lp, nw);
1495 if(lp[0]->layer->screen->image->data == screenimage->data)
1496 for(j=0; j<nw; j++)
1497 addflush(lp[j]->layer->screenr);
1498 free(lp);
1499 ll = drawlookup(client, BGLONG(a+1+1+2), 1);
1500 drawrefreshscreen(ll, client);
1501 continue;
1503 /* visible: 'v' */
1504 case 'v':
1505 m = 1;
1506 drawflush();
1507 continue;
1509 /* write: 'y' id[4] R[4*4] data[x*1] */
1510 /* write from compressed data: 'Y' id[4] R[4*4] data[x*1] */
1511 case 'y':
1512 case 'Y':
1513 m = 1+4+4*4;
1514 if(n < m)
1515 goto Eshortdraw;
1516 dstid = BGLONG(a+1);
1517 dst = drawimage(client, a+1);
1518 if(!dst)
1519 goto Enodrawimage;
1520 drawrectangle(&r, a+5);
1521 if(!rectinrect(r, dst->r))
1522 goto Ewriteoutside;
1523 y = memload(dst, r, a+m, n-m, *a=='Y');
1524 if(y < 0){
1525 err = "bad writeimage call";
1526 goto error;
1528 dstflush(dstid, dst, r);
1529 m += y;
1530 continue;
1533 qunlock(&sdraw.lk);
1534 return oldn - n;
1536 Enodrawimage:
1537 err = "unknown id for draw image";
1538 goto error;
1539 Enodrawscreen:
1540 err = "unknown id for draw screen";
1541 goto error;
1542 Eshortdraw:
1543 err = "short draw message";
1544 goto error;
1546 Eshortread:
1547 err = "draw read too short";
1548 goto error;
1550 Eimageexists:
1551 err = "image id in use";
1552 goto error;
1553 Escreenexists:
1554 err = "screen id in use";
1555 goto error;
1556 Edrawmem:
1557 err = "image memory allocation failed";
1558 goto error;
1559 Ereadoutside:
1560 err = "readimage outside image";
1561 goto error;
1562 Ewriteoutside:
1563 err = "writeimage outside image";
1564 goto error;
1565 Enotfont:
1566 err = "image not a font";
1567 goto error;
1568 Eindex:
1569 err = "character index out of range";
1570 goto error;
1572 Enoclient:
1573 err = "no such draw client";
1574 goto error;
1575 Edepth:
1576 err = "image has bad depth";
1577 goto error;
1578 Enameused:
1579 err = "image name in use";
1580 goto error;
1582 Enoname:
1583 err = "no image with that name";
1584 goto error;
1585 Eoldname:
1586 err = "named image no longer valid";
1587 goto error;
1588 Enamed:
1589 err = "image already has name";
1590 goto error;
1591 Ewrongname:
1592 err = "wrong name for image";
1593 goto error;
1594 Enomem:
1595 err = "out of memory";
1596 goto error;
1597 Ebadarg:
1598 err = "bad argument in draw message";
1599 goto error;
1601 error:
1602 werrstr("%s", err);
1603 qunlock(&sdraw.lk);
1604 return -1;