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.
 
 
 
 
 
 

810 lines
26 KiB

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