25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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