[35] | 1 | /* |
---|
[672] | 2 | * libcaca Colour ASCII-Art library |
---|
[527] | 3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
[268] | 4 | * All Rights Reserved |
---|
[35] | 5 | * |
---|
[769] | 6 | * $Id: driver_x11.c 1462 2006-12-12 01:53:54Z sam $ |
---|
| 7 | * |
---|
[1462] | 8 | * This library is free software. It comes without any warranty, to |
---|
[1452] | 9 | * the extent permitted by applicable law. You can redistribute it |
---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
[522] | 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
[35] | 13 | */ |
---|
[17] | 14 | |
---|
[769] | 15 | /* |
---|
[540] | 16 | * This file contains the libcaca X11 input and output driver |
---|
[205] | 17 | */ |
---|
| 18 | |
---|
[63] | 19 | #include "config.h" |
---|
[859] | 20 | #include "common.h" |
---|
[63] | 21 | |
---|
[539] | 22 | #if defined(USE_X11) |
---|
| 23 | |
---|
| 24 | #include <X11/Xlib.h> |
---|
[548] | 25 | #include <X11/Xutil.h> |
---|
| 26 | #include <X11/keysym.h> |
---|
[539] | 27 | #if defined(HAVE_X11_XKBLIB_H) |
---|
| 28 | # include <X11/XKBlib.h> |
---|
| 29 | #endif |
---|
| 30 | |
---|
[226] | 31 | #include <stdio.h> /* BUFSIZ */ |
---|
[17] | 32 | #include <stdlib.h> |
---|
| 33 | |
---|
[185] | 34 | #include "caca.h" |
---|
| 35 | #include "caca_internals.h" |
---|
[524] | 36 | #include "cucul.h" |
---|
| 37 | #include "cucul_internals.h" |
---|
[17] | 38 | |
---|
[281] | 39 | /* |
---|
| 40 | * Local functions |
---|
| 41 | */ |
---|
[300] | 42 | static int x11_error_handler(Display *, XErrorEvent *); |
---|
| 43 | |
---|
[550] | 44 | struct driver_private |
---|
| 45 | { |
---|
| 46 | Display *dpy; |
---|
| 47 | Window window; |
---|
| 48 | Pixmap pixmap; |
---|
| 49 | GC gc; |
---|
| 50 | long int event_mask; |
---|
| 51 | int font_width, font_height; |
---|
[780] | 52 | int colors[4096]; |
---|
[550] | 53 | Font font; |
---|
| 54 | XFontStruct *font_struct; |
---|
| 55 | int font_offset; |
---|
[687] | 56 | Cursor pointer; |
---|
[799] | 57 | Atom wm_protocols; |
---|
| 58 | Atom wm_delete_window; |
---|
[550] | 59 | #if defined(HAVE_X11_XKBLIB_H) |
---|
| 60 | Bool autorepeat; |
---|
| 61 | #endif |
---|
| 62 | }; |
---|
| 63 | |
---|
[811] | 64 | static int x11_init_graphics(caca_display_t *dp) |
---|
[227] | 65 | { |
---|
[539] | 66 | Colormap colormap; |
---|
| 67 | XSetWindowAttributes x11_winattr; |
---|
| 68 | int (*old_error_handler)(Display *, XErrorEvent *); |
---|
| 69 | char const *fonts[] = { NULL, "8x13bold", "fixed" }, **parser; |
---|
| 70 | char const *geometry; |
---|
[1233] | 71 | unsigned int width = dp->cv->width, height = dp->cv->height; |
---|
[539] | 72 | int i; |
---|
[231] | 73 | |
---|
[811] | 74 | dp->drv.p = malloc(sizeof(struct driver_private)); |
---|
[550] | 75 | |
---|
[684] | 76 | #if defined(HAVE_GETENV) |
---|
[539] | 77 | geometry = getenv("CACA_GEOMETRY"); |
---|
[613] | 78 | if(geometry && *geometry) |
---|
[539] | 79 | sscanf(geometry, "%ux%u", &width, &height); |
---|
[684] | 80 | #endif |
---|
[227] | 81 | |
---|
[1233] | 82 | _cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32); |
---|
[227] | 83 | |
---|
[811] | 84 | dp->drv.p->dpy = XOpenDisplay(NULL); |
---|
| 85 | if(dp->drv.p->dpy == NULL) |
---|
[539] | 86 | return -1; |
---|
[227] | 87 | |
---|
[684] | 88 | #if defined(HAVE_GETENV) |
---|
[539] | 89 | fonts[0] = getenv("CACA_FONT"); |
---|
| 90 | if(fonts[0] && *fonts[0]) |
---|
| 91 | parser = fonts; |
---|
| 92 | else |
---|
[684] | 93 | #endif |
---|
[539] | 94 | parser = fonts + 1; |
---|
[227] | 95 | |
---|
[539] | 96 | /* Ignore font errors */ |
---|
| 97 | old_error_handler = XSetErrorHandler(x11_error_handler); |
---|
[227] | 98 | |
---|
[539] | 99 | /* Parse our font list */ |
---|
| 100 | for( ; ; parser++) |
---|
[265] | 101 | { |
---|
[539] | 102 | if(!*parser) |
---|
[265] | 103 | { |
---|
[539] | 104 | XSetErrorHandler(old_error_handler); |
---|
[811] | 105 | XCloseDisplay(dp->drv.p->dpy); |
---|
[265] | 106 | return -1; |
---|
| 107 | } |
---|
[263] | 108 | |
---|
[811] | 109 | dp->drv.p->font = XLoadFont(dp->drv.p->dpy, *parser); |
---|
| 110 | if(!dp->drv.p->font) |
---|
[539] | 111 | continue; |
---|
[300] | 112 | |
---|
[811] | 113 | dp->drv.p->font_struct = XQueryFont(dp->drv.p->dpy, dp->drv.p->font); |
---|
| 114 | if(!dp->drv.p->font_struct) |
---|
[265] | 115 | { |
---|
[811] | 116 | XUnloadFont(dp->drv.p->dpy, dp->drv.p->font); |
---|
[539] | 117 | continue; |
---|
[265] | 118 | } |
---|
[251] | 119 | |
---|
[539] | 120 | break; |
---|
| 121 | } |
---|
[272] | 122 | |
---|
[539] | 123 | /* Reset the default X11 error handler */ |
---|
| 124 | XSetErrorHandler(old_error_handler); |
---|
[272] | 125 | |
---|
[811] | 126 | dp->drv.p->font_width = dp->drv.p->font_struct->max_bounds.width; |
---|
| 127 | dp->drv.p->font_height = dp->drv.p->font_struct->max_bounds.ascent |
---|
| 128 | + dp->drv.p->font_struct->max_bounds.descent; |
---|
| 129 | dp->drv.p->font_offset = dp->drv.p->font_struct->max_bounds.descent; |
---|
[272] | 130 | |
---|
[811] | 131 | colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy)); |
---|
[780] | 132 | for(i = 0x000; i < 0x1000; i++) |
---|
[539] | 133 | { |
---|
| 134 | XColor color; |
---|
[780] | 135 | color.red = ((i & 0xf00) >> 8) * 0x1111; |
---|
| 136 | color.green = ((i & 0x0f0) >> 4) * 0x1111; |
---|
| 137 | color.blue = (i & 0x00f) * 0x1111; |
---|
[811] | 138 | XAllocColor(dp->drv.p->dpy, colormap, &color); |
---|
| 139 | dp->drv.p->colors[i] = color.pixel; |
---|
[539] | 140 | } |
---|
[251] | 141 | |
---|
[539] | 142 | x11_winattr.backing_store = Always; |
---|
[811] | 143 | x11_winattr.background_pixel = dp->drv.p->colors[0x000]; |
---|
[539] | 144 | x11_winattr.event_mask = ExposureMask | StructureNotifyMask; |
---|
[251] | 145 | |
---|
[811] | 146 | dp->drv.p->window = |
---|
| 147 | XCreateWindow(dp->drv.p->dpy, DefaultRootWindow(dp->drv.p->dpy), 0, 0, |
---|
| 148 | dp->cv->width * dp->drv.p->font_width, |
---|
| 149 | dp->cv->height * dp->drv.p->font_height, |
---|
[539] | 150 | 0, 0, InputOutput, 0, |
---|
| 151 | CWBackingStore | CWBackPixel | CWEventMask, |
---|
| 152 | &x11_winattr); |
---|
[251] | 153 | |
---|
[811] | 154 | dp->drv.p->wm_protocols = |
---|
| 155 | XInternAtom(dp->drv.p->dpy, "WM_PROTOCOLS", True); |
---|
| 156 | dp->drv.p->wm_delete_window = |
---|
| 157 | XInternAtom(dp->drv.p->dpy, "WM_DELETE_WINDOW", True); |
---|
[799] | 158 | |
---|
[811] | 159 | if(dp->drv.p->wm_protocols != None && dp->drv.p->wm_delete_window != None) |
---|
| 160 | XSetWMProtocols(dp->drv.p->dpy, dp->drv.p->window, |
---|
| 161 | &dp->drv.p->wm_delete_window, 1); |
---|
[799] | 162 | |
---|
[811] | 163 | XStoreName(dp->drv.p->dpy, dp->drv.p->window, "caca for X"); |
---|
[324] | 164 | |
---|
[811] | 165 | XSelectInput(dp->drv.p->dpy, dp->drv.p->window, StructureNotifyMask); |
---|
| 166 | XMapWindow(dp->drv.p->dpy, dp->drv.p->window); |
---|
[251] | 167 | |
---|
[811] | 168 | dp->drv.p->gc = XCreateGC(dp->drv.p->dpy, dp->drv.p->window, 0, NULL); |
---|
| 169 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, dp->drv.p->colors[0x888]); |
---|
| 170 | XSetFont(dp->drv.p->dpy, dp->drv.p->gc, dp->drv.p->font); |
---|
[251] | 171 | |
---|
[539] | 172 | for(;;) |
---|
| 173 | { |
---|
[548] | 174 | XEvent xevent; |
---|
[811] | 175 | XNextEvent(dp->drv.p->dpy, &xevent); |
---|
[548] | 176 | if (xevent.type == MapNotify) |
---|
[539] | 177 | break; |
---|
[265] | 178 | } |
---|
[336] | 179 | |
---|
[539] | 180 | #if defined(HAVE_X11_XKBLIB_H) |
---|
| 181 | /* Disable autorepeat */ |
---|
[811] | 182 | XkbSetDetectableAutoRepeat(dp->drv.p->dpy, True, &dp->drv.p->autorepeat); |
---|
| 183 | if(!dp->drv.p->autorepeat) |
---|
| 184 | XAutoRepeatOff(dp->drv.p->dpy); |
---|
[336] | 185 | #endif |
---|
[491] | 186 | |
---|
[811] | 187 | dp->drv.p->event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask |
---|
[539] | 188 | | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask |
---|
| 189 | | ExposureMask; |
---|
[483] | 190 | |
---|
[811] | 191 | XSelectInput(dp->drv.p->dpy, dp->drv.p->window, dp->drv.p->event_mask); |
---|
[524] | 192 | |
---|
[811] | 193 | XSync(dp->drv.p->dpy, False); |
---|
[483] | 194 | |
---|
[811] | 195 | dp->drv.p->pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window, |
---|
| 196 | dp->cv->width * dp->drv.p->font_width, |
---|
| 197 | dp->cv->height * dp->drv.p->font_height, |
---|
| 198 | DefaultDepth(dp->drv.p->dpy, |
---|
| 199 | DefaultScreen(dp->drv.p->dpy))); |
---|
| 200 | dp->drv.p->pointer = None; |
---|
[687] | 201 | |
---|
[227] | 202 | return 0; |
---|
| 203 | } |
---|
| 204 | |
---|
[811] | 205 | static int x11_end_graphics(caca_display_t *dp) |
---|
[251] | 206 | { |
---|
[811] | 207 | XSync(dp->drv.p->dpy, False); |
---|
[324] | 208 | #if defined(HAVE_X11_XKBLIB_H) |
---|
[811] | 209 | if(!dp->drv.p->autorepeat) |
---|
| 210 | XAutoRepeatOn(dp->drv.p->dpy); |
---|
[324] | 211 | #endif |
---|
[811] | 212 | XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap); |
---|
| 213 | XFreeFont(dp->drv.p->dpy, dp->drv.p->font_struct); |
---|
| 214 | XFreeGC(dp->drv.p->dpy, dp->drv.p->gc); |
---|
| 215 | XUnmapWindow(dp->drv.p->dpy, dp->drv.p->window); |
---|
| 216 | XDestroyWindow(dp->drv.p->dpy, dp->drv.p->window); |
---|
| 217 | XCloseDisplay(dp->drv.p->dpy); |
---|
[300] | 218 | |
---|
[811] | 219 | free(dp->drv.p); |
---|
[550] | 220 | |
---|
[251] | 221 | return 0; |
---|
| 222 | } |
---|
| 223 | |
---|
[819] | 224 | static int x11_set_display_title(caca_display_t *dp, char const *title) |
---|
[343] | 225 | { |
---|
[811] | 226 | XStoreName(dp->drv.p->dpy, dp->drv.p->window, title); |
---|
[343] | 227 | return 0; |
---|
| 228 | } |
---|
| 229 | |
---|
[819] | 230 | static unsigned int x11_get_display_width(caca_display_t *dp) |
---|
[352] | 231 | { |
---|
[811] | 232 | return dp->cv->width * dp->drv.p->font_width; |
---|
[352] | 233 | } |
---|
| 234 | |
---|
[819] | 235 | static unsigned int x11_get_display_height(caca_display_t *dp) |
---|
[352] | 236 | { |
---|
[811] | 237 | return dp->cv->height * dp->drv.p->font_height; |
---|
[352] | 238 | } |
---|
| 239 | |
---|
[811] | 240 | static void x11_display(caca_display_t *dp) |
---|
[227] | 241 | { |
---|
[539] | 242 | unsigned int x, y, len; |
---|
[227] | 243 | |
---|
[539] | 244 | /* First draw the background colours. Splitting the process in two |
---|
| 245 | * loops like this is actually slightly faster. */ |
---|
[811] | 246 | for(y = 0; y < dp->cv->height; y++) |
---|
[265] | 247 | { |
---|
[811] | 248 | for(x = 0; x < dp->cv->width; x += len) |
---|
[527] | 249 | { |
---|
[1254] | 250 | uint32_t *attrs = dp->cv->attrs + x + y * dp->cv->width; |
---|
| 251 | uint16_t bg = _cucul_attr_to_rgb12bg(*attrs); |
---|
[261] | 252 | |
---|
[539] | 253 | len = 1; |
---|
[811] | 254 | while(x + len < dp->cv->width |
---|
[1254] | 255 | && _cucul_attr_to_rgb12bg(attrs[len]) == bg) |
---|
[539] | 256 | len++; |
---|
[300] | 257 | |
---|
[811] | 258 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, |
---|
| 259 | dp->drv.p->colors[bg]); |
---|
| 260 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc, |
---|
| 261 | x * dp->drv.p->font_width, y * dp->drv.p->font_height, |
---|
| 262 | len * dp->drv.p->font_width, dp->drv.p->font_height); |
---|
[303] | 263 | } |
---|
[539] | 264 | } |
---|
[300] | 265 | |
---|
[539] | 266 | /* Then print the foreground characters */ |
---|
[811] | 267 | for(y = 0; y < dp->cv->height; y++) |
---|
[336] | 268 | { |
---|
[811] | 269 | unsigned int yoff = (y + 1) * dp->drv.p->font_height |
---|
| 270 | - dp->drv.p->font_offset; |
---|
| 271 | uint32_t *chars = dp->cv->chars + y * dp->cv->width; |
---|
[557] | 272 | |
---|
[811] | 273 | for(x = 0; x < dp->cv->width; x++, chars++) |
---|
[336] | 274 | { |
---|
[1254] | 275 | uint32_t *attrs = dp->cv->attrs + x + y * dp->cv->width; |
---|
[336] | 276 | |
---|
[1264] | 277 | /* Underline */ |
---|
| 278 | if(*attrs & CUCUL_UNDERLINE) |
---|
| 279 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 280 | dp->drv.p->gc, |
---|
| 281 | x * dp->drv.p->font_width, |
---|
| 282 | (y + 1) * dp->drv.p->font_height - 1, |
---|
| 283 | dp->drv.p->font_width, 1); |
---|
| 284 | |
---|
[539] | 285 | /* Skip spaces */ |
---|
[1218] | 286 | if(*chars <= 0x00000020) |
---|
[539] | 287 | continue; |
---|
[491] | 288 | |
---|
[1218] | 289 | if(*chars == CUCUL_MAGIC_FULLWIDTH) |
---|
| 290 | continue; |
---|
| 291 | |
---|
[811] | 292 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, |
---|
[1254] | 293 | dp->drv.p->colors[_cucul_attr_to_rgb12fg(*attrs)]); |
---|
[532] | 294 | |
---|
[650] | 295 | /* Plain ASCII, no problem. */ |
---|
| 296 | if(*chars > 0x00000020 && *chars < 0x00000080) |
---|
[488] | 297 | { |
---|
[811] | 298 | char ch = (uint8_t)*chars; |
---|
| 299 | XDrawString(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc, |
---|
| 300 | x * dp->drv.p->font_width, yoff, &ch, 1); |
---|
[650] | 301 | continue; |
---|
[508] | 302 | } |
---|
[491] | 303 | |
---|
[650] | 304 | /* We want to be able to print a few special Unicode characters |
---|
| 305 | * such as the CP437 gradients and half blocks. For unknown |
---|
| 306 | * characters, just print '?'. */ |
---|
| 307 | switch(*chars) |
---|
| 308 | { |
---|
[947] | 309 | case 0x000000b7: /* · */ |
---|
| 310 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 311 | dp->drv.p->gc, |
---|
| 312 | x * dp->drv.p->font_width |
---|
| 313 | + dp->drv.p->font_width / 2, |
---|
| 314 | y * dp->drv.p->font_height |
---|
| 315 | + dp->drv.p->font_height / 2, 2, 2); |
---|
| 316 | break; |
---|
| 317 | case 0x00002500: /* ─ */ |
---|
| 318 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 319 | dp->drv.p->gc, |
---|
| 320 | x * dp->drv.p->font_width, |
---|
| 321 | y * dp->drv.p->font_height |
---|
| 322 | + dp->drv.p->font_height / 2 + 1, |
---|
| 323 | dp->drv.p->font_width, 1); |
---|
| 324 | break; |
---|
[650] | 325 | case 0x00002580: /* ▀ */ |
---|
[811] | 326 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 327 | dp->drv.p->gc, |
---|
| 328 | x * dp->drv.p->font_width, |
---|
| 329 | y * dp->drv.p->font_height, |
---|
| 330 | dp->drv.p->font_width, |
---|
| 331 | dp->drv.p->font_height / 2); |
---|
[650] | 332 | break; |
---|
| 333 | case 0x00002584: /* ▄ */ |
---|
[811] | 334 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 335 | dp->drv.p->gc, |
---|
| 336 | x * dp->drv.p->font_width, |
---|
| 337 | (y + 1) * dp->drv.p->font_height |
---|
| 338 | - dp->drv.p->font_height / 2, |
---|
| 339 | dp->drv.p->font_width, |
---|
| 340 | dp->drv.p->font_height / 2); |
---|
[650] | 341 | break; |
---|
| 342 | case 0x00002588: /* █ */ |
---|
[811] | 343 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 344 | dp->drv.p->gc, |
---|
| 345 | x * dp->drv.p->font_width, |
---|
| 346 | y * dp->drv.p->font_height, |
---|
| 347 | dp->drv.p->font_width, |
---|
| 348 | dp->drv.p->font_height); |
---|
[650] | 349 | break; |
---|
| 350 | case 0x0000258c: /* ▌ */ |
---|
[811] | 351 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 352 | dp->drv.p->gc, |
---|
| 353 | x * dp->drv.p->font_width, |
---|
| 354 | y * dp->drv.p->font_height, |
---|
| 355 | dp->drv.p->font_width / 2, |
---|
| 356 | dp->drv.p->font_height); |
---|
[650] | 357 | break; |
---|
| 358 | case 0x00002590: /* ▐ */ |
---|
[811] | 359 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 360 | dp->drv.p->gc, |
---|
| 361 | (x + 1) * dp->drv.p->font_width |
---|
| 362 | - dp->drv.p->font_width / 2, |
---|
| 363 | y * dp->drv.p->font_height, |
---|
| 364 | dp->drv.p->font_width / 2, |
---|
| 365 | dp->drv.p->font_height); |
---|
[650] | 366 | break; |
---|
[911] | 367 | case 0x000025a0: /* ■ */ |
---|
| 368 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 369 | dp->drv.p->gc, |
---|
| 370 | x * dp->drv.p->font_width, |
---|
| 371 | y * dp->drv.p->font_height |
---|
| 372 | + dp->drv.p->font_height / 4, |
---|
| 373 | dp->drv.p->font_width, |
---|
| 374 | dp->drv.p->font_height / 2); |
---|
| 375 | break; |
---|
[650] | 376 | case 0x00002593: /* ▓ */ |
---|
| 377 | case 0x00002592: /* ▒ */ |
---|
| 378 | case 0x00002591: /* ░ */ |
---|
| 379 | { |
---|
| 380 | /* FIXME: this sucks utterly */ |
---|
| 381 | int i, j, k = *chars - 0x00002591; |
---|
[811] | 382 | for(j = dp->drv.p->font_height; j--; ) |
---|
| 383 | for(i = dp->drv.p->font_width; i--; ) |
---|
[650] | 384 | { |
---|
| 385 | if(((i + 2 * (j & 1)) & 3) > k) |
---|
| 386 | continue; |
---|
| 387 | |
---|
[811] | 388 | XDrawPoint(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 389 | dp->drv.p->gc, |
---|
| 390 | x * dp->drv.p->font_width + i, |
---|
| 391 | y * dp->drv.p->font_height + j); |
---|
[650] | 392 | } |
---|
| 393 | break; |
---|
| 394 | } |
---|
| 395 | default: |
---|
| 396 | { |
---|
[811] | 397 | char ch; |
---|
| 398 | ch = '?'; |
---|
| 399 | XDrawString(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 400 | dp->drv.p->gc, |
---|
| 401 | x * dp->drv.p->font_width, yoff, &ch, 1); |
---|
[650] | 402 | break; |
---|
| 403 | } |
---|
| 404 | } |
---|
[508] | 405 | } |
---|
| 406 | } |
---|
[336] | 407 | |
---|
[811] | 408 | XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->window, |
---|
| 409 | dp->drv.p->gc, 0, 0, |
---|
| 410 | dp->cv->width * dp->drv.p->font_width, |
---|
| 411 | dp->cv->height * dp->drv.p->font_height, |
---|
[539] | 412 | 0, 0); |
---|
[811] | 413 | XFlush(dp->drv.p->dpy); |
---|
[227] | 414 | } |
---|
| 415 | |
---|
[811] | 416 | static void x11_handle_resize(caca_display_t *dp) |
---|
[347] | 417 | { |
---|
[539] | 418 | Pixmap new_pixmap; |
---|
| 419 | |
---|
[811] | 420 | new_pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window, |
---|
| 421 | dp->resize.w * dp->drv.p->font_width, |
---|
| 422 | dp->resize.h * dp->drv.p->font_height, |
---|
| 423 | DefaultDepth(dp->drv.p->dpy, |
---|
| 424 | DefaultScreen(dp->drv.p->dpy))); |
---|
| 425 | XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, new_pixmap, |
---|
| 426 | dp->drv.p->gc, 0, 0, |
---|
| 427 | dp->resize.w * dp->drv.p->font_width, |
---|
| 428 | dp->resize.h * dp->drv.p->font_height, 0, 0); |
---|
| 429 | XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap); |
---|
| 430 | dp->drv.p->pixmap = new_pixmap; |
---|
[347] | 431 | } |
---|
| 432 | |
---|
[811] | 433 | static int x11_get_event(caca_display_t *dp, caca_event_t *ev) |
---|
[548] | 434 | { |
---|
| 435 | XEvent xevent; |
---|
| 436 | char key; |
---|
| 437 | |
---|
[811] | 438 | while(XCheckWindowEvent(dp->drv.p->dpy, dp->drv.p->window, |
---|
| 439 | dp->drv.p->event_mask, &xevent) == True) |
---|
[548] | 440 | { |
---|
| 441 | KeySym keysym; |
---|
| 442 | |
---|
| 443 | /* Expose event */ |
---|
| 444 | if(xevent.type == Expose) |
---|
| 445 | { |
---|
[811] | 446 | XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 447 | dp->drv.p->window, dp->drv.p->gc, 0, 0, |
---|
| 448 | dp->cv->width * dp->drv.p->font_width, |
---|
| 449 | dp->cv->height * dp->drv.p->font_height, 0, 0); |
---|
[548] | 450 | continue; |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | /* Resize event */ |
---|
| 454 | if(xevent.type == ConfigureNotify) |
---|
| 455 | { |
---|
| 456 | unsigned int w, h; |
---|
| 457 | |
---|
[811] | 458 | w = (xevent.xconfigure.width + dp->drv.p->font_width / 3) |
---|
| 459 | / dp->drv.p->font_width; |
---|
| 460 | h = (xevent.xconfigure.height + dp->drv.p->font_height / 3) |
---|
| 461 | / dp->drv.p->font_height; |
---|
[548] | 462 | |
---|
[811] | 463 | if(!w || !h || (w == dp->cv->width && h == dp->cv->height)) |
---|
[548] | 464 | continue; |
---|
| 465 | |
---|
[811] | 466 | dp->resize.w = w; |
---|
| 467 | dp->resize.h = h; |
---|
| 468 | dp->resize.resized = 1; |
---|
[548] | 469 | |
---|
[553] | 470 | continue; |
---|
[548] | 471 | } |
---|
| 472 | |
---|
| 473 | /* Check for mouse motion events */ |
---|
| 474 | if(xevent.type == MotionNotify) |
---|
| 475 | { |
---|
[811] | 476 | unsigned int newx = xevent.xmotion.x / dp->drv.p->font_width; |
---|
| 477 | unsigned int newy = xevent.xmotion.y / dp->drv.p->font_height; |
---|
[548] | 478 | |
---|
[811] | 479 | if(newx >= dp->cv->width) |
---|
| 480 | newx = dp->cv->width - 1; |
---|
| 481 | if(newy >= dp->cv->height) |
---|
| 482 | newy = dp->cv->height - 1; |
---|
[548] | 483 | |
---|
[811] | 484 | if(dp->mouse.x == newx && dp->mouse.y == newy) |
---|
[548] | 485 | continue; |
---|
| 486 | |
---|
[811] | 487 | dp->mouse.x = newx; |
---|
| 488 | dp->mouse.y = newy; |
---|
[548] | 489 | |
---|
[681] | 490 | ev->type = CACA_EVENT_MOUSE_MOTION; |
---|
[811] | 491 | ev->data.mouse.x = dp->mouse.x; |
---|
| 492 | ev->data.mouse.y = dp->mouse.y; |
---|
[681] | 493 | return 1; |
---|
[548] | 494 | } |
---|
| 495 | |
---|
| 496 | /* Check for mouse press and release events */ |
---|
| 497 | if(xevent.type == ButtonPress) |
---|
[681] | 498 | { |
---|
| 499 | ev->type = CACA_EVENT_MOUSE_PRESS; |
---|
| 500 | ev->data.mouse.button = ((XButtonEvent *)&xevent)->button; |
---|
| 501 | return 1; |
---|
| 502 | } |
---|
[548] | 503 | |
---|
| 504 | if(xevent.type == ButtonRelease) |
---|
[681] | 505 | { |
---|
| 506 | ev->type = CACA_EVENT_MOUSE_RELEASE; |
---|
| 507 | ev->data.mouse.button = ((XButtonEvent *)&xevent)->button; |
---|
| 508 | return 1; |
---|
| 509 | } |
---|
[548] | 510 | |
---|
| 511 | /* Check for key press and release events */ |
---|
| 512 | if(xevent.type == KeyPress) |
---|
[681] | 513 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
[548] | 514 | else if(xevent.type == KeyRelease) |
---|
[681] | 515 | ev->type = CACA_EVENT_KEY_RELEASE; |
---|
[548] | 516 | else |
---|
| 517 | continue; |
---|
| 518 | |
---|
| 519 | if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL)) |
---|
[681] | 520 | { |
---|
[810] | 521 | ev->data.key.ch = key; |
---|
[969] | 522 | ev->data.key.utf32 = key; |
---|
[681] | 523 | ev->data.key.utf8[0] = key; |
---|
| 524 | ev->data.key.utf8[1] = '\0'; |
---|
| 525 | return 1; |
---|
| 526 | } |
---|
[548] | 527 | |
---|
[811] | 528 | keysym = XKeycodeToKeysym(dp->drv.p->dpy, xevent.xkey.keycode, 0); |
---|
[548] | 529 | switch(keysym) |
---|
| 530 | { |
---|
[810] | 531 | case XK_F1: ev->data.key.ch = CACA_KEY_F1; break; |
---|
| 532 | case XK_F2: ev->data.key.ch = CACA_KEY_F2; break; |
---|
| 533 | case XK_F3: ev->data.key.ch = CACA_KEY_F3; break; |
---|
| 534 | case XK_F4: ev->data.key.ch = CACA_KEY_F4; break; |
---|
| 535 | case XK_F5: ev->data.key.ch = CACA_KEY_F5; break; |
---|
| 536 | case XK_F6: ev->data.key.ch = CACA_KEY_F6; break; |
---|
| 537 | case XK_F7: ev->data.key.ch = CACA_KEY_F7; break; |
---|
| 538 | case XK_F8: ev->data.key.ch = CACA_KEY_F8; break; |
---|
| 539 | case XK_F9: ev->data.key.ch = CACA_KEY_F9; break; |
---|
| 540 | case XK_F10: ev->data.key.ch = CACA_KEY_F10; break; |
---|
| 541 | case XK_F11: ev->data.key.ch = CACA_KEY_F11; break; |
---|
| 542 | case XK_F12: ev->data.key.ch = CACA_KEY_F12; break; |
---|
| 543 | case XK_F13: ev->data.key.ch = CACA_KEY_F13; break; |
---|
| 544 | case XK_F14: ev->data.key.ch = CACA_KEY_F14; break; |
---|
| 545 | case XK_F15: ev->data.key.ch = CACA_KEY_F15; break; |
---|
| 546 | case XK_Left: ev->data.key.ch = CACA_KEY_LEFT; break; |
---|
| 547 | case XK_Right: ev->data.key.ch = CACA_KEY_RIGHT; break; |
---|
| 548 | case XK_Up: ev->data.key.ch = CACA_KEY_UP; break; |
---|
| 549 | case XK_Down: ev->data.key.ch = CACA_KEY_DOWN; break; |
---|
[909] | 550 | case XK_KP_Page_Up: |
---|
[907] | 551 | case XK_Page_Up: ev->data.key.ch = CACA_KEY_PAGEUP; break; |
---|
[909] | 552 | case XK_KP_Page_Down: |
---|
[907] | 553 | case XK_Page_Down: ev->data.key.ch = CACA_KEY_PAGEDOWN; break; |
---|
[909] | 554 | case XK_KP_Home: |
---|
[908] | 555 | case XK_Home: ev->data.key.ch = CACA_KEY_HOME; break; |
---|
[909] | 556 | case XK_KP_End: |
---|
[908] | 557 | case XK_End: ev->data.key.ch = CACA_KEY_END; break; |
---|
[681] | 558 | |
---|
| 559 | default: ev->type = CACA_EVENT_NONE; return 0; |
---|
[548] | 560 | } |
---|
[681] | 561 | |
---|
[969] | 562 | ev->data.key.utf32 = 0; |
---|
[681] | 563 | ev->data.key.utf8[0] = '\0'; |
---|
| 564 | return 1; |
---|
[548] | 565 | } |
---|
| 566 | |
---|
[811] | 567 | while(XCheckTypedEvent(dp->drv.p->dpy, ClientMessage, &xevent)) |
---|
[799] | 568 | { |
---|
[811] | 569 | if(xevent.xclient.message_type != dp->drv.p->wm_protocols) |
---|
[799] | 570 | continue; |
---|
| 571 | |
---|
[811] | 572 | if((Atom)xevent.xclient.data.l[0] == dp->drv.p->wm_delete_window) |
---|
[799] | 573 | { |
---|
| 574 | ev->type = CACA_EVENT_QUIT; |
---|
| 575 | return 1; |
---|
| 576 | } |
---|
| 577 | } |
---|
| 578 | |
---|
[681] | 579 | ev->type = CACA_EVENT_NONE; |
---|
| 580 | return 0; |
---|
[548] | 581 | } |
---|
| 582 | |
---|
[811] | 583 | static void x11_set_mouse(caca_display_t *dp, int flags) |
---|
[687] | 584 | { |
---|
[688] | 585 | Cursor no_ptr; |
---|
| 586 | Pixmap bm_no; |
---|
| 587 | XColor black, dummy; |
---|
| 588 | Colormap colormap; |
---|
[689] | 589 | static char const empty[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
---|
[687] | 590 | |
---|
[689] | 591 | if(flags) |
---|
| 592 | { |
---|
[811] | 593 | XDefineCursor(dp->drv.p->dpy,dp->drv.p->window, 0); |
---|
[689] | 594 | return; |
---|
| 595 | } |
---|
| 596 | |
---|
[811] | 597 | colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy)); |
---|
| 598 | if(!XAllocNamedColor(dp->drv.p->dpy, colormap, "black", &black, &dummy)) |
---|
[688] | 599 | { |
---|
[689] | 600 | return; |
---|
[688] | 601 | } |
---|
[811] | 602 | bm_no = XCreateBitmapFromData(dp->drv.p->dpy, dp->drv.p->window, |
---|
[689] | 603 | empty, 8, 8); |
---|
[811] | 604 | no_ptr = XCreatePixmapCursor(dp->drv.p->dpy, bm_no, bm_no, |
---|
[689] | 605 | &black, &black, 0, 0); |
---|
[811] | 606 | XDefineCursor(dp->drv.p->dpy, dp->drv.p->window, no_ptr); |
---|
| 607 | XFreeCursor(dp->drv.p->dpy, no_ptr); |
---|
[689] | 608 | if(bm_no != None) |
---|
[811] | 609 | XFreePixmap(dp->drv.p->dpy, bm_no); |
---|
| 610 | XFreeColors(dp->drv.p->dpy, colormap, &black.pixel, 1, 0); |
---|
[688] | 611 | |
---|
[811] | 612 | XSync(dp->drv.p->dpy, False); |
---|
[687] | 613 | } |
---|
| 614 | |
---|
[539] | 615 | /* |
---|
| 616 | * XXX: following functions are local |
---|
| 617 | */ |
---|
[281] | 618 | |
---|
[548] | 619 | static int x11_error_handler(Display *dpy, XErrorEvent *xevent) |
---|
[300] | 620 | { |
---|
[347] | 621 | /* Ignore the error */ |
---|
[300] | 622 | return 0; |
---|
| 623 | } |
---|
| 624 | |
---|
[539] | 625 | /* |
---|
| 626 | * Driver initialisation |
---|
| 627 | */ |
---|
[348] | 628 | |
---|
[811] | 629 | int x11_install(caca_display_t *dp) |
---|
[511] | 630 | { |
---|
[684] | 631 | #if defined(HAVE_GETENV) |
---|
| 632 | if(!getenv("DISPLAY") || !*(getenv("DISPLAY"))) |
---|
| 633 | return -1; |
---|
| 634 | #endif |
---|
| 635 | |
---|
[811] | 636 | dp->drv.driver = CACA_DRIVER_X11; |
---|
[527] | 637 | |
---|
[811] | 638 | dp->drv.init_graphics = x11_init_graphics; |
---|
| 639 | dp->drv.end_graphics = x11_end_graphics; |
---|
[819] | 640 | dp->drv.set_display_title = x11_set_display_title; |
---|
| 641 | dp->drv.get_display_width = x11_get_display_width; |
---|
| 642 | dp->drv.get_display_height = x11_get_display_height; |
---|
[811] | 643 | dp->drv.display = x11_display; |
---|
| 644 | dp->drv.handle_resize = x11_handle_resize; |
---|
| 645 | dp->drv.get_event = x11_get_event; |
---|
| 646 | dp->drv.set_mouse = x11_set_mouse; |
---|
[1430] | 647 | dp->drv.set_cursor = NULL; |
---|
[684] | 648 | |
---|
| 649 | return 0; |
---|
[511] | 650 | } |
---|
| 651 | |
---|
[539] | 652 | #endif /* USE_X11 */ |
---|
[527] | 653 | |
---|