No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

971 líneas
31 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
  4. * 2007 Ben Wiley Sittler <bsittler@gmail.com>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software. It comes without any warranty, to
  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
  13. * http://sam.zoy.org/wtfpl/COPYING for more details.
  14. */
  15. /*
  16. * This file contains the libcaca X11 input and output driver
  17. */
  18. #include "config.h"
  19. #if defined(USE_X11)
  20. #include <X11/Xlib.h>
  21. #include <X11/Xutil.h>
  22. #include <X11/keysym.h>
  23. #if defined HAVE_X11_XKBLIB_H
  24. # include <X11/XKBlib.h>
  25. #endif
  26. #include <stdio.h> /* BUFSIZ */
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #if defined HAVE_LOCALE_H
  30. # include <locale.h>
  31. #endif
  32. #include "caca.h"
  33. #include "caca.h"
  34. #include "caca_internals.h"
  35. /*
  36. * Local functions
  37. */
  38. static int x11_error_handler(Display *, XErrorEvent *);
  39. static void x11_put_glyph(caca_display_t *, int, int, int, int, int,
  40. uint32_t, uint32_t);
  41. struct driver_private
  42. {
  43. Display *dpy;
  44. Window window;
  45. Pixmap pixmap;
  46. GC gc;
  47. long int event_mask;
  48. int font_width, font_height;
  49. int colors[4096];
  50. #if defined X_HAVE_UTF8_STRING
  51. XFontSet font_set;
  52. #endif
  53. Font font;
  54. XFontStruct *font_struct;
  55. int font_offset;
  56. Cursor pointer;
  57. Atom wm_protocols;
  58. Atom wm_delete_window;
  59. #if defined HAVE_X11_XKBLIB_H
  60. Bool autorepeat;
  61. #endif
  62. uint32_t max_char;
  63. int cursor_flags;
  64. int dirty_cursor_x, dirty_cursor_y;
  65. #if defined X_HAVE_UTF8_STRING
  66. XIM im;
  67. XIC ic;
  68. #endif
  69. };
  70. #define UNICODE_XLFD_SUFFIX "-iso10646-1"
  71. #define LATIN_1_XLFD_SUFFIX "-iso8859-1"
  72. static int x11_init_graphics(caca_display_t *dp)
  73. {
  74. Colormap colormap;
  75. XSetWindowAttributes x11_winattr;
  76. #if defined X_HAVE_UTF8_STRING
  77. XVaNestedList list;
  78. #endif
  79. int (*old_error_handler)(Display *, XErrorEvent *);
  80. char const *fonts[] = { NULL, "8x13bold", "fixed", NULL }, **parser;
  81. char const *geometry;
  82. int width = caca_get_canvas_width(dp->cv);
  83. int height = caca_get_canvas_height(dp->cv);
  84. int i;
  85. dp->drv.p = malloc(sizeof(struct driver_private));
  86. #if defined HAVE_GETENV
  87. geometry = getenv("CACA_GEOMETRY");
  88. if(geometry && *geometry)
  89. sscanf(geometry, "%ux%u", &width, &height);
  90. #endif
  91. caca_add_dirty_rect(dp->cv, 0, 0, dp->cv->width, dp->cv->height);
  92. dp->resize.allow = 1;
  93. caca_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32);
  94. width = caca_get_canvas_width(dp->cv);
  95. height = caca_get_canvas_height(dp->cv);
  96. dp->resize.allow = 0;
  97. #if defined HAVE_LOCALE_H
  98. setlocale(LC_ALL, "");
  99. #endif
  100. dp->drv.p->dpy = XOpenDisplay(NULL);
  101. if(dp->drv.p->dpy == NULL)
  102. return -1;
  103. #if defined HAVE_GETENV
  104. fonts[0] = getenv("CACA_FONT");
  105. if(fonts[0] && *fonts[0])
  106. parser = fonts;
  107. else
  108. #endif
  109. parser = fonts + 1;
  110. /* Ignore font errors */
  111. old_error_handler = XSetErrorHandler(x11_error_handler);
  112. /* Parse our font list */
  113. for( ; ; parser++)
  114. {
  115. #if defined X_HAVE_UTF8_STRING
  116. char **missing_charset_list;
  117. char *def_string;
  118. int missing_charset_count;
  119. #endif
  120. uint32_t font_max_char;
  121. if(!*parser)
  122. {
  123. XSetErrorHandler(old_error_handler);
  124. XCloseDisplay(dp->drv.p->dpy);
  125. return -1;
  126. }
  127. #if defined X_HAVE_UTF8_STRING
  128. dp->drv.p->font_set = XCreateFontSet(dp->drv.p->dpy, *parser,
  129. &missing_charset_list,
  130. &missing_charset_count,
  131. &def_string);
  132. if (missing_charset_list)
  133. XFreeStringList(missing_charset_list);
  134. if (!dp->drv.p->font_set || missing_charset_count)
  135. {
  136. char buf[BUFSIZ];
  137. if (dp->drv.p->font_set)
  138. XFreeFontSet(dp->drv.p->dpy, dp->drv.p->font_set);
  139. snprintf(buf, BUFSIZ - 1, "%s,*", *parser);
  140. dp->drv.p->font_set = XCreateFontSet(dp->drv.p->dpy, buf,
  141. &missing_charset_list,
  142. &missing_charset_count,
  143. &def_string);
  144. if (missing_charset_list)
  145. XFreeStringList(missing_charset_list);
  146. }
  147. if (dp->drv.p->font_set)
  148. break;
  149. #endif
  150. dp->drv.p->font = XLoadFont(dp->drv.p->dpy, *parser);
  151. if(!dp->drv.p->font)
  152. continue;
  153. dp->drv.p->font_struct = XQueryFont(dp->drv.p->dpy, dp->drv.p->font);
  154. if(!dp->drv.p->font_struct)
  155. {
  156. XUnloadFont(dp->drv.p->dpy, dp->drv.p->font);
  157. continue;
  158. }
  159. if((strlen(*parser) > sizeof(UNICODE_XLFD_SUFFIX))
  160. && !strcasecmp(*parser + strlen(*parser)
  161. - strlen(UNICODE_XLFD_SUFFIX), UNICODE_XLFD_SUFFIX))
  162. dp->drv.p->max_char = 0xffff;
  163. else if((strlen(*parser) > sizeof(LATIN_1_XLFD_SUFFIX))
  164. && !strcasecmp(*parser + strlen(*parser)
  165. - strlen(LATIN_1_XLFD_SUFFIX), LATIN_1_XLFD_SUFFIX))
  166. dp->drv.p->max_char = 0xff;
  167. else
  168. dp->drv.p->max_char = 0x7f;
  169. font_max_char =
  170. (dp->drv.p->font_struct->max_byte1 << 8)
  171. | dp->drv.p->font_struct->max_char_or_byte2;
  172. if(font_max_char && (font_max_char < dp->drv.p->max_char))
  173. dp->drv.p->max_char = font_max_char;
  174. break;
  175. }
  176. /* Reset the default X11 error handler */
  177. XSetErrorHandler(old_error_handler);
  178. /* Set font width to the largest character in the set */
  179. #if defined X_HAVE_UTF8_STRING
  180. if (dp->drv.p->font_set)
  181. {
  182. /* Do not trust the fontset, because some fonts may have
  183. * irrelevantly large glyphs. Pango and rxvt use the same method. */
  184. static wchar_t const test[] =
  185. {
  186. '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
  187. 0x00cd, 0x00e7, 0x00d5, 0x0114, 0x0177, /* Í ç Õ Ĕ ŷ */
  188. /* 0x0643, */ /* ﻙ */
  189. 0x304c, 0x672c, /* が本 */
  190. 0,
  191. };
  192. XRectangle ink, logical;
  193. dp->drv.p->font_width =
  194. (XmbTextEscapement(dp->drv.p->font_set, "CAca", 4) + 2) / 4;
  195. XwcTextExtents(dp->drv.p->font_set, test, sizeof(test) / sizeof(*test),
  196. &ink, &logical);
  197. dp->drv.p->font_height = ink.height;
  198. dp->drv.p->font_offset = ink.height + ink.y;
  199. }
  200. else
  201. #endif
  202. {
  203. dp->drv.p->font_width = 0;
  204. if(dp->drv.p->font_struct->per_char
  205. && !dp->drv.p->font_struct->min_byte1
  206. && dp->drv.p->font_struct->min_char_or_byte2 <= 0x21
  207. && dp->drv.p->font_struct->max_char_or_byte2 >= 0x7e)
  208. {
  209. for(i = 0x21; i < 0x7f; i++)
  210. {
  211. int cw = dp->drv.p->font_struct->per_char[i
  212. - dp->drv.p->font_struct->min_char_or_byte2].width;
  213. if(cw > dp->drv.p->font_width)
  214. dp->drv.p->font_width = cw;
  215. }
  216. }
  217. if(!dp->drv.p->font_width)
  218. dp->drv.p->font_width = dp->drv.p->font_struct->max_bounds.width;
  219. dp->drv.p->font_height = dp->drv.p->font_struct->max_bounds.ascent
  220. + dp->drv.p->font_struct->max_bounds.descent;
  221. dp->drv.p->font_offset = dp->drv.p->font_struct->max_bounds.descent;
  222. }
  223. colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy));
  224. for(i = 0x000; i < 0x1000; i++)
  225. {
  226. XColor color;
  227. color.red = ((i & 0xf00) >> 8) * 0x1111;
  228. color.green = ((i & 0x0f0) >> 4) * 0x1111;
  229. color.blue = (i & 0x00f) * 0x1111;
  230. XAllocColor(dp->drv.p->dpy, colormap, &color);
  231. dp->drv.p->colors[i] = color.pixel;
  232. }
  233. x11_winattr.backing_store = Always;
  234. x11_winattr.background_pixel = dp->drv.p->colors[0x000];
  235. x11_winattr.event_mask = ExposureMask | StructureNotifyMask;
  236. dp->drv.p->window =
  237. XCreateWindow(dp->drv.p->dpy, DefaultRootWindow(dp->drv.p->dpy), 0, 0,
  238. width * dp->drv.p->font_width,
  239. height * dp->drv.p->font_height,
  240. 0, 0, InputOutput, 0,
  241. CWBackingStore | CWBackPixel | CWEventMask,
  242. &x11_winattr);
  243. dp->drv.p->wm_protocols =
  244. XInternAtom(dp->drv.p->dpy, "WM_PROTOCOLS", True);
  245. dp->drv.p->wm_delete_window =
  246. XInternAtom(dp->drv.p->dpy, "WM_DELETE_WINDOW", True);
  247. if(dp->drv.p->wm_protocols != None && dp->drv.p->wm_delete_window != None)
  248. XSetWMProtocols(dp->drv.p->dpy, dp->drv.p->window,
  249. &dp->drv.p->wm_delete_window, 1);
  250. XStoreName(dp->drv.p->dpy, dp->drv.p->window, "caca for X");
  251. XSelectInput(dp->drv.p->dpy, dp->drv.p->window, StructureNotifyMask);
  252. XMapWindow(dp->drv.p->dpy, dp->drv.p->window);
  253. dp->drv.p->gc = XCreateGC(dp->drv.p->dpy, dp->drv.p->window, 0, NULL);
  254. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, dp->drv.p->colors[0x888]);
  255. #if defined X_HAVE_UTF8_STRING
  256. if (!dp->drv.p->font_set)
  257. #endif
  258. XSetFont(dp->drv.p->dpy, dp->drv.p->gc, dp->drv.p->font);
  259. for(;;)
  260. {
  261. XEvent xevent;
  262. XNextEvent(dp->drv.p->dpy, &xevent);
  263. if(xevent.type == MapNotify)
  264. break;
  265. }
  266. #if defined HAVE_X11_XKBLIB_H
  267. /* Disable autorepeat */
  268. XkbSetDetectableAutoRepeat(dp->drv.p->dpy, True, &dp->drv.p->autorepeat);
  269. if(!dp->drv.p->autorepeat)
  270. XAutoRepeatOff(dp->drv.p->dpy);
  271. #endif
  272. dp->drv.p->event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask
  273. | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask
  274. | ExposureMask;
  275. XSelectInput(dp->drv.p->dpy, dp->drv.p->window, dp->drv.p->event_mask);
  276. XSync(dp->drv.p->dpy, False);
  277. dp->drv.p->pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window,
  278. width * dp->drv.p->font_width,
  279. height * dp->drv.p->font_height,
  280. DefaultDepth(dp->drv.p->dpy,
  281. DefaultScreen(dp->drv.p->dpy)));
  282. dp->drv.p->pointer = None;
  283. dp->drv.p->cursor_flags = 0;
  284. dp->drv.p->dirty_cursor_x = -1;
  285. dp->drv.p->dirty_cursor_y = -1;
  286. #if defined X_HAVE_UTF8_STRING
  287. list = XVaCreateNestedList(0, XNFontSet, dp->drv.p->font_set, NULL);
  288. dp->drv.p->im = XOpenIM(dp->drv.p->dpy, NULL, NULL, NULL);
  289. dp->drv.p->ic = XCreateIC(dp->drv.p->im,
  290. XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
  291. XNClientWindow, dp->drv.p->window,
  292. XNPreeditAttributes, list,
  293. XNStatusAttributes, list,
  294. NULL);
  295. #endif
  296. return 0;
  297. }
  298. static int x11_end_graphics(caca_display_t *dp)
  299. {
  300. XSync(dp->drv.p->dpy, False);
  301. #if defined HAVE_X11_XKBLIB_H
  302. if(!dp->drv.p->autorepeat)
  303. XAutoRepeatOn(dp->drv.p->dpy);
  304. #endif
  305. XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap);
  306. #if defined X_HAVE_UTF8_STRING
  307. if (dp->drv.p->font_set)
  308. XFreeFontSet(dp->drv.p->dpy, dp->drv.p->font_set);
  309. else
  310. #endif
  311. XFreeFont(dp->drv.p->dpy, dp->drv.p->font_struct);
  312. XFreeGC(dp->drv.p->dpy, dp->drv.p->gc);
  313. XUnmapWindow(dp->drv.p->dpy, dp->drv.p->window);
  314. XDestroyWindow(dp->drv.p->dpy, dp->drv.p->window);
  315. #if defined X_HAVE_UTF8_STRING
  316. XDestroyIC(dp->drv.p->ic);
  317. XCloseIM(dp->drv.p->im);
  318. #endif
  319. XCloseDisplay(dp->drv.p->dpy);
  320. free(dp->drv.p);
  321. return 0;
  322. }
  323. static int x11_set_display_title(caca_display_t *dp, char const *title)
  324. {
  325. XStoreName(dp->drv.p->dpy, dp->drv.p->window, title);
  326. return 0;
  327. }
  328. static int x11_get_display_width(caca_display_t const *dp)
  329. {
  330. return caca_get_canvas_width(dp->cv) * dp->drv.p->font_width;
  331. }
  332. static int x11_get_display_height(caca_display_t const *dp)
  333. {
  334. return caca_get_canvas_height(dp->cv) * dp->drv.p->font_height;
  335. }
  336. static void x11_display(caca_display_t *dp)
  337. {
  338. uint32_t const *cvchars = caca_get_canvas_chars(dp->cv);
  339. uint32_t const *cvattrs = caca_get_canvas_attrs(dp->cv);
  340. int width = caca_get_canvas_width(dp->cv);
  341. int height = caca_get_canvas_height(dp->cv);
  342. int x, y, i, len;
  343. /* XXX: the magic value -1 is used to handle the cursor area */
  344. for(i = -1; i < caca_get_dirty_rect_count(dp->cv); i++)
  345. {
  346. int dx, dy, dw, dh;
  347. /* Get the dirty rectangle coordinates, either from the previous
  348. * cursor position, or from the canvas's list. */
  349. if(i == -1)
  350. {
  351. if(dp->drv.p->dirty_cursor_x < 0 || dp->drv.p->dirty_cursor_y < 0
  352. || dp->drv.p->dirty_cursor_x >= width
  353. || dp->drv.p->dirty_cursor_y >= height)
  354. continue;
  355. dx = dp->drv.p->dirty_cursor_x;
  356. dy = dp->drv.p->dirty_cursor_y;
  357. dw = dh = 1;
  358. dp->drv.p->dirty_cursor_x = -1;
  359. dp->drv.p->dirty_cursor_y = -1;
  360. }
  361. else
  362. {
  363. caca_get_dirty_rect(dp->cv, i, &dx, &dy, &dw, &dh);
  364. }
  365. /* First draw the background colours. Splitting the process in two
  366. * loops like this is actually slightly faster. */
  367. for(y = dy; y < dy + dh; y++)
  368. {
  369. for(x = dx; x < dx + dw; x += len)
  370. {
  371. uint32_t const *attrs = cvattrs + x + y * width;
  372. uint16_t bg = caca_attr_to_rgb12_bg(*attrs);
  373. len = 1;
  374. while(x + len < dx + dw
  375. && caca_attr_to_rgb12_bg(attrs[len]) == bg)
  376. len++;
  377. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc,
  378. dp->drv.p->colors[bg]);
  379. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  380. dp->drv.p->gc,
  381. x * dp->drv.p->font_width,
  382. y * dp->drv.p->font_height,
  383. len * dp->drv.p->font_width,
  384. dp->drv.p->font_height);
  385. }
  386. }
  387. /* Then print the foreground characters */
  388. for(y = dy; y < dy + dh; y++)
  389. {
  390. int yoff = (y + 1) * dp->drv.p->font_height
  391. - dp->drv.p->font_offset;
  392. uint32_t const *chars = cvchars + dx + y * width;
  393. uint32_t const *attrs = cvattrs + dx + y * width;
  394. for(x = dx; x < dx + dw; x++, chars++, attrs++)
  395. {
  396. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc,
  397. dp->drv.p->colors[caca_attr_to_rgb12_fg(*attrs)]);
  398. x11_put_glyph(dp, x * dp->drv.p->font_width,
  399. y * dp->drv.p->font_height, yoff,
  400. dp->drv.p->font_width, dp->drv.p->font_height,
  401. *attrs, *chars);
  402. }
  403. }
  404. }
  405. /* Print the cursor if necessary. */
  406. if(dp->drv.p->cursor_flags)
  407. {
  408. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc,
  409. dp->drv.p->colors[0xfff]);
  410. x = caca_wherex(dp->cv);
  411. y = caca_wherey(dp->cv);
  412. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc,
  413. x * dp->drv.p->font_width, y * dp->drv.p->font_height,
  414. dp->drv.p->font_width, dp->drv.p->font_height);
  415. /* Mark the area as dirty */
  416. dp->drv.p->dirty_cursor_x = x;
  417. dp->drv.p->dirty_cursor_y = y;
  418. }
  419. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->window,
  420. dp->drv.p->gc, 0, 0,
  421. width * dp->drv.p->font_width,
  422. height * dp->drv.p->font_height,
  423. 0, 0);
  424. XFlush(dp->drv.p->dpy);
  425. }
  426. static void x11_handle_resize(caca_display_t *dp)
  427. {
  428. Pixmap new_pixmap;
  429. new_pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window,
  430. dp->resize.w * dp->drv.p->font_width,
  431. dp->resize.h * dp->drv.p->font_height,
  432. DefaultDepth(dp->drv.p->dpy,
  433. DefaultScreen(dp->drv.p->dpy)));
  434. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, new_pixmap,
  435. dp->drv.p->gc, 0, 0,
  436. dp->resize.w * dp->drv.p->font_width,
  437. dp->resize.h * dp->drv.p->font_height, 0, 0);
  438. XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap);
  439. dp->drv.p->pixmap = new_pixmap;
  440. }
  441. static int x11_get_event(caca_display_t *dp, caca_privevent_t *ev)
  442. {
  443. int width = caca_get_canvas_width(dp->cv);
  444. int height = caca_get_canvas_height(dp->cv);
  445. XEvent xevent;
  446. char key;
  447. while(XCheckWindowEvent(dp->drv.p->dpy, dp->drv.p->window,
  448. dp->drv.p->event_mask, &xevent) == True)
  449. {
  450. KeySym keysym;
  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 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 <= 0x7f)
  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 */