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.
 
 
 
 
 
 

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