Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include <fcall.h>
10 #include <plumb.h>
11 #include "dat.h"
12 #include "fns.h"
13 /* for generating syms in mkfile only: */
14 #include <bio.h>
15 #include "edit.h"
17 void mousethread(void*);
18 void keyboardthread(void*);
19 void waitthread(void*);
20 void xfidallocthread(void*);
21 void newwindowthread(void*);
22 void plumbproc(void*);
24 Reffont **fontcache;
25 int nfontcache;
26 char wdir[512] = ".";
27 Reffont *reffonts[2];
28 int snarffd = -1;
29 int mainpid;
30 int swapscrollbuttons = FALSE;
32 enum{
33 NSnarf = 1000 /* less than 1024, I/O buffer size */
34 };
35 Rune snarfrune[NSnarf+1];
37 char *fontnames[2] =
38 {
39 "/lib/font/bit/lucsans/euro.8.font",
40 "/lib/font/bit/lucm/unicode.9.font"
41 };
43 Command *command;
45 void shutdownthread(void*);
46 void acmeerrorinit(void);
47 void readfile(Column*, char*);
48 static int shutdown(void*, char*);
50 void
51 derror(Display *d, char *errorstr)
52 {
53 USED(d);
54 error(errorstr);
55 }
57 void
58 threadmain(int argc, char *argv[])
59 {
60 int i;
61 char *p, *loadfile;
62 Column *c;
63 int ncol;
64 Display *d;
66 rfork(RFENVG|RFNAMEG);
68 ncol = -1;
70 loadfile = nil;
71 ARGBEGIN{
72 case '$':
73 dodollarsigns = TRUE;
74 break;
75 case 'D':
76 {extern int _threaddebuglevel;
77 _threaddebuglevel = ~0;
78 }
79 break;
80 case 'a':
81 globalautoindent = TRUE;
82 break;
83 case 'b':
84 bartflag = TRUE;
85 break;
86 case 'c':
87 p = ARGF();
88 if(p == nil)
89 goto Usage;
90 ncol = atoi(p);
91 if(ncol <= 0)
92 goto Usage;
93 break;
94 case 'f':
95 fontnames[0] = ARGF();
96 if(fontnames[0] == nil)
97 goto Usage;
98 break;
99 case 'F':
100 fontnames[1] = ARGF();
101 if(fontnames[1] == nil)
102 goto Usage;
103 break;
104 case 'l':
105 loadfile = ARGF();
106 if(loadfile == nil)
107 goto Usage;
108 break;
109 case 'r':
110 swapscrollbuttons = TRUE;
111 break;
112 case 'W':
113 winsize = ARGF();
114 if(winsize == nil)
115 goto Usage;
116 break;
117 default:
118 Usage:
119 fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n");
120 threadexitsall("usage");
121 }ARGEND
123 fontnames[0] = estrdup(fontnames[0]);
124 fontnames[1] = estrdup(fontnames[1]);
126 quotefmtinstall();
127 cputype = getenv("cputype");
128 objtype = getenv("objtype");
129 home = getenv("HOME");
130 p = getenv("tabstop");
131 if(p != nil){
132 maxtab = strtoul(p, nil, 0);
133 free(p);
135 if(maxtab == 0)
136 maxtab = 4;
137 if(loadfile)
138 rowloadfonts(loadfile);
139 putenv("font", fontnames[0]);
140 snarffd = open("/dev/snarf", OREAD|OCEXEC);
141 /*
142 if(cputype){
143 sprint(buf, "/acme/bin/%s", cputype);
144 bind(buf, "/bin", MBEFORE);
146 bind("/acme/bin", "/bin", MBEFORE);
147 */
148 getwd(wdir, sizeof wdir);
150 /*
151 if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){
152 fprint(2, "acme: can't open display: %r\n");
153 threadexitsall("geninitdraw");
155 */
156 if(initdraw(derror, fontnames[0], "acme") < 0){
157 fprint(2, "acme: can't open display: %r\n");
158 threadexitsall("initdraw");
161 d = display;
162 font = d->defaultfont;
163 /*assert(font); */
165 reffont.f = font;
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*));
170 nfontcache = 1;
171 fontcache[0] = &reffont;
173 iconinit();
174 timerinit();
175 rxinit();
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);
202 if(mousectl == nil){
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");
212 mainpid = getpid();
213 startplumbing();
214 /*
215 plumbeditfd = plumbopen("edit", OREAD|OCEXEC);
216 if(plumbeditfd < 0)
217 fprint(2, "acme: can't initialize plumber: %r\n");
218 else{
219 cplumb = chancreate(sizeof(Plumbmsg*), 0);
220 threadcreate(plumbproc, nil, STACK);
222 plumbsendfd = plumbopen("send", OWRITE|OCEXEC);
223 */
225 fsysinit();
227 #define WPERCOL 8
228 disk = diskinit();
229 if(!loadfile || !rowload(&row, loadfile, TRUE)){
230 rowinit(&row, screen->clipr);
231 if(ncol < 0){
232 if(argc == 0)
233 ncol = 2;
234 else{
235 ncol = (argc+(WPERCOL-1))/WPERCOL;
236 if(ncol < 2)
237 ncol = 2;
240 if(ncol == 0)
241 ncol = 2;
242 for(i=0; i<ncol; i++){
243 c = rowadd(&row, nil, -1);
244 if(c==nil && i==0)
245 error("initializing columns");
247 c = row.col[row.ncol-1];
248 if(argc == 0)
249 readfile(c, wdir);
250 else
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]);
255 else
256 readfile(row.col[i/WPERCOL], argv[i]);
259 flushimage(display, 1);
261 acmeerrorinit();
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);
269 recvul(cexit);
270 killprocs();
271 threadexitsall(nil);
274 void
275 readfile(Column *c, char *s)
277 Window *w;
278 Rune rb[256];
279 int nr;
280 Runestr rs;
282 w = coladd(c, nil, nil, -1);
283 if(s[0] != '/')
284 runesnprint(rb, sizeof rb, "%s/%s", wdir, s);
285 else
286 runesnprint(rb, sizeof rb, "%s", s);
287 nr = runestrlen(rb);
288 rs = cleanrname(runestr(rb, nr));
289 winsetname(w, rs.r, rs.nr);
290 textload(&w->body, 0, s, 1);
291 w->body.file->mod = FALSE;
292 w->dirty = FALSE;
293 winsettag(w);
294 winresize(w, w->r, FALSE, TRUE);
295 textscrdraw(&w->body);
296 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
299 char *ignotes[] = {
300 "sys: write on closed pipe",
301 "sys: ttin",
302 "sys: ttou",
303 "sys: tstp",
304 nil
305 };
307 char *oknotes[] ={
308 "delete",
309 "hangup",
310 "kill",
311 "exit",
312 nil
313 };
315 int dumping;
317 static int
318 shutdown(void *v, char *msg)
320 int i;
322 USED(v);
324 for(i=0; ignotes[i]; i++)
325 if(strncmp(ignotes[i], msg, strlen(ignotes[i])) == 0)
326 return 1;
328 killprocs();
329 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
330 dumping = TRUE;
331 rowdump(&row, nil);
333 for(i=0; oknotes[i]; i++)
334 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
335 threadexitsall(msg);
336 print("acme: %s\n", msg);
337 return 0;
340 /*
341 void
342 shutdownthread(void *v)
344 char *msg;
345 Channel *c;
347 USED(v);
349 threadsetname("shutdown");
350 c = threadnotechan();
351 while((msg = recvp(c)) != nil)
352 shutdown(nil, msg);
354 */
356 void
357 killprocs(void)
359 Command *c;
361 fsysclose();
362 /* if(display) */
363 /* flushimage(display, 1); */
365 for(c=command; c; c=c->next)
366 postnote(PNGROUP, c->pid, "hangup");
369 static int errorfd;
370 int erroutfd;
372 void
373 acmeerrorproc(void *v)
375 char *buf;
376 int n;
378 USED(v);
379 threadsetname("acmeerrorproc");
380 buf = emalloc(8192+1);
381 while((n=read(errorfd, buf, 8192)) >= 0){
382 buf[n] = '\0';
383 sendp(cerr, estrdup(buf));
387 void
388 acmeerrorinit(void)
390 int pfd[2];
392 if(pipe(pfd) < 0)
393 error("can't create pipe");
394 #if 0
395 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
396 fd = create(acmeerrorfile, OWRITE, 0666);
397 if(fd < 0){
398 remove(acmeerrorfile);
399 fd = create(acmeerrorfile, OWRITE, 0666);
400 if(fd < 0)
401 error("can't create acmeerror file");
403 sprint(buf, "%d", pfd[0]);
404 write(fd, buf, strlen(buf));
405 close(fd);
406 /* reopen pfd[1] close on exec */
407 sprint(buf, "/fd/%d", pfd[1]);
408 errorfd = open(buf, OREAD|OCEXEC);
409 #endif
410 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
411 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
412 erroutfd = pfd[0];
413 errorfd = pfd[1];
414 if(errorfd < 0)
415 error("can't re-open acmeerror file");
416 proccreate(acmeerrorproc, nil, STACK);
419 /*
420 void
421 plumbproc(void *v)
423 Plumbmsg *m;
425 USED(v);
426 threadsetname("plumbproc");
427 for(;;){
428 m = threadplumbrecv(plumbeditfd);
429 if(m == nil)
430 threadexits(nil);
431 sendp(cplumb, m);
434 */
436 void
437 keyboardthread(void *v)
439 Rune r;
440 Timer *timer;
441 Text *t;
442 enum { KTimer, KKey, NKALT };
443 static Alt alts[NKALT+1];
445 USED(v);
446 alts[KTimer].c = nil;
447 alts[KTimer].v = nil;
448 alts[KTimer].op = CHANNOP;
449 alts[KKey].c = keyboardctl->c;
450 alts[KKey].v = &r;
451 alts[KKey].op = CHANRCV;
452 alts[NKALT].op = CHANEND;
454 timer = nil;
455 typetext = nil;
456 threadsetname("keyboardthread");
457 for(;;){
458 switch(alt(alts)){
459 case KTimer:
460 timerstop(timer);
461 t = typetext;
462 if(t!=nil && t->what==Tag){
463 winlock(t->w, 'K');
464 wincommit(t->w, t);
465 winunlock(t->w);
466 flushimage(display, 1);
468 alts[KTimer].c = nil;
469 alts[KTimer].op = CHANNOP;
470 break;
471 case KKey:
472 casekeyboard:
473 typetext = rowtype(&row, r, mouse->xy);
474 t = typetext;
475 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
476 activecol = t->col;
477 if(t!=nil && t->w!=nil)
478 t->w->body.file->curtext = &t->w->body;
479 if(timer != nil)
480 timercancel(timer);
481 if(t!=nil && t->what==Tag) {
482 timer = timerstart(500);
483 alts[KTimer].c = timer->c;
484 alts[KTimer].op = CHANRCV;
485 }else{
486 timer = nil;
487 alts[KTimer].c = nil;
488 alts[KTimer].op = CHANNOP;
490 if(nbrecv(keyboardctl->c, &r) > 0)
491 goto casekeyboard;
492 flushimage(display, 1);
493 break;
498 void
499 mousethread(void *v)
501 Text *t, *argt;
502 int but;
503 uint q0, q1;
504 Window *w;
505 Plumbmsg *pm;
506 Mouse m;
507 char *act;
508 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
509 static Alt alts[NMALT+1];
511 USED(v);
512 threadsetname("mousethread");
513 alts[MResize].c = mousectl->resizec;
514 alts[MResize].v = nil;
515 alts[MResize].op = CHANRCV;
516 alts[MMouse].c = mousectl->c;
517 alts[MMouse].v = &mousectl->m;
518 alts[MMouse].op = CHANRCV;
519 alts[MPlumb].c = cplumb;
520 alts[MPlumb].v = &pm;
521 alts[MPlumb].op = CHANRCV;
522 alts[MWarnings].c = cwarn;
523 alts[MWarnings].v = nil;
524 alts[MWarnings].op = CHANRCV;
525 if(cplumb == nil)
526 alts[MPlumb].op = CHANNOP;
527 alts[NMALT].op = CHANEND;
529 for(;;){
530 qlock(&row.lk);
531 flushwarnings();
532 qunlock(&row.lk);
533 flushimage(display, 1);
534 switch(alt(alts)){
535 case MResize:
536 if(getwindow(display, Refnone) < 0)
537 error("attach to window");
538 draw(screen, screen->r, display->white, nil, ZP);
539 scrlresize();
540 rowresize(&row, screen->clipr);
541 break;
542 case MPlumb:
543 if(strcmp(pm->type, "text") == 0){
544 act = plumblookup(pm->attr, "action");
545 if(act==nil || strcmp(act, "showfile")==0)
546 plumblook(pm);
547 else if(strcmp(act, "showdata")==0)
548 plumbshow(pm);
550 plumbfree(pm);
551 break;
552 case MWarnings:
553 break;
554 case MMouse:
555 /*
556 * Make a copy so decisions are consistent; mousectl changes
557 * underfoot. Can't just receive into m because this introduces
558 * another race; see /sys/src/libdraw/mouse.c.
559 */
560 m = mousectl->m;
561 qlock(&row.lk);
562 t = rowwhich(&row, m.xy);
563 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
564 winlock(mousetext->w, 'M');
565 mousetext->eq0 = ~0;
566 wincommit(mousetext->w, mousetext);
567 winunlock(mousetext->w);
569 mousetext = t;
570 if(t == nil)
571 goto Continue;
572 w = t->w;
573 if(t==nil || m.buttons==0)
574 goto Continue;
575 but = 0;
576 if(m.buttons == 1)
577 but = 1;
578 else if(m.buttons == 2)
579 but = 2;
580 else if(m.buttons == 4)
581 but = 3;
582 barttext = t;
583 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
584 if(but){
585 if(swapscrollbuttons){
586 if(but == 1)
587 but = 3;
588 else if(but == 3)
589 but = 1;
591 winlock(w, 'M');
592 t->eq0 = ~0;
593 textscroll(t, but);
594 winunlock(w);
596 goto Continue;
598 /* scroll buttons, wheels, etc. */
599 /*
600 * TAG used to require t->what==Body but now allow
601 * scroll wheel in tag too.
602 */
603 if(w != nil && (m.buttons & (8|16))){
604 if(m.buttons & 8)
605 but = Kscrolloneup;
606 else
607 but = Kscrollonedown;
608 winlock(w, 'M');
609 t->eq0 = ~0;
610 texttype(t, but);
611 winunlock(w);
612 goto Continue;
614 if(ptinrect(m.xy, t->scrollr)){
615 if(but){
616 if(t->what == Columntag)
617 rowdragcol(&row, t->col, but);
618 else if(t->what == Tag){
619 coldragwin(t->col, t->w, but);
620 if(t->w)
621 barttext = &t->w->body;
623 if(t->col)
624 activecol = t->col;
626 goto Continue;
628 if(m.buttons){
629 if(w)
630 winlock(w, 'M');
631 t->eq0 = ~0;
632 if(w)
633 wincommit(w, t);
634 else
635 textcommit(t, TRUE);
636 if(m.buttons & 1){
637 textselect(t);
638 if(w)
639 winsettag(w);
640 argtext = t;
641 seltext = t;
642 if(t->col)
643 activecol = t->col; /* button 1 only */
644 if(t->w!=nil && t==&t->w->body)
645 activewin = t->w;
646 }else if(m.buttons & 2){
647 if(textselect2(t, &q0, &q1, &argt))
648 execute(t, q0, q1, FALSE, argt);
649 }else if(m.buttons & 4){
650 if(textselect3(t, &q0, &q1))
651 look3(t, q0, q1, FALSE);
653 if(w)
654 winunlock(w);
655 goto Continue;
657 Continue:
658 qunlock(&row.lk);
659 break;
664 /*
665 * There is a race between process exiting and our finding out it was ever created.
666 * This structure keeps a list of processes that have exited we haven't heard of.
667 */
668 typedef struct Pid Pid;
669 struct Pid
671 int pid;
672 char msg[ERRMAX];
673 Pid *next;
674 };
676 void
677 waitthread(void *v)
679 Waitmsg *w;
680 Command *c, *lc;
681 uint pid;
682 int found, ncmd;
683 Rune *cmd;
684 char *err;
685 Text *t;
686 Pid *pids, *p, *lastp;
687 enum { WErr, WKill, WWait, WCmd, NWALT };
688 Alt alts[NWALT+1];
690 USED(v);
691 threadsetname("waitthread");
692 pids = nil;
693 alts[WErr].c = cerr;
694 alts[WErr].v = &err;
695 alts[WErr].op = CHANRCV;
696 alts[WKill].c = ckill;
697 alts[WKill].v = &cmd;
698 alts[WKill].op = CHANRCV;
699 alts[WWait].c = cwait;
700 alts[WWait].v = &w;
701 alts[WWait].op = CHANRCV;
702 alts[WCmd].c = ccommand;
703 alts[WCmd].v = &c;
704 alts[WCmd].op = CHANRCV;
705 alts[NWALT].op = CHANEND;
707 command = nil;
708 for(;;){
709 switch(alt(alts)){
710 case WErr:
711 qlock(&row.lk);
712 warning(nil, "%s", err);
713 free(err);
714 flushimage(display, 1);
715 qunlock(&row.lk);
716 break;
717 case WKill:
718 found = FALSE;
719 ncmd = runestrlen(cmd);
720 for(c=command; c; c=c->next){
721 /* -1 for blank */
722 if(runeeq(c->name, c->nname-1, cmd, ncmd) == TRUE){
723 if(postnote(PNGROUP, c->pid, "kill") < 0)
724 warning(nil, "kill %S: %r\n", cmd);
725 found = TRUE;
728 if(!found)
729 warning(nil, "Kill: no process %S\n", cmd);
730 free(cmd);
731 break;
732 case WWait:
733 pid = w->pid;
734 lc = nil;
735 for(c=command; c; c=c->next){
736 if(c->pid == pid){
737 if(lc)
738 lc->next = c->next;
739 else
740 command = c->next;
741 break;
743 lc = c;
745 qlock(&row.lk);
746 t = &row.tag;
747 textcommit(t, TRUE);
748 if(c == nil){
749 /* helper processes use this exit status */
750 if(strncmp(w->msg, "libthread", 9) != 0){
751 p = emalloc(sizeof(Pid));
752 p->pid = pid;
753 strncpy(p->msg, w->msg, sizeof(p->msg));
754 p->next = pids;
755 pids = p;
757 }else{
758 if(search(t, c->name, c->nname)){
759 textdelete(t, t->q0, t->q1, TRUE);
760 textsetselect(t, 0, 0);
762 if(w->msg[0])
763 warning(c->md, "%.*S: exit %s\n", c->nname-1, c->name, w->msg);
764 flushimage(display, 1);
766 qunlock(&row.lk);
767 free(w);
768 Freecmd:
769 if(c){
770 if(c->iseditcmd)
771 sendul(cedit, 0);
772 free(c->text);
773 free(c->name);
774 fsysdelid(c->md);
775 free(c);
777 break;
778 case WCmd:
779 /* has this command already exited? */
780 lastp = nil;
781 for(p=pids; p!=nil; p=p->next){
782 if(p->pid == c->pid){
783 if(p->msg[0])
784 warning(c->md, "%s\n", p->msg);
785 if(lastp == nil)
786 pids = p->next;
787 else
788 lastp->next = p->next;
789 free(p);
790 goto Freecmd;
792 lastp = p;
794 c->next = command;
795 command = c;
796 qlock(&row.lk);
797 t = &row.tag;
798 textcommit(t, TRUE);
799 textinsert(t, 0, c->name, c->nname, TRUE);
800 textsetselect(t, 0, 0);
801 flushimage(display, 1);
802 qunlock(&row.lk);
803 break;
808 void
809 xfidallocthread(void *v)
811 Xfid *xfree, *x;
812 enum { Alloc, Free, N };
813 static Alt alts[N+1];
815 USED(v);
816 threadsetname("xfidallocthread");
817 alts[Alloc].c = cxfidalloc;
818 alts[Alloc].v = nil;
819 alts[Alloc].op = CHANRCV;
820 alts[Free].c = cxfidfree;
821 alts[Free].v = &x;
822 alts[Free].op = CHANRCV;
823 alts[N].op = CHANEND;
825 xfree = nil;
826 for(;;){
827 switch(alt(alts)){
828 case Alloc:
829 x = xfree;
830 if(x)
831 xfree = x->next;
832 else{
833 x = emalloc(sizeof(Xfid));
834 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
835 chansetname(x->c, "xc%p", x->c);
836 x->arg = x;
837 threadcreate(xfidctl, x->arg, STACK);
839 sendp(cxfidalloc, x);
840 break;
841 case Free:
842 x->next = xfree;
843 xfree = x;
844 break;
849 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
850 void
851 newwindowthread(void *v)
853 Window *w;
855 USED(v);
856 threadsetname("newwindowthread");
858 for(;;){
859 /* only fsysproc is talking to us, so synchronization is trivial */
860 recvp(cnewwindow);
861 w = makenewwindow(nil);
862 winsettag(w);
863 sendp(cnewwindow, w);
867 Reffont*
868 rfget(int fix, int save, int setfont, char *name)
870 Reffont *r;
871 Font *f;
872 int i;
874 r = nil;
875 if(name == nil){
876 name = fontnames[fix];
877 r = reffonts[fix];
879 if(r == nil){
880 for(i=0; i<nfontcache; i++)
881 if(strcmp(name, fontcache[i]->f->name) == 0){
882 r = fontcache[i];
883 goto Found;
885 f = openfont(display, name);
886 if(f == nil){
887 fprint(2, "can't open font file %s: %r\n", name);
888 warning(nil, "can't open font file %s: %r\n", name);
889 return nil;
891 r = emalloc(sizeof(Reffont));
892 r->f = f;
893 fontcache = erealloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
894 fontcache[nfontcache++] = r;
896 Found:
897 if(save){
898 incref(&r->ref);
899 if(reffonts[fix])
900 rfclose(reffonts[fix]);
901 reffonts[fix] = r;
902 if(fontnames[fix] != name){
903 free(fontnames[fix]);
904 fontnames[fix] = estrdup(name);
907 if(setfont){
908 reffont.f = r->f;
909 incref(&r->ref);
910 rfclose(reffonts[0]);
911 font = r->f;
912 reffonts[0] = r;
913 incref(&r->ref);
914 iconinit();
916 incref(&r->ref);
917 return r;
920 void
921 rfclose(Reffont *r)
923 int i;
925 if(decref(&r->ref) == 0){
926 for(i=0; i<nfontcache; i++)
927 if(r == fontcache[i])
928 break;
929 if(i >= nfontcache)
930 warning(nil, "internal error: can't find font in cache\n");
931 else{
932 nfontcache--;
933 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
935 freefont(r->f);
936 free(r);
940 Cursor boxcursor = {
941 {-7, -7},
942 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
943 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
944 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
945 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
946 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
947 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
948 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
949 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
950 };
952 void
953 iconinit(void)
955 Rectangle r;
956 Image *tmp;
958 /* Blue */
959 tagcols[BACK] = allocimagemix(display, DPalebluegreen, DWhite);
960 tagcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
961 tagcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
962 tagcols[TEXT] = display->black;
963 tagcols[HTEXT] = display->black;
965 /* Yellow */
966 textcols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
967 textcols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
968 textcols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DYellowgreen);
969 textcols[TEXT] = display->black;
970 textcols[HTEXT] = display->black;
972 if(button){
973 freeimage(button);
974 freeimage(modbutton);
975 freeimage(colbutton);
978 r = Rect(0, 0, Scrollwid+2, font->height+1);
979 button = allocimage(display, r, screen->chan, 0, DNofill);
980 draw(button, r, tagcols[BACK], nil, r.min);
981 r.max.x -= 2;
982 border(button, r, 2, tagcols[BORD], ZP);
984 r = button->r;
985 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
986 draw(modbutton, r, tagcols[BACK], nil, r.min);
987 r.max.x -= 2;
988 border(modbutton, r, 2, tagcols[BORD], ZP);
989 r = insetrect(r, 2);
990 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
991 draw(modbutton, r, tmp, nil, ZP);
992 freeimage(tmp);
994 r = button->r;
995 colbutton = allocimage(display, r, screen->chan, 0, DPurpleblue);
997 but2col = allocimage(display, r, screen->chan, 1, 0xAA0000FF);
998 but3col = allocimage(display, r, screen->chan, 1, 0x006600FF);
1002 * /dev/snarf updates when the file is closed, so we must open our own
1003 * fd here rather than use snarffd
1006 /* rio truncates larges snarf buffers, so this avoids using the
1007 * service if the string is huge */
1009 #define MAXSNARF 100*1024
1011 void
1012 acmeputsnarf(void)
1014 int i, n;
1015 Fmt f;
1016 char *s;
1018 if(snarfbuf.nc==0)
1019 return;
1020 if(snarfbuf.nc > MAXSNARF)
1021 return;
1023 fmtstrinit(&f);
1024 for(i=0; i<snarfbuf.nc; i+=n){
1025 n = snarfbuf.nc-i;
1026 if(n >= NSnarf)
1027 n = NSnarf;
1028 bufread(&snarfbuf, i, snarfrune, n);
1029 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
1030 break;
1032 s = fmtstrflush(&f);
1033 if(s && s[0])
1034 putsnarf(s);
1035 free(s);
1038 void
1039 acmegetsnarf(void)
1041 char *s;
1042 int nb, nr, nulls, len;
1043 Rune *r;
1045 s = getsnarf();
1046 if(s == nil || s[0]==0){
1047 free(s);
1048 return;
1051 len = strlen(s);
1052 r = runemalloc(len+1);
1053 cvttorunes(s, len, r, &nb, &nr, &nulls);
1054 bufreset(&snarfbuf);
1055 bufinsert(&snarfbuf, 0, r, nr);
1056 free(r);
1057 free(s);