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);
257 memset(&attr, 0, sizeof attr);
258 attr.colormap = _x.cmap;
259 attr.background_pixel = ~0;
260 attr.border_pixel = 0;
261 _x.drawable = XCreateWindow(
262 _x.display, /* display */
263 xrootwin, /* parent */
268 0, /* border width */
269 _x.depth, /* depth */
270 InputOutput, /* class */
273 CWBackPixel|CWBorderPixel|CWColormap,
274 &attr /* attributes (the above aren't?!) */
278 * Label and other properties required by ICCCCM.
280 memset(&name, 0, sizeof name);
282 label = "pjw-face-here";
283 name.value = (uchar*)label;
284 name.encoding = XA_STRING;
286 name.nitems = strlen((char*)name.value);
288 memset(&normalhint, 0, sizeof normalhint);
289 normalhint.flags = PSize|PMaxSize;
290 if(winsize && winsize[0]){
291 normalhint.flags &= ~PSize;
292 normalhint.flags |= USSize;
293 normalhint.width = Dx(r);
294 normalhint.height = Dy(r);
296 if((mask & WidthValue) && (mask & HeightValue)){
297 normalhint.flags &= ~PSize;
298 normalhint.flags |= USSize;
299 normalhint.width = width;
300 normalhint.height = height;
302 if((mask & WidthValue) && (mask & HeightValue)){
303 normalhint.flags |= USPosition;
309 normalhint.max_width = WidthOfScreen(xscreen);
310 normalhint.max_height = HeightOfScreen(xscreen);
312 memset(&hint, 0, sizeof hint);
313 hint.flags = InputHint|StateHint;
315 hint.initial_state = NormalState;
317 memset(&classhint, 0, sizeof classhint);
318 classhint.res_name = label;
319 classhint.res_class = label;
325 _x.display, /* display */
326 _x.drawable, /* window */
327 &name, /* XA_WM_NAME property */
328 &name, /* XA_WM_ICON_NAME property */
329 argv, /* XA_WM_COMMAND */
331 &normalhint, /* XA_WM_NORMAL_HINTS */
332 &hint, /* XA_WM_HINTS */
333 &classhint /* XA_WM_CLASSHINTS */
340 memset(&ch, 0, sizeof ch);
343 XConfigureWindow(_x.display, _x.drawable, CWX|CWY, &ch);
345 * Must pretend origin is 0,0 for X.
347 r = Rect(0,0,Dx(r),Dy(r));
350 * Look up clipboard atom.
352 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", False);
353 _x.utf8string = XInternAtom(_x.display, "UTF8_STRING", False);
354 _x.targets = XInternAtom(_x.display, "TARGETS", False);
355 _x.text = XInternAtom(_x.display, "TEXT", False);
356 _x.compoundtext = XInternAtom(_x.display, "COMPOUND_TEXT", False);
357 _x.takefocus = XInternAtom(_x.display, "WM_TAKE_FOCUS", False);
358 _x.losefocus = XInternAtom(_x.display, "_9WM_LOSE_FOCUS", False);
359 _x.wmprotos = XInternAtom(_x.display, "WM_PROTOCOLS", False);
361 atoms[0] = _x.takefocus;
362 atoms[1] = _x.losefocus;
363 XChangeProperty(_x.display, _x.drawable, _x.wmprotos, XA_ATOM, 32,
364 PropModeReplace, (uchar*)atoms, 2);
367 * Put the window on the screen, check to see what size we actually got.
369 XMapWindow(_x.display, _x.drawable);
370 XSync(_x.display, False);
372 if(!XGetWindowAttributes(_x.display, _x.drawable, &wattr))
373 fprint(2, "XGetWindowAttributes failed\n");
374 else if(wattr.width && wattr.height){
375 if(wattr.width != Dx(r) || wattr.height != Dy(r)){
376 r.max.x = wattr.width;
377 r.max.y = wattr.height;
380 fprint(2, "XGetWindowAttributes: bad attrs\n");
383 * Allocate our local backing store.
386 _x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
387 _x.nextscreenpm = _x.screenpm;
388 _x.screenimage = _xallocmemimage(r, _x.chan, _x.screenpm);
391 * Allocate some useful graphics contexts for the future.
393 _x.gcfill = xgc(_x.screenpm, FillSolid, -1);
394 _x.gccopy = xgc(_x.screenpm, -1, -1);
395 _x.gcsimplesrc = xgc(_x.screenpm, FillStippled, -1);
396 _x.gczero = xgc(_x.screenpm, -1, -1);
397 _x.gcreplsrc = xgc(_x.screenpm, FillTiled, -1);
399 pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
400 _x.gcfill0 = xgc(pmid, FillSolid, 0);
401 _x.gccopy0 = xgc(pmid, -1, -1);
402 _x.gcsimplesrc0 = xgc(pmid, FillStippled, -1);
403 _x.gczero0 = xgc(pmid, -1, -1);
404 _x.gcreplsrc0 = xgc(pmid, FillTiled, -1);
405 XFreePixmap(_x.display, pmid);
407 return _x.screenimage;
411 * Should do a better job of cleaning up here.
413 XCloseDisplay(_x.display);
418 _xsetlabel(char *label)
423 * Label and other properties required by ICCCCM.
425 memset(&name, 0, sizeof name);
427 label = "pjw-face-here";
428 name.value = (uchar*)label;
429 name.encoding = XA_STRING;
431 name.nitems = strlen((char*)name.value);
434 _x.display, /* display */
435 _x.drawable, /* window */
436 &name, /* XA_WM_NAME property */
437 &name, /* XA_WM_ICON_NAME property */
438 nil, /* XA_WM_COMMAND */
440 nil, /* XA_WM_NORMAL_HINTS */
441 nil, /* XA_WM_HINTS */
442 nil /* XA_WM_CLASSHINTS */
449 * Create a GC with a particular fill style and XXX.
450 * Disable generation of GraphicsExpose/NoExpose events in the GC.
453 xgc(XDrawable d, int fillstyle, int foreground)
458 memset(&v, 0, sizeof v);
460 v.graphics_exposures = False;
461 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
463 XSetFillStyle(_x.display, gc, fillstyle);
465 XSetForeground(_x.display, gc, 0);
471 * Initialize map with the Plan 9 rgbv color map.
476 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
484 for(g = 0; g != 4; g++)
485 for(b = 0; b!=4; b++)
486 for(v = 0; v!=4; v++){
492 /* divide check -- pick grey shades */
501 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
502 _x.map[idx].red = cr*0x0101;
503 _x.map[idx].green = cg*0x0101;
504 _x.map[idx].blue = cb*0x0101;
505 _x.map[idx].pixel = idx;
506 _x.map[idx].flags = DoRed|DoGreen|DoBlue;
509 idx7 = r*32 + v7*16 + g*4 + b;
511 _x.map7to8[idx7][0] = idx;
512 if(den == 0) { /* divide check -- pick grey shades */
513 cr = ((255.0/7.0)*v7)+0.5;
518 num=17*15*(4*den+v7*2)/14;
523 _x.map7[idx7].red = cr*0x0101;
524 _x.map7[idx7].green = cg*0x0101;
525 _x.map7[idx7].blue = cb*0x0101;
526 _x.map7[idx7].pixel = idx7;
527 _x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
530 _x.map7to8[idx7][1] = idx;
535 * Initialize and install the rgbv color map as a private color map
536 * for this application. It gets the best colors when it has the
539 * We always choose the best depth possible, but that might not
540 * be the default depth. On such "suboptimal" systems, we have to allocate an
541 * empty color map anyway, according to Axel Belinfante.
556 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
559 * The pixel value returned from XGetPixel needs to
560 * be converted to RGB so we can call rgb2cmap()
561 * to translate between 24 bit X and our color. Unfortunately,
562 * the return value appears to be display server endian
563 * dependant. Therefore, we run some heuristics to later
564 * determine how to mask the int value correctly.
565 * Yeah, I know we can look at _x.vis->byte_order but
566 * some displays say MSB even though they run on LSB.
567 * Besides, this is more anal.
569 c = _x.map[19]; /* known to have different R, G, B values */
570 if(!XAllocColor(_x.display, _x.cmap, &c)){
571 werrstr("XAllocColor: %r");
575 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
576 if(pp != _x.map[19].pixel) {
577 /* check if endian is other way */
578 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
579 if(pp != _x.map[19].pixel){
580 werrstr("cannot detect X server byte order");
592 werrstr("cannot byteswap channel %s",
593 chantostr(buf, _x.chan));
597 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
599 * Do nothing. We have no way to express a
600 * mixed-endian 16-bit screen, so pretend they don't exist.
603 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
604 }else if(_x.vis->class == PseudoColor){
605 if(_x.usetable == 0){
606 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll);
607 XStoreColors(_x.display, _x.cmap, _x.map, 256);
608 for(i = 0; i < 256; i++){
613 for(i = 0; i < 128; i++){
615 if(!XAllocColor(_x.display, _x.cmap, &c)){
616 werrstr("can't allocate colors in 7-bit map");
619 _x.tox11[_x.map7to8[i][0]] = c.pixel;
620 _x.tox11[_x.map7to8[i][1]] = c.pixel;
621 _x.toplan9[c.pixel] = _x.map7to8[i][0];
625 werrstr("unsupported visual class %d", _x.vis->class);
632 _flushmemscreen(Rectangle r)
634 if(_x.nextscreenpm != _x.screenpm){
635 qlock(&_x.screenlock);
636 XSync(_x.display, False);
637 XFreePixmap(_x.display, _x.screenpm);
638 _x.screenpm = _x.nextscreenpm;
639 qunlock(&_x.screenlock);
642 if(r.min.x >= r.max.x || r.min.y >= r.max.y)
644 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
645 Dx(r), Dy(r), r.min.x, r.min.y);
655 qlock(&_x.screenlock);
656 if(_x.screenpm != _x.nextscreenpm){
657 qunlock(&_x.screenlock);
660 xe = (XExposeEvent*)e;
663 r.max.x = xe->x+xe->width;
664 r.max.y = xe->y+xe->height;
665 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
666 Dx(r), Dy(r), r.min.x, r.min.y);
667 XSync(_x.display, False);
668 qunlock(&_x.screenlock);
674 XDestroyWindowEvent *xe;
676 xe = (XDestroyWindowEvent*)e;
677 if(xe->window == _x.drawable){
685 _xconfigure(XEvent *e)
688 XConfigureEvent *xe = (XConfigureEvent*)e;
690 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
692 r = Rect(0, 0, xe->width, xe->height);
693 qlock(&_x.screenlock);
694 if(_x.screenpm != _x.nextscreenpm){
695 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
696 Dx(r), Dy(r), r.min.x, r.min.y);
697 XSync(_x.display, False);
699 qunlock(&_x.screenlock);
705 _xreplacescreenimage(void)
712 if(eqrect(_x.screenr, r))
715 pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
716 m = _xallocmemimage(r, _x.chan, pixmap);
717 if(_x.nextscreenpm != _x.screenpm)
718 XFreePixmap(_x.display, _x.nextscreenpm);
719 _x.nextscreenpm = pixmap;
721 _drawreplacescreenimage(m);
726 parsewinsize(char *s, Rectangle *r, int *havemin)
734 if(!isdigit((uchar)*s))
736 i = strtol(s, &s, 0);
739 if(!isdigit((uchar)*s))
741 j = strtol(s, &s, 0);
750 if(!isdigit((uchar)*s))
752 i = strtol(s, &s, 0);
753 if(*s != ',' && *s != ' ')
756 if(!isdigit((uchar)*s))
758 j = strtol(s, &s, 0);
761 *r = rectaddpt(*r, Pt(i,j));
767 if(c != ' ' && c != ',')
770 if(!isdigit((uchar)*s))
772 j = strtol(s, &s, 0);
776 if(!isdigit((uchar)*s))
778 k = strtol(s, &s, 0);
782 if(!isdigit((uchar)*s))
784 l = strtol(s, &s, 0);
792 werrstr("bad syntax in window size '%s'", os);