13 int debuglockdisplay = 1;
16 int visibleclicks = 0;
41 geninitdraw(char *devdir, void(*error)(Display*, char*), char *fontname, char *label, char *windir, int ref)
47 display = _initdisplay(error, label);
54 if(openfont(display, "*default*") == 0) {
55 fprint(2, "imageinit: can't open default subfont: %r\n");
57 closedisplay(display);
62 fontname = getenv("font");
65 * Build fonts with caches==depth of screen, for speed.
66 * If conversion were faster, we'd use 0 and save memory.
69 fontname = strdup("*default*");
71 font = openfont(display, fontname);
73 fprint(2, "imageinit: can't open font %s: %r\n", fontname);
76 display->defaultfont = font;
78 _screen = allocscreen(display->image, display->white, 0);
79 display->screenimage = display->image; /* _allocwindow wants screenimage->chan */
80 screen = _allocwindow(nil, _screen, display->image->r, Refnone, DWhite);
82 fprint(2, "_allocwindow: %r\n");
85 display->screenimage = screen;
86 draw(screen, screen->r, display->white, nil, ZP);
87 flushimage(display, 1);
89 p = getenv("visibleclicks");
90 visibleclicks = p != nil && *p == '1';
94 f = display->defaultfont;
95 mousebuttons = allocimage(display, Rect(0,0,64,22), screen->chan, 0, DWhite);
96 border(mousebuttons, mousebuttons->r, 1, display->black, ZP);
97 border(mousebuttons, Rect(0, 0, 22, 22), 1, display->black, ZP);
98 border(mousebuttons, Rect(42, 0, 64, 22), 1, display->black, ZP);
99 string(mousebuttons, Pt(10-stringwidth(display->defaultfont, "1")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "1");
100 string(mousebuttons, Pt(21+10-stringwidth(display->defaultfont, "2")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "2");
101 string(mousebuttons, Pt(42+10-stringwidth(display->defaultfont, "3")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "3");
102 mousesave = allocimage(display, Rect(0,0,64,22), screen->chan, 0, 0);
106 * I don't see any reason to go away gracefully,
107 * and if some other proc exits holding the display
108 * lock, this atexit call never finishes.
110 * atexit(drawshutdown);
116 initdraw(void (*error)(Display*, char*), char *fontname, char *label)
118 return geninitdraw("/dev", error, fontname, label, "/dev", Refnone);
121 extern int _freeimage1(Image*);
124 getimage0(Display *d, Image *image)
131 * If there's an old screen, it has id 0. The 'J' request below
132 * will try to install the new screen as id 0, so the old one
133 * must be freed first.
137 memset(image, 0, sizeof(Image));
143 if(flushimage(d, 0) < 0){
144 fprint(2, "cannot read screen info: %r\n");
148 n = _displayrddraw(d, info, sizeof info);
150 fprint(2, "short screen info\n");
155 image = mallocz(sizeof(Image), 1);
157 fprint(2, "cannot allocate image: %r\n");
164 image->chan = strtochan(info+2*12);
165 image->depth = chantodepth(image->chan);
166 image->repl = atoi(info+3*12);
167 image->r.min.x = atoi(info+4*12);
168 image->r.min.y = atoi(info+5*12);
169 image->r.max.x = atoi(info+6*12);
170 image->r.max.y = atoi(info+7*12);
171 image->clipr.min.x = atoi(info+8*12);
172 image->clipr.min.y = atoi(info+9*12);
173 image->clipr.max.x = atoi(info+10*12);
174 image->clipr.max.y = atoi(info+11*12);
181 if(flushimage(d, 0) >= 0 && _displayrddraw(d, info, 12) == 12)
188 * Attach, or possibly reattach, to window.
189 * If reattaching, maintain value of screen pointer.
192 getwindow(Display *d, int ref)
197 /* XXX check for destroyed? */
200 * Libdraw promises not to change the value of "screen",
201 * so we have to reuse the image structure
202 * memory we already have.
205 i = getimage0(d, oi);
207 sysfatal("getwindow failed");
209 /* fprint(2, "getwindow %p -> %p\n", oi, i); */
212 _screen = allocscreen(i, d->white, 0);
214 screen = _allocwindow(screen, _screen, i->r, ref, DWhite);
215 d->screenimage = screen;
218 if(d->dpi >= DefaultDPI*3/2) {
219 for(f=d->firstfont; f != nil; f=f->next)
222 for(f=d->firstfont; f != nil; f=f->next)
223 if(f->lodpi != nil && f->lodpi != f)
224 swapfont(f, &f->hidpi, &f->lodpi);
231 _initdisplay(void (*error)(Display*, char*), char *label)
236 fmtinstall('P', Pfmt);
237 fmtinstall('R', Rfmt);
239 disp = mallocz(sizeof(Display), 1);
252 disp->bufsize = 65500;
253 disp->buf = malloc(disp->bufsize+5); /* +5 for flush message */
254 disp->bufp = disp->buf;
266 if(_displaymux(disp) < 0
267 || _displayconnect(disp) < 0
268 || _displayinit(disp, label, winsize) < 0)
276 image = getimage0(disp, nil);
281 disp->white = allocimage(disp, Rect(0, 0, 1, 1), GREY1, 1, DWhite);
282 disp->black = allocimage(disp, Rect(0, 0, 1, 1), GREY1, 1, DBlack);
283 if(disp->white == nil || disp->black == nil){
289 disp->opaque = disp->white;
290 disp->transparent = disp->black;
296 * Call with d unlocked.
297 * Note that disp->defaultfont is not freed here.
300 closedisplay(Display *disp)
309 if(disp->oldlabel[0]){
310 snprint(buf, sizeof buf, "%s/label", disp->windir);
311 fd = open(buf, OWRITE);
313 write(fd, disp->oldlabel, strlen(disp->oldlabel));
321 freeimage(disp->white);
323 freeimage(disp->black);
330 lockdisplay(Display *disp)
332 if(debuglockdisplay){
333 /* avoid busy looping; it's rare we collide anyway */
334 while(!canqlock(&disp->qlock)){
335 fprint(1, "proc %d waiting for display lock...\n", getpid());
343 unlockdisplay(Display *disp)
345 qunlock(&disp->qlock);
349 drawerror(Display *d, char *s)
356 errstr(err, sizeof err);
357 fprint(2, "draw: %s: %s\n", s, err);
372 if(_displaywrdraw(d, d->buf, n) != n){
374 fprint(2, "flushimage fail: d=%p: %r\n", d); /**/
375 d->bufp = d->buf; /* might as well; chance of continuing */
383 flushimage(Display *d, int visible)
385 if(visible == 1 && visibleclicks && mousebuttons && _drawmouse.buttons) {
390 r = rectaddpt(r, _drawmouse.xy);
391 r = rectaddpt(r, Pt(-Dx(mousebuttons->r)/2, -Dy(mousebuttons->r)-3));
392 drawop(mousesave, mousesave->r, screen, nil, r.min, S);
394 r1 = rectaddpt(Rect(0, 0, 22, 22), r.min);
395 if(_drawmouse.buttons & 1)
396 drawop(screen, r1, mousebuttons, nil, ZP, S);
397 r1 = rectaddpt(r1, Pt(21, 0));
398 if(_drawmouse.buttons & 2)
399 drawop(screen, r1, mousebuttons, nil, Pt(21, 0), S);
400 r1 = rectaddpt(r1, Pt(21, 0));
401 if(_drawmouse.buttons & 4)
402 drawop(screen, r1, mousebuttons, nil, Pt(42, 0), S);
403 ret = flushimage(d, 2);
404 drawop(screen, r, mousesave, nil, ZP, S);
409 *d->bufp++ = 'v'; /* five bytes always reserved for this */
410 if(d->_isnewdisplay){
411 BPLONG(d->bufp, d->screenimage->id);
419 bufimage(Display *d, int n)
423 if(n<0 || d == nil || n>d->bufsize){
425 werrstr("bad count in bufimage");
428 if(d->bufp+n > d->buf+d->bufsize)
437 scalesize(Display *d, int n)
439 if(d == nil || d->dpi <= DefaultDPI)
441 return (n*d->dpi+DefaultDPI/2)/DefaultDPI;