10 static char deffontname[] = "*default*";
13 int debuglockdisplay = 1;
30 initdraw(void (*error)(Display*, char*), char *fontname, char *label)
35 display = _initdisplay(error, label); /* sets screen too */
40 display->screenimage = display->image;
45 df = getdefont(display);
46 display->defaultsubfont = df;
48 fprint(2, "imageinit: can't open default subfont: %r\n");
50 closedisplay(display);
55 fontname = getenv("font"); /* leak */
58 * Build fonts with caches==depth of screen, for speed.
59 * If conversion were faster, we'd use 0 and save memory.
62 snprint(buf, sizeof buf, "%d %d\n0 %d\t%s\n", df->height, df->ascent,
63 df->n-1, deffontname);
64 //BUG: Need something better for this installsubfont("*default*", df);
65 font = buildfont(display, buf, deffontname);
67 fprint(2, "initdraw: can't open default font: %r\n");
71 font = openfont(display, fontname); /* BUG: grey fonts */
73 fprint(2, "initdraw: can't open font %s: %r\n", fontname);
77 display->defaultfont = font;
79 display->white = allocimage(display, Rect(0,0,1,1), GREY1, 1, DWhite);
80 display->black = allocimage(display, Rect(0,0,1,1), GREY1, 1, DBlack);
81 if(display->white == nil || display->black == nil){
82 fprint(2, "initdraw: can't allocate white and black");
85 display->opaque = display->white;
86 display->transparent = display->black;
88 _screen = allocscreen(display->image, display->white, 0);
89 screen = _allocwindow(nil, _screen, display->image->r, Refnone, DWhite);
90 display->screenimage = screen;
91 draw(screen, screen->r, display->white, nil, ZP);
92 flushimage(display, 1);
95 * I don't see any reason to go away gracefully,
96 * and if some other proc exits holding the display
97 * lock, this atexit call never finishes.
99 * atexit(drawshutdown);
105 * Call with d unlocked.
106 * Note that disp->defaultfont and defaultsubfont are not freed here.
109 closedisplay(Display *disp)
118 if(disp->oldlabel[0]){
119 snprint(buf, sizeof buf, "%s/label", disp->windir);
120 fd = open(buf, OWRITE);
122 write(fd, disp->oldlabel, strlen(disp->oldlabel));
130 freeimage(disp->white);
132 freeimage(disp->black);
137 lockdisplay(Display *disp)
139 if(debuglockdisplay){
140 /* avoid busy looping; it's rare we collide anyway */
141 while(!canqlock(&disp->qlock)){
142 fprint(1, "proc %d waiting for display lock...\n", getpid());
150 unlockdisplay(Display *disp)
152 qunlock(&disp->qlock);
156 drawerror(Display *d, char *s)
163 errstr(err, sizeof err);
164 fprint(2, "draw: %s: %s\n", s, err);
179 if(_drawmsgwrite(d, d->buf, n) != n){
181 fprint(2, "flushimage fail: d=%p: %r\n", d); /**/
182 d->bufp = d->buf; /* might as well; chance of continuing */
190 flushimage(Display *d, int visible)
193 *d->bufp++ = 'v'; /* five bytes always reserved for this */
194 if(d->_isnewdisplay){
195 BPLONG(d->bufp, d->screenimage->id);
203 bufimage(Display *d, int n)
207 if(n<0 || d == nil || n>d->bufsize){
209 werrstr("bad count in bufimage");
212 if(d->bufp+n > d->buf+d->bufsize)