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 години
преди 15 години
преди 19 години
преди 15 години
преди 15 години
преди 15 години
преди 15 години
преди 15 години
преди 15 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2010 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. /* Expose event */
  450. if(xevent.type == Expose)
  451. {
  452. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap,
  453. dp->drv.p->window, dp->drv.p->gc, 0, 0,
  454. width * dp->drv.p->font_width,
  455. height * dp->drv.p->font_height, 0, 0);
  456. continue;
  457. }
  458. /* Resize event */
  459. if(xevent.type == ConfigureNotify)
  460. {
  461. int w, h;
  462. w = (xevent.xconfigure.width + dp->drv.p->font_width / 3)
  463. / dp->drv.p->font_width;
  464. h = (xevent.xconfigure.height + dp->drv.p->font_height / 3)
  465. / dp->drv.p->font_height;
  466. if(!w || !h || (w == width && h == height))
  467. continue;
  468. dp->resize.w = w;
  469. dp->resize.h = h;
  470. dp->resize.resized = 1;
  471. continue;
  472. }
  473. /* Check for mouse motion events */
  474. if(xevent.type == MotionNotify)
  475. {
  476. int newx = xevent.xmotion.x / dp->drv.p->font_width;
  477. int newy = xevent.xmotion.y / dp->drv.p->font_height;
  478. if(newx >= width)
  479. newx = width - 1;
  480. if(newy >= height)
  481. newy = height - 1;
  482. if(dp->mouse.x == newx && dp->mouse.y == newy)
  483. continue;
  484. dp->mouse.x = newx;
  485. dp->mouse.y = newy;
  486. ev->type = CACA_EVENT_MOUSE_MOTION;
  487. ev->data.mouse.x = dp->mouse.x;
  488. ev->data.mouse.y = dp->mouse.y;
  489. return 1;
  490. }
  491. /* Check for mouse press and release events */
  492. if(xevent.type == ButtonPress)
  493. {
  494. ev->type = CACA_EVENT_MOUSE_PRESS;
  495. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  496. return 1;
  497. }
  498. if(xevent.type == ButtonRelease)
  499. {
  500. ev->type = CACA_EVENT_MOUSE_RELEASE;
  501. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  502. return 1;
  503. }
  504. /* Check for key press and release events */
  505. if(xevent.type == KeyPress)
  506. ev->type = CACA_EVENT_KEY_PRESS;
  507. else if(xevent.type == KeyRelease)
  508. ev->type = CACA_EVENT_KEY_RELEASE;
  509. else
  510. continue;
  511. #if defined X_HAVE_UTF8_STRING
  512. if(Xutf8LookupString(dp->drv.p->ic, &xevent.xkey, ev->data.key.utf8, 8, NULL, NULL))
  513. {
  514. ev->data.key.utf32 = caca_utf8_to_utf32(ev->data.key.utf8, NULL);
  515. if(ev->data.key.utf32 <= 0x7f)
  516. {
  517. ev->data.key.ch = ev->data.key.utf32;
  518. } else {
  519. ev->data.key.ch = 0;
  520. }
  521. return 1;
  522. }
  523. #endif
  524. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  525. {
  526. ev->data.key.ch = key;
  527. ev->data.key.utf32 = key;
  528. ev->data.key.utf8[0] = key;
  529. ev->data.key.utf8[1] = '\0';
  530. return 1;
  531. }
  532. keysym = XKeycodeToKeysym(dp->drv.p->dpy, xevent.xkey.keycode, 0);
  533. switch(keysym)
  534. {
  535. case XK_F1: ev->data.key.ch = CACA_KEY_F1; break;
  536. case XK_F2: ev->data.key.ch = CACA_KEY_F2; break;
  537. case XK_F3: ev->data.key.ch = CACA_KEY_F3; break;
  538. case XK_F4: ev->data.key.ch = CACA_KEY_F4; break;
  539. case XK_F5: ev->data.key.ch = CACA_KEY_F5; break;
  540. case XK_F6: ev->data.key.ch = CACA_KEY_F6; break;
  541. case XK_F7: ev->data.key.ch = CACA_KEY_F7; break;
  542. case XK_F8: ev->data.key.ch = CACA_KEY_F8; break;
  543. case XK_F9: ev->data.key.ch = CACA_KEY_F9; break;
  544. case XK_F10: ev->data.key.ch = CACA_KEY_F10; break;
  545. case XK_F11: ev->data.key.ch = CACA_KEY_F11; break;
  546. case XK_F12: ev->data.key.ch = CACA_KEY_F12; break;
  547. case XK_F13: ev->data.key.ch = CACA_KEY_F13; break;
  548. case XK_F14: ev->data.key.ch = CACA_KEY_F14; break;
  549. case XK_F15: ev->data.key.ch = CACA_KEY_F15; break;
  550. case XK_Left: ev->data.key.ch = CACA_KEY_LEFT; break;
  551. case XK_Right: ev->data.key.ch = CACA_KEY_RIGHT; break;
  552. case XK_Up: ev->data.key.ch = CACA_KEY_UP; break;
  553. case XK_Down: ev->data.key.ch = CACA_KEY_DOWN; break;
  554. case XK_KP_Page_Up:
  555. case XK_Page_Up: ev->data.key.ch = CACA_KEY_PAGEUP; break;
  556. case XK_KP_Page_Down:
  557. case XK_Page_Down: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
  558. case XK_KP_Home:
  559. case XK_Home: ev->data.key.ch = CACA_KEY_HOME; break;
  560. case XK_KP_End:
  561. case XK_End: ev->data.key.ch = CACA_KEY_END; break;
  562. default: ev->type = CACA_EVENT_NONE; return 0;
  563. }
  564. ev->data.key.utf32 = 0;
  565. ev->data.key.utf8[0] = '\0';
  566. return 1;
  567. }
  568. while(XCheckTypedEvent(dp->drv.p->dpy, ClientMessage, &xevent))
  569. {
  570. if(xevent.xclient.message_type != dp->drv.p->wm_protocols)
  571. continue;
  572. if((Atom)xevent.xclient.data.l[0] == dp->drv.p->wm_delete_window)
  573. {
  574. ev->type = CACA_EVENT_QUIT;
  575. return 1;
  576. }
  577. }
  578. ev->type = CACA_EVENT_NONE;
  579. return 0;
  580. }
  581. static void x11_set_mouse(caca_display_t *dp, int flags)
  582. {
  583. Cursor no_ptr;
  584. Pixmap bm_no;
  585. XColor black, dummy;
  586. Colormap colormap;
  587. static char const empty[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  588. if(flags)
  589. {
  590. XDefineCursor(dp->drv.p->dpy,dp->drv.p->window, 0);
  591. return;
  592. }
  593. colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy));
  594. if(!XAllocNamedColor(dp->drv.p->dpy, colormap, "black", &black, &dummy))
  595. {
  596. return;
  597. }
  598. bm_no = XCreateBitmapFromData(dp->drv.p->dpy, dp->drv.p->window,
  599. empty, 8, 8);
  600. no_ptr = XCreatePixmapCursor(dp->drv.p->dpy, bm_no, bm_no,
  601. &black, &black, 0, 0);
  602. XDefineCursor(dp->drv.p->dpy, dp->drv.p->window, no_ptr);
  603. XFreeCursor(dp->drv.p->dpy, no_ptr);
  604. if(bm_no != None)
  605. XFreePixmap(dp->drv.p->dpy, bm_no);
  606. XFreeColors(dp->drv.p->dpy, colormap, &black.pixel, 1, 0);
  607. XSync(dp->drv.p->dpy, False);
  608. }
  609. static void x11_set_cursor(caca_display_t *dp, int flags)
  610. {
  611. dp->drv.p->cursor_flags = flags;
  612. }
  613. /*
  614. * XXX: following functions are local
  615. */
  616. static int x11_error_handler(Display *dpy, XErrorEvent *xevent)
  617. {
  618. /* Ignore the error */
  619. return 0;
  620. }
  621. static void x11_put_glyph(caca_display_t *dp, int x, int y, int yoff,
  622. int w, int h, uint32_t attr, uint32_t ch)
  623. {
  624. static uint8_t const udlr[] =
  625. {
  626. /* 0x2500 - 0x250f: ─ . │ . . . . . . . . . ┌ . . . */
  627. 0x05, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00,
  628. 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
  629. /* 0x2510 - 0x251f: ┐ . . . └ . . . ┘ . . . ├ . . . */
  630. 0x14, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00,
  631. 0x44, 0x00, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00,
  632. /* 0x2520 - 0x252f: . . . . ┤ . . . . . . . ┬ . . . */
  633. 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00,
  634. 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00,
  635. /* 0x2530 - 0x253f: . . . . ┴ . . . . . . . ┼ . . . */
  636. 0x00, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00,
  637. 0x00, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00,
  638. /* 0x2540 - 0x254f: . . . . . . . . . . . . . . . . */
  639. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  640. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  641. /* 0x2550 - 0x255f: ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ */
  642. 0x0a, 0xa0, 0x12, 0x21, 0x22, 0x18, 0x24, 0x28,
  643. 0x42, 0x81, 0x82, 0x48, 0x84, 0x88, 0x52, 0xa1,
  644. /* 0x2560 - 0x256c: ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ */
  645. 0xa2, 0x58, 0xa4, 0xa8, 0x1a, 0x25, 0x2a, 0x4a,
  646. 0x85, 0x8a, 0x5a, 0xa5, 0xaa,
  647. };
  648. Display *dpy = dp->drv.p->dpy;
  649. Pixmap px = dp->drv.p->pixmap;
  650. GC gc = dp->drv.p->gc;
  651. int fw;
  652. /* Underline */
  653. if(attr & CACA_UNDERLINE)
  654. XFillRectangle(dpy, px, gc, x, y + h - 1, w, 1);
  655. /* Skip spaces and magic stuff */
  656. if(ch <= 0x00000020)
  657. return;
  658. if(ch == CACA_MAGIC_FULLWIDTH)
  659. return;
  660. fw = w;
  661. if(caca_utf32_is_fullwidth(ch))
  662. fw *= 2;
  663. /* We want to be able to print a few special Unicode characters
  664. * such as the CP437 gradients and half blocks. For unknown
  665. * characters, print what caca_utf32_to_ascii() returns. */
  666. if(ch >= 0x2500 && ch <= 0x256c && udlr[ch - 0x2500])
  667. {
  668. uint16_t D = udlr[ch - 0x2500];
  669. if(D & 0x04)
  670. XFillRectangle(dpy, px, gc, x, y + h / 2, fw / 2 + 1, 1);
  671. if(D & 0x01)
  672. XFillRectangle(dpy, px, gc,
  673. x + fw / 2, y + h / 2, (fw + 1) / 2, 1);
  674. if(D & 0x40)
  675. XFillRectangle(dpy, px, gc, x + fw / 2, y, 1, h / 2 + 1);
  676. if(D & 0x10)
  677. XFillRectangle(dpy, px, gc, x + fw / 2, y + h / 2, 1, (h + 1) / 2);
  678. #define STEPIF(a,b) (D&(a)?-1:(D&(b))?1:0)
  679. if(D & 0x08)
  680. {
  681. XFillRectangle(dpy, px, gc, x, y - 1 + h / 2,
  682. fw / 2 + 1 + STEPIF(0xc0,0x20), 1);
  683. XFillRectangle(dpy, px, gc, x, y + 1 + h / 2,
  684. fw / 2 + 1 + STEPIF(0x30,0x80), 1);
  685. }
  686. if(D & 0x02)
  687. {
  688. XFillRectangle(dpy, px, gc, x - STEPIF(0xc0,0x20) + fw / 2,
  689. y - 1 + h / 2, (fw + 1) / 2 + STEPIF(0xc0,0x20), 1);
  690. XFillRectangle(dpy, px, gc, x - STEPIF(0x30,0x80) + fw / 2,
  691. y + 1 + h / 2, (fw + 1) / 2 + STEPIF(0x30,0x80), 1);
  692. }
  693. if(D & 0x80)
  694. {
  695. XFillRectangle(dpy, px, gc, x - 1 + fw / 2, y,
  696. 1, h / 2 + 1 + STEPIF(0x0c,0x02));
  697. XFillRectangle(dpy, px, gc, x + 1 + fw / 2, y,
  698. 1, h / 2 + 1 + STEPIF(0x03,0x08));
  699. }
  700. if(D & 0x20)
  701. {
  702. XFillRectangle(dpy, px, gc, x - 1 + fw / 2,
  703. y - STEPIF(0x0c,0x02) + h / 2,
  704. 1, (h + 1) / 2 + STEPIF(0x0c,0x02));
  705. XFillRectangle(dpy, px, gc, x + 1 + fw / 2,
  706. y - STEPIF(0x03,0x08) + h / 2,
  707. 1, (h + 1) / 2 + STEPIF(0x03,0x08));
  708. }
  709. return;
  710. }
  711. switch(ch)
  712. {
  713. case 0x000000b7: /* · */
  714. case 0x00002219: /* ∙ */
  715. case 0x000030fb: /* ・ */
  716. XFillRectangle(dpy, px, gc, x + fw / 2 - 1, y + h / 2 - 1, 2, 2);
  717. return;
  718. case 0x00002261: /* ≡ */
  719. XFillRectangle(dpy, px, gc, x + 1, y - 2 + h / 2, fw - 1, 1);
  720. XFillRectangle(dpy, px, gc, x + 1, y + h / 2, fw - 1, 1);
  721. XFillRectangle(dpy, px, gc, x + 1, y + 2 + h / 2, fw - 1, 1);
  722. return;
  723. case 0x00002580: /* ▀ */
  724. XFillRectangle(dpy, px, gc, x, y, fw, h / 2);
  725. return;
  726. case 0x00002584: /* ▄ */
  727. XFillRectangle(dpy, px, gc, x, y + h - h / 2, fw, h / 2);
  728. return;
  729. case 0x00002588: /* █ */
  730. case 0x000025ae: /* ▮ */
  731. XFillRectangle(dpy, px, gc, x, y, fw, h);
  732. return;
  733. case 0x0000258c: /* ▌ */
  734. XFillRectangle(dpy, px, gc, x, y, fw / 2, h);
  735. return;
  736. case 0x00002590: /* ▐ */
  737. XFillRectangle(dpy, px, gc, x + fw - fw / 2, y, fw / 2, h);
  738. return;
  739. case 0x000025a0: /* ■ */
  740. case 0x000025ac: /* ▬ */
  741. XFillRectangle(dpy, px, gc, x, y + h / 4, fw, h / 2);
  742. return;
  743. case 0x00002593: /* ▓ */
  744. case 0x00002592: /* ▒ */
  745. case 0x00002591: /* ░ */
  746. {
  747. /* FIXME: this sucks utterly */
  748. int i, j, k = ch - 0x00002591;
  749. for(j = h; j--; )
  750. for(i = fw; i--; )
  751. {
  752. if(((i + 2 * (j & 1)) & 3) > k)
  753. continue;
  754. XDrawPoint(dpy, px, gc, x + i, y + j);
  755. }
  756. return;
  757. }
  758. case 0x000025cb: /* ○ */
  759. case 0x00002022: /* • */
  760. case 0x000025cf: /* ● */
  761. {
  762. int d, xo, yo;
  763. d = fw >> (~ch & 0x1); /* XXX: hack */
  764. if(h < fw)
  765. d = h;
  766. if(d < 1)
  767. d = 1;
  768. xo = (fw - d) / 2;
  769. yo = (h - d) / 2;
  770. if(ch == 0x000025cb)
  771. XDrawArc(dpy, px, gc, x + xo, y + yo, d, d, 0, 64 * 360);
  772. else
  773. XFillArc(dpy, px, gc, x + xo, y + yo, d, d, 0, 64 * 360);
  774. return;
  775. }
  776. }
  777. #if defined X_HAVE_UTF8_STRING
  778. if (dp->drv.p->font_set)
  779. {
  780. wchar_t wch = ch;
  781. XwcDrawString(dpy, px, dp->drv.p->font_set, gc, x, yoff, &wch, 1);
  782. }
  783. else
  784. #endif
  785. {
  786. XChar2b ch16;
  787. #if !defined X_HAVE_UTF8_STRING
  788. if(ch > dp->drv.p->max_char)
  789. {
  790. ch16.byte1 = 0;
  791. ch16.byte2 = caca_utf32_to_ascii(ch);
  792. }
  793. else
  794. #endif
  795. {
  796. ch16.byte1 = (uint8_t)(ch >> 8);
  797. ch16.byte2 = (uint8_t)ch;
  798. }
  799. XDrawString16(dpy, px, gc, x, yoff, &ch16, 1);
  800. }
  801. }
  802. /*
  803. * Driver initialisation
  804. */
  805. int x11_install(caca_display_t *dp)
  806. {
  807. #if defined HAVE_GETENV
  808. if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
  809. return -1;
  810. #endif
  811. dp->drv.id = CACA_DRIVER_X11;
  812. dp->drv.driver = "x11";
  813. dp->drv.init_graphics = x11_init_graphics;
  814. dp->drv.end_graphics = x11_end_graphics;
  815. dp->drv.set_display_title = x11_set_display_title;
  816. dp->drv.get_display_width = x11_get_display_width;
  817. dp->drv.get_display_height = x11_get_display_height;
  818. dp->drv.display = x11_display;
  819. dp->drv.handle_resize = x11_handle_resize;
  820. dp->drv.get_event = x11_get_event;
  821. dp->drv.set_mouse = x11_set_mouse;
  822. dp->drv.set_cursor = x11_set_cursor;
  823. return 0;
  824. }
  825. #endif /* USE_X11 */