xfid.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <thread.h>
  5. #include <cursor.h>
  6. #include <mouse.h>
  7. #include <keyboard.h>
  8. #include <frame.h>
  9. #include <fcall.h>
  10. #include <plumb.h>
  11. #include "dat.h"
  12. #include "fns.h"
  13. #define MAXSNARF 100*1024
  14. char Einuse[] = "file in use";
  15. char Edeleted[] = "window deleted";
  16. char Ebadreq[] = "bad graphics request";
  17. char Etooshort[] = "buffer too small";
  18. char Ebadtile[] = "unknown tile";
  19. char Eshort[] = "short i/o request";
  20. char Elong[] = "snarf buffer too long";
  21. char Eunkid[] = "unknown id in attach";
  22. char Ebadrect[] = "bad rectangle in attach";
  23. char Ewindow[] = "cannot make window";
  24. char Enowindow[] = "window has no image";
  25. char Ebadmouse[] = "bad format on /dev/mouse";
  26. char Ebadwrect[] = "rectangle outside screen";
  27. char Ebadoffset[] = "window read not on scan line boundary";
  28. extern char Eperm[];
  29. static Xfid *xfidfree;
  30. static Xfid *xfid;
  31. static Channel *cxfidalloc; /* chan(Xfid*) */
  32. static Channel *cxfidfree; /* chan(Xfid*) */
  33. static char *tsnarf;
  34. static int ntsnarf;
  35. void
  36. xfidallocthread(void*)
  37. {
  38. Xfid *x;
  39. enum { Alloc, Free, N };
  40. static Alt alts[N+1];
  41. alts[Alloc].c = cxfidalloc;
  42. alts[Alloc].v = nil;
  43. alts[Alloc].op = CHANRCV;
  44. alts[Free].c = cxfidfree;
  45. alts[Free].v = &x;
  46. alts[Free].op = CHANRCV;
  47. alts[N].op = CHANEND;
  48. for(;;){
  49. switch(alt(alts)){
  50. case Alloc:
  51. x = xfidfree;
  52. if(x)
  53. xfidfree = x->free;
  54. else{
  55. x = emalloc(sizeof(Xfid));
  56. x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
  57. x->flushc = chancreate(sizeof(int), 0); /* notification only; no data */
  58. x->flushtag = -1;
  59. x->next = xfid;
  60. xfid = x;
  61. threadcreate(xfidctl, x, 16384);
  62. }
  63. if(x->ref != 0){
  64. fprint(2, "%p incref %ld\n", x, x->ref);
  65. error("incref");
  66. }
  67. if(x->flushtag != -1)
  68. error("flushtag in allocate");
  69. incref(x);
  70. sendp(cxfidalloc, x);
  71. break;
  72. case Free:
  73. if(x->ref != 0){
  74. fprint(2, "%p decref %ld\n", x, x->ref);
  75. error("decref");
  76. }
  77. if(x->flushtag != -1)
  78. error("flushtag in free");
  79. x->free = xfidfree;
  80. xfidfree = x;
  81. break;
  82. }
  83. }
  84. }
  85. Channel*
  86. xfidinit(void)
  87. {
  88. cxfidalloc = chancreate(sizeof(Xfid*), 0);
  89. cxfidfree = chancreate(sizeof(Xfid*), 0);
  90. threadcreate(xfidallocthread, nil, STACK);
  91. return cxfidalloc;
  92. }
  93. void
  94. xfidctl(void *arg)
  95. {
  96. Xfid *x;
  97. void (*f)(Xfid*);
  98. char buf[64];
  99. x = arg;
  100. snprint(buf, sizeof buf, "xfid.%p", x);
  101. threadsetname(buf);
  102. for(;;){
  103. f = recvp(x->c);
  104. (*f)(x);
  105. if(decref(x) == 0)
  106. sendp(cxfidfree, x);
  107. }
  108. }
  109. void
  110. xfidflush(Xfid *x)
  111. {
  112. Fcall t;
  113. Xfid *xf;
  114. for(xf=xfid; xf; xf=xf->next)
  115. if(xf->flushtag == x->oldtag){
  116. xf->flushtag = -1;
  117. xf->flushing = TRUE;
  118. incref(xf); /* to hold data structures up at tail of synchronization */
  119. if(xf->ref == 1)
  120. error("ref 1 in flush");
  121. if(canqlock(&xf->active)){
  122. qunlock(&xf->active);
  123. sendul(xf->flushc, 0);
  124. }else{
  125. qlock(&xf->active); /* wait for him to finish */
  126. qunlock(&xf->active);
  127. }
  128. xf->flushing = FALSE;
  129. if(decref(xf) == 0)
  130. sendp(cxfidfree, xf);
  131. break;
  132. }
  133. filsysrespond(x->fs, x, &t, nil);
  134. }
  135. void
  136. xfidattach(Xfid *x)
  137. {
  138. Fcall t;
  139. int id, hideit, scrollit;
  140. Window *w;
  141. char *err, *n, *dir, errbuf[ERRMAX];
  142. int pid, newlymade;
  143. Rectangle r;
  144. Image *i;
  145. t.qid = x->f->qid;
  146. qlock(&all);
  147. w = nil;
  148. err = Eunkid;
  149. newlymade = FALSE;
  150. hideit = 0;
  151. if(x->aname[0] == 'N'){ /* N 100,100, 200, 200 - old syntax */
  152. n = x->aname+1;
  153. pid = strtoul(n, &n, 0);
  154. if(*n == ',')
  155. n++;
  156. r.min.x = strtoul(n, &n, 0);
  157. if(*n == ',')
  158. n++;
  159. r.min.y = strtoul(n, &n, 0);
  160. if(*n == ',')
  161. n++;
  162. r.max.x = strtoul(n, &n, 0);
  163. if(*n == ',')
  164. n++;
  165. r.max.y = strtoul(n, &n, 0);
  166. Allocate:
  167. if(!goodrect(r))
  168. err = Ebadrect;
  169. else{
  170. if(hideit)
  171. i = allocimage(display, r, screen->chan, 0, DWhite);
  172. else
  173. i = allocwindow(wscreen, r, Refbackup, DWhite);
  174. if(i){
  175. border(i, r, Selborder, display->black, ZP);
  176. if(pid == 0)
  177. pid = -1; /* make sure we don't pop a shell! - UGH */
  178. w = new(i, hideit, scrolling, pid, nil, nil, nil);
  179. flushimage(display, 1);
  180. newlymade = TRUE;
  181. }else
  182. err = Ewindow;
  183. }
  184. }else if(strncmp(x->aname, "new", 3) == 0){ /* new -dx -dy - new syntax, as in wctl */
  185. pid = 0;
  186. if(parsewctl(nil, ZR, &r, &pid, nil, &hideit, &scrollit, &dir, x->aname, errbuf) < 0)
  187. err = errbuf;
  188. else
  189. goto Allocate;
  190. }else{
  191. id = atoi(x->aname);
  192. w = wlookid(id);
  193. }
  194. x->f->w = w;
  195. if(w == nil){
  196. qunlock(&all);
  197. x->f->busy = FALSE;
  198. filsysrespond(x->fs, x, &t, err);
  199. return;
  200. }
  201. if(!newlymade) /* counteract dec() in winshell() */
  202. incref(w);
  203. qunlock(&all);
  204. filsysrespond(x->fs, x, &t, nil);
  205. }
  206. void
  207. xfidopen(Xfid *x)
  208. {
  209. Fcall t;
  210. Window *w;
  211. w = x->f->w;
  212. if(w->deleted){
  213. filsysrespond(x->fs, x, &t, Edeleted);
  214. return;
  215. }
  216. switch(FILE(x->f->qid)){
  217. case Qconsctl:
  218. if(w->ctlopen){
  219. filsysrespond(x->fs, x, &t, Einuse);
  220. return;
  221. }
  222. w->ctlopen = TRUE;
  223. break;
  224. case Qkbdin:
  225. if(w != wkeyboard){
  226. filsysrespond(x->fs, x, &t, Eperm);
  227. return;
  228. }
  229. break;
  230. case Qmouse:
  231. if(w->mouseopen){
  232. filsysrespond(x->fs, x, &t, Einuse);
  233. return;
  234. }
  235. /*
  236. * Reshaped: there's a race if the appl. opens the
  237. * window, is resized, and then opens the mouse,
  238. * but that's rare. The alternative is to generate
  239. * a resized event every time a new program starts
  240. * up in a window that has been resized since the
  241. * dawn of time. We choose the lesser evil.
  242. */
  243. w->resized = FALSE;
  244. w->mouseopen = TRUE;
  245. break;
  246. case Qsnarf:
  247. if(x->mode==ORDWR || x->mode==OWRITE){
  248. if(tsnarf)
  249. free(tsnarf); /* collision, but OK */
  250. ntsnarf = 0;
  251. tsnarf = malloc(1);
  252. }
  253. break;
  254. case Qwctl:
  255. if(x->mode==OREAD || x->mode==ORDWR){
  256. /*
  257. * It would be much nicer to implement fan-out for wctl reads,
  258. * so multiple people can see the resizings, but rio just isn't
  259. * structured for that. It's structured for /dev/cons, which gives
  260. * alternate data to alternate readers. So to keep things sane for
  261. * wctl, we compromise and give an error if two people try to
  262. * open it. Apologies.
  263. */
  264. if(w->wctlopen){
  265. filsysrespond(x->fs, x, &t, Einuse);
  266. return;
  267. }
  268. w->wctlopen = TRUE;
  269. w->wctlready = 1;
  270. wsendctlmesg(w, Wakeup, ZR, nil);
  271. }
  272. break;
  273. }
  274. t.qid = x->f->qid;
  275. t.iounit = messagesize-IOHDRSZ;
  276. x->f->open = TRUE;
  277. x->f->mode = x->mode;
  278. filsysrespond(x->fs, x, &t, nil);
  279. }
  280. void
  281. xfidclose(Xfid *x)
  282. {
  283. Fcall t;
  284. Window *w;
  285. int nb, nulls;
  286. w = x->f->w;
  287. switch(FILE(x->f->qid)){
  288. case Qconsctl:
  289. if(w->rawing){
  290. w->rawing = FALSE;
  291. wsendctlmesg(w, Rawoff, ZR, nil);
  292. }
  293. if(w->holding){
  294. w->holding = FALSE;
  295. wsendctlmesg(w, Holdoff, ZR, nil);
  296. }
  297. w->ctlopen = FALSE;
  298. break;
  299. case Qcursor:
  300. w->cursorp = nil;
  301. wsetcursor(w, FALSE);
  302. break;
  303. case Qmouse:
  304. w->resized = FALSE;
  305. w->mouseopen = FALSE;
  306. if(w->i != nil)
  307. wsendctlmesg(w, Refresh, w->i->r, nil);
  308. break;
  309. /* odd behavior but really ok: replace snarf buffer when /dev/snarf is closed */
  310. case Qsnarf:
  311. if(x->f->mode==ORDWR || x->f->mode==OWRITE){
  312. snarf = runerealloc(snarf, ntsnarf+1);
  313. cvttorunes(tsnarf, ntsnarf, snarf, &nb, &nsnarf, &nulls);
  314. free(tsnarf);
  315. tsnarf = nil;
  316. ntsnarf = 0;
  317. }
  318. break;
  319. case Qwctl:
  320. if(x->f->mode==OREAD || x->f->mode==ORDWR)
  321. w->wctlopen = FALSE;
  322. break;
  323. }
  324. wclose(w);
  325. filsysrespond(x->fs, x, &t, nil);
  326. }
  327. void
  328. xfidwrite(Xfid *x)
  329. {
  330. Fcall fc;
  331. int c, cnt, qid, nb, off, nr;
  332. char buf[256], *p;
  333. Point pt;
  334. Window *w;
  335. Rune *r;
  336. Conswritemesg cwm;
  337. Stringpair pair;
  338. enum { CWdata, CWflush, NCW };
  339. Alt alts[NCW+1];
  340. w = x->f->w;
  341. if(w->deleted){
  342. filsysrespond(x->fs, x, &fc, Edeleted);
  343. return;
  344. }
  345. qid = FILE(x->f->qid);
  346. cnt = x->count;
  347. off = x->offset;
  348. x->data[cnt] = 0;
  349. switch(qid){
  350. case Qcons:
  351. nr = x->f->nrpart;
  352. if(nr > 0){
  353. memmove(x->data+nr, x->data, cnt); /* there's room: see malloc in filsysproc */
  354. memmove(x->data, x->f->rpart, nr);
  355. cnt += nr;
  356. x->f->nrpart = 0;
  357. }
  358. r = runemalloc(cnt);
  359. cvttorunes(x->data, cnt-UTFmax, r, &nb, &nr, nil);
  360. /* approach end of buffer */
  361. while(fullrune(x->data+nb, cnt-nb)){
  362. c = nb;
  363. nb += chartorune(&r[nr], x->data+c);
  364. if(r[nr])
  365. nr++;
  366. }
  367. if(nb < cnt){
  368. memmove(x->f->rpart, x->data+nb, cnt-nb);
  369. x->f->nrpart = cnt-nb;
  370. }
  371. x->flushtag = x->tag;
  372. alts[CWdata].c = w->conswrite;
  373. alts[CWdata].v = &cwm;
  374. alts[CWdata].op = CHANRCV;
  375. alts[CWflush].c = x->flushc;
  376. alts[CWflush].v = nil;
  377. alts[CWflush].op = CHANRCV;
  378. alts[NCW].op = CHANEND;
  379. switch(alt(alts)){
  380. case CWdata:
  381. break;
  382. case CWflush:
  383. filsyscancel(x);
  384. return;
  385. }
  386. /* received data */
  387. x->flushtag = -1;
  388. if(x->flushing){
  389. recv(x->flushc, nil); /* wake up flushing xfid */
  390. pair.s = runemalloc(1);
  391. pair.ns = 0;
  392. send(cwm.cw, &pair); /* wake up window with empty data */
  393. filsyscancel(x);
  394. return;
  395. }
  396. qlock(&x->active);
  397. pair.s = r;
  398. pair.ns = nr;
  399. send(cwm.cw, &pair);
  400. fc.count = x->count;
  401. filsysrespond(x->fs, x, &fc, nil);
  402. qunlock(&x->active);
  403. return;
  404. case Qconsctl:
  405. if(strncmp(x->data, "holdon", 6)==0){
  406. if(w->holding++ == 0)
  407. wsendctlmesg(w, Holdon, ZR, nil);
  408. break;
  409. }
  410. if(strncmp(x->data, "holdoff", 7)==0 && w->holding){
  411. if(--w->holding == FALSE)
  412. wsendctlmesg(w, Holdoff, ZR, nil);
  413. break;
  414. }
  415. if(strncmp(x->data, "rawon", 5)==0){
  416. if(w->holding){
  417. w->holding = FALSE;
  418. wsendctlmesg(w, Holdoff, ZR, nil);
  419. }
  420. if(w->rawing++ == 0)
  421. wsendctlmesg(w, Rawon, ZR, nil);
  422. break;
  423. }
  424. if(strncmp(x->data, "rawoff", 6)==0 && w->rawing){
  425. if(--w->rawing == 0)
  426. wsendctlmesg(w, Rawoff, ZR, nil);
  427. break;
  428. }
  429. filsysrespond(x->fs, x, &fc, "unknown control message");
  430. return;
  431. case Qcursor:
  432. if(cnt < 2*4+2*2*16)
  433. w->cursorp = nil;
  434. else{
  435. w->cursor.offset.x = BGLONG(x->data+0*4);
  436. w->cursor.offset.y = BGLONG(x->data+1*4);
  437. memmove(w->cursor.clr, x->data+2*4, 2*2*16);
  438. w->cursorp = &w->cursor;
  439. }
  440. wsetcursor(w, !sweeping);
  441. break;
  442. case Qlabel:
  443. if(off != 0){
  444. filsysrespond(x->fs, x, &fc, "non-zero offset writing label");
  445. return;
  446. }
  447. free(w->label);
  448. w->label = emalloc(cnt+1);
  449. memmove(w->label, x->data, cnt);
  450. w->label[cnt] = 0;
  451. break;
  452. case Qmouse:
  453. if(w!=input || Dx(w->screenr)<=0)
  454. break;
  455. if(x->data[0] != 'm'){
  456. filsysrespond(x->fs, x, &fc, Ebadmouse);
  457. return;
  458. }
  459. p = nil;
  460. pt.x = strtoul(x->data+1, &p, 0);
  461. if(p == nil){
  462. filsysrespond(x->fs, x, &fc, Eshort);
  463. return;
  464. }
  465. pt.y = strtoul(p, nil, 0);
  466. if(w==input && wpointto(mouse->xy)==w)
  467. wsendctlmesg(w, Movemouse, Rpt(pt, pt), nil);
  468. break;
  469. case Qsnarf:
  470. /* always append only */
  471. if(ntsnarf > MAXSNARF){ /* avoid thrashing when people cut huge text */
  472. filsysrespond(x->fs, x, &fc, Elong);
  473. return;
  474. }
  475. tsnarf = erealloc(tsnarf, ntsnarf+cnt+1); /* room for NUL */
  476. memmove(tsnarf+ntsnarf, x->data, cnt);
  477. ntsnarf += cnt;
  478. snarfversion++;
  479. break;
  480. case Qwdir:
  481. if(cnt == 0)
  482. break;
  483. if(x->data[cnt-1] == '\n'){
  484. if(cnt == 1)
  485. break;
  486. x->data[cnt-1] = '\0';
  487. }
  488. /* assume data comes in a single write */
  489. /*
  490. * Problem: programs like dossrv, ftp produce illegal UTF;
  491. * we must cope by converting it first.
  492. */
  493. snprint(buf, sizeof buf, "%.*s", cnt, x->data);
  494. if(buf[0] == '/'){
  495. free(w->dir);
  496. w->dir = estrdup(buf);
  497. }else{
  498. p = emalloc(strlen(w->dir) + 1 + strlen(buf) + 1);
  499. sprint(p, "%s/%s", w->dir, buf);
  500. free(w->dir);
  501. w->dir = cleanname(p);
  502. }
  503. break;
  504. case Qkbdin:
  505. keyboardsend(x->data, cnt);
  506. break;
  507. case Qwctl:
  508. if(writewctl(x, buf) < 0){
  509. filsysrespond(x->fs, x, &fc, buf);
  510. return;
  511. }
  512. flushimage(display, 1);
  513. break;
  514. default:
  515. fprint(2, buf, "unknown qid %d in write\n", qid);
  516. sprint(buf, "unknown qid in write");
  517. filsysrespond(x->fs, x, &fc, buf);
  518. return;
  519. }
  520. fc.count = cnt;
  521. filsysrespond(x->fs, x, &fc, nil);
  522. }
  523. int
  524. readwindow(Image *i, char *t, Rectangle r, int offset, int n)
  525. {
  526. int ww, y;
  527. offset -= 5*12;
  528. ww = bytesperline(r, screen->depth);
  529. r.min.y += offset/ww;
  530. if(r.min.y >= r.max.y)
  531. return 0;
  532. y = r.min.y + n/ww;
  533. if(y < r.max.y)
  534. r.max.y = y;
  535. if(r.max.y <= r.min.y)
  536. return 0;
  537. return unloadimage(i, r, (uchar*)t, n);
  538. }
  539. void
  540. xfidread(Xfid *x)
  541. {
  542. Fcall fc;
  543. int n, off, cnt, c;
  544. uint qid;
  545. char buf[128], *t;
  546. char cbuf[30];
  547. Window *w;
  548. Mouse ms;
  549. Rectangle r;
  550. Image *i;
  551. Channel *c1, *c2; /* chan (tuple(char*, int)) */
  552. Consreadmesg crm;
  553. Mousereadmesg mrm;
  554. Consreadmesg cwrm;
  555. Stringpair pair;
  556. enum { CRdata, CRflush, NCR };
  557. enum { MRdata, MRflush, NMR };
  558. enum { WCRdata, WCRflush, NWCR };
  559. Alt alts[NCR+1];
  560. w = x->f->w;
  561. if(w->deleted){
  562. filsysrespond(x->fs, x, &fc, Edeleted);
  563. return;
  564. }
  565. qid = FILE(x->f->qid);
  566. off = x->offset;
  567. cnt = x->count;
  568. switch(qid){
  569. case Qcons:
  570. x->flushtag = x->tag;
  571. alts[CRdata].c = w->consread;
  572. alts[CRdata].v = &crm;
  573. alts[CRdata].op = CHANRCV;
  574. alts[CRflush].c = x->flushc;
  575. alts[CRflush].v = nil;
  576. alts[CRflush].op = CHANRCV;
  577. alts[NMR].op = CHANEND;
  578. switch(alt(alts)){
  579. case CRdata:
  580. break;
  581. case CRflush:
  582. filsyscancel(x);
  583. return;
  584. }
  585. /* received data */
  586. x->flushtag = -1;
  587. c1 = crm.c1;
  588. c2 = crm.c2;
  589. t = malloc(cnt+UTFmax+1); /* room to unpack partial rune plus */
  590. pair.s = t;
  591. pair.ns = cnt;
  592. send(c1, &pair);
  593. if(x->flushing){
  594. recv(x->flushc, nil); /* wake up flushing xfid */
  595. recv(c2, nil); /* wake up window and toss data */
  596. free(t);
  597. filsyscancel(x);
  598. return;
  599. }
  600. qlock(&x->active);
  601. recv(c2, &pair);
  602. fc.data = pair.s;
  603. fc.count = pair.ns;
  604. filsysrespond(x->fs, x, &fc, nil);
  605. free(t);
  606. qunlock(&x->active);
  607. break;
  608. case Qlabel:
  609. n = strlen(w->label);
  610. if(off > n)
  611. off = n;
  612. if(off+cnt > n)
  613. cnt = n-off;
  614. fc.data = w->label+off;
  615. fc.count = cnt;
  616. filsysrespond(x->fs, x, &fc, nil);
  617. break;
  618. case Qmouse:
  619. x->flushtag = x->tag;
  620. alts[MRdata].c = w->mouseread;
  621. alts[MRdata].v = &mrm;
  622. alts[MRdata].op = CHANRCV;
  623. alts[MRflush].c = x->flushc;
  624. alts[MRflush].v = nil;
  625. alts[MRflush].op = CHANRCV;
  626. alts[NMR].op = CHANEND;
  627. switch(alt(alts)){
  628. case MRdata:
  629. break;
  630. case MRflush:
  631. filsyscancel(x);
  632. return;
  633. }
  634. /* received data */
  635. x->flushtag = -1;
  636. if(x->flushing){
  637. recv(x->flushc, nil); /* wake up flushing xfid */
  638. recv(mrm.cm, nil); /* wake up window and toss data */
  639. filsyscancel(x);
  640. return;
  641. }
  642. qlock(&x->active);
  643. recv(mrm.cm, &ms);
  644. c = 'm';
  645. if(w->resized)
  646. c = 'r';
  647. n = sprint(buf, "%c%11d %11d %11d %11ld ", c, ms.xy.x, ms.xy.y, ms.buttons, ms.msec);
  648. w->resized = 0;
  649. fc.data = buf;
  650. fc.count = min(n, cnt);
  651. filsysrespond(x->fs, x, &fc, nil);
  652. qunlock(&x->active);
  653. break;
  654. case Qcursor:
  655. filsysrespond(x->fs, x, &fc, "cursor read not implemented");
  656. break;
  657. /* The algorithm for snarf and text is expensive but easy and rarely used */
  658. case Qsnarf:
  659. getsnarf();
  660. if(nsnarf)
  661. t = runetobyte(snarf, nsnarf, &n);
  662. else {
  663. t = nil;
  664. n = 0;
  665. }
  666. goto Text;
  667. case Qtext:
  668. t = wcontents(w, &n);
  669. goto Text;
  670. Text:
  671. if(off > n){
  672. off = n;
  673. cnt = 0;
  674. }
  675. if(off+cnt > n)
  676. cnt = n-off;
  677. fc.data = t+off;
  678. fc.count = cnt;
  679. filsysrespond(x->fs, x, &fc, nil);
  680. free(t);
  681. break;
  682. case Qwdir:
  683. t = estrdup(w->dir);
  684. n = strlen(t);
  685. goto Text;
  686. case Qwinid:
  687. n = sprint(buf, "%11d ", w->id);
  688. t = estrdup(buf);
  689. goto Text;
  690. case Qwinname:
  691. n = strlen(w->name);
  692. if(n == 0){
  693. filsysrespond(x->fs, x, &fc, "window has no name");
  694. break;
  695. }
  696. t = estrdup(w->name);
  697. goto Text;
  698. case Qwindow:
  699. i = w->i;
  700. if(i == nil || Dx(w->screenr)<=0){
  701. filsysrespond(x->fs, x, &fc, Enowindow);
  702. return;
  703. }
  704. r = w->screenr;
  705. goto caseImage;
  706. case Qscreen:
  707. i = display->image;
  708. if(i == nil){
  709. filsysrespond(x->fs, x, &fc, "no top-level screen");
  710. break;
  711. }
  712. r = i->r;
  713. /* fall through */
  714. caseImage:
  715. if(off < 5*12){
  716. n = sprint(buf, "%11s %11d %11d %11d %11d ",
  717. chantostr(cbuf, screen->chan),
  718. i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y);
  719. t = estrdup(buf);
  720. goto Text;
  721. }
  722. t = malloc(cnt);
  723. fc.data = t;
  724. n = readwindow(i, t, r, off, cnt); /* careful; fc.count is unsigned */
  725. if(n < 0){
  726. buf[0] = 0;
  727. errstr(buf, sizeof buf);
  728. filsysrespond(x->fs, x, &fc, buf);
  729. }else{
  730. fc.count = n;
  731. filsysrespond(x->fs, x, &fc, nil);
  732. }
  733. free(t);
  734. return;
  735. case Qwctl: /* read returns rectangle, hangs if not resized */
  736. if(cnt < 4*12){
  737. filsysrespond(x->fs, x, &fc, Etooshort);
  738. break;
  739. }
  740. x->flushtag = x->tag;
  741. alts[WCRdata].c = w->wctlread;
  742. alts[WCRdata].v = &cwrm;
  743. alts[WCRdata].op = CHANRCV;
  744. alts[WCRflush].c = x->flushc;
  745. alts[WCRflush].v = nil;
  746. alts[WCRflush].op = CHANRCV;
  747. alts[NMR].op = CHANEND;
  748. switch(alt(alts)){
  749. case WCRdata:
  750. break;
  751. case WCRflush:
  752. filsyscancel(x);
  753. return;
  754. }
  755. /* received data */
  756. x->flushtag = -1;
  757. c1 = cwrm.c1;
  758. c2 = cwrm.c2;
  759. t = malloc(cnt+1); /* be sure to have room for NUL */
  760. pair.s = t;
  761. pair.ns = cnt+1;
  762. send(c1, &pair);
  763. if(x->flushing){
  764. recv(x->flushc, nil); /* wake up flushing xfid */
  765. recv(c2, nil); /* wake up window and toss data */
  766. free(t);
  767. filsyscancel(x);
  768. return;
  769. }
  770. qlock(&x->active);
  771. recv(c2, &pair);
  772. fc.data = pair.s;
  773. if(pair.ns > cnt)
  774. pair.ns = cnt;
  775. fc.count = pair.ns;
  776. filsysrespond(x->fs, x, &fc, nil);
  777. free(t);
  778. qunlock(&x->active);
  779. break;
  780. default:
  781. fprint(2, "unknown qid %d in read\n", qid);
  782. sprint(buf, "unknown qid in read");
  783. filsysrespond(x->fs, x, &fc, buf);
  784. break;
  785. }
  786. }