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");
114 p = getenv("tabstop");
116 maxtab = strtoul(p, nil, 0);
122 rowloadfonts(loadfile);
123 putenv("font", fontnames[0]);
124 snarffd = open("/dev/snarf", OREAD|OCEXEC);
127 sprint(buf, "/acme/bin/%s", cputype);
128 bind(buf, "/bin", MBEFORE);
130 bind("/acme/bin", "/bin", MBEFORE);
132 getwd(wdir, sizeof wdir);
135 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
136 fprint(2, "acme: can't open display: %r\n");
137 exits("geninitdraw");
140 if(initdraw(derror, fontnames[0], "acme") < 0){
141 fprint(2, "acme: can't open display: %r\n");
146 font = d->defaultfont;
150 reffonts[0] = &reffont;
151 incref(&reffont.ref); /* one to hold up 'font' variable */
152 incref(&reffont.ref); /* one to hold up reffonts[0] */
153 fontcache = emalloc(sizeof(Reffont*));
155 fontcache[0] = &reffont;
161 cwait = threadwaitchan();
162 ccommand = chancreate(sizeof(Command**), 0);
163 ckill = chancreate(sizeof(Rune*), 0);
164 cxfidalloc = chancreate(sizeof(Xfid*), 0);
165 cxfidfree = chancreate(sizeof(Xfid*), 0);
166 cnewwindow = chancreate(sizeof(Channel*), 0);
167 cerr = chancreate(sizeof(char*), 0);
168 cedit = chancreate(sizeof(int), 0);
169 cexit = chancreate(sizeof(int), 0);
170 cwarn = chancreate(sizeof(void*), 1);
171 if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){
172 fprint(2, "acme: can't create initial channels: %r\n");
176 mousectl = initmouse(nil, screen);
178 fprint(2, "acme: can't initialize mouse: %r\n");
181 mouse = &mousectl->m;
182 keyboardctl = initkeyboard(nil);
183 if(keyboardctl == nil){
184 fprint(2, "acme: can't initialize keyboard: %r\n");
190 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
192 fprint(2, "acme: can't initialize plumber: %r\n");
194 cplumb = chancreate(sizeof(Plumbmsg*), 0);
195 threadcreate(plumbproc, nil, STACK);
197 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
205 rowload(&row, loadfile, TRUE);
207 rowinit(&row, screen->clipr);
212 ncol = (argc+(WPERCOL-1))/WPERCOL;
219 for(i=0; i<ncol; i++){
220 c = rowadd(&row, nil, -1);
222 error("initializing columns");
224 c = row.col[row.ncol-1];
228 for(i=0; i<argc; i++){
229 p = utfrrune(argv[i], '/');
230 if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol)
231 readfile(c, argv[i]);
233 readfile(row.col[i/WPERCOL], argv[i]);
236 flushimage(display, 1);
239 threadcreate(keyboardthread, nil, STACK);
240 threadcreate(mousethread, nil, STACK);
241 threadcreate(waitthread, nil, STACK);
242 threadcreate(xfidallocthread, nil, STACK);
243 threadcreate(newwindowthread, nil, STACK);
245 threadnotify(shutdown, 1);
252 readfile(Column *c, char *s)
259 w = coladd(c, nil, nil, -1);
260 cvttorunes(s, strlen(s), rb, &nb, &nr, nil);
261 rs = cleanrname(runestr(rb, nr));
262 winsetname(w, rs.r, rs.nr);
263 textload(&w->body, 0, s, 1);
264 w->body.file->mod = FALSE;
267 textscrdraw(&w->body);
268 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
282 shutdown(void *v, char *msg)
286 if(strcmp(msg, "sys: write on closed pipe") == 0)
291 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
295 for(i=0; oknotes[i]; i++)
296 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
298 print("acme: %s\n", msg);
310 // flushimage(display, 1);
312 for(c=command; c; c=c->next)
313 postnote(PNGROUP, c->pid, "hangup");
320 acmeerrorproc(void *v)
326 threadsetname("acmeerrorproc");
327 buf = emalloc(8192+1);
328 while((n=threadread(errorfd, buf, 8192)) >= 0){
330 sendp(cerr, estrdup(buf));
340 error("can't create pipe");
342 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
343 fd = create(acmeerrorfile, OWRITE, 0666);
345 remove(acmeerrorfile);
346 fd = create(acmeerrorfile, OWRITE, 0666);
348 error("can't create acmeerror file");
350 sprint(buf, "%d", pfd[0]);
351 write(fd, buf, strlen(buf));
353 /* reopen pfd[1] close on exec */
354 sprint(buf, "/fd/%d", pfd[1]);
355 errorfd = open(buf, OREAD|OCEXEC);
357 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
358 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
362 error("can't re-open acmeerror file");
363 threadcreate(acmeerrorproc, nil, STACK);
373 threadsetname("plumbproc");
375 m = threadplumbrecv(plumbeditfd);
384 keyboardthread(void *v)
389 enum { KTimer, KKey, NKALT };
390 static Alt alts[NKALT+1];
393 alts[KTimer].c = nil;
394 alts[KTimer].v = nil;
395 alts[KTimer].op = CHANNOP;
396 alts[KKey].c = keyboardctl->c;
398 alts[KKey].op = CHANRCV;
399 alts[NKALT].op = CHANEND;
403 threadsetname("keyboardthread");
409 if(t!=nil && t->what==Tag){
413 flushimage(display, 1);
415 alts[KTimer].c = nil;
416 alts[KTimer].op = CHANNOP;
420 typetext = rowtype(&row, r, mouse->xy);
422 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
424 if(t!=nil && t->w!=nil)
425 t->w->body.file->curtext = &t->w->body;
428 if(t!=nil && t->what==Tag) {
429 timer = timerstart(500);
430 alts[KTimer].c = timer->c;
431 alts[KTimer].op = CHANRCV;
434 alts[KTimer].c = nil;
435 alts[KTimer].op = CHANNOP;
437 if(nbrecv(keyboardctl->c, &r) > 0)
439 flushimage(display, 1);
455 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
456 static Alt alts[NMALT+1];
459 threadsetname("mousethread");
460 alts[MResize].c = mousectl->resizec;
461 alts[MResize].v = nil;
462 alts[MResize].op = CHANRCV;
463 alts[MMouse].c = mousectl->c;
464 alts[MMouse].v = &mousectl->m;
465 alts[MMouse].op = CHANRCV;
466 alts[MPlumb].c = cplumb;
467 alts[MPlumb].v = ±
468 alts[MPlumb].op = CHANRCV;
469 alts[MWarnings].c = cwarn;
470 alts[MWarnings].v = nil;
471 alts[MWarnings].op = CHANRCV;
473 alts[MPlumb].op = CHANNOP;
474 alts[NMALT].op = CHANEND;
480 flushimage(display, 1);
483 if(getwindow(display, Refnone) < 0)
484 error("attach to window");
485 draw(screen, screen->r, display->white, nil, ZP);
487 rowresize(&row, screen->clipr);
490 if(strcmp(pm->type, "text") == 0){
491 act = plumblookup(pm->attr, "action");
492 if(act==nil || strcmp(act, "showfile")==0)
494 else if(strcmp(act, "showdata")==0)
503 * Make a copy so decisions are consistent; mousectl changes
504 * underfoot. Can't just receive into m because this introduces
505 * another race; see /sys/src/libdraw/mouse.c.
509 t = rowwhich(&row, m.xy);
510 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
511 winlock(mousetext->w, 'M');
513 wincommit(mousetext->w, mousetext);
514 winunlock(mousetext->w);
520 if(t==nil || m.buttons==0)
525 else if(m.buttons == 2)
527 else if(m.buttons == 4)
530 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
539 if(ptinrect(m.xy, t->scrollr)){
541 if(t->what == Columntag)
542 rowdragcol(&row, t->col, but);
543 else if(t->what == Tag){
544 coldragwin(t->col, t->w, but);
546 barttext = &t->w->body;
568 activecol = t->col; /* button 1 only */
569 if(t->w!=nil && t==&t->w->body)
571 }else if(m.buttons & 2){
572 if(textselect2(t, &q0, &q1, &argt))
573 execute(t, q0, q1, FALSE, argt);
574 }else if(m.buttons & 4){
575 if(textselect3(t, &q0, &q1))
576 look3(t, q0, q1, FALSE);
590 * There is a race between process exiting and our finding out it was ever created.
591 * This structure keeps a list of processes that have exited we haven't heard of.
593 typedef struct Pid Pid;
611 Pid *pids, *p, *lastp;
612 enum { WErr, WKill, WWait, WCmd, NWALT };
616 threadsetname("waitthread");
620 alts[WErr].op = CHANRCV;
621 alts[WKill].c = ckill;
622 alts[WKill].v = &cmd;
623 alts[WKill].op = CHANRCV;
624 alts[WWait].c = cwait;
626 alts[WWait].op = CHANRCV;
627 alts[WCmd].c = ccommand;
629 alts[WCmd].op = CHANRCV;
630 alts[NWALT].op = CHANEND;
637 warning(nil, "%s", err);
639 flushimage(display, 1);
644 ncmd = runestrlen(cmd);
645 for(c=command; c; c=c->next){
647 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
648 if(postnote(PNGROUP, c->pid, "kill") < 0)
649 warning(nil, "kill %S: %r\n", cmd);
654 warning(nil, "Kill: no process %S\n", cmd);
660 for(c=command; c; c=c->next){
674 /* helper processes use this exit status */
675 if(strncmp(w->msg, "libthread", 9) != 0){
676 p = emalloc(sizeof(Pid));
678 strncpy(p->msg, w->msg, sizeof(p->msg));
683 if(search(t, c->name, c->nname)){
684 textdelete(t, t->q0, t->q1, TRUE);
685 textsetselect(t, 0, 0);
688 warning(c->md, "%S: %s\n", c->name, w->msg);
689 flushimage(display, 1);
704 /* has this command already exited? */
706 for(p=pids; p!=nil; p=p->next){
707 if(p->pid == c->pid){
709 warning(c->md, "%s\n", p->msg);
713 lastp->next = p->next;
724 textinsert(t, 0, c->name, c->nname, TRUE);
725 textsetselect(t, 0, 0);
726 flushimage(display, 1);
734 xfidallocthread(void *v)
737 enum { Alloc, Free, N };
738 static Alt alts[N+1];
741 threadsetname("xfidallocthread");
742 alts[Alloc].c = cxfidalloc;
744 alts[Alloc].op = CHANRCV;
745 alts[Free].c = cxfidfree;
747 alts[Free].op = CHANRCV;
748 alts[N].op = CHANEND;
758 x = emalloc(sizeof(Xfid));
759 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
761 threadcreate(xfidctl, x->arg, STACK);
763 sendp(cxfidalloc, x);
773 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
775 newwindowthread(void *v)
780 threadsetname("newwindowthread");
783 /* only fsysproc is talking to us, so synchronization is trivial */
785 w = makenewwindow(nil);
787 sendp(cnewwindow, w);
792 rfget(int fix, int save, int setfont, char *name)
800 name = fontnames[fix];
804 for(i=0; i<nfontcache; i++)
805 if(strcmp(name, fontcache[i]->f->name) == 0){
809 f = openfont(display, name);
811 fprint(2, "can't open font file %s: %r\n", name);
812 warning(nil, "can't open font file %s: %r\n", name);
815 r = emalloc(sizeof(Reffont));
817 fontcache = realloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
818 fontcache[nfontcache++] = r;
824 rfclose(reffonts[fix]);
826 free(fontnames[fix]);
827 fontnames[fix] = name;
832 rfclose(reffonts[0]);
847 if(decref(&r->ref) == 0){
848 for(i=0; i<nfontcache; i++)
849 if(r == fontcache[i])
852 warning(nil, "internal error: can't find font in cache\n");
855 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
864 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
865 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
866 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
867 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
868 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
869 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
870 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
871 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
881 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
882 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
883 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
884 tagcols[TEXT] = display->black;
885 tagcols[HTEXT] = display->black;
888 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
889 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
890 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
891 textcols[TEXT] = display->black;
892 textcols[HTEXT] = display->black;
896 freeimage(modbutton);
897 freeimage(colbutton);
900 r = Rect(0, 0, Scrollwid+2, font->height+1);
901 button = allocimage(display, r, screen->chan, 0, DNofill);
902 draw(button, r, tagcols[BACK], nil, r.min);
904 border(button, r, 2, tagcols[BORD], ZP);
907 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
908 draw(modbutton, r, tagcols[BACK], nil, r.min);
910 border(modbutton, r, 2, tagcols[BORD], ZP);
912 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
913 draw(modbutton, r, tmp, nil, ZP);
917 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
919 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
920 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
924 * /dev/snarf updates when the file is closed, so we must open our own
925 * fd here rather than use snarffd
928 /* rio truncates larges snarf buffers, so this avoids using the
929 * service if the string is huge */
931 #define MAXSNARF 100*1024
942 if(snarfbuf.nc > MAXSNARF)
946 for(i=0; i<snarfbuf.nc; i+=n){
950 bufread(&snarfbuf, i, snarfrune, n);
951 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
964 int nb, nr, nulls, len;
968 if(s == nil || s[0]==0){
974 r = runemalloc(len+1);
975 cvttorunes(s, len, r, &nb, &nr, &nulls);
977 bufinsert(&snarfbuf, 0, r, nr);