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);
186 * Attach, or possibly reattach, to window.
187 * If reattaching, maintain value of screen pointer.
190 getwindow(Display *d, int ref)
194 /* XXX check for destroyed? */
197 * Libdraw promises not to change the value of "screen",
198 * so we have to reuse the image structure
199 * memory we already have.
202 i = getimage0(d, oi);
204 sysfatal("getwindow failed");
206 /* fprint(2, "getwindow %p -> %p\n", oi, i); */
209 _screen = allocscreen(i, d->white, 0);
211 screen = _allocwindow(screen, _screen, i->r, ref, DWhite);
212 d->screenimage = screen;
217 _initdisplay(void (*error)(Display*, char*), char *label)
222 fmtinstall('P', Pfmt);
223 fmtinstall('R', Rfmt);
225 disp = mallocz(sizeof(Display), 1);
238 disp->bufsize = 65500;
239 disp->buf = malloc(disp->bufsize+5); /* +5 for flush message */
240 disp->bufp = disp->buf;
252 if(_displaymux(disp) < 0
253 || _displayconnect(disp) < 0
254 || _displayinit(disp, label, winsize) < 0)
262 image = getimage0(disp, nil);
267 disp->white = allocimage(disp, Rect(0, 0, 1, 1), GREY1, 1, DWhite);
268 disp->black = allocimage(disp, Rect(0, 0, 1, 1), GREY1, 1, DBlack);
269 if(disp->white == nil || disp->black == nil){
275 disp->opaque = disp->white;
276 disp->transparent = disp->black;
282 * Call with d unlocked.
283 * Note that disp->defaultfont and defaultsubfont are not freed here.
286 closedisplay(Display *disp)
295 if(disp->oldlabel[0]){
296 snprint(buf, sizeof buf, "%s/label", disp->windir);
297 fd = open(buf, OWRITE);
299 write(fd, disp->oldlabel, strlen(disp->oldlabel));
307 freeimage(disp->white);
309 freeimage(disp->black);
316 lockdisplay(Display *disp)
318 if(debuglockdisplay){
319 /* avoid busy looping; it's rare we collide anyway */
320 while(!canqlock(&disp->qlock)){
321 fprint(1, "proc %d waiting for display lock...\n", getpid());
329 unlockdisplay(Display *disp)
331 qunlock(&disp->qlock);
335 drawerror(Display *d, char *s)
342 errstr(err, sizeof err);
343 fprint(2, "draw: %s: %s\n", s, err);
358 if(_displaywrdraw(d, d->buf, n) != n){
360 fprint(2, "flushimage fail: d=%p: %r\n", d); /**/
361 d->bufp = d->buf; /* might as well; chance of continuing */
369 flushimage(Display *d, int visible)
371 if(visible == 1 && visibleclicks && mousebuttons && _drawmouse.buttons) {
376 r = rectaddpt(r, _drawmouse.xy);
377 r = rectaddpt(r, Pt(-Dx(mousebuttons->r)/2, -Dy(mousebuttons->r)-3));
378 drawop(mousesave, mousesave->r, screen, nil, r.min, S);
380 r1 = rectaddpt(Rect(0, 0, 22, 22), r.min);
381 if(_drawmouse.buttons & 1)
382 drawop(screen, r1, mousebuttons, nil, ZP, S);
383 r1 = rectaddpt(r1, Pt(21, 0));
384 if(_drawmouse.buttons & 2)
385 drawop(screen, r1, mousebuttons, nil, Pt(21, 0), S);
386 r1 = rectaddpt(r1, Pt(21, 0));
387 if(_drawmouse.buttons & 4)
388 drawop(screen, r1, mousebuttons, nil, Pt(42, 0), S);
389 ret = flushimage(d, 2);
390 drawop(screen, r, mousesave, nil, ZP, S);
395 *d->bufp++ = 'v'; /* five bytes always reserved for this */
396 if(d->_isnewdisplay){
397 BPLONG(d->bufp, d->screenimage->id);
405 bufimage(Display *d, int n)
409 if(n<0 || d == nil || n>d->bufsize){
411 werrstr("bad count in bufimage");
414 if(d->bufp+n > d->buf+d->bufsize)