Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

953 righe
30 KiB

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