29 void derror(Display*, char*);
30 void mousethread(void*);
31 void keyboardthread(void*);
32 void winclosethread(void*);
33 void deletethread(void*);
34 void rcoutputproc(void*);
35 void rcinputproc(void*);
36 void hangupnote(void*, char*);
37 void resizethread(void*);
38 void servedevtext(void);
40 int errorshouldabort = 0;
45 fprint(2, "usage: 9term [-s] [-f font] [-W winsize] [cmd ...]\n");
46 threadexitsall("usage");
50 threadmain(int argc, char *argv[])
56 _wantfocuschanges = 1;
67 fontname = EARGF(usage());
72 case 'w': /* started from rio or 9wm */
76 winsize = EARGF(usage());
81 putenv("font", fontname);
83 p = getenv("tabstop");
85 p = getenv("TABSTOP");
87 maxtab = strtoul(p, 0, 0);
94 if(initdraw(derror, fontname, "9term") < 0)
95 sysfatal("initdraw: %r");
98 noteenable("sys: child");
100 mousectl = initmouse(nil, screen);
102 error("cannot find mouse");
103 keyboardctl = initkeyboard(nil);
104 if(keyboardctl == nil)
105 error("cannot find keyboard");
106 mouse = &mousectl->m;
108 winclosechan = chancreate(sizeof(Window*), 0);
109 deletechan = chancreate(sizeof(char*), 0);
113 rcpid = rcstart(argc, argv, &rcfd, &sfd);
114 w = new(screen, FALSE, scrolling, rcpid, ".", nil, nil);
116 threadcreate(keyboardthread, nil, STACK);
117 threadcreate(mousethread, nil, STACK);
118 threadcreate(resizethread, nil, STACK);
120 proccreate(rcoutputproc, nil, STACK);
121 proccreate(rcinputproc, nil, STACK);
125 derror(Display *d, char *errorstr)
132 hangupnote(void *a, char *msg)
134 if(getpid() != mainpid)
136 if(strcmp(msg, "hangup") == 0){
137 postnote(PNPROC, rcpid, "hangup");
140 if(strstr(msg, "child")){
144 n = awaitnohang(buf, sizeof buf-1);
147 if(atoi(buf) == rcpid)
156 keyboardthread(void *v)
158 Rune buf[2][20], *rp;
162 threadsetname("keyboardthread");
167 recv(keyboardctl->c, rp);
168 for(i=1; i<nelem(buf[0])-1; i++)
169 if(nbrecv(keyboardctl->c, rp+i) <= 0)
177 resizethread(void *v)
184 p = stringsize(display->defaultfont, "0");
186 updatewinsize(Dy(screen->r)/p.y, (Dx(screen->r)-Scrollwid-2)/p.x,
187 Dx(screen->r), Dy(screen->r));
188 wresize(w, screen, 0);
189 flushimage(display, 1);
190 if(recv(mousectl->resizec, nil) != 1)
192 if(getwindow(display, Refnone) < 0)
193 sysfatal("can't reattach to window");
206 threadsetname("mousethread");
207 while(readmouse(mousectl) >= 0){
211 if(mouse->buttons == 0)
219 if((mouse->buttons&1) || ptinrect(mouse->xy, w->scrollr)){
222 }else if(mouse->buttons&2)
230 wborder(Window *w, int type)
241 new(Image *i, int hideit, int scrollit, int pid, char *dir, char *cmd, char **argv)
245 Channel *cm, *ck, *cctl;
249 cm = chancreate(sizeof(Mouse), 0);
250 ck = chancreate(sizeof(Rune*), 0);
251 cctl = chancreate(sizeof(Wctlmesg), 4);
252 if(cm==nil || ck==nil || cctl==nil)
253 error("new: channel alloc failed");
254 mc = emalloc(sizeof(Mousectl));
258 w = wmk(i, mc, ck, cctl, scrollit);
259 free(mc); /* wmk copies *mc */
260 window = erealloc(window, ++nwindow*sizeof(Window*));
261 window[nwindow-1] = w;
263 hidden[nhidden++] = w;
266 threadcreate(winctl, w, STACK);
269 flushimage(display, 1);
273 w->dir = estrdup(dir);
278 * Button 2 menu. Extra entry for always cook
310 Rune newline[] = { '\n' };
313 button2menu(Window *w)
319 menu2str[Scroll] = "noscroll";
321 menu2str[Scroll] = "scroll";
323 menu2str[Cook] = "nocook";
325 menu2str[Cook] = "cook";
327 switch(menuhit(2, mousectl, &menu2, wscreen)){
354 waddraw(w, snarf, nsnarf);
355 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
356 waddraw(w, newline, 1);
358 winsert(w, snarf, nsnarf, w->nr);
359 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
360 winsert(w, newline, 1, w->nr);
362 wsetselect(w, w->nr, w->nr);
367 if(w->scrolling ^= 1)
376 wsendctlmesg(w, Wakeup, ZR, nil);
377 flushimage(display, 1);
383 return !cooked && !isecho(sfd);
390 int label(Rune*, int);
393 rcoutputproc(void *arg)
395 int i, cnt, n, nb, nr;
396 static char data[9000];
404 /* XXX Let typing have a go -- maybe there's a rubout waiting. */
406 n = read(rcfd, data+cnt, sizeof data-cnt);
409 fprint(2, "9term: rc read error: %r\n");
410 threadexitsall("eof on rc output");
414 cvttorunes(data, cnt-UTFmax, r, &nb, &nr, nil);
415 /* approach end of buffer */
416 while(fullrune(data+nb, cnt-nb)){
417 nb += chartorune(&r[nr], data+nb);
422 memmove(data, data+nb, cnt-nb);
429 recv(w->conswrite, &cwm);
437 winterrupt(Window *w)
442 rubout[0] = getintr(sfd);
443 write(rcfd, rubout, 1);
447 * Process in-band messages about window title changes.
448 * The messages are of the form:
452 * where xxx is the new directory. This format was chosen
453 * because it changes the label on xterm windows.
456 label(Rune *sr, int n)
458 Rune *sl, *el, *er, *r;
462 for(r=er-1; r>=sr; r--)
469 for(sl=el-3; sl>=sr; sl--)
470 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
475 dir = smprint("%.*S", (el-1)-(sl+3), sl+3);
482 /* remove trailing /-sysname if present */
483 p = strrchr(dir, '/');
484 if(p && *(p+1) == '-'){
490 runemove(sl, el, er-el);
496 rcinputproc(void *arg)
498 static char data[9000];
505 recv(w->consread, &crm);
510 pair.ns = sizeof data;
515 if(write(rcfd, pair.s, pair.ns) < 0)
523 * Snarf buffer - rio uses runes internally
530 s = smprint("%.*S", nsnarf, snarf);
548 snarf = runemalloc(n);
549 cvttorunes(s, n, snarf, &nb, &nsnarf, &nulls);
554 * Clumsy hack to make " and "" work.
555 * Then again, what's not a clumsy hack here in Unix land?
562 void listenproc(void*);
563 void textproc(void*);
566 removethesocket(void)
569 if(remove(thesocket) < 0)
570 fprint(2, "remove %s: %r\n", thesocket);
578 snprint(buf, sizeof buf, "unix!/tmp/9term-text.%d", getpid());
580 if((afd = announce(buf, adir)) < 0){
581 putenv("text9term", "");
585 putenv("text9term", buf);
586 proccreate(listenproc, nil, STACK);
587 strcpy(thesocket, buf+5);
588 atexit(removethesocket);
592 listenproc(void *arg)
597 threadsetname("listen %s", thesocket);
600 fd = listen(adir, dir);
605 proccreate(textproc, (void*)(uintptr)fd, STACK);
612 int fd, i, x, n, end;
614 char buf[4096], *p, *ep;
616 threadsetname("textproc");
624 end = w->org+w->nr; /* avoid possible output loop */
626 if(i >= end || ep-p < UTFmax){
627 for(x=0; x<p-buf; x+=n)
628 if((n = write(fd, buf+x, (p-x)-buf)) <= 0)
641 p += runetochar(p, &r);