2 * Some of the stuff in this file is not X-dependent and should be elsewhere.
12 #include "x11-memdraw.h"
14 static Memimage *xattach(char*);
15 static void plan9cmap(void);
16 static int setupcmap(XWindow);
17 static int xreplacescreenimage(void);
18 static XGC xgc(XDrawable, int, int);
19 static Image *getimage0(Display*, Image*);
24 _initdisplay(void (*error)(Display*, char*), char *label)
30 * This rfork(RFNOTEG) isn't exactly right,
31 * but we need some way to signal window
32 * closes. Right now we post a hangup
33 * note to the note group, which kills a whole
34 * lot more than just the current program
35 * if we don't do this.
40 d = mallocz(sizeof(Display), 1);
44 d->buf = malloc(16000+5);
45 d->obuf = malloc(16000);
46 if(d->buf == nil || d->obuf == nil){
64 _initdisplaymemimage(d, m);
65 d->screenimage = getimage0(d, 0);
70 getimage0(Display *d, Image *image)
75 extern int _freeimage1(Image*);
78 * If there's an old screen, it has id 0. The 'J' request below
79 * will try to install the new screen as id 0, so the old one
80 * must be freed first.
84 memset(image, 0, sizeof(Image));
90 if(flushimage(d, 0) < 0){
91 fprint(2, "cannot read screen info: %r\n");
95 n = _drawmsgread(d, info, sizeof info);
97 fprint(2, "short screen info\n");
102 image = mallocz(sizeof(Image), 1);
104 fprint(2, "cannot allocate image: %r\n");
111 image->chan = strtochan(info+2*12);
112 image->depth = chantodepth(image->chan);
113 image->repl = atoi(info+3*12);
114 image->r.min.x = atoi(info+4*12);
115 image->r.min.y = atoi(info+5*12);
116 image->r.max.x = atoi(info+6*12);
117 image->r.max.y = atoi(info+7*12);
118 image->clipr.min.x = atoi(info+8*12);
119 image->clipr.min.y = atoi(info+9*12);
120 image->clipr.max.x = atoi(info+10*12);
121 image->clipr.max.y = atoi(info+11*12);
126 getwindow(Display *d, int ref)
132 postnote(PNGROUP, getpgrp(), "hangup");
133 if(xreplacescreenimage() == 0)
137 * Libdraw promises not to change the value of "screen",
138 * so we have to reuse the image structure
139 * memory we already have.
142 i = getimage0(d, oi);
143 screen = d->screenimage = d->image = i;
144 // fprint(2, "getwindow %p -> %p\n", oi, i);
149 xerror(XDisplay *d, XErrorEvent *e)
153 if(e->request_code == 42) /* XSetInputFocus */
155 if(e->request_code == 18) /* XChangeProperty */
158 print("X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
159 e->error_code, e->request_code, e->minor_code, d);
160 XGetErrorText(d, e->error_code, buf, sizeof buf);
166 xioerror(XDisplay *d)
168 print("X I/O error\n");
177 char *argv[2], *disp;
180 XClassHint classhint;
182 XPixmapFormatValues *pfmt;
184 XSetWindowAttributes attr;
185 XSizeHints normalhint;
189 XWindowAttributes wattr;
194 if(XInitThreads() == 0){
195 fprint(2, "XInitThreads failed\n");
201 * Connect to X server.
203 _x.display = XOpenDisplay(NULL);
204 if(_x.display == nil){
205 disp = getenv("DISPLAY");
206 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
210 XSetErrorHandler(xerror);
211 XSetIOErrorHandler(xioerror);
212 xrootid = DefaultScreen(_x.display);
213 xrootwin = DefaultRootWindow(_x.display);
216 * Figure out underlying screen format.
218 if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
219 || XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
224 if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
225 || XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
230 if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
231 || XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
236 if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
237 || XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
239 werrstr("can't deal with colormapped depth %d screens",
247 _x.depth = DefaultDepth(_x.display, xrootid);
249 werrstr("can't understand depth %d screen", _x.depth);
252 _x.vis = DefaultVisual(_x.display, xrootid);
255 if(DefaultDepth(_x.display, xrootid) == _x.depth)
259 * _x.depth is only the number of significant pixel bits,
260 * not the total number of pixel bits. We need to walk the
261 * display list to find how many actual bits are used
265 pfmt = XListPixmapFormats(_x.display, &n);
267 if(pfmt[i].depth == _x.depth){
268 switch(pfmt[i].bits_per_pixel){
269 case 1: /* untested */
272 case 2: /* untested */
275 case 4: /* untested */
284 case 16: /* how to tell RGB15? */
287 case 24: /* untested (impossible?) */
297 werrstr("could not determine screen pixel format");
302 * Set up color map if necessary.
304 xscreen = DefaultScreenOfDisplay(_x.display);
305 _x.cmap = DefaultColormapOfScreen(xscreen);
306 if(_x.vis->class != StaticColor){
312 * We get to choose the initial rectangle size.
313 * This is arbitrary. In theory we should read the
314 * command line and allow the traditional X options.
316 r = Rect(0, 0, WidthOfScreen(xscreen)*3/4,
317 HeightOfScreen(xscreen)*3/4);
319 memset(&attr, 0, sizeof attr);
320 attr.colormap = _x.cmap;
321 attr.background_pixel = ~0;
322 attr.border_pixel = 0;
323 _x.drawable = XCreateWindow(
324 _x.display, /* display */
325 xrootwin, /* parent */
330 0, /* border width */
331 _x.depth, /* depth */
332 InputOutput, /* class */
335 CWBackPixel|CWBorderPixel|CWColormap,
336 &attr /* attributes (the above aren't?!) */
340 * Label and other properties required by ICCCCM.
342 memset(&name, 0, sizeof name);
344 label = "pjw-face-here";
345 name.value = (uchar*)label;
346 name.encoding = XA_STRING;
348 name.nitems = strlen((char*)name.value);
350 memset(&normalhint, 0, sizeof normalhint);
351 normalhint.flags = USSize|PMaxSize;
352 normalhint.max_width = WidthOfScreen(xscreen);
353 normalhint.max_height = HeightOfScreen(xscreen);
355 memset(&hint, 0, sizeof hint);
356 hint.flags = InputHint|StateHint;
358 hint.initial_state = NormalState;
360 memset(&classhint, 0, sizeof classhint);
361 classhint.res_name = label;
362 classhint.res_class = label;
368 _x.display, /* display */
369 _x.drawable, /* window */
370 &name, /* XA_WM_NAME property */
371 &name, /* XA_WM_ICON_NAME property */
372 argv, /* XA_WM_COMMAND */
374 &normalhint, /* XA_WM_NORMAL_HINTS */
375 &hint, /* XA_WM_HINTS */
376 &classhint /* XA_WM_CLASSHINTS */
381 * Look up clipboard atom.
383 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", False);
384 _x.utf8string = XInternAtom(_x.display, "UTF8_STRING", False);
385 _x.targets = XInternAtom(_x.display, "TARGETS", False);
386 _x.text = XInternAtom(_x.display, "TEXT", False);
387 _x.compoundtext = XInternAtom(_x.display, "COMPOUND_TEXT", False);
388 _x.takefocus = XInternAtom(_x.display, "WM_TAKE_FOCUS", False);
389 _x.losefocus = XInternAtom(_x.display, "_9WM_LOSE_FOCUS", False);
390 _x.wmprotos = XInternAtom(_x.display, "WM_PROTOCOLS", False);
392 atoms[0] = _x.takefocus;
393 atoms[1] = _x.losefocus;
394 XChangeProperty(_x.display, _x.drawable, _x.wmprotos, XA_ATOM, 32,
395 PropModeReplace, (uchar*)atoms, 2);
398 * Put the window on the screen, check to see what size we actually got.
400 XMapWindow(_x.display, _x.drawable);
401 XSync(_x.display, False);
403 if(!XGetWindowAttributes(_x.display, _x.drawable, &wattr))
404 fprint(2, "XGetWindowAttributes failed\n");
405 else if(wattr.width && wattr.height){
406 if(wattr.width != Dx(r) || wattr.height != Dy(r)){
407 r.max.x = wattr.width;
408 r.max.y = wattr.height;
411 fprint(2, "XGetWindowAttributes: bad attrs\n");
414 * Allocate our local backing store.
417 _x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
418 _x.nextscreenpm = _x.screenpm;
419 _x.screenimage = _xallocmemimage(r, _x.chan, _x.screenpm);
422 * Allocate some useful graphics contexts for the future.
424 _x.gcfill = xgc(_x.screenpm, FillSolid, -1);
425 _x.gccopy = xgc(_x.screenpm, -1, -1);
426 _x.gcsimplesrc = xgc(_x.screenpm, FillStippled, -1);
427 _x.gczero = xgc(_x.screenpm, -1, -1);
428 _x.gcreplsrc = xgc(_x.screenpm, FillTiled, -1);
430 pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
431 _x.gcfill0 = xgc(pmid, FillSolid, 0);
432 _x.gccopy0 = xgc(pmid, -1, -1);
433 _x.gcsimplesrc0 = xgc(pmid, FillStippled, -1);
434 _x.gczero0 = xgc(pmid, -1, -1);
435 _x.gcreplsrc0 = xgc(pmid, FillTiled, -1);
436 XFreePixmap(_x.display, pmid);
439 * Lots of display connections for various procs.
441 _x.kbdcon = XOpenDisplay(NULL);
442 _x.mousecon = XOpenDisplay(NULL);
443 _x.snarfcon = XOpenDisplay(NULL);
445 if(0) fprint(2, "x: display=%p kbd=%p mouse=%p snarf=%p\n",
446 _x.display, _x.kbdcon, _x.mousecon, _x.snarfcon);
448 _x.black = xscreen->black_pixel;
449 _x.white = xscreen->white_pixel;
451 return _x.screenimage;
456 * Should do a better job of cleaning up here.
458 XCloseDisplay(_x.display);
463 drawsetlabel(Display *d, char *label)
466 XClassHint classhint;
470 * Label and other properties required by ICCCCM.
472 memset(&name, 0, sizeof name);
474 label = "pjw-face-here";
475 name.value = (uchar*)label;
476 name.encoding = XA_STRING;
478 name.nitems = strlen((char*)name.value);
480 memset(&classhint, 0, sizeof classhint);
481 classhint.res_name = label;
482 classhint.res_class = label;
488 _x.display, /* display */
489 _x.drawable, /* window */
490 &name, /* XA_WM_NAME property */
491 &name, /* XA_WM_ICON_NAME property */
492 argv, /* XA_WM_COMMAND */
494 nil, /* XA_WM_NORMAL_HINTS */
495 nil, /* XA_WM_HINTS */
496 &classhint /* XA_WM_CLASSHINTS */
503 * Create a GC with a particular fill style and XXX.
504 * Disable generation of GraphicsExpose/NoExpose events in the GC.
507 xgc(XDrawable d, int fillstyle, int foreground)
512 memset(&v, 0, sizeof v);
514 v.graphics_exposures = False;
515 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
517 XSetFillStyle(_x.display, gc, fillstyle);
519 XSetForeground(_x.display, gc, 0);
525 * Initialize map with the Plan 9 rgbv color map.
530 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
538 for(g = 0; g != 4; g++)
539 for(b = 0; b!=4; b++)
540 for(v = 0; v!=4; v++){
546 /* divide check -- pick grey shades */
555 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
556 _x.map[idx].red = cr*0x0101;
557 _x.map[idx].green = cg*0x0101;
558 _x.map[idx].blue = cb*0x0101;
559 _x.map[idx].pixel = idx;
560 _x.map[idx].flags = DoRed|DoGreen|DoBlue;
563 idx7 = r*32 + v7*16 + g*4 + b;
565 _x.map7to8[idx7][0] = idx;
566 if(den == 0) { /* divide check -- pick grey shades */
567 cr = ((255.0/7.0)*v7)+0.5;
572 num=17*15*(4*den+v7*2)/14;
577 _x.map7[idx7].red = cr*0x0101;
578 _x.map7[idx7].green = cg*0x0101;
579 _x.map7[idx7].blue = cb*0x0101;
580 _x.map7[idx7].pixel = idx7;
581 _x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
584 _x.map7to8[idx7][1] = idx;
589 * Initialize and install the rgbv color map as a private color map
590 * for this application. It gets the best colors when it has the
593 * We always choose the best depth possible, but that might not
594 * be the default depth. On such "suboptimal" systems, we have to allocate an
595 * empty color map anyway, according to Axel Belinfante.
610 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
613 * The pixel value returned from XGetPixel needs to
614 * be converted to RGB so we can call rgb2cmap()
615 * to translate between 24 bit X and our color. Unfortunately,
616 * the return value appears to be display server endian
617 * dependant. Therefore, we run some heuristics to later
618 * determine how to mask the int value correctly.
619 * Yeah, I know we can look at _x.vis->byte_order but
620 * some displays say MSB even though they run on LSB.
621 * Besides, this is more anal.
623 c = _x.map[19]; /* known to have different R, G, B values */
624 if(!XAllocColor(_x.display, _x.cmap, &c)){
625 werrstr("XAllocColor: %r");
629 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
630 if(pp != _x.map[19].pixel) {
631 /* check if endian is other way */
632 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
633 if(pp != _x.map[19].pixel){
634 werrstr("cannot detect X server byte order");
646 werrstr("cannot byteswap channel %s",
647 chantostr(buf, _x.chan));
651 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
653 * Do nothing. We have no way to express a
654 * mixed-endian 16-bit screen, so pretend they don't exist.
657 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
658 }else if(_x.vis->class == PseudoColor){
659 if(_x.usetable == 0){
660 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll);
661 XStoreColors(_x.display, _x.cmap, _x.map, 256);
662 for(i = 0; i < 256; i++){
667 for(i = 0; i < 128; i++){
669 if(!XAllocColor(_x.display, _x.cmap, &c)){
670 werrstr("can't allocate colors in 7-bit map");
673 _x.tox11[_x.map7to8[i][0]] = c.pixel;
674 _x.tox11[_x.map7to8[i][1]] = c.pixel;
675 _x.toplan9[c.pixel] = _x.map7to8[i][0];
679 werrstr("unsupported visual class %d", _x.vis->class);
686 flushmemscreen(Rectangle r)
688 if(_x.nextscreenpm != _x.screenpm){
689 qlock(&_x.screenlock);
690 XSync(_x.display, False);
691 XFreePixmap(_x.display, _x.screenpm);
692 _x.screenpm = _x.nextscreenpm;
693 qunlock(&_x.screenlock);
696 if(r.min.x >= r.max.x || r.min.y >= r.max.y)
698 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
699 Dx(r), Dy(r), r.min.x, r.min.y);
704 _xexpose(XEvent *e, XDisplay *xd)
709 qlock(&_x.screenlock);
710 if(_x.screenpm != _x.nextscreenpm){
711 qunlock(&_x.screenlock);
714 xe = (XExposeEvent*)e;
717 r.max.x = xe->x+xe->width;
718 r.max.y = xe->y+xe->height;
719 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
720 Dx(r), Dy(r), r.min.x, r.min.y);
722 qunlock(&_x.screenlock);
726 _xdestroy(XEvent *e, XDisplay *xd)
728 XDestroyWindowEvent *xe;
730 xe = (XDestroyWindowEvent*)e;
731 if(xe->window == _x.drawable){
739 _xconfigure(XEvent *e, XDisplay *xd)
742 XConfigureEvent *xe = (XConfigureEvent*)e;
744 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
746 if(xe->width==0 || xe->height==0)
747 fprint(2, "ignoring resize to %dx%d\n", xe->width, xe->height);
748 r = Rect(0, 0, xe->width, xe->height);
749 qlock(&_x.screenlock);
750 if(_x.screenpm != _x.nextscreenpm){
751 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
752 Dx(r), Dy(r), r.min.x, r.min.y);
755 qunlock(&_x.screenlock);
761 xreplacescreenimage(void)
768 if(eqrect(_x.screenr, r))
771 pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
772 m = _xallocmemimage(r, _x.chan, pixmap);
773 if(_x.nextscreenpm != _x.screenpm)
774 XFreePixmap(_x.display, _x.nextscreenpm);
775 _x.nextscreenpm = pixmap;
777 _drawreplacescreenimage(m);