devdraw.c 45 KB

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