| @@ -0,0 +1,338 @@ |
| 1 | +#include <lib9.h> |
| 2 | +#include "draw.h" |
| 3 | +#include "inc.h" |
| 4 | +#include "x.h" |
| 5 | + |
| 6 | +static void plan9cmap(void); |
| 7 | +static int setupcmap(XWindow); |
| 8 | + |
| 9 | +Xconn xconn; |
| 10 | + |
| 11 | +static int |
| 12 | +xerror(XDisplay *d, XErrorEvent *e) |
| 13 | +{ |
| 14 | + char buf[200]; |
| 15 | + |
| 16 | + if(e->request_code == 42) /* XSetInputFocus */ |
| 17 | + return 0; |
| 18 | + if(e->request_code == 18) /* XChangeProperty */ |
| 19 | + return 0; |
| 20 | + /* |
| 21 | + * BadDrawable happens in apps that get resized a LOT, |
| 22 | + * e.g. when KDE is configured to resize continuously |
| 23 | + * during a window drag. |
| 24 | + */ |
| 25 | + if(e->error_code == 9) /* BadDrawable */ |
| 26 | + return 0; |
| 27 | + |
| 28 | + fprint(2, "X error: error_code=%d, request_code=%d, minor=%d disp=%p\n", |
| 29 | + e->error_code, e->request_code, e->minor_code, d); |
| 30 | + XGetErrorText(d, e->error_code, buf, sizeof buf); |
| 31 | + fprint(2, "%s\n", buf); |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +static int |
| 36 | +xioerror(XDisplay *d) |
| 37 | +{ |
| 38 | + /*print("X I/O error\n"); */ |
| 39 | + exit(0); |
| 40 | + /*sysfatal("X I/O error\n");*/ |
| 41 | + abort(); |
| 42 | + return -1; |
| 43 | +} |
| 44 | + |
| 45 | +/* |
| 46 | + * Connect to the X server. |
| 47 | + */ |
| 48 | +int |
| 49 | +winsrvinit(void) |
| 50 | +{ |
| 51 | + char *disp; |
| 52 | + int i, n, xrootid; |
| 53 | + XPixmapFormatValues *pfmt; |
| 54 | + XScreen *xscreen; |
| 55 | + XWindow xrootwin; |
| 56 | + XVisualInfo xvi; |
| 57 | + |
| 58 | + /* |
| 59 | + * Connect to X server. |
| 60 | + */ |
| 61 | + xconn.display = XOpenDisplay(NULL); |
| 62 | + if(xconn.display == nil){ |
| 63 | + disp = getenv("DISPLAY"); |
| 64 | + werrstr("XOpenDisplay %s: %r", disp ? disp : ":0"); |
| 65 | + free(disp); |
| 66 | + return -1; |
| 67 | + } |
| 68 | + xconn.fd = ConnectionNumber(xconn.display); |
| 69 | + XSetErrorHandler(xerror); |
| 70 | + XSetIOErrorHandler(xioerror); |
| 71 | + xrootid = DefaultScreen(xconn.display); |
| 72 | + xrootwin = DefaultRootWindow(xconn.display); |
| 73 | + |
| 74 | + /* |
| 75 | + * Figure out underlying screen format. |
| 76 | + */ |
| 77 | + if(XMatchVisualInfo(xconn.display, xrootid, 16, TrueColor, &xvi) |
| 78 | + || XMatchVisualInfo(xconn.display, xrootid, 16, DirectColor, &xvi)){ |
| 79 | + xconn.vis = xvi.visual; |
| 80 | + xconn.depth = 16; |
| 81 | + } |
| 82 | + else |
| 83 | + if(XMatchVisualInfo(xconn.display, xrootid, 15, TrueColor, &xvi) |
| 84 | + || XMatchVisualInfo(xconn.display, xrootid, 15, DirectColor, &xvi)){ |
| 85 | + xconn.vis = xvi.visual; |
| 86 | + xconn.depth = 15; |
| 87 | + } |
| 88 | + else |
| 89 | + if(XMatchVisualInfo(xconn.display, xrootid, 24, TrueColor, &xvi) |
| 90 | + || XMatchVisualInfo(xconn.display, xrootid, 24, DirectColor, &xvi)){ |
| 91 | + xconn.vis = xvi.visual; |
| 92 | + xconn.depth = 24; |
| 93 | + } |
| 94 | + else |
| 95 | + if(XMatchVisualInfo(xconn.display, xrootid, 8, PseudoColor, &xvi) |
| 96 | + || XMatchVisualInfo(xconn.display, xrootid, 8, StaticColor, &xvi)){ |
| 97 | + if(xconn.depth > 8){ |
| 98 | + werrstr("can't deal with colormapped depth %d screens", |
| 99 | + xconn.depth); |
| 100 | + goto err0; |
| 101 | + } |
| 102 | + xconn.vis = xvi.visual; |
| 103 | + xconn.depth = 8; |
| 104 | + } |
| 105 | + else{ |
| 106 | + xconn.depth = DefaultDepth(xconn.display, xrootid); |
| 107 | + if(xconn.depth != 8){ |
| 108 | + werrstr("can't understand depth %d screen", xconn.depth); |
| 109 | + goto err0; |
| 110 | + } |
| 111 | + xconn.vis = DefaultVisual(xconn.display, xrootid); |
| 112 | + } |
| 113 | + |
| 114 | + if(DefaultDepth(xconn.display, xrootid) == xconn.depth) |
| 115 | + xconn.usetable = 1; |
| 116 | + |
| 117 | + /* |
| 118 | + * xconn.depth is only the number of significant pixel bits, |
| 119 | + * not the total number of pixel bits. We need to walk the |
| 120 | + * display list to find how many actual bits are used |
| 121 | + * per pixel. |
| 122 | + */ |
| 123 | + xconn.chan = 0; |
| 124 | + pfmt = XListPixmapFormats(xconn.display, &n); |
| 125 | + for(i=0; i<n; i++){ |
| 126 | + if(pfmt[i].depth == xconn.depth){ |
| 127 | + switch(pfmt[i].bits_per_pixel){ |
| 128 | + case 1: /* untested */ |
| 129 | + xconn.chan = GREY1; |
| 130 | + break; |
| 131 | + case 2: /* untested */ |
| 132 | + xconn.chan = GREY2; |
| 133 | + break; |
| 134 | + case 4: /* untested */ |
| 135 | + xconn.chan = GREY4; |
| 136 | + break; |
| 137 | + case 8: |
| 138 | + xconn.chan = CMAP8; |
| 139 | + break; |
| 140 | + case 15: |
| 141 | + xconn.chan = RGB15; |
| 142 | + break; |
| 143 | + case 16: /* how to tell RGB15? */ |
| 144 | + xconn.chan = RGB16; |
| 145 | + break; |
| 146 | + case 24: /* untested (impossible?) */ |
| 147 | + xconn.chan = RGB24; |
| 148 | + break; |
| 149 | + case 32: |
| 150 | + xconn.chan = XRGB32; |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + if(xconn.chan == 0){ |
| 156 | + werrstr("could not determine screen pixel format"); |
| 157 | + goto err0; |
| 158 | + } |
| 159 | + |
| 160 | + /* |
| 161 | + * Set up color map if necessary. |
| 162 | + */ |
| 163 | + xscreen = DefaultScreenOfDisplay(xconn.display); |
| 164 | + xconn.cmap = DefaultColormapOfScreen(xscreen); |
| 165 | + if(xconn.vis->class != StaticColor){ |
| 166 | + plan9cmap(); |
| 167 | + setupcmap(xrootwin); |
| 168 | + } |
| 169 | + return 0; |
| 170 | + |
| 171 | +err0: |
| 172 | + /* |
| 173 | + * Should do a better job of cleaning up here. |
| 174 | + */ |
| 175 | + XCloseDisplay(xconn.display); |
| 176 | + return -1; |
| 177 | +} |
| 178 | + |
| 179 | +/* |
| 180 | + * Initialize map with the Plan 9 rgbv color map. |
| 181 | + */ |
| 182 | +static void |
| 183 | +plan9cmap(void) |
| 184 | +{ |
| 185 | + int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7; |
| 186 | + static int once; |
| 187 | + |
| 188 | + if(once) |
| 189 | + return; |
| 190 | + once = 1; |
| 191 | + |
| 192 | + for(r=0; r!=4; r++) |
| 193 | + for(g = 0; g != 4; g++) |
| 194 | + for(b = 0; b!=4; b++) |
| 195 | + for(v = 0; v!=4; v++){ |
| 196 | + den=r; |
| 197 | + if(g > den) |
| 198 | + den=g; |
| 199 | + if(b > den) |
| 200 | + den=b; |
| 201 | + /* divide check -- pick grey shades */ |
| 202 | + if(den==0) |
| 203 | + cr=cg=cb=v*17; |
| 204 | + else { |
| 205 | + num=17*(4*den+v); |
| 206 | + cr=r*num/den; |
| 207 | + cg=g*num/den; |
| 208 | + cb=b*num/den; |
| 209 | + } |
| 210 | + idx = r*64 + v*16 + ((g*4 + b + v - r) & 15); |
| 211 | + xconn.map[idx].red = cr*0x0101; |
| 212 | + xconn.map[idx].green = cg*0x0101; |
| 213 | + xconn.map[idx].blue = cb*0x0101; |
| 214 | + xconn.map[idx].pixel = idx; |
| 215 | + xconn.map[idx].flags = DoRed|DoGreen|DoBlue; |
| 216 | + |
| 217 | + v7 = v >> 1; |
| 218 | + idx7 = r*32 + v7*16 + g*4 + b; |
| 219 | + if((v & 1) == v7){ |
| 220 | + xconn.map7to8[idx7][0] = idx; |
| 221 | + if(den == 0) { /* divide check -- pick grey shades */ |
| 222 | + cr = ((255.0/7.0)*v7)+0.5; |
| 223 | + cg = cr; |
| 224 | + cb = cr; |
| 225 | + } |
| 226 | + else { |
| 227 | + num=17*15*(4*den+v7*2)/14; |
| 228 | + cr=r*num/den; |
| 229 | + cg=g*num/den; |
| 230 | + cb=b*num/den; |
| 231 | + } |
| 232 | + xconn.map7[idx7].red = cr*0x0101; |
| 233 | + xconn.map7[idx7].green = cg*0x0101; |
| 234 | + xconn.map7[idx7].blue = cb*0x0101; |
| 235 | + xconn.map7[idx7].pixel = idx7; |
| 236 | + xconn.map7[idx7].flags = DoRed|DoGreen|DoBlue; |
| 237 | + } |
| 238 | + else |
| 239 | + xconn.map7to8[idx7][1] = idx; |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +/* |
| 244 | + * Initialize and install the rgbv color map as a private color map |
| 245 | + * for this application. It gets the best colors when it has the |
| 246 | + * cursor focus. |
| 247 | + * |
| 248 | + * We always choose the best depth possible, but that might not |
| 249 | + * be the default depth. On such "suboptimal" systems, we have to allocate an |
| 250 | + * empty color map anyway, according to Axel Belinfante. |
| 251 | + */ |
| 252 | +static int |
| 253 | +setupcmap(XWindow w) |
| 254 | +{ |
| 255 | + char buf[30]; |
| 256 | + int i; |
| 257 | + u32int p, pp; |
| 258 | + XColor c; |
| 259 | + |
| 260 | + if(xconn.depth <= 1) |
| 261 | + return 0; |
| 262 | + |
| 263 | + if(xconn.depth >= 24) { |
| 264 | + if(xconn.usetable == 0) |
| 265 | + xconn.cmap = XCreateColormap(xconn.display, w, xconn.vis, AllocNone); |
| 266 | + |
| 267 | + /* |
| 268 | + * The pixel value returned from XGetPixel needs to |
| 269 | + * be converted to RGB so we can call rgb2cmap() |
| 270 | + * to translate between 24 bit X and our color. Unfortunately, |
| 271 | + * the return value appears to be display server endian |
| 272 | + * dependant. Therefore, we run some heuristics to later |
| 273 | + * determine how to mask the int value correctly. |
| 274 | + * Yeah, I know we can look at xconn.vis->byte_order but |
| 275 | + * some displays say MSB even though they run on LSB. |
| 276 | + * Besides, this is more anal. |
| 277 | + */ |
| 278 | + c = xconn.map[19]; /* known to have different R, G, B values */ |
| 279 | + if(!XAllocColor(xconn.display, xconn.cmap, &c)){ |
| 280 | + werrstr("XAllocColor: %r"); |
| 281 | + return -1; |
| 282 | + } |
| 283 | + p = c.pixel; |
| 284 | + pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff); |
| 285 | + if(pp != xconn.map[19].pixel) { |
| 286 | + /* check if endian is other way */ |
| 287 | + pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff); |
| 288 | + if(pp != xconn.map[19].pixel){ |
| 289 | + werrstr("cannot detect X server byte order"); |
| 290 | + return -1; |
| 291 | + } |
| 292 | + |
| 293 | + switch(xconn.chan){ |
| 294 | + case RGB24: |
| 295 | + xconn.chan = BGR24; |
| 296 | + break; |
| 297 | + case XRGB32: |
| 298 | + xconn.chan = XBGR32; |
| 299 | + break; |
| 300 | + default: |
| 301 | + werrstr("cannot byteswap channel %s", |
| 302 | + chantostr(buf, xconn.chan)); |
| 303 | + break; |
| 304 | + } |
| 305 | + } |
| 306 | + }else if(xconn.vis->class == TrueColor || xconn.vis->class == DirectColor){ |
| 307 | + /* |
| 308 | + * Do nothing. We have no way to express a |
| 309 | + * mixed-endian 16-bit screen, so pretend they don't exist. |
| 310 | + */ |
| 311 | + if(xconn.usetable == 0) |
| 312 | + xconn.cmap = XCreateColormap(xconn.display, w, xconn.vis, AllocNone); |
| 313 | + }else if(xconn.vis->class == PseudoColor){ |
| 314 | + if(xconn.usetable == 0){ |
| 315 | + xconn.cmap = XCreateColormap(xconn.display, w, xconn.vis, AllocAll); |
| 316 | + XStoreColors(xconn.display, xconn.cmap, xconn.map, 256); |
| 317 | + for(i = 0; i < 256; i++){ |
| 318 | + xconn.tox11[i] = i; |
| 319 | + xconn.toplan9[i] = i; |
| 320 | + } |
| 321 | + }else{ |
| 322 | + for(i = 0; i < 128; i++){ |
| 323 | + c = xconn.map7[i]; |
| 324 | + if(!XAllocColor(xconn.display, xconn.cmap, &c)){ |
| 325 | + werrstr("can't allocate colors in 7-bit map"); |
| 326 | + return -1; |
| 327 | + } |
| 328 | + xconn.tox11[xconn.map7to8[i][0]] = c.pixel; |
| 329 | + xconn.tox11[xconn.map7to8[i][1]] = c.pixel; |
| 330 | + xconn.toplan9[c.pixel] = xconn.map7to8[i][0]; |
| 331 | + } |
| 332 | + } |
| 333 | + }else{ |
| 334 | + werrstr("unsupported visual class %d", xconn.vis->class); |
| 335 | + return -1; |
| 336 | + } |
| 337 | + return 0; |
| 338 | +} |