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 print("X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
128 e->error_code, e->request_code, e->minor_code, d);
129 XGetErrorText(d, e->error_code, buf, sizeof buf);
130 print("%s\n", buf);
131 return 0;
134 static int
135 xioerror(XDisplay *d)
137 print("X I/O error\n");
138 abort();
139 return -1;
143 static Memimage*
144 xattach(char *label)
146 char *argv[2], *disp;
147 int i, n, xrootid;
148 Rectangle r;
149 XClassHint classhint;
150 XDrawable pmid;
151 XPixmapFormatValues *pfmt;
152 XScreen *xscreen;
153 XSetWindowAttributes attr;
154 XSizeHints normalhint;
155 XTextProperty name;
156 XVisualInfo xvi;
157 XWindow xrootwin;
158 XWindowAttributes wattr;
159 XWMHints hint;
161 /*
162 if(XInitThreads() == 0){
163 fprint(2, "XInitThreads failed\n");
164 abort();
166 */
168 /*
169 * Connect to X server.
170 */
171 _x.display = XOpenDisplay(NULL);
172 if(_x.display == nil){
173 disp = getenv("DISPLAY");
174 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
175 free(disp);
176 return nil;
178 XSetErrorHandler(xerror);
179 XSetIOErrorHandler(xioerror);
180 xrootid = DefaultScreen(_x.display);
181 xrootwin = DefaultRootWindow(_x.display);
183 /*
184 * Figure out underlying screen format.
185 */
186 _x.depth = DefaultDepth(_x.display, xrootid);
187 if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
188 || XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
189 _x.vis = xvi.visual;
190 _x.depth = 16;
191 _x.usetable = 1;
193 else
194 if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
195 || XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
196 _x.vis = xvi.visual;
197 _x.depth = 15;
198 _x.usetable = 1;
200 else
201 if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
202 || XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
203 _x.vis = xvi.visual;
204 _x.depth = 24;
205 _x.usetable = 1;
207 else
208 if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
209 || XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
210 if(_x.depth > 8){
211 werrstr("can't deal with colormapped depth %d screens",
212 _x.depth);
213 goto err0;
215 _x.vis = xvi.visual;
216 _x.depth = 8;
218 else{
219 if(_x.depth != 8){
220 werrstr("can't understand depth %d screen", _x.depth);
221 goto err0;
223 _x.vis = DefaultVisual(_x.display, xrootid);
226 /*
227 * _x.depth is only the number of significant pixel bits,
228 * not the total number of pixel bits. We need to walk the
229 * display list to find how many actual bits are used
230 * per pixel.
231 */
232 _x.chan = 0;
233 pfmt = XListPixmapFormats(_x.display, &n);
234 for(i=0; i<n; i++){
235 if(pfmt[i].depth == _x.depth){
236 switch(pfmt[i].bits_per_pixel){
237 case 1: /* untested */
238 _x.chan = GREY1;
239 break;
240 case 2: /* untested */
241 _x.chan = GREY2;
242 break;
243 case 4: /* untested */
244 _x.chan = GREY4;
245 break;
246 case 8:
247 _x.chan = CMAP8;
248 break;
249 case 15:
250 _x.chan = RGB15;
251 break;
252 case 16: /* how to tell RGB15? */
253 _x.chan = RGB16;
254 break;
255 case 24: /* untested (impossible?) */
256 _x.chan = RGB24;
257 break;
258 case 32:
259 _x.chan = XRGB32;
260 break;
264 if(_x.chan == 0){
265 werrstr("could not determine screen pixel format");
266 goto err0;
269 /*
270 * Set up color map if necessary.
271 */
272 xscreen = DefaultScreenOfDisplay(_x.display);
273 _x.cmap = DefaultColormapOfScreen(xscreen);
274 if(_x.vis->class != StaticColor){
275 plan9cmap();
276 setupcmap(xrootwin);
279 /*
280 * We get to choose the initial rectangle size.
281 * This is arbitrary. In theory we should read the
282 * command line and allow the traditional X options.
283 */
284 r = Rect(0, 0, WidthOfScreen(xscreen)*3/4,
285 HeightOfScreen(xscreen)*3/4);
287 memset(&attr, 0, sizeof attr);
288 attr.colormap = _x.cmap;
289 attr.background_pixel = ~0;
290 attr.border_pixel = 0;
291 _x.drawable = XCreateWindow(
292 _x.display, /* display */
293 xrootwin, /* parent */
294 0, /* x */
295 0, /* y */
296 Dx(r), /* width */
297 Dy(r), /* height */
298 0, /* border width */
299 DefaultDepthOfScreen(xscreen), /* depth */
300 InputOutput, /* class */
301 _x.vis, /* visual */
302 /* valuemask */
303 CWBackPixel|CWBorderPixel|CWColormap,
304 &attr /* attributes (the above aren't?!) */
305 );
307 if(!XGetWindowAttributes(_x.display, _x.drawable, &wattr))
308 fprint(2, "XGetWindowAttributes failed\n");
309 else if(wattr.width && wattr.height){
310 r.max.x = wattr.width;
311 r.max.y = wattr.height;
312 if(0) fprint(2, "new rect %dx%d\n", r.max.x, r.max.y);
315 /*
316 * Label and other properties required by ICCCCM.
317 */
318 memset(&name, 0, sizeof name);
319 if(label == nil)
320 label = "pjw-face-here";
321 name.value = (uchar*)label;
322 name.encoding = XA_STRING;
323 name.format = 8;
324 name.nitems = strlen(name.value);
326 memset(&normalhint, 0, sizeof normalhint);
327 normalhint.flags = USSize|PMaxSize;
328 normalhint.max_width = WidthOfScreen(xscreen);
329 normalhint.max_height = HeightOfScreen(xscreen);
331 memset(&hint, 0, sizeof hint);
332 hint.flags = InputHint|StateHint;
333 hint.input = 1;
334 hint.initial_state = NormalState;
336 memset(&classhint, 0, sizeof classhint);
337 classhint.res_name = label;
338 classhint.res_class = label;
340 argv[0] = label;
341 argv[1] = nil;
343 XSetWMProperties(
344 _x.display, /* display */
345 _x.drawable, /* window */
346 &name, /* XA_WM_NAME property */
347 &name, /* XA_WM_ICON_NAME property */
348 argv, /* XA_WM_COMMAND */
349 1, /* argc */
350 &normalhint, /* XA_WM_NORMAL_HINTS */
351 &hint, /* XA_WM_HINTS */
352 &classhint /* XA_WM_CLASSHINTS */
353 );
354 XFlush(_x.display);
356 /*
357 * Allocate our local backing store.
358 */
359 _x.screenr = r;
360 _x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
361 _x.nextscreenpm = _x.screenpm;
362 _x.screenimage = _xallocmemimage(r, _x.chan, _x.screenpm);
364 /*
365 * Allocate some useful graphics contexts for the future.
366 */
367 _x.gcfill = xgc(_x.screenpm, FillSolid, -1);
368 _x.gccopy = xgc(_x.screenpm, -1, -1);
369 _x.gcsimplesrc = xgc(_x.screenpm, FillStippled, -1);
370 _x.gczero = xgc(_x.screenpm, -1, -1);
371 _x.gcreplsrc = xgc(_x.screenpm, FillTiled, -1);
373 pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
374 _x.gcfill0 = xgc(pmid, FillSolid, 0);
375 _x.gccopy0 = xgc(pmid, -1, -1);
376 _x.gcsimplesrc0 = xgc(pmid, FillStippled, -1);
377 _x.gczero0 = xgc(pmid, -1, -1);
378 _x.gcreplsrc0 = xgc(pmid, FillTiled, -1);
379 XFreePixmap(_x.display, pmid);
381 /*
382 * Put the window on the screen.
383 */
384 XMapWindow(_x.display, _x.drawable);
385 XFlush(_x.display);
387 /*
388 * Look up clipboard atom.
389 */
390 _x.clipboard = XInternAtom(_x.display, "CLIPBOARD", False);
391 _x.utf8string = XInternAtom(_x.display, "UTF8_STRING", False);
392 _x.targets = XInternAtom(_x.display, "TARGETS", False);
393 _x.text = XInternAtom(_x.display, "TEXT", False);
394 _x.compoundtext = XInternAtom(_x.display, "COMPOUND_TEXT", False);
396 /*
397 * Lots of display connections for various procs.
398 */
399 _x.kbdcon = XOpenDisplay(NULL);
400 _x.mousecon = XOpenDisplay(NULL);
401 _x.snarfcon = XOpenDisplay(NULL);
403 if(0) fprint(2, "x: display=%p kbd=%p mouse=%p snarf=%p\n",
404 _x.display, _x.kbdcon, _x.mousecon, _x.snarfcon);
406 _x.black = xscreen->black_pixel;
407 _x.white = xscreen->white_pixel;
409 return _x.screenimage;
411 err0:
412 fprint(2, "%r\n");
413 /*
414 * Should do a better job of cleaning up here.
415 */
416 XCloseDisplay(_x.display);
417 return nil;
420 int
421 drawsetlabel(Display *d, char *label)
423 char *argv[2];
424 XClassHint classhint;
425 XTextProperty name;
427 /*
428 * Label and other properties required by ICCCCM.
429 */
430 memset(&name, 0, sizeof name);
431 if(label == nil)
432 label = "pjw-face-here";
433 name.value = (uchar*)label;
434 name.encoding = XA_STRING;
435 name.format = 8;
436 name.nitems = strlen(name.value);
438 memset(&classhint, 0, sizeof classhint);
439 classhint.res_name = label;
440 classhint.res_class = label;
442 argv[0] = label;
443 argv[1] = nil;
445 XSetWMProperties(
446 _x.display, /* display */
447 _x.drawable, /* window */
448 &name, /* XA_WM_NAME property */
449 &name, /* XA_WM_ICON_NAME property */
450 argv, /* XA_WM_COMMAND */
451 1, /* argc */
452 nil, /* XA_WM_NORMAL_HINTS */
453 nil, /* XA_WM_HINTS */
454 &classhint /* XA_WM_CLASSHINTS */
455 );
456 XFlush(_x.display);
457 return 0;
460 /*
461 * Create a GC with a particular fill style and XXX.
462 * Disable generation of GraphicsExpose/NoExpose events in the GC.
463 */
464 static XGC
465 xgc(XDrawable d, int fillstyle, int foreground)
467 XGC gc;
468 XGCValues v;
470 memset(&v, 0, sizeof v);
471 v.function = GXcopy;
472 v.graphics_exposures = False;
473 gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
474 if(fillstyle != -1)
475 XSetFillStyle(_x.display, gc, fillstyle);
476 if(foreground != -1)
477 XSetForeground(_x.display, gc, 0);
478 return gc;
482 /*
483 * Initialize map with the Plan 9 rgbv color map.
484 */
485 static void
486 plan9cmap(void)
488 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
489 static int once;
491 if(once)
492 return;
493 once = 1;
495 for(r=0; r!=4; r++)
496 for(g = 0; g != 4; g++)
497 for(b = 0; b!=4; b++)
498 for(v = 0; v!=4; v++){
499 den=r;
500 if(g > den)
501 den=g;
502 if(b > den)
503 den=b;
504 /* divide check -- pick grey shades */
505 if(den==0)
506 cr=cg=cb=v*17;
507 else {
508 num=17*(4*den+v);
509 cr=r*num/den;
510 cg=g*num/den;
511 cb=b*num/den;
513 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
514 _x.map[idx].red = cr*0x0101;
515 _x.map[idx].green = cg*0x0101;
516 _x.map[idx].blue = cb*0x0101;
517 _x.map[idx].pixel = idx;
518 _x.map[idx].flags = DoRed|DoGreen|DoBlue;
520 v7 = v >> 1;
521 idx7 = r*32 + v7*16 + g*4 + b;
522 if((v & 1) == v7){
523 _x.map7to8[idx7][0] = idx;
524 if(den == 0) { /* divide check -- pick grey shades */
525 cr = ((255.0/7.0)*v7)+0.5;
526 cg = cr;
527 cb = cr;
529 else {
530 num=17*15*(4*den+v7*2)/14;
531 cr=r*num/den;
532 cg=g*num/den;
533 cb=b*num/den;
535 _x.map7[idx7].red = cr*0x0101;
536 _x.map7[idx7].green = cg*0x0101;
537 _x.map7[idx7].blue = cb*0x0101;
538 _x.map7[idx7].pixel = idx7;
539 _x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
541 else
542 _x.map7to8[idx7][1] = idx;
546 /*
547 * Initialize and install the rgbv color map as a private color map
548 * for this application. It gets the best colors when it has the
549 * cursor focus.
550 */
551 static int
552 setupcmap(XWindow w)
554 char buf[30];
555 int i;
556 u32int p, pp;
557 XColor c;
559 if(_x.depth <= 1)
560 return 0;
562 if(_x.depth >= 24) {
563 /*
564 * The pixel value returned from XGetPixel needs to
565 * be converted to RGB so we can call rgb2cmap()
566 * to translate between 24 bit X and our color. Unfortunately,
567 * the return value appears to be display server endian
568 * dependant. Therefore, we run some heuristics to later
569 * determine how to mask the int value correctly.
570 * Yeah, I know we can look at _x.vis->byte_order but
571 * some displays say MSB even though they run on LSB.
572 * Besides, this is more anal.
573 */
575 c = _x.map[19]; /* known to have different R, G, B values */
576 if(!XAllocColor(_x.display, _x.cmap, &c)){
577 werrstr("XAllocColor: %r");
578 return -1;
580 p = c.pixel;
581 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
582 if(pp != _x.map[19].pixel) {
583 /* check if endian is other way */
584 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
585 if(pp != _x.map[19].pixel){
586 werrstr("cannot detect X server byte order");
587 return -1;
590 switch(_x.chan){
591 case RGB24:
592 _x.chan = BGR24;
593 break;
594 case XRGB32:
595 _x.chan = XBGR32;
596 break;
597 default:
598 werrstr("cannot byteswap channel %s",
599 chantostr(buf, _x.chan));
600 break;
603 }else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
604 /*
605 * Do nothing. We have no way to express a
606 * mixed-endian 16-bit screen, so pretend they don't exist.
607 */
608 }else if(_x.vis->class == PseudoColor){
609 if(_x.usetable == 0){
610 _x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll);
611 XStoreColors(_x.display, _x.cmap, _x.map, 256);
612 for(i = 0; i < 256; i++){
613 _x.tox11[i] = i;
614 _x.toplan9[i] = i;
616 }else{
617 for(i = 0; i < 128; i++){
618 c = _x.map7[i];
619 if(!XAllocColor(_x.display, _x.cmap, &c)){
620 werrstr("can't allocate colors in 7-bit map");
621 return -1;
623 _x.tox11[_x.map7to8[i][0]] = c.pixel;
624 _x.tox11[_x.map7to8[i][1]] = c.pixel;
625 _x.toplan9[c.pixel] = _x.map7to8[i][0];
628 }else{
629 werrstr("unsupported visual class %d", _x.vis->class);
630 return -1;
632 return 0;
635 void
636 flushmemscreen(Rectangle r)
638 if(_x.nextscreenpm != _x.screenpm){
639 qlock(&_x.screenlock);
640 XSync(_x.display, False);
641 XFreePixmap(_x.display, _x.screenpm);
642 _x.screenpm = _x.nextscreenpm;
643 qunlock(&_x.screenlock);
646 if(r.min.x >= r.max.x || r.min.y >= r.max.y)
647 return;
648 XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
649 Dx(r), Dy(r), r.min.x, r.min.y);
650 XFlush(_x.display);
653 void
654 _xexpose(XEvent *e, XDisplay *xd)
656 XExposeEvent *xe;
657 Rectangle r;
659 qlock(&_x.screenlock);
660 if(_x.screenpm != _x.nextscreenpm){
661 qunlock(&_x.screenlock);
662 return;
664 xe = (XExposeEvent*)e;
665 r.min.x = xe->x;
666 r.min.y = xe->y;
667 r.max.x = xe->x+xe->width;
668 r.max.y = xe->y+xe->height;
669 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
670 Dx(r), Dy(r), r.min.x, r.min.y);
671 XSync(xd, False);
672 qunlock(&_x.screenlock);
675 int
676 _xdestroy(XEvent *e, XDisplay *xd)
678 XDestroyWindowEvent *xe;
680 xe = (XDestroyWindowEvent*)e;
681 if(xe->window == _x.drawable){
682 _x.destroyed = 1;
683 return 1;
685 return 0;
688 int
689 _xconfigure(XEvent *e, XDisplay *xd)
691 Rectangle r;
692 XConfigureEvent *xe = (XConfigureEvent*)e;
694 if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
695 return 0;
696 if(xe->width==0 || xe->height==0)
697 fprint(2, "ignoring resize to %dx%d\n", xe->width, xe->height);
698 r = Rect(0, 0, xe->width, xe->height);
699 qlock(&_x.screenlock);
700 if(_x.screenpm != _x.nextscreenpm){
701 XCopyArea(xd, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
702 Dx(r), Dy(r), r.min.x, r.min.y);
703 XSync(xd, False);
705 qunlock(&_x.screenlock);
706 _x.newscreenr = r;
707 return 1;
710 static int
711 xreplacescreenimage(void)
713 Memimage *m;
714 XDrawable pixmap;
715 Rectangle r;
717 r = _x.newscreenr;
718 if(eqrect(_x.screenr, r))
719 return 0;
721 pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
722 m = _xallocmemimage(r, _x.chan, pixmap);
723 if(_x.nextscreenpm != _x.screenpm)
724 XFreePixmap(_x.display, _x.nextscreenpm);
725 _x.nextscreenpm = pixmap;
726 _x.screenr = r;
727 _drawreplacescreenimage(m);
728 return 1;