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.
 
 
 
 
 
 

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