11 static char deffontname[] = "*default*";
14 int debuglockdisplay = 1;
17 int visibleclicks = 0;
37 geninitdraw(char *devdir, void(*error)(Display*, char*), char *fontname, char *label, char *windir, int ref)
44 display = _initdisplay(error, label);
51 df = getdefont(display);
52 display->defaultsubfont = df;
54 fprint(2, "imageinit: can't open default subfont: %r\n");
56 closedisplay(display);
61 fontname = getenv("font");
64 * Build fonts with caches==depth of screen, for speed.
65 * If conversion were faster, we'd use 0 and save memory.
68 snprint(buf, sizeof buf, "%d %d\n0 %d\t%s\n", df->height, df->ascent,
69 df->n-1, deffontname);
70 //BUG: Need something better for this installsubfont("*default*", df);
71 font = buildfont(display, buf, deffontname);
73 fprint(2, "imageinit: can't open default font: %r\n");
77 font = openfont(display, fontname); /* BUG: grey fonts */
79 fprint(2, "imageinit: can't open font %s: %r\n", fontname);
83 display->defaultfont = font;
85 _screen = allocscreen(display->image, display->white, 0);
86 display->screenimage = display->image; /* _allocwindow wants screenimage->chan */
87 screen = _allocwindow(nil, _screen, display->image->r, Refnone, DWhite);
89 fprint(2, "_allocwindow: %r\n");
92 display->screenimage = screen;
93 draw(screen, screen->r, display->white, nil, ZP);
94 flushimage(display, 1);
96 p = getenv("visibleclicks");
97 visibleclicks = p != nil && *p == '1';
101 f = display->defaultfont;
102 mousebuttons = allocimage(display, Rect(0,0,64,22), screen->chan, 0, DWhite);
103 border(mousebuttons, mousebuttons->r, 1, display->black, ZP);
104 border(mousebuttons, Rect(0, 0, 22, 22), 1, display->black, ZP);
105 border(mousebuttons, Rect(42, 0, 64, 22), 1, display->black, ZP);
106 string(mousebuttons, Pt(10-stringwidth(display->defaultfont, "1")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "1");
107 string(mousebuttons, Pt(21+10-stringwidth(display->defaultfont, "2")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "2");
108 string(mousebuttons, Pt(42+10-stringwidth(display->defaultfont, "3")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "3");
109 mousesave = allocimage(display, Rect(0,0,64,22), screen->chan, 0, 0);
113 * I don't see any reason to go away gracefully,
114 * and if some other proc exits holding the display
115 * lock, this atexit call never finishes.
117 * atexit(drawshutdown);
123 initdraw(void (*error)(Display*, char*), char *fontname, char *label)
125 return geninitdraw("/dev", error, fontname, label, "/dev", Refnone);
128 extern int _freeimage1(Image*);
131 getimage0(Display *d, Image *image)
138 * If there's an old screen, it has id 0. The 'J' request below
139 * will try to install the new screen as id 0, so the old one
140 * must be freed first.
144 memset(image, 0, sizeof(Image));
150 if(flushimage(d, 0) < 0){
151 fprint(2, "cannot read screen info: %r\n");
155 n = _displayrddraw(d, info, sizeof info);
157 fprint(2, "short screen info\n");
162 image = mallocz(sizeof(Image), 1);
164 fprint(2, "cannot allocate image: %r\n");
171 image->chan = strtochan(info+2*12);
172 image->depth = chantodepth(image->chan);
173 image->repl = atoi(info+3*12);
174 image->r.min.x = atoi(info+4*12);
175 image->r.min.y = atoi(info+5*12);
176 image->r.max.x = atoi(info+6*12);
177 image->r.max.y = atoi(info+7*12);
178 image->clipr.min.x = atoi(info+8*12);
179 image->clipr.min.y = atoi(info+9*12);
180 image->clipr.max.x = atoi(info+10*12);
181 image->clipr.max.y = atoi(info+11*12);
188 if(flushimage(d, 0) >= 0 && _displayrddraw(d, info, 12) == 12)
195 * Attach, or possibly reattach, to window.
196 * If reattaching, maintain value of screen pointer.
199 getwindow(Display *d, int ref)
203 /* XXX check for destroyed? */
206 * Libdraw promises not to change the value of "screen",
207 * so we have to reuse the image structure
208 * memory we already have.
211 i = getimage0(d, oi);
213 sysfatal("getwindow failed");
215 /* fprint(2, "getwindow %p -> %p\n", oi, i); */
218 _screen = allocscreen(i, d->white, 0);
220 screen = _allocwindow(screen, _screen, i->r, ref, DWhite);
221 d->screenimage = screen;
226 _initdisplay(void (*error)(Display*, char*), char *label)
231 fmtinstall('P', Pfmt);
232 fmtinstall('R', Rfmt);
234 disp = mallocz(sizeof(Display), 1);
247 disp->bufsize = 65500;
248 disp->buf = malloc(disp->bufsize+5); /* +5 for flush message */
249 disp->bufp = disp->buf;
261 if(_displaymux(disp) < 0
262 || _displayconnect(disp) < 0
263 || _displayinit(disp, label, winsize) < 0)
271 image = getimage0(disp, nil);
276 disp->white = allocimage(disp, Rect(0, 0, 1, 1), GREY1, 1, DWhite);
277 disp->black = allocimage(disp, Rect(0, 0, 1, 1), GREY1, 1, DBlack);
278 if(disp->white == nil || disp->black == nil){
284 disp->opaque = disp->white;
285 disp->transparent = disp->black;
291 * Call with d unlocked.
292 * Note that disp->defaultfont and defaultsubfont are not freed here.
295 closedisplay(Display *disp)
304 if(disp->oldlabel[0]){
305 snprint(buf, sizeof buf, "%s/label", disp->windir);
306 fd = open(buf, OWRITE);
308 write(fd, disp->oldlabel, strlen(disp->oldlabel));
316 freeimage(disp->white);
318 freeimage(disp->black);
325 lockdisplay(Display *disp)
327 if(debuglockdisplay){
328 /* avoid busy looping; it's rare we collide anyway */
329 while(!canqlock(&disp->qlock)){
330 fprint(1, "proc %d waiting for display lock...\n", getpid());
338 unlockdisplay(Display *disp)
340 qunlock(&disp->qlock);
344 drawerror(Display *d, char *s)
351 errstr(err, sizeof err);
352 fprint(2, "draw: %s: %s\n", s, err);
367 if(_displaywrdraw(d, d->buf, n) != n){
369 fprint(2, "flushimage fail: d=%p: %r\n", d); /**/
370 d->bufp = d->buf; /* might as well; chance of continuing */
378 flushimage(Display *d, int visible)
380 if(visible == 1 && visibleclicks && mousebuttons && _drawmouse.buttons) {
385 r = rectaddpt(r, _drawmouse.xy);
386 r = rectaddpt(r, Pt(-Dx(mousebuttons->r)/2, -Dy(mousebuttons->r)-3));
387 drawop(mousesave, mousesave->r, screen, nil, r.min, S);
389 r1 = rectaddpt(Rect(0, 0, 22, 22), r.min);
390 if(_drawmouse.buttons & 1)
391 drawop(screen, r1, mousebuttons, nil, ZP, S);
392 r1 = rectaddpt(r1, Pt(21, 0));
393 if(_drawmouse.buttons & 2)
394 drawop(screen, r1, mousebuttons, nil, Pt(21, 0), S);
395 r1 = rectaddpt(r1, Pt(21, 0));
396 if(_drawmouse.buttons & 4)
397 drawop(screen, r1, mousebuttons, nil, Pt(42, 0), S);
398 ret = flushimage(d, 2);
399 drawop(screen, r, mousesave, nil, ZP, S);
404 *d->bufp++ = 'v'; /* five bytes always reserved for this */
405 if(d->_isnewdisplay){
406 BPLONG(d->bufp, d->screenimage->id);
414 bufimage(Display *d, int n)
418 if(n<0 || d == nil || n>d->bufsize){
420 werrstr("bad count in bufimage");
423 if(d->bufp+n > d->buf+d->bufsize)
432 scalesize(Display *d, int n)
434 if(d == nil || d->dpi <= DefaultDPI)
436 return (n*d->dpi+DefaultDPI/2)/DefaultDPI;