2 * Some of the stuff in this file is not X-dependent and should be elsewhere.
12 #include "x11-memdraw.h"
15 static void plan9cmap(void);
16 static int setupcmap(XWindow);
17 static XGC xgc(XDrawable, int, int);
22 xerror(XDisplay *d, XErrorEvent *e)
26 if(e->request_code == 42) /* XSetInputFocus */
28 if(e->request_code == 18) /* XChangeProperty */
31 * BadDrawable happens in apps that get resized a LOT,
32 * e.g. when KDE is configured to resize continuously
33 * during a window drag.
35 if(e->error_code == 9) /* BadDrawable */
38 fprint(2, "X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
39 e->error_code, e->request_code, e->minor_code, d);
40 XGetErrorText(d, e->error_code, buf, sizeof buf);
41 fprint(2, "%s\n", buf);
48 /*print("X I/O error\n"); */
50 /*sysfatal("X I/O error\n");*/
57 _xattach(char *label, char *winsize)
60 int i, havemin, height, mask, n, width, x, xrootid, y;
64 XPixmapFormatValues *pfmt;
66 XSetWindowAttributes attr;
67 XSizeHints normalhint;
71 XWindowAttributes wattr;
76 if(XInitThreads() == 0){
77 fprint(2, "XInitThreads failed\n");
83 * Connect to X server.
85 _x.display = XOpenDisplay(NULL);
86 if(_x.display == nil){
87 disp = getenv("DISPLAY");
88 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
92 _x.fd = ConnectionNumber(_x.display);
93 XSetErrorHandler(xerror);
94 XSetIOErrorHandler(xioerror);
95 xrootid = DefaultScreen(_x.display);
96 xrootwin = DefaultRootWindow(_x.display);
99 * Figure out underlying screen format.
101 if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
102 || XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
107 if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
108 || XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
113 if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
114 || XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
119 if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
120 || XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
122 werrstr("can't deal with colormapped depth %d screens",
130 _x.depth = DefaultDepth(_x.display, xrootid);
132 werrstr("can't understand depth %d screen", _x.depth);
135 _x.vis = DefaultVisual(_x.display, xrootid);
138 if(DefaultDepth(_x.display, xrootid) == _x.depth)
142 * _x.depth is only the number of significant pixel bits,
143 * not the total number of pixel bits. We need to walk the
144 * display list to find how many actual bits are used
148 pfmt = XListPixmapFormats(_x.display, &n);
150 if(pfmt[i].depth == _x.depth){
151 switch(pfmt[i].bits_per_pixel){
152 case 1: /* untested */
155 case 2: /* untested */
158 case 4: /* untested */
167 case 16: /* how to tell RGB15? */
170 case 24: /* untested (impossible?) */
180 werrstr("could not determine screen pixel format");
185 * Set up color map if necessary.
187 xscreen = DefaultScreenOfDisplay(_x.display);
188 _x.cmap = DefaultColormapOfScreen(xscreen);
189 if(_x.vis->class != StaticColor){
195 * We get to choose the initial rectangle size.
196 * This is arbitrary. In theory we should read the
197 * command line and allow the traditional X options.
202 if(winsize && winsize[0]){
203 if(parsewinsize(winsize, &r, &havemin) < 0)
207 * Parse the various X resources. Thanks to Peter Canning.
209 char *screen_resources, *display_resources, *geom,
210 *geomrestype, *home, *file;
211 XrmDatabase database;
214 database = XrmGetDatabase(_x.display);
215 screen_resources = XScreenResourceString(xscreen);
216 if(screen_resources != nil){
217 XrmCombineDatabase(XrmGetStringDatabase(screen_resources), &database, False);
218 XFree(screen_resources);
221 display_resources = XResourceManagerString(_x.display);
222 if(display_resources == nil){
223 home = getenv("HOME");
224 if(home!=nil && (file=smprint("%s/.Xdefaults", home)) != nil){
225 XrmCombineFileDatabase(file, &database, False);
230 XrmCombineDatabase(XrmGetStringDatabase(display_resources), &database, False);
232 geom = smprint("%s.geometry", label);
233 if(geom && XrmGetResource(database, geom, nil, &geomrestype, &geomres))
234 mask = XParseGeometry(geomres.addr, &x, &y, (uint*)&width, (uint*)&height);
237 if((mask & WidthValue) && (mask & HeightValue)){
238 r = Rect(0, 0, width, height);
240 r = Rect(0, 0, WidthOfScreen(xscreen)*3/4,
241 HeightOfScreen(xscreen)*3/4);
242 if(Dx(r) > Dy(r)*3/2)
243 r.max.x = r.min.x + Dy(r)*3/2;
244 if(Dy(r) > Dx(r)*3/2)
245 r.max.y = r.min.y + Dx(r)*3/2;
247 if(mask & XNegative){
248 x += WidthOfScreen(xscreen);
250 if(mask & YNegative){
251 y += HeightOfScreen(xscreen);
255 screenrect = Rect(0, 0, WidthOfScreen(xscreen), HeightOfScreen(xscreen));
258 memset(&attr, 0, sizeof attr);
259 attr.colormap = _x.cmap;
260 attr.background_pixel = ~0;
261 attr.border_pixel = 0;
262 _x.drawable = XCreateWindow(
263 _x.display, /* display */
264 xrootwin, /* parent */
269 0, /* border width */
270 _x.depth, /* depth */
271 InputOutput, /* class */
274 CWBackPixel|CWBorderPixel|CWColormap,
275 &attr /* attributes (the above aren't?!) */
279 * Label and other properties required by ICCCCM.
281 memset(&name, 0, sizeof name);
283 label = "pjw-face-here";
284 name.value = (uchar*)label;
285 name.encoding = XA_STRING;
287 name.nitems = strlen((char*)name.value);
289 memset(&normalhint, 0, sizeof normalhint);
290 normalhint.flags = PSize|PMaxSize;
291 if(winsize && winsize[0]){
292 normalhint.flags &= ~PSize;
293 normalhint.flags |= USSize;
294 normalhint.width = Dx(r);
295 normalhint.height = Dy(r);
297 if((mask & WidthValue) && (mask & HeightValue)){
298 normalhint.flags &= ~PSize;
299 normalhint.flags |= USSize;
300 normalhint.width = width;
301 normalhint.height = height;
303 if((mask & WidthValue) && (mask & HeightValue)){
304 normalhint.flags |= USPosition;
310 normalhint.max_width = WidthOfScreen(xscreen);
311 normalhint.max_height = HeightOfScreen(xscreen);
313 memset(&hint, 0, sizeof hint);
314 hint.flags = InputHint|StateHint;
316 hint.initial_state = NormalState;
318 memset(&classhint, 0, sizeof classhint);
319 classhint.res_name = label;
320 classhint.res_class = label;
326 _x.display, /* display */
327 _x.drawable, /* window */
328 &name, /* XA_WM_NAME property */
329 &name, /* XA_WM_ICON_NAME property */
330 argv, /* XA_WM_COMMAND */
332 &normalhint, /* XA_WM_NORMAL_HINTS */
333 &hint, /* XA_WM_HINTS */
334 &classhint /* XA_WM_CLASSHINTS */
341 memset(&ch, 0, sizeof ch);
344 XConfigureWindow(_x.display, _x.drawable, CWX|CWY, &ch);
346 * Must pretend origin is 0,0 for X.
348 r = Rect(0,0,Dx(r),Dy(r));
351 * Look up clipboard atom.
353 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", False);
354 _x.utf8string = XInternAtom(_x.display, "UTF8_STRING", False);
355 _x.targets = XInternAtom(_x.display, "TARGETS", False);
356 _x.text = XInternAtom(_x.display, "TEXT", False);
357 _x.compoundtext = XInternAtom(_x.display, "COMPOUND_TEXT", False);
358 _x.takefocus = XInternAtom(_x.display, "WM_TAKE_FOCUS", False);
359 _x.losefocus = XInternAtom(_x.display, "_9WM_LOSE_FOCUS", False);
360 _x.wmprotos = XInternAtom(_x.display, "WM_PROTOCOLS", False);
362 atoms[0] = _x.takefocus;
363 atoms[1] = _x.losefocus;
364 XChangeProperty(_x.display, _x.drawable, _x.wmprotos, XA_ATOM, 32,
365 PropModeReplace, (uchar*)atoms, 2);
368 * Put the window on the screen, check to see what size we actually got.
370 XMapWindow(_x.display, _x.drawable);
371 XSync(_x.display, False);
373 if(!XGetWindowAttributes(_x.display, _x.drawable, &wattr))
374 fprint(2, "XGetWindowAttributes failed\n");
375 else if(wattr.width && wattr.height){
376 if(wattr.width != Dx(r) || wattr.height != Dy(r)){
377 r.max.x = wattr.width;
378 r.max.y = wattr.height;
381 fprint(2, "XGetWindowAttributes: bad attrs\n");
384 * Allocate our local backing store.
387 _x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
388 _x.nextscreenpm = _x.screenpm;
389 _x.screenimage = _xallocmemimage(r, _x.chan, _x.screenpm);
392 * Allocate some useful graphics contexts for the future.
394 _x.gcfill = xgc(_x.screenpm, FillSolid, -1);
395 _x.gccopy = xgc(_x.screenpm, -1, -1);
396 _x.gcsimplesrc = xgc(_x.screenpm, FillStippled, -1);
397 _x.gczero = xgc(_x.screenpm, -1, -1);
398 _x.gcreplsrc = xgc(_x.screenpm, FillTiled, -1);
400 pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
401 _x.gcfill0 = xgc(pmid, FillSolid, 0);
402 _x.gccopy0 = xgc(pmid, -1, -1);
403 _x.gcsimplesrc0 = xgc(pmid, FillStippled, -1);
404 _x.gczero0 = xgc(pmid, -1, -1);
405 _x.gcreplsrc0 = xgc(pmid, FillTiled, -1);
406 XFreePixmap(_x.display, pmid);
408 return _x.screenimage;
412 * Should do a better job of cleaning up here.
414 XCloseDisplay(_x.display);
419 _xsetlabel(char *label)
424 * Label and other properties required by ICCCCM.
426 memset(&name, 0, sizeof name);
428 label = "pjw-face-here";
429 name.value = (uchar*)label;
430 name.encoding = XA_STRING;
432 name.nitems = strlen((char*)name.value);
435 _x.display, /* display */
436 _x.drawable, /* window */
437 &name, /* XA_WM_NAME property */
438 &name, /* XA_WM_ICON_NAME property */
439 nil, /* XA_WM_COMMAND */
441 nil, /* XA_WM_NORMAL_HINTS */
442 nil, /* XA_WM_HINTS */
443 nil /* XA_WM_CLASSHINTS */
450 * Create a GC with a particular fill style and XXX.
451 * Disable generation of GraphicsExpose/NoExpose events in the GC.
454 xgc(XDrawable d, int fillstyle, int foreground)
459 memset(&v, 0, sizeof v);
461 v.graphics_exposures = False;
462 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
464 XSetFillStyle(_x.display, gc, fillstyle);
466 XSetForeground(_x.display, gc, 0);
472 * Initialize map with the Plan 9 rgbv color map.
477 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
485 for(g = 0; g != 4; g++)
486 for(b = 0; b!=4; b++)
487 for(v = 0; v!=4; v++){
493 /* divide check -- pick grey shades */
502 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
503 _x.map[idx].red = cr*0x0101;
504 _x.map[idx].green = cg*0x0101;
505 _x.map[idx].blue = cb*0x0101;
506 _x.map[idx].pixel = idx;
507 _x.map[idx].flags = DoRed|DoGreen|DoBlue;
510 idx7 = r*32 + v7*16 + g*4 + b;
512 _x.map7to8[idx7][0] = idx;
513 if(den == 0) { /* divide check -- pick grey shades */
514 cr = ((255.0/7.0)*v7)+0.5;
519 num=17*15*(4*den+v7*2)/14;
524 _x.map7[idx7].red = cr*0x0101;
525 _x.map7[idx7].green = cg*0x0101;
526 _x.map7[idx7].blue = cb*0x0101;
527 _x.map7[idx7].pixel = idx7;
528 _x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
531 _x.map7to8[idx7][1] = idx;
536 * Initialize and install the rgbv color map as a private color map
537 * for this application. It gets the best colors when it has the
540 * We always choose the best depth possible, but that might not
541 * be the default depth. On such "suboptimal" systems, we have to allocate an
542 * empty color map anyway, according to Axel Belinfante.
557 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
560 * The pixel value returned from XGetPixel needs to
561 * be converted to RGB so we can call rgb2cmap()
562 * to translate between 24 bit X and our color. Unfortunately,
563 * the return value appears to be display server endian
564 * dependant. Therefore, we run some heuristics to later
565 * determine how to mask the int value correctly.
566 * Yeah, I know we can look at _x.vis->byte_order but
567 * some displays say MSB even though they run on LSB.
568 * Besides, this is more anal.
570 c = _x.map[19]; /* known to have different R, G, B values */
571 if(!XAllocColor(_x.display, _x.cmap, &c)){
572 werrstr("XAllocColor: %r");
576 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
577 if(pp != _x.map[19].pixel) {
578 /* check if endian is other way */
579 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
580 if(pp != _x.map[19].pixel){
581 werrstr("cannot detect X server byte order");
593 werrstr("cannot byteswap channel %s",
594 chantostr(buf, _x.chan));
598 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
600 * Do nothing. We have no way to express a
601 * mixed-endian 16-bit screen, so pretend they don't exist.
604 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
605 }else if(_x.vis->class == PseudoColor){
606 if(_x.usetable == 0){
607 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll);
608 XStoreColors(_x.display, _x.cmap, _x.map, 256);
609 for(i = 0; i < 256; i++){
614 for(i = 0; i < 128; i++){
616 if(!XAllocColor(_x.display, _x.cmap, &c)){
617 werrstr("can't allocate colors in 7-bit map");
620 _x.tox11[_x.map7to8[i][0]] = c.pixel;
621 _x.tox11[_x.map7to8[i][1]] = c.pixel;
622 _x.toplan9[c.pixel] = _x.map7to8[i][0];
626 werrstr("unsupported visual class %d", _x.vis->class);
633 _flushmemscreen(Rectangle r)
635 if(_x.nextscreenpm != _x.screenpm){
636 qlock(&_x.screenlock);
637 XSync(_x.display, False);
638 XFreePixmap(_x.display, _x.screenpm);
639 _x.screenpm = _x.nextscreenpm;
640 qunlock(&_x.screenlock);
643 if(r.min.x >= r.max.x || r.min.y >= r.max.y)
645 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
646 Dx(r), Dy(r), r.min.x, r.min.y);
656 qlock(&_x.screenlock);
657 if(_x.screenpm != _x.nextscreenpm){
658 qunlock(&_x.screenlock);
661 xe = (XExposeEvent*)e;
664 r.max.x = xe->x+xe->width;
665 r.max.y = xe->y+xe->height;
666 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
667 Dx(r), Dy(r), r.min.x, r.min.y);
668 XSync(_x.display, False);
669 qunlock(&_x.screenlock);
675 XDestroyWindowEvent *xe;
677 xe = (XDestroyWindowEvent*)e;
678 if(xe->window == _x.drawable){
686 _xconfigure(XEvent *e)
689 XConfigureEvent *xe = (XConfigureEvent*)e;
694 if(XTranslateCoordinates(_x.display, _x.drawable, DefaultRootWindow(_x.display), 0, 0, &rx, &ry, &w))
695 windowrect = Rect(rx, ry, rx+xe->width, ry+xe->height);
698 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
700 r = Rect(0, 0, xe->width, xe->height);
702 qlock(&_x.screenlock);
703 if(_x.screenpm != _x.nextscreenpm){
704 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
705 Dx(r), Dy(r), r.min.x, r.min.y);
706 XSync(_x.display, False);
708 qunlock(&_x.screenlock);
714 _xreplacescreenimage(void)
722 pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
723 m = _xallocmemimage(r, _x.chan, pixmap);
724 if(_x.nextscreenpm != _x.screenpm)
725 XFreePixmap(_x.display, _x.nextscreenpm);
726 _x.nextscreenpm = pixmap;
728 _drawreplacescreenimage(m);