Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

driver_x11.c 25 KiB

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