rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * /dev/draw simulator -- handles the messages prepared by the draw library. |
| 3 | * Includes all the memlayer code even though most programs don't use it. |
| 4 | * This whole approach is overkill, but cpu is cheap and it keeps things simple. |
| 5 | */ |
| 6 | |
| 7 | #include <u.h> |
| 8 | #include <libc.h> |
| 9 | #include <draw.h> |
| 10 | #include <memdraw.h> |
| 11 | #include <memlayer.h> |
| 12 | |
| 13 | #define NHASH (1<<5) |
| 14 | #define HASHMASK (NHASH-1) |
| 15 | |
| 16 | extern void flushmemscreen(Rectangle); |
| 17 | |
| 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; |
| 27 | |
| 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 | }; |
| 39 | |
| 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 | }; |
| 56 | |
| 57 | struct Refresh |
| 58 | { |
| 59 | DImage* dimage; |
| 60 | Rectangle r; |
| 61 | Refresh* next; |
| 62 | }; |
| 63 | |
| 64 | struct Refx |
| 65 | { |
| 66 | Client* client; |
| 67 | DImage* dimage; |
| 68 | }; |
| 69 | |
| 70 | struct DName |
| 71 | { |
| 72 | char *name; |
| 73 | Client *client; |
| 74 | DImage* dimage; |
| 75 | int vers; |
| 76 | }; |
| 77 | |
| 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 | }; |
| 87 | |
| 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 | }; |
| 108 | |
| 109 | struct CScreen |
| 110 | { |
| 111 | DScreen* dscreen; |
| 112 | CScreen* next; |
| 113 | }; |
| 114 | |
| 115 | struct DScreen |
| 116 | { |
| 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 | }; |
| 126 | |
| 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*); |
| 136 | |
| 137 | void |
| 138 | _initdisplaymemimage(Display *d, Memimage *m) |
| 139 | { |
| 140 | screenimage = m; |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 141 | m->screenref = 1; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 142 | client0 = mallocz(sizeof(Client), 1); |
| 143 | if(client0 == nil){ |
| 144 | fprint(2, "initdraw: allocating client0: out of memory"); |
| 145 | abort(); |
| 146 | } |
| 147 | client0->slot = 0; |
| 148 | client0->clientid = ++sdraw.clientid; |
| 149 | client0->op = SoverD; |
| 150 | sdraw.client[0] = client0; |
| 151 | sdraw.nclient = 1; |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | _drawreplacescreenimage(Memimage *m) |
| 156 | { |
| 157 | /* |
| 158 | * Replace the screen image because the screen |
| 159 | * was resized. |
| 160 | * |
| 161 | * In theory there should only be one reference |
| 162 | * to the current screen image, and that's through |
| 163 | * client0's image 0, installed a few lines above. |
| 164 | * Once the client drops the image, the underlying backing |
| 165 | * store freed properly. The client is being notified |
| 166 | * about the resize through external means, so all we |
| 167 | * need to do is this assignment. |
| 168 | */ |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 169 | Memimage *om; |
| 170 | |
| 171 | qlock(&sdraw.lk); |
| 172 | om = screenimage; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 173 | screenimage = m; |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 174 | m->screenref = 1; |
| 175 | if(om && --om->screenref == 0){ |
| 176 | _freememimage(om); |
| 177 | } |
| 178 | qunlock(&sdraw.lk); |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static |
| 182 | void |
| 183 | drawrefreshscreen(DImage *l, Client *client) |
| 184 | { |
| 185 | while(l != nil && l->dscreen == nil) |
| 186 | l = l->fromname; |
| 187 | if(l != nil && l->dscreen->owner != client) |
| 188 | l->dscreen->owner->refreshme = 1; |
| 189 | } |
| 190 | |
| 191 | static |
| 192 | void |
| 193 | drawrefresh(Memimage *m, Rectangle r, void *v) |
| 194 | { |
| 195 | Refx *x; |
| 196 | DImage *d; |
| 197 | Client *c; |
| 198 | Refresh *ref; |
| 199 | |
| 200 | USED(m); |
| 201 | |
| 202 | if(v == 0) |
| 203 | return; |
| 204 | x = v; |
| 205 | c = x->client; |
| 206 | d = x->dimage; |
| 207 | for(ref=c->refresh; ref; ref=ref->next) |
| 208 | if(ref->dimage == d){ |
| 209 | combinerect(&ref->r, r); |
| 210 | return; |
| 211 | } |
| 212 | ref = mallocz(sizeof(Refresh), 1); |
| 213 | if(ref){ |
| 214 | ref->dimage = d; |
| 215 | ref->r = r; |
| 216 | ref->next = c->refresh; |
| 217 | c->refresh = ref; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static void |
| 222 | addflush(Rectangle r) |
| 223 | { |
| 224 | int abb, ar, anbb; |
| 225 | Rectangle nbb; |
| 226 | |
| 227 | if(sdraw.softscreen==0 || !rectclip(&r, screenimage->r)) |
| 228 | return; |
| 229 | |
| 230 | if(flushrect.min.x >= flushrect.max.x){ |
| 231 | flushrect = r; |
| 232 | waste = 0; |
| 233 | return; |
| 234 | } |
| 235 | nbb = flushrect; |
| 236 | combinerect(&nbb, r); |
| 237 | ar = Dx(r)*Dy(r); |
| 238 | abb = Dx(flushrect)*Dy(flushrect); |
| 239 | anbb = Dx(nbb)*Dy(nbb); |
| 240 | /* |
| 241 | * Area of new waste is area of new bb minus area of old bb, |
| 242 | * less the area of the new segment, which we assume is not waste. |
| 243 | * This could be negative, but that's OK. |
| 244 | */ |
| 245 | waste += anbb-abb - ar; |
| 246 | if(waste < 0) |
| 247 | waste = 0; |
| 248 | /* |
| 249 | * absorb if: |
| 250 | * total area is small |
| 251 | * waste is less than half total area |
| 252 | * rectangles touch |
| 253 | */ |
| 254 | if(anbb<=1024 || waste*2<anbb || rectXrect(flushrect, r)){ |
| 255 | flushrect = nbb; |
| 256 | return; |
| 257 | } |
| 258 | /* emit current state */ |
| 259 | if(flushrect.min.x < flushrect.max.x) |
| 260 | flushmemscreen(flushrect); |
| 261 | flushrect = r; |
| 262 | waste = 0; |
| 263 | } |
| 264 | |
| 265 | static |
| 266 | void |
| 267 | dstflush(int dstid, Memimage *dst, Rectangle r) |
| 268 | { |
| 269 | Memlayer *l; |
| 270 | |
| 271 | if(dstid == 0){ |
| 272 | combinerect(&flushrect, r); |
| 273 | return; |
| 274 | } |
| 275 | /* how can this happen? -rsc, dec 12 2002 */ |
| 276 | if(dst == 0){ |
| 277 | print("nil dstflush\n"); |
| 278 | return; |
| 279 | } |
| 280 | l = dst->layer; |
| 281 | if(l == nil) |
| 282 | return; |
| 283 | do{ |
| 284 | if(l->screen->image->data != screenimage->data) |
| 285 | return; |
| 286 | r = rectaddpt(r, l->delta); |
| 287 | l = l->screen->image->layer; |
| 288 | }while(l); |
| 289 | addflush(r); |
| 290 | } |
| 291 | |
| 292 | static |
| 293 | void |
| 294 | drawflush(void) |
| 295 | { |
| 296 | if(flushrect.min.x < flushrect.max.x) |
| 297 | flushmemscreen(flushrect); |
| 298 | flushrect = Rect(10000, 10000, -10000, -10000); |
| 299 | } |
| 300 | |
| 301 | static |
| 302 | int |
| 303 | drawcmp(char *a, char *b, int n) |
| 304 | { |
| 305 | if(strlen(a) != n) |
| 306 | return 1; |
| 307 | return memcmp(a, b, n); |
| 308 | } |
| 309 | |
| 310 | static |
| 311 | DName* |
| 312 | drawlookupname(int n, char *str) |
| 313 | { |
| 314 | DName *name, *ename; |
| 315 | |
| 316 | name = sdraw.name; |
| 317 | ename = &name[sdraw.nname]; |
| 318 | for(; name<ename; name++) |
| 319 | if(drawcmp(name->name, str, n) == 0) |
| 320 | return name; |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static |
| 325 | int |
| 326 | drawgoodname(DImage *d) |
| 327 | { |
| 328 | DName *n; |
| 329 | |
| 330 | /* if window, validate the screen's own images */ |
| 331 | if(d->dscreen) |
| 332 | if(drawgoodname(d->dscreen->dimage) == 0 |
| 333 | || drawgoodname(d->dscreen->dfill) == 0) |
| 334 | return 0; |
| 335 | if(d->name == nil) |
| 336 | return 1; |
| 337 | n = drawlookupname(strlen(d->name), d->name); |
| 338 | if(n==nil || n->vers!=d->vers) |
| 339 | return 0; |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | static |
| 344 | DImage* |
| 345 | drawlookup(Client *client, int id, int checkname) |
| 346 | { |
| 347 | DImage *d; |
| 348 | |
| 349 | d = client->dimage[id&HASHMASK]; |
| 350 | while(d){ |
| 351 | if(d->id == id){ |
| 352 | /* |
| 353 | * BUG: should error out but too hard. |
| 354 | * Return 0 instead. |
| 355 | */ |
| 356 | if(checkname && !drawgoodname(d)) |
| 357 | return 0; |
| 358 | return d; |
| 359 | } |
| 360 | d = d->next; |
| 361 | } |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static |
| 366 | DScreen* |
| 367 | drawlookupdscreen(int id) |
| 368 | { |
| 369 | DScreen *s; |
| 370 | |
| 371 | s = dscreen; |
| 372 | while(s){ |
| 373 | if(s->id == id) |
| 374 | return s; |
| 375 | s = s->next; |
| 376 | } |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static |
| 381 | DScreen* |
| 382 | drawlookupscreen(Client *client, int id, CScreen **cs) |
| 383 | { |
| 384 | CScreen *s; |
| 385 | |
| 386 | s = client->cscreen; |
| 387 | while(s){ |
| 388 | if(s->dscreen->id == id){ |
| 389 | *cs = s; |
| 390 | return s->dscreen; |
| 391 | } |
| 392 | s = s->next; |
| 393 | } |
| 394 | /* caller must check! */ |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static |
| 399 | Memimage* |
| 400 | drawinstall(Client *client, int id, Memimage *i, DScreen *dscreen) |
| 401 | { |
| 402 | DImage *d; |
| 403 | |
| 404 | d = mallocz(sizeof(DImage), 1); |
| 405 | if(d == 0) |
| 406 | return 0; |
| 407 | d->id = id; |
| 408 | d->ref = 1; |
| 409 | d->name = 0; |
| 410 | d->vers = 0; |
| 411 | d->image = i; |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 412 | if(i->screenref) |
| 413 | ++i->screenref; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 414 | d->nfchar = 0; |
| 415 | d->fchar = 0; |
| 416 | d->fromname = 0; |
| 417 | d->dscreen = dscreen; |
| 418 | d->next = client->dimage[id&HASHMASK]; |
| 419 | client->dimage[id&HASHMASK] = d; |
| 420 | return i; |
| 421 | } |
| 422 | |
| 423 | static |
| 424 | Memscreen* |
| 425 | drawinstallscreen(Client *client, DScreen *d, int id, DImage *dimage, DImage *dfill, int public) |
| 426 | { |
| 427 | Memscreen *s; |
| 428 | CScreen *c; |
| 429 | |
| 430 | c = mallocz(sizeof(CScreen), 1); |
| 431 | if(dimage && dimage->image && dimage->image->chan == 0){ |
| 432 | print("bad image %p in drawinstallscreen", dimage->image); |
| 433 | abort(); |
| 434 | } |
| 435 | |
| 436 | if(c == 0) |
| 437 | return 0; |
| 438 | if(d == 0){ |
| 439 | d = mallocz(sizeof(DScreen), 1); |
| 440 | if(d == 0){ |
| 441 | free(c); |
| 442 | return 0; |
| 443 | } |
| 444 | s = mallocz(sizeof(Memscreen), 1); |
| 445 | if(s == 0){ |
| 446 | free(c); |
| 447 | free(d); |
| 448 | return 0; |
| 449 | } |
| 450 | s->frontmost = 0; |
| 451 | s->rearmost = 0; |
| 452 | d->dimage = dimage; |
| 453 | if(dimage){ |
| 454 | s->image = dimage->image; |
| 455 | dimage->ref++; |
| 456 | } |
| 457 | d->dfill = dfill; |
| 458 | if(dfill){ |
| 459 | s->fill = dfill->image; |
| 460 | dfill->ref++; |
| 461 | } |
| 462 | d->ref = 0; |
| 463 | d->id = id; |
| 464 | d->screen = s; |
| 465 | d->public = public; |
| 466 | d->next = dscreen; |
| 467 | d->owner = client; |
| 468 | dscreen = d; |
| 469 | } |
| 470 | c->dscreen = d; |
| 471 | d->ref++; |
| 472 | c->next = client->cscreen; |
| 473 | client->cscreen = c; |
| 474 | return d->screen; |
| 475 | } |
| 476 | |
| 477 | static |
| 478 | void |
| 479 | drawdelname(DName *name) |
| 480 | { |
| 481 | int i; |
| 482 | |
| 483 | i = name-sdraw.name; |
| 484 | memmove(name, name+1, (sdraw.nname-(i+1))*sizeof(DName)); |
| 485 | sdraw.nname--; |
| 486 | } |
| 487 | |
| 488 | static |
| 489 | void |
| 490 | drawfreedscreen(DScreen *this) |
| 491 | { |
| 492 | DScreen *ds, *next; |
| 493 | |
| 494 | this->ref--; |
| 495 | if(this->ref < 0) |
| 496 | print("negative ref in drawfreedscreen\n"); |
| 497 | if(this->ref > 0) |
| 498 | return; |
| 499 | ds = dscreen; |
| 500 | if(ds == this){ |
| 501 | dscreen = this->next; |
| 502 | goto Found; |
| 503 | } |
| 504 | while(next = ds->next){ /* assign = */ |
| 505 | if(next == this){ |
| 506 | ds->next = this->next; |
| 507 | goto Found; |
| 508 | } |
| 509 | ds = next; |
| 510 | } |
| 511 | /* |
| 512 | * Should signal Enodrawimage, but too hard. |
| 513 | */ |
| 514 | return; |
| 515 | |
| 516 | Found: |
| 517 | if(this->dimage) |
| 518 | drawfreedimage(this->dimage); |
| 519 | if(this->dfill) |
| 520 | drawfreedimage(this->dfill); |
| 521 | free(this->screen); |
| 522 | free(this); |
| 523 | } |
| 524 | |
| 525 | static |
| 526 | void |
| 527 | drawfreedimage(DImage *dimage) |
| 528 | { |
| 529 | int i; |
| 530 | Memimage *l; |
| 531 | DScreen *ds; |
| 532 | |
| 533 | dimage->ref--; |
| 534 | if(dimage->ref < 0) |
| 535 | print("negative ref in drawfreedimage\n"); |
| 536 | if(dimage->ref > 0) |
| 537 | return; |
| 538 | |
| 539 | /* any names? */ |
| 540 | for(i=0; i<sdraw.nname; ) |
| 541 | if(sdraw.name[i].dimage == dimage) |
| 542 | drawdelname(sdraw.name+i); |
| 543 | else |
| 544 | i++; |
| 545 | if(dimage->fromname){ /* acquired by name; owned by someone else*/ |
| 546 | drawfreedimage(dimage->fromname); |
| 547 | goto Return; |
| 548 | } |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 549 | ds = dimage->dscreen; |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 550 | l = dimage->image; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 551 | if(ds){ |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 552 | if(l->data == screenimage->data) |
| 553 | addflush(l->layer->screenr); |
| 554 | if(l->layer->refreshfn == drawrefresh) /* else true owner will clean up */ |
| 555 | free(l->layer->refreshptr); |
| 556 | l->layer->refreshptr = nil; |
| 557 | if(drawgoodname(dimage)) |
| 558 | memldelete(l); |
| 559 | else |
| 560 | memlfree(l); |
| 561 | drawfreedscreen(ds); |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 562 | }else{ |
| 563 | if(l->screenref==0) |
| 564 | freememimage(l); |
| 565 | else if(--l->screenref==0) |
| 566 | _freememimage(l); |
| 567 | } |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 568 | Return: |
| 569 | free(dimage->fchar); |
| 570 | free(dimage); |
| 571 | } |
| 572 | |
| 573 | static |
| 574 | void |
| 575 | drawuninstallscreen(Client *client, CScreen *this) |
| 576 | { |
| 577 | CScreen *cs, *next; |
| 578 | |
| 579 | cs = client->cscreen; |
| 580 | if(cs == this){ |
| 581 | client->cscreen = this->next; |
| 582 | drawfreedscreen(this->dscreen); |
| 583 | free(this); |
| 584 | return; |
| 585 | } |
| 586 | while(next = cs->next){ /* assign = */ |
| 587 | if(next == this){ |
| 588 | cs->next = this->next; |
| 589 | drawfreedscreen(this->dscreen); |
| 590 | free(this); |
| 591 | return; |
| 592 | } |
| 593 | cs = next; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | static |
| 598 | int |
| 599 | drawuninstall(Client *client, int id) |
| 600 | { |
| 601 | DImage *d, *next; |
| 602 | |
| 603 | d = client->dimage[id&HASHMASK]; |
| 604 | if(d == 0) |
| 605 | return -1; |
| 606 | if(d->id == id){ |
| 607 | client->dimage[id&HASHMASK] = d->next; |
| 608 | drawfreedimage(d); |
| 609 | return 0; |
| 610 | } |
| 611 | while(next = d->next){ /* assign = */ |
| 612 | if(next->id == id){ |
| 613 | d->next = next->next; |
| 614 | drawfreedimage(next); |
| 615 | return 0; |
| 616 | } |
| 617 | d = next; |
| 618 | } |
| 619 | return -1; |
| 620 | } |
| 621 | |
| 622 | static |
| 623 | int |
| 624 | drawaddname(Client *client, DImage *di, int n, char *str, char **err) |
| 625 | { |
| 626 | DName *name, *ename, *new, *t; |
| 627 | char *ns; |
| 628 | |
| 629 | name = sdraw.name; |
| 630 | ename = &name[sdraw.nname]; |
| 631 | for(; name<ename; name++) |
| 632 | if(drawcmp(name->name, str, n) == 0){ |
| 633 | *err = "image name in use"; |
| 634 | return -1; |
| 635 | } |
| 636 | t = mallocz((sdraw.nname+1)*sizeof(DName), 1); |
| 637 | ns = malloc(n+1); |
| 638 | if(t == nil || ns == nil){ |
| 639 | free(t); |
| 640 | free(ns); |
| 641 | *err = "out of memory"; |
| 642 | return -1; |
| 643 | } |
| 644 | memmove(t, sdraw.name, sdraw.nname*sizeof(DName)); |
| 645 | free(sdraw.name); |
| 646 | sdraw.name = t; |
| 647 | new = &sdraw.name[sdraw.nname++]; |
| 648 | new->name = ns; |
| 649 | memmove(new->name, str, n); |
| 650 | new->name[n] = 0; |
| 651 | new->dimage = di; |
| 652 | new->client = client; |
| 653 | new->vers = ++sdraw.vers; |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int |
| 658 | drawclientop(Client *cl) |
| 659 | { |
| 660 | int op; |
| 661 | |
| 662 | op = cl->op; |
| 663 | cl->op = SoverD; |
| 664 | return op; |
| 665 | } |
| 666 | |
| 667 | static |
| 668 | Memimage* |
| 669 | drawimage(Client *client, uchar *a) |
| 670 | { |
| 671 | DImage *d; |
| 672 | |
| 673 | d = drawlookup(client, BGLONG(a), 1); |
| 674 | if(d == nil) |
| 675 | return nil; /* caller must check! */ |
| 676 | return d->image; |
| 677 | } |
| 678 | |
| 679 | static |
| 680 | void |
| 681 | drawrectangle(Rectangle *r, uchar *a) |
| 682 | { |
| 683 | r->min.x = BGLONG(a+0*4); |
| 684 | r->min.y = BGLONG(a+1*4); |
| 685 | r->max.x = BGLONG(a+2*4); |
| 686 | r->max.y = BGLONG(a+3*4); |
| 687 | } |
| 688 | |
| 689 | static |
| 690 | void |
| 691 | drawpoint(Point *p, uchar *a) |
| 692 | { |
| 693 | p->x = BGLONG(a+0*4); |
| 694 | p->y = BGLONG(a+1*4); |
| 695 | } |
| 696 | |
| 697 | static |
| 698 | Point |
| 699 | drawchar(Memimage *dst, Point p, Memimage *src, Point *sp, DImage *font, int index, int op) |
| 700 | { |
| 701 | FChar *fc; |
| 702 | Rectangle r; |
| 703 | Point sp1; |
| 704 | |
| 705 | fc = &font->fchar[index]; |
| 706 | r.min.x = p.x+fc->left; |
| 707 | r.min.y = p.y-(font->ascent-fc->miny); |
| 708 | r.max.x = r.min.x+(fc->maxx-fc->minx); |
| 709 | r.max.y = r.min.y+(fc->maxy-fc->miny); |
| 710 | sp1.x = sp->x+fc->left; |
| 711 | sp1.y = sp->y+fc->miny; |
| 712 | memdraw(dst, r, src, sp1, font->image, Pt(fc->minx, fc->miny), op); |
| 713 | p.x += fc->width; |
| 714 | sp->x += fc->width; |
| 715 | return p; |
| 716 | } |
| 717 | |
| 718 | static |
| 719 | uchar* |
| 720 | drawcoord(uchar *p, uchar *maxp, int oldx, int *newx) |
| 721 | { |
| 722 | int b, x; |
| 723 | |
| 724 | if(p >= maxp) |
| 725 | return nil; |
| 726 | b = *p++; |
| 727 | x = b & 0x7F; |
| 728 | if(b & 0x80){ |
| 729 | if(p+1 >= maxp) |
| 730 | return nil; |
| 731 | x |= *p++ << 7; |
| 732 | x |= *p++ << 15; |
| 733 | if(x & (1<<22)) |
| 734 | x |= ~0<<23; |
| 735 | }else{ |
| 736 | if(b & 0x40) |
| 737 | x |= ~0<<7; |
| 738 | x += oldx; |
| 739 | } |
| 740 | *newx = x; |
| 741 | return p; |
| 742 | } |
| 743 | |
| 744 | int |
| 745 | _drawmsgread(Display *d, void *a, int n) |
| 746 | { |
rsc | e543c47 | 2004-04-19 05:56:17 +0000 | [diff] [blame] | 747 | Client *cl; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 748 | |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 749 | qlock(&sdraw.lk); |
rsc | e543c47 | 2004-04-19 05:56:17 +0000 | [diff] [blame] | 750 | cl = client0; |
| 751 | if(cl->readdata == nil){ |
| 752 | werrstr("no draw data"); |
| 753 | goto err; |
| 754 | } |
| 755 | if(n < cl->nreaddata){ |
| 756 | werrstr("short read"); |
| 757 | goto err; |
| 758 | } |
| 759 | n = cl->nreaddata; |
| 760 | memmove(a, cl->readdata, cl->nreaddata); |
| 761 | free(cl->readdata); |
| 762 | cl->readdata = nil; |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 763 | qunlock(&sdraw.lk); |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 764 | return n; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 765 | |
rsc | e543c47 | 2004-04-19 05:56:17 +0000 | [diff] [blame] | 766 | err: |
| 767 | qunlock(&sdraw.lk); |
| 768 | return -1; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | int |
| 772 | _drawmsgwrite(Display *d, void *v, int n) |
| 773 | { |
| 774 | char cbuf[40], *err, ibuf[12*12+1], *s; |
| 775 | int c, ci, doflush, dstid, e0, e1, esize, j, m; |
| 776 | int ni, nw, oesize, oldn, op, ox, oy, repl, scrnid, y; |
| 777 | uchar *a, refresh, *u; |
| 778 | u32int chan, value; |
| 779 | Client *client; |
| 780 | CScreen *cs; |
| 781 | DImage *di, *ddst, *dsrc, *font, *ll; |
| 782 | DName *dn; |
| 783 | DScreen *dscrn; |
| 784 | FChar *fc; |
| 785 | Memimage *dst, *i, *l, **lp, *mask, *src; |
| 786 | Memscreen *scrn; |
| 787 | Point p, *pp, q, sp; |
| 788 | Rectangle clipr, r; |
| 789 | Refreshfn reffn; |
| 790 | Refx *refx; |
| 791 | |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 792 | qlock(&sdraw.lk); |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 793 | d->obufp = d->obuf; |
| 794 | a = v; |
| 795 | m = 0; |
| 796 | oldn = n; |
| 797 | client = client0; |
| 798 | |
| 799 | while((n-=m) > 0){ |
| 800 | a += m; |
| 801 | //fprint(2, "msgwrite %d(%d)...", n, *a); |
| 802 | switch(*a){ |
| 803 | default: |
| 804 | //fprint(2, "bad command %d\n", *a); |
| 805 | err = "bad draw command"; |
| 806 | goto error; |
| 807 | |
| 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; |
| 833 | } |
rsc | be22ae2 | 2004-03-26 01:59:35 +0000 | [diff] [blame] | 834 | reffn = 0; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 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; |
| 847 | } |
| 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; |
| 857 | } |
| 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; |
| 867 | } |
| 868 | refx->client = client; |
| 869 | refx->dimage = drawlookup(client, dstid, 1); |
| 870 | } |
| 871 | memlsetrefresh(l, reffn, refx); |
| 872 | } |
| 873 | continue; |
| 874 | } |
| 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; |
| 886 | } |
| 887 | memfillcolor(i, value); |
| 888 | continue; |
| 889 | |
| 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; |
| 907 | |
| 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; |
| 919 | } |
| 920 | dst = ddst->image; |
| 921 | if(a[5]) |
| 922 | dst->flags |= Frepl; |
| 923 | drawrectangle(&dst->clipr, a+6); |
| 924 | continue; |
| 925 | |
| 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; |
| 944 | |
| 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; |
| 952 | |
| 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; |
| 970 | } |
| 971 | j = BGLONG(a+25); |
| 972 | if(j < 0){ |
| 973 | err = "negative ellipse thickness"; |
| 974 | goto error; |
| 975 | } |
| 976 | |
| 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 */ |
rsc | be22ae2 | 2004-03-26 01:59:35 +0000 | [diff] [blame] | 985 | if(ox & ((ulong)1<<31)){ |
| 986 | if((ox & ((ulong)1<<30)) == 0) |
| 987 | ox &= ~((ulong)1<<31); |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 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; |
| 993 | |
| 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; |
| 1005 | |
| 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; |
| 1015 | |
| 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; |
| 1025 | } |
| 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; |
| 1032 | } |
| 1033 | ni = BGLONG(a+5); |
| 1034 | if(ni<=0 || ni>4096){ |
| 1035 | err = "bad font size (4096 chars max)"; |
| 1036 | goto error; |
| 1037 | } |
| 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; |
| 1043 | } |
| 1044 | memset(font->fchar, 0, ni*sizeof(FChar)); |
| 1045 | font->nfchar = ni; |
| 1046 | font->ascent = a[9]; |
| 1047 | continue; |
| 1048 | |
| 1049 | /* set image 0 to screen image */ |
| 1050 | case 'J': |
| 1051 | m = 1; |
| 1052 | if(n < m) |
| 1053 | goto Eshortdraw; |
rsc | 6325e03 | 2004-03-26 17:06:55 +0000 | [diff] [blame] | 1054 | if(drawlookup(client, 0, 0)) |
| 1055 | goto Eimageexists; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 1056 | drawinstall(client, 0, screenimage, 0); |
| 1057 | client->infoid = 0; |
| 1058 | continue; |
| 1059 | |
| 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; |
| 1076 | } |
| 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); |
rsc | e543c47 | 2004-04-19 05:56:17 +0000 | [diff] [blame] | 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; |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 1092 | client->infoid = -1; |
| 1093 | continue; |
| 1094 | |
| 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; |
| 1122 | |
| 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; |
| 1138 | } |
| 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))); |
| 1150 | } |
| 1151 | continue; |
| 1152 | |
| 1153 | /* create image mask: 'm' newid[4] id[4] */ |
| 1154 | /* |
| 1155 | * |
| 1156 | case 'm': |
| 1157 | m = 4+4; |
| 1158 | if(n < m) |
| 1159 | goto Eshortdraw; |
| 1160 | break; |
| 1161 | * |
| 1162 | */ |
| 1163 | |
| 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; |
| 1197 | |
| 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); |
| 1225 | } |
| 1226 | continue; |
| 1227 | |
| 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; |
| 1244 | } |
| 1245 | if(ni > 0){ |
| 1246 | addflush(r); |
| 1247 | addflush(dst->layer->screenr); |
| 1248 | ll = drawlookup(client, BGLONG(a+1), 1); |
| 1249 | drawrefreshscreen(ll, client); |
| 1250 | } |
| 1251 | } |
| 1252 | continue; |
| 1253 | |
| 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; |
| 1261 | |
| 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; |
| 1275 | } |
| 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; |
| 1284 | } |
| 1285 | } |
| 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; |
| 1319 | } |
| 1320 | if(y == ni-1){ |
| 1321 | c = memlineendsize(e1); |
| 1322 | if(c > esize) |
| 1323 | esize = c; |
| 1324 | } |
| 1325 | } |
| 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)); |
| 1331 | } |
| 1332 | if(rectclip(&r, dst->clipr)) /* should perhaps be an arg to dstflush */ |
| 1333 | dstflush(dstid, dst, r); |
| 1334 | } |
| 1335 | pp[y] = p; |
| 1336 | } |
| 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; |
| 1347 | |
| 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; |
| 1366 | } |
| 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; |
| 1373 | } |
| 1374 | continue; |
| 1375 | |
| 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; |
| 1385 | |
| 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; |
| 1423 | } |
| 1424 | r.max.x += font->fchar[ci].width; |
| 1425 | u += 2; |
| 1426 | } |
| 1427 | memdraw(dst, r, l, q, memopaque, ZP, op); |
| 1428 | u -= 2*ni; |
| 1429 | } |
| 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; |
| 1436 | } |
| 1437 | q = drawchar(dst, q, src, &sp, font, ci, op); |
| 1438 | u += 2; |
| 1439 | } |
| 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; |
| 1444 | |
| 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; |
| 1459 | } |
| 1460 | if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0) |
| 1461 | goto Edrawmem; |
| 1462 | continue; |
| 1463 | |
| 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; |
| 1485 | } |
| 1486 | } |
| 1487 | if(lp[0]->layer == 0){ |
| 1488 | err = "images are not windows"; |
| 1489 | free(lp); |
| 1490 | goto error; |
| 1491 | } |
| 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; |
| 1497 | } |
| 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; |
| 1509 | |
| 1510 | /* visible: 'v' */ |
| 1511 | case 'v': |
| 1512 | m = 1; |
| 1513 | drawflush(); |
| 1514 | continue; |
| 1515 | |
| 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; |
| 1534 | } |
| 1535 | dstflush(dstid, dst, r); |
| 1536 | m += y; |
| 1537 | continue; |
| 1538 | } |
| 1539 | } |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 1540 | qunlock(&sdraw.lk); |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 1541 | return oldn - n; |
| 1542 | |
| 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; |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 1552 | /* |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 1553 | Eshortread: |
| 1554 | err = "draw read too short"; |
| 1555 | goto error; |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 1556 | */ |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 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; |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 1578 | /* |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 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; |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 1588 | */ |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 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; |
| 1607 | |
| 1608 | error: |
rsc | 161060a | 2003-10-11 02:47:43 +0000 | [diff] [blame] | 1609 | werrstr("%s", err); |
| 1610 | qunlock(&sdraw.lk); |
rsc | 76193d7 | 2003-09-30 17:47:42 +0000 | [diff] [blame] | 1611 | return -1; |
| 1612 | } |
| 1613 | |
| 1614 | |