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 return -1;
144 if(xreplacescreenimage() == 0)
145 return 0;
147 /*
148 * Libdraw promises not to change the value of "screen",
149 * so we have to reuse the image structure
150 * memory we already have.
151 */
152 oi = d->image;
153 i = getimage0(d, oi);
154 d->image = i;
155 // fprint(2, "getwindow %p -> %p\n", oi, i);
157 _screen = allocscreen(i, d->white, 0);
158 _freeimage1(screen);
159 screen = _allocwindow(screen, _screen, i->r, ref, DWhite);
160 d->screenimage = screen;
161 return 0;
164 static int
165 xerror(XDisplay *d, XErrorEvent *e)
167 char buf[200];
169 if(e->request_code == 42) /* XSetInputFocus */
170 return 0;
171 if(e->request_code == 18) /* XChangeProperty */
172 return 0;
174 print("X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
175 e->error_code, e->request_code, e->minor_code, d);
176 XGetErrorText(d, e->error_code, buf, sizeof buf);
177 print("%s\n", buf);
178 return 0;
181 static int
182 xioerror(XDisplay *d)
184 //print("X I/O error\n");
185 sysfatal("X I/O error\n");
186 abort();
187 return -1;
191 static Memimage*
192 xattach(char *label)
194 char *argv[2], *disp;
195 int i, havemin, height, mask, n, width, x, xrootid, y;
196 Rectangle r;
197 XClassHint classhint;
198 XDrawable pmid;
199 XPixmapFormatValues *pfmt;
200 XScreen *xscreen;
201 XSetWindowAttributes attr;
202 XSizeHints normalhint;
203 XTextProperty name;
204 XVisualInfo xvi;
205 XWindow xrootwin;
206 XWindowAttributes wattr;
207 XWMHints hint;
208 Atom atoms[2];
210 /*
211 if(XInitThreads() == 0){
212 fprint(2, "XInitThreads failed\n");
213 abort();
215 */
217 /*
218 * Connect to X server.
219 */
220 _x.display = XOpenDisplay(NULL);
221 if(_x.display == nil){
222 disp = getenv("DISPLAY");
223 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
224 free(disp);
225 return nil;
227 XSetErrorHandler(xerror);
228 XSetIOErrorHandler(xioerror);
229 xrootid = DefaultScreen(_x.display);
230 xrootwin = DefaultRootWindow(_x.display);
232 /*
233 * Figure out underlying screen format.
234 */
235 if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
236 || XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
237 _x.vis = xvi.visual;
238 _x.depth = 16;
240 else
241 if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
242 || XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
243 _x.vis = xvi.visual;
244 _x.depth = 15;
246 else
247 if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
248 || XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
249 _x.vis = xvi.visual;
250 _x.depth = 24;
252 else
253 if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
254 || XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
255 if(_x.depth > 8){
256 werrstr("can't deal with colormapped depth %d screens",
257 _x.depth);
258 goto err0;
260 _x.vis = xvi.visual;
261 _x.depth = 8;
263 else{
264 _x.depth = DefaultDepth(_x.display, xrootid);
265 if(_x.depth != 8){
266 werrstr("can't understand depth %d screen", _x.depth);
267 goto err0;
269 _x.vis = DefaultVisual(_x.display, xrootid);
272 if(DefaultDepth(_x.display, xrootid) == _x.depth)
273 _x.usetable = 1;
275 /*
276 * _x.depth is only the number of significant pixel bits,
277 * not the total number of pixel bits. We need to walk the
278 * display list to find how many actual bits are used
279 * per pixel.
280 */
281 _x.chan = 0;
282 pfmt = XListPixmapFormats(_x.display, &n);
283 for(i=0; i<n; i++){
284 if(pfmt[i].depth == _x.depth){
285 switch(pfmt[i].bits_per_pixel){
286 case 1: /* untested */
287 _x.chan = GREY1;
288 break;
289 case 2: /* untested */
290 _x.chan = GREY2;
291 break;
292 case 4: /* untested */
293 _x.chan = GREY4;
294 break;
295 case 8:
296 _x.chan = CMAP8;
297 break;
298 case 15:
299 _x.chan = RGB15;
300 break;
301 case 16: /* how to tell RGB15? */
302 _x.chan = RGB16;
303 break;
304 case 24: /* untested (impossible?) */
305 _x.chan = RGB24;
306 break;
307 case 32:
308 _x.chan = XRGB32;
309 break;
313 if(_x.chan == 0){
314 werrstr("could not determine screen pixel format");
315 goto err0;
318 /*
319 * Set up color map if necessary.
320 */
321 xscreen = DefaultScreenOfDisplay(_x.display);
322 _x.cmap = DefaultColormapOfScreen(xscreen);
323 if(_x.vis->class != StaticColor){
324 plan9cmap();
325 setupcmap(xrootwin);
328 /*
329 * We get to choose the initial rectangle size.
330 * This is arbitrary. In theory we should read the
331 * command line and allow the traditional X options.
332 */
333 mask = 0;
334 x = 0;
335 y = 0;
336 if(winsize){
337 if(parsewinsize(winsize, &r, &havemin) < 0)
338 sysfatal("%r");
339 }else{
340 /*
341 * Parse the various X resources. Thanks to Peter Canning.
342 */
343 char *screen_resources, *display_resources, *geom,
344 *geomrestype, *home, *file;
345 XrmDatabase database;
346 XrmValue geomres;
348 database = XrmGetDatabase(_x.display);
349 screen_resources = XScreenResourceString(xscreen);
350 if(screen_resources != nil){
351 XrmCombineDatabase(XrmGetStringDatabase(screen_resources), &database, False);
352 XFree(screen_resources);
355 display_resources = XResourceManagerString(_x.display);
356 if(display_resources == nil){
357 home = getenv("HOME");
358 if(home!=nil && (file=smprint("%s/.Xdefaults", home)) != nil){
359 XrmCombineFileDatabase(file, &database, False);
360 free(file);
362 free(home);
363 }else
364 XrmCombineDatabase(XrmGetStringDatabase(display_resources), &database, False);
366 geom = smprint("%s.geometry", label);
367 if(geom && XrmGetResource(database, geom, nil, &geomrestype, &geomres))
368 mask = XParseGeometry(geomres.addr, &x, &y, (uint*)&width, (uint*)&height);
369 free(geom);
371 if((mask & WidthValue) && (mask & HeightValue)){
372 r = Rect(0, 0, width, height);
373 }else{
374 r = Rect(0, 0, WidthOfScreen(xscreen)*3/4,
375 HeightOfScreen(xscreen)*3/4);
376 if(Dx(r) > Dy(r)*3/2)
377 r.max.x = r.min.x + Dy(r)*3/2;
378 if(Dy(r) > Dx(r)*3/2)
379 r.max.y = r.min.y + Dx(r)*3/2;
381 if(mask & XNegative){
382 x += WidthOfScreen(xscreen);
384 if(mask & YNegative){
385 y += HeightOfScreen(xscreen);
387 havemin = 0;
390 memset(&attr, 0, sizeof attr);
391 attr.colormap = _x.cmap;
392 attr.background_pixel = ~0;
393 attr.border_pixel = 0;
394 _x.drawable = XCreateWindow(
395 _x.display, /* display */
396 xrootwin, /* parent */
397 x, /* x */
398 y, /* y */
399 Dx(r), /* width */
400 Dy(r), /* height */
401 0, /* border width */
402 _x.depth, /* depth */
403 InputOutput, /* class */
404 _x.vis, /* visual */
405 /* valuemask */
406 CWBackPixel|CWBorderPixel|CWColormap,
407 &attr /* attributes (the above aren't?!) */
408 );
410 /*
411 * Label and other properties required by ICCCCM.
412 */
413 memset(&name, 0, sizeof name);
414 if(label == nil)
415 label = "pjw-face-here";
416 name.value = (uchar*)label;
417 name.encoding = XA_STRING;
418 name.format = 8;
419 name.nitems = strlen((char*)name.value);
421 memset(&normalhint, 0, sizeof normalhint);
422 normalhint.flags = PSize|PMaxSize;
423 if(winsize){
424 normalhint.flags &= ~PSize;
425 normalhint.flags |= USSize;
426 normalhint.width = Dx(r);
427 normalhint.height = Dy(r);
428 }else{
429 if((mask & WidthValue) && (mask & HeightValue)){
430 normalhint.flags &= ~PSize;
431 normalhint.flags |= USSize;
432 normalhint.width = width;
433 normalhint.height = height;
435 if((mask & WidthValue) && (mask & HeightValue)){
436 normalhint.flags |= USPosition;
437 normalhint.x = x;
438 normalhint.y = y;
442 normalhint.max_width = WidthOfScreen(xscreen);
443 normalhint.max_height = HeightOfScreen(xscreen);
445 memset(&hint, 0, sizeof hint);
446 hint.flags = InputHint|StateHint;
447 hint.input = 1;
448 hint.initial_state = NormalState;
450 memset(&classhint, 0, sizeof classhint);
451 classhint.res_name = label;
452 classhint.res_class = label;
454 argv[0] = label;
455 argv[1] = nil;
457 XSetWMProperties(
458 _x.display, /* display */
459 _x.drawable, /* window */
460 &name, /* XA_WM_NAME property */
461 &name, /* XA_WM_ICON_NAME property */
462 argv, /* XA_WM_COMMAND */
463 1, /* argc */
464 &normalhint, /* XA_WM_NORMAL_HINTS */
465 &hint, /* XA_WM_HINTS */
466 &classhint /* XA_WM_CLASSHINTS */
467 );
468 XFlush(_x.display);
470 if(havemin){
471 XWindowChanges ch;
473 memset(&ch, 0, sizeof ch);
474 ch.x = r.min.x;
475 ch.y = r.min.y;
476 XConfigureWindow(_x.display, _x.drawable, CWX|CWY, &ch);
477 /*
478 * Must pretend origin is 0,0 for X.
479 */
480 r = Rect(0,0,Dx(r),Dy(r));
482 /*
483 * Look up clipboard atom.
484 */
485 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", False);
486 _x.utf8string = XInternAtom(_x.display, "UTF8_STRING", False);
487 _x.targets = XInternAtom(_x.display, "TARGETS", False);
488 _x.text = XInternAtom(_x.display, "TEXT", False);
489 _x.compoundtext = XInternAtom(_x.display, "COMPOUND_TEXT", False);
490 _x.takefocus = XInternAtom(_x.display, "WM_TAKE_FOCUS", False);
491 _x.losefocus = XInternAtom(_x.display, "_9WM_LOSE_FOCUS", False);
492 _x.wmprotos = XInternAtom(_x.display, "WM_PROTOCOLS", False);
494 atoms[0] = _x.takefocus;
495 atoms[1] = _x.losefocus;
496 XChangeProperty(_x.display, _x.drawable, _x.wmprotos, XA_ATOM, 32,
497 PropModeReplace, (uchar*)atoms, 2);
499 /*
500 * Put the window on the screen, check to see what size we actually got.
501 */
502 XMapWindow(_x.display, _x.drawable);
503 XSync(_x.display, False);
505 if(!XGetWindowAttributes(_x.display, _x.drawable, &wattr))
506 fprint(2, "XGetWindowAttributes failed\n");
507 else if(wattr.width && wattr.height){
508 if(wattr.width != Dx(r) || wattr.height != Dy(r)){
509 r.max.x = wattr.width;
510 r.max.y = wattr.height;
512 }else
513 fprint(2, "XGetWindowAttributes: bad attrs\n");
515 /*
516 * Allocate our local backing store.
517 */
518 _x.screenr = r;
519 _x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
520 _x.nextscreenpm = _x.screenpm;
521 _x.screenimage = _xallocmemimage(r, _x.chan, _x.screenpm);
523 /*
524 * Allocate some useful graphics contexts for the future.
525 */
526 _x.gcfill = xgc(_x.screenpm, FillSolid, -1);
527 _x.gccopy = xgc(_x.screenpm, -1, -1);
528 _x.gcsimplesrc = xgc(_x.screenpm, FillStippled, -1);
529 _x.gczero = xgc(_x.screenpm, -1, -1);
530 _x.gcreplsrc = xgc(_x.screenpm, FillTiled, -1);
532 pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
533 _x.gcfill0 = xgc(pmid, FillSolid, 0);
534 _x.gccopy0 = xgc(pmid, -1, -1);
535 _x.gcsimplesrc0 = xgc(pmid, FillStippled, -1);
536 _x.gczero0 = xgc(pmid, -1, -1);
537 _x.gcreplsrc0 = xgc(pmid, FillTiled, -1);
538 XFreePixmap(_x.display, pmid);
540 /*
541 * Lots of display connections for various procs.
542 */
543 _x.kbdcon = XOpenDisplay(NULL);
544 _x.mousecon = XOpenDisplay(NULL);
545 _x.snarfcon = XOpenDisplay(NULL);
547 if(0) fprint(2, "x: display=%p kbd=%p mouse=%p snarf=%p\n",
548 _x.display, _x.kbdcon, _x.mousecon, _x.snarfcon);
550 return _x.screenimage;
552 err0:
553 fprint(2, "%r\n");
554 /*
555 * Should do a better job of cleaning up here.
556 */
557 XCloseDisplay(_x.display);
558 return nil;
561 int
562 drawsetlabel(char *label)
564 XTextProperty name;
566 /*
567 * Label and other properties required by ICCCCM.
568 */
569 memset(&name, 0, sizeof name);
570 if(label == nil)
571 label = "pjw-face-here";
572 name.value = (uchar*)label;
573 name.encoding = XA_STRING;
574 name.format = 8;
575 name.nitems = strlen((char*)name.value);
577 XSetWMProperties(
578 _x.display, /* display */
579 _x.drawable, /* window */
580 &name, /* XA_WM_NAME property */
581 &name, /* XA_WM_ICON_NAME property */
582 nil, /* XA_WM_COMMAND */
583 0, /* argc */
584 nil, /* XA_WM_NORMAL_HINTS */
585 nil, /* XA_WM_HINTS */
586 nil /* XA_WM_CLASSHINTS */
587 );
588 XFlush(_x.display);
589 return 0;
592 /*
593 * Create a GC with a particular fill style and XXX.
594 * Disable generation of GraphicsExpose/NoExpose events in the GC.
595 */
596 static XGC
597 xgc(XDrawable d, int fillstyle, int foreground)
599 XGC gc;
600 XGCValues v;
602 memset(&v, 0, sizeof v);
603 v.function = GXcopy;
604 v.graphics_exposures = False;
605 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
606 if(fillstyle != -1)
607 XSetFillStyle(_x.display, gc, fillstyle);
608 if(foreground != -1)
609 XSetForeground(_x.display, gc, 0);
610 return gc;
614 /*
615 * Initialize map with the Plan 9 rgbv color map.
616 */
617 static void
618 plan9cmap(void)
620 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
621 static int once;
623 if(once)
624 return;
625 once = 1;
627 for(r=0; r!=4; r++)
628 for(g = 0; g != 4; g++)
629 for(b = 0; b!=4; b++)
630 for(v = 0; v!=4; v++){
631 den=r;
632 if(g > den)
633 den=g;
634 if(b > den)
635 den=b;
636 /* divide check -- pick grey shades */
637 if(den==0)
638 cr=cg=cb=v*17;
639 else {
640 num=17*(4*den+v);
641 cr=r*num/den;
642 cg=g*num/den;
643 cb=b*num/den;
645 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
646 _x.map[idx].red = cr*0x0101;
647 _x.map[idx].green = cg*0x0101;
648 _x.map[idx].blue = cb*0x0101;
649 _x.map[idx].pixel = idx;
650 _x.map[idx].flags = DoRed|DoGreen|DoBlue;
652 v7 = v >> 1;
653 idx7 = r*32 + v7*16 + g*4 + b;
654 if((v & 1) == v7){
655 _x.map7to8[idx7][0] = idx;
656 if(den == 0) { /* divide check -- pick grey shades */
657 cr = ((255.0/7.0)*v7)+0.5;
658 cg = cr;
659 cb = cr;
661 else {
662 num=17*15*(4*den+v7*2)/14;
663 cr=r*num/den;
664 cg=g*num/den;
665 cb=b*num/den;
667 _x.map7[idx7].red = cr*0x0101;
668 _x.map7[idx7].green = cg*0x0101;
669 _x.map7[idx7].blue = cb*0x0101;
670 _x.map7[idx7].pixel = idx7;
671 _x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
673 else
674 _x.map7to8[idx7][1] = idx;
678 /*
679 * Initialize and install the rgbv color map as a private color map
680 * for this application. It gets the best colors when it has the
681 * cursor focus.
683 * We always choose the best depth possible, but that might not
684 * be the default depth. On such "suboptimal" systems, we have to allocate an
685 * empty color map anyway, according to Axel Belinfante.
686 */
687 static int
688 setupcmap(XWindow w)
690 char buf[30];
691 int i;
692 u32int p, pp;
693 XColor c;
695 if(_x.depth <= 1)
696 return 0;
698 if(_x.depth >= 24) {
699 if(_x.usetable == 0)
700 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
702 /*
703 * The pixel value returned from XGetPixel needs to
704 * be converted to RGB so we can call rgb2cmap()
705 * to translate between 24 bit X and our color. Unfortunately,
706 * the return value appears to be display server endian
707 * dependant. Therefore, we run some heuristics to later
708 * determine how to mask the int value correctly.
709 * Yeah, I know we can look at _x.vis->byte_order but
710 * some displays say MSB even though they run on LSB.
711 * Besides, this is more anal.
712 */
713 c = _x.map[19]; /* known to have different R, G, B values */
714 if(!XAllocColor(_x.display, _x.cmap, &c)){
715 werrstr("XAllocColor: %r");
716 return -1;
718 p = c.pixel;
719 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
720 if(pp != _x.map[19].pixel) {
721 /* check if endian is other way */
722 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
723 if(pp != _x.map[19].pixel){
724 werrstr("cannot detect X server byte order");
725 return -1;
728 switch(_x.chan){
729 case RGB24:
730 _x.chan = BGR24;
731 break;
732 case XRGB32:
733 _x.chan = XBGR32;
734 break;
735 default:
736 werrstr("cannot byteswap channel %s",
737 chantostr(buf, _x.chan));
738 break;
741 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
742 /*
743 * Do nothing. We have no way to express a
744 * mixed-endian 16-bit screen, so pretend they don't exist.
745 */
746 if(_x.usetable == 0)
747 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
748 }else if(_x.vis->class == PseudoColor){
749 if(_x.usetable == 0){
750 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll);
751 XStoreColors(_x.display, _x.cmap, _x.map, 256);
752 for(i = 0; i < 256; i++){
753 _x.tox11[i] = i;
754 _x.toplan9[i] = i;
756 }else{
757 for(i = 0; i < 128; i++){
758 c = _x.map7[i];
759 if(!XAllocColor(_x.display, _x.cmap, &c)){
760 werrstr("can't allocate colors in 7-bit map");
761 return -1;
763 _x.tox11[_x.map7to8[i][0]] = c.pixel;
764 _x.tox11[_x.map7to8[i][1]] = c.pixel;
765 _x.toplan9[c.pixel] = _x.map7to8[i][0];
768 }else{
769 werrstr("unsupported visual class %d", _x.vis->class);
770 return -1;
772 return 0;
775 void
776 _flushmemscreen(Rectangle r)
778 if(_x.nextscreenpm != _x.screenpm){
779 qlock(&_x.screenlock);
780 XSync(_x.display, False);
781 XFreePixmap(_x.display, _x.screenpm);
782 _x.screenpm = _x.nextscreenpm;
783 qunlock(&_x.screenlock);
786 if(r.min.x >= r.max.x || r.min.y >= r.max.y)
787 return;
788 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
789 Dx(r), Dy(r), r.min.x, r.min.y);
790 XFlush(_x.display);
793 void
794 _xexpose(XEvent *e, XDisplay *xd)
796 XExposeEvent *xe;
797 Rectangle r;
799 qlock(&_x.screenlock);
800 if(_x.screenpm != _x.nextscreenpm){
801 qunlock(&_x.screenlock);
802 return;
804 xe = (XExposeEvent*)e;
805 r.min.x = xe->x;
806 r.min.y = xe->y;
807 r.max.x = xe->x+xe->width;
808 r.max.y = xe->y+xe->height;
809 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
810 Dx(r), Dy(r), r.min.x, r.min.y);
811 XSync(xd, False);
812 qunlock(&_x.screenlock);
815 int
816 _xdestroy(XEvent *e, XDisplay *xd)
818 XDestroyWindowEvent *xe;
820 xe = (XDestroyWindowEvent*)e;
821 if(xe->window == _x.drawable){
822 _x.destroyed = 1;
823 return 1;
825 return 0;
828 int
829 _xconfigure(XEvent *e, XDisplay *xd)
831 Rectangle r;
832 XConfigureEvent *xe = (XConfigureEvent*)e;
834 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
835 return 0;
836 if(xe->width==0 || xe->height==0)
837 fprint(2, "ignoring resize to %dx%d\n", xe->width, xe->height);
838 r = Rect(0, 0, xe->width, xe->height);
839 qlock(&_x.screenlock);
840 if(_x.screenpm != _x.nextscreenpm){
841 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
842 Dx(r), Dy(r), r.min.x, r.min.y);
843 XSync(xd, False);
845 qunlock(&_x.screenlock);
846 _x.newscreenr = r;
847 return 1;
850 static int
851 xreplacescreenimage(void)
853 Memimage *m;
854 XDrawable pixmap;
855 Rectangle r;
857 r = _x.newscreenr;
858 if(eqrect(_x.screenr, r))
859 return 0;
861 pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
862 m = _xallocmemimage(r, _x.chan, pixmap);
863 if(_x.nextscreenpm != _x.screenpm)
864 XFreePixmap(_x.display, _x.nextscreenpm);
865 _x.nextscreenpm = pixmap;
866 _x.screenr = r;
867 _drawreplacescreenimage(m);
868 return 1;
871 static int
872 parsewinsize(char *s, Rectangle *r, int *havemin)
874 char c, *os;
875 int i, j, k, l;
877 os = s;
878 *havemin = 0;
879 *r = Rect(0,0,0,0);
880 if(!isdigit((uchar)*s))
881 goto oops;
882 i = strtol(s, &s, 0);
883 if(*s == 'x'){
884 s++;
885 if(!isdigit((uchar)*s))
886 goto oops;
887 j = strtol(s, &s, 0);
888 r->max.x = i;
889 r->max.y = j;
890 if(*s == 0)
891 return 0;
892 if(*s != '@')
893 goto oops;
895 s++;
896 if(!isdigit((uchar)*s))
897 goto oops;
898 i = strtol(s, &s, 0);
899 if(*s != ',' && *s != ' ')
900 goto oops;
901 s++;
902 if(!isdigit((uchar)*s))
903 goto oops;
904 j = strtol(s, &s, 0);
905 if(*s != 0)
906 goto oops;
907 *r = rectaddpt(*r, Pt(i,j));
908 *havemin = 1;
909 return 0;
912 c = *s;
913 if(c != ' ' && c != ',')
914 goto oops;
915 s++;
916 if(!isdigit((uchar)*s))
917 goto oops;
918 j = strtol(s, &s, 0);
919 if(*s != c)
920 goto oops;
921 s++;
922 if(!isdigit((uchar)*s))
923 goto oops;
924 k = strtol(s, &s, 0);
925 if(*s != c)
926 goto oops;
927 s++;
928 if(!isdigit((uchar)*s))
929 goto oops;
930 l = strtol(s, &s, 0);
931 if(*s != 0)
932 goto oops;
933 *r = Rect(i,j,k,l);
934 *havemin = 1;
935 return 0;
937 oops:
938 werrstr("bad syntax in window size '%s'", os);
939 return -1;