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