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*);
30 int swapscrollbuttons = FALSE;
33 NSnarf = 1000 /* less than 1024, I/O buffer size */
35 Rune snarfrune[NSnarf+1];
39 "/lib/font/bit/lucidasans/euro.8.font",
40 "/lib/font/bit/lucm/unicode.9.font",
45 void shutdownthread(void*);
46 void acmeerrorinit(void);
47 void readfile(Column*, char*);
48 static int shutdown(void*, char*);
51 derror(Display *d, char *errorstr)
58 threadmain(int argc, char *argv[])
66 rfork(RFENVG|RFNAMEG);
76 {extern int _threaddebuglevel;
77 _threaddebuglevel = ~0;
81 globalautoindent = TRUE;
95 fontnames[0] = ARGF();
96 if(fontnames[0] == nil)
100 fontnames[1] = ARGF();
101 if(fontnames[1] == nil)
110 swapscrollbuttons = TRUE;
119 fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
120 threadexitsall("usage");
123 fontnames[0] = estrdup(fontnames[0]);
124 fontnames[1] = estrdup(fontnames[1]);
127 cputype = getenv("cputype");
128 objtype = getenv("objtype");
129 home = getenv("HOME");
130 p = getenv("tabstop");
132 maxtab = strtoul(p, nil, 0);
138 rowloadfonts(loadfile);
139 putenv("font", fontnames[0]);
140 snarffd = open("/dev/snarf", OREAD|OCEXEC);
143 sprint(buf, "/acme/bin/%s", cputype);
144 bind(buf, "/bin", MBEFORE);
146 bind("/acme/bin", "/bin", MBEFORE);
148 getwd(wdir, sizeof wdir);
151 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
152 fprint(2, "acme: can't open display: %r\n");
153 threadexitsall("geninitdraw");
156 if(initdraw(derror, fontnames[0], "acme") < 0){
157 fprint(2, "acme: can't open display: %r\n");
158 threadexitsall("initdraw");
162 font = d->defaultfont;
166 reffonts[0] = &reffont;
167 incref(&reffont.ref); /* one to hold up 'font' variable */
168 incref(&reffont.ref); /* one to hold up reffonts[0] */
169 fontcache = emalloc(sizeof(Reffont*));
171 fontcache[0] = &reffont;
177 cwait = threadwaitchan();
178 ccommand = chancreate(sizeof(Command**), 0);
179 chansetname(ccommand, "ccommand");
180 ckill = chancreate(sizeof(Rune*), 0);
181 chansetname(ckill, "ckill");
182 cxfidalloc = chancreate(sizeof(Xfid*), 0);
183 chansetname(cxfidalloc, "cxfidalloc");
184 cxfidfree = chancreate(sizeof(Xfid*), 0);
185 chansetname(cxfidfree, "cxfidfree");
186 cnewwindow = chancreate(sizeof(Channel*), 0);
187 chansetname(cnewwindow, "cnewwindow");
188 cerr = chancreate(sizeof(char*), 0);
189 chansetname(cerr, "cerr");
190 cedit = chancreate(sizeof(int), 0);
191 chansetname(cedit, "cedit");
192 cexit = chancreate(sizeof(int), 0);
193 chansetname(cexit, "cexit");
194 cwarn = chancreate(sizeof(void*), 1);
195 chansetname(cwarn, "cwarn");
196 if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){
197 fprint(2, "acme: can't create initial channels: %r\n");
198 threadexitsall("channels");
201 mousectl = initmouse(nil, screen);
203 fprint(2, "acme: can't initialize mouse: %r\n");
204 threadexitsall("mouse");
206 mouse = &mousectl->m;
207 keyboardctl = initkeyboard(nil);
208 if(keyboardctl == nil){
209 fprint(2, "acme: can't initialize keyboard: %r\n");
210 threadexitsall("keyboard");
215 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
217 fprint(2, "acme: can't initialize plumber: %r\n");
219 cplumb = chancreate(sizeof(Plumbmsg*), 0);
220 threadcreate(plumbproc, nil, STACK);
222 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
229 if(!loadfile || !rowload(&row, loadfile, TRUE)){
230 rowinit(&row, screen->clipr);
235 ncol = (argc+(WPERCOL-1))/WPERCOL;
242 for(i=0; i<ncol; i++){
243 c = rowadd(&row, nil, -1);
245 error("initializing columns");
247 c = row.col[row.ncol-1];
251 for(i=0; i<argc; i++){
252 p = utfrrune(argv[i], '/');
253 if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol)
254 readfile(c, argv[i]);
256 readfile(row.col[i/WPERCOL], argv[i]);
259 flushimage(display, 1);
262 threadcreate(keyboardthread, nil, STACK);
263 threadcreate(mousethread, nil, STACK);
264 threadcreate(waitthread, nil, STACK);
265 threadcreate(xfidallocthread, nil, STACK);
266 threadcreate(newwindowthread, nil, STACK);
267 /* threadcreate(shutdownthread, nil, STACK); */
268 threadnotify(shutdown, 1);
275 readfile(Column *c, char *s)
282 w = coladd(c, nil, nil, -1);
283 cvttorunes(s, strlen(s), rb, &nb, &nr, nil);
284 rs = cleanrname(runestr(rb, nr));
285 winsetname(w, rs.r, rs.nr);
286 textload(&w->body, 0, s, 1);
287 w->body.file->mod = FALSE;
290 textscrdraw(&w->body);
291 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
295 "sys: write on closed pipe",
313 shutdown(void *v, char *msg)
319 for(i=0; ignotes[i]; i++)
320 if(strncmp(ignotes[i], msg, strlen(ignotes[i])) == 0)
324 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
328 for(i=0; oknotes[i]; i++)
329 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
331 print("acme: %s\n", msg);
337 shutdownthread(void *v)
344 threadsetname("shutdown");
345 c = threadnotechan();
346 while((msg = recvp(c)) != nil)
358 // flushimage(display, 1);
360 for(c=command; c; c=c->next)
361 postnote(PNGROUP, c->pid, "hangup");
368 acmeerrorproc(void *v)
374 threadsetname("acmeerrorproc");
375 buf = emalloc(8192+1);
376 while((n=read(errorfd, buf, 8192)) >= 0){
378 sendp(cerr, estrdup(buf));
388 error("can't create pipe");
390 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
391 fd = create(acmeerrorfile, OWRITE, 0666);
393 remove(acmeerrorfile);
394 fd = create(acmeerrorfile, OWRITE, 0666);
396 error("can't create acmeerror file");
398 sprint(buf, "%d", pfd[0]);
399 write(fd, buf, strlen(buf));
401 /* reopen pfd[1] close on exec */
402 sprint(buf, "/fd/%d", pfd[1]);
403 errorfd = open(buf, OREAD|OCEXEC);
405 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
406 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
410 error("can't re-open acmeerror file");
411 proccreate(acmeerrorproc, nil, STACK);
421 threadsetname("plumbproc");
423 m = threadplumbrecv(plumbeditfd);
432 keyboardthread(void *v)
437 enum { KTimer, KKey, NKALT };
438 static Alt alts[NKALT+1];
441 alts[KTimer].c = nil;
442 alts[KTimer].v = nil;
443 alts[KTimer].op = CHANNOP;
444 alts[KKey].c = keyboardctl->c;
446 alts[KKey].op = CHANRCV;
447 alts[NKALT].op = CHANEND;
451 threadsetname("keyboardthread");
457 if(t!=nil && t->what==Tag){
461 flushimage(display, 1);
463 alts[KTimer].c = nil;
464 alts[KTimer].op = CHANNOP;
468 typetext = rowtype(&row, r, mouse->xy);
470 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
472 if(t!=nil && t->w!=nil)
473 t->w->body.file->curtext = &t->w->body;
476 if(t!=nil && t->what==Tag) {
477 timer = timerstart(500);
478 alts[KTimer].c = timer->c;
479 alts[KTimer].op = CHANRCV;
482 alts[KTimer].c = nil;
483 alts[KTimer].op = CHANNOP;
485 if(nbrecv(keyboardctl->c, &r) > 0)
487 flushimage(display, 1);
503 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
504 static Alt alts[NMALT+1];
507 threadsetname("mousethread");
508 alts[MResize].c = mousectl->resizec;
509 alts[MResize].v = nil;
510 alts[MResize].op = CHANRCV;
511 alts[MMouse].c = mousectl->c;
512 alts[MMouse].v = &mousectl->m;
513 alts[MMouse].op = CHANRCV;
514 alts[MPlumb].c = cplumb;
515 alts[MPlumb].v = ±
516 alts[MPlumb].op = CHANRCV;
517 alts[MWarnings].c = cwarn;
518 alts[MWarnings].v = nil;
519 alts[MWarnings].op = CHANRCV;
521 alts[MPlumb].op = CHANNOP;
522 alts[NMALT].op = CHANEND;
528 flushimage(display, 1);
531 if(getwindow(display, Refnone) < 0)
532 error("attach to window");
533 draw(screen, screen->r, display->white, nil, ZP);
535 rowresize(&row, screen->clipr);
538 if(strcmp(pm->type, "text") == 0){
539 act = plumblookup(pm->attr, "action");
540 if(act==nil || strcmp(act, "showfile")==0)
542 else if(strcmp(act, "showdata")==0)
551 * Make a copy so decisions are consistent; mousectl changes
552 * underfoot. Can't just receive into m because this introduces
553 * another race; see /sys/src/libdraw/mouse.c.
557 t = rowwhich(&row, m.xy);
558 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
559 winlock(mousetext->w, 'M');
561 wincommit(mousetext->w, mousetext);
562 winunlock(mousetext->w);
568 if(t==nil || m.buttons==0)
573 else if(m.buttons == 2)
575 else if(m.buttons == 4)
578 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
580 if(swapscrollbuttons){
593 /* scroll buttons, wheels, etc. */
594 if(t->what==Body && w != nil && (m.buttons & (8|16))){
598 but = Kscrollonedown;
605 if(ptinrect(m.xy, t->scrollr)){
607 if(t->what == Columntag)
608 rowdragcol(&row, t->col, but);
609 else if(t->what == Tag){
610 coldragwin(t->col, t->w, but);
612 barttext = &t->w->body;
634 activecol = t->col; /* button 1 only */
635 if(t->w!=nil && t==&t->w->body)
637 }else if(m.buttons & 2){
638 if(textselect2(t, &q0, &q1, &argt))
639 execute(t, q0, q1, FALSE, argt);
640 }else if(m.buttons & 4){
641 if(textselect3(t, &q0, &q1))
642 look3(t, q0, q1, FALSE);
656 * There is a race between process exiting and our finding out it was ever created.
657 * This structure keeps a list of processes that have exited we haven't heard of.
659 typedef struct Pid Pid;
677 Pid *pids, *p, *lastp;
678 enum { WErr, WKill, WWait, WCmd, NWALT };
682 threadsetname("waitthread");
686 alts[WErr].op = CHANRCV;
687 alts[WKill].c = ckill;
688 alts[WKill].v = &cmd;
689 alts[WKill].op = CHANRCV;
690 alts[WWait].c = cwait;
692 alts[WWait].op = CHANRCV;
693 alts[WCmd].c = ccommand;
695 alts[WCmd].op = CHANRCV;
696 alts[NWALT].op = CHANEND;
703 warning(nil, "%s", err);
705 flushimage(display, 1);
710 ncmd = runestrlen(cmd);
711 for(c=command; c; c=c->next){
713 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
714 if(postnote(PNGROUP, c->pid, "kill") < 0)
715 warning(nil, "kill %S: %r\n", cmd);
720 warning(nil, "Kill: no process %S\n", cmd);
726 for(c=command; c; c=c->next){
740 /* helper processes use this exit status */
741 if(strncmp(w->msg, "libthread", 9) != 0){
742 p = emalloc(sizeof(Pid));
744 strncpy(p->msg, w->msg, sizeof(p->msg));
749 if(search(t, c->name, c->nname)){
750 textdelete(t, t->q0, t->q1, TRUE);
751 textsetselect(t, 0, 0);
754 warning(c->md, "%.*S: exit %s\n", c->nname-1, c->name, w->msg);
755 flushimage(display, 1);
770 /* has this command already exited? */
772 for(p=pids; p!=nil; p=p->next){
773 if(p->pid == c->pid){
775 warning(c->md, "%s\n", p->msg);
779 lastp->next = p->next;
790 textinsert(t, 0, c->name, c->nname, TRUE);
791 textsetselect(t, 0, 0);
792 flushimage(display, 1);
800 xfidallocthread(void *v)
803 enum { Alloc, Free, N };
804 static Alt alts[N+1];
807 threadsetname("xfidallocthread");
808 alts[Alloc].c = cxfidalloc;
810 alts[Alloc].op = CHANRCV;
811 alts[Free].c = cxfidfree;
813 alts[Free].op = CHANRCV;
814 alts[N].op = CHANEND;
824 x = emalloc(sizeof(Xfid));
825 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
826 chansetname(x->c, "xc%p", x->c);
828 threadcreate(xfidctl, x->arg, STACK);
830 sendp(cxfidalloc, x);
840 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
842 newwindowthread(void *v)
847 threadsetname("newwindowthread");
850 /* only fsysproc is talking to us, so synchronization is trivial */
852 w = makenewwindow(nil);
854 sendp(cnewwindow, w);
859 rfget(int fix, int save, int setfont, char *name)
867 name = fontnames[fix];
871 for(i=0; i<nfontcache; i++)
872 if(strcmp(name, fontcache[i]->f->name) == 0){
876 f = openfont(display, name);
878 fprint(2, "can't open font file %s: %r\n", name);
879 warning(nil, "can't open font file %s: %r\n", name);
882 r = emalloc(sizeof(Reffont));
884 fontcache = erealloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
885 fontcache[nfontcache++] = r;
891 rfclose(reffonts[fix]);
893 if(fontnames[fix] != name){
894 free(fontnames[fix]);
895 fontnames[fix] = estrdup(name);
901 rfclose(reffonts[0]);
916 if(decref(&r->ref) == 0){
917 for(i=0; i<nfontcache; i++)
918 if(r == fontcache[i])
921 warning(nil, "internal error: can't find font in cache\n");
924 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
933 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
934 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
935 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
936 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
937 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
938 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
939 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
940 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
950 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
951 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
952 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
953 tagcols[TEXT] = display->black;
954 tagcols[HTEXT] = display->black;
957 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
958 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
959 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
960 textcols[TEXT] = display->black;
961 textcols[HTEXT] = display->black;
965 freeimage(modbutton);
966 freeimage(colbutton);
969 r = Rect(0, 0, Scrollwid+2, font->height+1);
970 button = allocimage(display, r, screen->chan, 0, DNofill);
971 draw(button, r, tagcols[BACK], nil, r.min);
973 border(button, r, 2, tagcols[BORD], ZP);
976 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
977 draw(modbutton, r, tagcols[BACK], nil, r.min);
979 border(modbutton, r, 2, tagcols[BORD], ZP);
981 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
982 draw(modbutton, r, tmp, nil, ZP);
986 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
988 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
989 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
993 * /dev/snarf updates when the file is closed, so we must open our own
994 * fd here rather than use snarffd
997 /* rio truncates larges snarf buffers, so this avoids using the
998 * service if the string is huge */
1000 #define MAXSNARF 100*1024
1011 if(snarfbuf.nc > MAXSNARF)
1015 for(i=0; i<snarfbuf.nc; i+=n){
1019 bufread(&snarfbuf, i, snarfrune, n);
1020 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
1023 s = fmtstrflush(&f);
1033 int nb, nr, nulls, len;
1037 if(s == nil || s[0]==0){
1043 r = runemalloc(len+1);
1044 cvttorunes(s, len, r, &nb, &nr, &nulls);
1045 bufreset(&snarfbuf);
1046 bufinsert(&snarfbuf, 0, r, nr);