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/lucidasans/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 nb, nr;
280 Runestr rs;
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;
288 w->dirty = FALSE;
289 winsettag(w);
290 textscrdraw(&w->body);
291 textsetselect(&w->tag, w->tag.file->b.nc, w->tag.file->b.nc);
294 char *ignotes[] = {
295 "sys: write on closed pipe",
296 "sys: ttin",
297 "sys: ttou",
298 "sys: tstp",
299 nil
300 };
302 char *oknotes[] ={
303 "delete",
304 "hangup",
305 "kill",
306 "exit",
307 nil
308 };
310 int dumping;
312 static int
313 shutdown(void *v, char *msg)
315 int i;
317 USED(v);
319 for(i=0; ignotes[i]; i++)
320 if(strncmp(ignotes[i], msg, strlen(ignotes[i])) == 0)
321 return 1;
323 killprocs();
324 if(!dumping && strcmp(msg, "kill")!=0 && strcmp(msg, "exit")!=0 && getpid()==mainpid){
325 dumping = TRUE;
326 rowdump(&row, nil);
328 for(i=0; oknotes[i]; i++)
329 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0)
330 threadexitsall(msg);
331 print("acme: %s\n", msg);
332 return 0;
335 /*
336 void
337 shutdownthread(void *v)
339 char *msg;
340 Channel *c;
342 USED(v);
344 threadsetname("shutdown");
345 c = threadnotechan();
346 while((msg = recvp(c)) != nil)
347 shutdown(nil, msg);
349 */
351 void
352 killprocs(void)
354 Command *c;
356 fsysclose();
357 // if(display)
358 // flushimage(display, 1);
360 for(c=command; c; c=c->next)
361 postnote(PNGROUP, c->pid, "hangup");
364 static int errorfd;
365 int erroutfd;
367 void
368 acmeerrorproc(void *v)
370 char *buf;
371 int n;
373 USED(v);
374 threadsetname("acmeerrorproc");
375 buf = emalloc(8192+1);
376 while((n=read(errorfd, buf, 8192)) >= 0){
377 buf[n] = '\0';
378 sendp(cerr, estrdup(buf));
382 void
383 acmeerrorinit(void)
385 int pfd[2];
387 if(pipe(pfd) < 0)
388 error("can't create pipe");
389 #if 0
390 sprint(acmeerrorfile, "/srv/acme.%s.%d", getuser(), mainpid);
391 fd = create(acmeerrorfile, OWRITE, 0666);
392 if(fd < 0){
393 remove(acmeerrorfile);
394 fd = create(acmeerrorfile, OWRITE, 0666);
395 if(fd < 0)
396 error("can't create acmeerror file");
398 sprint(buf, "%d", pfd[0]);
399 write(fd, buf, strlen(buf));
400 close(fd);
401 /* reopen pfd[1] close on exec */
402 sprint(buf, "/fd/%d", pfd[1]);
403 errorfd = open(buf, OREAD|OCEXEC);
404 #endif
405 fcntl(pfd[0], F_SETFD, FD_CLOEXEC);
406 fcntl(pfd[1], F_SETFD, FD_CLOEXEC);
407 erroutfd = pfd[0];
408 errorfd = pfd[1];
409 if(errorfd < 0)
410 error("can't re-open acmeerror file");
411 proccreate(acmeerrorproc, nil, STACK);
414 /*
415 void
416 plumbproc(void *v)
418 Plumbmsg *m;
420 USED(v);
421 threadsetname("plumbproc");
422 for(;;){
423 m = threadplumbrecv(plumbeditfd);
424 if(m == nil)
425 threadexits(nil);
426 sendp(cplumb, m);
429 */
431 void
432 keyboardthread(void *v)
434 Rune r;
435 Timer *timer;
436 Text *t;
437 enum { KTimer, KKey, NKALT };
438 static Alt alts[NKALT+1];
440 USED(v);
441 alts[KTimer].c = nil;
442 alts[KTimer].v = nil;
443 alts[KTimer].op = CHANNOP;
444 alts[KKey].c = keyboardctl->c;
445 alts[KKey].v = &r;
446 alts[KKey].op = CHANRCV;
447 alts[NKALT].op = CHANEND;
449 timer = nil;
450 typetext = nil;
451 threadsetname("keyboardthread");
452 for(;;){
453 switch(alt(alts)){
454 case KTimer:
455 timerstop(timer);
456 t = typetext;
457 if(t!=nil && t->what==Tag){
458 winlock(t->w, 'K');
459 wincommit(t->w, t);
460 winunlock(t->w);
461 flushimage(display, 1);
463 alts[KTimer].c = nil;
464 alts[KTimer].op = CHANNOP;
465 break;
466 case KKey:
467 casekeyboard:
468 typetext = rowtype(&row, r, mouse->xy);
469 t = typetext;
470 if(t!=nil && t->col!=nil && !(r==Kdown || r==Kleft || r==Kright)) /* scrolling doesn't change activecol */
471 activecol = t->col;
472 if(t!=nil && t->w!=nil)
473 t->w->body.file->curtext = &t->w->body;
474 if(timer != nil)
475 timercancel(timer);
476 if(t!=nil && t->what==Tag) {
477 timer = timerstart(500);
478 alts[KTimer].c = timer->c;
479 alts[KTimer].op = CHANRCV;
480 }else{
481 timer = nil;
482 alts[KTimer].c = nil;
483 alts[KTimer].op = CHANNOP;
485 if(nbrecv(keyboardctl->c, &r) > 0)
486 goto casekeyboard;
487 flushimage(display, 1);
488 break;
493 void
494 mousethread(void *v)
496 Text *t, *argt;
497 int but;
498 uint q0, q1;
499 Window *w;
500 Plumbmsg *pm;
501 Mouse m;
502 char *act;
503 enum { MResize, MMouse, MPlumb, MWarnings, NMALT };
504 static Alt alts[NMALT+1];
506 USED(v);
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 = &pm;
516 alts[MPlumb].op = CHANRCV;
517 alts[MWarnings].c = cwarn;
518 alts[MWarnings].v = nil;
519 alts[MWarnings].op = CHANRCV;
520 if(cplumb == nil)
521 alts[MPlumb].op = CHANNOP;
522 alts[NMALT].op = CHANEND;
524 for(;;){
525 qlock(&row.lk);
526 flushwarnings();
527 qunlock(&row.lk);
528 flushimage(display, 1);
529 switch(alt(alts)){
530 case MResize:
531 if(getwindow(display, Refnone) < 0)
532 error("attach to window");
533 draw(screen, screen->r, display->white, nil, ZP);
534 scrlresize();
535 rowresize(&row, screen->clipr);
536 break;
537 case MPlumb:
538 if(strcmp(pm->type, "text") == 0){
539 act = plumblookup(pm->attr, "action");
540 if(act==nil || strcmp(act, "showfile")==0)
541 plumblook(pm);
542 else if(strcmp(act, "showdata")==0)
543 plumbshow(pm);
545 plumbfree(pm);
546 break;
547 case MWarnings:
548 break;
549 case MMouse:
550 /*
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.
554 */
555 m = mousectl->m;
556 qlock(&row.lk);
557 t = rowwhich(&row, m.xy);
558 if(t!=mousetext && mousetext!=nil && mousetext->w!=nil){
559 winlock(mousetext->w, 'M');
560 mousetext->eq0 = ~0;
561 wincommit(mousetext->w, mousetext);
562 winunlock(mousetext->w);
564 mousetext = t;
565 if(t == nil)
566 goto Continue;
567 w = t->w;
568 if(t==nil || m.buttons==0)
569 goto Continue;
570 but = 0;
571 if(m.buttons == 1)
572 but = 1;
573 else if(m.buttons == 2)
574 but = 2;
575 else if(m.buttons == 4)
576 but = 3;
577 barttext = t;
578 if(t->what==Body && ptinrect(m.xy, t->scrollr)){
579 if(but){
580 if(swapscrollbuttons){
581 if(but == 1)
582 but = 3;
583 else if(but == 3)
584 but = 1;
586 winlock(w, 'M');
587 t->eq0 = ~0;
588 textscroll(t, but);
589 winunlock(w);
591 goto Continue;
593 /* scroll buttons, wheels, etc. */
594 if(t->what==Body && w != nil && (m.buttons & (8|16))){
595 if(m.buttons & 8)
596 but = Kscrolloneup;
597 else
598 but = Kscrollonedown;
599 winlock(w, 'M');
600 t->eq0 = ~0;
601 texttype(t, but);
602 winunlock(w);
603 goto Continue;
605 if(ptinrect(m.xy, t->scrollr)){
606 if(but){
607 if(t->what == Columntag)
608 rowdragcol(&row, t->col, but);
609 else if(t->what == Tag){
610 coldragwin(t->col, t->w, but);
611 if(t->w)
612 barttext = &t->w->body;
614 if(t->col)
615 activecol = t->col;
617 goto Continue;
619 if(m.buttons){
620 if(w)
621 winlock(w, 'M');
622 t->eq0 = ~0;
623 if(w)
624 wincommit(w, t);
625 else
626 textcommit(t, TRUE);
627 if(m.buttons & 1){
628 textselect(t);
629 if(w)
630 winsettag(w);
631 argtext = t;
632 seltext = t;
633 if(t->col)
634 activecol = t->col; /* button 1 only */
635 if(t->w!=nil && t==&t->w->body)
636 activewin = t->w;
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);
644 if(w)
645 winunlock(w);
646 goto Continue;
648 Continue:
649 qunlock(&row.lk);
650 break;
655 /*
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.
658 */
659 typedef struct Pid Pid;
660 struct Pid
662 int pid;
663 char msg[ERRMAX];
664 Pid *next;
665 };
667 void
668 waitthread(void *v)
670 Waitmsg *w;
671 Command *c, *lc;
672 uint pid;
673 int found, ncmd;
674 Rune *cmd;
675 char *err;
676 Text *t;
677 Pid *pids, *p, *lastp;
678 enum { WErr, WKill, WWait, WCmd, NWALT };
679 Alt alts[NWALT+1];
681 USED(v);
682 threadsetname("waitthread");
683 pids = nil;
684 alts[WErr].c = cerr;
685 alts[WErr].v = &err;
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;
691 alts[WWait].v = &w;
692 alts[WWait].op = CHANRCV;
693 alts[WCmd].c = ccommand;
694 alts[WCmd].v = &c;
695 alts[WCmd].op = CHANRCV;
696 alts[NWALT].op = CHANEND;
698 command = nil;
699 for(;;){
700 switch(alt(alts)){
701 case WErr:
702 qlock(&row.lk);
703 warning(nil, "%s", err);
704 free(err);
705 flushimage(display, 1);
706 qunlock(&row.lk);
707 break;
708 case WKill:
709 found = FALSE;
710 ncmd = runestrlen(cmd);
711 for(c=command; c; c=c->next){
712 /* -1 for blank */
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);
716 found = TRUE;
719 if(!found)
720 warning(nil, "Kill: no process %S\n", cmd);
721 free(cmd);
722 break;
723 case WWait:
724 pid = w->pid;
725 lc = nil;
726 for(c=command; c; c=c->next){
727 if(c->pid == pid){
728 if(lc)
729 lc->next = c->next;
730 else
731 command = c->next;
732 break;
734 lc = c;
736 qlock(&row.lk);
737 t = &row.tag;
738 textcommit(t, TRUE);
739 if(c == nil){
740 /* helper processes use this exit status */
741 if(strncmp(w->msg, "libthread", 9) != 0){
742 p = emalloc(sizeof(Pid));
743 p->pid = pid;
744 strncpy(p->msg, w->msg, sizeof(p->msg));
745 p->next = pids;
746 pids = p;
748 }else{
749 if(search(t, c->name, c->nname)){
750 textdelete(t, t->q0, t->q1, TRUE);
751 textsetselect(t, 0, 0);
753 if(w->msg[0])
754 warning(c->md, "%.*S: exit %s\n", c->nname-1, c->name, w->msg);
755 flushimage(display, 1);
757 qunlock(&row.lk);
758 free(w);
759 Freecmd:
760 if(c){
761 if(c->iseditcmd)
762 sendul(cedit, 0);
763 free(c->text);
764 free(c->name);
765 fsysdelid(c->md);
766 free(c);
768 break;
769 case WCmd:
770 /* has this command already exited? */
771 lastp = nil;
772 for(p=pids; p!=nil; p=p->next){
773 if(p->pid == c->pid){
774 if(p->msg[0])
775 warning(c->md, "%s\n", p->msg);
776 if(lastp == nil)
777 pids = p->next;
778 else
779 lastp->next = p->next;
780 free(p);
781 goto Freecmd;
783 lastp = p;
785 c->next = command;
786 command = c;
787 qlock(&row.lk);
788 t = &row.tag;
789 textcommit(t, TRUE);
790 textinsert(t, 0, c->name, c->nname, TRUE);
791 textsetselect(t, 0, 0);
792 flushimage(display, 1);
793 qunlock(&row.lk);
794 break;
799 void
800 xfidallocthread(void *v)
802 Xfid *xfree, *x;
803 enum { Alloc, Free, N };
804 static Alt alts[N+1];
806 USED(v);
807 threadsetname("xfidallocthread");
808 alts[Alloc].c = cxfidalloc;
809 alts[Alloc].v = nil;
810 alts[Alloc].op = CHANRCV;
811 alts[Free].c = cxfidfree;
812 alts[Free].v = &x;
813 alts[Free].op = CHANRCV;
814 alts[N].op = CHANEND;
816 xfree = nil;
817 for(;;){
818 switch(alt(alts)){
819 case Alloc:
820 x = xfree;
821 if(x)
822 xfree = x->next;
823 else{
824 x = emalloc(sizeof(Xfid));
825 x->c = chancreate(sizeof(void(*)(Xfid*)), 0);
826 chansetname(x->c, "xc%p", x->c);
827 x->arg = x;
828 threadcreate(xfidctl, x->arg, STACK);
830 sendp(cxfidalloc, x);
831 break;
832 case Free:
833 x->next = xfree;
834 xfree = x;
835 break;
840 /* this thread, in the main proc, allows fsysproc to get a window made without doing graphics */
841 void
842 newwindowthread(void *v)
844 Window *w;
846 USED(v);
847 threadsetname("newwindowthread");
849 for(;;){
850 /* only fsysproc is talking to us, so synchronization is trivial */
851 recvp(cnewwindow);
852 w = makenewwindow(nil);
853 winsettag(w);
854 sendp(cnewwindow, w);
858 Reffont*
859 rfget(int fix, int save, int setfont, char *name)
861 Reffont *r;
862 Font *f;
863 int i;
865 r = nil;
866 if(name == nil){
867 name = fontnames[fix];
868 r = reffonts[fix];
870 if(r == nil){
871 for(i=0; i<nfontcache; i++)
872 if(strcmp(name, fontcache[i]->f->name) == 0){
873 r = fontcache[i];
874 goto Found;
876 f = openfont(display, name);
877 if(f == nil){
878 fprint(2, "can't open font file %s: %r\n", name);
879 warning(nil, "can't open font file %s: %r\n", name);
880 return nil;
882 r = emalloc(sizeof(Reffont));
883 r->f = f;
884 fontcache = erealloc(fontcache, (nfontcache+1)*sizeof(Reffont*));
885 fontcache[nfontcache++] = r;
887 Found:
888 if(save){
889 incref(&r->ref);
890 if(reffonts[fix])
891 rfclose(reffonts[fix]);
892 reffonts[fix] = r;
893 if(fontnames[fix] != name){
894 free(fontnames[fix]);
895 fontnames[fix] = estrdup(name);
898 if(setfont){
899 reffont.f = r->f;
900 incref(&r->ref);
901 rfclose(reffonts[0]);
902 font = r->f;
903 reffonts[0] = r;
904 incref(&r->ref);
905 iconinit();
907 incref(&r->ref);
908 return r;
911 void
912 rfclose(Reffont *r)
914 int i;
916 if(decref(&r->ref) == 0){
917 for(i=0; i<nfontcache; i++)
918 if(r == fontcache[i])
919 break;
920 if(i >= nfontcache)
921 warning(nil, "internal error: can't find font in cache\n");
922 else{
923 nfontcache--;
924 memmove(fontcache+i, fontcache+i+1, (nfontcache-i)*sizeof(Reffont*));
926 freefont(r->f);
927 free(r);
931 Cursor boxcursor = {
932 {-7, -7},
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}
941 };
943 void
944 iconinit(void)
946 Rectangle r;
947 Image *tmp;
949 /* Blue */
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;
956 /* Yellow */
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;
963 if(button){
964 freeimage(button);
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);
972 r.max.x -= 2;
973 border(button, r, 2, tagcols[BORD], ZP);
975 r = button->r;
976 modbutton = allocimage(display, r, screen->chan, 0, DNofill);
977 draw(modbutton, r, tagcols[BACK], nil, r.min);
978 r.max.x -= 2;
979 border(modbutton, r, 2, tagcols[BORD], ZP);
980 r = insetrect(r, 2);
981 tmp = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DMedblue);
982 draw(modbutton, r, tmp, nil, ZP);
983 freeimage(tmp);
985 r = button->r;
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);
992 /*
993 * /dev/snarf updates when the file is closed, so we must open our own
994 * fd here rather than use snarffd
995 */
997 /* rio truncates larges snarf buffers, so this avoids using the
998 * service if the string is huge */
1000 #define MAXSNARF 100*1024
1002 void
1003 acmeputsnarf(void)
1005 int i, n;
1006 Fmt f;
1007 char *s;
1009 if(snarfbuf.nc==0)
1010 return;
1011 if(snarfbuf.nc > MAXSNARF)
1012 return;
1014 fmtstrinit(&f);
1015 for(i=0; i<snarfbuf.nc; i+=n){
1016 n = snarfbuf.nc-i;
1017 if(n >= NSnarf)
1018 n = NSnarf;
1019 bufread(&snarfbuf, i, snarfrune, n);
1020 if(fmtprint(&f, "%.*S", n, snarfrune) < 0)
1021 break;
1023 s = fmtstrflush(&f);
1024 if(s && s[0])
1025 putsnarf(s);
1026 free(s);
1029 void
1030 acmegetsnarf(void)
1032 char *s;
1033 int nb, nr, nulls, len;
1034 Rune *r;
1036 s = getsnarf();
1037 if(s == nil || s[0]==0){
1038 free(s);
1039 return;
1042 len = strlen(s);
1043 r = runemalloc(len+1);
1044 cvttorunes(s, len, r, &nb, &nr, &nulls);
1045 bufreset(&snarfbuf);
1046 bufinsert(&snarfbuf, 0, r, nr);
1047 free(r);
1048 free(s);