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;
66 fontname = EARGF(usage());
71 case 'w': /* started from rio or 9wm */
75 winsize = EARGF(usage());
80 putenv("font", fontname);
82 p = getenv("tabstop");
84 p = getenv("TABSTOP");
86 maxtab = strtoul(p, 0, 0);
93 initdraw(derror, fontname, "9term");
95 noteenable("sys: child");
97 mousectl = initmouse(nil, screen);
99 error("cannot find mouse");
100 keyboardctl = initkeyboard(nil);
101 if(keyboardctl == nil)
102 error("cannot find keyboard");
103 mouse = &mousectl->m;
105 winclosechan = chancreate(sizeof(Window*), 0);
106 deletechan = chancreate(sizeof(char*), 0);
110 rcpid = rcstart(argc, argv, &rcfd, &sfd);
111 w = new(screen, FALSE, scrolling, rcpid, ".", nil, nil);
113 threadcreate(keyboardthread, nil, STACK);
114 threadcreate(mousethread, nil, STACK);
115 threadcreate(resizethread, nil, STACK);
117 proccreate(rcoutputproc, nil, STACK);
118 proccreate(rcinputproc, nil, STACK);
122 derror(Display *d, char *errorstr)
129 hangupnote(void *a, char *msg)
131 if(getpid() != mainpid)
133 if(strcmp(msg, "hangup") == 0)
135 if(strstr(msg, "child")){
139 n = awaitnohang(buf, sizeof buf-1);
142 if(atoi(buf) == rcpid)
151 keyboardthread(void *v)
153 Rune buf[2][20], *rp;
157 threadsetname("keyboardthread");
162 recv(keyboardctl->c, rp);
163 for(i=1; i<nelem(buf[0])-1; i++)
164 if(nbrecv(keyboardctl->c, rp+i) <= 0)
172 resizethread(void *v)
179 p = stringsize(display->defaultfont, "0");
181 updatewinsize(Dy(screen->r)/p.y, (Dx(screen->r)-Scrollwid-2)/p.x,
182 Dx(screen->r), Dy(screen->r));
183 wresize(w, screen, 0);
184 flushimage(display, 1);
185 if(recv(mousectl->resizec, nil) != 1)
187 if(getwindow(display, Refnone) < 0)
188 sysfatal("can't reattach to window");
201 threadsetname("mousethread");
202 while(readmouse(mousectl) >= 0){
206 if(mouse->buttons == 0)
214 if((mouse->buttons&1) || ptinrect(mouse->xy, w->scrollr)){
217 }else if(mouse->buttons&2)
225 wborder(Window *w, int type)
236 new(Image *i, int hideit, int scrollit, int pid, char *dir, char *cmd, char **argv)
240 Channel *cm, *ck, *cctl;
244 cm = chancreate(sizeof(Mouse), 0);
245 ck = chancreate(sizeof(Rune*), 0);
246 cctl = chancreate(sizeof(Wctlmesg), 4);
247 if(cm==nil || ck==nil || cctl==nil)
248 error("new: channel alloc failed");
249 mc = emalloc(sizeof(Mousectl));
253 w = wmk(i, mc, ck, cctl, scrollit);
254 free(mc); /* wmk copies *mc */
255 window = erealloc(window, ++nwindow*sizeof(Window*));
256 window[nwindow-1] = w;
258 hidden[nhidden++] = w;
261 threadcreate(winctl, w, STACK);
264 flushimage(display, 1);
268 w->dir = estrdup(dir);
273 * Button 2 menu. Extra entry for always cook
305 Rune newline[] = { '\n' };
308 button2menu(Window *w)
314 menu2str[Scroll] = "noscroll";
316 menu2str[Scroll] = "scroll";
318 menu2str[Cook] = "nocook";
320 menu2str[Cook] = "cook";
322 switch(menuhit(2, mousectl, &menu2, wscreen)){
349 waddraw(w, snarf, nsnarf);
350 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
351 waddraw(w, newline, 1);
353 winsert(w, snarf, nsnarf, w->nr);
354 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
355 winsert(w, newline, 1, w->nr);
357 wsetselect(w, w->nr, w->nr);
362 if(w->scrolling ^= 1)
371 wsendctlmesg(w, Wakeup, ZR, nil);
372 flushimage(display, 1);
378 return !cooked && !isecho(sfd);
385 int label(Rune*, int);
388 rcoutputproc(void *arg)
390 int i, cnt, n, nb, nr;
391 static char data[9000];
399 /* XXX Let typing have a go -- maybe there's a rubout waiting. */
401 n = read(rcfd, data+cnt, sizeof data-cnt);
404 fprint(2, "9term: rc read error: %r\n");
405 threadexitsall("eof on rc output");
409 cvttorunes(data, cnt-UTFmax, r, &nb, &nr, nil);
410 /* approach end of buffer */
411 while(fullrune(data+nb, cnt-nb)){
412 nb += chartorune(&r[nr], data+nb);
417 memmove(data, data+nb, cnt-nb);
424 recv(w->conswrite, &cwm);
432 winterrupt(Window *w)
437 rubout[0] = getintr(sfd);
438 write(rcfd, rubout, 1);
442 * Process in-band messages about window title changes.
443 * The messages are of the form:
447 * where xxx is the new directory. This format was chosen
448 * because it changes the label on xterm windows.
451 label(Rune *sr, int n)
453 Rune *sl, *el, *er, *r;
457 for(r=er-1; r>=sr; r--)
464 for(sl=el-3; sl>=sr; sl--)
465 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
470 dir = smprint("%.*S", (el-1)-(sl+3), sl+3);
477 /* remove trailing /-sysname if present */
478 p = strrchr(dir, '/');
479 if(p && *(p+1) == '-'){
485 runemove(sl, el, er-el);
491 rcinputproc(void *arg)
493 static char data[9000];
500 recv(w->consread, &crm);
505 pair.ns = sizeof data;
510 if(write(rcfd, pair.s, pair.ns) < 0)
518 * Snarf buffer - rio uses runes internally
525 s = smprint("%.*S", nsnarf, snarf);
543 snarf = runemalloc(n);
544 cvttorunes(s, n, snarf, &nb, &nsnarf, &nulls);
549 * Clumsy hack to make " and "" work.
550 * Then again, what's not a clumsy hack here in Unix land?
557 void listenproc(void*);
558 void textproc(void*);
561 removethesocket(void)
564 if(remove(thesocket) < 0)
565 fprint(2, "remove %s: %r\n", thesocket);
573 snprint(buf, sizeof buf, "unix!/tmp/9term-text.%d", getpid());
575 if((afd = announce(buf, adir)) < 0){
576 putenv("text9term", "");
580 putenv("text9term", buf);
581 proccreate(listenproc, nil, STACK);
582 strcpy(thesocket, buf+5);
583 atexit(removethesocket);
587 listenproc(void *arg)
592 threadsetname("listen %s", thesocket);
595 fd = listen(adir, dir);
600 proccreate(textproc, (void*)(uintptr)fd, STACK);
607 int fd, i, x, n, end;
609 char buf[4096], *p, *ep;
611 threadsetname("textproc");
619 end = w->org+w->nr; /* avoid possible output loop */
621 if(i >= end || ep-p < UTFmax){
622 for(x=0; x<p-buf; x+=n)
623 if((n = write(fd, buf+x, (p-x)-buf)) <= 0)
636 p += runetochar(p, &r);