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.
 
 
 
 
 
 

972 line
31 KiB

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