11 static char deffontname[] = "*default*";
14 int debuglockdisplay = 1;
17 int visibleclicks = 0;
42 geninitdraw(char *devdir, void(*error)(Display*, char*), char *fontname, char *label, char *windir, int ref)
49 display = _initdisplay(error, label);
56 df = getdefont(display);
57 display->defaultsubfont = df;
59 fprint(2, "imageinit: can't open default subfont: %r\n");
61 closedisplay(display);
66 fontname = getenv("font");
69 * Build fonts with caches==depth of screen, for speed.
70 * If conversion were faster, we'd use 0 and save memory.
73 snprint(buf, sizeof buf, "%d %d\n0 %d\t%s\n", df->height, df->ascent,
74 df->n-1, deffontname);
75 //BUG: Need something better for this installsubfont("*default*", df);
76 font = buildfont(display, buf, deffontname);
78 fprint(2, "imageinit: can't open default font: %r\n");
82 font = openfont(display, fontname); /* BUG: grey fonts */
84 fprint(2, "imageinit: can't open font %s: %r\n", fontname);
88 display->defaultfont = font;
90 _screen = allocscreen(display->image, display->white, 0);
91 display->screenimage = display->image; /* _allocwindow wants screenimage->chan */
92 screen = _allocwindow(nil, _screen, display->image->r, Refnone, DWhite);
94 fprint(2, "_allocwindow: %r\n");
97 display->screenimage = screen;
98 draw(screen, screen->r, display->white, nil, ZP);
99 flushimage(display, 1);
101 p = getenv("visibleclicks");
102 visibleclicks = p != nil && *p == '1';
106 f = display->defaultfont;
107 mousebuttons = allocimage(display, Rect(0,0,64,22), screen->chan, 0, DWhite);
108 border(mousebuttons, mousebuttons->r, 1, display->black, ZP);
109 border(mousebuttons, Rect(0, 0, 22, 22), 1, display->black, ZP);
110 border(mousebuttons, Rect(42, 0, 64, 22), 1, display->black, ZP);
111 string(mousebuttons, Pt(10-stringwidth(display->defaultfont, "1")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "1");
112 string(mousebuttons, Pt(21+10-stringwidth(display->defaultfont, "2")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "2");
113 string(mousebuttons, Pt(42+10-stringwidth(display->defaultfont, "3")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "3");
114 mousesave = allocimage(display, Rect(0,0,64,22), screen->chan, 0, 0);
118 * I don't see any reason to go away gracefully,
119 * and if some other proc exits holding the display
120 * lock, this atexit call never finishes.
122 * atexit(drawshutdown);
128 initdraw(void (*error)(Display*, char*), char *fontname, char *label)
130 return geninitdraw("/dev", error, fontname, label, "/dev", Refnone);
133 extern int _freeimage1(Image*);
136 getimage0(Display *d, Image *image)
143 * If there's an old screen, it has id 0. The 'J' request below
144 * will try to install the new screen as id 0, so the old one
145 * must be freed first.
149 memset(image, 0, sizeof(Image));
155 if(flushimage(d, 0) < 0){
156 fprint(2, "cannot read screen info: %r\n");
160 n = _displayrddraw(d, info, sizeof info);
162 fprint(2, "short screen info\n");
167 image = mallocz(sizeof(Image), 1);
169 fprint(2, "cannot allocate image: %r\n");
176 image->chan = strtochan(info+2*12);
177 image->depth = chantodepth(image->chan);
178 image->repl = atoi(info+3*12);
179 image->r.min.x = atoi(info+4*12);
180 image->r.min.y = atoi(info+5*12);
181 image->r.max.x = atoi(info+6*12);
182 image->r.max.y = atoi(info+7*12);
183 image->clipr.min.x = atoi(info+8*12);
184 image->clipr.min.y = atoi(info+9*12);
185 image->clipr.max.x = atoi(info+10*12);
186 image->clipr.max.y = atoi(info+11*12);
193 if(flushimage(d, 0) >= 0 && _displayrddraw(d, info, 12) == 12)
200 * Attach, or possibly reattach, to window.
201 * If reattaching, maintain value of screen pointer.
204 getwindow(Display *d, int ref)
209 /* XXX check for destroyed? */
212 * Libdraw promises not to change the value of "screen",
213 * so we have to reuse the image structure
214 * memory we already have.
217 i = getimage0(d, oi);
219 sysfatal("getwindow failed");
221 /* fprint(2, "getwindow %p -> %p\n", oi, i); */
224 _screen = allocscreen(i, d->white, 0);
226 screen = _allocwindow(screen, _screen, i->r, ref, DWhite);
227 d->screenimage = screen;
230 if(d->dpi >= DefaultDPI*3/2) {
231 for(f=d->firstfont; f != nil; f=f->next)
234 for(f=d->firstfont; f != nil; f=f->next)
235 if(f->lodpi != nil && f->lodpi != f)
236 swapfont(f, &f->hidpi, &f->lodpi);
243 _initdisplay(void (*error)(Display*, char*), char *label)
248 fmtinstall('P', Pfmt);
249 fmtinstall('R', Rfmt);
251 disp = mallocz(sizeof(Display), 1);
264 disp->bufsize = 65500;
265 disp->buf = malloc(disp->bufsize+5); /* +5 for flush message */
266 disp->bufp = disp->buf;
278 if(_displaymux(disp) < 0
279 || _displayconnect(disp) < 0
280 || _displayinit(disp, label, winsize) < 0)
288 image = getimage0(disp, nil);
293 disp->white = allocimage(disp, Rect(0, 0, 1, 1), GREY1, 1, DWhite);
294 disp->black = allocimage(disp, Rect(0, 0, 1, 1), GREY1, 1, DBlack);
295 if(disp->white == nil || disp->black == nil){
301 disp->opaque = disp->white;
302 disp->transparent = disp->black;
308 * Call with d unlocked.
309 * Note that disp->defaultfont and defaultsubfont are not freed here.
312 closedisplay(Display *disp)
321 if(disp->oldlabel[0]){
322 snprint(buf, sizeof buf, "%s/label", disp->windir);
323 fd = open(buf, OWRITE);
325 write(fd, disp->oldlabel, strlen(disp->oldlabel));
333 freeimage(disp->white);
335 freeimage(disp->black);
342 lockdisplay(Display *disp)
344 if(debuglockdisplay){
345 /* avoid busy looping; it's rare we collide anyway */
346 while(!canqlock(&disp->qlock)){
347 fprint(1, "proc %d waiting for display lock...\n", getpid());
355 unlockdisplay(Display *disp)
357 qunlock(&disp->qlock);
361 drawerror(Display *d, char *s)
368 errstr(err, sizeof err);
369 fprint(2, "draw: %s: %s\n", s, err);
384 if(_displaywrdraw(d, d->buf, n) != n){
386 fprint(2, "flushimage fail: d=%p: %r\n", d); /**/
387 d->bufp = d->buf; /* might as well; chance of continuing */
395 flushimage(Display *d, int visible)
397 if(visible == 1 && visibleclicks && mousebuttons && _drawmouse.buttons) {
402 r = rectaddpt(r, _drawmouse.xy);
403 r = rectaddpt(r, Pt(-Dx(mousebuttons->r)/2, -Dy(mousebuttons->r)-3));
404 drawop(mousesave, mousesave->r, screen, nil, r.min, S);
406 r1 = rectaddpt(Rect(0, 0, 22, 22), r.min);
407 if(_drawmouse.buttons & 1)
408 drawop(screen, r1, mousebuttons, nil, ZP, S);
409 r1 = rectaddpt(r1, Pt(21, 0));
410 if(_drawmouse.buttons & 2)
411 drawop(screen, r1, mousebuttons, nil, Pt(21, 0), S);
412 r1 = rectaddpt(r1, Pt(21, 0));
413 if(_drawmouse.buttons & 4)
414 drawop(screen, r1, mousebuttons, nil, Pt(42, 0), S);
415 ret = flushimage(d, 2);
416 drawop(screen, r, mousesave, nil, ZP, S);
421 *d->bufp++ = 'v'; /* five bytes always reserved for this */
422 if(d->_isnewdisplay){
423 BPLONG(d->bufp, d->screenimage->id);
431 bufimage(Display *d, int n)
435 if(n<0 || d == nil || n>d->bufsize){
437 werrstr("bad count in bufimage");
440 if(d->bufp+n > d->buf+d->bufsize)
449 scalesize(Display *d, int n)
451 if(d == nil || d->dpi <= DefaultDPI)
453 return (n*d->dpi+DefaultDPI/2)/DefaultDPI;