Blob


1 /*
2 * Some of the stuff in this file is not X-dependent and should be elsewhere.
3 */
4 #include "x11-inc.h"
5 #include <u.h>
6 #include <libc.h>
7 #include <draw.h>
8 #include <memdraw.h>
9 #include <keyboard.h>
10 #include <mouse.h>
11 #include <cursor.h>
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*);
21 Xprivate _x;
23 Display*
24 _initdisplay(void (*error)(Display*, char*), char *label)
25 {
26 Display *d;
27 Memimage *m;
29 memimageinit();
31 d = mallocz(sizeof(Display), 1);
32 if(d == nil)
33 return nil;
35 d->buf = malloc(16000+5);
36 d->obuf = malloc(16000);
37 if(d->buf == nil || d->obuf == nil){
38 free(d->buf);
39 free(d->obuf);
40 free(d);
41 return nil;
42 }
43 d->bufsize = 16000;
44 d->obufsize = 16000;
45 d->bufp = d->buf;
46 d->obufp = d->obuf;
48 m = xattach(label);
49 if(m == nil){
50 free(d);
51 return nil;
52 }
54 d->error = error;
55 _initdisplaymemimage(d, m);
56 d->screenimage = getimage0(d);
57 return d;
58 }
60 static Image*
61 getimage0(Display *d)
62 {
63 char *a, info[12*12+1];
64 int n;
65 Image *image;
67 a = bufimage(d, 2);
68 a[0] = 'J';
69 a[1] = 'I';
70 if(flushimage(d, 0) < 0){
71 fprint(2, "cannot read screen info: %r\n");
72 abort();
73 }
75 n = _drawmsgread(d, info, sizeof info);
76 if(n != 12*12){
77 fprint(2, "short screen info\n");
78 abort();
79 }
81 image = mallocz(sizeof(Image), 1);
82 image->display = d;
83 image->id = 0;
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);
95 return image;
96 }
98 int
99 getwindow(Display *d, int ref)
101 Image *i;
103 if(_x.destroyed)
104 postnote(PNGROUP, getpgrp(), "hangup");
105 if(xreplacescreenimage() == 0)
106 return 0;
107 freeimage(d->screenimage);
108 i = getimage0(d);
109 screen = d->screenimage = d->image = i;
110 return 0;
113 static int
114 xerror(XDisplay *d, XErrorEvent *e)
116 char buf[200];
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);
121 print("%s\n", buf);
122 return 0;
125 static int
126 xioerror(XDisplay *d)
128 print("X I/O error\n");
129 abort();
130 return -1;
134 static Memimage*
135 xattach(char *label)
137 char *argv[2], *disp;
138 int i, n, xrootid;
139 Rectangle r;
140 XClassHint classhint;
141 XDrawable pmid;
142 XPixmapFormatValues *pfmt;
143 XScreen *xscreen;
144 XSetWindowAttributes attr;
145 XSizeHints normalhint;
146 XTextProperty name;
147 XVisualInfo xvi;
148 XWindow xrootwin;
149 XWMHints hint;
151 /*
152 if(XInitThreads() == 0){
153 fprint(2, "XInitThreads failed\n");
154 abort();
156 */
158 /*
159 * Connect to X server.
160 */
161 _x.display = XOpenDisplay(NULL);
162 if(_x.display == nil){
163 disp = getenv("DISPLAY");
164 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
165 free(disp);
166 return nil;
168 XSetErrorHandler(xerror);
169 XSetIOErrorHandler(xioerror);
170 xrootid = DefaultScreen(_x.display);
171 xrootwin = DefaultRootWindow(_x.display);
173 /*
174 * Figure out underlying screen format.
175 */
176 _x.depth = DefaultDepth(_x.display, xrootid);
177 if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
178 || XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
179 _x.vis = xvi.visual;
180 _x.depth = 16;
181 _x.usetable = 1;
183 else
184 if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
185 || XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
186 _x.vis = xvi.visual;
187 _x.depth = 15;
188 _x.usetable = 1;
190 else
191 if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
192 || XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
193 _x.vis = xvi.visual;
194 _x.depth = 24;
195 _x.usetable = 1;
197 else
198 if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
199 || XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
200 if(_x.depth > 8){
201 werrstr("can't deal with colormapped depth %d screens",
202 _x.depth);
203 goto err0;
205 _x.vis = xvi.visual;
206 _x.depth = 8;
208 else{
209 if(_x.depth != 8){
210 werrstr("can't understand depth %d screen", _x.depth);
211 goto err0;
213 _x.vis = DefaultVisual(_x.display, xrootid);
216 /*
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
220 * per pixel.
221 */
222 _x.chan = 0;
223 pfmt = XListPixmapFormats(_x.display, &n);
224 for(i=0; i<n; i++){
225 if(pfmt[i].depth == _x.depth){
226 switch(pfmt[i].bits_per_pixel){
227 case 1: /* untested */
228 _x.chan = GREY1;
229 break;
230 case 2: /* untested */
231 _x.chan = GREY2;
232 break;
233 case 4: /* untested */
234 _x.chan = GREY4;
235 break;
236 case 8:
237 _x.chan = CMAP8;
238 break;
239 case 15:
240 _x.chan = RGB15;
241 break;
242 case 16: /* how to tell RGB15? */
243 _x.chan = RGB16;
244 break;
245 case 24: /* untested (impossible?) */
246 _x.chan = RGB24;
247 break;
248 case 32:
249 _x.chan = XRGB32;
250 break;
254 if(_x.chan == 0){
255 werrstr("could not determine screen pixel format");
256 goto err0;
259 /*
260 * Set up color map if necessary.
261 */
262 xscreen = DefaultScreenOfDisplay(_x.display);
263 _x.cmap = DefaultColormapOfScreen(xscreen);
264 if(_x.vis->class != StaticColor){
265 plan9cmap();
266 setupcmap(xrootwin);
269 /*
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.
273 */
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 */
284 0, /* x */
285 0, /* y */
286 Dx(r), /* width */
287 Dy(r), /* height */
288 0, /* border width */
289 DefaultDepthOfScreen(xscreen), /* depth */
290 InputOutput, /* class */
291 _x.vis, /* visual */
292 /* valuemask */
293 CWBackPixel|CWBorderPixel|CWColormap,
294 &attr /* attributes (the above aren't?!) */
295 );
297 /*
298 * Label and other properties required by ICCCCM.
299 */
300 memset(&name, 0, sizeof name);
301 if(label == nil)
302 label = "pjw-face-here";
303 name.value = (uchar*)label;
304 name.encoding = XA_STRING;
305 name.format = 8;
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;
315 hint.input = 1;
316 hint.initial_state = NormalState;
318 memset(&classhint, 0, sizeof classhint);
319 classhint.res_name = label;
320 classhint.res_class = label;
322 argv[0] = label;
323 argv[1] = nil;
325 XSetWMProperties(
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 */
331 1, /* argc */
332 &normalhint, /* XA_WM_NORMAL_HINTS */
333 &hint, /* XA_WM_HINTS */
334 &classhint /* XA_WM_CLASSHINTS */
335 );
336 XFlush(_x.display);
338 /*
339 * Allocate our local backing store.
340 */
341 _x.screenr = r;
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);
346 /*
347 * Allocate some useful graphics contexts for the future.
348 */
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);
363 /*
364 * Put the window on the screen.
365 */
366 XMapWindow(_x.display, _x.drawable);
367 XFlush(_x.display);
369 /*
370 * Look up clipboard atom.
371 */
372 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", True);
374 /*
375 * Lots of display connections for various procs.
376 */
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;
389 err0:
390 fprint(2, "%r\n");
391 /*
392 * Should do a better job of cleaning up here.
393 */
394 XCloseDisplay(_x.display);
395 return nil;
398 int
399 drawsetlabel(Display *d, char *label)
401 char *argv[2];
402 XClassHint classhint;
403 XTextProperty name;
405 /*
406 * Label and other properties required by ICCCCM.
407 */
408 memset(&name, 0, sizeof name);
409 if(label == nil)
410 label = "pjw-face-here";
411 name.value = (uchar*)label;
412 name.encoding = XA_STRING;
413 name.format = 8;
414 name.nitems = strlen(name.value);
416 memset(&classhint, 0, sizeof classhint);
417 classhint.res_name = label;
418 classhint.res_class = label;
420 argv[0] = label;
421 argv[1] = nil;
423 XSetWMProperties(
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 */
429 1, /* argc */
430 nil, /* XA_WM_NORMAL_HINTS */
431 nil, /* XA_WM_HINTS */
432 &classhint /* XA_WM_CLASSHINTS */
433 );
434 XFlush(_x.display);
435 return 0;
438 /*
439 * Create a GC with a particular fill style and XXX.
440 * Disable generation of GraphicsExpose/NoExpose events in the GC.
441 */
442 static XGC
443 xgc(XDrawable d, int fillstyle, int foreground)
445 XGC gc;
446 XGCValues v;
448 memset(&v, 0, sizeof v);
449 v.function = GXcopy;
450 v.graphics_exposures = False;
451 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
452 if(fillstyle != -1)
453 XSetFillStyle(_x.display, gc, fillstyle);
454 if(foreground != -1)
455 XSetForeground(_x.display, gc, 0);
456 return gc;
460 /*
461 * Initialize map with the Plan 9 rgbv color map.
462 */
463 static void
464 plan9cmap(void)
466 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
467 static int once;
469 if(once)
470 return;
471 once = 1;
473 for(r=0; r!=4; r++)
474 for(g = 0; g != 4; g++)
475 for(b = 0; b!=4; b++)
476 for(v = 0; v!=4; v++){
477 den=r;
478 if(g > den)
479 den=g;
480 if(b > den)
481 den=b;
482 /* divide check -- pick grey shades */
483 if(den==0)
484 cr=cg=cb=v*17;
485 else {
486 num=17*(4*den+v);
487 cr=r*num/den;
488 cg=g*num/den;
489 cb=b*num/den;
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;
498 v7 = v >> 1;
499 idx7 = r*32 + v7*16 + g*4 + b;
500 if((v & 1) == v7){
501 _x.map7to8[idx7][0] = idx;
502 if(den == 0) { /* divide check -- pick grey shades */
503 cr = ((255.0/7.0)*v7)+0.5;
504 cg = cr;
505 cb = cr;
507 else {
508 num=17*15*(4*den+v7*2)/14;
509 cr=r*num/den;
510 cg=g*num/den;
511 cb=b*num/den;
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;
519 else
520 _x.map7to8[idx7][1] = idx;
524 /*
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
527 * cursor focus.
528 */
529 static int
530 setupcmap(XWindow w)
532 char buf[30];
533 int i;
534 u32int p, pp;
535 XColor c;
537 if(_x.depth <= 1)
538 return 0;
540 if(_x.depth >= 24) {
541 /*
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.
551 */
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");
556 return -1;
558 p = c.pixel;
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");
565 return -1;
568 switch(_x.chan){
569 case RGB24:
570 _x.chan = BGR24;
571 break;
572 case XRGB32:
573 _x.chan = XBGR32;
574 break;
575 default:
576 werrstr("cannot byteswap channel %s",
577 chantostr(buf, _x.chan));
578 break;
581 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
582 /*
583 * Do nothing. We have no way to express a
584 * mixed-endian 16-bit screen, so pretend they don't exist.
585 */
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++){
591 _x.tox11[i] = i;
592 _x.toplan9[i] = i;
594 }else{
595 for(i = 0; i < 128; i++){
596 c = _x.map7[i];
597 if(!XAllocColor(_x.display, _x.cmap, &c)){
598 werrstr("can't allocate colors in 7-bit map");
599 return -1;
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];
606 }else{
607 werrstr("unsupported visual class %d", _x.vis->class);
608 return -1;
610 return 0;
613 void
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)
625 return;
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);
628 XFlush(_x.display);
631 void
632 xexpose(XEvent *e, XDisplay *xd)
634 XExposeEvent *xe;
635 Rectangle r;
637 qlock(&_x.screenlock);
638 if(_x.screenpm != _x.nextscreenpm){
639 qunlock(&_x.screenlock);
640 return;
642 xe = (XExposeEvent*)e;
643 r.min.x = xe->x;
644 r.min.y = xe->y;
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);
649 XSync(xd, False);
650 qunlock(&_x.screenlock);
653 int
654 xdestroy(XEvent *e, XDisplay *xd)
656 XDestroyWindowEvent *xe;
658 xe = (XDestroyWindowEvent*)e;
659 if(xe->window == _x.drawable){
660 _x.destroyed = 1;
661 return 1;
663 return 0;
666 int
667 xconfigure(XEvent *e, XDisplay *xd)
669 Rectangle r;
670 XConfigureEvent *xe = (XConfigureEvent*)e;
672 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
673 return 0;
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);
681 XSync(xd, False);
683 qunlock(&_x.screenlock);
684 _x.newscreenr = r;
685 return 1;
688 static int
689 xreplacescreenimage(void)
691 Memimage *m;
692 XDrawable pixmap;
693 Rectangle r;
695 r = _x.newscreenr;
696 if(eqrect(_x.screenr, r))
697 return 0;
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;
704 _x.screenr = r;
705 _drawreplacescreenimage(m);
706 return 1;