10 static char deffontname[] = "*default*";
13 int debuglockdisplay = 1;
31 geninitdraw(char *devdir, void(*error)(Display*, char*), char *fontname, char *label, char *windir, int ref)
38 display = _initdisplay(error, label);
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");
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, "imageinit: can't open default font: %r\n");
71 font = openfont(display, fontname); /* BUG: grey fonts */
73 fprint(2, "imageinit: can't open font %s: %r\n", fontname);
77 display->defaultfont = font;
79 _screen = allocscreen(display->image, display->white, 0);
80 display->screenimage = display->image; /* _allocwindow wants screenimage->chan */
81 screen = _allocwindow(nil, _screen, display->image->r, Refnone, DWhite);
83 fprint(2, "_allocwindow: %r\n");
86 display->screenimage = screen;
87 draw(screen, screen->r, display->white, nil, ZP);
88 flushimage(display, 1);
91 * I don't see any reason to go away gracefully,
92 * and if some other proc exits holding the display
93 * lock, this atexit call never finishes.
95 * atexit(drawshutdown);
101 initdraw(void (*error)(Display*, char*), char *fontname, char *label)
103 return geninitdraw("/dev", error, fontname, label, "/dev", Refnone);
106 extern int _freeimage1(Image*);
109 getimage0(Display *d, Image *image)
116 * If there's an old screen, it has id 0. The 'J' request below
117 * will try to install the new screen as id 0, so the old one
118 * must be freed first.
122 memset(image, 0, sizeof(Image));
128 if(flushimage(d, 0) < 0){
129 fprint(2, "cannot read screen info: %r\n");
133 n = _displayrddraw(d, info, sizeof info);
135 fprint(2, "short screen info\n");
140 image = mallocz(sizeof(Image), 1);
142 fprint(2, "cannot allocate image: %r\n");
149 image->chan = strtochan(info+2*12);
150 image->depth = chantodepth(image->chan);
151 image->repl = atoi(info+3*12);
152 image->r.min.x = atoi(info+4*12);
153 image->r.min.y = atoi(info+5*12);
154 image->r.max.x = atoi(info+6*12);
155 image->r.max.y = atoi(info+7*12);
156 image->clipr.min.x = atoi(info+8*12);
157 image->clipr.min.y = atoi(info+9*12);
158 image->clipr.max.x = atoi(info+10*12);
159 image->clipr.max.y = atoi(info+11*12);
164 * Attach, or possibly reattach, to window.
165 * If reattaching, maintain value of screen pointer.
168 getwindow(Display *d, int ref)
172 /* XXX check for destroyed? */
175 * Libdraw promises not to change the value of "screen",
176 * so we have to reuse the image structure
177 * memory we already have.
180 i = getimage0(d, oi);
182 sysfatal("getwindow failed");
184 /* fprint(2, "getwindow %p -> %p\n", oi, i); */
187 _screen = allocscreen(i, d->white, 0);
189 screen = _allocwindow(screen, _screen, i->r, ref, DWhite);
190 d->screenimage = screen;
195 _initdisplay(void (*error)(Display*, char*), char *label)
200 fmtinstall('P', Pfmt);
201 fmtinstall('R', Rfmt);
203 disp = mallocz(sizeof(Display), 1);
216 disp->bufsize = 65500;
217 disp->buf = malloc(disp->bufsize+5); /* +5 for flush message */
218 disp->bufp = disp->buf;
230 if(_displaymux(disp) < 0
231 || _displayconnect(disp) < 0
232 || _displayinit(disp, label, winsize) < 0)
240 image = getimage0(disp, nil);
245 disp->white = allocimage(disp, Rect(0, 0, 1, 1), GREY1, 1, DWhite);
246 disp->black = allocimage(disp, Rect(0, 0, 1, 1), GREY1, 1, DBlack);
247 if(disp->white == nil || disp->black == nil){
253 disp->opaque = disp->white;
254 disp->transparent = disp->black;
260 * Call with d unlocked.
261 * Note that disp->defaultfont and defaultsubfont are not freed here.
264 closedisplay(Display *disp)
273 if(disp->oldlabel[0]){
274 snprint(buf, sizeof buf, "%s/label", disp->windir);
275 fd = open(buf, OWRITE);
277 write(fd, disp->oldlabel, strlen(disp->oldlabel));
285 freeimage(disp->white);
287 freeimage(disp->black);
294 lockdisplay(Display *disp)
296 if(debuglockdisplay){
297 /* avoid busy looping; it's rare we collide anyway */
298 while(!canqlock(&disp->qlock)){
299 fprint(1, "proc %d waiting for display lock...\n", getpid());
307 unlockdisplay(Display *disp)
309 qunlock(&disp->qlock);
313 drawerror(Display *d, char *s)
320 errstr(err, sizeof err);
321 fprint(2, "draw: %s: %s\n", s, err);
336 if(_displaywrdraw(d, d->buf, n) != n){
338 fprint(2, "flushimage fail: d=%p: %r\n", d); /**/
339 d->bufp = d->buf; /* might as well; chance of continuing */
347 flushimage(Display *d, int visible)
350 *d->bufp++ = 'v'; /* five bytes always reserved for this */
351 if(d->_isnewdisplay){
352 BPLONG(d->bufp, d->screenimage->id);
360 bufimage(Display *d, int n)
364 if(n<0 || d == nil || n>d->bufsize){
366 werrstr("bad count in bufimage");
369 if(d->bufp+n > d->buf+d->bufsize)