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*);
24 _initdisplay(void (*error)(Display*, char*), char *label)
31 d = mallocz(sizeof(Display), 1);
35 d->buf = malloc(16000+5);
36 d->obuf = malloc(16000);
37 if(d->buf == nil || d->obuf == nil){
55 _initdisplaymemimage(d, m);
56 d->screenimage = getimage0(d);
63 char *a, info[12*12+1];
70 if(flushimage(d, 0) < 0){
71 fprint(2, "cannot read screen info: %r\n");
75 n = _drawmsgread(d, info, sizeof info);
77 fprint(2, "short screen info\n");
81 image = mallocz(sizeof(Image), 1);
84 image->chan = strtochan(info+2*12);
85 image->depth = chantodepth(image->chan);
86 image->repl = atoi(info+3*12);
87 image->r.min.x = atoi(info+4*12);
88 image->r.min.y = atoi(info+5*12);
89 image->r.max.x = atoi(info+6*12);
90 image->r.max.y = atoi(info+7*12);
91 image->clipr.min.x = atoi(info+8*12);
92 image->clipr.min.y = atoi(info+9*12);
93 image->clipr.max.x = atoi(info+10*12);
94 image->clipr.max.y = atoi(info+11*12);
99 getwindow(Display *d, int ref)
104 postnote(PNGROUP, getpgrp(), "hangup");
105 if(xreplacescreenimage() == 0)
107 freeimage(d->screenimage);
109 screen = d->screenimage = d->image = i;
114 xerror(XDisplay *d, XErrorEvent *e)
118 print("X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
119 e->error_code, e->request_code, e->minor_code, d);
120 XGetErrorText(d, e->error_code, buf, sizeof buf);
126 xioerror(XDisplay *d)
128 print("X I/O error\n");
137 char *argv[2], *disp;
140 XClassHint classhint;
142 XPixmapFormatValues *pfmt;
144 XSetWindowAttributes attr;
145 XSizeHints normalhint;
152 if(XInitThreads() == 0){
153 fprint(2, "XInitThreads failed\n");
159 * Connect to X server.
161 _x.display = XOpenDisplay(NULL);
162 if(_x.display == nil){
163 disp = getenv("DISPLAY");
164 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
168 XSetErrorHandler(xerror);
169 XSetIOErrorHandler(xioerror);
170 xrootid = DefaultScreen(_x.display);
171 xrootwin = DefaultRootWindow(_x.display);
174 * Figure out underlying screen format.
176 _x.depth = DefaultDepth(_x.display, xrootid);
177 if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
178 || XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
184 if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
185 || XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
191 if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
192 || XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
198 if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
199 || XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
201 werrstr("can't deal with colormapped depth %d screens",
210 werrstr("can't understand depth %d screen", _x.depth);
213 _x.vis = DefaultVisual(_x.display, xrootid);
217 * _x.depth is only the number of significant pixel bits,
218 * not the total number of pixel bits. We need to walk the
219 * display list to find how many actual bits are used
223 pfmt = XListPixmapFormats(_x.display, &n);
225 if(pfmt[i].depth == _x.depth){
226 switch(pfmt[i].bits_per_pixel){
227 case 1: /* untested */
230 case 2: /* untested */
233 case 4: /* untested */
242 case 16: /* how to tell RGB15? */
245 case 24: /* untested (impossible?) */
255 werrstr("could not determine screen pixel format");
260 * Set up color map if necessary.
262 xscreen = DefaultScreenOfDisplay(_x.display);
263 _x.cmap = DefaultColormapOfScreen(xscreen);
264 if(_x.vis->class != StaticColor){
270 * We get to choose the initial rectangle size.
271 * This is arbitrary. In theory we should read the
272 * command line and allow the traditional X options.
274 r = Rect(0, 0, WidthOfScreen(xscreen)*3/4,
275 HeightOfScreen(xscreen)*3/4);
277 memset(&attr, 0, sizeof attr);
278 attr.colormap = _x.cmap;
279 attr.background_pixel = ~0;
280 attr.border_pixel = 0;
281 _x.drawable = XCreateWindow(
282 _x.display, /* display */
283 xrootwin, /* parent */
288 0, /* border width */
289 DefaultDepthOfScreen(xscreen), /* depth */
290 InputOutput, /* class */
293 CWBackPixel|CWBorderPixel|CWColormap,
294 &attr /* attributes (the above aren't?!) */
298 * Label and other properties required by ICCCCM.
300 memset(&name, 0, sizeof name);
302 label = "pjw-face-here";
303 name.value = (uchar*)label;
304 name.encoding = XA_STRING;
306 name.nitems = strlen(name.value);
308 memset(&normalhint, 0, sizeof normalhint);
309 normalhint.flags = USSize|PMaxSize;
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 */
339 * Allocate our local backing store.
342 _x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
343 _x.nextscreenpm = _x.screenpm;
344 _x.screenimage = xallocmemimage(r, _x.chan, _x.screenpm);
347 * Allocate some useful graphics contexts for the future.
349 _x.gcfill = xgc(_x.screenpm, FillSolid, -1);
350 _x.gccopy = xgc(_x.screenpm, -1, -1);
351 _x.gcsimplesrc = xgc(_x.screenpm, FillStippled, -1);
352 _x.gczero = xgc(_x.screenpm, -1, -1);
353 _x.gcreplsrc = xgc(_x.screenpm, FillTiled, -1);
355 pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
356 _x.gcfill0 = xgc(pmid, FillSolid, 0);
357 _x.gccopy0 = xgc(pmid, -1, -1);
358 _x.gcsimplesrc0 = xgc(pmid, FillStippled, -1);
359 _x.gczero0 = xgc(pmid, -1, -1);
360 _x.gcreplsrc0 = xgc(pmid, FillTiled, -1);
361 XFreePixmap(_x.display, pmid);
364 * Put the window on the screen.
366 XMapWindow(_x.display, _x.drawable);
370 * Look up clipboard atom.
372 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", True);
375 * Lots of display connections for various procs.
377 _x.kbdcon = XOpenDisplay(NULL);
378 _x.mousecon = XOpenDisplay(NULL);
379 _x.snarfcon = XOpenDisplay(NULL);
381 if(0) fprint(2, "x: display=%p kbd=%p mouse=%p snarf=%p\n",
382 _x.display, _x.kbdcon, _x.mousecon, _x.snarfcon);
384 _x.black = xscreen->black_pixel;
385 _x.white = xscreen->white_pixel;
387 return _x.screenimage;
392 * Should do a better job of cleaning up here.
394 XCloseDisplay(_x.display);
399 drawsetlabel(Display *d, char *label)
402 XClassHint classhint;
406 * Label and other properties required by ICCCCM.
408 memset(&name, 0, sizeof name);
410 label = "pjw-face-here";
411 name.value = (uchar*)label;
412 name.encoding = XA_STRING;
414 name.nitems = strlen(name.value);
416 memset(&classhint, 0, sizeof classhint);
417 classhint.res_name = label;
418 classhint.res_class = label;
424 _x.display, /* display */
425 _x.drawable, /* window */
426 &name, /* XA_WM_NAME property */
427 &name, /* XA_WM_ICON_NAME property */
428 argv, /* XA_WM_COMMAND */
430 nil, /* XA_WM_NORMAL_HINTS */
431 nil, /* XA_WM_HINTS */
432 &classhint /* XA_WM_CLASSHINTS */
439 * Create a GC with a particular fill style and XXX.
440 * Disable generation of GraphicsExpose/NoExpose events in the GC.
443 xgc(XDrawable d, int fillstyle, int foreground)
448 memset(&v, 0, sizeof v);
450 v.graphics_exposures = False;
451 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
453 XSetFillStyle(_x.display, gc, fillstyle);
455 XSetForeground(_x.display, gc, 0);
461 * Initialize map with the Plan 9 rgbv color map.
466 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
474 for(g = 0; g != 4; g++)
475 for(b = 0; b!=4; b++)
476 for(v = 0; v!=4; v++){
482 /* divide check -- pick grey shades */
491 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
492 _x.map[idx].red = cr*0x0101;
493 _x.map[idx].green = cg*0x0101;
494 _x.map[idx].blue = cb*0x0101;
495 _x.map[idx].pixel = idx;
496 _x.map[idx].flags = DoRed|DoGreen|DoBlue;
499 idx7 = r*32 + v7*16 + g*4 + b;
501 _x.map7to8[idx7][0] = idx;
502 if(den == 0) { /* divide check -- pick grey shades */
503 cr = ((255.0/7.0)*v7)+0.5;
508 num=17*15*(4*den+v7*2)/14;
513 _x.map7[idx7].red = cr*0x0101;
514 _x.map7[idx7].green = cg*0x0101;
515 _x.map7[idx7].blue = cb*0x0101;
516 _x.map7[idx7].pixel = idx7;
517 _x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
520 _x.map7to8[idx7][1] = idx;
525 * Initialize and install the rgbv color map as a private color map
526 * for this application. It gets the best colors when it has the
542 * The pixel value returned from XGetPixel needs to
543 * be converted to RGB so we can call rgb2cmap()
544 * to translate between 24 bit X and our color. Unfortunately,
545 * the return value appears to be display server endian
546 * dependant. Therefore, we run some heuristics to later
547 * determine how to mask the int value correctly.
548 * Yeah, I know we can look at _x.vis->byte_order but
549 * some displays say MSB even though they run on LSB.
550 * Besides, this is more anal.
553 c = _x.map[19]; /* known to have different R, G, B values */
554 if(!XAllocColor(_x.display, _x.cmap, &c)){
555 werrstr("XAllocColor: %r");
559 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
560 if(pp != _x.map[19].pixel) {
561 /* check if endian is other way */
562 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
563 if(pp != _x.map[19].pixel){
564 werrstr("cannot detect X server byte order");
576 werrstr("cannot byteswap channel %s",
577 chantostr(buf, _x.chan));
581 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
583 * Do nothing. We have no way to express a
584 * mixed-endian 16-bit screen, so pretend they don't exist.
586 }else if(_x.vis->class == PseudoColor){
587 if(_x.usetable == 0){
588 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll);
589 XStoreColors(_x.display, _x.cmap, _x.map, 256);
590 for(i = 0; i < 256; i++){
595 for(i = 0; i < 128; i++){
597 if(!XAllocColor(_x.display, _x.cmap, &c)){
598 werrstr("can't allocate colors in 7-bit map");
601 _x.tox11[_x.map7to8[i][0]] = c.pixel;
602 _x.tox11[_x.map7to8[i][1]] = c.pixel;
603 _x.toplan9[c.pixel] = _x.map7to8[i][0];
607 werrstr("unsupported visual class %d", _x.vis->class);
614 flushmemscreen(Rectangle r)
616 if(_x.nextscreenpm != _x.screenpm){
617 qlock(&_x.screenlock);
618 XSync(_x.display, False);
619 XFreePixmap(_x.display, _x.screenpm);
620 _x.screenpm = _x.nextscreenpm;
621 qunlock(&_x.screenlock);
624 if(r.min.x >= r.max.x || r.min.y >= r.max.y)
626 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
627 Dx(r), Dy(r), r.min.x, r.min.y);
632 xexpose(XEvent *e, XDisplay *xd)
637 qlock(&_x.screenlock);
638 if(_x.screenpm != _x.nextscreenpm){
639 qunlock(&_x.screenlock);
642 xe = (XExposeEvent*)e;
645 r.max.x = xe->x+xe->width;
646 r.max.y = xe->y+xe->height;
647 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
648 Dx(r), Dy(r), r.min.x, r.min.y);
650 qunlock(&_x.screenlock);
654 xdestroy(XEvent *e, XDisplay *xd)
656 XDestroyWindowEvent *xe;
658 xe = (XDestroyWindowEvent*)e;
659 if(xe->window == _x.drawable){
667 xconfigure(XEvent *e, XDisplay *xd)
670 XConfigureEvent *xe = (XConfigureEvent*)e;
672 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
674 if(xe->width==0 || xe->height==0)
675 fprint(2, "ignoring resize to %dx%d\n", xe->width, xe->height);
676 r = Rect(0, 0, xe->width, xe->height);
677 qlock(&_x.screenlock);
678 if(_x.screenpm != _x.nextscreenpm){
679 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
680 Dx(r), Dy(r), r.min.x, r.min.y);
683 qunlock(&_x.screenlock);
689 xreplacescreenimage(void)
696 if(eqrect(_x.screenr, r))
699 pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
700 m = xallocmemimage(r, _x.chan, pixmap);
701 if(_x.nextscreenpm != _x.screenpm)
702 XFreePixmap(_x.display, _x.nextscreenpm);
703 _x.nextscreenpm = pixmap;
705 _drawreplacescreenimage(m);