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 acmeerrorinit(void);
45 void readfile(Column*, char*);
46 static int shutdown(void*, char*);
49 derror(Display *d, char *errorstr)
56 threadmain(int argc, char *argv[])
64 rfork(RFENVG|RFNAMEG);
71 globalautoindent = TRUE;
85 fontnames[0] = ARGF();
86 if(fontnames[0] == nil)
90 fontnames[1] = ARGF();
91 if(fontnames[1] == nil)
106 fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
111 cputype = getenv("cputype");
112 objtype = getenv("objtype");
113 home = getenv("home");
115 home = getenv("HOME");
116 p = getenv("tabstop");
118 maxtab = strtoul(p, nil, 0);
124 rowloadfonts(loadfile);
125 putenv("font", fontnames[0]);
126 snarffd = open("/dev/snarf", OREAD|OCEXEC);
129 sprint(buf, "/acme/bin/%s", cputype);
130 bind(buf, "/bin", MBEFORE);
132 bind("/acme/bin", "/bin", MBEFORE);
134 getwd(wdir, sizeof wdir);
137 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
138 fprint(2, "acme: can't open display: %r\n");
139 exits("geninitdraw");
142 if(initdraw(derror, fontnames[0], "acme") < 0){
143 fprint(2, "acme: can't open display: %r\n");
148 font = d->defaultfont;
152 reffonts[0] = &reffont;
153 incref(&reffont.ref); /* one to hold up 'font' variable */
154 incref(&reffont.ref); /* one to hold up reffonts[0] */
155 fontcache = emalloc(sizeof(Reffont*));
157 fontcache[0] = &reffont;
163 cwait = threadwaitchan();
164 ccommand = chancreate(sizeof(Command**), 0);
165 ckill = chancreate(sizeof(Rune*), 0);
166 cxfidalloc = chancreate(sizeof(Xfid*), 0);
167 cxfidfree = chancreate(sizeof(Xfid*), 0);
168 cnewwindow = chancreate(sizeof(Channel*), 0);
169 cerr = chancreate(sizeof(char*), 0);
170 cedit = chancreate(sizeof(int), 0);
171 cexit = chancreate(sizeof(int), 0);
172 cwarn = chancreate(sizeof(void*), 1);
173 if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){
174 fprint(2, "acme: can't create initial channels: %r\n");
178 mousectl = initmouse(nil, screen);
180 fprint(2, "acme: can't initialize mouse: %r\n");
183 mouse = &mousectl->m;
184 keyboardctl = initkeyboard(nil);
185 if(keyboardctl == nil){
186 fprint(2, "acme: can't initialize keyboard: %r\n");
192 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
194 fprint(2, "acme: can't initialize plumber: %r\n");
196 cplumb = chancreate(sizeof(Plumbmsg*), 0);
197 threadcreate(plumbproc, nil, STACK);
199 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
207 rowload(&row, loadfile, TRUE);
209 rowinit(&row, screen->clipr);
214 ncol = (argc+(WPERCOL-1))/WPERCOL;
221 for(i=0; i<ncol; i++){
222 c = rowadd(&row, nil, -1);
224 error("initializing columns");
226 c = row.col[row.ncol-1];
230 for(i=0; i<argc; i++){
231 p = utfrrune(argv[i], '/');
232 if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol)
233 readfile(c, argv[i]);
235 readfile(row.col[i/WPERCOL], argv[i]);
238 flushimage(display, 1);
241 threadcreate(keyboardthread, nil, STACK);
242 threadcreate(mousethread, nil, STACK);
243 threadcreate(waitthread, nil, STACK);
244 threadcreate(xfidallocthread, nil, STACK);
245 threadcreate(newwindowthread, nil, STACK);
247 threadnotify(shutdown, 1);
254 readfile(Column *c, char *s)
261 w = coladd(c, nil, nil, -1);
262 cvttorunes(s, strlen(s), rb, &nb, &nr, nil);
263 rs = cleanrname(runestr(rb, nr));
264 winsetname(w, rs.r, rs.nr);
265 textload(&w->body, 0, s, 1);
266 w->body.file->mod = FALSE;
269 textscrdraw(&w->body);
270 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
284 shutdown(void *v, char *msg)
288 if(strcmp(msg, "sys: write on closed pipe") == 0)
293 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
297 for(i=0; oknotes[i]; i++)
298 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
300 print("acme: %s\n", msg);
312 // flushimage(display, 1);
314 for(c=command; c; c=c->next)
315 postnote(PNGROUP, c->pid, "hangup");
322 acmeerrorproc(void *v)
328 threadsetname("acmeerrorproc");
329 buf = emalloc(8192+1);
330 while((n=threadread(errorfd, buf, 8192)) >= 0){
332 sendp(cerr, estrdup(buf));
342 error("can't create pipe");
344 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
345 fd = create(acmeerrorfile, OWRITE, 0666);
347 remove(acmeerrorfile);
348 fd = create(acmeerrorfile, OWRITE, 0666);
350 error("can't create acmeerror file");
352 sprint(buf, "%d", pfd[0]);
353 write(fd, buf, strlen(buf));
355 /* reopen pfd[1] close on exec */
356 sprint(buf, "/fd/%d", pfd[1]);
357 errorfd = open(buf, OREAD|OCEXEC);
359 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
360 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
364 error("can't re-open acmeerror file");
365 threadcreate(acmeerrorproc, nil, STACK);
375 threadsetname("plumbproc");
377 m = threadplumbrecv(plumbeditfd);
386 keyboardthread(void *v)
391 enum { KTimer, KKey, NKALT };
392 static Alt alts[NKALT+1];
395 alts[KTimer].c = nil;
396 alts[KTimer].v = nil;
397 alts[KTimer].op = CHANNOP;
398 alts[KKey].c = keyboardctl->c;
400 alts[KKey].op = CHANRCV;
401 alts[NKALT].op = CHANEND;
405 threadsetname("keyboardthread");
411 if(t!=nil && t->what==Tag){
415 flushimage(display, 1);
417 alts[KTimer].c = nil;
418 alts[KTimer].op = CHANNOP;
422 typetext = rowtype(&row, r, mouse->xy);
424 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
426 if(t!=nil && t->w!=nil)
427 t->w->body.file->curtext = &t->w->body;
430 if(t!=nil && t->what==Tag) {
431 timer = timerstart(500);
432 alts[KTimer].c = timer->c;
433 alts[KTimer].op = CHANRCV;
436 alts[KTimer].c = nil;
437 alts[KTimer].op = CHANNOP;
439 if(nbrecv(keyboardctl->c, &r) > 0)
441 flushimage(display, 1);
457 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
458 static Alt alts[NMALT+1];
461 threadsetname("mousethread");
462 alts[MResize].c = mousectl->resizec;
463 alts[MResize].v = nil;
464 alts[MResize].op = CHANRCV;
465 alts[MMouse].c = mousectl->c;
466 alts[MMouse].v = &mousectl->m;
467 alts[MMouse].op = CHANRCV;
468 alts[MPlumb].c = cplumb;
469 alts[MPlumb].v = ±
470 alts[MPlumb].op = CHANRCV;
471 alts[MWarnings].c = cwarn;
472 alts[MWarnings].v = nil;
473 alts[MWarnings].op = CHANRCV;
475 alts[MPlumb].op = CHANNOP;
476 alts[NMALT].op = CHANEND;
482 flushimage(display, 1);
485 if(getwindow(display, Refnone) < 0)
486 error("attach to window");
487 draw(screen, screen->r, display->white, nil, ZP);
489 rowresize(&row, screen->clipr);
492 if(strcmp(pm->type, "text") == 0){
493 act = plumblookup(pm->attr, "action");
494 if(act==nil || strcmp(act, "showfile")==0)
496 else if(strcmp(act, "showdata")==0)
505 * Make a copy so decisions are consistent; mousectl changes
506 * underfoot. Can't just receive into m because this introduces
507 * another race; see /sys/src/libdraw/mouse.c.
511 t = rowwhich(&row, m.xy);
512 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
513 winlock(mousetext->w, 'M');
515 wincommit(mousetext->w, mousetext);
516 winunlock(mousetext->w);
522 if(t==nil || m.buttons==0)
527 else if(m.buttons == 2)
529 else if(m.buttons == 4)
532 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
541 /* scroll buttons, wheels, etc. */
542 if(t->what==Body && w != nil && (m.buttons & (8|16))){
546 but = Kscrollonedown;
553 if(ptinrect(m.xy, t->scrollr)){
555 if(t->what == Columntag)
556 rowdragcol(&row, t->col, but);
557 else if(t->what == Tag){
558 coldragwin(t->col, t->w, but);
560 barttext = &t->w->body;
582 activecol = t->col; /* button 1 only */
583 if(t->w!=nil && t==&t->w->body)
585 }else if(m.buttons & 2){
586 if(textselect2(t, &q0, &q1, &argt))
587 execute(t, q0, q1, FALSE, argt);
588 }else if(m.buttons & 4){
589 if(textselect3(t, &q0, &q1))
590 look3(t, q0, q1, FALSE);
604 * There is a race between process exiting and our finding out it was ever created.
605 * This structure keeps a list of processes that have exited we haven't heard of.
607 typedef struct Pid Pid;
625 Pid *pids, *p, *lastp;
626 enum { WErr, WKill, WWait, WCmd, NWALT };
630 threadsetname("waitthread");
634 alts[WErr].op = CHANRCV;
635 alts[WKill].c = ckill;
636 alts[WKill].v = &cmd;
637 alts[WKill].op = CHANRCV;
638 alts[WWait].c = cwait;
640 alts[WWait].op = CHANRCV;
641 alts[WCmd].c = ccommand;
643 alts[WCmd].op = CHANRCV;
644 alts[NWALT].op = CHANEND;
651 warning(nil, "%s", err);
653 flushimage(display, 1);
658 ncmd = runestrlen(cmd);
659 for(c=command; c; c=c->next){
661 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
662 if(postnote(PNGROUP, c->pid, "kill") < 0)
663 warning(nil, "kill %S: %r\n", cmd);
668 warning(nil, "Kill: no process %S\n", cmd);
674 for(c=command; c; c=c->next){
688 /* helper processes use this exit status */
689 if(strncmp(w->msg, "libthread", 9) != 0){
690 p = emalloc(sizeof(Pid));
692 strncpy(p->msg, w->msg, sizeof(p->msg));
697 if(search(t, c->name, c->nname)){
698 textdelete(t, t->q0, t->q1, TRUE);
699 textsetselect(t, 0, 0);
702 warning(c->md, "%S: %s\n", c->name, w->msg);
703 flushimage(display, 1);
718 /* has this command already exited? */
720 for(p=pids; p!=nil; p=p->next){
721 if(p->pid == c->pid){
723 warning(c->md, "%s\n", p->msg);
727 lastp->next = p->next;
738 textinsert(t, 0, c->name, c->nname, TRUE);
739 textsetselect(t, 0, 0);
740 flushimage(display, 1);
748 xfidallocthread(void *v)
751 enum { Alloc, Free, N };
752 static Alt alts[N+1];
755 threadsetname("xfidallocthread");
756 alts[Alloc].c = cxfidalloc;
758 alts[Alloc].op = CHANRCV;
759 alts[Free].c = cxfidfree;
761 alts[Free].op = CHANRCV;
762 alts[N].op = CHANEND;
772 x = emalloc(sizeof(Xfid));
773 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
775 threadcreate(xfidctl, x->arg, STACK);
777 sendp(cxfidalloc, x);
787 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
789 newwindowthread(void *v)
794 threadsetname("newwindowthread");
797 /* only fsysproc is talking to us, so synchronization is trivial */
799 w = makenewwindow(nil);
801 sendp(cnewwindow, w);
806 rfget(int fix, int save, int setfont, char *name)
814 name = fontnames[fix];
818 for(i=0; i<nfontcache; i++)
819 if(strcmp(name, fontcache[i]->f->name) == 0){
823 f = openfont(display, name);
825 fprint(2, "can't open font file %s: %r\n", name);
826 warning(nil, "can't open font file %s: %r\n", name);
829 r = emalloc(sizeof(Reffont));
831 fontcache = realloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
832 fontcache[nfontcache++] = r;
838 rfclose(reffonts[fix]);
840 free(fontnames[fix]);
841 fontnames[fix] = name;
846 rfclose(reffonts[0]);
861 if(decref(&r->ref) == 0){
862 for(i=0; i<nfontcache; i++)
863 if(r == fontcache[i])
866 warning(nil, "internal error: can't find font in cache\n");
869 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
878 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
879 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
880 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
881 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
882 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
883 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
884 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
885 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
895 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
896 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
897 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
898 tagcols[TEXT] = display->black;
899 tagcols[HTEXT] = display->black;
902 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
903 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
904 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
905 textcols[TEXT] = display->black;
906 textcols[HTEXT] = display->black;
910 freeimage(modbutton);
911 freeimage(colbutton);
914 r = Rect(0, 0, Scrollwid+2, font->height+1);
915 button = allocimage(display, r, screen->chan, 0, DNofill);
916 draw(button, r, tagcols[BACK], nil, r.min);
918 border(button, r, 2, tagcols[BORD], ZP);
921 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
922 draw(modbutton, r, tagcols[BACK], nil, r.min);
924 border(modbutton, r, 2, tagcols[BORD], ZP);
926 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
927 draw(modbutton, r, tmp, nil, ZP);
931 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
933 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
934 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
938 * /dev/snarf updates when the file is closed, so we must open our own
939 * fd here rather than use snarffd
942 /* rio truncates larges snarf buffers, so this avoids using the
943 * service if the string is huge */
945 #define MAXSNARF 100*1024
956 if(snarfbuf.nc > MAXSNARF)
960 for(i=0; i<snarfbuf.nc; i+=n){
964 bufread(&snarfbuf, i, snarfrune, n);
965 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
978 int nb, nr, nulls, len;
982 if(s == nil || s[0]==0){
988 r = runemalloc(len+1);
989 cvttorunes(s, len, r, &nb, &nr, &nulls);
991 bufinsert(&snarfbuf, 0, r, nr);