You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

975 lines
31 KiB

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