18 const char *termprog = "9term";
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;
46 fprint(2, "usage: 9term [-s] [-f font] [-W winsize] [cmd ...]\n");
47 threadexitsall("usage");
51 threadmain(int argc, char *argv[])
57 _wantfocuschanges = 1;
68 fontname = EARGF(usage());
76 case 'w': /* started from rio or 9wm */
80 winsize = EARGF(usage());
85 putenv("font", fontname);
87 p = getenv("tabstop");
89 p = getenv("TABSTOP");
91 maxtab = strtoul(p, 0, 0);
98 if(initdraw(derror, fontname, "9term") < 0)
99 sysfatal("initdraw: %r");
102 noteenable("sys: child");
104 mousectl = initmouse(nil, screen);
106 error("cannot find mouse");
107 keyboardctl = initkeyboard(nil);
108 if(keyboardctl == nil)
109 error("cannot find keyboard");
110 mouse = &mousectl->m;
112 winclosechan = chancreate(sizeof(Window*), 0);
113 deletechan = chancreate(sizeof(char*), 0);
117 rcpid = rcstart(argc, argv, &rcfd, &sfd);
118 w = new(screen, FALSE, scrolling, rcpid, ".", nil, nil);
120 threadcreate(keyboardthread, nil, STACK);
121 threadcreate(mousethread, nil, STACK);
122 threadcreate(resizethread, nil, STACK);
124 proccreate(rcoutputproc, nil, STACK);
125 proccreate(rcinputproc, nil, STACK);
129 derror(Display *d, char *errorstr)
136 hangupnote(void *a, char *msg)
138 if(getpid() != mainpid)
140 if(strcmp(msg, "hangup") == 0){
141 postnote(PNPROC, rcpid, "hangup");
144 if(strstr(msg, "child")){
148 n = awaitnohang(buf, sizeof buf-1);
151 if(atoi(buf) == rcpid)
160 keyboardthread(void *v)
162 Rune buf[2][20], *rp;
166 threadsetname("keyboardthread");
171 recv(keyboardctl->c, rp);
172 for(i=1; i<nelem(buf[0])-1; i++)
173 if(nbrecv(keyboardctl->c, rp+i) <= 0)
181 resizethread(void *v)
188 p = stringsize(display->defaultfont, "0");
190 updatewinsize(Dy(screen->r)/p.y, (Dx(screen->r)-Scrollwid-2)/p.x,
191 Dx(screen->r), Dy(screen->r));
192 wresize(w, screen, 0);
193 flushimage(display, 1);
194 if(recv(mousectl->resizec, nil) != 1)
196 if(getwindow(display, Refnone) < 0)
197 sysfatal("can't reattach to window");
210 threadsetname("mousethread");
211 while(readmouse(mousectl) >= 0){
215 if(mouse->buttons == 0)
223 if((mouse->buttons&(1|8|16)) || ptinrect(mouse->xy, w->scrollr)){
226 }else if(mouse->buttons&2)
234 wborder(Window *w, int type)
245 new(Image *i, int hideit, int scrollit, int pid, char *dir, char *cmd, char **argv)
249 Channel *cm, *ck, *cctl;
253 cm = chancreate(sizeof(Mouse), 0);
254 ck = chancreate(sizeof(Rune*), 0);
255 cctl = chancreate(sizeof(Wctlmesg), 4);
256 if(cm==nil || ck==nil || cctl==nil)
257 error("new: channel alloc failed");
258 mc = emalloc(sizeof(Mousectl));
262 w = wmk(i, mc, ck, cctl, scrollit);
263 free(mc); /* wmk copies *mc */
264 window = erealloc(window, ++nwindow*sizeof(Window*));
265 window[nwindow-1] = w;
267 hidden[nhidden++] = w;
270 threadcreate(winctl, w, STACK);
273 flushimage(display, 1);
277 w->dir = estrdup(dir);
282 * Button 2 menu. Extra entry for always cook
315 Rune newline[] = { '\n' };
318 button2menu(Window *w)
324 menu2str[Scroll] = "noscroll";
326 menu2str[Scroll] = "scroll";
328 menu2str[Cook] = "nocook";
330 menu2str[Cook] = "cook";
332 switch(menuhit(2, mousectl, &menu2, wscreen)){
363 waddraw(w, snarf, nsnarf);
364 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
365 waddraw(w, newline, 1);
367 winsert(w, snarf, nsnarf, w->nr);
368 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
369 winsert(w, newline, 1, w->nr);
371 wsetselect(w, w->nr, w->nr);
376 if(w->scrolling ^= 1)
384 wsendctlmesg(w, Wakeup, ZR, nil);
385 flushimage(display, 1);
391 return !cooked && !isecho(sfd);
398 int label(Rune*, int);
401 rcoutputproc(void *arg)
403 int i, cnt, n, nb, nr;
404 static char data[9000];
412 /* XXX Let typing have a go -- maybe there's a rubout waiting. */
414 n = read(rcfd, data+cnt, sizeof data-cnt);
417 fprint(2, "9term: rc read error: %r\n");
418 threadexitsall("eof on rc output");
420 n = echocancel(data+cnt, n);
425 cvttorunes(data, cnt-UTFmax, r, &nb, &nr, nil);
426 /* approach end of buffer */
427 while(fullrune(data+nb, cnt-nb)){
428 nb += chartorune(&r[nr], data+nb);
433 memmove(data, data+nb, cnt-nb);
440 recv(w->conswrite, &cwm);
448 winterrupt(Window *w)
453 rubout[0] = getintr(sfd);
454 write(rcfd, rubout, 1);
464 * Process in-band messages about window title changes.
465 * The messages are of the form:
469 * where xxx is the new directory. This format was chosen
470 * because it changes the label on xterm windows.
473 label(Rune *sr, int n)
475 Rune *sl, *el, *er, *r;
479 for(r=er-1; r>=sr; r--)
486 for(sl=el-3; sl>=sr; sl--)
487 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
492 dir = smprint("%.*S", (el-1)-(sl+3), sl+3);
494 if(strcmp(dir, "*9term-hold+") == 0) {
497 flushimage(display, 1);
505 /* remove trailing /-sysname if present */
506 p = strrchr(dir, '/');
507 if(p && *(p+1) == '-'){
513 runemove(sl, el, er-el);
519 rcinputproc(void *arg)
521 static char data[9000];
527 recv(w->consread, &crm);
532 pair.ns = sizeof data;
537 echoed(pair.s, pair.ns);
538 if(write(rcfd, pair.s, pair.ns) < 0)
544 * Snarf buffer - rio uses runes internally
551 s = smprint("%.*S", nsnarf, snarf);
569 snarf = runemalloc(n);
570 cvttorunes(s, n, snarf, &nb, &nsnarf, &nulls);
575 * Clumsy hack to make " and "" work.
576 * Then again, what's not a clumsy hack here in Unix land?
583 void listenproc(void*);
584 void textproc(void*);
587 removethesocket(void)
590 if(remove(thesocket) < 0)
591 fprint(2, "remove %s: %r\n", thesocket);
599 snprint(buf, sizeof buf, "unix!/tmp/9term-text.%d", getpid());
601 if((afd = announce(buf, adir)) < 0){
602 putenv("text9term", "");
606 putenv("text9term", buf);
607 proccreate(listenproc, nil, STACK);
608 strcpy(thesocket, buf+5);
609 atexit(removethesocket);
613 listenproc(void *arg)
618 threadsetname("listen %s", thesocket);
621 fd = listen(adir, dir);
626 proccreate(textproc, (void*)(uintptr)fd, STACK);
633 int fd, i, x, n, end;
635 char buf[4096], *p, *ep;
637 threadsetname("textproc");
645 end = w->org+w->nr; /* avoid possible output loop */
647 if(i >= end || ep-p < UTFmax){
648 for(x=0; x<p-buf; x+=n)
649 if((n = write(fd, buf+x, (p-x)-buf)) <= 0)
662 p += runetochar(p, &r);