devdraw.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  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, Point p, Memimage *src, Point *sp, DImage *font, int index, int op)
  793. {
  794. FChar *fc;
  795. Rectangle r;
  796. Point sp1;
  797. fc = &font->fchar[index];
  798. r.min.x = p.x+fc->left;
  799. r.min.y = p.y-(font->ascent-fc->miny);
  800. r.max.x = r.min.x+(fc->maxx-fc->minx);
  801. r.max.y = r.min.y+(fc->maxy-fc->miny);
  802. sp1.x = sp->x+fc->left;
  803. sp1.y = sp->y+fc->miny;
  804. memdraw(dst, r, src, sp1, font->image, Pt(fc->minx, fc->miny), op);
  805. p.x += fc->width;
  806. sp->x += fc->width;
  807. return p;
  808. }
  809. static DImage*
  810. makescreenimage(void)
  811. {
  812. int width, depth;
  813. ulong chan;
  814. DImage *di;
  815. Memdata *md;
  816. Memimage *i;
  817. Rectangle r;
  818. md = malloc(sizeof *md);
  819. if(md == nil)
  820. return nil;
  821. md->allocd = 1;
  822. md->base = nil;
  823. md->bdata = attachscreen(&r, &chan, &depth, &width, &sdraw.softscreen);
  824. if(md->bdata == nil){
  825. free(md);
  826. return nil;
  827. }
  828. md->ref = 1;
  829. i = allocmemimaged(r, chan, md);
  830. if(i == nil){
  831. free(md);
  832. return nil;
  833. }
  834. i->width = width;
  835. i->clipr = r;
  836. di = allocdimage(i);
  837. if(di == nil){
  838. freememimage(i); /* frees md */
  839. return nil;
  840. }
  841. if(!waserror()){
  842. snprint(screenname, sizeof screenname, "noborder.screen.%d", ++screennameid);
  843. drawaddname(nil, di, strlen(screenname), screenname);
  844. poperror();
  845. }
  846. return di;
  847. }
  848. static int
  849. initscreenimage(void)
  850. {
  851. if(screenimage != nil)
  852. return 1;
  853. screendimage = makescreenimage();
  854. if(screendimage == nil)
  855. return 0;
  856. screenimage = screendimage->image;
  857. // iprint("initscreenimage %p %p\n", screendimage, screenimage);
  858. mouseresize();
  859. return 1;
  860. }
  861. void
  862. deletescreenimage(void)
  863. {
  864. dlock();
  865. if(screenimage){
  866. /* will be freed via screendimage; disable */
  867. screenimage->clipr = ZR;
  868. screenimage = nil;
  869. }
  870. if(screendimage){
  871. drawfreedimage(screendimage);
  872. screendimage = nil;
  873. }
  874. dunlock();
  875. }
  876. void
  877. resetscreenimage(void)
  878. {
  879. dlock();
  880. initscreenimage();
  881. dunlock();
  882. }
  883. static Chan*
  884. drawattach(char *spec)
  885. {
  886. dlock();
  887. if(!initscreenimage()){
  888. dunlock();
  889. error("no frame buffer");
  890. }
  891. dunlock();
  892. return devattach('i', spec);
  893. }
  894. static Walkqid*
  895. drawwalk(Chan *c, Chan *nc, char **name, int nname)
  896. {
  897. if(screenimage == nil)
  898. error("no frame buffer");
  899. return devwalk(c, nc, name, nname, 0, 0, drawgen);
  900. }
  901. static int
  902. drawstat(Chan *c, uchar *db, int n)
  903. {
  904. return devstat(c, db, n, 0, 0, drawgen);
  905. }
  906. static Chan*
  907. drawopen(Chan *c, int omode)
  908. {
  909. Client *cl;
  910. DName *dn;
  911. DImage *di;
  912. if(c->qid.type & QTDIR){
  913. c = devopen(c, omode, 0, 0, drawgen);
  914. c->iounit = IOUNIT;
  915. }
  916. dlock();
  917. if(waserror()){
  918. dunlock();
  919. nexterror();
  920. }
  921. if(QID(c->qid) == Qnew){
  922. cl = drawnewclient();
  923. if(cl == 0)
  924. error(Enodev);
  925. c->qid.path = Qctl|((cl->slot+1)<<QSHIFT);
  926. }
  927. switch(QID(c->qid)){
  928. case Qwinname:
  929. break;
  930. case Qnew:
  931. break;
  932. case Qctl:
  933. cl = drawclient(c);
  934. if(cl->busy)
  935. error(Einuse);
  936. cl->busy = 1;
  937. flushrect = Rect(10000, 10000, -10000, -10000);
  938. dn = drawlookupname(strlen(screenname), screenname);
  939. if(dn == 0)
  940. error("draw: cannot happen 2");
  941. if(drawinstall(cl, 0, dn->dimage->image, 0) == 0)
  942. error(Edrawmem);
  943. di = drawlookup(cl, 0, 0);
  944. if(di == 0)
  945. error("draw: cannot happen 1");
  946. di->vers = dn->vers;
  947. di->name = smalloc(strlen(screenname)+1);
  948. strcpy(di->name, screenname);
  949. di->fromname = dn->dimage;
  950. di->fromname->ref++;
  951. incref(&cl->r);
  952. break;
  953. case Qcolormap:
  954. case Qdata:
  955. case Qrefresh:
  956. cl = drawclient(c);
  957. incref(&cl->r);
  958. break;
  959. }
  960. dunlock();
  961. poperror();
  962. c->mode = openmode(omode);
  963. c->flag |= COPEN;
  964. c->offset = 0;
  965. c->iounit = IOUNIT;
  966. return c;
  967. }
  968. static void
  969. drawclose(Chan *c)
  970. {
  971. int i;
  972. DImage *d, **dp;
  973. Client *cl;
  974. Refresh *r;
  975. if(QID(c->qid) < Qcolormap) /* Qtopdir, Qnew, Q3rd, Q2nd have no client */
  976. return;
  977. dlock();
  978. if(waserror()){
  979. dunlock();
  980. nexterror();
  981. }
  982. cl = drawclient(c);
  983. if(QID(c->qid) == Qctl)
  984. cl->busy = 0;
  985. if((c->flag&COPEN) && (decref(&cl->r)==0)){
  986. while(r = cl->refresh){ /* assign = */
  987. cl->refresh = r->next;
  988. free(r);
  989. }
  990. /* free names */
  991. for(i=0; i<sdraw.nname; )
  992. if(sdraw.name[i].client == cl)
  993. drawdelname(sdraw.name+i);
  994. else
  995. i++;
  996. while(cl->cscreen)
  997. drawuninstallscreen(cl, cl->cscreen);
  998. /* all screens are freed, so now we can free images */
  999. dp = cl->dimage;
  1000. for(i=0; i<NHASH; i++){
  1001. while((d = *dp) != nil){
  1002. *dp = d->next;
  1003. drawfreedimage(d);
  1004. }
  1005. dp++;
  1006. }
  1007. sdraw.client[cl->slot] = 0;
  1008. drawflush(); /* to erase visible, now dead windows */
  1009. free(cl);
  1010. }
  1011. dunlock();
  1012. poperror();
  1013. }
  1014. long
  1015. drawread(Chan *c, void *a, long n, vlong off)
  1016. {
  1017. int index, m;
  1018. ulong red, green, blue;
  1019. Client *cl;
  1020. uchar *p;
  1021. Refresh *r;
  1022. DImage *di;
  1023. Memimage *i;
  1024. ulong offset = off;
  1025. char buf[16];
  1026. if(c->qid.type & QTDIR)
  1027. return devdirread(c, a, n, 0, 0, drawgen);
  1028. if(QID(c->qid) == Qwinname)
  1029. return readstr(off, a, n, screenname);
  1030. cl = drawclient(c);
  1031. dlock();
  1032. if(waserror()){
  1033. dunlock();
  1034. nexterror();
  1035. }
  1036. switch(QID(c->qid)){
  1037. case Qctl:
  1038. if(n < 12*12)
  1039. error(Eshortread);
  1040. if(cl->infoid < 0)
  1041. error(Enodrawimage);
  1042. if(cl->infoid == 0){
  1043. i = screenimage;
  1044. if(i == nil)
  1045. error(Enodrawimage);
  1046. }else{
  1047. di = drawlookup(cl, cl->infoid, 1);
  1048. if(di == nil)
  1049. error(Enodrawimage);
  1050. i = di->image;
  1051. }
  1052. n = sprint(a, "%11d %11d %11s %11d %11d %11d %11d %11d %11d %11d %11d %11d ",
  1053. cl->clientid, cl->infoid, chantostr(buf, i->chan), (i->flags&Frepl)==Frepl,
  1054. i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y,
  1055. i->clipr.min.x, i->clipr.min.y, i->clipr.max.x, i->clipr.max.y);
  1056. cl->infoid = -1;
  1057. break;
  1058. case Qcolormap:
  1059. drawactive(1); /* to restore map from backup */
  1060. p = malloc(4*12*256+1);
  1061. if(p == 0)
  1062. error(Enomem);
  1063. m = 0;
  1064. for(index = 0; index < 256; index++){
  1065. getcolor(index, &red, &green, &blue);
  1066. m += sprint((char*)p+m, "%11d %11lud %11lud %11lud\n", index, red>>24, green>>24, blue>>24);
  1067. }
  1068. n = readstr(offset, a, n, (char*)p);
  1069. free(p);
  1070. break;
  1071. case Qdata:
  1072. if(cl->readdata == nil)
  1073. error("no draw data");
  1074. if(n < cl->nreaddata)
  1075. error(Eshortread);
  1076. n = cl->nreaddata;
  1077. memmove(a, cl->readdata, cl->nreaddata);
  1078. free(cl->readdata);
  1079. cl->readdata = nil;
  1080. break;
  1081. case Qrefresh:
  1082. if(n < 5*4)
  1083. error(Ebadarg);
  1084. for(;;){
  1085. if(cl->refreshme || cl->refresh)
  1086. break;
  1087. dunlock();
  1088. if(waserror()){
  1089. dlock(); /* restore lock for waserror() above */
  1090. nexterror();
  1091. }
  1092. sleep(&cl->refrend, drawrefactive, cl);
  1093. poperror();
  1094. dlock();
  1095. }
  1096. p = a;
  1097. while(cl->refresh && n>=5*4){
  1098. r = cl->refresh;
  1099. BPLONG(p+0*4, r->dimage->id);
  1100. BPLONG(p+1*4, r->r.min.x);
  1101. BPLONG(p+2*4, r->r.min.y);
  1102. BPLONG(p+3*4, r->r.max.x);
  1103. BPLONG(p+4*4, r->r.max.y);
  1104. cl->refresh = r->next;
  1105. free(r);
  1106. p += 5*4;
  1107. n -= 5*4;
  1108. }
  1109. cl->refreshme = 0;
  1110. n = p-(uchar*)a;
  1111. break;
  1112. }
  1113. dunlock();
  1114. poperror();
  1115. return n;
  1116. }
  1117. void
  1118. drawwakeall(void)
  1119. {
  1120. Client *cl;
  1121. int i;
  1122. for(i=0; i<sdraw.nclient; i++){
  1123. cl = sdraw.client[i];
  1124. if(cl && (cl->refreshme || cl->refresh))
  1125. wakeup(&cl->refrend);
  1126. }
  1127. }
  1128. static long
  1129. drawwrite(Chan *c, void *a, long n, vlong)
  1130. {
  1131. char buf[128], *fields[4], *q;
  1132. Client *cl;
  1133. int i, m, red, green, blue, x;
  1134. if(c->qid.type & QTDIR)
  1135. error(Eisdir);
  1136. cl = drawclient(c);
  1137. dlock();
  1138. if(waserror()){
  1139. drawwakeall();
  1140. dunlock();
  1141. nexterror();
  1142. }
  1143. switch(QID(c->qid)){
  1144. case Qctl:
  1145. if(n != 4)
  1146. error("unknown draw control request");
  1147. cl->infoid = BGLONG((uchar*)a);
  1148. break;
  1149. case Qcolormap:
  1150. drawactive(1); /* to restore map from backup */
  1151. m = n;
  1152. n = 0;
  1153. while(m > 0){
  1154. x = m;
  1155. if(x > sizeof(buf)-1)
  1156. x = sizeof(buf)-1;
  1157. q = memccpy(buf, a, '\n', x);
  1158. if(q == 0)
  1159. break;
  1160. i = q-buf;
  1161. n += i;
  1162. a = (char*)a + i;
  1163. m -= i;
  1164. *q = 0;
  1165. if(tokenize(buf, fields, nelem(fields)) != 4)
  1166. error(Ebadarg);
  1167. i = strtoul(fields[0], 0, 0);
  1168. red = strtoul(fields[1], 0, 0);
  1169. green = strtoul(fields[2], 0, 0);
  1170. blue = strtoul(fields[3], &q, 0);
  1171. if(fields[3] == q)
  1172. error(Ebadarg);
  1173. if(red>255 || green>255 || blue>255 || i<0 || i>255)
  1174. error(Ebadarg);
  1175. red |= red<<8;
  1176. red |= red<<16;
  1177. green |= green<<8;
  1178. green |= green<<16;
  1179. blue |= blue<<8;
  1180. blue |= blue<<16;
  1181. setcolor(i, red, green, blue);
  1182. }
  1183. break;
  1184. case Qdata:
  1185. drawmesg(cl, a, n);
  1186. drawwakeall();
  1187. break;
  1188. default:
  1189. error(Ebadusefd);
  1190. }
  1191. dunlock();
  1192. poperror();
  1193. return n;
  1194. }
  1195. uchar*
  1196. drawcoord(uchar *p, uchar *maxp, int oldx, int *newx)
  1197. {
  1198. int b, x;
  1199. if(p >= maxp)
  1200. error(Eshortdraw);
  1201. b = *p++;
  1202. x = b & 0x7F;
  1203. if(b & 0x80){
  1204. if(p+1 >= maxp)
  1205. error(Eshortdraw);
  1206. x |= *p++ << 7;
  1207. x |= *p++ << 15;
  1208. if(x & (1<<22))
  1209. x |= ~0<<23;
  1210. }else{
  1211. if(b & 0x40)
  1212. x |= ~0<<7;
  1213. x += oldx;
  1214. }
  1215. *newx = x;
  1216. return p;
  1217. }
  1218. static void
  1219. printmesg(char *fmt, uchar *a, int plsprnt)
  1220. {
  1221. char buf[256];
  1222. char *p, *q;
  1223. int s;
  1224. if(1|| plsprnt==0){
  1225. SET(s,q,p);
  1226. USED(fmt, a, buf, p, q, s);
  1227. return;
  1228. }
  1229. q = buf;
  1230. *q++ = *a++;
  1231. for(p=fmt; *p; p++){
  1232. switch(*p){
  1233. case 'l':
  1234. q += sprint(q, " %ld", (long)BGLONG(a));
  1235. a += 4;
  1236. break;
  1237. case 'L':
  1238. q += sprint(q, " %.8lux", (ulong)BGLONG(a));
  1239. a += 4;
  1240. break;
  1241. case 'R':
  1242. q += sprint(q, " [%d %d %d %d]", BGLONG(a), BGLONG(a+4), BGLONG(a+8), BGLONG(a+12));
  1243. a += 16;
  1244. break;
  1245. case 'P':
  1246. q += sprint(q, " [%d %d]", BGLONG(a), BGLONG(a+4));
  1247. a += 8;
  1248. break;
  1249. case 'b':
  1250. q += sprint(q, " %d", *a++);
  1251. break;
  1252. case 's':
  1253. q += sprint(q, " %d", BGSHORT(a));
  1254. a += 2;
  1255. break;
  1256. case 'S':
  1257. q += sprint(q, " %.4ux", BGSHORT(a));
  1258. a += 2;
  1259. break;
  1260. }
  1261. }
  1262. *q++ = '\n';
  1263. *q = 0;
  1264. iprint("%.*s", (int)(q-buf), buf);
  1265. }
  1266. void
  1267. drawmesg(Client *client, void *av, int n)
  1268. {
  1269. int c, repl, m, y, dstid, scrnid, ni, ci, j, nw, e0, e1, op, ox, oy, oesize, esize, doflush;
  1270. uchar *u, *a, refresh;
  1271. char *fmt;
  1272. ulong value, chan;
  1273. Rectangle r, clipr;
  1274. Point p, q, *pp, sp;
  1275. Memimage *i, *dst, *src, *mask;
  1276. Memimage *l, **lp;
  1277. Memscreen *scrn;
  1278. DImage *font, *ll, *di, *ddst, *dsrc;
  1279. DName *dn;
  1280. DScreen *dscrn;
  1281. FChar *fc;
  1282. Refx *refx;
  1283. CScreen *cs;
  1284. Refreshfn reffn;
  1285. a = av;
  1286. m = 0;
  1287. fmt = nil;
  1288. if(waserror()){
  1289. if(fmt) printmesg(fmt, a, 1);
  1290. /* iprint("error: %s\n", up->errstr); */
  1291. nexterror();
  1292. }
  1293. while((n-=m) > 0){
  1294. USED(fmt);
  1295. a += m;
  1296. switch(*a){
  1297. default:
  1298. error("bad draw command");
  1299. /* new allocate: 'b' id[4] screenid[4] refresh[1] chan[4] repl[1] R[4*4] clipR[4*4] rrggbbaa[4] */
  1300. case 'b':
  1301. printmesg(fmt="LLbLbRRL", a, 0);
  1302. m = 1+4+4+1+4+1+4*4+4*4+4;
  1303. if(n < m)
  1304. error(Eshortdraw);
  1305. dstid = BGLONG(a+1);
  1306. scrnid = BGSHORT(a+5);
  1307. refresh = a[9];
  1308. chan = BGLONG(a+10);
  1309. repl = a[14];
  1310. drawrectangle(&r, a+15);
  1311. drawrectangle(&clipr, a+31);
  1312. value = BGLONG(a+47);
  1313. if(drawlookup(client, dstid, 0))
  1314. error(Eimageexists);
  1315. if(scrnid){
  1316. dscrn = drawlookupscreen(client, scrnid, &cs);
  1317. scrn = dscrn->screen;
  1318. if(repl || chan!=scrn->image->chan)
  1319. error("image parameters incompatible with screen");
  1320. reffn = nil;
  1321. switch(refresh){
  1322. case Refbackup:
  1323. break;
  1324. case Refnone:
  1325. reffn = memlnorefresh;
  1326. break;
  1327. case Refmesg:
  1328. reffn = drawrefresh;
  1329. break;
  1330. default:
  1331. error("unknown refresh method");
  1332. }
  1333. l = memlalloc(scrn, r, reffn, 0, value);
  1334. if(l == 0)
  1335. error(Edrawmem);
  1336. addflush(l->layer->screenr);
  1337. l->clipr = clipr;
  1338. rectclip(&l->clipr, r);
  1339. if(drawinstall(client, dstid, l, dscrn) == 0){
  1340. memldelete(l);
  1341. error(Edrawmem);
  1342. }
  1343. dscrn->ref++;
  1344. if(reffn){
  1345. refx = nil;
  1346. if(reffn == drawrefresh){
  1347. refx = malloc(sizeof(Refx));
  1348. if(refx == 0){
  1349. drawuninstall(client, dstid);
  1350. error(Edrawmem);
  1351. }
  1352. refx->client = client;
  1353. refx->dimage = drawlookup(client, dstid, 1);
  1354. }
  1355. memlsetrefresh(l, reffn, refx);
  1356. }
  1357. continue;
  1358. }
  1359. i = allocmemimage(r, chan);
  1360. if(i == 0)
  1361. error(Edrawmem);
  1362. if(repl)
  1363. i->flags |= Frepl;
  1364. i->clipr = clipr;
  1365. if(!repl)
  1366. rectclip(&i->clipr, r);
  1367. if(drawinstall(client, dstid, i, 0) == 0){
  1368. freememimage(i);
  1369. error(Edrawmem);
  1370. }
  1371. memfillcolor(i, value);
  1372. continue;
  1373. /* allocate screen: 'A' id[4] imageid[4] fillid[4] public[1] */
  1374. case 'A':
  1375. printmesg(fmt="LLLb", a, 1);
  1376. m = 1+4+4+4+1;
  1377. if(n < m)
  1378. error(Eshortdraw);
  1379. dstid = BGLONG(a+1);
  1380. if(dstid == 0)
  1381. error(Ebadarg);
  1382. if(drawlookupdscreen(dstid))
  1383. error(Escreenexists);
  1384. ddst = drawlookup(client, BGLONG(a+5), 1);
  1385. dsrc = drawlookup(client, BGLONG(a+9), 1);
  1386. if(ddst==0 || dsrc==0)
  1387. error(Enodrawimage);
  1388. if(drawinstallscreen(client, 0, dstid, ddst, dsrc, a[13]) == 0)
  1389. error(Edrawmem);
  1390. continue;
  1391. /* set repl and clip: 'c' dstid[4] repl[1] clipR[4*4] */
  1392. case 'c':
  1393. printmesg(fmt="LbR", a, 0);
  1394. m = 1+4+1+4*4;
  1395. if(n < m)
  1396. error(Eshortdraw);
  1397. ddst = drawlookup(client, BGLONG(a+1), 1);
  1398. if(ddst == nil)
  1399. error(Enodrawimage);
  1400. if(ddst->name)
  1401. error("cannot change repl/clipr of shared image");
  1402. dst = ddst->image;
  1403. if(a[5])
  1404. dst->flags |= Frepl;
  1405. drawrectangle(&dst->clipr, a+6);
  1406. continue;
  1407. /* draw: 'd' dstid[4] srcid[4] maskid[4] R[4*4] P[2*4] P[2*4] */
  1408. case 'd':
  1409. printmesg(fmt="LLLRPP", a, 0);
  1410. m = 1+4+4+4+4*4+2*4+2*4;
  1411. if(n < m)
  1412. error(Eshortdraw);
  1413. dst = drawimage(client, a+1);
  1414. dstid = BGLONG(a+1);
  1415. src = drawimage(client, a+5);
  1416. mask = drawimage(client, a+9);
  1417. drawrectangle(&r, a+13);
  1418. drawpoint(&p, a+29);
  1419. drawpoint(&q, a+37);
  1420. op = drawclientop(client);
  1421. memdraw(dst, r, src, p, mask, q, op);
  1422. dstflush(dstid, dst, r);
  1423. continue;
  1424. /* toggle debugging: 'D' val[1] */
  1425. case 'D':
  1426. printmesg(fmt="b", a, 0);
  1427. m = 1+1;
  1428. if(n < m)
  1429. error(Eshortdraw);
  1430. drawdebug = a[1];
  1431. continue;
  1432. /* ellipse: 'e' dstid[4] srcid[4] center[2*4] a[4] b[4] thick[4] sp[2*4] alpha[4] phi[4]*/
  1433. case 'e':
  1434. case 'E':
  1435. printmesg(fmt="LLPlllPll", a, 0);
  1436. m = 1+4+4+2*4+4+4+4+2*4+2*4;
  1437. if(n < m)
  1438. error(Eshortdraw);
  1439. dst = drawimage(client, a+1);
  1440. dstid = BGLONG(a+1);
  1441. src = drawimage(client, a+5);
  1442. drawpoint(&p, a+9);
  1443. e0 = BGLONG(a+17);
  1444. e1 = BGLONG(a+21);
  1445. if(e0<0 || e1<0)
  1446. error("invalid ellipse semidiameter");
  1447. j = BGLONG(a+25);
  1448. if(j < 0)
  1449. error("negative ellipse thickness");
  1450. drawpoint(&sp, a+29);
  1451. c = j;
  1452. if(*a == 'E')
  1453. c = -1;
  1454. ox = BGLONG(a+37);
  1455. oy = BGLONG(a+41);
  1456. op = drawclientop(client);
  1457. /* high bit indicates arc angles are present */
  1458. if(ox & (1<<31)){
  1459. if((ox & (1<<30)) == 0)
  1460. ox &= ~(1<<31);
  1461. memarc(dst, p, e0, e1, c, src, sp, ox, oy, op);
  1462. }else
  1463. memellipse(dst, p, e0, e1, c, src, sp, op);
  1464. dstflush(dstid, dst, Rect(p.x-e0-j, p.y-e1-j, p.x+e0+j+1, p.y+e1+j+1));
  1465. continue;
  1466. /* free: 'f' id[4] */
  1467. case 'f':
  1468. printmesg(fmt="L", a, 1);
  1469. m = 1+4;
  1470. if(n < m)
  1471. error(Eshortdraw);
  1472. ll = drawlookup(client, BGLONG(a+1), 0);
  1473. if(ll && ll->dscreen && ll->dscreen->owner != client)
  1474. ll->dscreen->owner->refreshme = 1;
  1475. drawuninstall(client, BGLONG(a+1));
  1476. continue;
  1477. /* free screen: 'F' id[4] */
  1478. case 'F':
  1479. printmesg(fmt="L", a, 1);
  1480. m = 1+4;
  1481. if(n < m)
  1482. error(Eshortdraw);
  1483. drawlookupscreen(client, BGLONG(a+1), &cs);
  1484. drawuninstallscreen(client, cs);
  1485. continue;
  1486. /* initialize font: 'i' fontid[4] nchars[4] ascent[1] */
  1487. case 'i':
  1488. printmesg(fmt="Llb", a, 1);
  1489. m = 1+4+4+1;
  1490. if(n < m)
  1491. error(Eshortdraw);
  1492. dstid = BGLONG(a+1);
  1493. if(dstid == 0)
  1494. error("cannot use display as font");
  1495. font = drawlookup(client, dstid, 1);
  1496. if(font == 0)
  1497. error(Enodrawimage);
  1498. if(font->image->layer)
  1499. error("cannot use window as font");
  1500. ni = BGLONG(a+5);
  1501. if(ni<=0 || ni>4096)
  1502. error("bad font size (4096 chars max)");
  1503. free(font->fchar); /* should we complain if non-zero? */
  1504. font->fchar = malloc(ni*sizeof(FChar));
  1505. if(font->fchar == 0)
  1506. error("no memory for font");
  1507. memset(font->fchar, 0, ni*sizeof(FChar));
  1508. font->nfchar = ni;
  1509. font->ascent = a[9];
  1510. continue;
  1511. /* load character: 'l' fontid[4] srcid[4] index[2] R[4*4] P[2*4] left[1] width[1] */
  1512. case 'l':
  1513. printmesg(fmt="LLSRPbb", a, 0);
  1514. m = 1+4+4+2+4*4+2*4+1+1;
  1515. if(n < m)
  1516. error(Eshortdraw);
  1517. font = drawlookup(client, BGLONG(a+1), 1);
  1518. if(font == 0)
  1519. error(Enodrawimage);
  1520. if(font->nfchar == 0)
  1521. error(Enotfont);
  1522. src = drawimage(client, a+5);
  1523. ci = BGSHORT(a+9);
  1524. if(ci >= font->nfchar)
  1525. error(Eindex);
  1526. drawrectangle(&r, a+11);
  1527. drawpoint(&p, a+27);
  1528. memdraw(font->image, r, src, p, memopaque, p, S);
  1529. fc = &font->fchar[ci];
  1530. fc->minx = r.min.x;
  1531. fc->maxx = r.max.x;
  1532. fc->miny = r.min.y;
  1533. fc->maxy = r.max.y;
  1534. fc->left = a[35];
  1535. fc->width = a[36];
  1536. continue;
  1537. /* draw line: 'L' dstid[4] p0[2*4] p1[2*4] end0[4] end1[4] radius[4] srcid[4] sp[2*4] */
  1538. case 'L':
  1539. printmesg(fmt="LPPlllLP", a, 0);
  1540. m = 1+4+2*4+2*4+4+4+4+4+2*4;
  1541. if(n < m)
  1542. error(Eshortdraw);
  1543. dst = drawimage(client, a+1);
  1544. dstid = BGLONG(a+1);
  1545. drawpoint(&p, a+5);
  1546. drawpoint(&q, a+13);
  1547. e0 = BGLONG(a+21);
  1548. e1 = BGLONG(a+25);
  1549. j = BGLONG(a+29);
  1550. if(j < 0)
  1551. error("negative line width");
  1552. src = drawimage(client, a+33);
  1553. drawpoint(&sp, a+37);
  1554. op = drawclientop(client);
  1555. memline(dst, p, q, e0, e1, j, src, sp, op);
  1556. /* avoid memlinebbox if possible */
  1557. if(dstid==0 || dst->layer!=nil){
  1558. /* BUG: this is terribly inefficient: update maximal containing rect*/
  1559. r = memlinebbox(p, q, e0, e1, j);
  1560. dstflush(dstid, dst, insetrect(r, -(1+1+j)));
  1561. }
  1562. continue;
  1563. /* create image mask: 'm' newid[4] id[4] */
  1564. /*
  1565. *
  1566. case 'm':
  1567. printmesg("LL", a, 0);
  1568. m = 4+4;
  1569. if(n < m)
  1570. error(Eshortdraw);
  1571. break;
  1572. *
  1573. */
  1574. /* attach to a named image: 'n' dstid[4] j[1] name[j] */
  1575. case 'n':
  1576. printmesg(fmt="Lz", a, 0);
  1577. m = 1+4+1;
  1578. if(n < m)
  1579. error(Eshortdraw);
  1580. j = a[5];
  1581. if(j == 0) /* give me a non-empty name please */
  1582. error(Eshortdraw);
  1583. m += j;
  1584. if(n < m)
  1585. error(Eshortdraw);
  1586. dstid = BGLONG(a+1);
  1587. if(drawlookup(client, dstid, 0))
  1588. error(Eimageexists);
  1589. dn = drawlookupname(j, (char*)a+6);
  1590. if(dn == nil)
  1591. error(Enoname);
  1592. if(drawinstall(client, dstid, dn->dimage->image, 0) == 0)
  1593. error(Edrawmem);
  1594. di = drawlookup(client, dstid, 0);
  1595. if(di == 0)
  1596. error("draw: cannot happen");
  1597. di->vers = dn->vers;
  1598. di->name = smalloc(j+1);
  1599. di->fromname = dn->dimage;
  1600. di->fromname->ref++;
  1601. memmove(di->name, a+6, j);
  1602. di->name[j] = 0;
  1603. client->infoid = dstid;
  1604. continue;
  1605. /* name an image: 'N' dstid[4] in[1] j[1] name[j] */
  1606. case 'N':
  1607. printmesg(fmt="Lbz", a, 0);
  1608. m = 1+4+1+1;
  1609. if(n < m)
  1610. error(Eshortdraw);
  1611. c = a[5];
  1612. j = a[6];
  1613. if(j == 0) /* give me a non-empty name please */
  1614. error(Eshortdraw);
  1615. m += j;
  1616. if(n < m)
  1617. error(Eshortdraw);
  1618. di = drawlookup(client, BGLONG(a+1), 0);
  1619. if(di == 0)
  1620. error(Enodrawimage);
  1621. if(di->name)
  1622. error(Enamed);
  1623. if(c)
  1624. drawaddname(client, di, j, (char*)a+7);
  1625. else{
  1626. dn = drawlookupname(j, (char*)a+7);
  1627. if(dn == nil)
  1628. error(Enoname);
  1629. if(dn->dimage != di)
  1630. error(Ewrongname);
  1631. drawdelname(dn);
  1632. }
  1633. continue;
  1634. /* position window: 'o' id[4] r.min [2*4] screenr.min [2*4] */
  1635. case 'o':
  1636. printmesg(fmt="LPP", a, 0);
  1637. m = 1+4+2*4+2*4;
  1638. if(n < m)
  1639. error(Eshortdraw);
  1640. dst = drawimage(client, a+1);
  1641. if(dst->layer){
  1642. drawpoint(&p, a+5);
  1643. drawpoint(&q, a+13);
  1644. r = dst->layer->screenr;
  1645. ni = memlorigin(dst, p, q);
  1646. if(ni < 0)
  1647. error("image origin failed");
  1648. if(ni > 0){
  1649. addflush(r);
  1650. addflush(dst->layer->screenr);
  1651. ll = drawlookup(client, BGLONG(a+1), 1);
  1652. drawrefreshscreen(ll, client);
  1653. }
  1654. }
  1655. continue;
  1656. /* set compositing operator for next draw operation: 'O' op */
  1657. case 'O':
  1658. printmesg(fmt="b", a, 0);
  1659. m = 1+1;
  1660. if(n < m)
  1661. error(Eshortdraw);
  1662. client->op = a[1];
  1663. continue;
  1664. /* filled polygon: 'P' dstid[4] n[2] wind[4] ignore[2*4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
  1665. /* polygon: 'p' dstid[4] n[2] end0[4] end1[4] radius[4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
  1666. case 'p':
  1667. case 'P':
  1668. printmesg(fmt="LslllLPP", a, 0);
  1669. m = 1+4+2+4+4+4+4+2*4;
  1670. if(n < m)
  1671. error(Eshortdraw);
  1672. dstid = BGLONG(a+1);
  1673. dst = drawimage(client, a+1);
  1674. ni = BGSHORT(a+5);
  1675. if(ni < 0)
  1676. error("negative count in polygon");
  1677. e0 = BGLONG(a+7);
  1678. e1 = BGLONG(a+11);
  1679. j = 0;
  1680. if(*a == 'p'){
  1681. j = BGLONG(a+15);
  1682. if(j < 0)
  1683. error("negative polygon line width");
  1684. }
  1685. src = drawimage(client, a+19);
  1686. drawpoint(&sp, a+23);
  1687. drawpoint(&p, a+31);
  1688. ni++;
  1689. pp = malloc(ni*sizeof(Point));
  1690. if(pp == nil)
  1691. error(Enomem);
  1692. doflush = 0;
  1693. if(dstid==0 || (dst->layer && dst->layer->screen->image->data == screenimage->data))
  1694. doflush = 1; /* simplify test in loop */
  1695. ox = oy = 0;
  1696. esize = 0;
  1697. u = a+m;
  1698. for(y=0; y<ni; y++){
  1699. q = p;
  1700. oesize = esize;
  1701. u = drawcoord(u, a+n, ox, &p.x);
  1702. u = drawcoord(u, a+n, oy, &p.y);
  1703. ox = p.x;
  1704. oy = p.y;
  1705. if(doflush){
  1706. esize = j;
  1707. if(*a == 'p'){
  1708. if(y == 0){
  1709. c = memlineendsize(e0);
  1710. if(c > esize)
  1711. esize = c;
  1712. }
  1713. if(y == ni-1){
  1714. c = memlineendsize(e1);
  1715. if(c > esize)
  1716. esize = c;
  1717. }
  1718. }
  1719. if(*a=='P' && e0!=1 && e0 !=~0)
  1720. r = dst->clipr;
  1721. else if(y > 0){
  1722. r = Rect(q.x-oesize, q.y-oesize, q.x+oesize+1, q.y+oesize+1);
  1723. combinerect(&r, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
  1724. }
  1725. if(rectclip(&r, dst->clipr)) /* should perhaps be an arg to dstflush */
  1726. dstflush(dstid, dst, r);
  1727. }
  1728. pp[y] = p;
  1729. }
  1730. if(y == 1)
  1731. dstflush(dstid, dst, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
  1732. op = drawclientop(client);
  1733. if(*a == 'p')
  1734. mempoly(dst, pp, ni, e0, e1, j, src, sp, op);
  1735. else
  1736. memfillpoly(dst, pp, ni, e0, src, sp, op);
  1737. free(pp);
  1738. m = u-a;
  1739. continue;
  1740. /* read: 'r' id[4] R[4*4] */
  1741. case 'r':
  1742. printmesg(fmt="LR", a, 0);
  1743. m = 1+4+4*4;
  1744. if(n < m)
  1745. error(Eshortdraw);
  1746. i = drawimage(client, a+1);
  1747. drawrectangle(&r, a+5);
  1748. if(!rectinrect(r, i->r))
  1749. error(Ereadoutside);
  1750. c = bytesperline(r, i->depth);
  1751. c *= Dy(r);
  1752. free(client->readdata);
  1753. client->readdata = mallocz(c, 0);
  1754. if(client->readdata == nil)
  1755. error("readimage malloc failed");
  1756. client->nreaddata = memunload(i, r, client->readdata, c);
  1757. if(client->nreaddata < 0){
  1758. free(client->readdata);
  1759. client->readdata = nil;
  1760. error("bad readimage call");
  1761. }
  1762. continue;
  1763. /* string: 's' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] ni*(index[2]) */
  1764. /* 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]) */
  1765. case 's':
  1766. case 'x':
  1767. printmesg(fmt="LLLPRPs", a, 0);
  1768. m = 1+4+4+4+2*4+4*4+2*4+2;
  1769. if(*a == 'x')
  1770. m += 4+2*4;
  1771. if(n < m)
  1772. error(Eshortdraw);
  1773. dst = drawimage(client, a+1);
  1774. dstid = BGLONG(a+1);
  1775. src = drawimage(client, a+5);
  1776. font = drawlookup(client, BGLONG(a+9), 1);
  1777. if(font == 0)
  1778. error(Enodrawimage);
  1779. if(font->nfchar == 0)
  1780. error(Enotfont);
  1781. drawpoint(&p, a+13);
  1782. drawrectangle(&r, a+21);
  1783. drawpoint(&sp, a+37);
  1784. ni = BGSHORT(a+45);
  1785. u = a+m;
  1786. m += ni*2;
  1787. if(n < m)
  1788. error(Eshortdraw);
  1789. clipr = dst->clipr;
  1790. dst->clipr = r;
  1791. op = drawclientop(client);
  1792. if(*a == 'x'){
  1793. /* paint background */
  1794. l = drawimage(client, a+47);
  1795. drawpoint(&q, a+51);
  1796. r.min.x = p.x;
  1797. r.min.y = p.y-font->ascent;
  1798. r.max.x = p.x;
  1799. r.max.y = r.min.y+Dy(font->image->r);
  1800. j = ni;
  1801. while(--j >= 0){
  1802. ci = BGSHORT(u);
  1803. if(ci<0 || ci>=font->nfchar){
  1804. dst->clipr = clipr;
  1805. error(Eindex);
  1806. }
  1807. r.max.x += font->fchar[ci].width;
  1808. u += 2;
  1809. }
  1810. memdraw(dst, r, l, q, memopaque, ZP, op);
  1811. u -= 2*ni;
  1812. }
  1813. q = p;
  1814. while(--ni >= 0){
  1815. ci = BGSHORT(u);
  1816. if(ci<0 || ci>=font->nfchar){
  1817. dst->clipr = clipr;
  1818. error(Eindex);
  1819. }
  1820. q = drawchar(dst, q, src, &sp, font, ci, op);
  1821. u += 2;
  1822. }
  1823. dst->clipr = clipr;
  1824. p.y -= font->ascent;
  1825. dstflush(dstid, dst, Rect(p.x, p.y, q.x, p.y+Dy(font->image->r)));
  1826. continue;
  1827. /* use public screen: 'S' id[4] chan[4] */
  1828. case 'S':
  1829. printmesg(fmt="Ll", a, 0);
  1830. m = 1+4+4;
  1831. if(n < m)
  1832. error(Eshortdraw);
  1833. dstid = BGLONG(a+1);
  1834. if(dstid == 0)
  1835. error(Ebadarg);
  1836. dscrn = drawlookupdscreen(dstid);
  1837. if(dscrn==0 || (dscrn->public==0 && dscrn->owner!=client))
  1838. error(Enodrawscreen);
  1839. if(dscrn->screen->image->chan != BGLONG(a+5))
  1840. error("inconsistent chan");
  1841. if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0)
  1842. error(Edrawmem);
  1843. continue;
  1844. /* top or bottom windows: 't' top[1] nw[2] n*id[4] */
  1845. case 't':
  1846. printmesg(fmt="bsL", a, 0);
  1847. m = 1+1+2;
  1848. if(n < m)
  1849. error(Eshortdraw);
  1850. nw = BGSHORT(a+2);
  1851. if(nw < 0)
  1852. error(Ebadarg);
  1853. if(nw == 0)
  1854. continue;
  1855. m += nw*4;
  1856. if(n < m)
  1857. error(Eshortdraw);
  1858. lp = malloc(nw*sizeof(Memimage*));
  1859. if(lp == 0)
  1860. error(Enomem);
  1861. if(waserror()){
  1862. free(lp);
  1863. nexterror();
  1864. }
  1865. for(j=0; j<nw; j++)
  1866. lp[j] = drawimage(client, a+1+1+2+j*4);
  1867. if(lp[0]->layer == 0)
  1868. error("images are not windows");
  1869. for(j=1; j<nw; j++)
  1870. if(lp[j]->layer->screen != lp[0]->layer->screen)
  1871. error("images not on same screen");
  1872. if(a[1])
  1873. memltofrontn(lp, nw);
  1874. else
  1875. memltorearn(lp, nw);
  1876. if(lp[0]->layer->screen->image->data == screenimage->data)
  1877. for(j=0; j<nw; j++)
  1878. addflush(lp[j]->layer->screenr);
  1879. ll = drawlookup(client, BGLONG(a+1+1+2), 1);
  1880. drawrefreshscreen(ll, client);
  1881. poperror();
  1882. free(lp);
  1883. continue;
  1884. /* visible: 'v' */
  1885. case 'v':
  1886. printmesg(fmt="", a, 0);
  1887. m = 1;
  1888. drawflush();
  1889. continue;
  1890. /* write: 'y' id[4] R[4*4] data[x*1] */
  1891. /* write from compressed data: 'Y' id[4] R[4*4] data[x*1] */
  1892. case 'y':
  1893. case 'Y':
  1894. printmesg(fmt="LR", a, 0);
  1895. // iprint("load %c\n", *a);
  1896. m = 1+4+4*4;
  1897. if(n < m)
  1898. error(Eshortdraw);
  1899. dstid = BGLONG(a+1);
  1900. dst = drawimage(client, a+1);
  1901. drawrectangle(&r, a+5);
  1902. if(!rectinrect(r, dst->r))
  1903. error(Ewriteoutside);
  1904. y = memload(dst, r, a+m, n-m, *a=='Y');
  1905. if(y < 0)
  1906. error("bad writeimage call");
  1907. dstflush(dstid, dst, r);
  1908. m += y;
  1909. continue;
  1910. }
  1911. }
  1912. poperror();
  1913. }
  1914. Dev drawdevtab = {
  1915. 'i',
  1916. "draw",
  1917. devreset,
  1918. devinit,
  1919. devshutdown,
  1920. drawattach,
  1921. drawwalk,
  1922. drawstat,
  1923. drawopen,
  1924. devcreate,
  1925. drawclose,
  1926. drawread,
  1927. devbread,
  1928. drawwrite,
  1929. devbwrite,
  1930. devremove,
  1931. devwstat,
  1932. };
  1933. /*
  1934. * On 8 bit displays, load the default color map
  1935. */
  1936. void
  1937. drawcmap(void)
  1938. {
  1939. int r, g, b, cr, cg, cb, v;
  1940. int num, den;
  1941. int i, j;
  1942. drawactive(1); /* to restore map from backup */
  1943. for(r=0,i=0; r!=4; r++)
  1944. for(v=0; v!=4; v++,i+=16){
  1945. for(g=0,j=v-r; g!=4; g++)
  1946. for(b=0;b!=4;b++,j++){
  1947. den = r;
  1948. if(g > den)
  1949. den = g;
  1950. if(b > den)
  1951. den = b;
  1952. if(den == 0) /* divide check -- pick grey shades */
  1953. cr = cg = cb = v*17;
  1954. else{
  1955. num = 17*(4*den+v);
  1956. cr = r*num/den;
  1957. cg = g*num/den;
  1958. cb = b*num/den;
  1959. }
  1960. setcolor(i+(j&15),
  1961. cr*0x01010101, cg*0x01010101, cb*0x01010101);
  1962. }
  1963. }
  1964. }
  1965. void
  1966. drawblankscreen(int blank)
  1967. {
  1968. int i, nc;
  1969. ulong *p;
  1970. if(blank == sdraw.blanked)
  1971. return;
  1972. if(!candlock())
  1973. return;
  1974. if(!initscreenimage()){
  1975. dunlock();
  1976. return;
  1977. }
  1978. p = sdraw.savemap;
  1979. nc = screenimage->depth > 8 ? 256 : 1<<screenimage->depth;
  1980. /*
  1981. * blankscreen uses the hardware to blank the screen
  1982. * when possible. to help in cases when it is not possible,
  1983. * we set the color map to be all black.
  1984. */
  1985. if(blank == 0){ /* turn screen on */
  1986. for(i=0; i<nc; i++, p+=3)
  1987. setcolor(i, p[0], p[1], p[2]);
  1988. blankscreen(0);
  1989. }else{ /* turn screen off */
  1990. blankscreen(1);
  1991. for(i=0; i<nc; i++, p+=3){
  1992. getcolor(i, &p[0], &p[1], &p[2]);
  1993. setcolor(i, 0, 0, 0);
  1994. }
  1995. }
  1996. sdraw.blanked = blank;
  1997. dunlock();
  1998. }
  1999. /*
  2000. * record activity on screen, changing blanking as appropriate
  2001. */
  2002. void
  2003. drawactive(int active)
  2004. {
  2005. if(active){
  2006. drawblankscreen(0);
  2007. sdraw.blanktime = MACHP(0)->ticks;
  2008. }else{
  2009. if(blanktime && sdraw.blanktime && TK2SEC(MACHP(0)->ticks - sdraw.blanktime)/60 >= blanktime)
  2010. drawblankscreen(1);
  2011. }
  2012. }
  2013. int
  2014. drawidletime(void)
  2015. {
  2016. return TK2SEC(MACHP(0)->ticks - sdraw.blanktime)/60;
  2017. }