13 /* for generating syms in mkfile only: */
17 void mousethread(void*);
18 void keyboardthread(void*);
19 void waitthread(void*);
20 void xfidallocthread(void*);
21 void newwindowthread(void*);
22 void plumbproc(void*);
30 int swapscrollbuttons = FALSE;
34 NSnarf = 1000 /* less than 1024, I/O buffer size */
36 Rune snarfrune[NSnarf+1];
40 "/lib/font/bit/lucsans/euro.8.font",
41 "/lib/font/bit/lucm/unicode.9.font"
46 void shutdownthread(void*);
47 void acmeerrorinit(void);
48 void readfile(Column*, char*);
49 static int shutdown(void*, char*);
52 derror(Display *d, char *errorstr)
59 threadmain(int argc, char *argv[])
67 rfork(RFENVG|RFNAMEG);
74 {extern int _threaddebuglevel;
75 _threaddebuglevel = ~0;
79 globalautoindent = TRUE;
93 fontnames[0] = ARGF();
94 if(fontnames[0] == nil)
98 fontnames[1] = ARGF();
99 if(fontnames[1] == nil)
113 swapscrollbuttons = TRUE;
122 fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
123 threadexitsall("usage");
126 fontnames[0] = estrdup(fontnames[0]);
127 fontnames[1] = estrdup(fontnames[1]);
130 cputype = getenv("cputype");
131 objtype = getenv("objtype");
132 home = getenv("HOME");
133 p = getenv("tabstop");
135 maxtab = strtoul(p, nil, 0);
141 rowloadfonts(loadfile);
142 putenv("font", fontnames[0]);
143 snarffd = open("/dev/snarf", OREAD|OCEXEC);
146 sprint(buf, "/acme/bin/%s", cputype);
147 bind(buf, "/bin", MBEFORE);
149 bind("/acme/bin", "/bin", MBEFORE);
151 getwd(wdir, sizeof wdir);
154 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
155 fprint(2, "acme: can't open display: %r\n");
156 threadexitsall("geninitdraw");
159 if(initdraw(derror, fontnames[0], "acme") < 0){
160 fprint(2, "acme: can't open display: %r\n");
161 threadexitsall("initdraw");
165 font = d->defaultfont;
169 reffonts[0] = &reffont;
170 incref(&reffont.ref); /* one to hold up 'font' variable */
171 incref(&reffont.ref); /* one to hold up reffonts[0] */
172 fontcache = emalloc(sizeof(Reffont*));
174 fontcache[0] = &reffont;
180 cwait = threadwaitchan();
181 ccommand = chancreate(sizeof(Command**), 0);
182 ckill = chancreate(sizeof(Rune*), 0);
183 cxfidalloc = chancreate(sizeof(Xfid*), 0);
184 cxfidfree = chancreate(sizeof(Xfid*), 0);
185 cnewwindow = chancreate(sizeof(Channel*), 0);
186 cerr = chancreate(sizeof(char*), 0);
187 cedit = chancreate(sizeof(int), 0);
188 cexit = chancreate(sizeof(int), 0);
189 cwarn = chancreate(sizeof(void*), 1);
190 if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){
191 fprint(2, "acme: can't create initial channels: %r\n");
192 threadexitsall("channels");
194 chansetname(ccommand, "ccommand");
195 chansetname(ckill, "ckill");
196 chansetname(cxfidalloc, "cxfidalloc");
197 chansetname(cxfidfree, "cxfidfree");
198 chansetname(cnewwindow, "cnewwindow");
199 chansetname(cerr, "cerr");
200 chansetname(cedit, "cedit");
201 chansetname(cexit, "cexit");
202 chansetname(cwarn, "cwarn");
204 mousectl = initmouse(nil, screen);
206 fprint(2, "acme: can't initialize mouse: %r\n");
207 threadexitsall("mouse");
209 mouse = &mousectl->m;
210 keyboardctl = initkeyboard(nil);
211 if(keyboardctl == nil){
212 fprint(2, "acme: can't initialize keyboard: %r\n");
213 threadexitsall("keyboard");
218 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
220 fprint(2, "acme: can't initialize plumber: %r\n");
222 cplumb = chancreate(sizeof(Plumbmsg*), 0);
223 threadcreate(plumbproc, nil, STACK);
225 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
232 if(!loadfile || !rowload(&row, loadfile, TRUE)){
233 rowinit(&row, screen->clipr);
238 ncol = (argc+(WPERCOL-1))/WPERCOL;
245 for(i=0; i<ncol; i++){
246 c = rowadd(&row, nil, -1);
248 error("initializing columns");
250 c = row.col[row.ncol-1];
254 for(i=0; i<argc; i++){
255 p = utfrrune(argv[i], '/');
256 if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol)
257 readfile(c, argv[i]);
259 readfile(row.col[i/WPERCOL], argv[i]);
262 flushimage(display, 1);
265 threadcreate(keyboardthread, nil, STACK);
266 threadcreate(mousethread, nil, STACK);
267 threadcreate(waitthread, nil, STACK);
268 threadcreate(xfidallocthread, nil, STACK);
269 threadcreate(newwindowthread, nil, STACK);
270 /* threadcreate(shutdownthread, nil, STACK); */
271 threadnotify(shutdown, 1);
278 readfile(Column *c, char *s)
285 w = coladd(c, nil, nil, -1);
287 runesnprint(rb, sizeof rb, "%s/%s", wdir, s);
289 runesnprint(rb, sizeof rb, "%s", s);
291 rs = cleanrname(runestr(rb, nr));
292 winsetname(w, rs.r, rs.nr);
293 textload(&w->body, 0, s, 1);
294 w->body.file->mod = FALSE;
297 winresize(w, w->r, FALSE, TRUE);
298 textscrdraw(&w->body);
299 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
303 "sys: write on closed pipe",
321 shutdown(void *v, char *msg)
327 for(i=0; ignotes[i]; i++)
328 if(strncmp(ignotes[i], msg, strlen(ignotes[i])) == 0)
332 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
336 for(i=0; oknotes[i]; i++)
337 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
339 print("acme: %s\n", msg);
345 shutdownthread(void *v)
352 threadsetname("shutdown");
353 c = threadnotechan();
354 while((msg = recvp(c)) != nil)
366 /* flushimage(display, 1); */
368 for(c=command; c; c=c->next)
369 postnote(PNGROUP, c->pid, "hangup");
376 acmeerrorproc(void *v)
382 threadsetname("acmeerrorproc");
383 buf = emalloc(8192+1);
384 while((n=read(errorfd, buf, 8192)) >= 0){
386 sendp(cerr, estrdup(buf));
396 error("can't create pipe");
398 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
399 fd = create(acmeerrorfile, OWRITE, 0666);
401 remove(acmeerrorfile);
402 fd = create(acmeerrorfile, OWRITE, 0666);
404 error("can't create acmeerror file");
406 sprint(buf, "%d", pfd[0]);
407 write(fd, buf, strlen(buf));
409 /* reopen pfd[1] close on exec */
410 sprint(buf, "/fd/%d", pfd[1]);
411 errorfd = open(buf, OREAD|OCEXEC);
413 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
414 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
418 error("can't re-open acmeerror file");
419 proccreate(acmeerrorproc, nil, STACK);
429 threadsetname("plumbproc");
431 m = threadplumbrecv(plumbeditfd);
440 keyboardthread(void *v)
445 enum { KTimer, KKey, NKALT };
446 static Alt alts[NKALT+1];
449 alts[KTimer].c = nil;
450 alts[KTimer].v = nil;
451 alts[KTimer].op = CHANNOP;
452 alts[KKey].c = keyboardctl->c;
454 alts[KKey].op = CHANRCV;
455 alts[NKALT].op = CHANEND;
459 threadsetname("keyboardthread");
465 if(t!=nil && t->what==Tag){
469 flushimage(display, 1);
471 alts[KTimer].c = nil;
472 alts[KTimer].op = CHANNOP;
476 typetext = rowtype(&row, r, mouse->xy);
478 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
480 if(t!=nil && t->w!=nil)
481 t->w->body.file->curtext = &t->w->body;
484 if(t!=nil && t->what==Tag) {
485 timer = timerstart(500);
486 alts[KTimer].c = timer->c;
487 alts[KTimer].op = CHANRCV;
490 alts[KTimer].c = nil;
491 alts[KTimer].op = CHANNOP;
493 if(nbrecv(keyboardctl->c, &r) > 0)
495 flushimage(display, 1);
511 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
512 static Alt alts[NMALT+1];
515 threadsetname("mousethread");
516 alts[MResize].c = mousectl->resizec;
517 alts[MResize].v = nil;
518 alts[MResize].op = CHANRCV;
519 alts[MMouse].c = mousectl->c;
520 alts[MMouse].v = &mousectl->m;
521 alts[MMouse].op = CHANRCV;
522 alts[MPlumb].c = cplumb;
523 alts[MPlumb].v = ±
524 alts[MPlumb].op = CHANRCV;
525 alts[MWarnings].c = cwarn;
526 alts[MWarnings].v = nil;
527 alts[MWarnings].op = CHANRCV;
529 alts[MPlumb].op = CHANNOP;
530 alts[NMALT].op = CHANEND;
536 flushimage(display, 1);
539 if(getwindow(display, Refnone) < 0)
540 error("attach to window");
541 draw(screen, screen->r, display->white, nil, ZP);
543 rowresize(&row, screen->clipr);
546 if(strcmp(pm->type, "text") == 0){
547 act = plumblookup(pm->attr, "action");
548 if(act==nil || strcmp(act, "showfile")==0)
550 else if(strcmp(act, "showdata")==0)
559 * Make a copy so decisions are consistent; mousectl changes
560 * underfoot. Can't just receive into m because this introduces
561 * another race; see /sys/src/libdraw/mouse.c.
565 t = rowwhich(&row, m.xy);
566 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
567 winlock(mousetext->w, 'M');
569 wincommit(mousetext->w, mousetext);
570 winunlock(mousetext->w);
576 if(t==nil || m.buttons==0)
581 else if(m.buttons == 2)
583 else if(m.buttons == 4)
586 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
588 if(swapscrollbuttons){
601 /* scroll buttons, wheels, etc. */
602 if(w != nil && (m.buttons & (8|16))){
606 but = Kscrollonedown;
613 if(ptinrect(m.xy, t->scrollr)){
615 if(t->what == Columntag)
616 rowdragcol(&row, t->col, but);
617 else if(t->what == Tag){
618 coldragwin(t->col, t->w, but);
620 barttext = &t->w->body;
642 activecol = t->col; /* button 1 only */
643 if(t->w!=nil && t==&t->w->body)
645 }else if(m.buttons & 2){
646 if(textselect2(t, &q0, &q1, &argt))
647 execute(t, q0, q1, FALSE, argt);
648 }else if(m.buttons & 4){
649 if(textselect3(t, &q0, &q1))
650 look3(t, q0, q1, FALSE);
664 * There is a race between process exiting and our finding out it was ever created.
665 * This structure keeps a list of processes that have exited we haven't heard of.
667 typedef struct Pid Pid;
685 Pid *pids, *p, *lastp;
686 enum { WErr, WKill, WWait, WCmd, NWALT };
690 threadsetname("waitthread");
694 alts[WErr].op = CHANRCV;
695 alts[WKill].c = ckill;
696 alts[WKill].v = &cmd;
697 alts[WKill].op = CHANRCV;
698 alts[WWait].c = cwait;
700 alts[WWait].op = CHANRCV;
701 alts[WCmd].c = ccommand;
703 alts[WCmd].op = CHANRCV;
704 alts[NWALT].op = CHANEND;
711 warning(nil, "%s", err);
713 flushimage(display, 1);
718 ncmd = runestrlen(cmd);
719 for(c=command; c; c=c->next){
721 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
722 if(postnote(PNGROUP, c->pid, "kill") < 0)
723 warning(nil, "kill %S: %r\n", cmd);
728 warning(nil, "Kill: no process %S\n", cmd);
734 for(c=command; c; c=c->next){
748 /* helper processes use this exit status */
749 if(strncmp(w->msg, "libthread", 9) != 0){
750 p = emalloc(sizeof(Pid));
752 strncpy(p->msg, w->msg, sizeof(p->msg));
757 if(search(t, c->name, c->nname)){
758 textdelete(t, t->q0, t->q1, TRUE);
759 textsetselect(t, 0, 0);
762 warning(c->md, "%.*S: exit %s\n", c->nname-1, c->name, w->msg);
763 flushimage(display, 1);
778 /* has this command already exited? */
780 for(p=pids; p!=nil; p=p->next){
781 if(p->pid == c->pid){
783 warning(c->md, "%s\n", p->msg);
787 lastp->next = p->next;
798 textinsert(t, 0, c->name, c->nname, TRUE);
799 textsetselect(t, 0, 0);
800 flushimage(display, 1);
808 xfidallocthread(void *v)
811 enum { Alloc, Free, N };
812 static Alt alts[N+1];
815 threadsetname("xfidallocthread");
816 alts[Alloc].c = cxfidalloc;
818 alts[Alloc].op = CHANRCV;
819 alts[Free].c = cxfidfree;
821 alts[Free].op = CHANRCV;
822 alts[N].op = CHANEND;
832 x = emalloc(sizeof(Xfid));
833 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
834 chansetname(x->c, "xc%p", x->c);
836 threadcreate(xfidctl, x->arg, STACK);
838 sendp(cxfidalloc, x);
848 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
850 newwindowthread(void *v)
855 threadsetname("newwindowthread");
858 /* only fsysproc is talking to us, so synchronization is trivial */
860 w = makenewwindow(nil);
862 sendp(cnewwindow, w);
867 rfget(int fix, int save, int setfont, char *name)
875 name = fontnames[fix];
879 for(i=0; i<nfontcache; i++)
880 if(strcmp(name, fontcache[i]->f->name) == 0){
884 f = openfont(display, name);
886 warning(nil, "can't open font file %s: %r\n", name);
889 r = emalloc(sizeof(Reffont));
891 fontcache = erealloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
892 fontcache[nfontcache++] = r;
898 rfclose(reffonts[fix]);
900 if(name != fontnames[fix]){
901 free(fontnames[fix]);
902 fontnames[fix] = estrdup(name);
908 rfclose(reffonts[0]);
923 if(decref(&r->ref) == 0){
924 for(i=0; i<nfontcache; i++)
925 if(r == fontcache[i])
928 warning(nil, "internal error: can't find font in cache\n");
931 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
940 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
941 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
942 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
943 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
944 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
945 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
946 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
947 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
957 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
958 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
959 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
960 tagcols[TEXT] = display->black;
961 tagcols[HTEXT] = display->black;
964 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
965 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
966 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
967 textcols[TEXT] = display->black;
968 textcols[HTEXT] = display->black;
972 freeimage(modbutton);
973 freeimage(colbutton);
976 r = Rect(0, 0, Scrollwid+2, font->height+1);
977 button = allocimage(display, r, screen->chan, 0, DNofill);
978 draw(button, r, tagcols[BACK], nil, r.min);
980 border(button, r, 2, tagcols[BORD], ZP);
983 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
984 draw(modbutton, r, tagcols[BACK], nil, r.min);
986 border(modbutton, r, 2, tagcols[BORD], ZP);
988 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
989 draw(modbutton, r, tmp, nil, ZP);
993 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
995 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
996 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
1000 * /dev/snarf updates when the file is closed, so we must open our own
1001 * fd here rather than use snarffd
1004 /* rio truncates larges snarf buffers, so this avoids using the
1005 * service if the string is huge */
1007 #define MAXSNARF 100*1024
1018 if(snarfbuf.nc > MAXSNARF)
1022 for(i=0; i<snarfbuf.nc; i+=n){
1026 bufread(&snarfbuf, i, snarfrune, n);
1027 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
1030 s = fmtstrflush(&f);
1040 int nb, nr, nulls, len;
1044 if(s == nil || s[0]==0){
1050 r = runemalloc(len+1);
1051 cvttorunes(s, len, r, &nb, &nr, &nulls);
1052 bufreset(&snarfbuf);
1053 bufinsert(&snarfbuf, 0, r, nr);
1066 /* This is not foolproof, but it will stop a lot of them. */
1068 return strncmp(file, mtpt, n) == 0 && ((n > 0 && mtpt[n-1] == '/') || file[n] == '/' || file[n] == 0);