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);
73 {extern int _threaddebuglevel;
74 _threaddebuglevel = ~0;
78 globalautoindent = TRUE;
92 fontnames[0] = ARGF();
93 if(fontnames[0] == nil)
97 fontnames[1] = ARGF();
98 if(fontnames[1] == nil)
107 swapscrollbuttons = TRUE;
116 fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
120 fontnames[0] = estrdup(fontnames[0]);
121 fontnames[1] = estrdup(fontnames[1]);
124 cputype = getenv("cputype");
125 objtype = getenv("objtype");
126 home = getenv("HOME");
127 p = getenv("tabstop");
129 maxtab = strtoul(p, nil, 0);
135 rowloadfonts(loadfile);
136 putenv("font", fontnames[0]);
137 snarffd = open("/dev/snarf", OREAD|OCEXEC);
140 sprint(buf, "/acme/bin/%s", cputype);
141 bind(buf, "/bin", MBEFORE);
143 bind("/acme/bin", "/bin", MBEFORE);
145 getwd(wdir, sizeof wdir);
148 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
149 fprint(2, "acme: can't open display: %r\n");
150 exits("geninitdraw");
153 if(initdraw(derror, fontnames[0], "acme") < 0){
154 fprint(2, "acme: can't open display: %r\n");
159 font = d->defaultfont;
163 reffonts[0] = &reffont;
164 incref(&reffont.ref); /* one to hold up 'font' variable */
165 incref(&reffont.ref); /* one to hold up reffonts[0] */
166 fontcache = emalloc(sizeof(Reffont*));
168 fontcache[0] = &reffont;
174 cwait = threadwaitchan();
175 ccommand = chancreate(sizeof(Command**), 0);
176 chansetname(ccommand, "ccommand");
177 ckill = chancreate(sizeof(Rune*), 0);
178 chansetname(ckill, "ckill");
179 cxfidalloc = chancreate(sizeof(Xfid*), 0);
180 chansetname(cxfidalloc, "cxfidalloc");
181 cxfidfree = chancreate(sizeof(Xfid*), 0);
182 chansetname(cxfidfree, "cxfidfree");
183 cnewwindow = chancreate(sizeof(Channel*), 0);
184 chansetname(cnewwindow, "cnewwindow");
185 cerr = chancreate(sizeof(char*), 0);
186 chansetname(cerr, "cerr");
187 cedit = chancreate(sizeof(int), 0);
188 chansetname(cedit, "cedit");
189 cexit = chancreate(sizeof(int), 0);
190 chansetname(cexit, "cexit");
191 cwarn = chancreate(sizeof(void*), 1);
192 chansetname(cwarn, "cwarn");
193 if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){
194 fprint(2, "acme: can't create initial channels: %r\n");
198 mousectl = initmouse(nil, screen);
200 fprint(2, "acme: can't initialize mouse: %r\n");
203 mouse = &mousectl->m;
204 keyboardctl = initkeyboard(nil);
205 if(keyboardctl == nil){
206 fprint(2, "acme: can't initialize keyboard: %r\n");
212 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
214 fprint(2, "acme: can't initialize plumber: %r\n");
216 cplumb = chancreate(sizeof(Plumbmsg*), 0);
217 threadcreate(plumbproc, nil, STACK);
219 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
226 if(!loadfile || !rowload(&row, loadfile, TRUE)){
227 rowinit(&row, screen->clipr);
232 ncol = (argc+(WPERCOL-1))/WPERCOL;
239 for(i=0; i<ncol; i++){
240 c = rowadd(&row, nil, -1);
242 error("initializing columns");
244 c = row.col[row.ncol-1];
248 for(i=0; i<argc; i++){
249 p = utfrrune(argv[i], '/');
250 if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol)
251 readfile(c, argv[i]);
253 readfile(row.col[i/WPERCOL], argv[i]);
256 flushimage(display, 1);
259 threadcreate(keyboardthread, nil, STACK);
260 threadcreate(mousethread, nil, STACK);
261 threadcreate(waitthread, nil, STACK);
262 threadcreate(xfidallocthread, nil, STACK);
263 threadcreate(newwindowthread, nil, STACK);
264 /* threadcreate(shutdownthread, nil, STACK); */
265 threadnotify(shutdown, 1);
272 readfile(Column *c, char *s)
279 w = coladd(c, nil, nil, -1);
280 cvttorunes(s, strlen(s), rb, &nb, &nr, nil);
281 rs = cleanrname(runestr(rb, nr));
282 winsetname(w, rs.r, rs.nr);
283 textload(&w->body, 0, s, 1);
284 w->body.file->mod = FALSE;
287 textscrdraw(&w->body);
288 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
302 shutdown(void *v, char *msg)
306 if(strcmp(msg, "sys: write on closed pipe") == 0)
311 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
315 for(i=0; oknotes[i]; i++)
316 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
318 print("acme: %s\n", msg);
325 shutdownthread(void *v)
332 c = threadnotechan();
333 while((msg = recvp(c)) != nil)
345 // flushimage(display, 1);
347 for(c=command; c; c=c->next)
348 postnote(PNGROUP, c->pid, "hangup");
355 acmeerrorproc(void *v)
361 threadsetname("acmeerrorproc");
362 buf = emalloc(8192+1);
363 while((n=read(errorfd, buf, 8192)) >= 0){
365 sendp(cerr, estrdup(buf));
375 error("can't create pipe");
377 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
378 fd = create(acmeerrorfile, OWRITE, 0666);
380 remove(acmeerrorfile);
381 fd = create(acmeerrorfile, OWRITE, 0666);
383 error("can't create acmeerror file");
385 sprint(buf, "%d", pfd[0]);
386 write(fd, buf, strlen(buf));
388 /* reopen pfd[1] close on exec */
389 sprint(buf, "/fd/%d", pfd[1]);
390 errorfd = open(buf, OREAD|OCEXEC);
392 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
393 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
397 error("can't re-open acmeerror file");
398 proccreate(acmeerrorproc, nil, STACK);
408 threadsetname("plumbproc");
410 m = threadplumbrecv(plumbeditfd);
419 keyboardthread(void *v)
424 enum { KTimer, KKey, NKALT };
425 static Alt alts[NKALT+1];
428 alts[KTimer].c = nil;
429 alts[KTimer].v = nil;
430 alts[KTimer].op = CHANNOP;
431 alts[KKey].c = keyboardctl->c;
433 alts[KKey].op = CHANRCV;
434 alts[NKALT].op = CHANEND;
438 threadsetname("keyboardthread");
444 if(t!=nil && t->what==Tag){
448 flushimage(display, 1);
450 alts[KTimer].c = nil;
451 alts[KTimer].op = CHANNOP;
455 typetext = rowtype(&row, r, mouse->xy);
457 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
459 if(t!=nil && t->w!=nil)
460 t->w->body.file->curtext = &t->w->body;
463 if(t!=nil && t->what==Tag) {
464 timer = timerstart(500);
465 alts[KTimer].c = timer->c;
466 alts[KTimer].op = CHANRCV;
469 alts[KTimer].c = nil;
470 alts[KTimer].op = CHANNOP;
472 if(nbrecv(keyboardctl->c, &r) > 0)
474 flushimage(display, 1);
490 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
491 static Alt alts[NMALT+1];
494 threadsetname("mousethread");
495 alts[MResize].c = mousectl->resizec;
496 alts[MResize].v = nil;
497 alts[MResize].op = CHANRCV;
498 alts[MMouse].c = mousectl->c;
499 alts[MMouse].v = &mousectl->m;
500 alts[MMouse].op = CHANRCV;
501 alts[MPlumb].c = cplumb;
502 alts[MPlumb].v = ±
503 alts[MPlumb].op = CHANRCV;
504 alts[MWarnings].c = cwarn;
505 alts[MWarnings].v = nil;
506 alts[MWarnings].op = CHANRCV;
508 alts[MPlumb].op = CHANNOP;
509 alts[NMALT].op = CHANEND;
515 flushimage(display, 1);
518 if(getwindow(display, Refnone) < 0)
519 error("attach to window");
520 draw(screen, screen->r, display->white, nil, ZP);
522 rowresize(&row, screen->clipr);
525 if(strcmp(pm->type, "text") == 0){
526 act = plumblookup(pm->attr, "action");
527 if(act==nil || strcmp(act, "showfile")==0)
529 else if(strcmp(act, "showdata")==0)
538 * Make a copy so decisions are consistent; mousectl changes
539 * underfoot. Can't just receive into m because this introduces
540 * another race; see /sys/src/libdraw/mouse.c.
544 t = rowwhich(&row, m.xy);
545 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
546 winlock(mousetext->w, 'M');
548 wincommit(mousetext->w, mousetext);
549 winunlock(mousetext->w);
555 if(t==nil || m.buttons==0)
560 else if(m.buttons == 2)
562 else if(m.buttons == 4)
565 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
567 if(swapscrollbuttons){
580 /* scroll buttons, wheels, etc. */
581 if(t->what==Body && w != nil && (m.buttons & (8|16))){
585 but = Kscrollonedown;
592 if(ptinrect(m.xy, t->scrollr)){
594 if(t->what == Columntag)
595 rowdragcol(&row, t->col, but);
596 else if(t->what == Tag){
597 coldragwin(t->col, t->w, but);
599 barttext = &t->w->body;
621 activecol = t->col; /* button 1 only */
622 if(t->w!=nil && t==&t->w->body)
624 }else if(m.buttons & 2){
625 if(textselect2(t, &q0, &q1, &argt))
626 execute(t, q0, q1, FALSE, argt);
627 }else if(m.buttons & 4){
628 if(textselect3(t, &q0, &q1))
629 look3(t, q0, q1, FALSE);
643 * There is a race between process exiting and our finding out it was ever created.
644 * This structure keeps a list of processes that have exited we haven't heard of.
646 typedef struct Pid Pid;
664 Pid *pids, *p, *lastp;
665 enum { WErr, WKill, WWait, WCmd, NWALT };
669 threadsetname("waitthread");
673 alts[WErr].op = CHANRCV;
674 alts[WKill].c = ckill;
675 alts[WKill].v = &cmd;
676 alts[WKill].op = CHANRCV;
677 alts[WWait].c = cwait;
679 alts[WWait].op = CHANRCV;
680 alts[WCmd].c = ccommand;
682 alts[WCmd].op = CHANRCV;
683 alts[NWALT].op = CHANEND;
690 warning(nil, "%s", err);
692 flushimage(display, 1);
697 ncmd = runestrlen(cmd);
698 for(c=command; c; c=c->next){
700 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
701 if(postnote(PNGROUP, c->pid, "kill") < 0)
702 warning(nil, "kill %S: %r\n", cmd);
707 warning(nil, "Kill: no process %S\n", cmd);
713 for(c=command; c; c=c->next){
727 /* helper processes use this exit status */
728 if(strncmp(w->msg, "libthread", 9) != 0){
729 p = emalloc(sizeof(Pid));
731 strncpy(p->msg, w->msg, sizeof(p->msg));
736 if(search(t, c->name, c->nname)){
737 textdelete(t, t->q0, t->q1, TRUE);
738 textsetselect(t, 0, 0);
741 warning(c->md, "%.*S: exit %s\n", c->nname-1, c->name, w->msg);
742 flushimage(display, 1);
757 /* has this command already exited? */
759 for(p=pids; p!=nil; p=p->next){
760 if(p->pid == c->pid){
762 warning(c->md, "%s\n", p->msg);
766 lastp->next = p->next;
777 textinsert(t, 0, c->name, c->nname, TRUE);
778 textsetselect(t, 0, 0);
779 flushimage(display, 1);
787 xfidallocthread(void *v)
790 enum { Alloc, Free, N };
791 static Alt alts[N+1];
794 threadsetname("xfidallocthread");
795 alts[Alloc].c = cxfidalloc;
797 alts[Alloc].op = CHANRCV;
798 alts[Free].c = cxfidfree;
800 alts[Free].op = CHANRCV;
801 alts[N].op = CHANEND;
811 x = emalloc(sizeof(Xfid));
812 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
813 chansetname(x->c, "xc%p", x->c);
815 threadcreate(xfidctl, x->arg, STACK);
817 sendp(cxfidalloc, x);
827 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
829 newwindowthread(void *v)
834 threadsetname("newwindowthread");
837 /* only fsysproc is talking to us, so synchronization is trivial */
839 w = makenewwindow(nil);
841 sendp(cnewwindow, w);
846 rfget(int fix, int save, int setfont, char *name)
854 name = fontnames[fix];
858 for(i=0; i<nfontcache; i++)
859 if(strcmp(name, fontcache[i]->f->name) == 0){
863 f = openfont(display, name);
865 fprint(2, "can't open font file %s: %r\n", name);
866 warning(nil, "can't open font file %s: %r\n", name);
869 r = emalloc(sizeof(Reffont));
871 fontcache = erealloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
872 fontcache[nfontcache++] = r;
878 rfclose(reffonts[fix]);
880 if(fontnames[fix] != name){
881 free(fontnames[fix]);
882 fontnames[fix] = estrdup(name);
888 rfclose(reffonts[0]);
903 if(decref(&r->ref) == 0){
904 for(i=0; i<nfontcache; i++)
905 if(r == fontcache[i])
908 warning(nil, "internal error: can't find font in cache\n");
911 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
920 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
921 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
922 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
923 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
924 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
925 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
926 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
927 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
937 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
938 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
939 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
940 tagcols[TEXT] = display->black;
941 tagcols[HTEXT] = display->black;
944 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
945 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
946 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
947 textcols[TEXT] = display->black;
948 textcols[HTEXT] = display->black;
952 freeimage(modbutton);
953 freeimage(colbutton);
956 r = Rect(0, 0, Scrollwid+2, font->height+1);
957 button = allocimage(display, r, screen->chan, 0, DNofill);
958 draw(button, r, tagcols[BACK], nil, r.min);
960 border(button, r, 2, tagcols[BORD], ZP);
963 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
964 draw(modbutton, r, tagcols[BACK], nil, r.min);
966 border(modbutton, r, 2, tagcols[BORD], ZP);
968 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
969 draw(modbutton, r, tmp, nil, ZP);
973 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
975 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
976 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
980 * /dev/snarf updates when the file is closed, so we must open our own
981 * fd here rather than use snarffd
984 /* rio truncates larges snarf buffers, so this avoids using the
985 * service if the string is huge */
987 #define MAXSNARF 100*1024
998 if(snarfbuf.nc > MAXSNARF)
1002 for(i=0; i<snarfbuf.nc; i+=n){
1006 bufread(&snarfbuf, i, snarfrune, n);
1007 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
1010 s = fmtstrflush(&f);
1020 int nb, nr, nulls, len;
1024 if(s == nil || s[0]==0){
1030 r = runemalloc(len+1);
1031 cvttorunes(s, len, r, &nb, &nr, &nulls);
1032 bufreset(&snarfbuf);
1033 bufinsert(&snarfbuf, 0, r, nr);