PageRenderTime 34ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/sys/src/9/port/devdraw.c

https://bitbucket.org/rminnich/sysfromiso
C | 2203 lines | 1934 code | 162 blank | 107 comment | 445 complexity | e30a745c16f47c606d38b33a9e340f1f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, Unlicense
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. #define Image IMAGE
  8. #include <draw.h>
  9. #include <memdraw.h>
  10. #include <memlayer.h>
  11. #include <cursor.h>
  12. #include "screen.h"
  13. enum
  14. {
  15. Qtopdir = 0,
  16. Qnew,
  17. Qwinname,
  18. Q3rd,
  19. Q2nd,
  20. Qcolormap,
  21. Qctl,
  22. Qdata,
  23. Qrefresh,
  24. };
  25. /*
  26. * Qid path is:
  27. * 4 bits of file type (qids above)
  28. * 24 bits of mux slot number +1; 0 means not attached to client
  29. */
  30. #define QSHIFT 4 /* location in qid of client # */
  31. #define QID(q) ((((ulong)(q).path)&0x0000000F)>>0)
  32. #define CLIENTPATH(q) ((((ulong)q)&0x7FFFFFF0)>>QSHIFT)
  33. #define CLIENT(q) CLIENTPATH((q).path)
  34. #define NHASH (1<<5)
  35. #define HASHMASK (NHASH-1)
  36. #define IOUNIT (64*1024)
  37. typedef struct Client Client;
  38. typedef struct Draw Draw;
  39. typedef struct DImage DImage;
  40. typedef struct DScreen DScreen;
  41. typedef struct CScreen CScreen;
  42. typedef struct FChar FChar;
  43. typedef struct Refresh Refresh;
  44. typedef struct Refx Refx;
  45. typedef struct DName DName;
  46. ulong blanktime = 30; /* in minutes; a half hour */
  47. struct Draw
  48. {
  49. int clientid;
  50. int nclient;
  51. Client** client;
  52. int nname;
  53. DName* name;
  54. int vers;
  55. int softscreen;
  56. int blanked; /* screen turned off */
  57. ulong blanktime; /* time of last operation */
  58. ulong savemap[3*256];
  59. };
  60. struct Client
  61. {
  62. Ref r;
  63. DImage* dimage[NHASH];
  64. CScreen* cscreen;
  65. Refresh* refresh;
  66. Rendez refrend;
  67. uchar* readdata;
  68. int nreaddata;
  69. int busy;
  70. int clientid;
  71. int slot;
  72. int refreshme;
  73. int infoid;
  74. int op;
  75. };
  76. struct Refresh
  77. {
  78. DImage* dimage;
  79. Rectangle r;
  80. Refresh* next;
  81. };
  82. struct Refx
  83. {
  84. Client* client;
  85. DImage* dimage;
  86. };
  87. struct DName
  88. {
  89. char *name;
  90. Client *client;
  91. DImage* dimage;
  92. int vers;
  93. };
  94. struct FChar
  95. {
  96. int minx; /* left edge of bits */
  97. int maxx; /* right edge of bits */
  98. uchar miny; /* first non-zero scan-line */
  99. uchar maxy; /* last non-zero scan-line + 1 */
  100. schar left; /* offset of baseline */
  101. uchar width; /* width of baseline */
  102. };
  103. /*
  104. * Reference counts in DImages:
  105. * one per open by original client
  106. * one per screen image or fill
  107. * one per image derived from this one by name
  108. */
  109. struct DImage
  110. {
  111. int id;
  112. int ref;
  113. char *name;
  114. int vers;
  115. Memimage* image;
  116. int ascent;
  117. int nfchar;
  118. FChar* fchar;
  119. DScreen* dscreen; /* 0 if not a window */
  120. DImage* fromname; /* image this one is derived from, by name */
  121. DImage* next;
  122. };
  123. struct CScreen
  124. {
  125. DScreen* dscreen;
  126. CScreen* next;
  127. };
  128. struct DScreen
  129. {
  130. int id;
  131. int public;
  132. int ref;
  133. DImage *dimage;
  134. DImage *dfill;
  135. Memscreen* screen;
  136. Client* owner;
  137. DScreen* next;
  138. };
  139. static Draw sdraw;
  140. QLock drawlock;
  141. static Memimage *screenimage;
  142. static DImage* screendimage;
  143. static char screenname[40];
  144. static int screennameid;
  145. static Rectangle flushrect;
  146. static int waste;
  147. static DScreen* dscreen;
  148. extern void flushmemscreen(Rectangle);
  149. void drawmesg(Client*, void*, int);
  150. void drawuninstall(Client*, int);
  151. void drawfreedimage(DImage*);
  152. Client* drawclientofpath(ulong);
  153. DImage* allocdimage(Memimage*);
  154. static char Enodrawimage[] = "unknown id for draw image";
  155. static char Enodrawscreen[] = "unknown id for draw screen";
  156. static char Eshortdraw[] = "short draw message";
  157. static char Eshortread[] = "draw read too short";
  158. static char Eimageexists[] = "image id in use";
  159. static char Escreenexists[] = "screen id in use";
  160. static char Edrawmem[] = "image memory allocation failed";
  161. static char Ereadoutside[] = "readimage outside image";
  162. static char Ewriteoutside[] = "writeimage outside image";
  163. static char Enotfont[] = "image not a font";
  164. static char Eindex[] = "character index out of range";
  165. static char Enoclient[] = "no such draw client";
  166. static char Edepth[] = "image has bad depth";
  167. static char Enameused[] = "image name in use";
  168. static char Enoname[] = "no image with that name";
  169. static char Eoldname[] = "named image no longer valid";
  170. static char Enamed[] = "image already has name";
  171. static char Ewrongname[] = "wrong name for image";
  172. static void
  173. dlock(void)
  174. {
  175. qlock(&drawlock);
  176. }
  177. static int
  178. candlock(void)
  179. {
  180. return canqlock(&drawlock);
  181. }
  182. static void
  183. dunlock(void)
  184. {
  185. qunlock(&drawlock);
  186. }
  187. static int
  188. drawgen(Chan *c, char*, Dirtab*, int, int s, Dir *dp)
  189. {
  190. int t;
  191. Qid q;
  192. ulong path;
  193. Client *cl;
  194. q.vers = 0;
  195. if(s == DEVDOTDOT){
  196. switch(QID(c->qid)){
  197. case Qtopdir:
  198. case Q2nd:
  199. mkqid(&q, Qtopdir, 0, QTDIR);
  200. devdir(c, q, "#i", 0, eve, 0500, dp);
  201. break;
  202. case Q3rd:
  203. cl = drawclientofpath(c->qid.path);
  204. if(cl == nil)
  205. strcpy(up->genbuf, "??");
  206. else
  207. sprint(up->genbuf, "%d", cl->clientid);
  208. mkqid(&q, Q2nd, 0, QTDIR);
  209. devdir(c, q, up->genbuf, 0, eve, 0500, dp);
  210. break;
  211. default:
  212. panic("drawwalk %llux", c->qid.path);
  213. }
  214. return 1;
  215. }
  216. /*
  217. * Top level directory contains the name of the device.
  218. */
  219. t = QID(c->qid);
  220. if(t == Qtopdir){
  221. switch(s){
  222. case 0:
  223. mkqid(&q, Q2nd, 0, QTDIR);
  224. devdir(c, q, "draw", 0, eve, 0555, dp);
  225. break;
  226. case 1:
  227. mkqid(&q, Qwinname, 0, 0);
  228. devdir(c, q, "winname", 0, eve, 0444, dp);
  229. break;
  230. default:
  231. return -1;
  232. }
  233. return 1;
  234. }
  235. /*
  236. * Second level contains "new" plus all the clients.
  237. */
  238. if(t == Q2nd || t == Qnew){
  239. if(s == 0){
  240. mkqid(&q, Qnew, 0, QTFILE);
  241. devdir(c, q, "new", 0, eve, 0666, dp);
  242. }
  243. else if(s <= sdraw.nclient){
  244. cl = sdraw.client[s-1];
  245. if(cl == 0)
  246. return 0;
  247. sprint(up->genbuf, "%d", cl->clientid);
  248. mkqid(&q, (s<<QSHIFT)|Q3rd, 0, QTDIR);
  249. devdir(c, q, up->genbuf, 0, eve, 0555, dp);
  250. return 1;
  251. }
  252. else
  253. return -1;
  254. return 1;
  255. }
  256. /*
  257. * Third level.
  258. */
  259. path = c->qid.path&~((1<<QSHIFT)-1); /* slot component */
  260. q.vers = c->qid.vers;
  261. q.type = QTFILE;
  262. switch(s){
  263. case 0:
  264. q.path = path|Qcolormap;
  265. devdir(c, q, "colormap", 0, eve, 0600, dp);
  266. break;
  267. case 1:
  268. q.path = path|Qctl;
  269. devdir(c, q, "ctl", 0, eve, 0600, dp);
  270. break;
  271. case 2:
  272. q.path = path|Qdata;
  273. devdir(c, q, "data", 0, eve, 0600, dp);
  274. break;
  275. case 3:
  276. q.path = path|Qrefresh;
  277. devdir(c, q, "refresh", 0, eve, 0400, dp);
  278. break;
  279. default:
  280. return -1;
  281. }
  282. return 1;
  283. }
  284. static
  285. int
  286. drawrefactive(void *a)
  287. {
  288. Client *c;
  289. c = a;
  290. return c->refreshme || c->refresh!=0;
  291. }
  292. static
  293. void
  294. drawrefreshscreen(DImage *l, Client *client)
  295. {
  296. while(l != nil && l->dscreen == nil)
  297. l = l->fromname;
  298. if(l != nil && l->dscreen->owner != client)
  299. l->dscreen->owner->refreshme = 1;
  300. }
  301. static
  302. void
  303. drawrefresh(Memimage*, Rectangle r, void *v)
  304. {
  305. Refx *x;
  306. DImage *d;
  307. Client *c;
  308. Refresh *ref;
  309. if(v == 0)
  310. return;
  311. x = v;
  312. c = x->client;
  313. d = x->dimage;
  314. for(ref=c->refresh; ref; ref=ref->next)
  315. if(ref->dimage == d){
  316. combinerect(&ref->r, r);
  317. return;
  318. }
  319. ref = malloc(sizeof(Refresh));
  320. if(ref){
  321. ref->dimage = d;
  322. ref->r = r;
  323. ref->next = c->refresh;
  324. c->refresh = ref;
  325. }
  326. }
  327. static void
  328. addflush(Rectangle r)
  329. {
  330. int abb, ar, anbb;
  331. Rectangle nbb;
  332. if(sdraw.softscreen==0 || !rectclip(&r, screenimage->r))
  333. return;
  334. if(flushrect.min.x >= flushrect.max.x){
  335. flushrect = r;
  336. waste = 0;
  337. return;
  338. }
  339. nbb = flushrect;
  340. combinerect(&nbb, r);
  341. ar = Dx(r)*Dy(r);
  342. abb = Dx(flushrect)*Dy(flushrect);
  343. anbb = Dx(nbb)*Dy(nbb);
  344. /*
  345. * Area of new waste is area of new bb minus area of old bb,
  346. * less the area of the new segment, which we assume is not waste.
  347. * This could be negative, but that's OK.
  348. */
  349. waste += anbb-abb - ar;
  350. if(waste < 0)
  351. waste = 0;
  352. /*
  353. * absorb if:
  354. * total area is small
  355. * waste is less than half total area
  356. * rectangles touch
  357. */
  358. if(anbb<=1024 || waste*2<anbb || rectXrect(flushrect, r)){
  359. flushrect = nbb;
  360. return;
  361. }
  362. /* emit current state */
  363. if(flushrect.min.x < flushrect.max.x)
  364. flushmemscreen(flushrect);
  365. flushrect = r;
  366. waste = 0;
  367. }
  368. static
  369. void
  370. dstflush(int dstid, Memimage *dst, Rectangle r)
  371. {
  372. Memlayer *l;
  373. if(dstid == 0){
  374. combinerect(&flushrect, r);
  375. return;
  376. }
  377. /* how can this happen? -rsc, dec 12 2002 */
  378. if(dst == 0){
  379. print("nil dstflush\n");
  380. return;
  381. }
  382. l = dst->layer;
  383. if(l == nil)
  384. return;
  385. do{
  386. if(l->screen->image->data != screenimage->data)
  387. return;
  388. r = rectaddpt(r, l->delta);
  389. l = l->screen->image->layer;
  390. }while(l);
  391. addflush(r);
  392. }
  393. void
  394. drawflush(void)
  395. {
  396. if(flushrect.min.x < flushrect.max.x)
  397. flushmemscreen(flushrect);
  398. flushrect = Rect(10000, 10000, -10000, -10000);
  399. }
  400. static
  401. int
  402. drawcmp(char *a, char *b, int n)
  403. {
  404. if(strlen(a) != n)
  405. return 1;
  406. return memcmp(a, b, n);
  407. }
  408. DName*
  409. drawlookupname(int n, char *str)
  410. {
  411. DName *name, *ename;
  412. name = sdraw.name;
  413. ename = &name[sdraw.nname];
  414. for(; name<ename; name++)
  415. if(drawcmp(name->name, str, n) == 0)
  416. return name;
  417. return 0;
  418. }
  419. int
  420. drawgoodname(DImage *d)
  421. {
  422. DName *n;
  423. /* if window, validate the screen's own images */
  424. if(d->dscreen)
  425. if(drawgoodname(d->dscreen->dimage) == 0
  426. || drawgoodname(d->dscreen->dfill) == 0)
  427. return 0;
  428. if(d->name == nil)
  429. return 1;
  430. n = drawlookupname(strlen(d->name), d->name);
  431. if(n==nil || n->vers!=d->vers)
  432. return 0;
  433. return 1;
  434. }
  435. DImage*
  436. drawlookup(Client *client, int id, int checkname)
  437. {
  438. DImage *d;
  439. d = client->dimage[id&HASHMASK];
  440. while(d){
  441. if(d->id == id){
  442. if(checkname && !drawgoodname(d))
  443. error(Eoldname);
  444. return d;
  445. }
  446. d = d->next;
  447. }
  448. return 0;
  449. }
  450. DScreen*
  451. drawlookupdscreen(int id)
  452. {
  453. DScreen *s;
  454. s = dscreen;
  455. while(s){
  456. if(s->id == id)
  457. return s;
  458. s = s->next;
  459. }
  460. return 0;
  461. }
  462. DScreen*
  463. drawlookupscreen(Client *client, int id, CScreen **cs)
  464. {
  465. CScreen *s;
  466. s = client->cscreen;
  467. while(s){
  468. if(s->dscreen->id == id){
  469. *cs = s;
  470. return s->dscreen;
  471. }
  472. s = s->next;
  473. }
  474. error(Enodrawscreen);
  475. return 0;
  476. }
  477. DImage*
  478. allocdimage(Memimage *i)
  479. {
  480. DImage *d;
  481. d = malloc(sizeof(DImage));
  482. if(d == 0)
  483. return 0;
  484. d->ref = 1;
  485. d->name = 0;
  486. d->vers = 0;
  487. d->image = i;
  488. d->nfchar = 0;
  489. d->fchar = 0;
  490. d->fromname = 0;
  491. return d;
  492. }
  493. Memimage*
  494. drawinstall(Client *client, int id, Memimage *i, DScreen *dscreen)
  495. {
  496. DImage *d;
  497. d = allocdimage(i);
  498. if(d == 0)
  499. return 0;
  500. d->id = id;
  501. d->dscreen = dscreen;
  502. d->next = client->dimage[id&HASHMASK];
  503. client->dimage[id&HASHMASK] = d;
  504. return i;
  505. }
  506. Memscreen*
  507. drawinstallscreen(Client *client, DScreen *d, int id, DImage *dimage, DImage *dfill, int public)
  508. {
  509. Memscreen *s;
  510. CScreen *c;
  511. c = malloc(sizeof(CScreen));
  512. if(dimage && dimage->image && dimage->image->chan == 0)
  513. panic("bad image %p in drawinstallscreen", dimage->image);
  514. if(c == 0)
  515. return 0;
  516. if(d == 0){
  517. d = malloc(sizeof(DScreen));
  518. if(d == 0){
  519. free(c);
  520. return 0;
  521. }
  522. s = malloc(sizeof(Memscreen));
  523. if(s == 0){
  524. free(c);
  525. free(d);
  526. return 0;
  527. }
  528. s->frontmost = 0;
  529. s->rearmost = 0;
  530. d->dimage = dimage;
  531. if(dimage){
  532. s->image = dimage->image;
  533. dimage->ref++;
  534. }
  535. d->dfill = dfill;
  536. if(dfill){
  537. s->fill = dfill->image;
  538. dfill->ref++;
  539. }
  540. d->ref = 0;
  541. d->id = id;
  542. d->screen = s;
  543. d->public = public;
  544. d->next = dscreen;
  545. d->owner = client;
  546. dscreen = d;
  547. }
  548. c->dscreen = d;
  549. d->ref++;
  550. c->next = client->cscreen;
  551. client->cscreen = c;
  552. return d->screen;
  553. }
  554. void
  555. drawdelname(DName *name)
  556. {
  557. int i;
  558. i = name-sdraw.name;
  559. memmove(name, name+1, (sdraw.nname-(i+1))*sizeof(DName));
  560. sdraw.nname--;
  561. }
  562. void
  563. drawfreedscreen(DScreen *this)
  564. {
  565. DScreen *ds, *next;
  566. this->ref--;
  567. if(this->ref < 0)
  568. print("negative ref in drawfreedscreen\n");
  569. if(this->ref > 0)
  570. return;
  571. ds = dscreen;
  572. if(ds == this){
  573. dscreen = this->next;
  574. goto Found;
  575. }
  576. while(next = ds->next){ /* assign = */
  577. if(next == this){
  578. ds->next = this->next;
  579. goto Found;
  580. }
  581. ds = next;
  582. }
  583. error(Enodrawimage);
  584. Found:
  585. if(this->dimage)
  586. drawfreedimage(this->dimage);
  587. if(this->dfill)
  588. drawfreedimage(this->dfill);
  589. free(this->screen);
  590. free(this);
  591. }
  592. void
  593. drawfreedimage(DImage *dimage)
  594. {
  595. int i;
  596. Memimage *l;
  597. DScreen *ds;
  598. dimage->ref--;
  599. if(dimage->ref < 0)
  600. print("negative ref in drawfreedimage\n");
  601. if(dimage->ref > 0)
  602. return;
  603. /* any names? */
  604. for(i=0; i<sdraw.nname; )
  605. if(sdraw.name[i].dimage == dimage)
  606. drawdelname(sdraw.name+i);
  607. else
  608. i++;
  609. if(dimage->fromname){ /* acquired by name; owned by someone else*/
  610. drawfreedimage(dimage->fromname);
  611. goto Return;
  612. }
  613. // if(dimage->image == screenimage) /* don't free the display */
  614. // goto Return;
  615. ds = dimage->dscreen;
  616. if(ds){
  617. l = dimage->image;
  618. if(l->data == screenimage->data)
  619. addflush(l->layer->screenr);
  620. if(l->layer->refreshfn == drawrefresh) /* else true owner will clean up */
  621. free(l->layer->refreshptr);
  622. l->layer->refreshptr = nil;
  623. if(drawgoodname(dimage))
  624. memldelete(l);
  625. else
  626. memlfree(l);
  627. drawfreedscreen(ds);
  628. }else
  629. freememimage(dimage->image);
  630. Return:
  631. free(dimage->fchar);
  632. free(dimage);
  633. }
  634. void
  635. drawuninstallscreen(Client *client, CScreen *this)
  636. {
  637. CScreen *cs, *next;
  638. cs = client->cscreen;
  639. if(cs == this){
  640. client->cscreen = this->next;
  641. drawfreedscreen(this->dscreen);
  642. free(this);
  643. return;
  644. }
  645. while(next = cs->next){ /* assign = */
  646. if(next == this){
  647. cs->next = this->next;
  648. drawfreedscreen(this->dscreen);
  649. free(this);
  650. return;
  651. }
  652. cs = next;
  653. }
  654. }
  655. void
  656. drawuninstall(Client *client, int id)
  657. {
  658. DImage *d, *next;
  659. d = client->dimage[id&HASHMASK];
  660. if(d == 0)
  661. error(Enodrawimage);
  662. if(d->id == id){
  663. client->dimage[id&HASHMASK] = d->next;
  664. drawfreedimage(d);
  665. return;
  666. }
  667. while(next = d->next){ /* assign = */
  668. if(next->id == id){
  669. d->next = next->next;
  670. drawfreedimage(next);
  671. return;
  672. }
  673. d = next;
  674. }
  675. error(Enodrawimage);
  676. }
  677. void
  678. drawaddname(Client *client, DImage *di, int n, char *str)
  679. {
  680. DName *name, *ename, *new, *t;
  681. name = sdraw.name;
  682. ename = &name[sdraw.nname];
  683. for(; name<ename; name++)
  684. if(drawcmp(name->name, str, n) == 0)
  685. error(Enameused);
  686. t = smalloc((sdraw.nname+1)*sizeof(DName));
  687. memmove(t, sdraw.name, sdraw.nname*sizeof(DName));
  688. free(sdraw.name);
  689. sdraw.name = t;
  690. new = &sdraw.name[sdraw.nname++];
  691. new->name = smalloc(n+1);
  692. memmove(new->name, str, n);
  693. new->name[n] = 0;
  694. new->dimage = di;
  695. new->client = client;
  696. new->vers = ++sdraw.vers;
  697. }
  698. Client*
  699. drawnewclient(void)
  700. {
  701. Client *cl, **cp;
  702. int i;
  703. for(i=0; i<sdraw.nclient; i++){
  704. cl = sdraw.client[i];
  705. if(cl == 0)
  706. break;
  707. }
  708. if(i == sdraw.nclient){
  709. cp = malloc((sdraw.nclient+1)*sizeof(Client*));
  710. if(cp == 0)
  711. return 0;
  712. memmove(cp, sdraw.client, sdraw.nclient*sizeof(Client*));
  713. free(sdraw.client);
  714. sdraw.client = cp;
  715. sdraw.nclient++;
  716. cp[i] = 0;
  717. }
  718. cl = malloc(sizeof(Client));
  719. if(cl == 0)
  720. return 0;
  721. memset(cl, 0, sizeof(Client));
  722. cl->slot = i;
  723. cl->clientid = ++sdraw.clientid;
  724. cl->op = SoverD;
  725. sdraw.client[i] = cl;
  726. return cl;
  727. }
  728. static int
  729. drawclientop(Client *cl)
  730. {
  731. int op;
  732. op = cl->op;
  733. cl->op = SoverD;
  734. return op;
  735. }
  736. int
  737. drawhasclients(void)
  738. {
  739. /*
  740. * if draw has ever been used, we can't resize the frame buffer,
  741. * even if all clients have exited (nclients is cumulative); it's too
  742. * hard to make work.
  743. */
  744. return sdraw.nclient != 0;
  745. }
  746. Client*
  747. drawclientofpath(ulong path)
  748. {
  749. Client *cl;
  750. int slot;
  751. slot = CLIENTPATH(path);
  752. if(slot == 0)
  753. return nil;
  754. cl = sdraw.client[slot-1];
  755. if(cl==0 || cl->clientid==0)
  756. return nil;
  757. return cl;
  758. }
  759. Client*
  760. drawclient(Chan *c)
  761. {
  762. Client *client;
  763. client = drawclientofpath(c->qid.path);
  764. if(client == nil)
  765. error(Enoclient);
  766. return client;
  767. }
  768. Memimage*
  769. drawimage(Client *client, uchar *a)
  770. {
  771. DImage *d;
  772. d = drawlookup(client, BGLONG(a), 1);
  773. if(d == nil)
  774. error(Enodrawimage);
  775. return d->image;
  776. }
  777. void
  778. drawrectangle(Rectangle *r, uchar *a)
  779. {
  780. r->min.x = BGLONG(a+0*4);
  781. r->min.y = BGLONG(a+1*4);
  782. r->max.x = BGLONG(a+2*4);
  783. r->max.y = BGLONG(a+3*4);
  784. }
  785. void
  786. drawpoint(Point *p, uchar *a)
  787. {
  788. p->x = BGLONG(a+0*4);
  789. p->y = BGLONG(a+1*4);
  790. }
  791. Point
  792. drawchar(Memimage *dst, Memimage *rdst, Point p, Memimage *src, Point *sp, DImage *font, int index, int op)
  793. {
  794. FChar *fc;
  795. Rectangle r;
  796. Point sp1;
  797. static Memimage *tmp;
  798. fc = &font->fchar[index];
  799. r.min.x = p.x+fc->left;
  800. r.min.y = p.y-(font->ascent-fc->miny);
  801. r.max.x = r.min.x+(fc->maxx-fc->minx);
  802. r.max.y = r.min.y+(fc->maxy-fc->miny);
  803. sp1.x = sp->x+fc->left;
  804. sp1.y = sp->y+fc->miny;
  805. /*
  806. * If we're drawing greyscale fonts onto a VGA screen,
  807. * it's very costly to read the screen memory to do the
  808. * alpha blending inside memdraw. If this is really a stringbg,
  809. * then rdst is the bg image (in main memory) which we can
  810. * refer to for the underlying dst pixels instead of reading dst
  811. * directly.
  812. */
  813. if(ishwimage(dst) && !ishwimage(rdst) && font->image->depth > 1){
  814. if(tmp == nil || tmp->chan != dst->chan || Dx(tmp->r) < Dx(r) || Dy(tmp->r) < Dy(r)){
  815. if(tmp)
  816. freememimage(tmp);
  817. tmp = allocmemimage(Rect(0,0,Dx(r),Dy(r)), dst->chan);
  818. if(tmp == nil)
  819. goto fallback;
  820. }
  821. memdraw(tmp, Rect(0,0,Dx(r),Dy(r)), rdst, r.min, memopaque, ZP, S);
  822. memdraw(tmp, Rect(0,0,Dx(r),Dy(r)), src, sp1, font->image, Pt(fc->minx, fc->miny), op);
  823. memdraw(dst, r, tmp, ZP, memopaque, ZP, S);
  824. }else{
  825. fallback:
  826. memdraw(dst, r, src, sp1, font->image, Pt(fc->minx, fc->miny), op);
  827. }
  828. p.x += fc->width;
  829. sp->x += fc->width;
  830. return p;
  831. }
  832. static DImage*
  833. makescreenimage(void)
  834. {
  835. int width, depth;
  836. ulong chan;
  837. DImage *di;
  838. Memdata *md;
  839. Memimage *i;
  840. Rectangle r;
  841. md = malloc(sizeof *md);
  842. if(md == nil)
  843. return nil;
  844. md->allocd = 1;
  845. md->base = nil;
  846. md->bdata = attachscreen(&r, &chan, &depth, &width, &sdraw.softscreen);
  847. if(md->bdata == nil){
  848. free(md);
  849. return nil;
  850. }
  851. md->ref = 1;
  852. i = allocmemimaged(r, chan, md);
  853. if(i == nil){
  854. free(md);
  855. return nil;
  856. }
  857. i->width = width;
  858. i->clipr = r;
  859. di = allocdimage(i);
  860. if(di == nil){
  861. freememimage(i); /* frees md */
  862. return nil;
  863. }
  864. if(!waserror()){
  865. snprint(screenname, sizeof screenname, "noborder.screen.%d", ++screennameid);
  866. drawaddname(nil, di, strlen(screenname), screenname);
  867. poperror();
  868. }
  869. return di;
  870. }
  871. static int
  872. initscreenimage(void)
  873. {
  874. if(screenimage != nil)
  875. return 1;
  876. screendimage = makescreenimage();
  877. if(screendimage == nil)
  878. return 0;
  879. screenimage = screendimage->image;
  880. // iprint("initscreenimage %p %p\n", screendimage, screenimage);
  881. mouseresize();
  882. return 1;
  883. }
  884. void
  885. deletescreenimage(void)
  886. {
  887. dlock();
  888. if(screenimage){
  889. /* will be freed via screendimage; disable */
  890. screenimage->clipr = ZR;
  891. screenimage = nil;
  892. }
  893. if(screendimage){
  894. drawfreedimage(screendimage);
  895. screendimage = nil;
  896. }
  897. dunlock();
  898. }
  899. void
  900. resetscreenimage(void)
  901. {
  902. dlock();
  903. initscreenimage();
  904. dunlock();
  905. }
  906. static Chan*
  907. drawattach(char *spec)
  908. {
  909. dlock();
  910. if(!initscreenimage()){
  911. dunlock();
  912. error("no frame buffer");
  913. }
  914. dunlock();
  915. return devattach('i', spec);
  916. }
  917. static Walkqid*
  918. drawwalk(Chan *c, Chan *nc, char **name, int nname)
  919. {
  920. if(screenimage == nil)
  921. error("no frame buffer");
  922. return devwalk(c, nc, name, nname, 0, 0, drawgen);
  923. }
  924. static int
  925. drawstat(Chan *c, uchar *db, int n)
  926. {
  927. return devstat(c, db, n, 0, 0, drawgen);
  928. }
  929. static Chan*
  930. drawopen(Chan *c, int omode)
  931. {
  932. Client *cl;
  933. DName *dn;
  934. DImage *di;
  935. if(c->qid.type & QTDIR){
  936. c = devopen(c, omode, 0, 0, drawgen);
  937. c->iounit = IOUNIT;
  938. }
  939. dlock();
  940. if(waserror()){
  941. dunlock();
  942. nexterror();
  943. }
  944. if(QID(c->qid) == Qnew){
  945. cl = drawnewclient();
  946. if(cl == 0)
  947. error(Enodev);
  948. c->qid.path = Qctl|((cl->slot+1)<<QSHIFT);
  949. }
  950. switch(QID(c->qid)){
  951. case Qwinname:
  952. break;
  953. case Qnew:
  954. break;
  955. case Qctl:
  956. cl = drawclient(c);
  957. if(cl->busy)
  958. error(Einuse);
  959. cl->busy = 1;
  960. flushrect = Rect(10000, 10000, -10000, -10000);
  961. dn = drawlookupname(strlen(screenname), screenname);
  962. if(dn == 0)
  963. error("draw: cannot happen 2");
  964. if(drawinstall(cl, 0, dn->dimage->image, 0) == 0)
  965. error(Edrawmem);
  966. di = drawlookup(cl, 0, 0);
  967. if(di == 0)
  968. error("draw: cannot happen 1");
  969. di->vers = dn->vers;
  970. di->name = smalloc(strlen(screenname)+1);
  971. strcpy(di->name, screenname);
  972. di->fromname = dn->dimage;
  973. di->fromname->ref++;
  974. incref(&cl->r);
  975. break;
  976. case Qcolormap:
  977. case Qdata:
  978. case Qrefresh:
  979. cl = drawclient(c);
  980. incref(&cl->r);
  981. break;
  982. }
  983. dunlock();
  984. poperror();
  985. c->mode = openmode(omode);
  986. c->flag |= COPEN;
  987. c->offset = 0;
  988. c->iounit = IOUNIT;
  989. return c;
  990. }
  991. static void
  992. drawclose(Chan *c)
  993. {
  994. int i;
  995. DImage *d, **dp;
  996. Client *cl;
  997. Refresh *r;
  998. if(QID(c->qid) < Qcolormap) /* Qtopdir, Qnew, Q3rd, Q2nd have no client */
  999. return;
  1000. dlock();
  1001. if(waserror()){
  1002. dunlock();
  1003. nexterror();
  1004. }
  1005. cl = drawclient(c);
  1006. if(QID(c->qid) == Qctl)
  1007. cl->busy = 0;
  1008. if((c->flag&COPEN) && (decref(&cl->r)==0)){
  1009. while(r = cl->refresh){ /* assign = */
  1010. cl->refresh = r->next;
  1011. free(r);
  1012. }
  1013. /* free names */
  1014. for(i=0; i<sdraw.nname; )
  1015. if(sdraw.name[i].client == cl)
  1016. drawdelname(sdraw.name+i);
  1017. else
  1018. i++;
  1019. while(cl->cscreen)
  1020. drawuninstallscreen(cl, cl->cscreen);
  1021. /* all screens are freed, so now we can free images */
  1022. dp = cl->dimage;
  1023. for(i=0; i<NHASH; i++){
  1024. while((d = *dp) != nil){
  1025. *dp = d->next;
  1026. drawfreedimage(d);
  1027. }
  1028. dp++;
  1029. }
  1030. sdraw.client[cl->slot] = 0;
  1031. drawflush(); /* to erase visible, now dead windows */
  1032. free(cl);
  1033. }
  1034. dunlock();
  1035. poperror();
  1036. }
  1037. long
  1038. drawread(Chan *c, void *a, long n, vlong off)
  1039. {
  1040. int index, m;
  1041. ulong red, green, blue;
  1042. Client *cl;
  1043. uchar *p;
  1044. Refresh *r;
  1045. DImage *di;
  1046. Memimage *i;
  1047. ulong offset = off;
  1048. char buf[16];
  1049. if(c->qid.type & QTDIR)
  1050. return devdirread(c, a, n, 0, 0, drawgen);
  1051. if(QID(c->qid) == Qwinname)
  1052. return readstr(off, a, n, screenname);
  1053. cl = drawclient(c);
  1054. dlock();
  1055. if(waserror()){
  1056. dunlock();
  1057. nexterror();
  1058. }
  1059. switch(QID(c->qid)){
  1060. case Qctl:
  1061. if(n < 12*12)
  1062. error(Eshortread);
  1063. if(cl->infoid < 0)
  1064. error(Enodrawimage);
  1065. if(cl->infoid == 0){
  1066. i = screenimage;
  1067. if(i == nil)
  1068. error(Enodrawimage);
  1069. }else{
  1070. di = drawlookup(cl, cl->infoid, 1);
  1071. if(di == nil)
  1072. error(Enodrawimage);
  1073. i = di->image;
  1074. }
  1075. n = sprint(a, "%11d %11d %11s %11d %11d %11d %11d %11d %11d %11d %11d %11d ",
  1076. cl->clientid, cl->infoid, chantostr(buf, i->chan), (i->flags&Frepl)==Frepl,
  1077. i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y,
  1078. i->clipr.min.x, i->clipr.min.y, i->clipr.max.x, i->clipr.max.y);
  1079. cl->infoid = -1;
  1080. break;
  1081. case Qcolormap:
  1082. drawactive(1); /* to restore map from backup */
  1083. p = malloc(4*12*256+1);
  1084. if(p == 0)
  1085. error(Enomem);
  1086. m = 0;
  1087. for(index = 0; index < 256; index++){
  1088. getcolor(index, &red, &green, &blue);
  1089. m += sprint((char*)p+m, "%11d %11lud %11lud %11lud\n", index, red>>24, green>>24, blue>>24);
  1090. }
  1091. n = readstr(offset, a, n, (char*)p);
  1092. free(p);
  1093. break;
  1094. case Qdata:
  1095. if(cl->readdata == nil)
  1096. error("no draw data");
  1097. if(n < cl->nreaddata)
  1098. error(Eshortread);
  1099. n = cl->nreaddata;
  1100. memmove(a, cl->readdata, cl->nreaddata);
  1101. free(cl->readdata);
  1102. cl->readdata = nil;
  1103. break;
  1104. case Qrefresh:
  1105. if(n < 5*4)
  1106. error(Ebadarg);
  1107. for(;;){
  1108. if(cl->refreshme || cl->refresh)
  1109. break;
  1110. dunlock();
  1111. if(waserror()){
  1112. dlock(); /* restore lock for waserror() above */
  1113. nexterror();
  1114. }
  1115. sleep(&cl->refrend, drawrefactive, cl);
  1116. poperror();
  1117. dlock();
  1118. }
  1119. p = a;
  1120. while(cl->refresh && n>=5*4){
  1121. r = cl->refresh;
  1122. BPLONG(p+0*4, r->dimage->id);
  1123. BPLONG(p+1*4, r->r.min.x);
  1124. BPLONG(p+2*4, r->r.min.y);
  1125. BPLONG(p+3*4, r->r.max.x);
  1126. BPLONG(p+4*4, r->r.max.y);
  1127. cl->refresh = r->next;
  1128. free(r);
  1129. p += 5*4;
  1130. n -= 5*4;
  1131. }
  1132. cl->refreshme = 0;
  1133. n = p-(uchar*)a;
  1134. break;
  1135. }
  1136. dunlock();
  1137. poperror();
  1138. return n;
  1139. }
  1140. void
  1141. drawwakeall(void)
  1142. {
  1143. Client *cl;
  1144. int i;
  1145. for(i=0; i<sdraw.nclient; i++){
  1146. cl = sdraw.client[i];
  1147. if(cl && (cl->refreshme || cl->refresh))
  1148. wakeup(&cl->refrend);
  1149. }
  1150. }
  1151. static long
  1152. drawwrite(Chan *c, void *a, long n, vlong)
  1153. {
  1154. char buf[128], *fields[4], *q;
  1155. Client *cl;
  1156. int i, m, red, green, blue, x;
  1157. if(c->qid.type & QTDIR)
  1158. error(Eisdir);
  1159. cl = drawclient(c);
  1160. dlock();
  1161. if(waserror()){
  1162. drawwakeall();
  1163. dunlock();
  1164. nexterror();
  1165. }
  1166. switch(QID(c->qid)){
  1167. case Qctl:
  1168. if(n != 4)
  1169. error("unknown draw control request");
  1170. cl->infoid = BGLONG((uchar*)a);
  1171. break;
  1172. case Qcolormap:
  1173. drawactive(1); /* to restore map from backup */
  1174. m = n;
  1175. n = 0;
  1176. while(m > 0){
  1177. x = m;
  1178. if(x > sizeof(buf)-1)
  1179. x = sizeof(buf)-1;
  1180. q = memccpy(buf, a, '\n', x);
  1181. if(q == 0)
  1182. break;
  1183. i = q-buf;
  1184. n += i;
  1185. a = (char*)a + i;
  1186. m -= i;
  1187. *q = 0;
  1188. if(tokenize(buf, fields, nelem(fields)) != 4)
  1189. error(Ebadarg);
  1190. i = strtoul(fields[0], 0, 0);
  1191. red = strtoul(fields[1], 0, 0);
  1192. green = strtoul(fields[2], 0, 0);
  1193. blue = strtoul(fields[3], &q, 0);
  1194. if(fields[3] == q)
  1195. error(Ebadarg);
  1196. if(red>255 || green>255 || blue>255 || i<0 || i>255)
  1197. error(Ebadarg);
  1198. red |= red<<8;
  1199. red |= red<<16;
  1200. green |= green<<8;
  1201. green |= green<<16;
  1202. blue |= blue<<8;
  1203. blue |= blue<<16;
  1204. setcolor(i, red, green, blue);
  1205. }
  1206. break;
  1207. case Qdata:
  1208. drawmesg(cl, a, n);
  1209. drawwakeall();
  1210. break;
  1211. default:
  1212. error(Ebadusefd);
  1213. }
  1214. dunlock();
  1215. poperror();
  1216. return n;
  1217. }
  1218. uchar*
  1219. drawcoord(uchar *p, uchar *maxp, int oldx, int *newx)
  1220. {
  1221. int b, x;
  1222. if(p >= maxp)
  1223. error(Eshortdraw);
  1224. b = *p++;
  1225. x = b & 0x7F;
  1226. if(b & 0x80){
  1227. if(p+1 >= maxp)
  1228. error(Eshortdraw);
  1229. x |= *p++ << 7;
  1230. x |= *p++ << 15;
  1231. if(x & (1<<22))
  1232. x |= ~0<<23;
  1233. }else{
  1234. if(b & 0x40)
  1235. x |= ~0<<7;
  1236. x += oldx;
  1237. }
  1238. *newx = x;
  1239. return p;
  1240. }
  1241. static void
  1242. printmesg(char *fmt, uchar *a, int plsprnt)
  1243. {
  1244. char buf[256];
  1245. char *p, *q;
  1246. int s;
  1247. if(1|| plsprnt==0){
  1248. SET(s,q,p);
  1249. USED(fmt, a, buf, p, q, s);
  1250. return;
  1251. }
  1252. q = buf;
  1253. *q++ = *a++;
  1254. for(p=fmt; *p; p++){
  1255. switch(*p){
  1256. case 'l':
  1257. q += sprint(q, " %ld", (long)BGLONG(a));
  1258. a += 4;
  1259. break;
  1260. case 'L':
  1261. q += sprint(q, " %.8lux", (ulong)BGLONG(a));
  1262. a += 4;
  1263. break;
  1264. case 'R':
  1265. q += sprint(q, " [%d %d %d %d]", BGLONG(a), BGLONG(a+4), BGLONG(a+8), BGLONG(a+12));
  1266. a += 16;
  1267. break;
  1268. case 'P':
  1269. q += sprint(q, " [%d %d]", BGLONG(a), BGLONG(a+4));
  1270. a += 8;
  1271. break;
  1272. case 'b':
  1273. q += sprint(q, " %d", *a++);
  1274. break;
  1275. case 's':
  1276. q += sprint(q, " %d", BGSHORT(a));
  1277. a += 2;
  1278. break;
  1279. case 'S':
  1280. q += sprint(q, " %.4ux", BGSHORT(a));
  1281. a += 2;
  1282. break;
  1283. }
  1284. }
  1285. *q++ = '\n';
  1286. *q = 0;
  1287. iprint("%.*s", (int)(q-buf), buf);
  1288. }
  1289. void
  1290. drawmesg(Client *client, void *av, int n)
  1291. {
  1292. int c, repl, m, y, dstid, scrnid, ni, ci, j, nw, e0, e1, op, ox, oy, oesize, esize, doflush;
  1293. uchar *u, *a, refresh;
  1294. char *fmt;
  1295. ulong value, chan;
  1296. Rectangle r, clipr;
  1297. Point p, q, *pp, sp;
  1298. Memimage *i, *bg, *dst, *src, *mask;
  1299. Memimage *l, **lp;
  1300. Memscreen *scrn;
  1301. DImage *font, *ll, *di, *ddst, *dsrc;
  1302. DName *dn;
  1303. DScreen *dscrn;
  1304. FChar *fc;
  1305. Refx *refx;
  1306. CScreen *cs;
  1307. Refreshfn reffn;
  1308. a = av;
  1309. m = 0;
  1310. fmt = nil;
  1311. if(waserror()){
  1312. if(fmt) printmesg(fmt, a, 1);
  1313. /* iprint("error: %s\n", up->errstr); */
  1314. nexterror();
  1315. }
  1316. while((n-=m) > 0){
  1317. USED(fmt);
  1318. a += m;
  1319. switch(*a){
  1320. default:
  1321. error("bad draw command");
  1322. /* new allocate: 'b' id[4] screenid[4] refresh[1] chan[4] repl[1] R[4*4] clipR[4*4] rrggbbaa[4] */
  1323. case 'b':
  1324. printmesg(fmt="LLbLbRRL", a, 0);
  1325. m = 1+4+4+1+4+1+4*4+4*4+4;
  1326. if(n < m)
  1327. error(Eshortdraw);
  1328. dstid = BGLONG(a+1);
  1329. scrnid = BGSHORT(a+5);
  1330. refresh = a[9];
  1331. chan = BGLONG(a+10);
  1332. repl = a[14];
  1333. drawrectangle(&r, a+15);
  1334. drawrectangle(&clipr, a+31);
  1335. value = BGLONG(a+47);
  1336. if(drawlookup(client, dstid, 0))
  1337. error(Eimageexists);
  1338. if(scrnid){
  1339. dscrn = drawlookupscreen(client, scrnid, &cs);
  1340. scrn = dscrn->screen;
  1341. if(repl || chan!=scrn->image->chan)
  1342. error("image parameters incompatible with screen");
  1343. reffn = nil;
  1344. switch(refresh){
  1345. case Refbackup:
  1346. break;
  1347. case Refnone:
  1348. reffn = memlnorefresh;
  1349. break;
  1350. case Refmesg:
  1351. reffn = drawrefresh;
  1352. break;
  1353. default:
  1354. error("unknown refresh method");
  1355. }
  1356. l = memlalloc(scrn, r, reffn, 0, value);
  1357. if(l == 0)
  1358. error(Edrawmem);
  1359. addflush(l->layer->screenr);
  1360. l->clipr = clipr;
  1361. rectclip(&l->clipr, r);
  1362. if(drawinstall(client, dstid, l, dscrn) == 0){
  1363. memldelete(l);
  1364. error(Edrawmem);
  1365. }
  1366. dscrn->ref++;
  1367. if(reffn){
  1368. refx = nil;
  1369. if(reffn == drawrefresh){
  1370. refx = malloc(sizeof(Refx));
  1371. if(refx == 0){
  1372. drawuninstall(client, dstid);
  1373. error(Edrawmem);
  1374. }
  1375. refx->client = client;
  1376. refx->dimage = drawlookup(client, dstid, 1);
  1377. }
  1378. memlsetrefresh(l, reffn, refx);
  1379. }
  1380. continue;
  1381. }
  1382. i = allocmemimage(r, chan);
  1383. if(i == 0)
  1384. error(Edrawmem);
  1385. if(repl)
  1386. i->flags |= Frepl;
  1387. i->clipr = clipr;
  1388. if(!repl)
  1389. rectclip(&i->clipr, r);
  1390. if(drawinstall(client, dstid, i, 0) == 0){
  1391. freememimage(i);
  1392. error(Edrawmem);
  1393. }
  1394. memfillcolor(i, value);
  1395. continue;
  1396. /* allocate screen: 'A' id[4] imageid[4] fillid[4] public[1] */
  1397. case 'A':
  1398. printmesg(fmt="LLLb", a, 1);
  1399. m = 1+4+4+4+1;
  1400. if(n < m)
  1401. error(Eshortdraw);
  1402. dstid = BGLONG(a+1);
  1403. if(dstid == 0)
  1404. error(Ebadarg);
  1405. if(drawlookupdscreen(dstid))
  1406. error(Escreenexists);
  1407. ddst = drawlookup(client, BGLONG(a+5), 1);
  1408. dsrc = drawlookup(client, BGLONG(a+9), 1);
  1409. if(ddst==0 || dsrc==0)
  1410. error(Enodrawimage);
  1411. if(drawinstallscreen(client, 0, dstid, ddst, dsrc, a[13]) == 0)
  1412. error(Edrawmem);
  1413. continue;
  1414. /* set repl and clip: 'c' dstid[4] repl[1] clipR[4*4] */
  1415. case 'c':
  1416. printmesg(fmt="LbR", a, 0);
  1417. m = 1+4+1+4*4;
  1418. if(n < m)
  1419. error(Eshortdraw);
  1420. ddst = drawlookup(client, BGLONG(a+1), 1);
  1421. if(ddst == nil)
  1422. error(Enodrawimage);
  1423. if(ddst->name)
  1424. error("cannot change repl/clipr of shared image");
  1425. dst = ddst->image;
  1426. if(a[5])
  1427. dst->flags |= Frepl;
  1428. drawrectangle(&dst->clipr, a+6);
  1429. continue;
  1430. /* draw: 'd' dstid[4] srcid[4] maskid[4] R[4*4] P[2*4] P[2*4] */
  1431. case 'd':
  1432. printmesg(fmt="LLLRPP", a, 0);
  1433. m = 1+4+4+4+4*4+2*4+2*4;
  1434. if(n < m)
  1435. error(Eshortdraw);
  1436. dst = drawimage(client, a+1);
  1437. dstid = BGLONG(a+1);
  1438. src = drawimage(client, a+5);
  1439. mask = drawimage(client, a+9);
  1440. drawrectangle(&r, a+13);
  1441. drawpoint(&p, a+29);
  1442. drawpoint(&q, a+37);
  1443. op = drawclientop(client);
  1444. memdraw(dst, r, src, p, mask, q, op);
  1445. dstflush(dstid, dst, r);
  1446. continue;
  1447. /* toggle debugging: 'D' val[1] */
  1448. case 'D':
  1449. printmesg(fmt="b", a, 0);
  1450. m = 1+1;
  1451. if(n < m)
  1452. error(Eshortdraw);
  1453. drawdebug = a[1];
  1454. continue;
  1455. /* ellipse: 'e' dstid[4] srcid[4] center[2*4] a[4] b[4] thick[4] sp[2*4] alpha[4] phi[4]*/
  1456. case 'e':
  1457. case 'E':
  1458. printmesg(fmt="LLPlllPll", a, 0);
  1459. m = 1+4+4+2*4+4+4+4+2*4+2*4;
  1460. if(n < m)
  1461. error(Eshortdraw);
  1462. dst = drawimage(client, a+1);
  1463. dstid = BGLONG(a+1);
  1464. src = drawimage(client, a+5);
  1465. drawpoint(&p, a+9);
  1466. e0 = BGLONG(a+17);
  1467. e1 = BGLONG(a+21);
  1468. if(e0<0 || e1<0)
  1469. error("invalid ellipse semidiameter");
  1470. j = BGLONG(a+25);
  1471. if(j < 0)
  1472. error("negative ellipse thickness");
  1473. drawpoint(&sp, a+29);
  1474. c = j;
  1475. if(*a == 'E')
  1476. c = -1;
  1477. ox = BGLONG(a+37);
  1478. oy = BGLONG(a+41);
  1479. op = drawclientop(client);
  1480. /* high bit indicates arc angles are present */
  1481. if(ox & (1<<31)){
  1482. if((ox & (1<<30)) == 0)
  1483. ox &= ~(1<<31);
  1484. memarc(dst, p, e0, e1, c, src, sp, ox, oy, op);
  1485. }else
  1486. memellipse(dst, p, e0, e1, c, src, sp, op);
  1487. dstflush(dstid, dst, Rect(p.x-e0-j, p.y-e1-j, p.x+e0+j+1, p.y+e1+j+1));
  1488. continue;
  1489. /* free: 'f' id[4] */
  1490. case 'f':
  1491. printmesg(fmt="L", a, 1);
  1492. m = 1+4;
  1493. if(n < m)
  1494. error(Eshortdraw);
  1495. ll = drawlookup(client, BGLONG(a+1), 0);
  1496. if(ll && ll->dscreen && ll->dscreen->owner != client)
  1497. ll->dscreen->owner->refreshme = 1;
  1498. drawuninstall(client, BGLONG(a+1));
  1499. continue;
  1500. /* free screen: 'F' id[4] */
  1501. case 'F':
  1502. printmesg(fmt="L", a, 1);
  1503. m = 1+4;
  1504. if(n < m)
  1505. error(Eshortdraw);
  1506. drawlookupscreen(client, BGLONG(a+1), &cs);
  1507. drawuninstallscreen(client, cs);
  1508. continue;
  1509. /* initialize font: 'i' fontid[4] nchars[4] ascent[1] */
  1510. case 'i':
  1511. printmesg(fmt="Llb", a, 1);
  1512. m = 1+4+4+1;
  1513. if(n < m)
  1514. error(Eshortdraw);
  1515. dstid = BGLONG(a+1);
  1516. if(dstid == 0)
  1517. error("cannot use display as font");
  1518. font = drawlookup(client, dstid, 1);
  1519. if(font == 0)
  1520. error(Enodrawimage);
  1521. if(font->image->layer)
  1522. error("cannot use window as font");
  1523. ni = BGLONG(a+5);
  1524. if(ni<=0 || ni>4096)
  1525. error("bad font size (4096 chars max)");
  1526. free(font->fchar); /* should we complain if non-zero? */
  1527. font->fchar = malloc(ni*sizeof(FChar));
  1528. if(font->fchar == 0)
  1529. error("no memory for font");
  1530. memset(font->fchar, 0, ni*sizeof(FChar));
  1531. font->nfchar = ni;
  1532. font->ascent = a[9];
  1533. continue;
  1534. /* load character: 'l' fontid[4] srcid[4] index[2] R[4*4] P[2*4] left[1] width[1] */
  1535. case 'l':
  1536. printmesg(fmt="LLSRPbb", a, 0);
  1537. m = 1+4+4+2+4*4+2*4+1+1;
  1538. if(n < m)
  1539. error(Eshortdraw);
  1540. font = drawlookup(client, BGLONG(a+1), 1);
  1541. if(font == 0)
  1542. error(Enodrawimage);
  1543. if(font->nfchar == 0)
  1544. error(Enotfont);
  1545. src = drawimage(client, a+5);
  1546. ci = BGSHORT(a+9);
  1547. if(ci >= font->nfchar)
  1548. error(Eindex);
  1549. drawrectangle(&r, a+11);
  1550. drawpoint(&p, a+27);
  1551. memdraw(font->image, r, src, p, memopaque, p, S);
  1552. fc = &font->fchar[ci];
  1553. fc->minx = r.min.x;
  1554. fc->maxx = r.max.x;
  1555. fc->miny = r.min.y;
  1556. fc->maxy = r.max.y;
  1557. fc->left = a[35];
  1558. fc->width = a[36];
  1559. continue;
  1560. /* draw line: 'L' dstid[4] p0[2*4] p1[2*4] end0[4] end1[4] radius[4] srcid[4] sp[2*4] */
  1561. case 'L':
  1562. printmesg(fmt="LPPlllLP", a, 0);
  1563. m = 1+4+2*4+2*4+4+4+4+4+2*4;
  1564. if(n < m)
  1565. error(Eshortdraw);
  1566. dst = drawimage(client, a+1);
  1567. dstid = BGLONG(a+1);
  1568. drawpoint(&p, a+5);
  1569. drawpoint(&q, a+13);
  1570. e0 = BGLONG(a+21);
  1571. e1 = BGLONG(a+25);
  1572. j = BGLONG(a+29);
  1573. if(j < 0)
  1574. error("negative line width");
  1575. src = drawimage(client, a+33);
  1576. drawpoint(&sp, a+37);
  1577. op = drawclientop(client);
  1578. memline(dst, p, q, e0, e1, j, src, sp, op);
  1579. /* avoid memlinebbox if possible */
  1580. if(dstid==0 || dst->layer!=nil){
  1581. /* BUG: this is terribly inefficient: update maximal containing rect*/
  1582. r = memlinebbox(p, q, e0, e1, j);
  1583. dstflush(dstid, dst, insetrect(r, -(1+1+j)));
  1584. }
  1585. continue;
  1586. /* create image mask: 'm' newid[4] id[4] */
  1587. /*
  1588. *
  1589. case 'm':
  1590. printmesg("LL", a, 0);
  1591. m = 4+4;
  1592. if(n < m)
  1593. error(Eshortdraw);
  1594. break;
  1595. *
  1596. */
  1597. /* attach to a named image: 'n' dstid[4] j[1] name[j] */
  1598. case 'n':
  1599. printmesg(fmt="Lz", a, 0);
  1600. m = 1+4+1;
  1601. if(n < m)
  1602. error(Eshortdraw);
  1603. j = a[5];
  1604. if(j == 0) /* give me a non-empty name please */
  1605. error(Eshortdraw);
  1606. m += j;
  1607. if(n < m)
  1608. error(Eshortdraw);
  1609. dstid = BGLONG(a+1);
  1610. if(drawlookup(client, dstid, 0))
  1611. error(Eimageexists);
  1612. dn = drawlookupname(j, (char*)a+6);
  1613. if(dn == nil)
  1614. error(Enoname);
  1615. if(drawinstall(client, dstid, dn->dimage->image, 0) == 0)
  1616. error(Edrawmem);
  1617. di = drawlookup(client, dstid, 0);
  1618. if(di == 0)
  1619. error("draw: cannot happen");
  1620. di->vers = dn->vers;
  1621. di->name = smalloc(j+1);
  1622. di->fromname = dn->dimage;
  1623. di->fromname->ref++;
  1624. memmove(di->name, a+6, j);
  1625. di->name[j] = 0;
  1626. client->infoid = dstid;
  1627. continue;
  1628. /* name an image: 'N' dstid[4] in[1] j[1] name[j] */
  1629. case 'N':
  1630. printmesg(fmt="Lbz", a, 0);
  1631. m = 1+4+1+1;
  1632. if(n < m)
  1633. error(Eshortdraw);
  1634. c = a[5];
  1635. j = a[6];
  1636. if(j == 0) /* give me a non-empty name please */
  1637. error(Eshortdraw);
  1638. m += j;
  1639. if(n < m)
  1640. error(Eshortdraw);
  1641. di = drawlookup(client, BGLONG(a+1), 0);
  1642. if(di == 0)
  1643. error(Enodrawimage);
  1644. if(di->name)
  1645. error(Enamed);
  1646. if(c)
  1647. drawaddname(client, di, j, (char*)a+7);
  1648. else{
  1649. dn = drawlookupname(j, (char*)a+7);
  1650. if(dn == nil)
  1651. error(Enoname);
  1652. if(dn->dimage != di)
  1653. error(Ewrongname);
  1654. drawdelname(dn);
  1655. }
  1656. continue;
  1657. /* position window: 'o' id[4] r.min [2*4] screenr.min [2*4] */
  1658. case 'o':
  1659. printmesg(fmt="LPP", a, 0);
  1660. m = 1+4+2*4+2*4;
  1661. if(n < m)
  1662. error(Eshortdraw);
  1663. dst = drawimage(client, a+1);
  1664. if(dst->layer){
  1665. drawpoint(&p, a+5);
  1666. drawpoint(&q, a+13);
  1667. r = dst->layer->screenr;
  1668. ni = memlorigin(dst, p, q);
  1669. if(ni < 0)
  1670. error("image origin failed");
  1671. if(ni > 0){
  1672. addflush(r);
  1673. addflush(dst->layer->screenr);
  1674. ll = drawlookup(client, BGLONG(a+1), 1);
  1675. drawrefreshscreen(ll, client);
  1676. }
  1677. }
  1678. continue;
  1679. /* set compositing operator for next draw operation: 'O' op */
  1680. case 'O':
  1681. printmesg(fmt="b", a, 0);
  1682. m = 1+1;
  1683. if(n < m)
  1684. error(Eshortdraw);
  1685. client->op = a[1];
  1686. continue;
  1687. /* filled polygon: 'P' dstid[4] n[2] wind[4] ignore[2*4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
  1688. /* polygon: 'p' dstid[4] n[2] end0[4] end1[4] radius[4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
  1689. case 'p':
  1690. case 'P':
  1691. printmesg(fmt="LslllLPP", a, 0);
  1692. m = 1+4+2+4+4+4+4+2*4;
  1693. if(n < m)
  1694. error(Eshortdraw);
  1695. dstid = BGLONG(a+1);
  1696. dst = drawimage(client, a+1);
  1697. ni = BGSHORT(a+5);
  1698. if(ni < 0)
  1699. error("negative count in polygon");
  1700. e0 = BGLONG(a+7);
  1701. e1 = BGLONG(a+11);
  1702. j = 0;
  1703. if(*a == 'p'){
  1704. j = BGLONG(a+15);
  1705. if(j < 0)
  1706. error("negative polygon line width");
  1707. }
  1708. src = drawimage(client, a+19);
  1709. drawpoint(&sp, a+23);
  1710. drawpoint(&p, a+31);
  1711. ni++;
  1712. pp = malloc(ni*sizeof(Point));
  1713. if(pp == nil)
  1714. error(Enomem);
  1715. doflush = 0;
  1716. if(dstid==0 || (dst->layer && dst->layer->screen->image->data == screenimage->data))
  1717. doflush = 1; /* simplify test in loop */
  1718. ox = oy = 0;
  1719. esize = 0;
  1720. u = a+m;
  1721. for(y=0; y<ni; y++){
  1722. q = p;
  1723. oesize = esize;
  1724. u = drawcoord(u, a+n, ox, &p.x);
  1725. u = drawcoord(u, a+n, oy, &p.y);
  1726. ox = p.x;
  1727. oy = p.y;
  1728. if(doflush){
  1729. esize = j;
  1730. if(*a == 'p'){
  1731. if(y == 0){
  1732. c = memlineendsize(e0);
  1733. if(c > esize)
  1734. esize = c;
  1735. }
  1736. if(y == ni-1){
  1737. c = memlineendsize(e1);
  1738. if(c > esize)
  1739. esize = c;
  1740. }
  1741. }
  1742. if(*a=='P' && e0!=1 && e0 !=~0)
  1743. r = dst->clipr;
  1744. else if(y > 0){
  1745. r = Rect(q.x-oesize, q.y-oesize, q.x+oesize+1, q.y+oesize+1);
  1746. combinerect(&r, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
  1747. }
  1748. if(rectclip(&r, dst->clipr)) /* should perhaps be an arg to dstflush */
  1749. dstflush(dstid, dst, r);
  1750. }
  1751. pp[y] = p;
  1752. }
  1753. if(y == 1)
  1754. dstflush(dstid, dst, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
  1755. op = drawclientop(client);
  1756. if(*a == 'p')
  1757. mempoly(dst, pp, ni, e0, e1, j, src, sp, op);
  1758. else
  1759. memfillpoly(dst, pp, ni, e0, src, sp, op);
  1760. free(pp);
  1761. m = u-a;
  1762. continue;
  1763. /* read: 'r' id[4] R[4*4] */
  1764. case 'r':
  1765. printmesg(fmt="LR", a, 0);
  1766. m = 1+4+4*4;
  1767. if(n < m)
  1768. error(Eshortdraw);
  1769. i = drawimage(client, a+1);
  1770. drawrectangle(&r, a+5);
  1771. if(!rectinrect(r, i->r))
  1772. error(Ereadoutside);
  1773. c = bytesperline(r, i->depth);
  1774. c *= Dy(r);
  1775. free(client->readdata);
  1776. client->readdata = mallocz(c, 0);
  1777. if(client->readdata == nil)
  1778. error("readimage malloc failed");
  1779. client->nreaddata = memunload(i, r, client->readdata, c);
  1780. if(client->nreaddata < 0){
  1781. free(client->readdata);
  1782. client->readdata = nil;
  1783. error("bad readimage call");
  1784. }
  1785. continue;
  1786. /* string: 's' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] ni*(index[2]) */
  1787. /* 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]) */
  1788. case 's':
  1789. case 'x':
  1790. printmesg(fmt="LLLPRPs", a, 0);
  1791. m = 1+4+4+4+2*4+4*4+2*4+2;
  1792. if(*a == 'x')
  1793. m += 4+2*4;
  1794. if(n < m)
  1795. error(Eshortdraw);
  1796. dst = drawimage(client, a+1);
  1797. dstid = BGLONG(a+1);
  1798. src = drawimage(client, a+5);
  1799. font = drawlookup(client, BGLONG(a+9), 1);
  1800. if(font == 0)
  1801. error(Enodrawimage);
  1802. if(font->nfchar == 0)
  1803. error(Enotfont);
  1804. drawpoint(&p, a+13);
  1805. drawrectangle(&r, a+21);
  1806. drawpoint(&sp, a+37);
  1807. ni = BGSHORT(a+45);
  1808. u = a+m;
  1809. m += ni*2;
  1810. if(n < m)
  1811. error(Eshortdraw);
  1812. clipr = dst->clipr;
  1813. dst->clipr = r;
  1814. op = drawclientop(client);
  1815. bg = dst;
  1816. if(*a == 'x'){
  1817. /* paint background */
  1818. bg = drawimage(client, a+47);
  1819. drawpoint(&q, a+51);
  1820. r.min.x = p.x;
  1821. r.min.y = p.y-font->ascent;
  1822. r.max.x = p.x;
  1823. r.max.y = r.min.y+Dy(font->image->r);
  1824. j = ni;
  1825. while(--j >= 0){
  1826. ci = BGSHORT(u);
  1827. if(ci<0 || ci>=font->nfchar){
  1828. dst->clipr = clipr;
  1829. error(Eindex);
  1830. }
  1831. r.max.x += font->fchar[ci].width;
  1832. u += 2;
  1833. }
  1834. memdraw(dst, r, bg, q, memopaque, ZP, op);
  1835. u -= 2*ni;
  1836. }
  1837. q = p;
  1838. while(--ni >= 0){
  1839. ci = BGSHORT(u);
  1840. if(ci<0 || ci>=font->nfchar){
  1841. dst->clipr = clipr;
  1842. error(Eindex);
  1843. }
  1844. q = drawchar(dst, bg, q, src, &sp, font, ci, op);
  1845. u += 2;
  1846. }
  1847. dst->clipr = clipr;
  1848. p.y -= font->ascent;
  1849. dstflush(dstid, dst, Rect(p.x, p.y, q.x, p.y+Dy(font->image->r)));
  1850. continue;
  1851. /* use public screen: 'S' id[4] chan[4] */
  1852. case 'S':
  1853. printmesg(fmt="Ll", a, 0);
  1854. m = 1+4+4;
  1855. if(n < m)
  1856. error(Eshortdraw);
  1857. dstid = BGLONG(a+1);
  1858. if(dstid == 0)
  1859. error(Ebadarg);
  1860. dscrn = drawlookupdscreen(dstid);
  1861. if(dscrn==0 || (dscrn->public==0 && dscrn->owner!=client))
  1862. error(Enodrawscreen);
  1863. if(dscrn->screen->image->chan != BGLONG(a+5))
  1864. error("inconsistent chan");
  1865. if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0)
  1866. error(Edrawmem);
  1867. continue;
  1868. /* top or bottom windows: 't' top[1] nw[2] n*id[4] */
  1869. case 't':
  1870. printmesg(fmt="bsL", a, 0);
  1871. m = 1+1+2;
  1872. if(n < m)
  1873. error(Eshortdraw);
  1874. nw = BGSHORT(a+2);
  1875. if(nw < 0)
  1876. error(Ebadarg);
  1877. if(nw == 0)
  1878. continue;
  1879. m += nw*4;
  1880. if(n < m)
  1881. error(Eshortdraw);
  1882. lp = malloc(nw*sizeof(Memimage*));
  1883. if(lp == 0)
  1884. error(Enomem);
  1885. if(waserror()){
  1886. free(lp);
  1887. nexterror();
  1888. }
  1889. for(j=0; j<nw; j++)
  1890. lp[j] = drawimage(client, a+1+1+2+j*4);
  1891. if(lp[0]->layer == 0)
  1892. error("images are not windows");
  1893. for(j=1; j<nw; j++)
  1894. if(lp[j]->layer->screen != lp[0]->layer->screen)
  1895. error("images not on same screen");
  1896. if(a[1])
  1897. memltofrontn(lp, nw);
  1898. else
  1899. memltorearn(lp, nw);
  1900. if(lp[0]->layer->screen->image->data == screenimage->data)
  1901. for(j=0; j<nw; j++)
  1902. addflush(lp[j]->layer->screenr);
  1903. ll = drawlookup(client, BGLONG(a+1+1+2), 1);
  1904. drawrefreshscreen(ll, client);
  1905. poperror();
  1906. free(lp);
  1907. continue;
  1908. /* visible: 'v' */
  1909. case 'v':
  1910. printmesg(fmt="", a, 0);
  1911. m = 1;
  1912. drawflush();
  1913. continue;
  1914. /* write: 'y' id[4] R[4*4] data[x*1] */
  1915. /* write from compressed data: 'Y' id[4] R[4*4] data[x*1] */
  1916. case 'y':
  1917. case 'Y':
  1918. printmesg(fmt="LR", a, 0);
  1919. // iprint("load %c\n", *a);
  1920. m = 1+4+4*4;
  1921. if(n < m)
  1922. error(Eshortdraw);
  1923. dstid = BGLONG(a+1);
  1924. dst = drawimage(client, a+1);
  1925. drawrectangle(&r, a+5);
  1926. if(!rectinrect(r, dst->r))
  1927. error(Ewriteoutside);
  1928. y = memload(dst, r, a+m, n-m, *a=='Y');
  1929. if(y < 0)
  1930. error("bad writeimage call");
  1931. dstflush(dstid, dst, r);
  1932. m += y;
  1933. continue;
  1934. }
  1935. }
  1936. poperror();
  1937. }
  1938. Dev drawdevtab = {
  1939. 'i',
  1940. "draw",
  1941. devreset,
  1942. devinit,
  1943. devshutdown,
  1944. drawattach,
  1945. drawwalk,
  1946. drawstat,
  1947. drawopen,
  1948. devcreate,
  1949. drawclose,
  1950. drawread,
  1951. devbread,
  1952. drawwrite,
  1953. devbwrite,
  1954. devremove,
  1955. devwstat,
  1956. };
  1957. /*
  1958. * On 8 bit displays, load the default color map
  1959. */
  1960. void
  1961. drawcmap(void)
  1962. {
  1963. int r, g, b, cr, cg, cb, v;
  1964. int num, den;
  1965. int i, j;
  1966. drawactive(1); /* to restore map from backup */
  1967. for(r=0,i=0; r!=4; r++)
  1968. for(v=0; v!=4; v++,i+=16){
  1969. for(g=0,j=v-r; g!=4; g++)
  1970. for(b=0;b!=4;b++,j++){
  1971. den = r;
  1972. if(g > den)
  1973. den = g;
  1974. if(b > den)
  1975. den = b;
  1976. if(den == 0) /* divide check -- pick grey shades */
  1977. cr = cg = cb = v*17;
  1978. else{
  1979. num = 17*(4*den+v);
  1980. cr = r*num/den;
  1981. cg = g*num/den;
  1982. cb = b*num/den;
  1983. }
  1984. setcolor(i+(j&15),
  1985. cr*0x01010101, cg*0x01010101, cb*0x01010101);
  1986. }
  1987. }
  1988. }
  1989. void
  1990. drawblankscreen(int blank)
  1991. {
  1992. int i, nc;
  1993. ulong *p;
  1994. if(blank == sdraw.blanked)
  1995. return;
  1996. if(!candlock())
  1997. return;
  1998. if(screenimage == nil){
  1999. dunlock();
  2000. return;
  2001. }
  2002. p = sdraw.savemap;
  2003. nc = screenimage->depth > 8 ? 256 : 1<<screenimage->depth;
  2004. /*
  2005. * blankscreen uses the hardware to blank the screen
  2006. * when possible. to help in cases when it is not possible,
  2007. * we set the color map to be all black.
  2008. */
  2009. if(blank == 0){ /* turn screen on */
  2010. for(i=0; i<nc; i++, p+=3)
  2011. setcolor(i, p[0], p[1], p[2]);
  2012. blankscreen(0);
  2013. }else{ /* turn screen off */
  2014. blankscreen(1);
  2015. for(i=0; i<nc; i++, p+=3){
  2016. getcolor(i, &p[0], &p[1], &p[2]);
  2017. setcolor(i, 0, 0, 0);
  2018. }
  2019. }
  2020. sdraw.blanked = blank;
  2021. dunlock();
  2022. }
  2023. /*
  2024. * record activity on screen, changing blanking as appropriate
  2025. */
  2026. void
  2027. drawactive(int active)
  2028. {
  2029. if(active){
  2030. drawblankscreen(0);
  2031. sdraw.blanktime = MACHP(0)->ticks;
  2032. }else{
  2033. if(blanktime && sdraw.blanktime && TK2SEC(MACHP(0)->ticks - sdraw.blanktime)/60 >= blanktime)
  2034. drawblankscreen(1);
  2035. }
  2036. }
  2037. int
  2038. drawidletime(void)
  2039. {
  2040. return TK2SEC(MACHP(0)->ticks - sdraw.blanktime)/60;
  2041. }