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*);
32 NSnarf = 1000 /* less than 1024, I/O buffer size */
34 Rune snarfrune[NSnarf+1];
38 "/lib/font/bit/lucidasans/euro.8.font",
39 "/lib/font/bit/lucm/unicode.9.font",
44 void shutdownthread(void*);
45 void acmeerrorinit(void);
46 void readfile(Column*, char*);
47 static int shutdown(void*, char*);
50 derror(Display *d, char *errorstr)
57 threadmain(int argc, char *argv[])
65 rfork(RFENVG|RFNAMEG);
72 {extern int _threaddebuglevel;
73 _threaddebuglevel = ~0;
77 globalautoindent = TRUE;
91 fontnames[0] = ARGF();
92 if(fontnames[0] == nil)
96 fontnames[1] = ARGF();
97 if(fontnames[1] == nil)
112 fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
116 fontnames[0] = estrdup(fontnames[0]);
117 fontnames[1] = estrdup(fontnames[1]);
120 cputype = getenv("cputype");
121 objtype = getenv("objtype");
122 home = getenv("home");
124 home = getenv("HOME");
125 p = getenv("tabstop");
127 maxtab = strtoul(p, nil, 0);
133 rowloadfonts(loadfile);
134 putenv("font", fontnames[0]);
135 snarffd = open("/dev/snarf", OREAD|OCEXEC);
138 sprint(buf, "/acme/bin/%s", cputype);
139 bind(buf, "/bin", MBEFORE);
141 bind("/acme/bin", "/bin", MBEFORE);
143 getwd(wdir, sizeof wdir);
146 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
147 fprint(2, "acme: can't open display: %r\n");
148 exits("geninitdraw");
151 if(initdraw(derror, fontnames[0], "acme") < 0){
152 fprint(2, "acme: can't open display: %r\n");
157 font = d->defaultfont;
161 reffonts[0] = &reffont;
162 incref(&reffont.ref); /* one to hold up 'font' variable */
163 incref(&reffont.ref); /* one to hold up reffonts[0] */
164 fontcache = emalloc(sizeof(Reffont*));
166 fontcache[0] = &reffont;
172 cwait = threadwaitchan();
173 ccommand = chancreate(sizeof(Command**), 0);
174 chansetname(ccommand, "ccommand");
175 ckill = chancreate(sizeof(Rune*), 0);
176 chansetname(ckill, "ckill");
177 cxfidalloc = chancreate(sizeof(Xfid*), 0);
178 chansetname(cxfidalloc, "cxfidalloc");
179 cxfidfree = chancreate(sizeof(Xfid*), 0);
180 chansetname(cxfidfree, "cxfidfree");
181 cnewwindow = chancreate(sizeof(Channel*), 0);
182 chansetname(cnewwindow, "cnewwindow");
183 cerr = chancreate(sizeof(char*), 0);
184 chansetname(cerr, "cerr");
185 cedit = chancreate(sizeof(int), 0);
186 chansetname(cedit, "cedit");
187 cexit = chancreate(sizeof(int), 0);
188 chansetname(cexit, "cexit");
189 cwarn = chancreate(sizeof(void*), 1);
190 chansetname(cwarn, "cwarn");
191 if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){
192 fprint(2, "acme: can't create initial channels: %r\n");
196 mousectl = initmouse(nil, screen);
198 fprint(2, "acme: can't initialize mouse: %r\n");
201 mouse = &mousectl->m;
202 keyboardctl = initkeyboard(nil);
203 if(keyboardctl == nil){
204 fprint(2, "acme: can't initialize keyboard: %r\n");
210 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
212 fprint(2, "acme: can't initialize plumber: %r\n");
214 cplumb = chancreate(sizeof(Plumbmsg*), 0);
215 threadcreate(plumbproc, nil, STACK);
217 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
224 if(!loadfile || !rowload(&row, loadfile, TRUE)){
225 rowinit(&row, screen->clipr);
230 ncol = (argc+(WPERCOL-1))/WPERCOL;
237 for(i=0; i<ncol; i++){
238 c = rowadd(&row, nil, -1);
240 error("initializing columns");
242 c = row.col[row.ncol-1];
246 for(i=0; i<argc; i++){
247 p = utfrrune(argv[i], '/');
248 if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol)
249 readfile(c, argv[i]);
251 readfile(row.col[i/WPERCOL], argv[i]);
254 flushimage(display, 1);
257 threadcreate(keyboardthread, nil, STACK);
258 threadcreate(mousethread, nil, STACK);
259 threadcreate(waitthread, nil, STACK);
260 threadcreate(xfidallocthread, nil, STACK);
261 threadcreate(newwindowthread, nil, STACK);
262 /* threadcreate(shutdownthread, nil, STACK); */
263 threadnotify(shutdown, 1);
270 readfile(Column *c, char *s)
277 w = coladd(c, nil, nil, -1);
278 cvttorunes(s, strlen(s), rb, &nb, &nr, nil);
279 rs = cleanrname(runestr(rb, nr));
280 winsetname(w, rs.r, rs.nr);
281 textload(&w->body, 0, s, 1);
282 w->body.file->mod = FALSE;
285 textscrdraw(&w->body);
286 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
300 shutdown(void *v, char *msg)
304 if(strcmp(msg, "sys: write on closed pipe") == 0)
309 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
313 for(i=0; oknotes[i]; i++)
314 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
316 print("acme: %s\n", msg);
323 shutdownthread(void *v)
330 c = threadnotechan();
331 while((msg = recvp(c)) != nil)
343 // flushimage(display, 1);
345 for(c=command; c; c=c->next)
346 postnote(PNGROUP, c->pid, "hangup");
353 acmeerrorproc(void *v)
359 threadsetname("acmeerrorproc");
360 buf = emalloc(8192+1);
361 while((n=read(errorfd, buf, 8192)) >= 0){
363 sendp(cerr, estrdup(buf));
373 error("can't create pipe");
375 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
376 fd = create(acmeerrorfile, OWRITE, 0666);
378 remove(acmeerrorfile);
379 fd = create(acmeerrorfile, OWRITE, 0666);
381 error("can't create acmeerror file");
383 sprint(buf, "%d", pfd[0]);
384 write(fd, buf, strlen(buf));
386 /* reopen pfd[1] close on exec */
387 sprint(buf, "/fd/%d", pfd[1]);
388 errorfd = open(buf, OREAD|OCEXEC);
390 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
391 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
395 error("can't re-open acmeerror file");
396 proccreate(acmeerrorproc, nil, STACK);
406 threadsetname("plumbproc");
408 m = threadplumbrecv(plumbeditfd);
417 keyboardthread(void *v)
422 enum { KTimer, KKey, NKALT };
423 static Alt alts[NKALT+1];
426 alts[KTimer].c = nil;
427 alts[KTimer].v = nil;
428 alts[KTimer].op = CHANNOP;
429 alts[KKey].c = keyboardctl->c;
431 alts[KKey].op = CHANRCV;
432 alts[NKALT].op = CHANEND;
436 threadsetname("keyboardthread");
442 if(t!=nil && t->what==Tag){
446 flushimage(display, 1);
448 alts[KTimer].c = nil;
449 alts[KTimer].op = CHANNOP;
453 typetext = rowtype(&row, r, mouse->xy);
455 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
457 if(t!=nil && t->w!=nil)
458 t->w->body.file->curtext = &t->w->body;
461 if(t!=nil && t->what==Tag) {
462 timer = timerstart(500);
463 alts[KTimer].c = timer->c;
464 alts[KTimer].op = CHANRCV;
467 alts[KTimer].c = nil;
468 alts[KTimer].op = CHANNOP;
470 if(nbrecv(keyboardctl->c, &r) > 0)
472 flushimage(display, 1);
488 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
489 static Alt alts[NMALT+1];
492 threadsetname("mousethread");
493 alts[MResize].c = mousectl->resizec;
494 alts[MResize].v = nil;
495 alts[MResize].op = CHANRCV;
496 alts[MMouse].c = mousectl->c;
497 alts[MMouse].v = &mousectl->m;
498 alts[MMouse].op = CHANRCV;
499 alts[MPlumb].c = cplumb;
500 alts[MPlumb].v = ±
501 alts[MPlumb].op = CHANRCV;
502 alts[MWarnings].c = cwarn;
503 alts[MWarnings].v = nil;
504 alts[MWarnings].op = CHANRCV;
506 alts[MPlumb].op = CHANNOP;
507 alts[NMALT].op = CHANEND;
513 flushimage(display, 1);
516 if(getwindow(display, Refnone) < 0)
517 error("attach to window");
518 draw(screen, screen->r, display->white, nil, ZP);
520 rowresize(&row, screen->clipr);
523 if(strcmp(pm->type, "text") == 0){
524 act = plumblookup(pm->attr, "action");
525 if(act==nil || strcmp(act, "showfile")==0)
527 else if(strcmp(act, "showdata")==0)
536 * Make a copy so decisions are consistent; mousectl changes
537 * underfoot. Can't just receive into m because this introduces
538 * another race; see /sys/src/libdraw/mouse.c.
542 t = rowwhich(&row, m.xy);
543 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
544 winlock(mousetext->w, 'M');
546 wincommit(mousetext->w, mousetext);
547 winunlock(mousetext->w);
553 if(t==nil || m.buttons==0)
558 else if(m.buttons == 2)
560 else if(m.buttons == 4)
563 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
572 /* scroll buttons, wheels, etc. */
573 if(t->what==Body && w != nil && (m.buttons & (8|16))){
577 but = Kscrollonedown;
584 if(ptinrect(m.xy, t->scrollr)){
586 if(t->what == Columntag)
587 rowdragcol(&row, t->col, but);
588 else if(t->what == Tag){
589 coldragwin(t->col, t->w, but);
591 barttext = &t->w->body;
613 activecol = t->col; /* button 1 only */
614 if(t->w!=nil && t==&t->w->body)
616 }else if(m.buttons & 2){
617 if(textselect2(t, &q0, &q1, &argt))
618 execute(t, q0, q1, FALSE, argt);
619 }else if(m.buttons & 4){
620 if(textselect3(t, &q0, &q1))
621 look3(t, q0, q1, FALSE);
635 * There is a race between process exiting and our finding out it was ever created.
636 * This structure keeps a list of processes that have exited we haven't heard of.
638 typedef struct Pid Pid;
656 Pid *pids, *p, *lastp;
657 enum { WErr, WKill, WWait, WCmd, NWALT };
661 threadsetname("waitthread");
665 alts[WErr].op = CHANRCV;
666 alts[WKill].c = ckill;
667 alts[WKill].v = &cmd;
668 alts[WKill].op = CHANRCV;
669 alts[WWait].c = cwait;
671 alts[WWait].op = CHANRCV;
672 alts[WCmd].c = ccommand;
674 alts[WCmd].op = CHANRCV;
675 alts[NWALT].op = CHANEND;
682 warning(nil, "%s", err);
684 flushimage(display, 1);
689 ncmd = runestrlen(cmd);
690 for(c=command; c; c=c->next){
692 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
693 if(postnote(PNGROUP, c->pid, "kill") < 0)
694 warning(nil, "kill %S: %r\n", cmd);
699 warning(nil, "Kill: no process %S\n", cmd);
705 for(c=command; c; c=c->next){
719 /* helper processes use this exit status */
720 if(strncmp(w->msg, "libthread", 9) != 0){
721 p = emalloc(sizeof(Pid));
723 strncpy(p->msg, w->msg, sizeof(p->msg));
728 if(search(t, c->name, c->nname)){
729 textdelete(t, t->q0, t->q1, TRUE);
730 textsetselect(t, 0, 0);
733 warning(c->md, "%.*S: exit %s\n", c->nname-1, c->name, w->msg);
734 flushimage(display, 1);
749 /* has this command already exited? */
751 for(p=pids; p!=nil; p=p->next){
752 if(p->pid == c->pid){
754 warning(c->md, "%s\n", p->msg);
758 lastp->next = p->next;
769 textinsert(t, 0, c->name, c->nname, TRUE);
770 textsetselect(t, 0, 0);
771 flushimage(display, 1);
779 xfidallocthread(void *v)
782 enum { Alloc, Free, N };
783 static Alt alts[N+1];
786 threadsetname("xfidallocthread");
787 alts[Alloc].c = cxfidalloc;
789 alts[Alloc].op = CHANRCV;
790 alts[Free].c = cxfidfree;
792 alts[Free].op = CHANRCV;
793 alts[N].op = CHANEND;
803 x = emalloc(sizeof(Xfid));
804 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
805 chansetname(x->c, "xc%p", x->c);
807 threadcreate(xfidctl, x->arg, STACK);
809 sendp(cxfidalloc, x);
819 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
821 newwindowthread(void *v)
826 threadsetname("newwindowthread");
829 /* only fsysproc is talking to us, so synchronization is trivial */
831 w = makenewwindow(nil);
833 sendp(cnewwindow, w);
838 rfget(int fix, int save, int setfont, char *name)
846 name = fontnames[fix];
850 for(i=0; i<nfontcache; i++)
851 if(strcmp(name, fontcache[i]->f->name) == 0){
855 f = openfont(display, name);
857 fprint(2, "can't open font file %s: %r\n", name);
858 warning(nil, "can't open font file %s: %r\n", name);
861 r = emalloc(sizeof(Reffont));
863 fontcache = erealloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
864 fontcache[nfontcache++] = r;
870 rfclose(reffonts[fix]);
872 if(fontnames[fix] != name){
873 free(fontnames[fix]);
874 fontnames[fix] = estrdup(name);
880 rfclose(reffonts[0]);
895 if(decref(&r->ref) == 0){
896 for(i=0; i<nfontcache; i++)
897 if(r == fontcache[i])
900 warning(nil, "internal error: can't find font in cache\n");
903 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
912 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
913 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
914 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
915 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
916 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
917 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
918 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
919 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
929 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
930 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
931 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
932 tagcols[TEXT] = display->black;
933 tagcols[HTEXT] = display->black;
936 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
937 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
938 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
939 textcols[TEXT] = display->black;
940 textcols[HTEXT] = display->black;
944 freeimage(modbutton);
945 freeimage(colbutton);
948 r = Rect(0, 0, Scrollwid+2, font->height+1);
949 button = allocimage(display, r, screen->chan, 0, DNofill);
950 draw(button, r, tagcols[BACK], nil, r.min);
952 border(button, r, 2, tagcols[BORD], ZP);
955 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
956 draw(modbutton, r, tagcols[BACK], nil, r.min);
958 border(modbutton, r, 2, tagcols[BORD], ZP);
960 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
961 draw(modbutton, r, tmp, nil, ZP);
965 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
967 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
968 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
972 * /dev/snarf updates when the file is closed, so we must open our own
973 * fd here rather than use snarffd
976 /* rio truncates larges snarf buffers, so this avoids using the
977 * service if the string is huge */
979 #define MAXSNARF 100*1024
990 if(snarfbuf.nc > MAXSNARF)
994 for(i=0; i<snarfbuf.nc; i+=n){
998 bufread(&snarfbuf, i, snarfrune, n);
999 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
1002 s = fmtstrflush(&f);
1012 int nb, nr, nulls, len;
1016 if(s == nil || s[0]==0){
1022 r = runemalloc(len+1);
1023 cvttorunes(s, len, r, &nb, &nr, &nulls);
1024 bufreset(&snarfbuf);
1025 bufinsert(&snarfbuf, 0, r, nr);