Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

driver_x11.c 25 KiB

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