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 /*
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.
36 */
37 rfork(RFNOTEG);
38 memimageinit();
40 d = mallocz(sizeof(Display), 1);
41 if(d == nil)
42 return nil;
44 d->buf = malloc(16000+5);
45 d->obuf = malloc(16000);
46 if(d->buf == nil || d->obuf == nil){
47 free(d->buf);
48 free(d->obuf);
49 free(d);
50 return nil;
51 }
52 d->bufsize = 16000;
53 d->obufsize = 16000;
54 d->bufp = d->buf;
55 d->obufp = d->obuf;
57 m = xattach(label);
58 if(m == nil){
59 free(d);
60 return nil;
61 }
63 d->error = error;
64 _initdisplaymemimage(d, m);
65 d->screenimage = getimage0(d);
66 return d;
67 }
69 static Image*
70 getimage0(Display *d)
71 {
72 char *a, info[12*12+1];
73 int n;
74 Image *image;
76 a = bufimage(d, 2);
77 a[0] = 'J';
78 a[1] = 'I';
79 if(flushimage(d, 0) < 0){
80 fprint(2, "cannot read screen info: %r\n");
81 abort();
82 }
84 n = _drawmsgread(d, info, sizeof info);
85 if(n != 12*12){
86 fprint(2, "short screen info\n");
87 abort();
88 }
90 image = mallocz(sizeof(Image), 1);
91 image->display = d;
92 image->id = 0;
93 image->chan = strtochan(info+2*12);
94 image->depth = chantodepth(image->chan);
95 image->repl = atoi(info+3*12);
96 image->r.min.x = atoi(info+4*12);
97 image->r.min.y = atoi(info+5*12);
98 image->r.max.x = atoi(info+6*12);
99 image->r.max.y = atoi(info+7*12);
100 image->clipr.min.x = atoi(info+8*12);
101 image->clipr.min.y = atoi(info+9*12);
102 image->clipr.max.x = atoi(info+10*12);
103 image->clipr.max.y = atoi(info+11*12);
104 return image;
107 int
108 getwindow(Display *d, int ref)
110 Image *i;
112 if(_x.destroyed)
113 postnote(PNGROUP, getpgrp(), "hangup");
114 if(xreplacescreenimage() == 0)
115 return 0;
116 freeimage(d->screenimage);
117 i = getimage0(d);
118 screen = d->screenimage = d->image = i;
119 return 0;
122 static int
123 xerror(XDisplay *d, XErrorEvent *e)
125 char buf[200];
127 if(e->request_code == 42) /* XSetInputFocus */
128 return 0;
129 print("X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
130 e->error_code, e->request_code, e->minor_code, d);
131 XGetErrorText(d, e->error_code, buf, sizeof buf);
132 print("%s\n", buf);
133 return 0;
136 static int
137 xioerror(XDisplay *d)
139 print("X I/O error\n");
140 abort();
141 return -1;
145 static Memimage*
146 xattach(char *label)
148 char *argv[2], *disp;
149 int i, n, xrootid;
150 Rectangle r;
151 XClassHint classhint;
152 XDrawable pmid;
153 XPixmapFormatValues *pfmt;
154 XScreen *xscreen;
155 XSetWindowAttributes attr;
156 XSizeHints normalhint;
157 XTextProperty name;
158 XVisualInfo xvi;
159 XWindow xrootwin;
160 XWindowAttributes wattr;
161 XWMHints hint;
163 /*
164 if(XInitThreads() == 0){
165 fprint(2, "XInitThreads failed\n");
166 abort();
168 */
170 /*
171 * Connect to X server.
172 */
173 _x.display = XOpenDisplay(NULL);
174 if(_x.display == nil){
175 disp = getenv("DISPLAY");
176 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
177 free(disp);
178 return nil;
180 XSetErrorHandler(xerror);
181 XSetIOErrorHandler(xioerror);
182 xrootid = DefaultScreen(_x.display);
183 xrootwin = DefaultRootWindow(_x.display);
185 /*
186 * Figure out underlying screen format.
187 */
188 _x.depth = DefaultDepth(_x.display, xrootid);
189 if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
190 || XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
191 _x.vis = xvi.visual;
192 _x.depth = 16;
193 _x.usetable = 1;
195 else
196 if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
197 || XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
198 _x.vis = xvi.visual;
199 _x.depth = 15;
200 _x.usetable = 1;
202 else
203 if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
204 || XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
205 _x.vis = xvi.visual;
206 _x.depth = 24;
207 _x.usetable = 1;
209 else
210 if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
211 || XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
212 if(_x.depth > 8){
213 werrstr("can't deal with colormapped depth %d screens",
214 _x.depth);
215 goto err0;
217 _x.vis = xvi.visual;
218 _x.depth = 8;
220 else{
221 if(_x.depth != 8){
222 werrstr("can't understand depth %d screen", _x.depth);
223 goto err0;
225 _x.vis = DefaultVisual(_x.display, xrootid);
228 /*
229 * _x.depth is only the number of significant pixel bits,
230 * not the total number of pixel bits. We need to walk the
231 * display list to find how many actual bits are used
232 * per pixel.
233 */
234 _x.chan = 0;
235 pfmt = XListPixmapFormats(_x.display, &n);
236 for(i=0; i<n; i++){
237 if(pfmt[i].depth == _x.depth){
238 switch(pfmt[i].bits_per_pixel){
239 case 1: /* untested */
240 _x.chan = GREY1;
241 break;
242 case 2: /* untested */
243 _x.chan = GREY2;
244 break;
245 case 4: /* untested */
246 _x.chan = GREY4;
247 break;
248 case 8:
249 _x.chan = CMAP8;
250 break;
251 case 15:
252 _x.chan = RGB15;
253 break;
254 case 16: /* how to tell RGB15? */
255 _x.chan = RGB16;
256 break;
257 case 24: /* untested (impossible?) */
258 _x.chan = RGB24;
259 break;
260 case 32:
261 _x.chan = XRGB32;
262 break;
266 if(_x.chan == 0){
267 werrstr("could not determine screen pixel format");
268 goto err0;
271 /*
272 * Set up color map if necessary.
273 */
274 xscreen = DefaultScreenOfDisplay(_x.display);
275 _x.cmap = DefaultColormapOfScreen(xscreen);
276 if(_x.vis->class != StaticColor){
277 plan9cmap();
278 setupcmap(xrootwin);
281 /*
282 * We get to choose the initial rectangle size.
283 * This is arbitrary. In theory we should read the
284 * command line and allow the traditional X options.
285 */
286 r = Rect(0, 0, WidthOfScreen(xscreen)*3/4,
287 HeightOfScreen(xscreen)*3/4);
289 memset(&attr, 0, sizeof attr);
290 attr.colormap = _x.cmap;
291 attr.background_pixel = ~0;
292 attr.border_pixel = 0;
293 _x.drawable = XCreateWindow(
294 _x.display, /* display */
295 xrootwin, /* parent */
296 0, /* x */
297 0, /* y */
298 Dx(r), /* width */
299 Dy(r), /* height */
300 0, /* border width */
301 DefaultDepthOfScreen(xscreen), /* depth */
302 InputOutput, /* class */
303 _x.vis, /* visual */
304 /* valuemask */
305 CWBackPixel|CWBorderPixel|CWColormap,
306 &attr /* attributes (the above aren't?!) */
307 );
309 if(!XGetWindowAttributes(_x.display, _x.drawable, &wattr))
310 fprint(2, "XGetWindowAttributes failed\n");
311 else if(wattr.width && wattr.height){
312 r.max.x = wattr.width;
313 r.max.y = wattr.height;
314 if(0) fprint(2, "new rect %dx%d\n", r.max.x, r.max.y);
317 /*
318 * Label and other properties required by ICCCCM.
319 */
320 memset(&name, 0, sizeof name);
321 if(label == nil)
322 label = "pjw-face-here";
323 name.value = (uchar*)label;
324 name.encoding = XA_STRING;
325 name.format = 8;
326 name.nitems = strlen(name.value);
328 memset(&normalhint, 0, sizeof normalhint);
329 normalhint.flags = USSize|PMaxSize;
330 normalhint.max_width = WidthOfScreen(xscreen);
331 normalhint.max_height = HeightOfScreen(xscreen);
333 memset(&hint, 0, sizeof hint);
334 hint.flags = InputHint|StateHint;
335 hint.input = 1;
336 hint.initial_state = NormalState;
338 memset(&classhint, 0, sizeof classhint);
339 classhint.res_name = label;
340 classhint.res_class = label;
342 argv[0] = label;
343 argv[1] = nil;
345 XSetWMProperties(
346 _x.display, /* display */
347 _x.drawable, /* window */
348 &name, /* XA_WM_NAME property */
349 &name, /* XA_WM_ICON_NAME property */
350 argv, /* XA_WM_COMMAND */
351 1, /* argc */
352 &normalhint, /* XA_WM_NORMAL_HINTS */
353 &hint, /* XA_WM_HINTS */
354 &classhint /* XA_WM_CLASSHINTS */
355 );
356 XFlush(_x.display);
358 /*
359 * Allocate our local backing store.
360 */
361 _x.screenr = r;
362 _x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
363 _x.nextscreenpm = _x.screenpm;
364 _x.screenimage = _xallocmemimage(r, _x.chan, _x.screenpm);
366 /*
367 * Allocate some useful graphics contexts for the future.
368 */
369 _x.gcfill = xgc(_x.screenpm, FillSolid, -1);
370 _x.gccopy = xgc(_x.screenpm, -1, -1);
371 _x.gcsimplesrc = xgc(_x.screenpm, FillStippled, -1);
372 _x.gczero = xgc(_x.screenpm, -1, -1);
373 _x.gcreplsrc = xgc(_x.screenpm, FillTiled, -1);
375 pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
376 _x.gcfill0 = xgc(pmid, FillSolid, 0);
377 _x.gccopy0 = xgc(pmid, -1, -1);
378 _x.gcsimplesrc0 = xgc(pmid, FillStippled, -1);
379 _x.gczero0 = xgc(pmid, -1, -1);
380 _x.gcreplsrc0 = xgc(pmid, FillTiled, -1);
381 XFreePixmap(_x.display, pmid);
383 /*
384 * Put the window on the screen.
385 */
386 XMapWindow(_x.display, _x.drawable);
387 XFlush(_x.display);
389 /*
390 * Look up clipboard atom.
391 */
392 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", False);
393 _x.utf8string = XInternAtom(_x.display, "UTF8_STRING", False);
394 _x.targets = XInternAtom(_x.display, "TARGETS", False);
395 _x.text = XInternAtom(_x.display, "TEXT", False);
396 _x.compoundtext = XInternAtom(_x.display, "COMPOUND_TEXT", False);
398 /*
399 * Lots of display connections for various procs.
400 */
401 _x.kbdcon = XOpenDisplay(NULL);
402 _x.mousecon = XOpenDisplay(NULL);
403 _x.snarfcon = XOpenDisplay(NULL);
405 if(0) fprint(2, "x: display=%p kbd=%p mouse=%p snarf=%p\n",
406 _x.display, _x.kbdcon, _x.mousecon, _x.snarfcon);
408 _x.black = xscreen->black_pixel;
409 _x.white = xscreen->white_pixel;
411 return _x.screenimage;
413 err0:
414 fprint(2, "%r\n");
415 /*
416 * Should do a better job of cleaning up here.
417 */
418 XCloseDisplay(_x.display);
419 return nil;
422 int
423 drawsetlabel(Display *d, char *label)
425 char *argv[2];
426 XClassHint classhint;
427 XTextProperty name;
429 /*
430 * Label and other properties required by ICCCCM.
431 */
432 memset(&name, 0, sizeof name);
433 if(label == nil)
434 label = "pjw-face-here";
435 name.value = (uchar*)label;
436 name.encoding = XA_STRING;
437 name.format = 8;
438 name.nitems = strlen(name.value);
440 memset(&classhint, 0, sizeof classhint);
441 classhint.res_name = label;
442 classhint.res_class = label;
444 argv[0] = label;
445 argv[1] = nil;
447 XSetWMProperties(
448 _x.display, /* display */
449 _x.drawable, /* window */
450 &name, /* XA_WM_NAME property */
451 &name, /* XA_WM_ICON_NAME property */
452 argv, /* XA_WM_COMMAND */
453 1, /* argc */
454 nil, /* XA_WM_NORMAL_HINTS */
455 nil, /* XA_WM_HINTS */
456 &classhint /* XA_WM_CLASSHINTS */
457 );
458 XFlush(_x.display);
459 return 0;
462 /*
463 * Create a GC with a particular fill style and XXX.
464 * Disable generation of GraphicsExpose/NoExpose events in the GC.
465 */
466 static XGC
467 xgc(XDrawable d, int fillstyle, int foreground)
469 XGC gc;
470 XGCValues v;
472 memset(&v, 0, sizeof v);
473 v.function = GXcopy;
474 v.graphics_exposures = False;
475 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
476 if(fillstyle != -1)
477 XSetFillStyle(_x.display, gc, fillstyle);
478 if(foreground != -1)
479 XSetForeground(_x.display, gc, 0);
480 return gc;
484 /*
485 * Initialize map with the Plan 9 rgbv color map.
486 */
487 static void
488 plan9cmap(void)
490 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
491 static int once;
493 if(once)
494 return;
495 once = 1;
497 for(r=0; r!=4; r++)
498 for(g = 0; g != 4; g++)
499 for(b = 0; b!=4; b++)
500 for(v = 0; v!=4; v++){
501 den=r;
502 if(g > den)
503 den=g;
504 if(b > den)
505 den=b;
506 /* divide check -- pick grey shades */
507 if(den==0)
508 cr=cg=cb=v*17;
509 else {
510 num=17*(4*den+v);
511 cr=r*num/den;
512 cg=g*num/den;
513 cb=b*num/den;
515 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
516 _x.map[idx].red = cr*0x0101;
517 _x.map[idx].green = cg*0x0101;
518 _x.map[idx].blue = cb*0x0101;
519 _x.map[idx].pixel = idx;
520 _x.map[idx].flags = DoRed|DoGreen|DoBlue;
522 v7 = v >> 1;
523 idx7 = r*32 + v7*16 + g*4 + b;
524 if((v & 1) == v7){
525 _x.map7to8[idx7][0] = idx;
526 if(den == 0) { /* divide check -- pick grey shades */
527 cr = ((255.0/7.0)*v7)+0.5;
528 cg = cr;
529 cb = cr;
531 else {
532 num=17*15*(4*den+v7*2)/14;
533 cr=r*num/den;
534 cg=g*num/den;
535 cb=b*num/den;
537 _x.map7[idx7].red = cr*0x0101;
538 _x.map7[idx7].green = cg*0x0101;
539 _x.map7[idx7].blue = cb*0x0101;
540 _x.map7[idx7].pixel = idx7;
541 _x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
543 else
544 _x.map7to8[idx7][1] = idx;
548 /*
549 * Initialize and install the rgbv color map as a private color map
550 * for this application. It gets the best colors when it has the
551 * cursor focus.
552 */
553 static int
554 setupcmap(XWindow w)
556 char buf[30];
557 int i;
558 u32int p, pp;
559 XColor c;
561 if(_x.depth <= 1)
562 return 0;
564 if(_x.depth >= 24) {
565 /*
566 * The pixel value returned from XGetPixel needs to
567 * be converted to RGB so we can call rgb2cmap()
568 * to translate between 24 bit X and our color. Unfortunately,
569 * the return value appears to be display server endian
570 * dependant. Therefore, we run some heuristics to later
571 * determine how to mask the int value correctly.
572 * Yeah, I know we can look at _x.vis->byte_order but
573 * some displays say MSB even though they run on LSB.
574 * Besides, this is more anal.
575 */
577 c = _x.map[19]; /* known to have different R, G, B values */
578 if(!XAllocColor(_x.display, _x.cmap, &c)){
579 werrstr("XAllocColor: %r");
580 return -1;
582 p = c.pixel;
583 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
584 if(pp != _x.map[19].pixel) {
585 /* check if endian is other way */
586 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
587 if(pp != _x.map[19].pixel){
588 werrstr("cannot detect X server byte order");
589 return -1;
592 switch(_x.chan){
593 case RGB24:
594 _x.chan = BGR24;
595 break;
596 case XRGB32:
597 _x.chan = XBGR32;
598 break;
599 default:
600 werrstr("cannot byteswap channel %s",
601 chantostr(buf, _x.chan));
602 break;
605 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
606 /*
607 * Do nothing. We have no way to express a
608 * mixed-endian 16-bit screen, so pretend they don't exist.
609 */
610 }else if(_x.vis->class == PseudoColor){
611 if(_x.usetable == 0){
612 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll);
613 XStoreColors(_x.display, _x.cmap, _x.map, 256);
614 for(i = 0; i < 256; i++){
615 _x.tox11[i] = i;
616 _x.toplan9[i] = i;
618 }else{
619 for(i = 0; i < 128; i++){
620 c = _x.map7[i];
621 if(!XAllocColor(_x.display, _x.cmap, &c)){
622 werrstr("can't allocate colors in 7-bit map");
623 return -1;
625 _x.tox11[_x.map7to8[i][0]] = c.pixel;
626 _x.tox11[_x.map7to8[i][1]] = c.pixel;
627 _x.toplan9[c.pixel] = _x.map7to8[i][0];
630 }else{
631 werrstr("unsupported visual class %d", _x.vis->class);
632 return -1;
634 return 0;
637 void
638 flushmemscreen(Rectangle r)
640 if(_x.nextscreenpm != _x.screenpm){
641 qlock(&_x.screenlock);
642 XSync(_x.display, False);
643 XFreePixmap(_x.display, _x.screenpm);
644 _x.screenpm = _x.nextscreenpm;
645 qunlock(&_x.screenlock);
648 if(r.min.x >= r.max.x || r.min.y >= r.max.y)
649 return;
650 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
651 Dx(r), Dy(r), r.min.x, r.min.y);
652 XFlush(_x.display);
655 void
656 _xexpose(XEvent *e, XDisplay *xd)
658 XExposeEvent *xe;
659 Rectangle r;
661 qlock(&_x.screenlock);
662 if(_x.screenpm != _x.nextscreenpm){
663 qunlock(&_x.screenlock);
664 return;
666 xe = (XExposeEvent*)e;
667 r.min.x = xe->x;
668 r.min.y = xe->y;
669 r.max.x = xe->x+xe->width;
670 r.max.y = xe->y+xe->height;
671 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
672 Dx(r), Dy(r), r.min.x, r.min.y);
673 XSync(xd, False);
674 qunlock(&_x.screenlock);
677 int
678 _xdestroy(XEvent *e, XDisplay *xd)
680 XDestroyWindowEvent *xe;
682 xe = (XDestroyWindowEvent*)e;
683 if(xe->window == _x.drawable){
684 _x.destroyed = 1;
685 return 1;
687 return 0;
690 int
691 _xconfigure(XEvent *e, XDisplay *xd)
693 Rectangle r;
694 XConfigureEvent *xe = (XConfigureEvent*)e;
696 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
697 return 0;
698 if(xe->width==0 || xe->height==0)
699 fprint(2, "ignoring resize to %dx%d\n", xe->width, xe->height);
700 r = Rect(0, 0, xe->width, xe->height);
701 qlock(&_x.screenlock);
702 if(_x.screenpm != _x.nextscreenpm){
703 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
704 Dx(r), Dy(r), r.min.x, r.min.y);
705 XSync(xd, False);
707 qunlock(&_x.screenlock);
708 _x.newscreenr = r;
709 return 1;
712 static int
713 xreplacescreenimage(void)
715 Memimage *m;
716 XDrawable pixmap;
717 Rectangle r;
719 r = _x.newscreenr;
720 if(eqrect(_x.screenr, r))
721 return 0;
723 pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
724 m = _xallocmemimage(r, _x.chan, pixmap);
725 if(_x.nextscreenpm != _x.screenpm)
726 XFreePixmap(_x.display, _x.nextscreenpm);
727 _x.nextscreenpm = pixmap;
728 _x.screenr = r;
729 _drawreplacescreenimage(m);
730 return 1;