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 {extern int _threaddebuglevel;
72 _threaddebuglevel = ~0;
76 globalautoindent = TRUE;
90 fontnames[0] = ARGF();
91 if(fontnames[0] == nil)
95 fontnames[1] = ARGF();
96 if(fontnames[1] == nil)
111 fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
116 cputype = getenv("cputype");
117 objtype = getenv("objtype");
118 home = getenv("home");
120 home = getenv("HOME");
121 p = getenv("tabstop");
123 maxtab = strtoul(p, nil, 0);
129 rowloadfonts(loadfile);
130 putenv("font", fontnames[0]);
131 snarffd = open("/dev/snarf", OREAD|OCEXEC);
134 sprint(buf, "/acme/bin/%s", cputype);
135 bind(buf, "/bin", MBEFORE);
137 bind("/acme/bin", "/bin", MBEFORE);
139 getwd(wdir, sizeof wdir);
142 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
143 fprint(2, "acme: can't open display: %r\n");
144 exits("geninitdraw");
147 if(initdraw(derror, fontnames[0], "acme") < 0){
148 fprint(2, "acme: can't open display: %r\n");
153 font = d->defaultfont;
157 reffonts[0] = &reffont;
158 incref(&reffont.ref); /* one to hold up 'font' variable */
159 incref(&reffont.ref); /* one to hold up reffonts[0] */
160 fontcache = emalloc(sizeof(Reffont*));
162 fontcache[0] = &reffont;
168 cwait = threadwaitchan();
169 ccommand = chancreate(sizeof(Command**), 0);
170 ckill = chancreate(sizeof(Rune*), 0);
171 cxfidalloc = chancreate(sizeof(Xfid*), 0);
172 cxfidfree = chancreate(sizeof(Xfid*), 0);
173 cnewwindow = chancreate(sizeof(Channel*), 0);
174 cerr = chancreate(sizeof(char*), 0);
175 cedit = chancreate(sizeof(int), 0);
176 cexit = chancreate(sizeof(int), 0);
177 cwarn = chancreate(sizeof(void*), 1);
178 if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){
179 fprint(2, "acme: can't create initial channels: %r\n");
183 mousectl = initmouse(nil, screen);
185 fprint(2, "acme: can't initialize mouse: %r\n");
188 mouse = &mousectl->m;
189 keyboardctl = initkeyboard(nil);
190 if(keyboardctl == nil){
191 fprint(2, "acme: can't initialize keyboard: %r\n");
197 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
199 fprint(2, "acme: can't initialize plumber: %r\n");
201 cplumb = chancreate(sizeof(Plumbmsg*), 0);
202 threadcreate(plumbproc, nil, STACK);
204 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
212 rowload(&row, loadfile, TRUE);
214 rowinit(&row, screen->clipr);
219 ncol = (argc+(WPERCOL-1))/WPERCOL;
226 for(i=0; i<ncol; i++){
227 c = rowadd(&row, nil, -1);
229 error("initializing columns");
231 c = row.col[row.ncol-1];
235 for(i=0; i<argc; i++){
236 p = utfrrune(argv[i], '/');
237 if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol)
238 readfile(c, argv[i]);
240 readfile(row.col[i/WPERCOL], argv[i]);
243 flushimage(display, 1);
246 threadcreate(keyboardthread, nil, STACK);
247 threadcreate(mousethread, nil, STACK);
248 threadcreate(waitthread, nil, STACK);
249 threadcreate(xfidallocthread, nil, STACK);
250 threadcreate(newwindowthread, nil, STACK);
252 threadnotify(shutdown, 1);
259 readfile(Column *c, char *s)
266 w = coladd(c, nil, nil, -1);
267 cvttorunes(s, strlen(s), rb, &nb, &nr, nil);
268 rs = cleanrname(runestr(rb, nr));
269 winsetname(w, rs.r, rs.nr);
270 textload(&w->body, 0, s, 1);
271 w->body.file->mod = FALSE;
274 textscrdraw(&w->body);
275 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
289 shutdown(void *v, char *msg)
293 if(strcmp(msg, "sys: write on closed pipe") == 0)
298 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
302 for(i=0; oknotes[i]; i++)
303 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
305 print("acme: %s\n", msg);
317 // flushimage(display, 1);
319 for(c=command; c; c=c->next)
320 postnote(PNGROUP, c->pid, "hangup");
327 acmeerrorproc(void *v)
333 threadsetname("acmeerrorproc");
334 buf = emalloc(8192+1);
335 while((n=threadread(errorfd, buf, 8192)) >= 0){
337 sendp(cerr, estrdup(buf));
347 error("can't create pipe");
349 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
350 fd = create(acmeerrorfile, OWRITE, 0666);
352 remove(acmeerrorfile);
353 fd = create(acmeerrorfile, OWRITE, 0666);
355 error("can't create acmeerror file");
357 sprint(buf, "%d", pfd[0]);
358 write(fd, buf, strlen(buf));
360 /* reopen pfd[1] close on exec */
361 sprint(buf, "/fd/%d", pfd[1]);
362 errorfd = open(buf, OREAD|OCEXEC);
364 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
365 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
369 error("can't re-open acmeerror file");
370 threadcreate(acmeerrorproc, nil, STACK);
380 threadsetname("plumbproc");
382 m = threadplumbrecv(plumbeditfd);
391 keyboardthread(void *v)
396 enum { KTimer, KKey, NKALT };
397 static Alt alts[NKALT+1];
400 alts[KTimer].c = nil;
401 alts[KTimer].v = nil;
402 alts[KTimer].op = CHANNOP;
403 alts[KKey].c = keyboardctl->c;
405 alts[KKey].op = CHANRCV;
406 alts[NKALT].op = CHANEND;
410 threadsetname("keyboardthread");
416 if(t!=nil && t->what==Tag){
420 flushimage(display, 1);
422 alts[KTimer].c = nil;
423 alts[KTimer].op = CHANNOP;
427 typetext = rowtype(&row, r, mouse->xy);
429 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
431 if(t!=nil && t->w!=nil)
432 t->w->body.file->curtext = &t->w->body;
435 if(t!=nil && t->what==Tag) {
436 timer = timerstart(500);
437 alts[KTimer].c = timer->c;
438 alts[KTimer].op = CHANRCV;
441 alts[KTimer].c = nil;
442 alts[KTimer].op = CHANNOP;
444 if(nbrecv(keyboardctl->c, &r) > 0)
446 flushimage(display, 1);
462 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
463 static Alt alts[NMALT+1];
466 threadsetname("mousethread");
467 alts[MResize].c = mousectl->resizec;
468 alts[MResize].v = nil;
469 alts[MResize].op = CHANRCV;
470 alts[MMouse].c = mousectl->c;
471 alts[MMouse].v = &mousectl->m;
472 alts[MMouse].op = CHANRCV;
473 alts[MPlumb].c = cplumb;
474 alts[MPlumb].v = ±
475 alts[MPlumb].op = CHANRCV;
476 alts[MWarnings].c = cwarn;
477 alts[MWarnings].v = nil;
478 alts[MWarnings].op = CHANRCV;
480 alts[MPlumb].op = CHANNOP;
481 alts[NMALT].op = CHANEND;
487 flushimage(display, 1);
490 if(getwindow(display, Refnone) < 0)
491 error("attach to window");
492 draw(screen, screen->r, display->white, nil, ZP);
494 rowresize(&row, screen->clipr);
497 if(strcmp(pm->type, "text") == 0){
498 act = plumblookup(pm->attr, "action");
499 if(act==nil || strcmp(act, "showfile")==0)
501 else if(strcmp(act, "showdata")==0)
510 * Make a copy so decisions are consistent; mousectl changes
511 * underfoot. Can't just receive into m because this introduces
512 * another race; see /sys/src/libdraw/mouse.c.
516 t = rowwhich(&row, m.xy);
517 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
518 winlock(mousetext->w, 'M');
520 wincommit(mousetext->w, mousetext);
521 winunlock(mousetext->w);
527 if(t==nil || m.buttons==0)
532 else if(m.buttons == 2)
534 else if(m.buttons == 4)
537 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
546 /* scroll buttons, wheels, etc. */
547 if(t->what==Body && w != nil && (m.buttons & (8|16))){
551 but = Kscrollonedown;
558 if(ptinrect(m.xy, t->scrollr)){
560 if(t->what == Columntag)
561 rowdragcol(&row, t->col, but);
562 else if(t->what == Tag){
563 coldragwin(t->col, t->w, but);
565 barttext = &t->w->body;
587 activecol = t->col; /* button 1 only */
588 if(t->w!=nil && t==&t->w->body)
590 }else if(m.buttons & 2){
591 if(textselect2(t, &q0, &q1, &argt))
592 execute(t, q0, q1, FALSE, argt);
593 }else if(m.buttons & 4){
594 if(textselect3(t, &q0, &q1))
595 look3(t, q0, q1, FALSE);
609 * There is a race between process exiting and our finding out it was ever created.
610 * This structure keeps a list of processes that have exited we haven't heard of.
612 typedef struct Pid Pid;
630 Pid *pids, *p, *lastp;
631 enum { WErr, WKill, WWait, WCmd, NWALT };
635 threadsetname("waitthread");
639 alts[WErr].op = CHANRCV;
640 alts[WKill].c = ckill;
641 alts[WKill].v = &cmd;
642 alts[WKill].op = CHANRCV;
643 alts[WWait].c = cwait;
645 alts[WWait].op = CHANRCV;
646 alts[WCmd].c = ccommand;
648 alts[WCmd].op = CHANRCV;
649 alts[NWALT].op = CHANEND;
652 * BUG. Actually there's no bug here but this is the
653 * first place you'd look. When a program is run,
654 * it doesn't disappear from the main tag until the
655 * mouse is moved or keyboard is hit. This would
656 * suggest that the WWait case isn't working right,
657 * but what's actually going on is that the X11 code
658 * is running a select-based threading loop that
659 * doesn't get interrupted until there is data from X11.
660 * This was done to make acme work on Suns and
661 * other systems where our threading was sub-par.
662 * Now that we've gotten pthreads working (sort of),
663 * we might be able to fix this properly.
664 * But the bug is in libdraw and libthread, not here.
671 warning(nil, "%s", err);
673 flushimage(display, 1);
678 ncmd = runestrlen(cmd);
679 for(c=command; c; c=c->next){
681 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
682 if(postnote(PNGROUP, c->pid, "kill") < 0)
683 warning(nil, "kill %S: %r\n", cmd);
688 warning(nil, "Kill: no process %S\n", cmd);
694 for(c=command; c; c=c->next){
708 /* helper processes use this exit status */
709 if(strncmp(w->msg, "libthread", 9) != 0){
710 p = emalloc(sizeof(Pid));
712 strncpy(p->msg, w->msg, sizeof(p->msg));
717 if(search(t, c->name, c->nname)){
718 textdelete(t, t->q0, t->q1, TRUE);
719 textsetselect(t, 0, 0);
722 warning(c->md, "%S: %s\n", c->name, w->msg);
723 flushimage(display, 1);
738 /* has this command already exited? */
740 for(p=pids; p!=nil; p=p->next){
741 if(p->pid == c->pid){
743 warning(c->md, "%s\n", p->msg);
747 lastp->next = p->next;
758 textinsert(t, 0, c->name, c->nname, TRUE);
759 textsetselect(t, 0, 0);
760 flushimage(display, 1);
768 xfidallocthread(void *v)
771 enum { Alloc, Free, N };
772 static Alt alts[N+1];
775 threadsetname("xfidallocthread");
776 alts[Alloc].c = cxfidalloc;
778 alts[Alloc].op = CHANRCV;
779 alts[Free].c = cxfidfree;
781 alts[Free].op = CHANRCV;
782 alts[N].op = CHANEND;
792 x = emalloc(sizeof(Xfid));
793 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
795 threadcreate(xfidctl, x->arg, STACK);
797 sendp(cxfidalloc, x);
807 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
809 newwindowthread(void *v)
814 threadsetname("newwindowthread");
817 /* only fsysproc is talking to us, so synchronization is trivial */
819 w = makenewwindow(nil);
821 sendp(cnewwindow, w);
826 rfget(int fix, int save, int setfont, char *name)
834 name = fontnames[fix];
838 for(i=0; i<nfontcache; i++)
839 if(strcmp(name, fontcache[i]->f->name) == 0){
843 f = openfont(display, name);
845 fprint(2, "can't open font file %s: %r\n", name);
846 warning(nil, "can't open font file %s: %r\n", name);
849 r = emalloc(sizeof(Reffont));
851 fontcache = realloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
852 fontcache[nfontcache++] = r;
858 rfclose(reffonts[fix]);
860 free(fontnames[fix]);
861 fontnames[fix] = name;
866 rfclose(reffonts[0]);
881 if(decref(&r->ref) == 0){
882 for(i=0; i<nfontcache; i++)
883 if(r == fontcache[i])
886 warning(nil, "internal error: can't find font in cache\n");
889 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
898 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
899 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
900 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
901 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
902 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
903 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
904 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
905 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
915 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
916 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
917 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
918 tagcols[TEXT] = display->black;
919 tagcols[HTEXT] = display->black;
922 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
923 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
924 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
925 textcols[TEXT] = display->black;
926 textcols[HTEXT] = display->black;
930 freeimage(modbutton);
931 freeimage(colbutton);
934 r = Rect(0, 0, Scrollwid+2, font->height+1);
935 button = allocimage(display, r, screen->chan, 0, DNofill);
936 draw(button, r, tagcols[BACK], nil, r.min);
938 border(button, r, 2, tagcols[BORD], ZP);
941 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
942 draw(modbutton, r, tagcols[BACK], nil, r.min);
944 border(modbutton, r, 2, tagcols[BORD], ZP);
946 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
947 draw(modbutton, r, tmp, nil, ZP);
951 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
953 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
954 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
958 * /dev/snarf updates when the file is closed, so we must open our own
959 * fd here rather than use snarffd
962 /* rio truncates larges snarf buffers, so this avoids using the
963 * service if the string is huge */
965 #define MAXSNARF 100*1024
976 if(snarfbuf.nc > MAXSNARF)
980 for(i=0; i<snarfbuf.nc; i+=n){
984 bufread(&snarfbuf, i, snarfrune, n);
985 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
998 int nb, nr, nulls, len;
1002 if(s == nil || s[0]==0){
1008 r = runemalloc(len+1);
1009 cvttorunes(s, len, r, &nb, &nr, &nulls);
1010 bufreset(&snarfbuf);
1011 bufinsert(&snarfbuf, 0, r, nr);