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;
33 NSnarf = 1000 /* less than 1024, I/O buffer size */
35 Rune snarfrune[NSnarf+1];
39 "/lib/font/bit/lucsans/euro.8.font",
40 "/lib/font/bit/lucm/unicode.9.font"
45 void shutdownthread(void*);
46 void acmeerrorinit(void);
47 void readfile(Column*, char*);
48 static int shutdown(void*, char*);
51 derror(Display *d, char *errorstr)
58 threadmain(int argc, char *argv[])
66 rfork(RFENVG|RFNAMEG);
76 {extern int _threaddebuglevel;
77 _threaddebuglevel = ~0;
81 globalautoindent = TRUE;
95 fontnames[0] = ARGF();
96 if(fontnames[0] == nil)
100 fontnames[1] = ARGF();
101 if(fontnames[1] == nil)
110 swapscrollbuttons = TRUE;
119 fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
120 threadexitsall("usage");
123 fontnames[0] = estrdup(fontnames[0]);
124 fontnames[1] = estrdup(fontnames[1]);
127 cputype = getenv("cputype");
128 objtype = getenv("objtype");
129 home = getenv("HOME");
130 p = getenv("tabstop");
132 maxtab = strtoul(p, nil, 0);
138 rowloadfonts(loadfile);
139 putenv("font", fontnames[0]);
140 snarffd = open("/dev/snarf", OREAD|OCEXEC);
143 sprint(buf, "/acme/bin/%s", cputype);
144 bind(buf, "/bin", MBEFORE);
146 bind("/acme/bin", "/bin", MBEFORE);
148 getwd(wdir, sizeof wdir);
151 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
152 fprint(2, "acme: can't open display: %r\n");
153 threadexitsall("geninitdraw");
156 if(initdraw(derror, fontnames[0], "acme") < 0){
157 fprint(2, "acme: can't open display: %r\n");
158 threadexitsall("initdraw");
162 font = d->defaultfont;
166 reffonts[0] = &reffont;
167 incref(&reffont.ref); /* one to hold up 'font' variable */
168 incref(&reffont.ref); /* one to hold up reffonts[0] */
169 fontcache = emalloc(sizeof(Reffont*));
171 fontcache[0] = &reffont;
177 cwait = threadwaitchan();
178 ccommand = chancreate(sizeof(Command**), 0);
179 chansetname(ccommand, "ccommand");
180 ckill = chancreate(sizeof(Rune*), 0);
181 chansetname(ckill, "ckill");
182 cxfidalloc = chancreate(sizeof(Xfid*), 0);
183 chansetname(cxfidalloc, "cxfidalloc");
184 cxfidfree = chancreate(sizeof(Xfid*), 0);
185 chansetname(cxfidfree, "cxfidfree");
186 cnewwindow = chancreate(sizeof(Channel*), 0);
187 chansetname(cnewwindow, "cnewwindow");
188 cerr = chancreate(sizeof(char*), 0);
189 chansetname(cerr, "cerr");
190 cedit = chancreate(sizeof(int), 0);
191 chansetname(cedit, "cedit");
192 cexit = chancreate(sizeof(int), 0);
193 chansetname(cexit, "cexit");
194 cwarn = chancreate(sizeof(void*), 1);
195 chansetname(cwarn, "cwarn");
196 if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){
197 fprint(2, "acme: can't create initial channels: %r\n");
198 threadexitsall("channels");
201 mousectl = initmouse(nil, screen);
203 fprint(2, "acme: can't initialize mouse: %r\n");
204 threadexitsall("mouse");
206 mouse = &mousectl->m;
207 keyboardctl = initkeyboard(nil);
208 if(keyboardctl == nil){
209 fprint(2, "acme: can't initialize keyboard: %r\n");
210 threadexitsall("keyboard");
215 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
217 fprint(2, "acme: can't initialize plumber: %r\n");
219 cplumb = chancreate(sizeof(Plumbmsg*), 0);
220 threadcreate(plumbproc, nil, STACK);
222 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
229 if(!loadfile || !rowload(&row, loadfile, TRUE)){
230 rowinit(&row, screen->clipr);
235 ncol = (argc+(WPERCOL-1))/WPERCOL;
242 for(i=0; i<ncol; i++){
243 c = rowadd(&row, nil, -1);
245 error("initializing columns");
247 c = row.col[row.ncol-1];
251 for(i=0; i<argc; i++){
252 p = utfrrune(argv[i], '/');
253 if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol)
254 readfile(c, argv[i]);
256 readfile(row.col[i/WPERCOL], argv[i]);
259 flushimage(display, 1);
262 threadcreate(keyboardthread, nil, STACK);
263 threadcreate(mousethread, nil, STACK);
264 threadcreate(waitthread, nil, STACK);
265 threadcreate(xfidallocthread, nil, STACK);
266 threadcreate(newwindowthread, nil, STACK);
267 /* threadcreate(shutdownthread, nil, STACK); */
268 threadnotify(shutdown, 1);
275 readfile(Column *c, char *s)
282 w = coladd(c, nil, nil, -1);
284 runesnprint(rb, sizeof rb, "%s/%s", wdir, s);
286 runesnprint(rb, sizeof rb, "%s", s);
288 rs = cleanrname(runestr(rb, nr));
289 winsetname(w, rs.r, rs.nr);
290 textload(&w->body, 0, s, 1);
291 w->body.file->mod = FALSE;
294 winresize(w, w->r, FALSE, TRUE);
295 textscrdraw(&w->body);
296 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
300 "sys: write on closed pipe",
318 shutdown(void *v, char *msg)
324 for(i=0; ignotes[i]; i++)
325 if(strncmp(ignotes[i], msg, strlen(ignotes[i])) == 0)
329 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
333 for(i=0; oknotes[i]; i++)
334 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
336 print("acme: %s\n", msg);
342 shutdownthread(void *v)
349 threadsetname("shutdown");
350 c = threadnotechan();
351 while((msg = recvp(c)) != nil)
363 /* flushimage(display, 1); */
365 for(c=command; c; c=c->next)
366 postnote(PNGROUP, c->pid, "hangup");
373 acmeerrorproc(void *v)
379 threadsetname("acmeerrorproc");
380 buf = emalloc(8192+1);
381 while((n=read(errorfd, buf, 8192)) >= 0){
383 sendp(cerr, estrdup(buf));
393 error("can't create pipe");
395 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
396 fd = create(acmeerrorfile, OWRITE, 0666);
398 remove(acmeerrorfile);
399 fd = create(acmeerrorfile, OWRITE, 0666);
401 error("can't create acmeerror file");
403 sprint(buf, "%d", pfd[0]);
404 write(fd, buf, strlen(buf));
406 /* reopen pfd[1] close on exec */
407 sprint(buf, "/fd/%d", pfd[1]);
408 errorfd = open(buf, OREAD|OCEXEC);
410 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
411 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
415 error("can't re-open acmeerror file");
416 proccreate(acmeerrorproc, nil, STACK);
426 threadsetname("plumbproc");
428 m = threadplumbrecv(plumbeditfd);
437 keyboardthread(void *v)
442 enum { KTimer, KKey, NKALT };
443 static Alt alts[NKALT+1];
446 alts[KTimer].c = nil;
447 alts[KTimer].v = nil;
448 alts[KTimer].op = CHANNOP;
449 alts[KKey].c = keyboardctl->c;
451 alts[KKey].op = CHANRCV;
452 alts[NKALT].op = CHANEND;
456 threadsetname("keyboardthread");
462 if(t!=nil && t->what==Tag){
466 flushimage(display, 1);
468 alts[KTimer].c = nil;
469 alts[KTimer].op = CHANNOP;
473 typetext = rowtype(&row, r, mouse->xy);
475 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
477 if(t!=nil && t->w!=nil)
478 t->w->body.file->curtext = &t->w->body;
481 if(t!=nil && t->what==Tag) {
482 timer = timerstart(500);
483 alts[KTimer].c = timer->c;
484 alts[KTimer].op = CHANRCV;
487 alts[KTimer].c = nil;
488 alts[KTimer].op = CHANNOP;
490 if(nbrecv(keyboardctl->c, &r) > 0)
492 flushimage(display, 1);
508 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
509 static Alt alts[NMALT+1];
512 threadsetname("mousethread");
513 alts[MResize].c = mousectl->resizec;
514 alts[MResize].v = nil;
515 alts[MResize].op = CHANRCV;
516 alts[MMouse].c = mousectl->c;
517 alts[MMouse].v = &mousectl->m;
518 alts[MMouse].op = CHANRCV;
519 alts[MPlumb].c = cplumb;
520 alts[MPlumb].v = ±
521 alts[MPlumb].op = CHANRCV;
522 alts[MWarnings].c = cwarn;
523 alts[MWarnings].v = nil;
524 alts[MWarnings].op = CHANRCV;
526 alts[MPlumb].op = CHANNOP;
527 alts[NMALT].op = CHANEND;
533 flushimage(display, 1);
536 if(getwindow(display, Refnone) < 0)
537 error("attach to window");
538 draw(screen, screen->r, display->white, nil, ZP);
540 rowresize(&row, screen->clipr);
543 if(strcmp(pm->type, "text") == 0){
544 act = plumblookup(pm->attr, "action");
545 if(act==nil || strcmp(act, "showfile")==0)
547 else if(strcmp(act, "showdata")==0)
556 * Make a copy so decisions are consistent; mousectl changes
557 * underfoot. Can't just receive into m because this introduces
558 * another race; see /sys/src/libdraw/mouse.c.
562 t = rowwhich(&row, m.xy);
563 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
564 winlock(mousetext->w, 'M');
566 wincommit(mousetext->w, mousetext);
567 winunlock(mousetext->w);
573 if(t==nil || m.buttons==0)
578 else if(m.buttons == 2)
580 else if(m.buttons == 4)
583 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
585 if(swapscrollbuttons){
598 /* scroll buttons, wheels, etc. */
600 * TAG used to require t->what==Body but now allow
601 * scroll wheel in tag too.
603 if(w != nil && (m.buttons & (8|16))){
607 but = Kscrollonedown;
614 if(ptinrect(m.xy, t->scrollr)){
616 if(t->what == Columntag)
617 rowdragcol(&row, t->col, but);
618 else if(t->what == Tag){
619 coldragwin(t->col, t->w, but);
621 barttext = &t->w->body;
643 activecol = t->col; /* button 1 only */
644 if(t->w!=nil && t==&t->w->body)
646 }else if(m.buttons & 2){
647 if(textselect2(t, &q0, &q1, &argt))
648 execute(t, q0, q1, FALSE, argt);
649 }else if(m.buttons & 4){
650 if(textselect3(t, &q0, &q1))
651 look3(t, q0, q1, FALSE);
665 * There is a race between process exiting and our finding out it was ever created.
666 * This structure keeps a list of processes that have exited we haven't heard of.
668 typedef struct Pid Pid;
686 Pid *pids, *p, *lastp;
687 enum { WErr, WKill, WWait, WCmd, NWALT };
691 threadsetname("waitthread");
695 alts[WErr].op = CHANRCV;
696 alts[WKill].c = ckill;
697 alts[WKill].v = &cmd;
698 alts[WKill].op = CHANRCV;
699 alts[WWait].c = cwait;
701 alts[WWait].op = CHANRCV;
702 alts[WCmd].c = ccommand;
704 alts[WCmd].op = CHANRCV;
705 alts[NWALT].op = CHANEND;
712 warning(nil, "%s", err);
714 flushimage(display, 1);
719 ncmd = runestrlen(cmd);
720 for(c=command; c; c=c->next){
722 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
723 if(postnote(PNGROUP, c->pid, "kill") < 0)
724 warning(nil, "kill %S: %r\n", cmd);
729 warning(nil, "Kill: no process %S\n", cmd);
735 for(c=command; c; c=c->next){
749 /* helper processes use this exit status */
750 if(strncmp(w->msg, "libthread", 9) != 0){
751 p = emalloc(sizeof(Pid));
753 strncpy(p->msg, w->msg, sizeof(p->msg));
758 if(search(t, c->name, c->nname)){
759 textdelete(t, t->q0, t->q1, TRUE);
760 textsetselect(t, 0, 0);
763 warning(c->md, "%.*S: exit %s\n", c->nname-1, c->name, w->msg);
764 flushimage(display, 1);
779 /* has this command already exited? */
781 for(p=pids; p!=nil; p=p->next){
782 if(p->pid == c->pid){
784 warning(c->md, "%s\n", p->msg);
788 lastp->next = p->next;
799 textinsert(t, 0, c->name, c->nname, TRUE);
800 textsetselect(t, 0, 0);
801 flushimage(display, 1);
809 xfidallocthread(void *v)
812 enum { Alloc, Free, N };
813 static Alt alts[N+1];
816 threadsetname("xfidallocthread");
817 alts[Alloc].c = cxfidalloc;
819 alts[Alloc].op = CHANRCV;
820 alts[Free].c = cxfidfree;
822 alts[Free].op = CHANRCV;
823 alts[N].op = CHANEND;
833 x = emalloc(sizeof(Xfid));
834 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
835 chansetname(x->c, "xc%p", x->c);
837 threadcreate(xfidctl, x->arg, STACK);
839 sendp(cxfidalloc, x);
849 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
851 newwindowthread(void *v)
856 threadsetname("newwindowthread");
859 /* only fsysproc is talking to us, so synchronization is trivial */
861 w = makenewwindow(nil);
863 sendp(cnewwindow, w);
868 rfget(int fix, int save, int setfont, char *name)
876 name = fontnames[fix];
880 for(i=0; i<nfontcache; i++)
881 if(strcmp(name, fontcache[i]->f->name) == 0){
885 f = openfont(display, name);
887 fprint(2, "can't open font file %s: %r\n", name);
888 warning(nil, "can't open font file %s: %r\n", name);
891 r = emalloc(sizeof(Reffont));
893 fontcache = erealloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
894 fontcache[nfontcache++] = r;
900 rfclose(reffonts[fix]);
902 if(fontnames[fix] != name){
903 free(fontnames[fix]);
904 fontnames[fix] = estrdup(name);
910 rfclose(reffonts[0]);
925 if(decref(&r->ref) == 0){
926 for(i=0; i<nfontcache; i++)
927 if(r == fontcache[i])
930 warning(nil, "internal error: can't find font in cache\n");
933 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
942 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
943 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
944 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
945 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
946 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
947 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
948 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
949 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
959 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
960 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
961 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
962 tagcols[TEXT] = display->black;
963 tagcols[HTEXT] = display->black;
966 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
967 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
968 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
969 textcols[TEXT] = display->black;
970 textcols[HTEXT] = display->black;
974 freeimage(modbutton);
975 freeimage(colbutton);
978 r = Rect(0, 0, Scrollwid+2, font->height+1);
979 button = allocimage(display, r, screen->chan, 0, DNofill);
980 draw(button, r, tagcols[BACK], nil, r.min);
982 border(button, r, 2, tagcols[BORD], ZP);
985 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
986 draw(modbutton, r, tagcols[BACK], nil, r.min);
988 border(modbutton, r, 2, tagcols[BORD], ZP);
990 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
991 draw(modbutton, r, tmp, nil, ZP);
995 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
997 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
998 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
1002 * /dev/snarf updates when the file is closed, so we must open our own
1003 * fd here rather than use snarffd
1006 /* rio truncates larges snarf buffers, so this avoids using the
1007 * service if the string is huge */
1009 #define MAXSNARF 100*1024
1020 if(snarfbuf.nc > MAXSNARF)
1024 for(i=0; i<snarfbuf.nc; i+=n){
1028 bufread(&snarfbuf, i, snarfrune, n);
1029 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
1032 s = fmtstrflush(&f);
1042 int nb, nr, nulls, len;
1046 if(s == nil || s[0]==0){
1052 r = runemalloc(len+1);
1053 cvttorunes(s, len, r, &nb, &nr, &nulls);
1054 bufreset(&snarfbuf);
1055 bufinsert(&snarfbuf, 0, r, nr);