28 void derror(Display*, char*);
29 void mousethread(void*);
30 void keyboardthread(void*);
31 void winclosethread(void*);
32 void deletethread(void*);
33 void rcoutputproc(void*);
34 void rcinputproc(void*);
35 void hangupnote(void*, char*);
36 void resizethread(void*);
37 void servedevtext(void);
39 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());
75 case 'w': /* started from rio or 9wm */
79 winsize = EARGF(usage());
84 putenv("font", fontname);
86 p = getenv("tabstop");
88 p = getenv("TABSTOP");
90 maxtab = strtoul(p, 0, 0);
97 if(initdraw(derror, fontname, "9term") < 0)
98 sysfatal("initdraw: %r");
101 noteenable("sys: child");
103 mousectl = initmouse(nil, screen);
105 error("cannot find mouse");
106 keyboardctl = initkeyboard(nil);
107 if(keyboardctl == nil)
108 error("cannot find keyboard");
109 mouse = &mousectl->m;
111 winclosechan = chancreate(sizeof(Window*), 0);
112 deletechan = chancreate(sizeof(char*), 0);
116 rcpid = rcstart(argc, argv, &rcfd, &sfd);
117 w = new(screen, FALSE, rcpid, ".", nil, nil);
119 threadcreate(keyboardthread, nil, STACK);
120 threadcreate(mousethread, nil, STACK);
121 threadcreate(resizethread, nil, STACK);
123 proccreate(rcoutputproc, nil, STACK);
124 proccreate(rcinputproc, nil, STACK);
128 derror(Display *d, char *errorstr)
135 hangupnote(void *a, char *msg)
137 if(getpid() != mainpid)
139 if(strcmp(msg, "hangup") == 0){
140 postnote(PNPROC, rcpid, "hangup");
143 if(strstr(msg, "child")){
147 n = awaitnohang(buf, sizeof buf-1);
150 if(atoi(buf) == rcpid)
159 keyboardthread(void *v)
161 Rune buf[2][20], *rp;
165 threadsetname("keyboardthread");
170 recv(keyboardctl->c, rp);
171 for(i=1; i<nelem(buf[0])-1; i++)
172 if(nbrecv(keyboardctl->c, rp+i) <= 0)
180 resizethread(void *v)
187 p = stringsize(display->defaultfont, "0");
189 updatewinsize(Dy(screen->r)/p.y, (Dx(screen->r)-Scrollwid-2)/p.x,
190 Dx(screen->r), Dy(screen->r));
191 wresize(w, screen, 0);
192 flushimage(display, 1);
193 if(recv(mousectl->resizec, nil) != 1)
195 if(getwindow(display, Refnone) < 0)
196 sysfatal("can't reattach to window");
209 threadsetname("mousethread");
210 while(readmouse(mousectl) >= 0){
214 if(mouse->buttons == 0)
222 if((mouse->buttons&(1|8|16)) || ptinrect(mouse->xy, w->scrollr)){
225 }else if(mouse->buttons&2)
233 wborder(Window *w, int type)
244 new(Image *i, int hideit, int pid, char *dir, char *cmd, char **argv)
248 Channel *cm, *ck, *cctl;
252 cm = chancreate(sizeof(Mouse), 0);
253 ck = chancreate(sizeof(Rune*), 0);
254 cctl = chancreate(sizeof(Wctlmesg), 4);
255 if(cm==nil || ck==nil || cctl==nil)
256 error("new: channel alloc failed");
257 mc = emalloc(sizeof(Mousectl));
261 w = wmk(i, mc, ck, cctl);
262 free(mc); /* wmk copies *mc */
263 window = erealloc(window, ++nwindow*sizeof(Window*));
264 window[nwindow-1] = w;
266 hidden[nhidden++] = w;
269 threadcreate(winctl, w, STACK);
272 flushimage(display, 1);
276 w->dir = estrdup(dir);
281 * Button 2 menu. Extra entry for always cook
310 Rune newline[] = { '\n' };
313 button2menu(Window *w)
319 menu2str[Cook] = "nocook";
321 menu2str[Cook] = "cook";
323 switch(menuhit(2, mousectl, &menu2, wscreen)){
350 waddraw(w, snarf, nsnarf);
351 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
352 waddraw(w, newline, 1);
354 winsert(w, snarf, nsnarf, w->nr);
355 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
356 winsert(w, newline, 1, w->nr);
358 wsetselect(w, w->nr, w->nr);
367 wsendctlmesg(w, Wakeup, ZR, nil);
368 flushimage(display, 1);
374 return !cooked && !isecho(sfd);
381 int label(Rune*, int);
384 rcoutputproc(void *arg)
386 int i, cnt, n, nb, nr;
387 static char data[9000];
395 /* XXX Let typing have a go -- maybe there's a rubout waiting. */
397 n = read(rcfd, data+cnt, sizeof data-cnt);
400 fprint(2, "9term: rc read error: %r\n");
401 threadexitsall("eof on rc output");
403 n = echocancel(data+cnt, n);
408 cvttorunes(data, cnt-UTFmax, r, &nb, &nr, nil);
409 /* approach end of buffer */
410 while(fullrune(data+nb, cnt-nb)){
411 nb += chartorune(&r[nr], data+nb);
416 memmove(data, data+nb, cnt-nb);
423 recv(w->conswrite, &cwm);
431 winterrupt(Window *w)
436 rubout[0] = getintr(sfd);
437 write(rcfd, rubout, 1);
441 * Process in-band messages about window title changes.
442 * The messages are of the form:
446 * where xxx is the new directory. This format was chosen
447 * because it changes the label on xterm windows.
450 label(Rune *sr, int n)
452 Rune *sl, *el, *er, *r;
456 for(r=er-1; r>=sr; r--)
463 for(sl=el-3; sl>=sr; sl--)
464 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
469 dir = smprint("%.*S", (el-1)-(sl+3), sl+3);
476 /* remove trailing /-sysname if present */
477 p = strrchr(dir, '/');
478 if(p && *(p+1) == '-'){
484 runemove(sl, el, er-el);
490 rcinputproc(void *arg)
492 static char data[9000];
498 recv(w->consread, &crm);
503 pair.ns = sizeof data;
508 echoed(pair.s, pair.ns);
509 if(write(rcfd, pair.s, pair.ns) < 0)
515 * Snarf buffer - rio uses runes internally
522 s = smprint("%.*S", nsnarf, snarf);
540 snarf = runemalloc(n);
541 cvttorunes(s, n, snarf, &nb, &nsnarf, &nulls);
546 * Clumsy hack to make " and "" work.
547 * Then again, what's not a clumsy hack here in Unix land?
554 void listenproc(void*);
555 void textproc(void*);
558 removethesocket(void)
561 if(remove(thesocket) < 0)
562 fprint(2, "remove %s: %r\n", thesocket);
570 snprint(buf, sizeof buf, "unix!/tmp/9term-text.%d", getpid());
572 if((afd = announce(buf, adir)) < 0){
573 putenv("text9term", "");
577 putenv("text9term", buf);
578 proccreate(listenproc, nil, STACK);
579 strcpy(thesocket, buf+5);
580 atexit(removethesocket);
584 listenproc(void *arg)
589 threadsetname("listen %s", thesocket);
592 fd = listen(adir, dir);
597 proccreate(textproc, (void*)(uintptr)fd, STACK);
604 int fd, i, x, n, end;
606 char buf[4096], *p, *ep;
608 threadsetname("textproc");
616 end = w->org+w->nr; /* avoid possible output loop */
618 if(i >= end || ep-p < UTFmax){
619 for(x=0; x<p-buf; x+=n)
620 if((n = write(fd, buf+x, (p-x)-buf)) <= 0)
633 p += runetochar(p, &r);