[35] | 1 | /* |
---|
[672] | 2 | * libcaca Colour ASCII-Art library |
---|
[3494] | 3 | * Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net> |
---|
[1775] | 4 | * 2007 Ben Wiley Sittler <bsittler@gmail.com> |
---|
[268] | 5 | * All Rights Reserved |
---|
[35] | 6 | * |
---|
[769] | 7 | * $Id: x11.c 4146 2009-12-18 21:50:37Z sam $ |
---|
| 8 | * |
---|
[1462] | 9 | * This library is free software. It comes without any warranty, to |
---|
[1452] | 10 | * the extent permitted by applicable law. You can redistribute it |
---|
| 11 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
| 12 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
[522] | 13 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
[35] | 14 | */ |
---|
[17] | 15 | |
---|
[769] | 16 | /* |
---|
[540] | 17 | * This file contains the libcaca X11 input and output driver |
---|
[205] | 18 | */ |
---|
| 19 | |
---|
[63] | 20 | #include "config.h" |
---|
| 21 | |
---|
[539] | 22 | #if defined(USE_X11) |
---|
| 23 | |
---|
| 24 | #include <X11/Xlib.h> |
---|
[548] | 25 | #include <X11/Xutil.h> |
---|
| 26 | #include <X11/keysym.h> |
---|
[4141] | 27 | #if defined HAVE_X11_XKBLIB_H |
---|
[539] | 28 | # include <X11/XKBlib.h> |
---|
| 29 | #endif |
---|
| 30 | |
---|
[226] | 31 | #include <stdio.h> /* BUFSIZ */ |
---|
[17] | 32 | #include <stdlib.h> |
---|
[1775] | 33 | #include <string.h> |
---|
[4142] | 34 | #if defined HAVE_LOCALE_H |
---|
| 35 | # include <locale.h> |
---|
| 36 | #endif |
---|
[17] | 37 | |
---|
[185] | 38 | #include "caca.h" |
---|
[2821] | 39 | #include "caca.h" |
---|
[185] | 40 | #include "caca_internals.h" |
---|
[17] | 41 | |
---|
[281] | 42 | /* |
---|
| 43 | * Local functions |
---|
| 44 | */ |
---|
[300] | 45 | static int x11_error_handler(Display *, XErrorEvent *); |
---|
[1775] | 46 | static void x11_put_glyph(caca_display_t *, int, int, int, int, int, |
---|
| 47 | uint32_t, uint32_t); |
---|
[300] | 48 | |
---|
[550] | 49 | struct driver_private |
---|
| 50 | { |
---|
| 51 | Display *dpy; |
---|
| 52 | Window window; |
---|
| 53 | Pixmap pixmap; |
---|
| 54 | GC gc; |
---|
| 55 | long int event_mask; |
---|
| 56 | int font_width, font_height; |
---|
[780] | 57 | int colors[4096]; |
---|
[4141] | 58 | #if defined X_HAVE_UTF8_STRING |
---|
| 59 | XFontSet font_set; |
---|
| 60 | #endif |
---|
[550] | 61 | Font font; |
---|
| 62 | XFontStruct *font_struct; |
---|
| 63 | int font_offset; |
---|
[687] | 64 | Cursor pointer; |
---|
[799] | 65 | Atom wm_protocols; |
---|
| 66 | Atom wm_delete_window; |
---|
[4141] | 67 | #if defined HAVE_X11_XKBLIB_H |
---|
[550] | 68 | Bool autorepeat; |
---|
| 69 | #endif |
---|
[1775] | 70 | uint32_t max_char; |
---|
[1837] | 71 | int cursor_flags; |
---|
[3581] | 72 | int dirty_cursor_x, dirty_cursor_y; |
---|
[4140] | 73 | #if defined X_HAVE_UTF8_STRING |
---|
[3949] | 74 | XIM im; |
---|
| 75 | XIC ic; |
---|
| 76 | #endif |
---|
[550] | 77 | }; |
---|
| 78 | |
---|
[1775] | 79 | #define UNICODE_XLFD_SUFFIX "-iso10646-1" |
---|
| 80 | #define LATIN_1_XLFD_SUFFIX "-iso8859-1" |
---|
| 81 | |
---|
[811] | 82 | static int x11_init_graphics(caca_display_t *dp) |
---|
[227] | 83 | { |
---|
[539] | 84 | Colormap colormap; |
---|
| 85 | XSetWindowAttributes x11_winattr; |
---|
[4141] | 86 | #if defined X_HAVE_UTF8_STRING |
---|
| 87 | XVaNestedList list; |
---|
| 88 | #endif |
---|
[539] | 89 | int (*old_error_handler)(Display *, XErrorEvent *); |
---|
[3913] | 90 | char const *fonts[] = { NULL, "8x13bold", "fixed", NULL }, **parser; |
---|
[539] | 91 | char const *geometry; |
---|
[2821] | 92 | int width = caca_get_canvas_width(dp->cv); |
---|
| 93 | int height = caca_get_canvas_height(dp->cv); |
---|
[539] | 94 | int i; |
---|
[231] | 95 | |
---|
[811] | 96 | dp->drv.p = malloc(sizeof(struct driver_private)); |
---|
[550] | 97 | |
---|
[4141] | 98 | #if defined HAVE_GETENV |
---|
[539] | 99 | geometry = getenv("CACA_GEOMETRY"); |
---|
[613] | 100 | if(geometry && *geometry) |
---|
[539] | 101 | sscanf(geometry, "%ux%u", &width, &height); |
---|
[684] | 102 | #endif |
---|
[227] | 103 | |
---|
[3569] | 104 | caca_add_dirty_rect(dp->cv, 0, 0, dp->cv->width, dp->cv->height); |
---|
[2055] | 105 | dp->resize.allow = 1; |
---|
[2821] | 106 | caca_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32); |
---|
| 107 | width = caca_get_canvas_width(dp->cv); |
---|
| 108 | height = caca_get_canvas_height(dp->cv); |
---|
[2055] | 109 | dp->resize.allow = 0; |
---|
[227] | 110 | |
---|
[4142] | 111 | #if defined HAVE_LOCALE_H |
---|
| 112 | setlocale(LC_ALL, ""); |
---|
| 113 | #endif |
---|
| 114 | |
---|
[811] | 115 | dp->drv.p->dpy = XOpenDisplay(NULL); |
---|
| 116 | if(dp->drv.p->dpy == NULL) |
---|
[539] | 117 | return -1; |
---|
[227] | 118 | |
---|
[4141] | 119 | #if defined HAVE_GETENV |
---|
[539] | 120 | fonts[0] = getenv("CACA_FONT"); |
---|
| 121 | if(fonts[0] && *fonts[0]) |
---|
| 122 | parser = fonts; |
---|
| 123 | else |
---|
[684] | 124 | #endif |
---|
[539] | 125 | parser = fonts + 1; |
---|
[227] | 126 | |
---|
[539] | 127 | /* Ignore font errors */ |
---|
| 128 | old_error_handler = XSetErrorHandler(x11_error_handler); |
---|
[227] | 129 | |
---|
[539] | 130 | /* Parse our font list */ |
---|
| 131 | for( ; ; parser++) |
---|
[265] | 132 | { |
---|
[4141] | 133 | #if defined X_HAVE_UTF8_STRING |
---|
| 134 | char **missing_charset_list; |
---|
| 135 | char *def_string; |
---|
| 136 | int missing_charset_count; |
---|
| 137 | #endif |
---|
[2305] | 138 | uint32_t font_max_char; |
---|
[1775] | 139 | |
---|
[539] | 140 | if(!*parser) |
---|
[265] | 141 | { |
---|
[539] | 142 | XSetErrorHandler(old_error_handler); |
---|
[811] | 143 | XCloseDisplay(dp->drv.p->dpy); |
---|
[265] | 144 | return -1; |
---|
| 145 | } |
---|
[263] | 146 | |
---|
[4141] | 147 | #if defined X_HAVE_UTF8_STRING |
---|
| 148 | dp->drv.p->font_set = XCreateFontSet(dp->drv.p->dpy, *parser, |
---|
| 149 | &missing_charset_list, |
---|
| 150 | &missing_charset_count, |
---|
| 151 | &def_string); |
---|
| 152 | if (missing_charset_list) |
---|
| 153 | XFreeStringList(missing_charset_list); |
---|
| 154 | |
---|
| 155 | if (!dp->drv.p->font_set || missing_charset_count) |
---|
| 156 | { |
---|
| 157 | char buf[BUFSIZ]; |
---|
| 158 | |
---|
| 159 | if (dp->drv.p->font_set) |
---|
| 160 | XFreeFontSet(dp->drv.p->dpy, dp->drv.p->font_set); |
---|
| 161 | snprintf(buf, BUFSIZ - 1, "%s,*", *parser); |
---|
| 162 | dp->drv.p->font_set = XCreateFontSet(dp->drv.p->dpy, buf, |
---|
| 163 | &missing_charset_list, |
---|
| 164 | &missing_charset_count, |
---|
| 165 | &def_string); |
---|
| 166 | if (missing_charset_list) |
---|
| 167 | XFreeStringList(missing_charset_list); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | if (dp->drv.p->font_set) |
---|
| 171 | break; |
---|
| 172 | #endif |
---|
| 173 | |
---|
[811] | 174 | dp->drv.p->font = XLoadFont(dp->drv.p->dpy, *parser); |
---|
| 175 | if(!dp->drv.p->font) |
---|
[539] | 176 | continue; |
---|
[300] | 177 | |
---|
[811] | 178 | dp->drv.p->font_struct = XQueryFont(dp->drv.p->dpy, dp->drv.p->font); |
---|
| 179 | if(!dp->drv.p->font_struct) |
---|
[265] | 180 | { |
---|
[811] | 181 | XUnloadFont(dp->drv.p->dpy, dp->drv.p->font); |
---|
[539] | 182 | continue; |
---|
[265] | 183 | } |
---|
[251] | 184 | |
---|
[1775] | 185 | if((strlen(*parser) > sizeof(UNICODE_XLFD_SUFFIX)) |
---|
| 186 | && !strcasecmp(*parser + strlen(*parser) |
---|
| 187 | - strlen(UNICODE_XLFD_SUFFIX), UNICODE_XLFD_SUFFIX)) |
---|
| 188 | dp->drv.p->max_char = 0xffff; |
---|
| 189 | else if((strlen(*parser) > sizeof(LATIN_1_XLFD_SUFFIX)) |
---|
| 190 | && !strcasecmp(*parser + strlen(*parser) |
---|
| 191 | - strlen(LATIN_1_XLFD_SUFFIX), LATIN_1_XLFD_SUFFIX)) |
---|
| 192 | dp->drv.p->max_char = 0xff; |
---|
| 193 | else |
---|
| 194 | dp->drv.p->max_char = 0x7f; |
---|
| 195 | |
---|
[3480] | 196 | font_max_char = |
---|
[2305] | 197 | (dp->drv.p->font_struct->max_byte1 << 8) |
---|
[1775] | 198 | | dp->drv.p->font_struct->max_char_or_byte2; |
---|
| 199 | if(font_max_char && (font_max_char < dp->drv.p->max_char)) |
---|
| 200 | dp->drv.p->max_char = font_max_char; |
---|
| 201 | |
---|
[539] | 202 | break; |
---|
| 203 | } |
---|
[272] | 204 | |
---|
[539] | 205 | /* Reset the default X11 error handler */ |
---|
| 206 | XSetErrorHandler(old_error_handler); |
---|
[3480] | 207 | |
---|
[4141] | 208 | /* Set font width to the largest character in the set */ |
---|
| 209 | #if defined X_HAVE_UTF8_STRING |
---|
| 210 | if (dp->drv.p->font_set) |
---|
[1775] | 211 | { |
---|
[4143] | 212 | /* Do not trust the fontset, because some fonts may have |
---|
| 213 | * irrelevantly large glyphs. Pango and rxvt use the same method. */ |
---|
| 214 | static wchar_t const test[] = |
---|
| 215 | { |
---|
| 216 | '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', |
---|
| 217 | 0x00cd, 0x00e7, 0x00d5, 0x0114, 0x0177, /* Í ç Õ Ĕ ŷ */ |
---|
| 218 | /* 0x0643, */ /* ﻙ */ |
---|
| 219 | 0x304c, 0x672c, /* が本 */ |
---|
| 220 | 0, |
---|
| 221 | }; |
---|
| 222 | |
---|
| 223 | XRectangle ink, logical; |
---|
| 224 | |
---|
[4141] | 225 | dp->drv.p->font_width = |
---|
[4143] | 226 | (XmbTextEscapement(dp->drv.p->font_set, "CAca", 4) + 2) / 4; |
---|
| 227 | XwcTextExtents(dp->drv.p->font_set, test, sizeof(test) / sizeof(*test), |
---|
| 228 | &ink, &logical); |
---|
| 229 | dp->drv.p->font_height = ink.height; |
---|
| 230 | dp->drv.p->font_offset = ink.height + ink.y; |
---|
[4141] | 231 | } |
---|
| 232 | else |
---|
| 233 | #endif |
---|
| 234 | { |
---|
| 235 | dp->drv.p->font_width = 0; |
---|
| 236 | if(dp->drv.p->font_struct->per_char |
---|
| 237 | && !dp->drv.p->font_struct->min_byte1 |
---|
| 238 | && dp->drv.p->font_struct->min_char_or_byte2 <= 0x21 |
---|
| 239 | && dp->drv.p->font_struct->max_char_or_byte2 >= 0x7e) |
---|
[1775] | 240 | { |
---|
[4141] | 241 | for(i = 0x21; i < 0x7f; i++) |
---|
| 242 | { |
---|
| 243 | int cw = dp->drv.p->font_struct->per_char[i |
---|
| 244 | - dp->drv.p->font_struct->min_char_or_byte2].width; |
---|
| 245 | if(cw > dp->drv.p->font_width) |
---|
| 246 | dp->drv.p->font_width = cw; |
---|
| 247 | } |
---|
[1775] | 248 | } |
---|
[272] | 249 | |
---|
[4141] | 250 | if(!dp->drv.p->font_width) |
---|
| 251 | dp->drv.p->font_width = dp->drv.p->font_struct->max_bounds.width; |
---|
[1775] | 252 | |
---|
[4141] | 253 | dp->drv.p->font_height = dp->drv.p->font_struct->max_bounds.ascent |
---|
| 254 | + dp->drv.p->font_struct->max_bounds.descent; |
---|
| 255 | dp->drv.p->font_offset = dp->drv.p->font_struct->max_bounds.descent; |
---|
| 256 | } |
---|
[272] | 257 | |
---|
[811] | 258 | colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy)); |
---|
[780] | 259 | for(i = 0x000; i < 0x1000; i++) |
---|
[539] | 260 | { |
---|
| 261 | XColor color; |
---|
[780] | 262 | color.red = ((i & 0xf00) >> 8) * 0x1111; |
---|
| 263 | color.green = ((i & 0x0f0) >> 4) * 0x1111; |
---|
| 264 | color.blue = (i & 0x00f) * 0x1111; |
---|
[811] | 265 | XAllocColor(dp->drv.p->dpy, colormap, &color); |
---|
| 266 | dp->drv.p->colors[i] = color.pixel; |
---|
[539] | 267 | } |
---|
[251] | 268 | |
---|
[539] | 269 | x11_winattr.backing_store = Always; |
---|
[811] | 270 | x11_winattr.background_pixel = dp->drv.p->colors[0x000]; |
---|
[539] | 271 | x11_winattr.event_mask = ExposureMask | StructureNotifyMask; |
---|
[251] | 272 | |
---|
[811] | 273 | dp->drv.p->window = |
---|
| 274 | XCreateWindow(dp->drv.p->dpy, DefaultRootWindow(dp->drv.p->dpy), 0, 0, |
---|
[2056] | 275 | width * dp->drv.p->font_width, |
---|
| 276 | height * dp->drv.p->font_height, |
---|
[539] | 277 | 0, 0, InputOutput, 0, |
---|
| 278 | CWBackingStore | CWBackPixel | CWEventMask, |
---|
| 279 | &x11_winattr); |
---|
[251] | 280 | |
---|
[811] | 281 | dp->drv.p->wm_protocols = |
---|
| 282 | XInternAtom(dp->drv.p->dpy, "WM_PROTOCOLS", True); |
---|
| 283 | dp->drv.p->wm_delete_window = |
---|
| 284 | XInternAtom(dp->drv.p->dpy, "WM_DELETE_WINDOW", True); |
---|
[799] | 285 | |
---|
[811] | 286 | if(dp->drv.p->wm_protocols != None && dp->drv.p->wm_delete_window != None) |
---|
| 287 | XSetWMProtocols(dp->drv.p->dpy, dp->drv.p->window, |
---|
| 288 | &dp->drv.p->wm_delete_window, 1); |
---|
[799] | 289 | |
---|
[811] | 290 | XStoreName(dp->drv.p->dpy, dp->drv.p->window, "caca for X"); |
---|
[324] | 291 | |
---|
[811] | 292 | XSelectInput(dp->drv.p->dpy, dp->drv.p->window, StructureNotifyMask); |
---|
| 293 | XMapWindow(dp->drv.p->dpy, dp->drv.p->window); |
---|
[251] | 294 | |
---|
[811] | 295 | dp->drv.p->gc = XCreateGC(dp->drv.p->dpy, dp->drv.p->window, 0, NULL); |
---|
| 296 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, dp->drv.p->colors[0x888]); |
---|
[4141] | 297 | #if defined X_HAVE_UTF8_STRING |
---|
| 298 | if (!dp->drv.p->font_set) |
---|
| 299 | #endif |
---|
| 300 | XSetFont(dp->drv.p->dpy, dp->drv.p->gc, dp->drv.p->font); |
---|
[251] | 301 | |
---|
[539] | 302 | for(;;) |
---|
| 303 | { |
---|
[548] | 304 | XEvent xevent; |
---|
[811] | 305 | XNextEvent(dp->drv.p->dpy, &xevent); |
---|
[1775] | 306 | if(xevent.type == MapNotify) |
---|
[539] | 307 | break; |
---|
[265] | 308 | } |
---|
[336] | 309 | |
---|
[4141] | 310 | #if defined HAVE_X11_XKBLIB_H |
---|
[539] | 311 | /* Disable autorepeat */ |
---|
[811] | 312 | XkbSetDetectableAutoRepeat(dp->drv.p->dpy, True, &dp->drv.p->autorepeat); |
---|
| 313 | if(!dp->drv.p->autorepeat) |
---|
| 314 | XAutoRepeatOff(dp->drv.p->dpy); |
---|
[336] | 315 | #endif |
---|
[491] | 316 | |
---|
[811] | 317 | dp->drv.p->event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask |
---|
[539] | 318 | | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask |
---|
| 319 | | ExposureMask; |
---|
[483] | 320 | |
---|
[811] | 321 | XSelectInput(dp->drv.p->dpy, dp->drv.p->window, dp->drv.p->event_mask); |
---|
[524] | 322 | |
---|
[811] | 323 | XSync(dp->drv.p->dpy, False); |
---|
[483] | 324 | |
---|
[811] | 325 | dp->drv.p->pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window, |
---|
[2056] | 326 | width * dp->drv.p->font_width, |
---|
| 327 | height * dp->drv.p->font_height, |
---|
| 328 | DefaultDepth(dp->drv.p->dpy, |
---|
| 329 | DefaultScreen(dp->drv.p->dpy))); |
---|
[811] | 330 | dp->drv.p->pointer = None; |
---|
[687] | 331 | |
---|
[1837] | 332 | dp->drv.p->cursor_flags = 0; |
---|
[3581] | 333 | dp->drv.p->dirty_cursor_x = -1; |
---|
| 334 | dp->drv.p->dirty_cursor_y = -1; |
---|
[1837] | 335 | |
---|
[4141] | 336 | #if defined X_HAVE_UTF8_STRING |
---|
| 337 | list = XVaCreateNestedList(0, XNFontSet, dp->drv.p->font_set, NULL); |
---|
[3949] | 338 | dp->drv.p->im = XOpenIM(dp->drv.p->dpy, NULL, NULL, NULL); |
---|
[4141] | 339 | dp->drv.p->ic = XCreateIC(dp->drv.p->im, |
---|
| 340 | XNInputStyle, XIMPreeditNothing | XIMStatusNothing, |
---|
| 341 | XNClientWindow, dp->drv.p->window, |
---|
| 342 | XNPreeditAttributes, list, |
---|
| 343 | XNStatusAttributes, list, |
---|
| 344 | NULL); |
---|
| 345 | #endif |
---|
[3949] | 346 | |
---|
[227] | 347 | return 0; |
---|
| 348 | } |
---|
| 349 | |
---|
[811] | 350 | static int x11_end_graphics(caca_display_t *dp) |
---|
[251] | 351 | { |
---|
[811] | 352 | XSync(dp->drv.p->dpy, False); |
---|
[4141] | 353 | #if defined HAVE_X11_XKBLIB_H |
---|
[811] | 354 | if(!dp->drv.p->autorepeat) |
---|
| 355 | XAutoRepeatOn(dp->drv.p->dpy); |
---|
[324] | 356 | #endif |
---|
[811] | 357 | XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap); |
---|
[4141] | 358 | #if defined X_HAVE_UTF8_STRING |
---|
| 359 | if (dp->drv.p->font_set) |
---|
| 360 | XFreeFontSet(dp->drv.p->dpy, dp->drv.p->font_set); |
---|
| 361 | else |
---|
| 362 | #endif |
---|
| 363 | XFreeFont(dp->drv.p->dpy, dp->drv.p->font_struct); |
---|
[811] | 364 | XFreeGC(dp->drv.p->dpy, dp->drv.p->gc); |
---|
| 365 | XUnmapWindow(dp->drv.p->dpy, dp->drv.p->window); |
---|
| 366 | XDestroyWindow(dp->drv.p->dpy, dp->drv.p->window); |
---|
[4141] | 367 | #if defined X_HAVE_UTF8_STRING |
---|
[3949] | 368 | XDestroyIC(dp->drv.p->ic); |
---|
| 369 | XCloseIM(dp->drv.p->im); |
---|
[4141] | 370 | #endif |
---|
[811] | 371 | XCloseDisplay(dp->drv.p->dpy); |
---|
[300] | 372 | |
---|
[811] | 373 | free(dp->drv.p); |
---|
[550] | 374 | |
---|
[251] | 375 | return 0; |
---|
| 376 | } |
---|
| 377 | |
---|
[819] | 378 | static int x11_set_display_title(caca_display_t *dp, char const *title) |
---|
[343] | 379 | { |
---|
[811] | 380 | XStoreName(dp->drv.p->dpy, dp->drv.p->window, title); |
---|
[343] | 381 | return 0; |
---|
| 382 | } |
---|
| 383 | |
---|
[2305] | 384 | static int x11_get_display_width(caca_display_t const *dp) |
---|
[352] | 385 | { |
---|
[2821] | 386 | return caca_get_canvas_width(dp->cv) * dp->drv.p->font_width; |
---|
[352] | 387 | } |
---|
| 388 | |
---|
[2305] | 389 | static int x11_get_display_height(caca_display_t const *dp) |
---|
[352] | 390 | { |
---|
[2821] | 391 | return caca_get_canvas_height(dp->cv) * dp->drv.p->font_height; |
---|
[352] | 392 | } |
---|
| 393 | |
---|
[811] | 394 | static void x11_display(caca_display_t *dp) |
---|
[227] | 395 | { |
---|
[4146] | 396 | uint32_t const *cvchars = caca_get_canvas_chars(dp->cv); |
---|
| 397 | uint32_t const *cvattrs = caca_get_canvas_attrs(dp->cv); |
---|
[2821] | 398 | int width = caca_get_canvas_width(dp->cv); |
---|
| 399 | int height = caca_get_canvas_height(dp->cv); |
---|
[3470] | 400 | int x, y, i, len; |
---|
[227] | 401 | |
---|
[3581] | 402 | /* XXX: the magic value -1 is used to handle the cursor area */ |
---|
| 403 | for(i = -1; i < caca_get_dirty_rect_count(dp->cv); i++) |
---|
[3470] | 404 | { |
---|
[3494] | 405 | int dx, dy, dw, dh; |
---|
[3467] | 406 | |
---|
[3581] | 407 | /* Get the dirty rectangle coordinates, either from the previous |
---|
| 408 | * cursor position, or from the canvas's list. */ |
---|
| 409 | if(i == -1) |
---|
| 410 | { |
---|
| 411 | if(dp->drv.p->dirty_cursor_x < 0 || dp->drv.p->dirty_cursor_y < 0 |
---|
| 412 | || dp->drv.p->dirty_cursor_x >= width |
---|
| 413 | || dp->drv.p->dirty_cursor_y >= height) |
---|
| 414 | continue; |
---|
[3470] | 415 | |
---|
[3581] | 416 | dx = dp->drv.p->dirty_cursor_x; |
---|
| 417 | dy = dp->drv.p->dirty_cursor_y; |
---|
| 418 | dw = dh = 1; |
---|
| 419 | |
---|
| 420 | dp->drv.p->dirty_cursor_x = -1; |
---|
| 421 | dp->drv.p->dirty_cursor_y = -1; |
---|
| 422 | } |
---|
| 423 | else |
---|
| 424 | { |
---|
| 425 | caca_get_dirty_rect(dp->cv, i, &dx, &dy, &dw, &dh); |
---|
| 426 | } |
---|
| 427 | |
---|
[3470] | 428 | /* First draw the background colours. Splitting the process in two |
---|
| 429 | * loops like this is actually slightly faster. */ |
---|
[3494] | 430 | for(y = dy; y < dy + dh; y++) |
---|
[527] | 431 | { |
---|
[3494] | 432 | for(x = dx; x < dx + dw; x += len) |
---|
[3470] | 433 | { |
---|
| 434 | uint32_t const *attrs = cvattrs + x + y * width; |
---|
| 435 | uint16_t bg = caca_attr_to_rgb12_bg(*attrs); |
---|
[261] | 436 | |
---|
[3470] | 437 | len = 1; |
---|
[3510] | 438 | while(x + len < dx + dw |
---|
[3470] | 439 | && caca_attr_to_rgb12_bg(attrs[len]) == bg) |
---|
| 440 | len++; |
---|
[300] | 441 | |
---|
[3470] | 442 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, |
---|
| 443 | dp->drv.p->colors[bg]); |
---|
| 444 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 445 | dp->drv.p->gc, |
---|
| 446 | x * dp->drv.p->font_width, |
---|
| 447 | y * dp->drv.p->font_height, |
---|
| 448 | len * dp->drv.p->font_width, |
---|
| 449 | dp->drv.p->font_height); |
---|
| 450 | } |
---|
[303] | 451 | } |
---|
[300] | 452 | |
---|
[3470] | 453 | /* Then print the foreground characters */ |
---|
[3494] | 454 | for(y = dy; y < dy + dh; y++) |
---|
[3470] | 455 | { |
---|
| 456 | int yoff = (y + 1) * dp->drv.p->font_height |
---|
| 457 | - dp->drv.p->font_offset; |
---|
[3494] | 458 | uint32_t const *chars = cvchars + dx + y * width; |
---|
| 459 | uint32_t const *attrs = cvattrs + dx + y * width; |
---|
[557] | 460 | |
---|
[3494] | 461 | for(x = dx; x < dx + dw; x++, chars++, attrs++) |
---|
[3470] | 462 | { |
---|
| 463 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, |
---|
[2821] | 464 | dp->drv.p->colors[caca_attr_to_rgb12_fg(*attrs)]); |
---|
[532] | 465 | |
---|
[3470] | 466 | x11_put_glyph(dp, x * dp->drv.p->font_width, |
---|
| 467 | y * dp->drv.p->font_height, yoff, |
---|
| 468 | dp->drv.p->font_width, dp->drv.p->font_height, |
---|
| 469 | *attrs, *chars); |
---|
| 470 | } |
---|
[508] | 471 | } |
---|
| 472 | } |
---|
[336] | 473 | |
---|
[3581] | 474 | /* Print the cursor if necessary. */ |
---|
[1837] | 475 | if(dp->drv.p->cursor_flags) |
---|
| 476 | { |
---|
| 477 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, |
---|
| 478 | dp->drv.p->colors[0xfff]); |
---|
[3587] | 479 | x = caca_wherex(dp->cv); |
---|
| 480 | y = caca_wherey(dp->cv); |
---|
[1837] | 481 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc, |
---|
| 482 | x * dp->drv.p->font_width, y * dp->drv.p->font_height, |
---|
| 483 | dp->drv.p->font_width, dp->drv.p->font_height); |
---|
[3581] | 484 | |
---|
| 485 | /* Mark the area as dirty */ |
---|
| 486 | dp->drv.p->dirty_cursor_x = x; |
---|
| 487 | dp->drv.p->dirty_cursor_y = y; |
---|
[1837] | 488 | } |
---|
| 489 | |
---|
[811] | 490 | XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->window, |
---|
| 491 | dp->drv.p->gc, 0, 0, |
---|
[2056] | 492 | width * dp->drv.p->font_width, |
---|
| 493 | height * dp->drv.p->font_height, |
---|
[539] | 494 | 0, 0); |
---|
[811] | 495 | XFlush(dp->drv.p->dpy); |
---|
[227] | 496 | } |
---|
| 497 | |
---|
[811] | 498 | static void x11_handle_resize(caca_display_t *dp) |
---|
[347] | 499 | { |
---|
[539] | 500 | Pixmap new_pixmap; |
---|
| 501 | |
---|
[811] | 502 | new_pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window, |
---|
| 503 | dp->resize.w * dp->drv.p->font_width, |
---|
| 504 | dp->resize.h * dp->drv.p->font_height, |
---|
| 505 | DefaultDepth(dp->drv.p->dpy, |
---|
| 506 | DefaultScreen(dp->drv.p->dpy))); |
---|
| 507 | XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, new_pixmap, |
---|
| 508 | dp->drv.p->gc, 0, 0, |
---|
| 509 | dp->resize.w * dp->drv.p->font_width, |
---|
| 510 | dp->resize.h * dp->drv.p->font_height, 0, 0); |
---|
| 511 | XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap); |
---|
| 512 | dp->drv.p->pixmap = new_pixmap; |
---|
[347] | 513 | } |
---|
| 514 | |
---|
[2049] | 515 | static int x11_get_event(caca_display_t *dp, caca_privevent_t *ev) |
---|
[548] | 516 | { |
---|
[2821] | 517 | int width = caca_get_canvas_width(dp->cv); |
---|
| 518 | int height = caca_get_canvas_height(dp->cv); |
---|
[548] | 519 | XEvent xevent; |
---|
| 520 | char key; |
---|
| 521 | |
---|
[811] | 522 | while(XCheckWindowEvent(dp->drv.p->dpy, dp->drv.p->window, |
---|
| 523 | dp->drv.p->event_mask, &xevent) == True) |
---|
[548] | 524 | { |
---|
| 525 | KeySym keysym; |
---|
| 526 | |
---|
| 527 | /* Expose event */ |
---|
| 528 | if(xevent.type == Expose) |
---|
| 529 | { |
---|
[811] | 530 | XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
| 531 | dp->drv.p->window, dp->drv.p->gc, 0, 0, |
---|
[2056] | 532 | width * dp->drv.p->font_width, |
---|
| 533 | height * dp->drv.p->font_height, 0, 0); |
---|
[548] | 534 | continue; |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | /* Resize event */ |
---|
| 538 | if(xevent.type == ConfigureNotify) |
---|
| 539 | { |
---|
[2305] | 540 | int w, h; |
---|
[548] | 541 | |
---|
[811] | 542 | w = (xevent.xconfigure.width + dp->drv.p->font_width / 3) |
---|
| 543 | / dp->drv.p->font_width; |
---|
| 544 | h = (xevent.xconfigure.height + dp->drv.p->font_height / 3) |
---|
| 545 | / dp->drv.p->font_height; |
---|
[548] | 546 | |
---|
[2056] | 547 | if(!w || !h || (w == width && h == height)) |
---|
[548] | 548 | continue; |
---|
| 549 | |
---|
[811] | 550 | dp->resize.w = w; |
---|
| 551 | dp->resize.h = h; |
---|
| 552 | dp->resize.resized = 1; |
---|
[548] | 553 | |
---|
[553] | 554 | continue; |
---|
[548] | 555 | } |
---|
| 556 | |
---|
| 557 | /* Check for mouse motion events */ |
---|
| 558 | if(xevent.type == MotionNotify) |
---|
| 559 | { |
---|
[2305] | 560 | int newx = xevent.xmotion.x / dp->drv.p->font_width; |
---|
| 561 | int newy = xevent.xmotion.y / dp->drv.p->font_height; |
---|
[548] | 562 | |
---|
[2056] | 563 | if(newx >= width) |
---|
| 564 | newx = width - 1; |
---|
| 565 | if(newy >= height) |
---|
| 566 | newy = height - 1; |
---|
[548] | 567 | |
---|
[811] | 568 | if(dp->mouse.x == newx && dp->mouse.y == newy) |
---|
[548] | 569 | continue; |
---|
| 570 | |
---|
[811] | 571 | dp->mouse.x = newx; |
---|
| 572 | dp->mouse.y = newy; |
---|
[548] | 573 | |
---|
[681] | 574 | ev->type = CACA_EVENT_MOUSE_MOTION; |
---|
[811] | 575 | ev->data.mouse.x = dp->mouse.x; |
---|
| 576 | ev->data.mouse.y = dp->mouse.y; |
---|
[681] | 577 | return 1; |
---|
[548] | 578 | } |
---|
| 579 | |
---|
| 580 | /* Check for mouse press and release events */ |
---|
| 581 | if(xevent.type == ButtonPress) |
---|
[681] | 582 | { |
---|
| 583 | ev->type = CACA_EVENT_MOUSE_PRESS; |
---|
| 584 | ev->data.mouse.button = ((XButtonEvent *)&xevent)->button; |
---|
| 585 | return 1; |
---|
| 586 | } |
---|
[548] | 587 | |
---|
| 588 | if(xevent.type == ButtonRelease) |
---|
[681] | 589 | { |
---|
| 590 | ev->type = CACA_EVENT_MOUSE_RELEASE; |
---|
| 591 | ev->data.mouse.button = ((XButtonEvent *)&xevent)->button; |
---|
| 592 | return 1; |
---|
| 593 | } |
---|
[548] | 594 | |
---|
| 595 | /* Check for key press and release events */ |
---|
| 596 | if(xevent.type == KeyPress) |
---|
[681] | 597 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
[548] | 598 | else if(xevent.type == KeyRelease) |
---|
[681] | 599 | ev->type = CACA_EVENT_KEY_RELEASE; |
---|
[548] | 600 | else |
---|
| 601 | continue; |
---|
| 602 | |
---|
[4140] | 603 | #if defined X_HAVE_UTF8_STRING |
---|
[3949] | 604 | if(Xutf8LookupString(dp->drv.p->ic, &xevent.xkey, ev->data.key.utf8, 8, NULL, NULL)) |
---|
| 605 | { |
---|
| 606 | ev->data.key.utf32 = caca_utf8_to_utf32(ev->data.key.utf8, NULL); |
---|
[4069] | 607 | if(ev->data.key.utf32 <= 0x7f) |
---|
[3949] | 608 | { |
---|
| 609 | ev->data.key.ch = ev->data.key.utf32; |
---|
| 610 | } else { |
---|
| 611 | ev->data.key.ch = 0; |
---|
| 612 | } |
---|
| 613 | return 1; |
---|
| 614 | } |
---|
| 615 | #endif |
---|
[548] | 616 | if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL)) |
---|
[681] | 617 | { |
---|
[810] | 618 | ev->data.key.ch = key; |
---|
[969] | 619 | ev->data.key.utf32 = key; |
---|
[681] | 620 | ev->data.key.utf8[0] = key; |
---|
| 621 | ev->data.key.utf8[1] = '\0'; |
---|
| 622 | return 1; |
---|
| 623 | } |
---|
[548] | 624 | |
---|
[811] | 625 | keysym = XKeycodeToKeysym(dp->drv.p->dpy, xevent.xkey.keycode, 0); |
---|
[548] | 626 | switch(keysym) |
---|
| 627 | { |
---|
[810] | 628 | case XK_F1: ev->data.key.ch = CACA_KEY_F1; break; |
---|
| 629 | case XK_F2: ev->data.key.ch = CACA_KEY_F2; break; |
---|
| 630 | case XK_F3: ev->data.key.ch = CACA_KEY_F3; break; |
---|
| 631 | case XK_F4: ev->data.key.ch = CACA_KEY_F4; break; |
---|
| 632 | case XK_F5: ev->data.key.ch = CACA_KEY_F5; break; |
---|
| 633 | case XK_F6: ev->data.key.ch = CACA_KEY_F6; break; |
---|
| 634 | case XK_F7: ev->data.key.ch = CACA_KEY_F7; break; |
---|
| 635 | case XK_F8: ev->data.key.ch = CACA_KEY_F8; break; |
---|
| 636 | case XK_F9: ev->data.key.ch = CACA_KEY_F9; break; |
---|
| 637 | case XK_F10: ev->data.key.ch = CACA_KEY_F10; break; |
---|
| 638 | case XK_F11: ev->data.key.ch = CACA_KEY_F11; break; |
---|
| 639 | case XK_F12: ev->data.key.ch = CACA_KEY_F12; break; |
---|
| 640 | case XK_F13: ev->data.key.ch = CACA_KEY_F13; break; |
---|
| 641 | case XK_F14: ev->data.key.ch = CACA_KEY_F14; break; |
---|
| 642 | case XK_F15: ev->data.key.ch = CACA_KEY_F15; break; |
---|
| 643 | case XK_Left: ev->data.key.ch = CACA_KEY_LEFT; break; |
---|
| 644 | case XK_Right: ev->data.key.ch = CACA_KEY_RIGHT; break; |
---|
| 645 | case XK_Up: ev->data.key.ch = CACA_KEY_UP; break; |
---|
| 646 | case XK_Down: ev->data.key.ch = CACA_KEY_DOWN; break; |
---|
[909] | 647 | case XK_KP_Page_Up: |
---|
[907] | 648 | case XK_Page_Up: ev->data.key.ch = CACA_KEY_PAGEUP; break; |
---|
[909] | 649 | case XK_KP_Page_Down: |
---|
[907] | 650 | case XK_Page_Down: ev->data.key.ch = CACA_KEY_PAGEDOWN; break; |
---|
[909] | 651 | case XK_KP_Home: |
---|
[908] | 652 | case XK_Home: ev->data.key.ch = CACA_KEY_HOME; break; |
---|
[909] | 653 | case XK_KP_End: |
---|
[908] | 654 | case XK_End: ev->data.key.ch = CACA_KEY_END; break; |
---|
[681] | 655 | |
---|
| 656 | default: ev->type = CACA_EVENT_NONE; return 0; |
---|
[548] | 657 | } |
---|
[681] | 658 | |
---|
[969] | 659 | ev->data.key.utf32 = 0; |
---|
[681] | 660 | ev->data.key.utf8[0] = '\0'; |
---|
| 661 | return 1; |
---|
[548] | 662 | } |
---|
| 663 | |
---|
[811] | 664 | while(XCheckTypedEvent(dp->drv.p->dpy, ClientMessage, &xevent)) |
---|
[799] | 665 | { |
---|
[811] | 666 | if(xevent.xclient.message_type != dp->drv.p->wm_protocols) |
---|
[799] | 667 | continue; |
---|
| 668 | |
---|
[811] | 669 | if((Atom)xevent.xclient.data.l[0] == dp->drv.p->wm_delete_window) |
---|
[799] | 670 | { |
---|
| 671 | ev->type = CACA_EVENT_QUIT; |
---|
| 672 | return 1; |
---|
| 673 | } |
---|
| 674 | } |
---|
| 675 | |
---|
[681] | 676 | ev->type = CACA_EVENT_NONE; |
---|
| 677 | return 0; |
---|
[548] | 678 | } |
---|
| 679 | |
---|
[811] | 680 | static void x11_set_mouse(caca_display_t *dp, int flags) |
---|
[687] | 681 | { |
---|
[688] | 682 | Cursor no_ptr; |
---|
| 683 | Pixmap bm_no; |
---|
| 684 | XColor black, dummy; |
---|
| 685 | Colormap colormap; |
---|
[689] | 686 | static char const empty[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
---|
[687] | 687 | |
---|
[689] | 688 | if(flags) |
---|
| 689 | { |
---|
[811] | 690 | XDefineCursor(dp->drv.p->dpy,dp->drv.p->window, 0); |
---|
[689] | 691 | return; |
---|
| 692 | } |
---|
| 693 | |
---|
[811] | 694 | colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy)); |
---|
| 695 | if(!XAllocNamedColor(dp->drv.p->dpy, colormap, "black", &black, &dummy)) |
---|
[688] | 696 | { |
---|
[689] | 697 | return; |
---|
[688] | 698 | } |
---|
[811] | 699 | bm_no = XCreateBitmapFromData(dp->drv.p->dpy, dp->drv.p->window, |
---|
[689] | 700 | empty, 8, 8); |
---|
[811] | 701 | no_ptr = XCreatePixmapCursor(dp->drv.p->dpy, bm_no, bm_no, |
---|
[689] | 702 | &black, &black, 0, 0); |
---|
[811] | 703 | XDefineCursor(dp->drv.p->dpy, dp->drv.p->window, no_ptr); |
---|
| 704 | XFreeCursor(dp->drv.p->dpy, no_ptr); |
---|
[689] | 705 | if(bm_no != None) |
---|
[811] | 706 | XFreePixmap(dp->drv.p->dpy, bm_no); |
---|
| 707 | XFreeColors(dp->drv.p->dpy, colormap, &black.pixel, 1, 0); |
---|
[688] | 708 | |
---|
[811] | 709 | XSync(dp->drv.p->dpy, False); |
---|
[687] | 710 | } |
---|
| 711 | |
---|
[1837] | 712 | static void x11_set_cursor(caca_display_t *dp, int flags) |
---|
| 713 | { |
---|
| 714 | dp->drv.p->cursor_flags = flags; |
---|
| 715 | } |
---|
| 716 | |
---|
[539] | 717 | /* |
---|
| 718 | * XXX: following functions are local |
---|
| 719 | */ |
---|
[281] | 720 | |
---|
[548] | 721 | static int x11_error_handler(Display *dpy, XErrorEvent *xevent) |
---|
[300] | 722 | { |
---|
[347] | 723 | /* Ignore the error */ |
---|
[300] | 724 | return 0; |
---|
| 725 | } |
---|
| 726 | |
---|
[1775] | 727 | static void x11_put_glyph(caca_display_t *dp, int x, int y, int yoff, |
---|
| 728 | int w, int h, uint32_t attr, uint32_t ch) |
---|
| 729 | { |
---|
[1778] | 730 | static uint8_t const udlr[] = |
---|
| 731 | { |
---|
| 732 | /* 0x2500 - 0x250f: ─ . │ . . . . . . . . . ┌ . . . */ |
---|
| 733 | 0x05, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
| 734 | 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, |
---|
| 735 | /* 0x2510 - 0x251f: ┐ . . . └ . . . ┘ . . . ├ . . . */ |
---|
| 736 | 0x14, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, |
---|
[1779] | 737 | 0x44, 0x00, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00, |
---|
[1778] | 738 | /* 0x2520 - 0x252f: . . . . ┤ . . . . . . . ┬ . . . */ |
---|
[1779] | 739 | 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, |
---|
[1778] | 740 | 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, |
---|
| 741 | /* 0x2530 - 0x253f: . . . . ┴ . . . . . . . ┼ . . . */ |
---|
| 742 | 0x00, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00, |
---|
| 743 | 0x00, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, |
---|
| 744 | /* 0x2540 - 0x254f: . . . . . . . . . . . . . . . . */ |
---|
| 745 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
| 746 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
| 747 | /* 0x2550 - 0x255f: ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ */ |
---|
| 748 | 0x0a, 0xa0, 0x12, 0x21, 0x22, 0x18, 0x24, 0x28, |
---|
| 749 | 0x42, 0x81, 0x82, 0x48, 0x84, 0x88, 0x52, 0xa1, |
---|
| 750 | /* 0x2560 - 0x256c: ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ */ |
---|
| 751 | 0xa2, 0x58, 0xa4, 0xa8, 0x1a, 0x25, 0x2a, 0x4a, |
---|
| 752 | 0x85, 0x8a, 0x5a, 0xa5, 0xaa, |
---|
| 753 | }; |
---|
| 754 | |
---|
[1775] | 755 | Display *dpy = dp->drv.p->dpy; |
---|
| 756 | Pixmap px = dp->drv.p->pixmap; |
---|
| 757 | GC gc = dp->drv.p->gc; |
---|
| 758 | int fw; |
---|
| 759 | |
---|
| 760 | /* Underline */ |
---|
[2821] | 761 | if(attr & CACA_UNDERLINE) |
---|
[1775] | 762 | XFillRectangle(dpy, px, gc, x, y + h - 1, w, 1); |
---|
| 763 | |
---|
| 764 | /* Skip spaces and magic stuff */ |
---|
| 765 | if(ch <= 0x00000020) |
---|
| 766 | return; |
---|
| 767 | |
---|
[2821] | 768 | if(ch == CACA_MAGIC_FULLWIDTH) |
---|
[1775] | 769 | return; |
---|
| 770 | |
---|
| 771 | fw = w; |
---|
[2821] | 772 | if(caca_utf32_is_fullwidth(ch)) |
---|
[1775] | 773 | fw *= 2; |
---|
| 774 | |
---|
| 775 | /* We want to be able to print a few special Unicode characters |
---|
| 776 | * such as the CP437 gradients and half blocks. For unknown |
---|
[2821] | 777 | * characters, print what caca_utf32_to_ascii() returns. */ |
---|
[1775] | 778 | |
---|
[1778] | 779 | if(ch >= 0x2500 && ch <= 0x256c && udlr[ch - 0x2500]) |
---|
[1775] | 780 | { |
---|
| 781 | uint16_t D = udlr[ch - 0x2500]; |
---|
| 782 | |
---|
| 783 | if(D & 0x04) |
---|
| 784 | XFillRectangle(dpy, px, gc, x, y + h / 2, fw / 2 + 1, 1); |
---|
| 785 | |
---|
| 786 | if(D & 0x01) |
---|
| 787 | XFillRectangle(dpy, px, gc, |
---|
| 788 | x + fw / 2, y + h / 2, (fw + 1) / 2, 1); |
---|
| 789 | |
---|
| 790 | if(D & 0x40) |
---|
| 791 | XFillRectangle(dpy, px, gc, x + fw / 2, y, 1, h / 2 + 1); |
---|
| 792 | |
---|
| 793 | if(D & 0x10) |
---|
| 794 | XFillRectangle(dpy, px, gc, x + fw / 2, y + h / 2, 1, (h + 1) / 2); |
---|
| 795 | |
---|
| 796 | #define STEPIF(a,b) (D&(a)?-1:(D&(b))?1:0) |
---|
| 797 | |
---|
| 798 | if(D & 0x08) |
---|
| 799 | { |
---|
| 800 | XFillRectangle(dpy, px, gc, x, y - 1 + h / 2, |
---|
| 801 | fw / 2 + 1 + STEPIF(0xc0,0x20), 1); |
---|
| 802 | XFillRectangle(dpy, px, gc, x, y + 1 + h / 2, |
---|
| 803 | fw / 2 + 1 + STEPIF(0x30,0x80), 1); |
---|
| 804 | } |
---|
| 805 | |
---|
| 806 | if(D & 0x02) |
---|
| 807 | { |
---|
| 808 | XFillRectangle(dpy, px, gc, x - STEPIF(0xc0,0x20) + fw / 2, |
---|
| 809 | y - 1 + h / 2, (fw + 1) / 2 + STEPIF(0xc0,0x20), 1); |
---|
| 810 | XFillRectangle(dpy, px, gc, x - STEPIF(0x30,0x80) + fw / 2, |
---|
| 811 | y + 1 + h / 2, (fw + 1) / 2 + STEPIF(0x30,0x80), 1); |
---|
| 812 | } |
---|
| 813 | |
---|
| 814 | if(D & 0x80) |
---|
| 815 | { |
---|
| 816 | XFillRectangle(dpy, px, gc, x - 1 + fw / 2, y, |
---|
| 817 | 1, h / 2 + 1 + STEPIF(0x0c,0x02)); |
---|
| 818 | XFillRectangle(dpy, px, gc, x + 1 + fw / 2, y, |
---|
| 819 | 1, h / 2 + 1 + STEPIF(0x03,0x08)); |
---|
| 820 | } |
---|
| 821 | |
---|
| 822 | if(D & 0x20) |
---|
| 823 | { |
---|
| 824 | XFillRectangle(dpy, px, gc, x - 1 + fw / 2, |
---|
| 825 | y - STEPIF(0x0c,0x02) + h / 2, |
---|
| 826 | 1, (h + 1) / 2 + STEPIF(0x0c,0x02)); |
---|
| 827 | XFillRectangle(dpy, px, gc, x + 1 + fw / 2, |
---|
| 828 | y - STEPIF(0x03,0x08) + h / 2, |
---|
| 829 | 1, (h + 1) / 2 + STEPIF(0x03,0x08)); |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | return; |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | switch(ch) |
---|
| 836 | { |
---|
| 837 | case 0x000000b7: /* · */ |
---|
| 838 | case 0x00002219: /* ∙ */ |
---|
| 839 | case 0x000030fb: /* ・ */ |
---|
| 840 | XFillRectangle(dpy, px, gc, x + fw / 2 - 1, y + h / 2 - 1, 2, 2); |
---|
| 841 | return; |
---|
| 842 | |
---|
| 843 | case 0x00002261: /* ≡ */ |
---|
| 844 | XFillRectangle(dpy, px, gc, x + 1, y - 2 + h / 2, fw - 1, 1); |
---|
| 845 | XFillRectangle(dpy, px, gc, x + 1, y + h / 2, fw - 1, 1); |
---|
| 846 | XFillRectangle(dpy, px, gc, x + 1, y + 2 + h / 2, fw - 1, 1); |
---|
| 847 | return; |
---|
| 848 | |
---|
| 849 | case 0x00002580: /* ▀ */ |
---|
| 850 | XFillRectangle(dpy, px, gc, x, y, fw, h / 2); |
---|
| 851 | return; |
---|
| 852 | |
---|
| 853 | case 0x00002584: /* ▄ */ |
---|
| 854 | XFillRectangle(dpy, px, gc, x, y + h - h / 2, fw, h / 2); |
---|
| 855 | return; |
---|
| 856 | |
---|
| 857 | case 0x00002588: /* █ */ |
---|
| 858 | case 0x000025ae: /* ▮ */ |
---|
| 859 | XFillRectangle(dpy, px, gc, x, y, fw, h); |
---|
| 860 | return; |
---|
| 861 | |
---|
| 862 | case 0x0000258c: /* ▌ */ |
---|
| 863 | XFillRectangle(dpy, px, gc, x, y, fw / 2, h); |
---|
| 864 | return; |
---|
| 865 | |
---|
| 866 | case 0x00002590: /* ▐ */ |
---|
| 867 | XFillRectangle(dpy, px, gc, x + fw - fw / 2, y, fw / 2, h); |
---|
| 868 | return; |
---|
| 869 | |
---|
| 870 | case 0x000025a0: /* ■ */ |
---|
| 871 | case 0x000025ac: /* ▬ */ |
---|
| 872 | XFillRectangle(dpy, px, gc, x, y + h / 4, fw, h / 2); |
---|
| 873 | return; |
---|
| 874 | |
---|
| 875 | case 0x00002593: /* ▓ */ |
---|
| 876 | case 0x00002592: /* ▒ */ |
---|
| 877 | case 0x00002591: /* ░ */ |
---|
| 878 | { |
---|
| 879 | /* FIXME: this sucks utterly */ |
---|
| 880 | int i, j, k = ch - 0x00002591; |
---|
| 881 | for(j = h; j--; ) |
---|
| 882 | for(i = fw; i--; ) |
---|
| 883 | { |
---|
| 884 | if(((i + 2 * (j & 1)) & 3) > k) |
---|
| 885 | continue; |
---|
| 886 | |
---|
| 887 | XDrawPoint(dpy, px, gc, x + i, y + j); |
---|
| 888 | } |
---|
| 889 | return; |
---|
| 890 | } |
---|
| 891 | |
---|
| 892 | case 0x000025cb: /* ○ */ |
---|
| 893 | case 0x00002022: /* • */ |
---|
| 894 | case 0x000025cf: /* ● */ |
---|
| 895 | { |
---|
| 896 | int d, xo, yo; |
---|
| 897 | |
---|
[1780] | 898 | d = fw >> (~ch & 0x1); /* XXX: hack */ |
---|
[1775] | 899 | if(h < fw) |
---|
| 900 | d = h; |
---|
| 901 | if(d < 1) |
---|
| 902 | d = 1; |
---|
| 903 | xo = (fw - d) / 2; |
---|
| 904 | yo = (h - d) / 2; |
---|
| 905 | if(ch == 0x000025cb) |
---|
| 906 | XDrawArc(dpy, px, gc, x + xo, y + yo, d, d, 0, 64 * 360); |
---|
| 907 | else |
---|
| 908 | XFillArc(dpy, px, gc, x + xo, y + yo, d, d, 0, 64 * 360); |
---|
| 909 | return; |
---|
| 910 | } |
---|
| 911 | } |
---|
| 912 | |
---|
[4140] | 913 | #if defined X_HAVE_UTF8_STRING |
---|
[4141] | 914 | if (dp->drv.p->font_set) |
---|
[1775] | 915 | { |
---|
[4141] | 916 | wchar_t wch = ch; |
---|
[4143] | 917 | XwcDrawString(dpy, px, dp->drv.p->font_set, gc, x, yoff, &wch, 1); |
---|
[1775] | 918 | } |
---|
| 919 | else |
---|
[4141] | 920 | #endif |
---|
[1775] | 921 | { |
---|
[4141] | 922 | XChar2b ch16; |
---|
| 923 | |
---|
| 924 | #if !defined X_HAVE_UTF8_STRING |
---|
| 925 | if(ch > dp->drv.p->max_char) |
---|
| 926 | { |
---|
| 927 | ch16.byte1 = 0; |
---|
| 928 | ch16.byte2 = caca_utf32_to_ascii(ch); |
---|
| 929 | } |
---|
| 930 | else |
---|
| 931 | #endif |
---|
| 932 | { |
---|
| 933 | ch16.byte1 = (uint8_t)(ch >> 8); |
---|
| 934 | ch16.byte2 = (uint8_t)ch; |
---|
| 935 | } |
---|
| 936 | |
---|
[4143] | 937 | XDrawString16(dpy, px, gc, x, yoff, &ch16, 1); |
---|
[1775] | 938 | } |
---|
| 939 | } |
---|
| 940 | |
---|
[539] | 941 | /* |
---|
| 942 | * Driver initialisation |
---|
| 943 | */ |
---|
[348] | 944 | |
---|
[811] | 945 | int x11_install(caca_display_t *dp) |
---|
[511] | 946 | { |
---|
[4141] | 947 | #if defined HAVE_GETENV |
---|
[684] | 948 | if(!getenv("DISPLAY") || !*(getenv("DISPLAY"))) |
---|
| 949 | return -1; |
---|
| 950 | #endif |
---|
| 951 | |
---|
[2138] | 952 | dp->drv.id = CACA_DRIVER_X11; |
---|
| 953 | dp->drv.driver = "x11"; |
---|
[527] | 954 | |
---|
[811] | 955 | dp->drv.init_graphics = x11_init_graphics; |
---|
| 956 | dp->drv.end_graphics = x11_end_graphics; |
---|
[819] | 957 | dp->drv.set_display_title = x11_set_display_title; |
---|
| 958 | dp->drv.get_display_width = x11_get_display_width; |
---|
| 959 | dp->drv.get_display_height = x11_get_display_height; |
---|
[811] | 960 | dp->drv.display = x11_display; |
---|
| 961 | dp->drv.handle_resize = x11_handle_resize; |
---|
| 962 | dp->drv.get_event = x11_get_event; |
---|
| 963 | dp->drv.set_mouse = x11_set_mouse; |
---|
[1837] | 964 | dp->drv.set_cursor = x11_set_cursor; |
---|
[684] | 965 | |
---|
| 966 | return 0; |
---|
[511] | 967 | } |
---|
| 968 | |
---|
[539] | 969 | #endif /* USE_X11 */ |
---|
[527] | 970 | |
---|