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->screenimage = getimage0(d, 0);
74 return d;
75 }
77 static Image*
78 getimage0(Display *d, Image *image)
79 {
80 char info[12*12+1];
81 uchar *a;
82 int n;
83 extern int _freeimage1(Image*);
85 /*
86 * If there's an old screen, it has id 0. The 'J' request below
87 * will try to install the new screen as id 0, so the old one
88 * must be freed first.
89 */
90 if(image){
91 _freeimage1(image);
92 memset(image, 0, sizeof(Image));
93 }
95 a = bufimage(d, 2);
96 a[0] = 'J';
97 a[1] = 'I';
98 if(flushimage(d, 0) < 0){
99 fprint(2, "cannot read screen info: %r\n");
100 abort();
103 n = _drawmsgread(d, info, sizeof info);
104 if(n != 12*12){
105 fprint(2, "short screen info\n");
106 abort();
109 if(image == nil){
110 image = mallocz(sizeof(Image), 1);
111 if(image == nil){
112 fprint(2, "cannot allocate image: %r\n");
113 abort();
117 image->display = d;
118 image->id = 0;
119 image->chan = strtochan(info+2*12);
120 image->depth = chantodepth(image->chan);
121 image->repl = atoi(info+3*12);
122 image->r.min.x = atoi(info+4*12);
123 image->r.min.y = atoi(info+5*12);
124 image->r.max.x = atoi(info+6*12);
125 image->r.max.y = atoi(info+7*12);
126 image->clipr.min.x = atoi(info+8*12);
127 image->clipr.min.y = atoi(info+9*12);
128 image->clipr.max.x = atoi(info+10*12);
129 image->clipr.max.y = atoi(info+11*12);
130 return image;
133 int
134 getwindow(Display *d, int ref)
136 Image *i;
137 Image *oi;
139 if(_x.destroyed)
140 postnote(PNGROUP, getpgrp(), "hangup");
141 if(xreplacescreenimage() == 0)
142 return 0;
144 /*
145 * Libdraw promises not to change the value of "screen",
146 * so we have to reuse the image structure
147 * memory we already have.
148 */
149 oi = d->screenimage;
150 i = getimage0(d, oi);
151 screen = d->screenimage = d->image = i;
152 // fprint(2, "getwindow %p -> %p\n", oi, i);
153 return 0;
156 static int
157 xerror(XDisplay *d, XErrorEvent *e)
159 char buf[200];
161 if(e->request_code == 42) /* XSetInputFocus */
162 return 0;
163 if(e->request_code == 18) /* XChangeProperty */
164 return 0;
166 print("X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
167 e->error_code, e->request_code, e->minor_code, d);
168 XGetErrorText(d, e->error_code, buf, sizeof buf);
169 print("%s\n", buf);
170 return 0;
173 static int
174 xioerror(XDisplay *d)
176 //print("X I/O error\n");
177 sysfatal("X I/O error\n");
178 abort();
179 return -1;
183 static Memimage*
184 xattach(char *label)
186 char *argv[2], *disp;
187 int i, n, xrootid, havemin;
188 Rectangle r;
189 XClassHint classhint;
190 XDrawable pmid;
191 XPixmapFormatValues *pfmt;
192 XScreen *xscreen;
193 XSetWindowAttributes attr;
194 XSizeHints normalhint;
195 XTextProperty name;
196 XVisualInfo xvi;
197 XWindow xrootwin;
198 XWindowAttributes wattr;
199 XWMHints hint;
200 Atom atoms[2];
202 /*
203 if(XInitThreads() == 0){
204 fprint(2, "XInitThreads failed\n");
205 abort();
207 */
209 /*
210 * Connect to X server.
211 */
212 _x.display = XOpenDisplay(NULL);
213 if(_x.display == nil){
214 disp = getenv("DISPLAY");
215 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
216 free(disp);
217 return nil;
219 XSetErrorHandler(xerror);
220 XSetIOErrorHandler(xioerror);
221 xrootid = DefaultScreen(_x.display);
222 xrootwin = DefaultRootWindow(_x.display);
224 /*
225 * Figure out underlying screen format.
226 */
227 if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
228 || XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
229 _x.vis = xvi.visual;
230 _x.depth = 16;
232 else
233 if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
234 || XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
235 _x.vis = xvi.visual;
236 _x.depth = 15;
238 else
239 if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
240 || XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
241 _x.vis = xvi.visual;
242 _x.depth = 24;
244 else
245 if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
246 || XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
247 if(_x.depth > 8){
248 werrstr("can't deal with colormapped depth %d screens",
249 _x.depth);
250 goto err0;
252 _x.vis = xvi.visual;
253 _x.depth = 8;
255 else{
256 _x.depth = DefaultDepth(_x.display, xrootid);
257 if(_x.depth != 8){
258 werrstr("can't understand depth %d screen", _x.depth);
259 goto err0;
261 _x.vis = DefaultVisual(_x.display, xrootid);
264 if(DefaultDepth(_x.display, xrootid) == _x.depth)
265 _x.usetable = 1;
267 /*
268 * _x.depth is only the number of significant pixel bits,
269 * not the total number of pixel bits. We need to walk the
270 * display list to find how many actual bits are used
271 * per pixel.
272 */
273 _x.chan = 0;
274 pfmt = XListPixmapFormats(_x.display, &n);
275 for(i=0; i<n; i++){
276 if(pfmt[i].depth == _x.depth){
277 switch(pfmt[i].bits_per_pixel){
278 case 1: /* untested */
279 _x.chan = GREY1;
280 break;
281 case 2: /* untested */
282 _x.chan = GREY2;
283 break;
284 case 4: /* untested */
285 _x.chan = GREY4;
286 break;
287 case 8:
288 _x.chan = CMAP8;
289 break;
290 case 15:
291 _x.chan = RGB15;
292 break;
293 case 16: /* how to tell RGB15? */
294 _x.chan = RGB16;
295 break;
296 case 24: /* untested (impossible?) */
297 _x.chan = RGB24;
298 break;
299 case 32:
300 _x.chan = XRGB32;
301 break;
305 if(_x.chan == 0){
306 werrstr("could not determine screen pixel format");
307 goto err0;
310 /*
311 * Set up color map if necessary.
312 */
313 xscreen = DefaultScreenOfDisplay(_x.display);
314 _x.cmap = DefaultColormapOfScreen(xscreen);
315 if(_x.vis->class != StaticColor){
316 plan9cmap();
317 setupcmap(xrootwin);
320 /*
321 * We get to choose the initial rectangle size.
322 * This is arbitrary. In theory we should read the
323 * command line and allow the traditional X options.
324 */
325 if(winsize){
326 if(parsewinsize(winsize, &r, &havemin) < 0)
327 sysfatal("%r");
328 }else{
329 r = Rect(0, 0, WidthOfScreen(xscreen)*2/3,
330 HeightOfScreen(xscreen)*2/3);
331 if(Dx(r) > Dy(r)*3/2)
332 r.max.x = r.min.x + Dy(r)*3/2;
333 if(Dy(r) > Dx(r)*3/2)
334 r.max.y = r.min.y + Dx(r)*3/2;
335 havemin = 0;
338 memset(&attr, 0, sizeof attr);
339 attr.colormap = _x.cmap;
340 attr.background_pixel = ~0;
341 attr.border_pixel = 0;
342 _x.drawable = XCreateWindow(
343 _x.display, /* display */
344 xrootwin, /* parent */
345 0, /* x */
346 0, /* y */
347 Dx(r), /* width */
348 Dy(r), /* height */
349 0, /* border width */
350 _x.depth, /* depth */
351 InputOutput, /* class */
352 _x.vis, /* visual */
353 /* valuemask */
354 CWBackPixel|CWBorderPixel|CWColormap,
355 &attr /* attributes (the above aren't?!) */
356 );
358 /*
359 * Label and other properties required by ICCCCM.
360 */
361 memset(&name, 0, sizeof name);
362 if(label == nil)
363 label = "pjw-face-here";
364 name.value = (uchar*)label;
365 name.encoding = XA_STRING;
366 name.format = 8;
367 name.nitems = strlen((char*)name.value);
369 memset(&normalhint, 0, sizeof normalhint);
370 normalhint.flags = PSize|PMaxSize;
371 if(winsize){
372 normalhint.flags &= ~PSize;
373 normalhint.flags |= USSize;
374 normalhint.width = Dx(r);
375 normalhint.height = Dy(r);
378 normalhint.max_width = WidthOfScreen(xscreen);
379 normalhint.max_height = HeightOfScreen(xscreen);
381 memset(&hint, 0, sizeof hint);
382 hint.flags = InputHint|StateHint;
383 hint.input = 1;
384 hint.initial_state = NormalState;
386 memset(&classhint, 0, sizeof classhint);
387 classhint.res_name = label;
388 classhint.res_class = label;
390 argv[0] = label;
391 argv[1] = nil;
393 XSetWMProperties(
394 _x.display, /* display */
395 _x.drawable, /* window */
396 &name, /* XA_WM_NAME property */
397 &name, /* XA_WM_ICON_NAME property */
398 argv, /* XA_WM_COMMAND */
399 1, /* argc */
400 &normalhint, /* XA_WM_NORMAL_HINTS */
401 &hint, /* XA_WM_HINTS */
402 &classhint /* XA_WM_CLASSHINTS */
403 );
404 XFlush(_x.display);
406 if(havemin){
407 XWindowChanges ch;
409 memset(&ch, 0, sizeof ch);
410 ch.x = r.min.x;
411 ch.y = r.min.y;
412 XConfigureWindow(_x.display, _x.drawable, CWX|CWY, &ch);
413 fprint(2, "havemin %d %d\n", r.min.x, r.min.y);
415 /*
416 * Look up clipboard atom.
417 */
418 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", False);
419 _x.utf8string = XInternAtom(_x.display, "UTF8_STRING", False);
420 _x.targets = XInternAtom(_x.display, "TARGETS", False);
421 _x.text = XInternAtom(_x.display, "TEXT", False);
422 _x.compoundtext = XInternAtom(_x.display, "COMPOUND_TEXT", False);
423 _x.takefocus = XInternAtom(_x.display, "WM_TAKE_FOCUS", False);
424 _x.losefocus = XInternAtom(_x.display, "_9WM_LOSE_FOCUS", False);
425 _x.wmprotos = XInternAtom(_x.display, "WM_PROTOCOLS", False);
427 atoms[0] = _x.takefocus;
428 atoms[1] = _x.losefocus;
429 XChangeProperty(_x.display, _x.drawable, _x.wmprotos, XA_ATOM, 32,
430 PropModeReplace, (uchar*)atoms, 2);
432 /*
433 * Put the window on the screen, check to see what size we actually got.
434 */
435 XMapWindow(_x.display, _x.drawable);
436 XSync(_x.display, False);
438 if(!XGetWindowAttributes(_x.display, _x.drawable, &wattr))
439 fprint(2, "XGetWindowAttributes failed\n");
440 else if(wattr.width && wattr.height){
441 if(wattr.width != Dx(r) || wattr.height != Dy(r)){
442 r.max.x = wattr.width;
443 r.max.y = wattr.height;
445 }else
446 fprint(2, "XGetWindowAttributes: bad attrs\n");
448 /*
449 * Allocate our local backing store.
450 */
451 _x.screenr = r;
452 _x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
453 _x.nextscreenpm = _x.screenpm;
454 _x.screenimage = _xallocmemimage(r, _x.chan, _x.screenpm);
456 /*
457 * Allocate some useful graphics contexts for the future.
458 */
459 _x.gcfill = xgc(_x.screenpm, FillSolid, -1);
460 _x.gccopy = xgc(_x.screenpm, -1, -1);
461 _x.gcsimplesrc = xgc(_x.screenpm, FillStippled, -1);
462 _x.gczero = xgc(_x.screenpm, -1, -1);
463 _x.gcreplsrc = xgc(_x.screenpm, FillTiled, -1);
465 pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
466 _x.gcfill0 = xgc(pmid, FillSolid, 0);
467 _x.gccopy0 = xgc(pmid, -1, -1);
468 _x.gcsimplesrc0 = xgc(pmid, FillStippled, -1);
469 _x.gczero0 = xgc(pmid, -1, -1);
470 _x.gcreplsrc0 = xgc(pmid, FillTiled, -1);
471 XFreePixmap(_x.display, pmid);
473 /*
474 * Lots of display connections for various procs.
475 */
476 _x.kbdcon = XOpenDisplay(NULL);
477 _x.mousecon = XOpenDisplay(NULL);
478 _x.snarfcon = XOpenDisplay(NULL);
480 if(0) fprint(2, "x: display=%p kbd=%p mouse=%p snarf=%p\n",
481 _x.display, _x.kbdcon, _x.mousecon, _x.snarfcon);
483 _x.black = xscreen->black_pixel;
484 _x.white = xscreen->white_pixel;
486 return _x.screenimage;
488 err0:
489 fprint(2, "%r\n");
490 /*
491 * Should do a better job of cleaning up here.
492 */
493 XCloseDisplay(_x.display);
494 return nil;
497 int
498 drawsetlabel(Display *d, char *label)
500 char *argv[2];
501 XClassHint classhint;
502 XTextProperty name;
504 /*
505 * Label and other properties required by ICCCCM.
506 */
507 memset(&name, 0, sizeof name);
508 if(label == nil)
509 label = "pjw-face-here";
510 name.value = (uchar*)label;
511 name.encoding = XA_STRING;
512 name.format = 8;
513 name.nitems = strlen((char*)name.value);
515 memset(&classhint, 0, sizeof classhint);
516 classhint.res_name = label;
517 classhint.res_class = label;
519 argv[0] = label;
520 argv[1] = nil;
522 XSetWMProperties(
523 _x.display, /* display */
524 _x.drawable, /* window */
525 &name, /* XA_WM_NAME property */
526 &name, /* XA_WM_ICON_NAME property */
527 argv, /* XA_WM_COMMAND */
528 1, /* argc */
529 nil, /* XA_WM_NORMAL_HINTS */
530 nil, /* XA_WM_HINTS */
531 &classhint /* XA_WM_CLASSHINTS */
532 );
533 XFlush(_x.display);
534 return 0;
537 /*
538 * Create a GC with a particular fill style and XXX.
539 * Disable generation of GraphicsExpose/NoExpose events in the GC.
540 */
541 static XGC
542 xgc(XDrawable d, int fillstyle, int foreground)
544 XGC gc;
545 XGCValues v;
547 memset(&v, 0, sizeof v);
548 v.function = GXcopy;
549 v.graphics_exposures = False;
550 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
551 if(fillstyle != -1)
552 XSetFillStyle(_x.display, gc, fillstyle);
553 if(foreground != -1)
554 XSetForeground(_x.display, gc, 0);
555 return gc;
559 /*
560 * Initialize map with the Plan 9 rgbv color map.
561 */
562 static void
563 plan9cmap(void)
565 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
566 static int once;
568 if(once)
569 return;
570 once = 1;
572 for(r=0; r!=4; r++)
573 for(g = 0; g != 4; g++)
574 for(b = 0; b!=4; b++)
575 for(v = 0; v!=4; v++){
576 den=r;
577 if(g > den)
578 den=g;
579 if(b > den)
580 den=b;
581 /* divide check -- pick grey shades */
582 if(den==0)
583 cr=cg=cb=v*17;
584 else {
585 num=17*(4*den+v);
586 cr=r*num/den;
587 cg=g*num/den;
588 cb=b*num/den;
590 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
591 _x.map[idx].red = cr*0x0101;
592 _x.map[idx].green = cg*0x0101;
593 _x.map[idx].blue = cb*0x0101;
594 _x.map[idx].pixel = idx;
595 _x.map[idx].flags = DoRed|DoGreen|DoBlue;
597 v7 = v >> 1;
598 idx7 = r*32 + v7*16 + g*4 + b;
599 if((v & 1) == v7){
600 _x.map7to8[idx7][0] = idx;
601 if(den == 0) { /* divide check -- pick grey shades */
602 cr = ((255.0/7.0)*v7)+0.5;
603 cg = cr;
604 cb = cr;
606 else {
607 num=17*15*(4*den+v7*2)/14;
608 cr=r*num/den;
609 cg=g*num/den;
610 cb=b*num/den;
612 _x.map7[idx7].red = cr*0x0101;
613 _x.map7[idx7].green = cg*0x0101;
614 _x.map7[idx7].blue = cb*0x0101;
615 _x.map7[idx7].pixel = idx7;
616 _x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
618 else
619 _x.map7to8[idx7][1] = idx;
623 /*
624 * Initialize and install the rgbv color map as a private color map
625 * for this application. It gets the best colors when it has the
626 * cursor focus.
628 * We always choose the best depth possible, but that might not
629 * be the default depth. On such "suboptimal" systems, we have to allocate an
630 * empty color map anyway, according to Axel Belinfante.
631 */
632 static int
633 setupcmap(XWindow w)
635 char buf[30];
636 int i;
637 u32int p, pp;
638 XColor c;
640 if(_x.depth <= 1)
641 return 0;
643 if(_x.depth >= 24) {
644 if(_x.usetable == 0)
645 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
647 /*
648 * The pixel value returned from XGetPixel needs to
649 * be converted to RGB so we can call rgb2cmap()
650 * to translate between 24 bit X and our color. Unfortunately,
651 * the return value appears to be display server endian
652 * dependant. Therefore, we run some heuristics to later
653 * determine how to mask the int value correctly.
654 * Yeah, I know we can look at _x.vis->byte_order but
655 * some displays say MSB even though they run on LSB.
656 * Besides, this is more anal.
657 */
658 c = _x.map[19]; /* known to have different R, G, B values */
659 if(!XAllocColor(_x.display, _x.cmap, &c)){
660 werrstr("XAllocColor: %r");
661 return -1;
663 p = c.pixel;
664 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
665 if(pp != _x.map[19].pixel) {
666 /* check if endian is other way */
667 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
668 if(pp != _x.map[19].pixel){
669 werrstr("cannot detect X server byte order");
670 return -1;
673 switch(_x.chan){
674 case RGB24:
675 _x.chan = BGR24;
676 break;
677 case XRGB32:
678 _x.chan = XBGR32;
679 break;
680 default:
681 werrstr("cannot byteswap channel %s",
682 chantostr(buf, _x.chan));
683 break;
686 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
687 /*
688 * Do nothing. We have no way to express a
689 * mixed-endian 16-bit screen, so pretend they don't exist.
690 */
691 if(_x.usetable == 0)
692 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
693 }else if(_x.vis->class == PseudoColor){
694 if(_x.usetable == 0){
695 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll);
696 XStoreColors(_x.display, _x.cmap, _x.map, 256);
697 for(i = 0; i < 256; i++){
698 _x.tox11[i] = i;
699 _x.toplan9[i] = i;
701 }else{
702 for(i = 0; i < 128; i++){
703 c = _x.map7[i];
704 if(!XAllocColor(_x.display, _x.cmap, &c)){
705 werrstr("can't allocate colors in 7-bit map");
706 return -1;
708 _x.tox11[_x.map7to8[i][0]] = c.pixel;
709 _x.tox11[_x.map7to8[i][1]] = c.pixel;
710 _x.toplan9[c.pixel] = _x.map7to8[i][0];
713 }else{
714 werrstr("unsupported visual class %d", _x.vis->class);
715 return -1;
717 return 0;
720 void
721 flushmemscreen(Rectangle r)
723 if(_x.nextscreenpm != _x.screenpm){
724 qlock(&_x.screenlock);
725 XSync(_x.display, False);
726 XFreePixmap(_x.display, _x.screenpm);
727 _x.screenpm = _x.nextscreenpm;
728 qunlock(&_x.screenlock);
731 if(r.min.x >= r.max.x || r.min.y >= r.max.y)
732 return;
733 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
734 Dx(r), Dy(r), r.min.x, r.min.y);
735 XFlush(_x.display);
738 void
739 _xexpose(XEvent *e, XDisplay *xd)
741 XExposeEvent *xe;
742 Rectangle r;
744 qlock(&_x.screenlock);
745 if(_x.screenpm != _x.nextscreenpm){
746 qunlock(&_x.screenlock);
747 return;
749 xe = (XExposeEvent*)e;
750 r.min.x = xe->x;
751 r.min.y = xe->y;
752 r.max.x = xe->x+xe->width;
753 r.max.y = xe->y+xe->height;
754 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
755 Dx(r), Dy(r), r.min.x, r.min.y);
756 XSync(xd, False);
757 qunlock(&_x.screenlock);
760 int
761 _xdestroy(XEvent *e, XDisplay *xd)
763 XDestroyWindowEvent *xe;
765 xe = (XDestroyWindowEvent*)e;
766 if(xe->window == _x.drawable){
767 _x.destroyed = 1;
768 return 1;
770 return 0;
773 int
774 _xconfigure(XEvent *e, XDisplay *xd)
776 Rectangle r;
777 XConfigureEvent *xe = (XConfigureEvent*)e;
779 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
780 return 0;
781 if(xe->width==0 || xe->height==0)
782 fprint(2, "ignoring resize to %dx%d\n", xe->width, xe->height);
783 r = Rect(0, 0, xe->width, xe->height);
784 qlock(&_x.screenlock);
785 if(_x.screenpm != _x.nextscreenpm){
786 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
787 Dx(r), Dy(r), r.min.x, r.min.y);
788 XSync(xd, False);
790 qunlock(&_x.screenlock);
791 _x.newscreenr = r;
792 return 1;
795 static int
796 xreplacescreenimage(void)
798 Memimage *m;
799 XDrawable pixmap;
800 Rectangle r;
802 r = _x.newscreenr;
803 if(eqrect(_x.screenr, r))
804 return 0;
806 pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
807 m = _xallocmemimage(r, _x.chan, pixmap);
808 if(_x.nextscreenpm != _x.screenpm)
809 XFreePixmap(_x.display, _x.nextscreenpm);
810 _x.nextscreenpm = pixmap;
811 _x.screenr = r;
812 _drawreplacescreenimage(m);
813 return 1;
816 static int
817 parsewinsize(char *s, Rectangle *r, int *havemin)
819 char c, *os;
820 int i, j, k, l;
822 os = s;
823 *havemin = 0;
824 *r = Rect(0,0,0,0);
825 if(!isdigit(*s))
826 goto oops;
827 i = strtol(s, &s, 0);
828 if(*s == 'x'){
829 s++;
830 if(!isdigit(*s))
831 goto oops;
832 j = strtol(s, &s, 0);
833 r->max.x = i;
834 r->max.y = j;
835 if(*s == 0)
836 return 0;
837 if(*s != '@')
838 goto oops;
840 s++;
841 if(!isdigit(*s))
842 goto oops;
843 i = strtol(s, &s, 0);
844 if(*s != ',' && *s != ' ')
845 goto oops;
846 s++;
847 if(!isdigit(*s))
848 goto oops;
849 j = strtol(s, &s, 0);
850 if(*s != 0)
851 goto oops;
852 *r = rectaddpt(*r, Pt(i,j));
853 *havemin = 1;
854 return 0;
857 c = *s;
858 if(c != ' ' && c != ',')
859 goto oops;
860 s++;
861 if(!isdigit(*s))
862 goto oops;
863 j = strtol(s, &s, 0);
864 if(*s != c)
865 goto oops;
866 s++;
867 if(!isdigit(*s))
868 goto oops;
869 k = strtol(s, &s, 0);
870 if(*s != c)
871 goto oops;
872 s++;
873 if(!isdigit(*s))
874 goto oops;
875 l = strtol(s, &s, 0);
876 if(*s != 0)
877 goto oops;
878 *r = Rect(i,j,k,l);
879 *havemin = 1;
880 return 0;
882 oops:
883 werrstr("bad syntax in window size '%s'", os);
884 return -1;