Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

995 строки
32 KiB

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