xfid.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. filsysrespond(x->fs, x, &t, err);
  198. return;
  199. }
  200. if(!newlymade) /* counteract dec() in winshell() */
  201. incref(w);
  202. qunlock(&all);
  203. filsysrespond(x->fs, x, &t, nil);
  204. }
  205. void
  206. xfidopen(Xfid *x)
  207. {
  208. Fcall t;
  209. Window *w;
  210. w = x->f->w;
  211. if(w->deleted){
  212. filsysrespond(x->fs, x, &t, Edeleted);
  213. return;
  214. }
  215. switch(FILE(x->f->qid)){
  216. case Qconsctl:
  217. if(w->ctlopen){
  218. filsysrespond(x->fs, x, &t, Einuse);
  219. return;
  220. }
  221. w->ctlopen = TRUE;
  222. break;
  223. case Qkbdin:
  224. if(w != wkeyboard){
  225. filsysrespond(x->fs, x, &t, Eperm);
  226. return;
  227. }
  228. break;
  229. case Qmouse:
  230. if(w->mouseopen){
  231. filsysrespond(x->fs, x, &t, Einuse);
  232. return;
  233. }
  234. /*
  235. * Reshaped: there's a race if the appl. opens the
  236. * window, is resized, and then opens the mouse,
  237. * but that's rare. The alternative is to generate
  238. * a resized event every time a new program starts
  239. * up in a window that has been resized since the
  240. * dawn of time. We choose the lesser evil.
  241. */
  242. w->resized = FALSE;
  243. w->mouseopen = TRUE;
  244. break;
  245. case Qsnarf:
  246. if(x->mode==ORDWR || x->mode==OWRITE){
  247. if(tsnarf)
  248. free(tsnarf); /* collision, but OK */
  249. ntsnarf = 0;
  250. tsnarf = malloc(1);
  251. }
  252. break;
  253. case Qwctl:
  254. if(x->mode==OREAD || x->mode==ORDWR){
  255. /*
  256. * It would be much nicer to implement fan-out for wctl reads,
  257. * so multiple people can see the resizings, but rio just isn't
  258. * structured for that. It's structured for /dev/cons, which gives
  259. * alternate data to alternate readers. So to keep things sane for
  260. * wctl, we compromise and give an error if two people try to
  261. * open it. Apologies.
  262. */
  263. if(w->wctlopen){
  264. filsysrespond(x->fs, x, &t, Einuse);
  265. return;
  266. }
  267. w->wctlopen = TRUE;
  268. w->wctlready = 1;
  269. wsendctlmesg(w, Wakeup, ZR, nil);
  270. }
  271. break;
  272. }
  273. t.qid = x->f->qid;
  274. t.iounit = messagesize-IOHDRSZ;
  275. x->f->open = TRUE;
  276. x->f->mode = x->mode;
  277. filsysrespond(x->fs, x, &t, nil);
  278. }
  279. void
  280. xfidclose(Xfid *x)
  281. {
  282. Fcall t;
  283. Window *w;
  284. int nb, nulls;
  285. w = x->f->w;
  286. switch(FILE(x->f->qid)){
  287. case Qconsctl:
  288. if(w->rawing){
  289. w->rawing = FALSE;
  290. wsendctlmesg(w, Rawoff, ZR, nil);
  291. }
  292. if(w->holding){
  293. w->holding = FALSE;
  294. wsendctlmesg(w, Holdoff, ZR, nil);
  295. }
  296. w->ctlopen = FALSE;
  297. break;
  298. case Qcursor:
  299. w->cursorp = nil;
  300. wsetcursor(w, FALSE);
  301. break;
  302. case Qmouse:
  303. w->resized = FALSE;
  304. w->mouseopen = FALSE;
  305. if(w->i != nil)
  306. wsendctlmesg(w, Refresh, w->i->r, nil);
  307. break;
  308. /* odd behavior but really ok: replace snarf buffer when /dev/snarf is closed */
  309. case Qsnarf:
  310. if(x->f->mode==ORDWR || x->f->mode==OWRITE){
  311. snarf = runerealloc(snarf, ntsnarf+1);
  312. cvttorunes(tsnarf, ntsnarf, snarf, &nb, &nsnarf, &nulls);
  313. free(tsnarf);
  314. tsnarf = nil;
  315. ntsnarf = 0;
  316. }
  317. break;
  318. case Qwctl:
  319. if(x->f->mode==OREAD || x->f->mode==ORDWR)
  320. w->wctlopen = FALSE;
  321. break;
  322. }
  323. wclose(w);
  324. filsysrespond(x->fs, x, &t, nil);
  325. }
  326. void
  327. xfidwrite(Xfid *x)
  328. {
  329. Fcall fc;
  330. int c, cnt, qid, nb, off, nr;
  331. char buf[256], *p;
  332. Point pt;
  333. Window *w;
  334. Rune *r;
  335. Conswritemesg cwm;
  336. Stringpair pair;
  337. enum { CWdata, CWflush, NCW };
  338. Alt alts[NCW+1];
  339. w = x->f->w;
  340. if(w->deleted){
  341. filsysrespond(x->fs, x, &fc, Edeleted);
  342. return;
  343. }
  344. qid = FILE(x->f->qid);
  345. cnt = x->count;
  346. off = x->offset;
  347. x->data[cnt] = 0;
  348. switch(qid){
  349. case Qcons:
  350. nr = x->f->nrpart;
  351. if(nr > 0){
  352. memmove(x->data+nr, x->data, cnt); /* there's room: see malloc in filsysproc */
  353. memmove(x->data, x->f->rpart, nr);
  354. cnt += nr;
  355. x->f->nrpart = 0;
  356. }
  357. r = runemalloc(cnt);
  358. cvttorunes(x->data, cnt-UTFmax, r, &nb, &nr, nil);
  359. /* approach end of buffer */
  360. while(fullrune(x->data+nb, cnt-nb)){
  361. c = nb;
  362. nb += chartorune(&r[nr], x->data+c);
  363. if(r[nr])
  364. nr++;
  365. }
  366. if(nb < cnt){
  367. memmove(x->f->rpart, x->data+nb, cnt-nb);
  368. x->f->nrpart = cnt-nb;
  369. }
  370. x->flushtag = x->tag;
  371. alts[CWdata].c = w->conswrite;
  372. alts[CWdata].v = &cwm;
  373. alts[CWdata].op = CHANRCV;
  374. alts[CWflush].c = x->flushc;
  375. alts[CWflush].v = nil;
  376. alts[CWflush].op = CHANRCV;
  377. alts[NCW].op = CHANEND;
  378. switch(alt(alts)){
  379. case CWdata:
  380. break;
  381. case CWflush:
  382. filsyscancel(x);
  383. return;
  384. }
  385. /* received data */
  386. x->flushtag = -1;
  387. if(x->flushing){
  388. recv(x->flushc, nil); /* wake up flushing xfid */
  389. pair.s = runemalloc(1);
  390. pair.ns = 0;
  391. send(cwm.cw, &pair); /* wake up window with empty data */
  392. filsyscancel(x);
  393. return;
  394. }
  395. qlock(&x->active);
  396. pair.s = r;
  397. pair.ns = nr;
  398. send(cwm.cw, &pair);
  399. fc.count = x->count;
  400. filsysrespond(x->fs, x, &fc, nil);
  401. qunlock(&x->active);
  402. return;
  403. case Qconsctl:
  404. if(strncmp(x->data, "holdon", 6)==0){
  405. if(w->holding++ == 0)
  406. wsendctlmesg(w, Holdon, ZR, nil);
  407. break;
  408. }
  409. if(strncmp(x->data, "holdoff", 7)==0 && w->holding){
  410. if(--w->holding == FALSE)
  411. wsendctlmesg(w, Holdoff, ZR, nil);
  412. break;
  413. }
  414. if(strncmp(x->data, "rawon", 5)==0){
  415. if(w->holding){
  416. w->holding = FALSE;
  417. wsendctlmesg(w, Holdoff, ZR, nil);
  418. }
  419. if(w->rawing++ == 0)
  420. wsendctlmesg(w, Rawon, ZR, nil);
  421. break;
  422. }
  423. if(strncmp(x->data, "rawoff", 6)==0 && w->rawing){
  424. if(--w->rawing == 0)
  425. wsendctlmesg(w, Rawoff, ZR, nil);
  426. break;
  427. }
  428. filsysrespond(x->fs, x, &fc, "unknown control message");
  429. return;
  430. case Qcursor:
  431. if(cnt < 2*4+2*2*16)
  432. w->cursorp = nil;
  433. else{
  434. w->cursor.offset.x = BGLONG(x->data+0*4);
  435. w->cursor.offset.y = BGLONG(x->data+1*4);
  436. memmove(w->cursor.clr, x->data+2*4, 2*2*16);
  437. w->cursorp = &w->cursor;
  438. }
  439. wsetcursor(w, !sweeping);
  440. break;
  441. case Qlabel:
  442. if(off != 0){
  443. filsysrespond(x->fs, x, &fc, "non-zero offset writing label");
  444. return;
  445. }
  446. free(w->label);
  447. w->label = emalloc(cnt+1);
  448. memmove(w->label, x->data, cnt);
  449. w->label[cnt] = 0;
  450. break;
  451. case Qmouse:
  452. if(w!=input || Dx(w->screenr)<=0)
  453. break;
  454. if(x->data[0] != 'm'){
  455. filsysrespond(x->fs, x, &fc, Ebadmouse);
  456. return;
  457. }
  458. p = nil;
  459. pt.x = strtoul(x->data+1, &p, 0);
  460. if(p == nil){
  461. filsysrespond(x->fs, x, &fc, Eshort);
  462. return;
  463. }
  464. pt.y = strtoul(p, nil, 0);
  465. if(w==input && wpointto(mouse->xy)==w)
  466. wsendctlmesg(w, Movemouse, Rpt(pt, pt), nil);
  467. break;
  468. case Qsnarf:
  469. /* always append only */
  470. if(ntsnarf > MAXSNARF){ /* avoid thrashing when people cut huge text */
  471. filsysrespond(x->fs, x, &fc, Elong);
  472. return;
  473. }
  474. tsnarf = erealloc(tsnarf, ntsnarf+cnt+1); /* room for NUL */
  475. memmove(tsnarf+ntsnarf, x->data, cnt);
  476. ntsnarf += cnt;
  477. snarfversion++;
  478. break;
  479. case Qwdir:
  480. if(cnt == 0)
  481. break;
  482. if(x->data[cnt-1] == '\n'){
  483. if(cnt == 1)
  484. break;
  485. x->data[cnt-1] = '\0';
  486. }
  487. /* assume data comes in a single write */
  488. /*
  489. * Problem: programs like dossrv, ftp produce illegal UTF;
  490. * we must cope by converting it first.
  491. */
  492. snprint(buf, sizeof buf, "%.*s", cnt, x->data);
  493. if(buf[0] == '/'){
  494. free(w->dir);
  495. w->dir = estrdup(buf);
  496. }else{
  497. p = emalloc(strlen(w->dir) + 1 + strlen(buf) + 1);
  498. sprint(p, "%s/%s", w->dir, buf);
  499. free(w->dir);
  500. w->dir = cleanname(p);
  501. }
  502. break;
  503. case Qkbdin:
  504. keyboardsend(x->data, cnt);
  505. break;
  506. case Qwctl:
  507. if(writewctl(x, buf) < 0){
  508. filsysrespond(x->fs, x, &fc, buf);
  509. return;
  510. }
  511. flushimage(display, 1);
  512. break;
  513. default:
  514. fprint(2, buf, "unknown qid %d in write\n", qid);
  515. sprint(buf, "unknown qid in write");
  516. filsysrespond(x->fs, x, &fc, buf);
  517. return;
  518. }
  519. fc.count = cnt;
  520. filsysrespond(x->fs, x, &fc, nil);
  521. }
  522. int
  523. readwindow(Image *i, char *t, Rectangle r, int offset, int n)
  524. {
  525. int ww, y;
  526. offset -= 5*12;
  527. ww = bytesperline(r, screen->depth);
  528. r.min.y += offset/ww;
  529. if(r.min.y >= r.max.y)
  530. return 0;
  531. y = r.min.y + n/ww;
  532. if(y < r.max.y)
  533. r.max.y = y;
  534. if(r.max.y <= r.min.y)
  535. return 0;
  536. return unloadimage(i, r, (uchar*)t, n);
  537. }
  538. void
  539. xfidread(Xfid *x)
  540. {
  541. Fcall fc;
  542. int n, off, cnt, c;
  543. uint qid;
  544. char buf[128], *t;
  545. char cbuf[30];
  546. Window *w;
  547. Mouse ms;
  548. Rectangle r;
  549. Image *i;
  550. Channel *c1, *c2; /* chan (tuple(char*, int)) */
  551. Consreadmesg crm;
  552. Mousereadmesg mrm;
  553. Consreadmesg cwrm;
  554. Stringpair pair;
  555. enum { CRdata, CRflush, NCR };
  556. enum { MRdata, MRflush, NMR };
  557. enum { WCRdata, WCRflush, NWCR };
  558. Alt alts[NCR+1];
  559. w = x->f->w;
  560. if(w->deleted){
  561. filsysrespond(x->fs, x, &fc, Edeleted);
  562. return;
  563. }
  564. qid = FILE(x->f->qid);
  565. off = x->offset;
  566. cnt = x->count;
  567. switch(qid){
  568. case Qcons:
  569. x->flushtag = x->tag;
  570. alts[CRdata].c = w->consread;
  571. alts[CRdata].v = &crm;
  572. alts[CRdata].op = CHANRCV;
  573. alts[CRflush].c = x->flushc;
  574. alts[CRflush].v = nil;
  575. alts[CRflush].op = CHANRCV;
  576. alts[NMR].op = CHANEND;
  577. switch(alt(alts)){
  578. case CRdata:
  579. break;
  580. case CRflush:
  581. filsyscancel(x);
  582. return;
  583. }
  584. /* received data */
  585. x->flushtag = -1;
  586. c1 = crm.c1;
  587. c2 = crm.c2;
  588. t = malloc(cnt+UTFmax+1); /* room to unpack partial rune plus */
  589. pair.s = t;
  590. pair.ns = cnt;
  591. send(c1, &pair);
  592. if(x->flushing){
  593. recv(x->flushc, nil); /* wake up flushing xfid */
  594. recv(c2, nil); /* wake up window and toss data */
  595. free(t);
  596. filsyscancel(x);
  597. return;
  598. }
  599. qlock(&x->active);
  600. recv(c2, &pair);
  601. fc.data = pair.s;
  602. fc.count = pair.ns;
  603. filsysrespond(x->fs, x, &fc, nil);
  604. free(t);
  605. qunlock(&x->active);
  606. break;
  607. case Qlabel:
  608. n = strlen(w->label);
  609. if(off > n)
  610. off = n;
  611. if(off+cnt > n)
  612. cnt = n-off;
  613. fc.data = w->label+off;
  614. fc.count = cnt;
  615. filsysrespond(x->fs, x, &fc, nil);
  616. break;
  617. case Qmouse:
  618. x->flushtag = x->tag;
  619. alts[MRdata].c = w->mouseread;
  620. alts[MRdata].v = &mrm;
  621. alts[MRdata].op = CHANRCV;
  622. alts[MRflush].c = x->flushc;
  623. alts[MRflush].v = nil;
  624. alts[MRflush].op = CHANRCV;
  625. alts[NMR].op = CHANEND;
  626. switch(alt(alts)){
  627. case MRdata:
  628. break;
  629. case MRflush:
  630. filsyscancel(x);
  631. return;
  632. }
  633. /* received data */
  634. x->flushtag = -1;
  635. if(x->flushing){
  636. recv(x->flushc, nil); /* wake up flushing xfid */
  637. recv(mrm.cm, nil); /* wake up window and toss data */
  638. filsyscancel(x);
  639. return;
  640. }
  641. qlock(&x->active);
  642. recv(mrm.cm, &ms);
  643. c = 'm';
  644. if(w->resized)
  645. c = 'r';
  646. n = sprint(buf, "%c%11d %11d %11d %11ld ", c, ms.xy.x, ms.xy.y, ms.buttons, ms.msec);
  647. w->resized = 0;
  648. fc.data = buf;
  649. fc.count = min(n, cnt);
  650. filsysrespond(x->fs, x, &fc, nil);
  651. qunlock(&x->active);
  652. break;
  653. case Qcursor:
  654. filsysrespond(x->fs, x, &fc, "cursor read not implemented");
  655. break;
  656. /* The algorithm for snarf and text is expensive but easy and rarely used */
  657. case Qsnarf:
  658. getsnarf();
  659. if(nsnarf)
  660. t = runetobyte(snarf, nsnarf, &n);
  661. else {
  662. t = nil;
  663. n = 0;
  664. }
  665. goto Text;
  666. case Qtext:
  667. t = wcontents(w, &n);
  668. goto Text;
  669. Text:
  670. if(off > n){
  671. off = n;
  672. cnt = 0;
  673. }
  674. if(off+cnt > n)
  675. cnt = n-off;
  676. fc.data = t+off;
  677. fc.count = cnt;
  678. filsysrespond(x->fs, x, &fc, nil);
  679. free(t);
  680. break;
  681. case Qwdir:
  682. t = estrdup(w->dir);
  683. n = strlen(t);
  684. goto Text;
  685. case Qwinid:
  686. n = sprint(buf, "%11d ", w->id);
  687. t = estrdup(buf);
  688. goto Text;
  689. case Qwinname:
  690. n = strlen(w->name);
  691. if(n == 0){
  692. filsysrespond(x->fs, x, &fc, "window has no name");
  693. break;
  694. }
  695. t = estrdup(w->name);
  696. goto Text;
  697. case Qwindow:
  698. i = w->i;
  699. if(i == nil || Dx(w->screenr)<=0){
  700. filsysrespond(x->fs, x, &fc, Enowindow);
  701. return;
  702. }
  703. r = w->screenr;
  704. goto caseImage;
  705. case Qscreen:
  706. i = display->image;
  707. if(i == nil){
  708. filsysrespond(x->fs, x, &fc, "no top-level screen");
  709. break;
  710. }
  711. r = i->r;
  712. /* fall through */
  713. caseImage:
  714. if(off < 5*12){
  715. n = sprint(buf, "%11s %11d %11d %11d %11d ",
  716. chantostr(cbuf, screen->chan),
  717. i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y);
  718. t = estrdup(buf);
  719. goto Text;
  720. }
  721. t = malloc(cnt);
  722. fc.data = t;
  723. n = readwindow(i, t, r, off, cnt); /* careful; fc.count is unsigned */
  724. if(n < 0){
  725. buf[0] = 0;
  726. errstr(buf, sizeof buf);
  727. filsysrespond(x->fs, x, &fc, buf);
  728. }else{
  729. fc.count = n;
  730. filsysrespond(x->fs, x, &fc, nil);
  731. }
  732. free(t);
  733. return;
  734. case Qwctl: /* read returns rectangle, hangs if not resized */
  735. if(cnt < 4*12){
  736. filsysrespond(x->fs, x, &fc, Etooshort);
  737. break;
  738. }
  739. x->flushtag = x->tag;
  740. alts[WCRdata].c = w->wctlread;
  741. alts[WCRdata].v = &cwrm;
  742. alts[WCRdata].op = CHANRCV;
  743. alts[WCRflush].c = x->flushc;
  744. alts[WCRflush].v = nil;
  745. alts[WCRflush].op = CHANRCV;
  746. alts[NMR].op = CHANEND;
  747. switch(alt(alts)){
  748. case WCRdata:
  749. break;
  750. case WCRflush:
  751. filsyscancel(x);
  752. return;
  753. }
  754. /* received data */
  755. x->flushtag = -1;
  756. c1 = cwrm.c1;
  757. c2 = cwrm.c2;
  758. t = malloc(cnt+1); /* be sure to have room for NUL */
  759. pair.s = t;
  760. pair.ns = cnt+1;
  761. send(c1, &pair);
  762. if(x->flushing){
  763. recv(x->flushc, nil); /* wake up flushing xfid */
  764. recv(c2, nil); /* wake up window and toss data */
  765. free(t);
  766. filsyscancel(x);
  767. return;
  768. }
  769. qlock(&x->active);
  770. recv(c2, &pair);
  771. fc.data = pair.s;
  772. if(pair.ns > cnt)
  773. pair.ns = cnt;
  774. fc.count = pair.ns;
  775. filsysrespond(x->fs, x, &fc, nil);
  776. free(t);
  777. qunlock(&x->active);
  778. break;
  779. default:
  780. fprint(2, "unknown qid %d in read\n", qid);
  781. sprint(buf, "unknown qid in read");
  782. filsysrespond(x->fs, x, &fc, buf);
  783. break;
  784. }
  785. }