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)
101 fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile\n");
105 cputype = getenv("cputype");
106 objtype = getenv("objtype");
107 home = getenv("home");
108 p = getenv("tabstop");
110 maxtab = strtoul(p, nil, 0);
116 rowloadfonts(loadfile);
117 putenv("font", fontnames[0]);
118 snarffd = open("/dev/snarf", OREAD|OCEXEC);
121 sprint(buf, "/acme/bin/%s", cputype);
122 bind(buf, "/bin", MBEFORE);
124 bind("/acme/bin", "/bin", MBEFORE);
126 getwd(wdir, sizeof wdir);
129 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
130 fprint(2, "acme: can't open display: %r\n");
131 exits("geninitdraw");
134 if(initdraw(derror, fontnames[0], "acme") < 0){
135 fprint(2, "acme: can't open display: %r\n");
140 font = d->defaultfont;
144 reffonts[0] = &reffont;
145 incref(&reffont.ref); /* one to hold up 'font' variable */
146 incref(&reffont.ref); /* one to hold up reffonts[0] */
147 fontcache = emalloc(sizeof(Reffont*));
149 fontcache[0] = &reffont;
155 cwait = threadwaitchan();
156 ccommand = chancreate(sizeof(Command**), 0);
157 ckill = chancreate(sizeof(Rune*), 0);
158 cxfidalloc = chancreate(sizeof(Xfid*), 0);
159 cxfidfree = chancreate(sizeof(Xfid*), 0);
160 cnewwindow = chancreate(sizeof(Channel*), 0);
161 cerr = chancreate(sizeof(char*), 0);
162 cedit = chancreate(sizeof(int), 0);
163 cexit = chancreate(sizeof(int), 0);
164 cwarn = chancreate(sizeof(void*), 1);
165 if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){
166 fprint(2, "acme: can't create initial channels: %r\n");
170 mousectl = initmouse(nil, screen);
172 fprint(2, "acme: can't initialize mouse: %r\n");
175 mouse = &mousectl->m;
176 keyboardctl = initkeyboard(nil);
177 if(keyboardctl == nil){
178 fprint(2, "acme: can't initialize keyboard: %r\n");
184 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
186 fprint(2, "acme: can't initialize plumber: %r\n");
188 cplumb = chancreate(sizeof(Plumbmsg*), 0);
189 threadcreate(plumbproc, nil, STACK);
191 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
199 rowload(&row, loadfile, TRUE);
201 rowinit(&row, screen->clipr);
206 ncol = (argc+(WPERCOL-1))/WPERCOL;
213 for(i=0; i<ncol; i++){
214 c = rowadd(&row, nil, -1);
216 error("initializing columns");
218 c = row.col[row.ncol-1];
222 for(i=0; i<argc; i++){
223 p = utfrrune(argv[i], '/');
224 if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol)
225 readfile(c, argv[i]);
227 readfile(row.col[i/WPERCOL], argv[i]);
230 flushimage(display, 1);
233 threadcreate(keyboardthread, nil, STACK);
234 threadcreate(mousethread, nil, STACK);
235 threadcreate(waitthread, nil, STACK);
236 threadcreate(xfidallocthread, nil, STACK);
237 threadcreate(newwindowthread, nil, STACK);
239 threadnotify(shutdown, 1);
246 readfile(Column *c, char *s)
253 w = coladd(c, nil, nil, -1);
254 cvttorunes(s, strlen(s), rb, &nb, &nr, nil);
255 rs = cleanrname(runestr(rb, nr));
256 winsetname(w, rs.r, rs.nr);
257 textload(&w->body, 0, s, 1);
258 w->body.file->mod = FALSE;
261 textscrdraw(&w->body);
262 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
276 shutdown(void *v, char *msg)
280 if(strcmp(msg, "sys: write on closed pipe") == 0)
285 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
289 for(i=0; oknotes[i]; i++)
290 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
292 print("acme: %s\n", msg);
304 // flushimage(display, 1);
306 for(c=command; c; c=c->next)
307 postnote(PNGROUP, c->pid, "hangup");
314 acmeerrorproc(void *v)
320 threadsetname("acmeerrorproc");
321 buf = emalloc(8192+1);
322 while((n=threadread(errorfd, buf, 8192)) >= 0){
324 sendp(cerr, estrdup(buf));
334 error("can't create pipe");
336 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
337 fd = create(acmeerrorfile, OWRITE, 0666);
339 remove(acmeerrorfile);
340 fd = create(acmeerrorfile, OWRITE, 0666);
342 error("can't create acmeerror file");
344 sprint(buf, "%d", pfd[0]);
345 write(fd, buf, strlen(buf));
347 /* reopen pfd[1] close on exec */
348 sprint(buf, "/fd/%d", pfd[1]);
349 errorfd = open(buf, OREAD|OCEXEC);
351 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
352 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
356 error("can't re-open acmeerror file");
357 threadcreate(acmeerrorproc, nil, STACK);
367 threadsetname("plumbproc");
369 m = threadplumbrecv(plumbeditfd);
378 keyboardthread(void *v)
383 enum { KTimer, KKey, NKALT };
384 static Alt alts[NKALT+1];
387 alts[KTimer].c = nil;
388 alts[KTimer].v = nil;
389 alts[KTimer].op = CHANNOP;
390 alts[KKey].c = keyboardctl->c;
392 alts[KKey].op = CHANRCV;
393 alts[NKALT].op = CHANEND;
397 threadsetname("keyboardthread");
403 if(t!=nil && t->what==Tag){
407 flushimage(display, 1);
409 alts[KTimer].c = nil;
410 alts[KTimer].op = CHANNOP;
414 typetext = rowtype(&row, r, mouse->xy);
416 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
418 if(t!=nil && t->w!=nil)
419 t->w->body.file->curtext = &t->w->body;
422 if(t!=nil && t->what==Tag) {
423 timer = timerstart(500);
424 alts[KTimer].c = timer->c;
425 alts[KTimer].op = CHANRCV;
428 alts[KTimer].c = nil;
429 alts[KTimer].op = CHANNOP;
431 if(nbrecv(keyboardctl->c, &r) > 0)
433 flushimage(display, 1);
449 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
450 static Alt alts[NMALT+1];
453 threadsetname("mousethread");
454 alts[MResize].c = mousectl->resizec;
455 alts[MResize].v = nil;
456 alts[MResize].op = CHANRCV;
457 alts[MMouse].c = mousectl->c;
458 alts[MMouse].v = &mousectl->m;
459 alts[MMouse].op = CHANRCV;
460 alts[MPlumb].c = cplumb;
461 alts[MPlumb].v = ±
462 alts[MPlumb].op = CHANRCV;
463 alts[MWarnings].c = cwarn;
464 alts[MWarnings].v = nil;
465 alts[MWarnings].op = CHANRCV;
467 alts[MPlumb].op = CHANNOP;
468 alts[NMALT].op = CHANEND;
474 flushimage(display, 1);
477 if(getwindow(display, Refnone) < 0)
478 error("attach to window");
479 draw(screen, screen->r, display->white, nil, ZP);
481 rowresize(&row, screen->clipr);
484 if(strcmp(pm->type, "text") == 0){
485 act = plumblookup(pm->attr, "action");
486 if(act==nil || strcmp(act, "showfile")==0)
488 else if(strcmp(act, "showdata")==0)
497 * Make a copy so decisions are consistent; mousectl changes
498 * underfoot. Can't just receive into m because this introduces
499 * another race; see /sys/src/libdraw/mouse.c.
503 t = rowwhich(&row, m.xy);
504 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
505 winlock(mousetext->w, 'M');
507 wincommit(mousetext->w, mousetext);
508 winunlock(mousetext->w);
514 if(t==nil || m.buttons==0)
519 else if(m.buttons == 2)
521 else if(m.buttons == 4)
524 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
533 if(ptinrect(m.xy, t->scrollr)){
535 if(t->what == Columntag)
536 rowdragcol(&row, t->col, but);
537 else if(t->what == Tag){
538 coldragwin(t->col, t->w, but);
540 barttext = &t->w->body;
562 activecol = t->col; /* button 1 only */
563 if(t->w!=nil && t==&t->w->body)
565 }else if(m.buttons & 2){
566 if(textselect2(t, &q0, &q1, &argt))
567 execute(t, q0, q1, FALSE, argt);
568 }else if(m.buttons & 4){
569 if(textselect3(t, &q0, &q1))
570 look3(t, q0, q1, FALSE);
584 * There is a race between process exiting and our finding out it was ever created.
585 * This structure keeps a list of processes that have exited we haven't heard of.
587 typedef struct Pid Pid;
605 Pid *pids, *p, *lastp;
606 enum { WErr, WKill, WWait, WCmd, NWALT };
610 threadsetname("waitthread");
614 alts[WErr].op = CHANRCV;
615 alts[WKill].c = ckill;
616 alts[WKill].v = &cmd;
617 alts[WKill].op = CHANRCV;
618 alts[WWait].c = cwait;
620 alts[WWait].op = CHANRCV;
621 alts[WCmd].c = ccommand;
623 alts[WCmd].op = CHANRCV;
624 alts[NWALT].op = CHANEND;
631 warning(nil, "%s", err);
633 flushimage(display, 1);
638 ncmd = runestrlen(cmd);
639 for(c=command; c; c=c->next){
641 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
642 if(postnote(PNGROUP, c->pid, "kill") < 0)
643 warning(nil, "kill %S: %r\n", cmd);
648 warning(nil, "Kill: no process %S\n", cmd);
654 for(c=command; c; c=c->next){
668 /* helper processes use this exit status */
669 if(strncmp(w->msg, "libthread", 9) != 0){
670 p = emalloc(sizeof(Pid));
672 strncpy(p->msg, w->msg, sizeof(p->msg));
677 if(search(t, c->name, c->nname)){
678 textdelete(t, t->q0, t->q1, TRUE);
679 textsetselect(t, 0, 0);
682 warning(c->md, "%S: %s\n", c->name, w->msg);
683 flushimage(display, 1);
698 /* has this command already exited? */
700 for(p=pids; p!=nil; p=p->next){
701 if(p->pid == c->pid){
703 warning(c->md, "%s\n", p->msg);
707 lastp->next = p->next;
718 textinsert(t, 0, c->name, c->nname, TRUE);
719 textsetselect(t, 0, 0);
720 flushimage(display, 1);
728 xfidallocthread(void *v)
731 enum { Alloc, Free, N };
732 static Alt alts[N+1];
735 threadsetname("xfidallocthread");
736 alts[Alloc].c = cxfidalloc;
738 alts[Alloc].op = CHANRCV;
739 alts[Free].c = cxfidfree;
741 alts[Free].op = CHANRCV;
742 alts[N].op = CHANEND;
752 x = emalloc(sizeof(Xfid));
753 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
755 threadcreate(xfidctl, x->arg, STACK);
757 sendp(cxfidalloc, x);
767 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
769 newwindowthread(void *v)
774 threadsetname("newwindowthread");
777 /* only fsysproc is talking to us, so synchronization is trivial */
779 w = makenewwindow(nil);
781 sendp(cnewwindow, w);
786 rfget(int fix, int save, int setfont, char *name)
794 name = fontnames[fix];
798 for(i=0; i<nfontcache; i++)
799 if(strcmp(name, fontcache[i]->f->name) == 0){
803 f = openfont(display, name);
805 fprint(2, "can't open font file %s: %r\n", name);
806 warning(nil, "can't open font file %s: %r\n", name);
809 r = emalloc(sizeof(Reffont));
811 fontcache = realloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
812 fontcache[nfontcache++] = r;
818 rfclose(reffonts[fix]);
820 free(fontnames[fix]);
821 fontnames[fix] = name;
826 rfclose(reffonts[0]);
841 if(decref(&r->ref) == 0){
842 for(i=0; i<nfontcache; i++)
843 if(r == fontcache[i])
846 warning(nil, "internal error: can't find font in cache\n");
849 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
858 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
859 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
860 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
861 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
862 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
863 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
864 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
865 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
875 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
876 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
877 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
878 tagcols[TEXT] = display->black;
879 tagcols[HTEXT] = display->black;
882 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
883 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
884 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
885 textcols[TEXT] = display->black;
886 textcols[HTEXT] = display->black;
890 freeimage(modbutton);
891 freeimage(colbutton);
894 r = Rect(0, 0, Scrollwid+2, font->height+1);
895 button = allocimage(display, r, screen->chan, 0, DNofill);
896 draw(button, r, tagcols[BACK], nil, r.min);
898 border(button, r, 2, tagcols[BORD], ZP);
901 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
902 draw(modbutton, r, tagcols[BACK], nil, r.min);
904 border(modbutton, r, 2, tagcols[BORD], ZP);
906 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
907 draw(modbutton, r, tmp, nil, ZP);
911 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
913 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
914 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
918 * /dev/snarf updates when the file is closed, so we must open our own
919 * fd here rather than use snarffd
922 /* rio truncates larges snarf buffers, so this avoids using the
923 * service if the string is huge */
925 #define MAXSNARF 100*1024
936 if(snarfbuf.nc > MAXSNARF)
940 for(i=0; i<snarfbuf.nc; i+=n){
944 bufread(&snarfbuf, i, snarfrune, n);
945 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
958 int nb, nr, nulls, len;
962 if(s == nil || s[0]==0){
968 r = runemalloc(len+1);
969 cvttorunes(s, len, r, &nb, &nr, &nulls);
971 bufinsert(&snarfbuf, 0, r, nr);