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.
 
 
 
 
 
 

869 líneas
28 KiB

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