Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

810 строки
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. /* First draw the background colours. Splitting the process in two
  248. * loops like this is actually slightly faster. */
  249. for(y = 0; y < height; y++)
  250. {
  251. for(x = 0; x < width; x += len)
  252. {
  253. uint32_t const *attrs = cvattrs + x + y * width;
  254. uint16_t bg = caca_attr_to_rgb12_bg(*attrs);
  255. len = 1;
  256. while(x + len < width
  257. && caca_attr_to_rgb12_bg(attrs[len]) == bg)
  258. len++;
  259. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc,
  260. dp->drv.p->colors[bg]);
  261. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc,
  262. x * dp->drv.p->font_width,
  263. y * dp->drv.p->font_height,
  264. len * dp->drv.p->font_width,
  265. dp->drv.p->font_height);
  266. }
  267. }
  268. /* Then print the foreground characters */
  269. for(y = 0; y < height; y++)
  270. {
  271. int yoff = (y + 1) * dp->drv.p->font_height
  272. - dp->drv.p->font_offset;
  273. uint32_t const *chars = cvchars + y * width;
  274. uint32_t const *attrs = cvattrs + y * width;
  275. for(x = 0; x < width; x++, chars++, attrs++)
  276. {
  277. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc,
  278. dp->drv.p->colors[caca_attr_to_rgb12_fg(*attrs)]);
  279. x11_put_glyph(dp, x * dp->drv.p->font_width,
  280. y * dp->drv.p->font_height, yoff,
  281. dp->drv.p->font_width, dp->drv.p->font_height,
  282. *attrs, *chars);
  283. }
  284. }
  285. /* Print the cursor if necessary */
  286. if(dp->drv.p->cursor_flags)
  287. {
  288. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc,
  289. dp->drv.p->colors[0xfff]);
  290. x = caca_get_cursor_x(dp->cv);
  291. y = caca_get_cursor_y(dp->cv);
  292. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc,
  293. x * dp->drv.p->font_width, y * dp->drv.p->font_height,
  294. dp->drv.p->font_width, dp->drv.p->font_height);
  295. }
  296. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->window,
  297. dp->drv.p->gc, 0, 0,
  298. width * dp->drv.p->font_width,
  299. height * dp->drv.p->font_height,
  300. 0, 0);
  301. XFlush(dp->drv.p->dpy);
  302. }
  303. static void x11_handle_resize(caca_display_t *dp)
  304. {
  305. Pixmap new_pixmap;
  306. new_pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window,
  307. dp->resize.w * dp->drv.p->font_width,
  308. dp->resize.h * dp->drv.p->font_height,
  309. DefaultDepth(dp->drv.p->dpy,
  310. DefaultScreen(dp->drv.p->dpy)));
  311. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, new_pixmap,
  312. dp->drv.p->gc, 0, 0,
  313. dp->resize.w * dp->drv.p->font_width,
  314. dp->resize.h * dp->drv.p->font_height, 0, 0);
  315. XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap);
  316. dp->drv.p->pixmap = new_pixmap;
  317. }
  318. static int x11_get_event(caca_display_t *dp, caca_privevent_t *ev)
  319. {
  320. int width = caca_get_canvas_width(dp->cv);
  321. int height = caca_get_canvas_height(dp->cv);
  322. XEvent xevent;
  323. char key;
  324. while(XCheckWindowEvent(dp->drv.p->dpy, dp->drv.p->window,
  325. dp->drv.p->event_mask, &xevent) == True)
  326. {
  327. KeySym keysym;
  328. /* Expose event */
  329. if(xevent.type == Expose)
  330. {
  331. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap,
  332. dp->drv.p->window, dp->drv.p->gc, 0, 0,
  333. width * dp->drv.p->font_width,
  334. height * dp->drv.p->font_height, 0, 0);
  335. continue;
  336. }
  337. /* Resize event */
  338. if(xevent.type == ConfigureNotify)
  339. {
  340. int w, h;
  341. w = (xevent.xconfigure.width + dp->drv.p->font_width / 3)
  342. / dp->drv.p->font_width;
  343. h = (xevent.xconfigure.height + dp->drv.p->font_height / 3)
  344. / dp->drv.p->font_height;
  345. if(!w || !h || (w == width && h == height))
  346. continue;
  347. dp->resize.w = w;
  348. dp->resize.h = h;
  349. dp->resize.resized = 1;
  350. continue;
  351. }
  352. /* Check for mouse motion events */
  353. if(xevent.type == MotionNotify)
  354. {
  355. int newx = xevent.xmotion.x / dp->drv.p->font_width;
  356. int newy = xevent.xmotion.y / dp->drv.p->font_height;
  357. if(newx >= width)
  358. newx = width - 1;
  359. if(newy >= height)
  360. newy = height - 1;
  361. if(dp->mouse.x == newx && dp->mouse.y == newy)
  362. continue;
  363. dp->mouse.x = newx;
  364. dp->mouse.y = newy;
  365. ev->type = CACA_EVENT_MOUSE_MOTION;
  366. ev->data.mouse.x = dp->mouse.x;
  367. ev->data.mouse.y = dp->mouse.y;
  368. return 1;
  369. }
  370. /* Check for mouse press and release events */
  371. if(xevent.type == ButtonPress)
  372. {
  373. ev->type = CACA_EVENT_MOUSE_PRESS;
  374. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  375. return 1;
  376. }
  377. if(xevent.type == ButtonRelease)
  378. {
  379. ev->type = CACA_EVENT_MOUSE_RELEASE;
  380. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  381. return 1;
  382. }
  383. /* Check for key press and release events */
  384. if(xevent.type == KeyPress)
  385. ev->type = CACA_EVENT_KEY_PRESS;
  386. else if(xevent.type == KeyRelease)
  387. ev->type = CACA_EVENT_KEY_RELEASE;
  388. else
  389. continue;
  390. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  391. {
  392. ev->data.key.ch = key;
  393. ev->data.key.utf32 = key;
  394. ev->data.key.utf8[0] = key;
  395. ev->data.key.utf8[1] = '\0';
  396. return 1;
  397. }
  398. keysym = XKeycodeToKeysym(dp->drv.p->dpy, xevent.xkey.keycode, 0);
  399. switch(keysym)
  400. {
  401. case XK_F1: ev->data.key.ch = CACA_KEY_F1; break;
  402. case XK_F2: ev->data.key.ch = CACA_KEY_F2; break;
  403. case XK_F3: ev->data.key.ch = CACA_KEY_F3; break;
  404. case XK_F4: ev->data.key.ch = CACA_KEY_F4; break;
  405. case XK_F5: ev->data.key.ch = CACA_KEY_F5; break;
  406. case XK_F6: ev->data.key.ch = CACA_KEY_F6; break;
  407. case XK_F7: ev->data.key.ch = CACA_KEY_F7; break;
  408. case XK_F8: ev->data.key.ch = CACA_KEY_F8; break;
  409. case XK_F9: ev->data.key.ch = CACA_KEY_F9; break;
  410. case XK_F10: ev->data.key.ch = CACA_KEY_F10; break;
  411. case XK_F11: ev->data.key.ch = CACA_KEY_F11; break;
  412. case XK_F12: ev->data.key.ch = CACA_KEY_F12; break;
  413. case XK_F13: ev->data.key.ch = CACA_KEY_F13; break;
  414. case XK_F14: ev->data.key.ch = CACA_KEY_F14; break;
  415. case XK_F15: ev->data.key.ch = CACA_KEY_F15; break;
  416. case XK_Left: ev->data.key.ch = CACA_KEY_LEFT; break;
  417. case XK_Right: ev->data.key.ch = CACA_KEY_RIGHT; break;
  418. case XK_Up: ev->data.key.ch = CACA_KEY_UP; break;
  419. case XK_Down: ev->data.key.ch = CACA_KEY_DOWN; break;
  420. case XK_KP_Page_Up:
  421. case XK_Page_Up: ev->data.key.ch = CACA_KEY_PAGEUP; break;
  422. case XK_KP_Page_Down:
  423. case XK_Page_Down: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
  424. case XK_KP_Home:
  425. case XK_Home: ev->data.key.ch = CACA_KEY_HOME; break;
  426. case XK_KP_End:
  427. case XK_End: ev->data.key.ch = CACA_KEY_END; break;
  428. default: ev->type = CACA_EVENT_NONE; return 0;
  429. }
  430. ev->data.key.utf32 = 0;
  431. ev->data.key.utf8[0] = '\0';
  432. return 1;
  433. }
  434. while(XCheckTypedEvent(dp->drv.p->dpy, ClientMessage, &xevent))
  435. {
  436. if(xevent.xclient.message_type != dp->drv.p->wm_protocols)
  437. continue;
  438. if((Atom)xevent.xclient.data.l[0] == dp->drv.p->wm_delete_window)
  439. {
  440. ev->type = CACA_EVENT_QUIT;
  441. return 1;
  442. }
  443. }
  444. ev->type = CACA_EVENT_NONE;
  445. return 0;
  446. }
  447. static void x11_set_mouse(caca_display_t *dp, int flags)
  448. {
  449. Cursor no_ptr;
  450. Pixmap bm_no;
  451. XColor black, dummy;
  452. Colormap colormap;
  453. static char const empty[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  454. if(flags)
  455. {
  456. XDefineCursor(dp->drv.p->dpy,dp->drv.p->window, 0);
  457. return;
  458. }
  459. colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy));
  460. if(!XAllocNamedColor(dp->drv.p->dpy, colormap, "black", &black, &dummy))
  461. {
  462. return;
  463. }
  464. bm_no = XCreateBitmapFromData(dp->drv.p->dpy, dp->drv.p->window,
  465. empty, 8, 8);
  466. no_ptr = XCreatePixmapCursor(dp->drv.p->dpy, bm_no, bm_no,
  467. &black, &black, 0, 0);
  468. XDefineCursor(dp->drv.p->dpy, dp->drv.p->window, no_ptr);
  469. XFreeCursor(dp->drv.p->dpy, no_ptr);
  470. if(bm_no != None)
  471. XFreePixmap(dp->drv.p->dpy, bm_no);
  472. XFreeColors(dp->drv.p->dpy, colormap, &black.pixel, 1, 0);
  473. XSync(dp->drv.p->dpy, False);
  474. }
  475. static void x11_set_cursor(caca_display_t *dp, int flags)
  476. {
  477. dp->drv.p->cursor_flags = flags;
  478. }
  479. /*
  480. * XXX: following functions are local
  481. */
  482. static int x11_error_handler(Display *dpy, XErrorEvent *xevent)
  483. {
  484. /* Ignore the error */
  485. return 0;
  486. }
  487. static void x11_put_glyph(caca_display_t *dp, int x, int y, int yoff,
  488. int w, int h, uint32_t attr, uint32_t ch)
  489. {
  490. static uint8_t const udlr[] =
  491. {
  492. /* 0x2500 - 0x250f: ─ . │ . . . . . . . . . ┌ . . . */
  493. 0x05, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00,
  494. 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
  495. /* 0x2510 - 0x251f: ┐ . . . └ . . . ┘ . . . ├ . . . */
  496. 0x14, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00,
  497. 0x44, 0x00, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00,
  498. /* 0x2520 - 0x252f: . . . . ┤ . . . . . . . ┬ . . . */
  499. 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00,
  500. 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00,
  501. /* 0x2530 - 0x253f: . . . . ┴ . . . . . . . ┼ . . . */
  502. 0x00, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00,
  503. 0x00, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00,
  504. /* 0x2540 - 0x254f: . . . . . . . . . . . . . . . . */
  505. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  506. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  507. /* 0x2550 - 0x255f: ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ */
  508. 0x0a, 0xa0, 0x12, 0x21, 0x22, 0x18, 0x24, 0x28,
  509. 0x42, 0x81, 0x82, 0x48, 0x84, 0x88, 0x52, 0xa1,
  510. /* 0x2560 - 0x256c: ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ */
  511. 0xa2, 0x58, 0xa4, 0xa8, 0x1a, 0x25, 0x2a, 0x4a,
  512. 0x85, 0x8a, 0x5a, 0xa5, 0xaa,
  513. };
  514. Display *dpy = dp->drv.p->dpy;
  515. Pixmap px = dp->drv.p->pixmap;
  516. GC gc = dp->drv.p->gc;
  517. int fw;
  518. XChar2b ch16;
  519. /* Underline */
  520. if(attr & CACA_UNDERLINE)
  521. XFillRectangle(dpy, px, gc, x, y + h - 1, w, 1);
  522. /* Skip spaces and magic stuff */
  523. if(ch <= 0x00000020)
  524. return;
  525. if(ch == CACA_MAGIC_FULLWIDTH)
  526. return;
  527. fw = w;
  528. if(caca_utf32_is_fullwidth(ch))
  529. fw *= 2;
  530. /* We want to be able to print a few special Unicode characters
  531. * such as the CP437 gradients and half blocks. For unknown
  532. * characters, print what caca_utf32_to_ascii() returns. */
  533. if(ch >= 0x2500 && ch <= 0x256c && udlr[ch - 0x2500])
  534. {
  535. uint16_t D = udlr[ch - 0x2500];
  536. if(D & 0x04)
  537. XFillRectangle(dpy, px, gc, x, y + h / 2, fw / 2 + 1, 1);
  538. if(D & 0x01)
  539. XFillRectangle(dpy, px, gc,
  540. x + fw / 2, y + h / 2, (fw + 1) / 2, 1);
  541. if(D & 0x40)
  542. XFillRectangle(dpy, px, gc, x + fw / 2, y, 1, h / 2 + 1);
  543. if(D & 0x10)
  544. XFillRectangle(dpy, px, gc, x + fw / 2, y + h / 2, 1, (h + 1) / 2);
  545. #define STEPIF(a,b) (D&(a)?-1:(D&(b))?1:0)
  546. if(D & 0x08)
  547. {
  548. XFillRectangle(dpy, px, gc, x, y - 1 + h / 2,
  549. fw / 2 + 1 + STEPIF(0xc0,0x20), 1);
  550. XFillRectangle(dpy, px, gc, x, y + 1 + h / 2,
  551. fw / 2 + 1 + STEPIF(0x30,0x80), 1);
  552. }
  553. if(D & 0x02)
  554. {
  555. XFillRectangle(dpy, px, gc, x - STEPIF(0xc0,0x20) + fw / 2,
  556. y - 1 + h / 2, (fw + 1) / 2 + STEPIF(0xc0,0x20), 1);
  557. XFillRectangle(dpy, px, gc, x - STEPIF(0x30,0x80) + fw / 2,
  558. y + 1 + h / 2, (fw + 1) / 2 + STEPIF(0x30,0x80), 1);
  559. }
  560. if(D & 0x80)
  561. {
  562. XFillRectangle(dpy, px, gc, x - 1 + fw / 2, y,
  563. 1, h / 2 + 1 + STEPIF(0x0c,0x02));
  564. XFillRectangle(dpy, px, gc, x + 1 + fw / 2, y,
  565. 1, h / 2 + 1 + STEPIF(0x03,0x08));
  566. }
  567. if(D & 0x20)
  568. {
  569. XFillRectangle(dpy, px, gc, x - 1 + fw / 2,
  570. y - STEPIF(0x0c,0x02) + h / 2,
  571. 1, (h + 1) / 2 + STEPIF(0x0c,0x02));
  572. XFillRectangle(dpy, px, gc, x + 1 + fw / 2,
  573. y - STEPIF(0x03,0x08) + h / 2,
  574. 1, (h + 1) / 2 + STEPIF(0x03,0x08));
  575. }
  576. return;
  577. }
  578. switch(ch)
  579. {
  580. case 0x000000b7: /* · */
  581. case 0x00002219: /* ∙ */
  582. case 0x000030fb: /* ・ */
  583. XFillRectangle(dpy, px, gc, x + fw / 2 - 1, y + h / 2 - 1, 2, 2);
  584. return;
  585. case 0x00002261: /* ≡ */
  586. XFillRectangle(dpy, px, gc, x + 1, y - 2 + h / 2, fw - 1, 1);
  587. XFillRectangle(dpy, px, gc, x + 1, y + h / 2, fw - 1, 1);
  588. XFillRectangle(dpy, px, gc, x + 1, y + 2 + h / 2, fw - 1, 1);
  589. return;
  590. case 0x00002580: /* ▀ */
  591. XFillRectangle(dpy, px, gc, x, y, fw, h / 2);
  592. return;
  593. case 0x00002584: /* ▄ */
  594. XFillRectangle(dpy, px, gc, x, y + h - h / 2, fw, h / 2);
  595. return;
  596. case 0x00002588: /* █ */
  597. case 0x000025ae: /* ▮ */
  598. XFillRectangle(dpy, px, gc, x, y, fw, h);
  599. return;
  600. case 0x0000258c: /* ▌ */
  601. XFillRectangle(dpy, px, gc, x, y, fw / 2, h);
  602. return;
  603. case 0x00002590: /* ▐ */
  604. XFillRectangle(dpy, px, gc, x + fw - fw / 2, y, fw / 2, h);
  605. return;
  606. case 0x000025a0: /* ■ */
  607. case 0x000025ac: /* ▬ */
  608. XFillRectangle(dpy, px, gc, x, y + h / 4, fw, h / 2);
  609. return;
  610. case 0x00002593: /* ▓ */
  611. case 0x00002592: /* ▒ */
  612. case 0x00002591: /* ░ */
  613. {
  614. /* FIXME: this sucks utterly */
  615. int i, j, k = ch - 0x00002591;
  616. for(j = h; j--; )
  617. for(i = fw; i--; )
  618. {
  619. if(((i + 2 * (j & 1)) & 3) > k)
  620. continue;
  621. XDrawPoint(dpy, px, gc, x + i, y + j);
  622. }
  623. return;
  624. }
  625. case 0x000025cb: /* ○ */
  626. case 0x00002022: /* • */
  627. case 0x000025cf: /* ● */
  628. {
  629. int d, xo, yo;
  630. d = fw >> (~ch & 0x1); /* XXX: hack */
  631. if(h < fw)
  632. d = h;
  633. if(d < 1)
  634. d = 1;
  635. xo = (fw - d) / 2;
  636. yo = (h - d) / 2;
  637. if(ch == 0x000025cb)
  638. XDrawArc(dpy, px, gc, x + xo, y + yo, d, d, 0, 64 * 360);
  639. else
  640. XFillArc(dpy, px, gc, x + xo, y + yo, d, d, 0, 64 * 360);
  641. return;
  642. }
  643. }
  644. if(ch >= 0x00000020 && ch <= dp->drv.p->max_char)
  645. {
  646. /* ascii, latin-1 or unicode font (might draw a blank square) */
  647. ch16.byte1 = (ch) >> 8;
  648. ch16.byte2 = (ch) & 0xff;
  649. }
  650. else
  651. {
  652. ch16.byte1 = 0;
  653. ch16.byte2 = caca_utf32_to_ascii(ch);
  654. }
  655. XDrawString16(dpy, px, gc, x + (ch16.byte1 ? 0 : (fw - w) / 2), yoff, &ch16, 1);
  656. }
  657. /*
  658. * Driver initialisation
  659. */
  660. int x11_install(caca_display_t *dp)
  661. {
  662. #if defined(HAVE_GETENV)
  663. if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
  664. return -1;
  665. #endif
  666. dp->drv.id = CACA_DRIVER_X11;
  667. dp->drv.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 */