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*);
31 int swapscrollbuttons = FALSE;
35 NSnarf = 1000 /* less than 1024, I/O buffer size */
37 Rune snarfrune[NSnarf+1];
41 "/lib/font/bit/lucsans/euro.8.font",
42 "/lib/font/bit/lucm/unicode.9.font"
47 void shutdownthread(void*);
48 void acmeerrorinit(void);
49 void readfile(Column*, char*);
50 static int shutdown(void*, char*);
53 derror(Display *d, char *errorstr)
60 threadmain(int argc, char *argv[])
68 rfork(RFENVG|RFNAMEG);
75 {extern int _threaddebuglevel;
76 _threaddebuglevel = ~0;
80 globalautoindent = TRUE;
94 fontnames[0] = ARGF();
95 if(fontnames[0] == nil)
99 fontnames[1] = ARGF();
100 if(fontnames[1] == nil)
114 swapscrollbuttons = TRUE;
123 fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
124 threadexitsall("usage");
127 fontnames[0] = estrdup(fontnames[0]);
128 fontnames[1] = estrdup(fontnames[1]);
131 fmtinstall('t', timefmt);
133 cputype = getenv("cputype");
134 objtype = getenv("objtype");
135 home = getenv("HOME");
136 p = getenv("tabstop");
138 maxtab = strtoul(p, nil, 0);
144 rowloadfonts(loadfile);
145 putenv("font", fontnames[0]);
146 snarffd = open("/dev/snarf", OREAD|OCEXEC);
149 sprint(buf, "/acme/bin/%s", cputype);
150 bind(buf, "/bin", MBEFORE);
152 bind("/acme/bin", "/bin", MBEFORE);
154 getwd(wdir, sizeof wdir);
157 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
158 fprint(2, "acme: can't open display: %r\n");
159 threadexitsall("geninitdraw");
162 if(initdraw(derror, fontnames[0], "acme") < 0){
163 fprint(2, "acme: can't open display: %r\n");
164 threadexitsall("initdraw");
168 font = d->defaultfont;
172 reffonts[0] = &reffont;
173 incref(&reffont.ref); /* one to hold up 'font' variable */
174 incref(&reffont.ref); /* one to hold up reffonts[0] */
175 fontcache = emalloc(sizeof(Reffont*));
177 fontcache[0] = &reffont;
183 cwait = threadwaitchan();
184 ccommand = chancreate(sizeof(Command**), 0);
185 ckill = chancreate(sizeof(Rune*), 0);
186 cxfidalloc = chancreate(sizeof(Xfid*), 0);
187 cxfidfree = chancreate(sizeof(Xfid*), 0);
188 cnewwindow = chancreate(sizeof(Channel*), 0);
189 cerr = chancreate(sizeof(char*), 0);
190 cedit = chancreate(sizeof(int), 0);
191 cexit = chancreate(sizeof(int), 0);
192 cwarn = chancreate(sizeof(void*), 1);
193 if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){
194 fprint(2, "acme: can't create initial channels: %r\n");
195 threadexitsall("channels");
197 chansetname(ccommand, "ccommand");
198 chansetname(ckill, "ckill");
199 chansetname(cxfidalloc, "cxfidalloc");
200 chansetname(cxfidfree, "cxfidfree");
201 chansetname(cnewwindow, "cnewwindow");
202 chansetname(cerr, "cerr");
203 chansetname(cedit, "cedit");
204 chansetname(cexit, "cexit");
205 chansetname(cwarn, "cwarn");
207 mousectl = initmouse(nil, screen);
209 fprint(2, "acme: can't initialize mouse: %r\n");
210 threadexitsall("mouse");
212 mouse = &mousectl->m;
213 keyboardctl = initkeyboard(nil);
214 if(keyboardctl == nil){
215 fprint(2, "acme: can't initialize keyboard: %r\n");
216 threadexitsall("keyboard");
221 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
223 fprint(2, "acme: can't initialize plumber: %r\n");
225 cplumb = chancreate(sizeof(Plumbmsg*), 0);
226 threadcreate(plumbproc, nil, STACK);
228 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
235 if(!loadfile || !rowload(&row, loadfile, TRUE)){
236 rowinit(&row, screen->clipr);
241 ncol = (argc+(WPERCOL-1))/WPERCOL;
248 for(i=0; i<ncol; i++){
249 c = rowadd(&row, nil, -1);
251 error("initializing columns");
253 c = row.col[row.ncol-1];
257 for(i=0; i<argc; i++){
258 p = utfrrune(argv[i], '/');
259 if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol)
260 readfile(c, argv[i]);
262 readfile(row.col[i/WPERCOL], argv[i]);
265 flushimage(display, 1);
268 threadcreate(keyboardthread, nil, STACK);
269 threadcreate(mousethread, nil, STACK);
270 threadcreate(waitthread, nil, STACK);
271 threadcreate(xfidallocthread, nil, STACK);
272 threadcreate(newwindowthread, nil, STACK);
273 /* threadcreate(shutdownthread, nil, STACK); */
274 threadnotify(shutdown, 1);
281 readfile(Column *c, char *s)
288 w = coladd(c, nil, nil, -1);
290 runesnprint(rb, sizeof rb, "%s/%s", wdir, s);
292 runesnprint(rb, sizeof rb, "%s", s);
294 rs = cleanrname(runestr(rb, nr));
295 winsetname(w, rs.r, rs.nr);
296 textload(&w->body, 0, s, 1);
297 w->body.file->mod = FALSE;
300 winresize(w, w->r, FALSE, TRUE);
301 textscrdraw(&w->body);
302 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
306 "sys: write on closed pipe",
324 shutdown(void *v, char *msg)
330 for(i=0; ignotes[i]; i++)
331 if(strncmp(ignotes[i], msg, strlen(ignotes[i])) == 0)
335 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
339 for(i=0; oknotes[i]; i++)
340 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
342 print("acme: %s\n", msg);
348 shutdownthread(void *v)
355 threadsetname("shutdown");
356 c = threadnotechan();
357 while((msg = recvp(c)) != nil)
369 /* flushimage(display, 1); */
371 for(c=command; c; c=c->next)
372 postnote(PNGROUP, c->pid, "hangup");
379 acmeerrorproc(void *v)
385 threadsetname("acmeerrorproc");
386 buf = emalloc(8192+1);
387 while((n=read(errorfd, buf, 8192)) >= 0){
389 sendp(cerr, estrdup(buf));
399 error("can't create pipe");
401 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
402 fd = create(acmeerrorfile, OWRITE, 0666);
404 remove(acmeerrorfile);
405 fd = create(acmeerrorfile, OWRITE, 0666);
407 error("can't create acmeerror file");
409 sprint(buf, "%d", pfd[0]);
410 write(fd, buf, strlen(buf));
412 /* reopen pfd[1] close on exec */
413 sprint(buf, "/fd/%d", pfd[1]);
414 errorfd = open(buf, OREAD|OCEXEC);
416 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
417 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
421 error("can't re-open acmeerror file");
422 proccreate(acmeerrorproc, nil, STACK);
432 threadsetname("plumbproc");
434 m = threadplumbrecv(plumbeditfd);
443 keyboardthread(void *v)
448 enum { KTimer, KKey, NKALT };
449 static Alt alts[NKALT+1];
452 alts[KTimer].c = nil;
453 alts[KTimer].v = nil;
454 alts[KTimer].op = CHANNOP;
455 alts[KKey].c = keyboardctl->c;
457 alts[KKey].op = CHANRCV;
458 alts[NKALT].op = CHANEND;
462 threadsetname("keyboardthread");
468 if(t!=nil && t->what==Tag){
472 flushimage(display, 1);
474 alts[KTimer].c = nil;
475 alts[KTimer].op = CHANNOP;
479 typetext = rowtype(&row, r, mouse->xy);
481 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
483 if(t!=nil && t->w!=nil)
484 t->w->body.file->curtext = &t->w->body;
487 if(t!=nil && t->what==Tag) {
488 timer = timerstart(500);
489 alts[KTimer].c = timer->c;
490 alts[KTimer].op = CHANRCV;
493 alts[KTimer].c = nil;
494 alts[KTimer].op = CHANNOP;
496 if(nbrecv(keyboardctl->c, &r) > 0)
498 flushimage(display, 1);
514 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
515 static Alt alts[NMALT+1];
518 threadsetname("mousethread");
519 alts[MResize].c = mousectl->resizec;
520 alts[MResize].v = nil;
521 alts[MResize].op = CHANRCV;
522 alts[MMouse].c = mousectl->c;
523 alts[MMouse].v = &mousectl->m;
524 alts[MMouse].op = CHANRCV;
525 alts[MPlumb].c = cplumb;
526 alts[MPlumb].v = ±
527 alts[MPlumb].op = CHANRCV;
528 alts[MWarnings].c = cwarn;
529 alts[MWarnings].v = nil;
530 alts[MWarnings].op = CHANRCV;
532 alts[MPlumb].op = CHANNOP;
533 alts[NMALT].op = CHANEND;
539 flushimage(display, 1);
542 if(getwindow(display, Refnone) < 0)
543 error("attach to window");
544 draw(screen, screen->r, display->white, nil, ZP);
546 rowresize(&row, screen->clipr);
549 if(strcmp(pm->type, "text") == 0){
550 act = plumblookup(pm->attr, "action");
551 if(act==nil || strcmp(act, "showfile")==0)
553 else if(strcmp(act, "showdata")==0)
562 * Make a copy so decisions are consistent; mousectl changes
563 * underfoot. Can't just receive into m because this introduces
564 * another race; see /sys/src/libdraw/mouse.c.
568 t = rowwhich(&row, m.xy);
569 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
570 winlock(mousetext->w, 'M');
572 wincommit(mousetext->w, mousetext);
573 winunlock(mousetext->w);
579 if(t==nil || m.buttons==0)
584 else if(m.buttons == 2)
586 else if(m.buttons == 4)
589 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
591 if(swapscrollbuttons){
604 /* scroll buttons, wheels, etc. */
605 if(w != nil && (m.buttons & (8|16))){
609 but = Kscrollonedown;
616 if(ptinrect(m.xy, t->scrollr)){
618 if(t->what == Columntag)
619 rowdragcol(&row, t->col, but);
620 else if(t->what == Tag){
621 coldragwin(t->col, t->w, but);
623 barttext = &t->w->body;
645 activecol = t->col; /* button 1 only */
646 if(t->w!=nil && t==&t->w->body)
648 }else if(m.buttons & 2){
649 if(textselect2(t, &q0, &q1, &argt))
650 execute(t, q0, q1, FALSE, argt);
651 }else if(m.buttons & 4){
652 if(textselect3(t, &q0, &q1))
653 look3(t, q0, q1, FALSE);
667 * There is a race between process exiting and our finding out it was ever created.
668 * This structure keeps a list of processes that have exited we haven't heard of.
670 typedef struct Pid Pid;
688 Pid *pids, *p, *lastp;
689 enum { WErr, WKill, WWait, WCmd, NWALT };
693 threadsetname("waitthread");
697 alts[WErr].op = CHANRCV;
698 alts[WKill].c = ckill;
699 alts[WKill].v = &cmd;
700 alts[WKill].op = CHANRCV;
701 alts[WWait].c = cwait;
703 alts[WWait].op = CHANRCV;
704 alts[WCmd].c = ccommand;
706 alts[WCmd].op = CHANRCV;
707 alts[NWALT].op = CHANEND;
714 warning(nil, "%s", err);
716 flushimage(display, 1);
721 ncmd = runestrlen(cmd);
722 for(c=command; c; c=c->next){
724 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
725 if(postnote(PNGROUP, c->pid, "kill") < 0)
726 warning(nil, "kill %S: %r\n", cmd);
731 warning(nil, "Kill: no process %S\n", cmd);
737 for(c=command; c; c=c->next){
751 /* helper processes use this exit status */
752 if(strncmp(w->msg, "libthread", 9) != 0){
753 p = emalloc(sizeof(Pid));
755 strncpy(p->msg, w->msg, sizeof(p->msg));
760 if(search(t, c->name, c->nname)){
761 textdelete(t, t->q0, t->q1, TRUE);
762 textsetselect(t, 0, 0);
765 warning(c->md, "%.*S: exit %s\n", c->nname-1, c->name, w->msg);
766 flushimage(display, 1);
781 /* has this command already exited? */
783 for(p=pids; p!=nil; p=p->next){
784 if(p->pid == c->pid){
786 warning(c->md, "%s\n", p->msg);
790 lastp->next = p->next;
801 textinsert(t, 0, c->name, c->nname, TRUE);
802 textsetselect(t, 0, 0);
803 flushimage(display, 1);
811 xfidallocthread(void *v)
814 enum { Alloc, Free, N };
815 static Alt alts[N+1];
818 threadsetname("xfidallocthread");
819 alts[Alloc].c = cxfidalloc;
821 alts[Alloc].op = CHANRCV;
822 alts[Free].c = cxfidfree;
824 alts[Free].op = CHANRCV;
825 alts[N].op = CHANEND;
835 x = emalloc(sizeof(Xfid));
836 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
837 chansetname(x->c, "xc%p", x->c);
839 threadcreate(xfidctl, x->arg, STACK);
841 sendp(cxfidalloc, x);
851 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
853 newwindowthread(void *v)
858 threadsetname("newwindowthread");
861 /* only fsysproc is talking to us, so synchronization is trivial */
863 w = makenewwindow(nil);
865 sendp(cnewwindow, w);
870 rfget(int fix, int save, int setfont, char *name)
878 name = fontnames[fix];
882 for(i=0; i<nfontcache; i++)
883 if(strcmp(name, fontcache[i]->f->name) == 0){
887 f = openfont(display, name);
889 warning(nil, "can't open font file %s: %r\n", name);
892 r = emalloc(sizeof(Reffont));
894 fontcache = erealloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
895 fontcache[nfontcache++] = r;
901 rfclose(reffonts[fix]);
903 if(name != fontnames[fix]){
904 free(fontnames[fix]);
905 fontnames[fix] = estrdup(name);
911 rfclose(reffonts[0]);
926 if(decref(&r->ref) == 0){
927 for(i=0; i<nfontcache; i++)
928 if(r == fontcache[i])
931 warning(nil, "internal error: can't find font in cache\n");
934 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
943 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
944 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
945 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
946 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
947 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
948 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
949 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
950 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
960 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
961 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
962 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
963 tagcols[TEXT] = display->black;
964 tagcols[HTEXT] = display->black;
967 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
968 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
969 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
970 textcols[TEXT] = display->black;
971 textcols[HTEXT] = display->black;
975 freeimage(modbutton);
976 freeimage(colbutton);
979 r = Rect(0, 0, Scrollwid+2, font->height+1);
980 button = allocimage(display, r, screen->chan, 0, DNofill);
981 draw(button, r, tagcols[BACK], nil, r.min);
983 border(button, r, 2, tagcols[BORD], ZP);
986 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
987 draw(modbutton, r, tagcols[BACK], nil, r.min);
989 border(modbutton, r, 2, tagcols[BORD], ZP);
991 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
992 draw(modbutton, r, tmp, nil, ZP);
996 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
998 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
999 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
1003 * /dev/snarf updates when the file is closed, so we must open our own
1004 * fd here rather than use snarffd
1007 /* rio truncates larges snarf buffers, so this avoids using the
1008 * service if the string is huge */
1010 #define MAXSNARF 100*1024
1021 if(snarfbuf.nc > MAXSNARF)
1025 for(i=0; i<snarfbuf.nc; i+=n){
1029 bufread(&snarfbuf, i, snarfrune, n);
1030 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
1033 s = fmtstrflush(&f);
1043 int nb, nr, nulls, len;
1047 if(s == nil || s[0]==0){
1053 r = runemalloc(len+1);
1054 cvttorunes(s, len, r, &nb, &nr, &nulls);
1055 bufreset(&snarfbuf);
1056 bufinsert(&snarfbuf, 0, r, nr);
1069 /* This is not foolproof, but it will stop a lot of them. */
1071 return strncmp(file, mtpt, n) == 0 && ((n > 0 && mtpt[n-1] == '/') || file[n] == '/' || file[n] == 0);
1079 tm = localtime(va_arg(f->args, ulong));
1080 return fmtprint(f, "%04d/%02d/%02d %02d:%02d:%02d",
1081 tm->year+1900, tm->mon+1, tm->mday, tm->hour, tm->min, tm->sec);