You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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