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;
44 fprint(2, "usage: 9term [-s] [-f font] [-W winsize] [cmd ...]\n");
45 threadexitsall("usage");
49 threadmain(int argc, char *argv[])
55 _wantfocuschanges = 1;
63 fontname = EARGF(usage());
68 case 'w': /* started from rio or 9wm */
72 winsize = EARGF(usage());
77 putenv("font", fontname);
79 p = getenv("tabstop");
81 p = getenv("TABSTOP");
83 maxtab = strtoul(p, 0, 0);
90 initdraw(derror, fontname, "9term");
92 noteenable("sys: child");
94 mousectl = initmouse(nil, screen);
96 error("cannot find mouse");
97 keyboardctl = initkeyboard(nil);
98 if(keyboardctl == nil)
99 error("cannot find keyboard");
100 mouse = &mousectl->m;
102 winclosechan = chancreate(sizeof(Window*), 0);
103 deletechan = chancreate(sizeof(char*), 0);
107 rcpid = rcstart(argc, argv, &rcfd, &sfd);
108 w = new(screen, FALSE, scrolling, rcpid, ".", nil, nil);
110 threadcreate(keyboardthread, nil, STACK);
111 threadcreate(mousethread, nil, STACK);
112 threadcreate(resizethread, nil, STACK);
114 proccreate(rcoutputproc, nil, STACK);
115 proccreate(rcinputproc, nil, STACK);
119 derror(Display *d, char *errorstr)
126 hangupnote(void *a, char *msg)
128 if(getpid() != mainpid)
130 if(strcmp(msg, "hangup") == 0 && rcpid != 0){
131 postnote(PNGROUP, rcpid, "hangup");
134 if(strstr(msg, "child")){
138 n = awaitnohang(buf, sizeof buf-1);
141 if(atoi(buf) == rcpid)
150 keyboardthread(void *v)
152 Rune buf[2][20], *rp;
156 threadsetname("keyboardthread");
161 recv(keyboardctl->c, rp);
162 for(i=1; i<nelem(buf[0])-1; i++)
163 if(nbrecv(keyboardctl->c, rp+i) <= 0)
171 resizethread(void *v)
177 while(recv(mousectl->resizec, nil) == 1){
178 if(getwindow(display, Refnone) < 0)
179 sysfatal("can't reattach to window");
180 p = stringsize(display->defaultfont, "0");
182 updatewinsize(Dy(screen->r)/p.y, (Dx(screen->r)-Scrollwid-2)/p.x,
183 Dx(screen->r), Dy(screen->r));
184 wresize(w, screen, 0);
185 flushimage(display, 1);
198 threadsetname("mousethread");
199 while(readmouse(mousectl) >= 0){
203 if(mouse->buttons == 0)
211 if((mouse->buttons&1) || ptinrect(mouse->xy, w->scrollr)){
214 }else if(mouse->buttons&2)
222 wborder(Window *w, int type)
233 new(Image *i, int hideit, int scrollit, int pid, char *dir, char *cmd, char **argv)
237 Channel *cm, *ck, *cctl;
241 cm = chancreate(sizeof(Mouse), 0);
242 ck = chancreate(sizeof(Rune*), 0);
243 cctl = chancreate(sizeof(Wctlmesg), 4);
244 if(cm==nil || ck==nil || cctl==nil)
245 error("new: channel alloc failed");
246 mc = emalloc(sizeof(Mousectl));
250 w = wmk(i, mc, ck, cctl, scrollit);
251 free(mc); /* wmk copies *mc */
252 window = erealloc(window, ++nwindow*sizeof(Window*));
253 window[nwindow-1] = w;
255 hidden[nhidden++] = w;
258 threadcreate(winctl, w, 8192);
261 flushimage(display, 1);
265 w->dir = estrdup(dir);
270 * Button 2 menu. Extra entry for always cook
302 Rune newline[] = { '\n' };
305 button2menu(Window *w)
311 menu2str[Scroll] = "noscroll";
313 menu2str[Scroll] = "scroll";
315 menu2str[Cook] = "nocook";
317 menu2str[Cook] = "cook";
319 switch(menuhit(2, mousectl, &menu2, wscreen)){
346 waddraw(w, snarf, nsnarf);
347 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
348 waddraw(w, newline, 1);
350 winsert(w, snarf, nsnarf, w->nr);
351 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
352 winsert(w, newline, 1, w->nr);
354 wsetselect(w, w->nr, w->nr);
359 if(w->scrolling ^= 1)
368 wsendctlmesg(w, Wakeup, ZR, nil);
369 flushimage(display, 1);
375 return !cooked && !isecho(sfd);
382 int label(Rune*, int);
385 rcoutputproc(void *arg)
387 int i, cnt, n, nb, nr;
388 static char data[9000];
396 /* XXX Let typing have a go -- maybe there's a rubout waiting. */
398 n = read(rcfd, data+cnt, sizeof data-cnt);
401 fprint(2, "9term: rc read error: %r\n");
402 threadexitsall("eof on rc output");
406 cvttorunes(data, cnt-UTFmax, r, &nb, &nr, nil);
407 /* approach end of buffer */
408 while(fullrune(data+nb, cnt-nb)){
409 nb += chartorune(&r[nr], data+nb);
414 memmove(data, data+nb, cnt-nb);
421 recv(w->conswrite, &cwm);
429 winterrupt(Window *w)
434 rubout[0] = getintr(sfd);
435 write(rcfd, rubout, 1);
439 * Process in-band messages about window title changes.
440 * The messages are of the form:
444 * where xxx is the new directory. This format was chosen
445 * because it changes the label on xterm windows.
448 label(Rune *sr, int n)
450 Rune *sl, *el, *er, *r;
454 for(r=er-1; r>=sr; r--)
461 for(sl=el-3; sl>=sr; sl--)
462 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
467 dir = smprint("%.*S", (el-1)-(sl+3), sl+3);
474 /* remove trailing /-sysname if present */
475 p = strrchr(dir, '/');
476 if(p && *(p+1) == '-'){
482 runemove(sl, el, er-el);
488 rcinputproc(void *arg)
490 static char data[9000];
497 recv(w->consread, &crm);
502 pair.ns = sizeof data;
507 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*)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);