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.
 
 
 
 
 
 

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