Blob


1 /*
2 * Some of the stuff in this file is not X-dependent and should be elsewhere.
3 */
4 #include <u.h>
5 #include "x11-inc.h"
6 #include <libc.h>
7 #include <draw.h>
8 #include <memdraw.h>
9 #include <keyboard.h>
10 #include <mouse.h>
11 #include <cursor.h>
12 #include "x11-memdraw.h"
14 char *winsize;
15 static int parsewinsize(char*, Rectangle*, int*);
17 static Memimage *xattach(char*);
18 static void plan9cmap(void);
19 static int setupcmap(XWindow);
20 static int xreplacescreenimage(void);
21 static XGC xgc(XDrawable, int, int);
22 static Image *getimage0(Display*, Image*);
24 Xprivate _x;
26 Display*
27 _initdisplay(void (*error)(Display*, char*), char *label)
28 {
29 Display *d;
30 Memimage *m;
32 /*
33 * This rfork(RFNOTEG) isn't exactly right,
34 * but we need some way to signal window
35 * closes. Right now we post a hangup
36 * note to the note group, which kills a whole
37 * lot more than just the current program
38 * if we don't do this.
39 */
40 /*
41 * Actually, I don't know what I changed but
42 * this appears not to be necessary anymore.
43 * I'll regret this probably.
44 rfork(RFNOTEG);
45 */
46 memimageinit();
48 d = mallocz(sizeof(Display), 1);
49 if(d == nil)
50 return nil;
52 d->buf = malloc(16000+5);
53 d->obuf = malloc(16000);
54 if(d->buf == nil || d->obuf == nil){
55 free(d->buf);
56 free(d->obuf);
57 free(d);
58 return nil;
59 }
60 d->bufsize = 16000;
61 d->obufsize = 16000;
62 d->bufp = d->buf;
63 d->obufp = d->obuf;
65 m = xattach(label);
66 if(m == nil){
67 free(d);
68 return nil;
69 }
71 d->error = error;
72 _initdisplaymemimage(d, m);
73 d->image = getimage0(d, 0);
75 return d;
76 }
78 static Image*
79 getimage0(Display *d, Image *image)
80 {
81 char info[12*12+1];
82 uchar *a;
83 int n;
84 extern int _freeimage1(Image*);
86 /*
87 * If there's an old screen, it has id 0. The 'J' request below
88 * will try to install the new screen as id 0, so the old one
89 * must be freed first.
90 */
91 if(image){
92 _freeimage1(image);
93 memset(image, 0, sizeof(Image));
94 }
96 a = bufimage(d, 2);
97 a[0] = 'J';
98 a[1] = 'I';
99 if(flushimage(d, 0) < 0){
100 fprint(2, "cannot read screen info: %r\n");
101 abort();
104 n = _drawmsgread(d, info, sizeof info);
105 if(n != 12*12){
106 fprint(2, "short screen info\n");
107 abort();
110 if(image == nil){
111 image = mallocz(sizeof(Image), 1);
112 if(image == nil){
113 fprint(2, "cannot allocate image: %r\n");
114 abort();
118 image->display = d;
119 image->id = 0;
120 image->chan = strtochan(info+2*12);
121 image->depth = chantodepth(image->chan);
122 image->repl = atoi(info+3*12);
123 image->r.min.x = atoi(info+4*12);
124 image->r.min.y = atoi(info+5*12);
125 image->r.max.x = atoi(info+6*12);
126 image->r.max.y = atoi(info+7*12);
127 image->clipr.min.x = atoi(info+8*12);
128 image->clipr.min.y = atoi(info+9*12);
129 image->clipr.max.x = atoi(info+10*12);
130 image->clipr.max.y = atoi(info+11*12);
131 return image;
134 int
135 getwindow(Display *d, int ref)
137 Image *i;
138 Image *oi;
140 if(_x.destroyed)
141 postnote(PNGROUP, getpgrp(), "hangup");
142 if(xreplacescreenimage() == 0)
143 return 0;
145 /*
146 * Libdraw promises not to change the value of "screen",
147 * so we have to reuse the image structure
148 * memory we already have.
149 */
150 oi = d->image;
151 i = getimage0(d, oi);
152 d->image = i;
153 // fprint(2, "getwindow %p -> %p\n", oi, i);
155 _screen = allocscreen(i, d->white, 0);
156 _freeimage1(screen);
157 screen = _allocwindow(screen, _screen, i->r, ref, DWhite);
158 d->screenimage = screen;
159 return 0;
162 static int
163 xerror(XDisplay *d, XErrorEvent *e)
165 char buf[200];
167 if(e->request_code == 42) /* XSetInputFocus */
168 return 0;
169 if(e->request_code == 18) /* XChangeProperty */
170 return 0;
172 print("X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
173 e->error_code, e->request_code, e->minor_code, d);
174 XGetErrorText(d, e->error_code, buf, sizeof buf);
175 print("%s\n", buf);
176 return 0;
179 static int
180 xioerror(XDisplay *d)
182 //print("X I/O error\n");
183 sysfatal("X I/O error\n");
184 abort();
185 return -1;
189 static Memimage*
190 xattach(char *label)
192 char *argv[2], *disp;
193 int i, havemin, height, mask, n, width, x, xrootid, y;
194 Rectangle r;
195 XClassHint classhint;
196 XDrawable pmid;
197 XPixmapFormatValues *pfmt;
198 XScreen *xscreen;
199 XSetWindowAttributes attr;
200 XSizeHints normalhint;
201 XTextProperty name;
202 XVisualInfo xvi;
203 XWindow xrootwin;
204 XWindowAttributes wattr;
205 XWMHints hint;
206 Atom atoms[2];
208 /*
209 if(XInitThreads() == 0){
210 fprint(2, "XInitThreads failed\n");
211 abort();
213 */
215 /*
216 * Connect to X server.
217 */
218 _x.display = XOpenDisplay(NULL);
219 if(_x.display == nil){
220 disp = getenv("DISPLAY");
221 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
222 free(disp);
223 return nil;
225 XSetErrorHandler(xerror);
226 XSetIOErrorHandler(xioerror);
227 xrootid = DefaultScreen(_x.display);
228 xrootwin = DefaultRootWindow(_x.display);
230 /*
231 * Figure out underlying screen format.
232 */
233 if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
234 || XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
235 _x.vis = xvi.visual;
236 _x.depth = 16;
238 else
239 if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
240 || XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
241 _x.vis = xvi.visual;
242 _x.depth = 15;
244 else
245 if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
246 || XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
247 _x.vis = xvi.visual;
248 _x.depth = 24;
250 else
251 if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
252 || XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
253 if(_x.depth > 8){
254 werrstr("can't deal with colormapped depth %d screens",
255 _x.depth);
256 goto err0;
258 _x.vis = xvi.visual;
259 _x.depth = 8;
261 else{
262 _x.depth = DefaultDepth(_x.display, xrootid);
263 if(_x.depth != 8){
264 werrstr("can't understand depth %d screen", _x.depth);
265 goto err0;
267 _x.vis = DefaultVisual(_x.display, xrootid);
270 if(DefaultDepth(_x.display, xrootid) == _x.depth)
271 _x.usetable = 1;
273 /*
274 * _x.depth is only the number of significant pixel bits,
275 * not the total number of pixel bits. We need to walk the
276 * display list to find how many actual bits are used
277 * per pixel.
278 */
279 _x.chan = 0;
280 pfmt = XListPixmapFormats(_x.display, &n);
281 for(i=0; i<n; i++){
282 if(pfmt[i].depth == _x.depth){
283 switch(pfmt[i].bits_per_pixel){
284 case 1: /* untested */
285 _x.chan = GREY1;
286 break;
287 case 2: /* untested */
288 _x.chan = GREY2;
289 break;
290 case 4: /* untested */
291 _x.chan = GREY4;
292 break;
293 case 8:
294 _x.chan = CMAP8;
295 break;
296 case 15:
297 _x.chan = RGB15;
298 break;
299 case 16: /* how to tell RGB15? */
300 _x.chan = RGB16;
301 break;
302 case 24: /* untested (impossible?) */
303 _x.chan = RGB24;
304 break;
305 case 32:
306 _x.chan = XRGB32;
307 break;
311 if(_x.chan == 0){
312 werrstr("could not determine screen pixel format");
313 goto err0;
316 /*
317 * Set up color map if necessary.
318 */
319 xscreen = DefaultScreenOfDisplay(_x.display);
320 _x.cmap = DefaultColormapOfScreen(xscreen);
321 if(_x.vis->class != StaticColor){
322 plan9cmap();
323 setupcmap(xrootwin);
326 /*
327 * We get to choose the initial rectangle size.
328 * This is arbitrary. In theory we should read the
329 * command line and allow the traditional X options.
330 */
331 mask = 0;
332 x = 0;
333 y = 0;
334 if(winsize){
335 if(parsewinsize(winsize, &r, &havemin) < 0)
336 sysfatal("%r");
337 }else{
338 /*
339 * Parse the various X resources. Thanks to Peter Canning.
340 */
341 char *screen_resources, *display_resources, *geom,
342 *geomrestype, *home, *file;
343 XrmDatabase database;
344 XrmValue geomres;
346 database = XrmGetDatabase(_x.display);
347 screen_resources = XScreenResourceString(xscreen);
348 if(screen_resources != nil){
349 XrmCombineDatabase(XrmGetStringDatabase(screen_resources), &database, False);
350 XFree(screen_resources);
353 display_resources = XResourceManagerString(_x.display);
354 if(display_resources == nil){
355 home = getenv("HOME");
356 if(home!=nil && (file=smprint("%s/.Xdefaults", home)) != nil){
357 XrmCombineFileDatabase(file, &database, False);
358 free(file);
360 free(home);
361 }else
362 XrmCombineDatabase(XrmGetStringDatabase(display_resources), &database, False);
364 geom = smprint("%s.geometry", label);
365 if(geom && XrmGetResource(database, geom, nil, &geomrestype, &geomres))
366 mask = XParseGeometry(geomres.addr, &x, &y, (uint*)&width, (uint*)&height);
367 free(geom);
369 if((mask & WidthValue) && (mask & HeightValue)){
370 r = Rect(0, 0, width, height);
371 }else{
372 r = Rect(0, 0, WidthOfScreen(xscreen)*3/4,
373 HeightOfScreen(xscreen)*3/4);
374 if(Dx(r) > Dy(r)*3/2)
375 r.max.x = r.min.x + Dy(r)*3/2;
376 if(Dy(r) > Dx(r)*3/2)
377 r.max.y = r.min.y + Dx(r)*3/2;
379 if(mask & XNegative){
380 x += WidthOfScreen(xscreen);
382 if(mask & YNegative){
383 y += HeightOfScreen(xscreen);
385 havemin = 0;
388 memset(&attr, 0, sizeof attr);
389 attr.colormap = _x.cmap;
390 attr.background_pixel = ~0;
391 attr.border_pixel = 0;
392 _x.drawable = XCreateWindow(
393 _x.display, /* display */
394 xrootwin, /* parent */
395 x, /* x */
396 y, /* y */
397 Dx(r), /* width */
398 Dy(r), /* height */
399 0, /* border width */
400 _x.depth, /* depth */
401 InputOutput, /* class */
402 _x.vis, /* visual */
403 /* valuemask */
404 CWBackPixel|CWBorderPixel|CWColormap,
405 &attr /* attributes (the above aren't?!) */
406 );
408 /*
409 * Label and other properties required by ICCCCM.
410 */
411 memset(&name, 0, sizeof name);
412 if(label == nil)
413 label = "pjw-face-here";
414 name.value = (uchar*)label;
415 name.encoding = XA_STRING;
416 name.format = 8;
417 name.nitems = strlen((char*)name.value);
419 memset(&normalhint, 0, sizeof normalhint);
420 normalhint.flags = PSize|PMaxSize;
421 if(winsize){
422 normalhint.flags &= ~PSize;
423 normalhint.flags |= USSize;
424 normalhint.width = Dx(r);
425 normalhint.height = Dy(r);
426 }else{
427 if((mask & WidthValue) && (mask & HeightValue)){
428 normalhint.flags &= ~PSize;
429 normalhint.flags |= USSize;
430 normalhint.width = width;
431 normalhint.height = height;
433 if((mask & WidthValue) && (mask & HeightValue)){
434 normalhint.flags |= USPosition;
435 normalhint.x = x;
436 normalhint.y = y;
440 normalhint.max_width = WidthOfScreen(xscreen);
441 normalhint.max_height = HeightOfScreen(xscreen);
443 memset(&hint, 0, sizeof hint);
444 hint.flags = InputHint|StateHint;
445 hint.input = 1;
446 hint.initial_state = NormalState;
448 memset(&classhint, 0, sizeof classhint);
449 classhint.res_name = label;
450 classhint.res_class = label;
452 argv[0] = label;
453 argv[1] = nil;
455 XSetWMProperties(
456 _x.display, /* display */
457 _x.drawable, /* window */
458 &name, /* XA_WM_NAME property */
459 &name, /* XA_WM_ICON_NAME property */
460 argv, /* XA_WM_COMMAND */
461 1, /* argc */
462 &normalhint, /* XA_WM_NORMAL_HINTS */
463 &hint, /* XA_WM_HINTS */
464 &classhint /* XA_WM_CLASSHINTS */
465 );
466 XFlush(_x.display);
468 if(havemin){
469 XWindowChanges ch;
471 memset(&ch, 0, sizeof ch);
472 ch.x = r.min.x;
473 ch.y = r.min.y;
474 XConfigureWindow(_x.display, _x.drawable, CWX|CWY, &ch);
475 /*
476 * Must pretend origin is 0,0 for X.
477 */
478 r = Rect(0,0,Dx(r),Dy(r));
480 /*
481 * Look up clipboard atom.
482 */
483 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", False);
484 _x.utf8string = XInternAtom(_x.display, "UTF8_STRING", False);
485 _x.targets = XInternAtom(_x.display, "TARGETS", False);
486 _x.text = XInternAtom(_x.display, "TEXT", False);
487 _x.compoundtext = XInternAtom(_x.display, "COMPOUND_TEXT", False);
488 _x.takefocus = XInternAtom(_x.display, "WM_TAKE_FOCUS", False);
489 _x.losefocus = XInternAtom(_x.display, "_9WM_LOSE_FOCUS", False);
490 _x.wmprotos = XInternAtom(_x.display, "WM_PROTOCOLS", False);
492 atoms[0] = _x.takefocus;
493 atoms[1] = _x.losefocus;
494 XChangeProperty(_x.display, _x.drawable, _x.wmprotos, XA_ATOM, 32,
495 PropModeReplace, (uchar*)atoms, 2);
497 /*
498 * Put the window on the screen, check to see what size we actually got.
499 */
500 XMapWindow(_x.display, _x.drawable);
501 XSync(_x.display, False);
503 if(!XGetWindowAttributes(_x.display, _x.drawable, &wattr))
504 fprint(2, "XGetWindowAttributes failed\n");
505 else if(wattr.width && wattr.height){
506 if(wattr.width != Dx(r) || wattr.height != Dy(r)){
507 r.max.x = wattr.width;
508 r.max.y = wattr.height;
510 }else
511 fprint(2, "XGetWindowAttributes: bad attrs\n");
513 /*
514 * Allocate our local backing store.
515 */
516 _x.screenr = r;
517 _x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
518 _x.nextscreenpm = _x.screenpm;
519 _x.screenimage = _xallocmemimage(r, _x.chan, _x.screenpm);
521 /*
522 * Allocate some useful graphics contexts for the future.
523 */
524 _x.gcfill = xgc(_x.screenpm, FillSolid, -1);
525 _x.gccopy = xgc(_x.screenpm, -1, -1);
526 _x.gcsimplesrc = xgc(_x.screenpm, FillStippled, -1);
527 _x.gczero = xgc(_x.screenpm, -1, -1);
528 _x.gcreplsrc = xgc(_x.screenpm, FillTiled, -1);
530 pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
531 _x.gcfill0 = xgc(pmid, FillSolid, 0);
532 _x.gccopy0 = xgc(pmid, -1, -1);
533 _x.gcsimplesrc0 = xgc(pmid, FillStippled, -1);
534 _x.gczero0 = xgc(pmid, -1, -1);
535 _x.gcreplsrc0 = xgc(pmid, FillTiled, -1);
536 XFreePixmap(_x.display, pmid);
538 /*
539 * Lots of display connections for various procs.
540 */
541 _x.kbdcon = XOpenDisplay(NULL);
542 _x.mousecon = XOpenDisplay(NULL);
543 _x.snarfcon = XOpenDisplay(NULL);
545 if(0) fprint(2, "x: display=%p kbd=%p mouse=%p snarf=%p\n",
546 _x.display, _x.kbdcon, _x.mousecon, _x.snarfcon);
548 return _x.screenimage;
550 err0:
551 fprint(2, "%r\n");
552 /*
553 * Should do a better job of cleaning up here.
554 */
555 XCloseDisplay(_x.display);
556 return nil;
559 int
560 drawsetlabel(char *label)
562 XTextProperty name;
564 /*
565 * Label and other properties required by ICCCCM.
566 */
567 memset(&name, 0, sizeof name);
568 if(label == nil)
569 label = "pjw-face-here";
570 name.value = (uchar*)label;
571 name.encoding = XA_STRING;
572 name.format = 8;
573 name.nitems = strlen((char*)name.value);
575 XSetWMProperties(
576 _x.display, /* display */
577 _x.drawable, /* window */
578 &name, /* XA_WM_NAME property */
579 &name, /* XA_WM_ICON_NAME property */
580 nil, /* XA_WM_COMMAND */
581 0, /* argc */
582 nil, /* XA_WM_NORMAL_HINTS */
583 nil, /* XA_WM_HINTS */
584 nil /* XA_WM_CLASSHINTS */
585 );
586 XFlush(_x.display);
587 return 0;
590 /*
591 * Create a GC with a particular fill style and XXX.
592 * Disable generation of GraphicsExpose/NoExpose events in the GC.
593 */
594 static XGC
595 xgc(XDrawable d, int fillstyle, int foreground)
597 XGC gc;
598 XGCValues v;
600 memset(&v, 0, sizeof v);
601 v.function = GXcopy;
602 v.graphics_exposures = False;
603 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
604 if(fillstyle != -1)
605 XSetFillStyle(_x.display, gc, fillstyle);
606 if(foreground != -1)
607 XSetForeground(_x.display, gc, 0);
608 return gc;
612 /*
613 * Initialize map with the Plan 9 rgbv color map.
614 */
615 static void
616 plan9cmap(void)
618 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
619 static int once;
621 if(once)
622 return;
623 once = 1;
625 for(r=0; r!=4; r++)
626 for(g = 0; g != 4; g++)
627 for(b = 0; b!=4; b++)
628 for(v = 0; v!=4; v++){
629 den=r;
630 if(g > den)
631 den=g;
632 if(b > den)
633 den=b;
634 /* divide check -- pick grey shades */
635 if(den==0)
636 cr=cg=cb=v*17;
637 else {
638 num=17*(4*den+v);
639 cr=r*num/den;
640 cg=g*num/den;
641 cb=b*num/den;
643 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
644 _x.map[idx].red = cr*0x0101;
645 _x.map[idx].green = cg*0x0101;
646 _x.map[idx].blue = cb*0x0101;
647 _x.map[idx].pixel = idx;
648 _x.map[idx].flags = DoRed|DoGreen|DoBlue;
650 v7 = v >> 1;
651 idx7 = r*32 + v7*16 + g*4 + b;
652 if((v & 1) == v7){
653 _x.map7to8[idx7][0] = idx;
654 if(den == 0) { /* divide check -- pick grey shades */
655 cr = ((255.0/7.0)*v7)+0.5;
656 cg = cr;
657 cb = cr;
659 else {
660 num=17*15*(4*den+v7*2)/14;
661 cr=r*num/den;
662 cg=g*num/den;
663 cb=b*num/den;
665 _x.map7[idx7].red = cr*0x0101;
666 _x.map7[idx7].green = cg*0x0101;
667 _x.map7[idx7].blue = cb*0x0101;
668 _x.map7[idx7].pixel = idx7;
669 _x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
671 else
672 _x.map7to8[idx7][1] = idx;
676 /*
677 * Initialize and install the rgbv color map as a private color map
678 * for this application. It gets the best colors when it has the
679 * cursor focus.
681 * We always choose the best depth possible, but that might not
682 * be the default depth. On such "suboptimal" systems, we have to allocate an
683 * empty color map anyway, according to Axel Belinfante.
684 */
685 static int
686 setupcmap(XWindow w)
688 char buf[30];
689 int i;
690 u32int p, pp;
691 XColor c;
693 if(_x.depth <= 1)
694 return 0;
696 if(_x.depth >= 24) {
697 if(_x.usetable == 0)
698 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
700 /*
701 * The pixel value returned from XGetPixel needs to
702 * be converted to RGB so we can call rgb2cmap()
703 * to translate between 24 bit X and our color. Unfortunately,
704 * the return value appears to be display server endian
705 * dependant. Therefore, we run some heuristics to later
706 * determine how to mask the int value correctly.
707 * Yeah, I know we can look at _x.vis->byte_order but
708 * some displays say MSB even though they run on LSB.
709 * Besides, this is more anal.
710 */
711 c = _x.map[19]; /* known to have different R, G, B values */
712 if(!XAllocColor(_x.display, _x.cmap, &c)){
713 werrstr("XAllocColor: %r");
714 return -1;
716 p = c.pixel;
717 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
718 if(pp != _x.map[19].pixel) {
719 /* check if endian is other way */
720 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
721 if(pp != _x.map[19].pixel){
722 werrstr("cannot detect X server byte order");
723 return -1;
726 switch(_x.chan){
727 case RGB24:
728 _x.chan = BGR24;
729 break;
730 case XRGB32:
731 _x.chan = XBGR32;
732 break;
733 default:
734 werrstr("cannot byteswap channel %s",
735 chantostr(buf, _x.chan));
736 break;
739 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
740 /*
741 * Do nothing. We have no way to express a
742 * mixed-endian 16-bit screen, so pretend they don't exist.
743 */
744 if(_x.usetable == 0)
745 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
746 }else if(_x.vis->class == PseudoColor){
747 if(_x.usetable == 0){
748 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll);
749 XStoreColors(_x.display, _x.cmap, _x.map, 256);
750 for(i = 0; i < 256; i++){
751 _x.tox11[i] = i;
752 _x.toplan9[i] = i;
754 }else{
755 for(i = 0; i < 128; i++){
756 c = _x.map7[i];
757 if(!XAllocColor(_x.display, _x.cmap, &c)){
758 werrstr("can't allocate colors in 7-bit map");
759 return -1;
761 _x.tox11[_x.map7to8[i][0]] = c.pixel;
762 _x.tox11[_x.map7to8[i][1]] = c.pixel;
763 _x.toplan9[c.pixel] = _x.map7to8[i][0];
766 }else{
767 werrstr("unsupported visual class %d", _x.vis->class);
768 return -1;
770 return 0;
773 void
774 _flushmemscreen(Rectangle r)
776 if(_x.nextscreenpm != _x.screenpm){
777 qlock(&_x.screenlock);
778 XSync(_x.display, False);
779 XFreePixmap(_x.display, _x.screenpm);
780 _x.screenpm = _x.nextscreenpm;
781 qunlock(&_x.screenlock);
784 if(r.min.x >= r.max.x || r.min.y >= r.max.y)
785 return;
786 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
787 Dx(r), Dy(r), r.min.x, r.min.y);
788 XFlush(_x.display);
791 void
792 _xexpose(XEvent *e, XDisplay *xd)
794 XExposeEvent *xe;
795 Rectangle r;
797 qlock(&_x.screenlock);
798 if(_x.screenpm != _x.nextscreenpm){
799 qunlock(&_x.screenlock);
800 return;
802 xe = (XExposeEvent*)e;
803 r.min.x = xe->x;
804 r.min.y = xe->y;
805 r.max.x = xe->x+xe->width;
806 r.max.y = xe->y+xe->height;
807 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
808 Dx(r), Dy(r), r.min.x, r.min.y);
809 XSync(xd, False);
810 qunlock(&_x.screenlock);
813 int
814 _xdestroy(XEvent *e, XDisplay *xd)
816 XDestroyWindowEvent *xe;
818 xe = (XDestroyWindowEvent*)e;
819 if(xe->window == _x.drawable){
820 _x.destroyed = 1;
821 return 1;
823 return 0;
826 int
827 _xconfigure(XEvent *e, XDisplay *xd)
829 Rectangle r;
830 XConfigureEvent *xe = (XConfigureEvent*)e;
832 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
833 return 0;
834 if(xe->width==0 || xe->height==0)
835 fprint(2, "ignoring resize to %dx%d\n", xe->width, xe->height);
836 r = Rect(0, 0, xe->width, xe->height);
837 qlock(&_x.screenlock);
838 if(_x.screenpm != _x.nextscreenpm){
839 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
840 Dx(r), Dy(r), r.min.x, r.min.y);
841 XSync(xd, False);
843 qunlock(&_x.screenlock);
844 _x.newscreenr = r;
845 return 1;
848 static int
849 xreplacescreenimage(void)
851 Memimage *m;
852 XDrawable pixmap;
853 Rectangle r;
855 r = _x.newscreenr;
856 if(eqrect(_x.screenr, r))
857 return 0;
859 pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
860 m = _xallocmemimage(r, _x.chan, pixmap);
861 if(_x.nextscreenpm != _x.screenpm)
862 XFreePixmap(_x.display, _x.nextscreenpm);
863 _x.nextscreenpm = pixmap;
864 _x.screenr = r;
865 _drawreplacescreenimage(m);
866 return 1;
869 static int
870 parsewinsize(char *s, Rectangle *r, int *havemin)
872 char c, *os;
873 int i, j, k, l;
875 os = s;
876 *havemin = 0;
877 *r = Rect(0,0,0,0);
878 if(!isdigit((uchar)*s))
879 goto oops;
880 i = strtol(s, &s, 0);
881 if(*s == 'x'){
882 s++;
883 if(!isdigit((uchar)*s))
884 goto oops;
885 j = strtol(s, &s, 0);
886 r->max.x = i;
887 r->max.y = j;
888 if(*s == 0)
889 return 0;
890 if(*s != '@')
891 goto oops;
893 s++;
894 if(!isdigit((uchar)*s))
895 goto oops;
896 i = strtol(s, &s, 0);
897 if(*s != ',' && *s != ' ')
898 goto oops;
899 s++;
900 if(!isdigit((uchar)*s))
901 goto oops;
902 j = strtol(s, &s, 0);
903 if(*s != 0)
904 goto oops;
905 *r = rectaddpt(*r, Pt(i,j));
906 *havemin = 1;
907 return 0;
910 c = *s;
911 if(c != ' ' && c != ',')
912 goto oops;
913 s++;
914 if(!isdigit((uchar)*s))
915 goto oops;
916 j = strtol(s, &s, 0);
917 if(*s != c)
918 goto oops;
919 s++;
920 if(!isdigit((uchar)*s))
921 goto oops;
922 k = strtol(s, &s, 0);
923 if(*s != c)
924 goto oops;
925 s++;
926 if(!isdigit((uchar)*s))
927 goto oops;
928 l = strtol(s, &s, 0);
929 if(*s != 0)
930 goto oops;
931 *r = Rect(i,j,k,l);
932 *havemin = 1;
933 return 0;
935 oops:
936 werrstr("bad syntax in window size '%s'", os);
937 return -1;