2 * Some of the stuff in this file is not X-dependent and should be elsewhere.
12 #include "x11-memdraw.h"
14 static int parsewinsize(char*, Rectangle*, int*);
16 static void plan9cmap(void);
17 static int setupcmap(XWindow);
18 static XGC xgc(XDrawable, int, int);
23 xerror(XDisplay *d, XErrorEvent *e)
27 if(e->request_code == 42) /* XSetInputFocus */
29 if(e->request_code == 18) /* XChangeProperty */
32 * BadDrawable happens in apps that get resized a LOT,
33 * e.g. when KDE is configured to resize continuously
34 * during a window drag.
36 if(e->error_code == 9) /* BadDrawable */
39 fprint(2, "X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
40 e->error_code, e->request_code, e->minor_code, d);
41 XGetErrorText(d, e->error_code, buf, sizeof buf);
42 fprint(2, "%s\n", buf);
49 /*print("X I/O error\n"); */
51 /*sysfatal("X I/O error\n");*/
58 _xattach(char *label, char *winsize)
61 int i, havemin, height, mask, n, width, x, xrootid, y;
65 XPixmapFormatValues *pfmt;
67 XSetWindowAttributes attr;
68 XSizeHints normalhint;
72 XWindowAttributes wattr;
77 if(XInitThreads() == 0){
78 fprint(2, "XInitThreads failed\n");
84 * Connect to X server.
86 _x.display = XOpenDisplay(NULL);
87 if(_x.display == nil){
88 disp = getenv("DISPLAY");
89 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
93 _x.fd = ConnectionNumber(_x.display);
94 XSetErrorHandler(xerror);
95 XSetIOErrorHandler(xioerror);
96 xrootid = DefaultScreen(_x.display);
97 xrootwin = DefaultRootWindow(_x.display);
100 * Figure out underlying screen format.
102 if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
103 || XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
108 if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
109 || XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
114 if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
115 || XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
120 if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
121 || XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
123 werrstr("can't deal with colormapped depth %d screens",
131 _x.depth = DefaultDepth(_x.display, xrootid);
133 werrstr("can't understand depth %d screen", _x.depth);
136 _x.vis = DefaultVisual(_x.display, xrootid);
139 if(DefaultDepth(_x.display, xrootid) == _x.depth)
143 * _x.depth is only the number of significant pixel bits,
144 * not the total number of pixel bits. We need to walk the
145 * display list to find how many actual bits are used
149 pfmt = XListPixmapFormats(_x.display, &n);
151 if(pfmt[i].depth == _x.depth){
152 switch(pfmt[i].bits_per_pixel){
153 case 1: /* untested */
156 case 2: /* untested */
159 case 4: /* untested */
168 case 16: /* how to tell RGB15? */
171 case 24: /* untested (impossible?) */
181 werrstr("could not determine screen pixel format");
186 * Set up color map if necessary.
188 xscreen = DefaultScreenOfDisplay(_x.display);
189 _x.cmap = DefaultColormapOfScreen(xscreen);
190 if(_x.vis->class != StaticColor){
196 * We get to choose the initial rectangle size.
197 * This is arbitrary. In theory we should read the
198 * command line and allow the traditional X options.
203 if(winsize && winsize[0]){
204 if(parsewinsize(winsize, &r, &havemin) < 0)
208 * Parse the various X resources. Thanks to Peter Canning.
210 char *screen_resources, *display_resources, *geom,
211 *geomrestype, *home, *file;
212 XrmDatabase database;
215 database = XrmGetDatabase(_x.display);
216 screen_resources = XScreenResourceString(xscreen);
217 if(screen_resources != nil){
218 XrmCombineDatabase(XrmGetStringDatabase(screen_resources), &database, False);
219 XFree(screen_resources);
222 display_resources = XResourceManagerString(_x.display);
223 if(display_resources == nil){
224 home = getenv("HOME");
225 if(home!=nil && (file=smprint("%s/.Xdefaults", home)) != nil){
226 XrmCombineFileDatabase(file, &database, False);
231 XrmCombineDatabase(XrmGetStringDatabase(display_resources), &database, False);
233 geom = smprint("%s.geometry", label);
234 if(geom && XrmGetResource(database, geom, nil, &geomrestype, &geomres))
235 mask = XParseGeometry(geomres.addr, &x, &y, (uint*)&width, (uint*)&height);
238 if((mask & WidthValue) && (mask & HeightValue)){
239 r = Rect(0, 0, width, height);
241 r = Rect(0, 0, WidthOfScreen(xscreen)*3/4,
242 HeightOfScreen(xscreen)*3/4);
243 if(Dx(r) > Dy(r)*3/2)
244 r.max.x = r.min.x + Dy(r)*3/2;
245 if(Dy(r) > Dx(r)*3/2)
246 r.max.y = r.min.y + Dx(r)*3/2;
248 if(mask & XNegative){
249 x += WidthOfScreen(xscreen);
251 if(mask & YNegative){
252 y += HeightOfScreen(xscreen);
256 screenrect = Rect(0, 0, WidthOfScreen(xscreen), HeightOfScreen(xscreen));
259 memset(&attr, 0, sizeof attr);
260 attr.colormap = _x.cmap;
261 attr.background_pixel = ~0;
262 attr.border_pixel = 0;
263 _x.drawable = XCreateWindow(
264 _x.display, /* display */
265 xrootwin, /* parent */
270 0, /* border width */
271 _x.depth, /* depth */
272 InputOutput, /* class */
275 CWBackPixel|CWBorderPixel|CWColormap,
276 &attr /* attributes (the above aren't?!) */
280 * Label and other properties required by ICCCCM.
282 memset(&name, 0, sizeof name);
284 label = "pjw-face-here";
285 name.value = (uchar*)label;
286 name.encoding = XA_STRING;
288 name.nitems = strlen((char*)name.value);
290 memset(&normalhint, 0, sizeof normalhint);
291 normalhint.flags = PSize|PMaxSize;
292 if(winsize && winsize[0]){
293 normalhint.flags &= ~PSize;
294 normalhint.flags |= USSize;
295 normalhint.width = Dx(r);
296 normalhint.height = Dy(r);
298 if((mask & WidthValue) && (mask & HeightValue)){
299 normalhint.flags &= ~PSize;
300 normalhint.flags |= USSize;
301 normalhint.width = width;
302 normalhint.height = height;
304 if((mask & WidthValue) && (mask & HeightValue)){
305 normalhint.flags |= USPosition;
311 normalhint.max_width = WidthOfScreen(xscreen);
312 normalhint.max_height = HeightOfScreen(xscreen);
314 memset(&hint, 0, sizeof hint);
315 hint.flags = InputHint|StateHint;
317 hint.initial_state = NormalState;
319 memset(&classhint, 0, sizeof classhint);
320 classhint.res_name = label;
321 classhint.res_class = label;
327 _x.display, /* display */
328 _x.drawable, /* window */
329 &name, /* XA_WM_NAME property */
330 &name, /* XA_WM_ICON_NAME property */
331 argv, /* XA_WM_COMMAND */
333 &normalhint, /* XA_WM_NORMAL_HINTS */
334 &hint, /* XA_WM_HINTS */
335 &classhint /* XA_WM_CLASSHINTS */
342 memset(&ch, 0, sizeof ch);
345 XConfigureWindow(_x.display, _x.drawable, CWX|CWY, &ch);
347 * Must pretend origin is 0,0 for X.
349 r = Rect(0,0,Dx(r),Dy(r));
352 * Look up clipboard atom.
354 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", False);
355 _x.utf8string = XInternAtom(_x.display, "UTF8_STRING", False);
356 _x.targets = XInternAtom(_x.display, "TARGETS", False);
357 _x.text = XInternAtom(_x.display, "TEXT", False);
358 _x.compoundtext = XInternAtom(_x.display, "COMPOUND_TEXT", False);
359 _x.takefocus = XInternAtom(_x.display, "WM_TAKE_FOCUS", False);
360 _x.losefocus = XInternAtom(_x.display, "_9WM_LOSE_FOCUS", False);
361 _x.wmprotos = XInternAtom(_x.display, "WM_PROTOCOLS", False);
363 atoms[0] = _x.takefocus;
364 atoms[1] = _x.losefocus;
365 XChangeProperty(_x.display, _x.drawable, _x.wmprotos, XA_ATOM, 32,
366 PropModeReplace, (uchar*)atoms, 2);
369 * Put the window on the screen, check to see what size we actually got.
371 XMapWindow(_x.display, _x.drawable);
372 XSync(_x.display, False);
374 if(!XGetWindowAttributes(_x.display, _x.drawable, &wattr))
375 fprint(2, "XGetWindowAttributes failed\n");
376 else if(wattr.width && wattr.height){
377 if(wattr.width != Dx(r) || wattr.height != Dy(r)){
378 r.max.x = wattr.width;
379 r.max.y = wattr.height;
382 fprint(2, "XGetWindowAttributes: bad attrs\n");
385 * Allocate our local backing store.
388 _x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
389 _x.nextscreenpm = _x.screenpm;
390 _x.screenimage = _xallocmemimage(r, _x.chan, _x.screenpm);
393 * Allocate some useful graphics contexts for the future.
395 _x.gcfill = xgc(_x.screenpm, FillSolid, -1);
396 _x.gccopy = xgc(_x.screenpm, -1, -1);
397 _x.gcsimplesrc = xgc(_x.screenpm, FillStippled, -1);
398 _x.gczero = xgc(_x.screenpm, -1, -1);
399 _x.gcreplsrc = xgc(_x.screenpm, FillTiled, -1);
401 pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
402 _x.gcfill0 = xgc(pmid, FillSolid, 0);
403 _x.gccopy0 = xgc(pmid, -1, -1);
404 _x.gcsimplesrc0 = xgc(pmid, FillStippled, -1);
405 _x.gczero0 = xgc(pmid, -1, -1);
406 _x.gcreplsrc0 = xgc(pmid, FillTiled, -1);
407 XFreePixmap(_x.display, pmid);
409 return _x.screenimage;
413 * Should do a better job of cleaning up here.
415 XCloseDisplay(_x.display);
420 _xsetlabel(char *label)
425 * Label and other properties required by ICCCCM.
427 memset(&name, 0, sizeof name);
429 label = "pjw-face-here";
430 name.value = (uchar*)label;
431 name.encoding = XA_STRING;
433 name.nitems = strlen((char*)name.value);
436 _x.display, /* display */
437 _x.drawable, /* window */
438 &name, /* XA_WM_NAME property */
439 &name, /* XA_WM_ICON_NAME property */
440 nil, /* XA_WM_COMMAND */
442 nil, /* XA_WM_NORMAL_HINTS */
443 nil, /* XA_WM_HINTS */
444 nil /* XA_WM_CLASSHINTS */
451 * Create a GC with a particular fill style and XXX.
452 * Disable generation of GraphicsExpose/NoExpose events in the GC.
455 xgc(XDrawable d, int fillstyle, int foreground)
460 memset(&v, 0, sizeof v);
462 v.graphics_exposures = False;
463 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
465 XSetFillStyle(_x.display, gc, fillstyle);
467 XSetForeground(_x.display, gc, 0);
473 * Initialize map with the Plan 9 rgbv color map.
478 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
486 for(g = 0; g != 4; g++)
487 for(b = 0; b!=4; b++)
488 for(v = 0; v!=4; v++){
494 /* divide check -- pick grey shades */
503 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
504 _x.map[idx].red = cr*0x0101;
505 _x.map[idx].green = cg*0x0101;
506 _x.map[idx].blue = cb*0x0101;
507 _x.map[idx].pixel = idx;
508 _x.map[idx].flags = DoRed|DoGreen|DoBlue;
511 idx7 = r*32 + v7*16 + g*4 + b;
513 _x.map7to8[idx7][0] = idx;
514 if(den == 0) { /* divide check -- pick grey shades */
515 cr = ((255.0/7.0)*v7)+0.5;
520 num=17*15*(4*den+v7*2)/14;
525 _x.map7[idx7].red = cr*0x0101;
526 _x.map7[idx7].green = cg*0x0101;
527 _x.map7[idx7].blue = cb*0x0101;
528 _x.map7[idx7].pixel = idx7;
529 _x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
532 _x.map7to8[idx7][1] = idx;
537 * Initialize and install the rgbv color map as a private color map
538 * for this application. It gets the best colors when it has the
541 * We always choose the best depth possible, but that might not
542 * be the default depth. On such "suboptimal" systems, we have to allocate an
543 * empty color map anyway, according to Axel Belinfante.
558 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
561 * The pixel value returned from XGetPixel needs to
562 * be converted to RGB so we can call rgb2cmap()
563 * to translate between 24 bit X and our color. Unfortunately,
564 * the return value appears to be display server endian
565 * dependant. Therefore, we run some heuristics to later
566 * determine how to mask the int value correctly.
567 * Yeah, I know we can look at _x.vis->byte_order but
568 * some displays say MSB even though they run on LSB.
569 * Besides, this is more anal.
571 c = _x.map[19]; /* known to have different R, G, B values */
572 if(!XAllocColor(_x.display, _x.cmap, &c)){
573 werrstr("XAllocColor: %r");
577 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
578 if(pp != _x.map[19].pixel) {
579 /* check if endian is other way */
580 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
581 if(pp != _x.map[19].pixel){
582 werrstr("cannot detect X server byte order");
594 werrstr("cannot byteswap channel %s",
595 chantostr(buf, _x.chan));
599 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
601 * Do nothing. We have no way to express a
602 * mixed-endian 16-bit screen, so pretend they don't exist.
605 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
606 }else if(_x.vis->class == PseudoColor){
607 if(_x.usetable == 0){
608 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll);
609 XStoreColors(_x.display, _x.cmap, _x.map, 256);
610 for(i = 0; i < 256; i++){
615 for(i = 0; i < 128; i++){
617 if(!XAllocColor(_x.display, _x.cmap, &c)){
618 werrstr("can't allocate colors in 7-bit map");
621 _x.tox11[_x.map7to8[i][0]] = c.pixel;
622 _x.tox11[_x.map7to8[i][1]] = c.pixel;
623 _x.toplan9[c.pixel] = _x.map7to8[i][0];
627 werrstr("unsupported visual class %d", _x.vis->class);
634 _flushmemscreen(Rectangle r)
636 if(_x.nextscreenpm != _x.screenpm){
637 qlock(&_x.screenlock);
638 XSync(_x.display, False);
639 XFreePixmap(_x.display, _x.screenpm);
640 _x.screenpm = _x.nextscreenpm;
641 qunlock(&_x.screenlock);
644 if(r.min.x >= r.max.x || r.min.y >= r.max.y)
646 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
647 Dx(r), Dy(r), r.min.x, r.min.y);
657 qlock(&_x.screenlock);
658 if(_x.screenpm != _x.nextscreenpm){
659 qunlock(&_x.screenlock);
662 xe = (XExposeEvent*)e;
665 r.max.x = xe->x+xe->width;
666 r.max.y = xe->y+xe->height;
667 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
668 Dx(r), Dy(r), r.min.x, r.min.y);
669 XSync(_x.display, False);
670 qunlock(&_x.screenlock);
676 XDestroyWindowEvent *xe;
678 xe = (XDestroyWindowEvent*)e;
679 if(xe->window == _x.drawable){
687 _xconfigure(XEvent *e)
690 XConfigureEvent *xe = (XConfigureEvent*)e;
695 if(XTranslateCoordinates(_x.display, _x.drawable, DefaultRootWindow(_x.display), 0, 0, &rx, &ry, &w))
696 windowrect = Rect(rx, ry, rx+xe->width, ry+xe->height);
699 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
701 r = Rect(0, 0, xe->width, xe->height);
703 qlock(&_x.screenlock);
704 if(_x.screenpm != _x.nextscreenpm){
705 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
706 Dx(r), Dy(r), r.min.x, r.min.y);
707 XSync(_x.display, False);
709 qunlock(&_x.screenlock);
715 _xreplacescreenimage(void)
723 pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
724 m = _xallocmemimage(r, _x.chan, pixmap);
725 if(_x.nextscreenpm != _x.screenpm)
726 XFreePixmap(_x.display, _x.nextscreenpm);
727 _x.nextscreenpm = pixmap;
729 _drawreplacescreenimage(m);
734 parsewinsize(char *s, Rectangle *r, int *havemin)
742 if(!isdigit((uchar)*s))
744 i = strtol(s, &s, 0);
747 if(!isdigit((uchar)*s))
749 j = strtol(s, &s, 0);
758 if(!isdigit((uchar)*s))
760 i = strtol(s, &s, 0);
761 if(*s != ',' && *s != ' ')
764 if(!isdigit((uchar)*s))
766 j = strtol(s, &s, 0);
769 *r = rectaddpt(*r, Pt(i,j));
775 if(c != ' ' && c != ',')
778 if(!isdigit((uchar)*s))
780 j = strtol(s, &s, 0);
784 if(!isdigit((uchar)*s))
786 k = strtol(s, &s, 0);
790 if(!isdigit((uchar)*s))
792 l = strtol(s, &s, 0);
800 werrstr("bad syntax in window size '%s'", os);