Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

542 lignes
18 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file driver_x11.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief X11 driver
  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 "caca.h"
  29. #include "caca_internals.h"
  30. #include "cucul.h"
  31. #include "cucul_internals.h"
  32. /*
  33. * Local functions
  34. */
  35. static int x11_error_handler(Display *, XErrorEvent *);
  36. struct driver_private
  37. {
  38. Display *dpy;
  39. Window window;
  40. Pixmap pixmap;
  41. GC gc;
  42. long int event_mask;
  43. int font_width, font_height;
  44. int colors[16];
  45. Font font;
  46. XFontStruct *font_struct;
  47. int font_offset;
  48. #if defined(HAVE_X11_XKBLIB_H)
  49. Bool autorepeat;
  50. #endif
  51. };
  52. static int x11_init_graphics(caca_t *kk)
  53. {
  54. static int const x11_palette[] =
  55. {
  56. /* Standard curses colours */
  57. 0x0, 0x0, 0x0,
  58. 0x0, 0x0, 0x8000,
  59. 0x0, 0x8000, 0x0,
  60. 0x0, 0x8000, 0x8000,
  61. 0x8000, 0x0, 0x0,
  62. 0x8000, 0x0, 0x8000,
  63. 0x8000, 0x8000, 0x0,
  64. 0x8000, 0x8000, 0x8000,
  65. /* Extra values for xterm-16color */
  66. 0x4000, 0x4000, 0x4000,
  67. 0x4000, 0x4000, 0xffff,
  68. 0x4000, 0xffff, 0x4000,
  69. 0x4000, 0xffff, 0xffff,
  70. 0xffff, 0x4000, 0x4000,
  71. 0xffff, 0x4000, 0xffff,
  72. 0xffff, 0xffff, 0x4000,
  73. 0xffff, 0xffff, 0xffff,
  74. };
  75. Colormap colormap;
  76. XSetWindowAttributes x11_winattr;
  77. int (*old_error_handler)(Display *, XErrorEvent *);
  78. char const *fonts[] = { NULL, "8x13bold", "fixed" }, **parser;
  79. char const *geometry;
  80. unsigned int width = 0, height = 0;
  81. int i;
  82. kk->drv.p = malloc(sizeof(struct driver_private));
  83. geometry = getenv("CACA_GEOMETRY");
  84. if(geometry && *geometry)
  85. sscanf(geometry, "%ux%u", &width, &height);
  86. if(width && height)
  87. _cucul_set_size(kk->qq, width, height);
  88. kk->drv.p->dpy = XOpenDisplay(NULL);
  89. if(kk->drv.p->dpy == NULL)
  90. return -1;
  91. fonts[0] = getenv("CACA_FONT");
  92. if(fonts[0] && *fonts[0])
  93. parser = fonts;
  94. else
  95. parser = fonts + 1;
  96. /* Ignore font errors */
  97. old_error_handler = XSetErrorHandler(x11_error_handler);
  98. /* Parse our font list */
  99. for( ; ; parser++)
  100. {
  101. if(!*parser)
  102. {
  103. XSetErrorHandler(old_error_handler);
  104. XCloseDisplay(kk->drv.p->dpy);
  105. return -1;
  106. }
  107. kk->drv.p->font = XLoadFont(kk->drv.p->dpy, *parser);
  108. if(!kk->drv.p->font)
  109. continue;
  110. kk->drv.p->font_struct = XQueryFont(kk->drv.p->dpy, kk->drv.p->font);
  111. if(!kk->drv.p->font_struct)
  112. {
  113. XUnloadFont(kk->drv.p->dpy, kk->drv.p->font);
  114. continue;
  115. }
  116. break;
  117. }
  118. /* Reset the default X11 error handler */
  119. XSetErrorHandler(old_error_handler);
  120. kk->drv.p->font_width = kk->drv.p->font_struct->max_bounds.width;
  121. kk->drv.p->font_height = kk->drv.p->font_struct->max_bounds.ascent
  122. + kk->drv.p->font_struct->max_bounds.descent;
  123. kk->drv.p->font_offset = kk->drv.p->font_struct->max_bounds.descent;
  124. colormap = DefaultColormap(kk->drv.p->dpy, DefaultScreen(kk->drv.p->dpy));
  125. for(i = 0; i < 16; i++)
  126. {
  127. XColor color;
  128. color.red = x11_palette[i * 3];
  129. color.green = x11_palette[i * 3 + 1];
  130. color.blue = x11_palette[i * 3 + 2];
  131. XAllocColor(kk->drv.p->dpy, colormap, &color);
  132. kk->drv.p->colors[i] = color.pixel;
  133. }
  134. x11_winattr.backing_store = Always;
  135. x11_winattr.background_pixel = kk->drv.p->colors[0];
  136. x11_winattr.event_mask = ExposureMask | StructureNotifyMask;
  137. kk->drv.p->window =
  138. XCreateWindow(kk->drv.p->dpy, DefaultRootWindow(kk->drv.p->dpy), 0, 0,
  139. kk->qq->width * kk->drv.p->font_width,
  140. kk->qq->height * kk->drv.p->font_height,
  141. 0, 0, InputOutput, 0,
  142. CWBackingStore | CWBackPixel | CWEventMask,
  143. &x11_winattr);
  144. XStoreName(kk->drv.p->dpy, kk->drv.p->window, "caca for X");
  145. XSelectInput(kk->drv.p->dpy, kk->drv.p->window, StructureNotifyMask);
  146. XMapWindow(kk->drv.p->dpy, kk->drv.p->window);
  147. kk->drv.p->gc = XCreateGC(kk->drv.p->dpy, kk->drv.p->window, 0, NULL);
  148. XSetForeground(kk->drv.p->dpy, kk->drv.p->gc, kk->drv.p->colors[15]);
  149. XSetFont(kk->drv.p->dpy, kk->drv.p->gc, kk->drv.p->font);
  150. for(;;)
  151. {
  152. XEvent xevent;
  153. XNextEvent(kk->drv.p->dpy, &xevent);
  154. if (xevent.type == MapNotify)
  155. break;
  156. }
  157. #if defined(HAVE_X11_XKBLIB_H)
  158. /* Disable autorepeat */
  159. XkbSetDetectableAutoRepeat(kk->drv.p->dpy, True, &kk->drv.p->autorepeat);
  160. if(!kk->drv.p->autorepeat)
  161. XAutoRepeatOff(kk->drv.p->dpy);
  162. #endif
  163. kk->drv.p->event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask
  164. | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask
  165. | ExposureMask;
  166. XSelectInput(kk->drv.p->dpy, kk->drv.p->window, kk->drv.p->event_mask);
  167. XSync(kk->drv.p->dpy, False);
  168. kk->drv.p->pixmap = XCreatePixmap(kk->drv.p->dpy, kk->drv.p->window,
  169. kk->qq->width * kk->drv.p->font_width,
  170. kk->qq->height * kk->drv.p->font_height,
  171. DefaultDepth(kk->drv.p->dpy,
  172. DefaultScreen(kk->drv.p->dpy)));
  173. return 0;
  174. }
  175. static int x11_end_graphics(caca_t *kk)
  176. {
  177. XSync(kk->drv.p->dpy, False);
  178. #if defined(HAVE_X11_XKBLIB_H)
  179. if(!kk->drv.p->autorepeat)
  180. XAutoRepeatOn(kk->drv.p->dpy);
  181. #endif
  182. XFreePixmap(kk->drv.p->dpy, kk->drv.p->pixmap);
  183. XFreeFont(kk->drv.p->dpy, kk->drv.p->font_struct);
  184. XFreeGC(kk->drv.p->dpy, kk->drv.p->gc);
  185. XUnmapWindow(kk->drv.p->dpy, kk->drv.p->window);
  186. XDestroyWindow(kk->drv.p->dpy, kk->drv.p->window);
  187. XCloseDisplay(kk->drv.p->dpy);
  188. free(kk->drv.p);
  189. return 0;
  190. }
  191. static int x11_set_window_title(caca_t *kk, char const *title)
  192. {
  193. XStoreName(kk->drv.p->dpy, kk->drv.p->window, title);
  194. return 0;
  195. }
  196. static unsigned int x11_get_window_width(caca_t *kk)
  197. {
  198. return kk->qq->width * kk->drv.p->font_width;
  199. }
  200. static unsigned int x11_get_window_height(caca_t *kk)
  201. {
  202. return kk->qq->height * kk->drv.p->font_height;
  203. }
  204. static void x11_display(caca_t *kk)
  205. {
  206. unsigned int x, y, len;
  207. /* First draw the background colours. Splitting the process in two
  208. * loops like this is actually slightly faster. */
  209. for(y = 0; y < kk->qq->height; y++)
  210. {
  211. for(x = 0; x < kk->qq->width; x += len)
  212. {
  213. uint8_t *attr = kk->qq->attr + x + y * kk->qq->width;
  214. len = 1;
  215. while(x + len < kk->qq->width
  216. && (attr[len] >> 4) == (attr[0] >> 4))
  217. len++;
  218. XSetForeground(kk->drv.p->dpy, kk->drv.p->gc,
  219. kk->drv.p->colors[attr[0] >> 4]);
  220. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->gc,
  221. x * kk->drv.p->font_width, y * kk->drv.p->font_height,
  222. len * kk->drv.p->font_width, kk->drv.p->font_height);
  223. }
  224. }
  225. /* Then print the foreground characters */
  226. for(y = 0; y < kk->qq->height; y++)
  227. {
  228. unsigned int yoff = (y + 1) * kk->drv.p->font_height
  229. - kk->drv.p->font_offset;
  230. uint32_t *chars = kk->qq->chars + y * kk->qq->width;
  231. for(x = 0; x < kk->qq->width; x++, chars++)
  232. {
  233. uint8_t *attr = kk->qq->attr + x + y * kk->qq->width;
  234. /* Skip spaces */
  235. if(*chars == 0x00000020)
  236. continue;
  237. XSetForeground(kk->drv.p->dpy, kk->drv.p->gc,
  238. kk->drv.p->colors[*attr & 0xf]);
  239. /* Plain ASCII, no problem. */
  240. if(*chars > 0x00000020 && *chars < 0x00000080)
  241. {
  242. char c = (uint8_t)*chars;
  243. XDrawString(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->gc,
  244. x * kk->drv.p->font_width, yoff, &c, 1);
  245. continue;
  246. }
  247. /* We want to be able to print a few special Unicode characters
  248. * such as the CP437 gradients and half blocks. For unknown
  249. * characters, just print '?'. */
  250. switch(*chars)
  251. {
  252. case 0x00002580: /* ▀ */
  253. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap,
  254. kk->drv.p->gc,
  255. x * kk->drv.p->font_width,
  256. y * kk->drv.p->font_height,
  257. kk->drv.p->font_width,
  258. kk->drv.p->font_height / 2);
  259. break;
  260. case 0x00002584: /* ▄ */
  261. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap,
  262. kk->drv.p->gc,
  263. x * kk->drv.p->font_width,
  264. (y + 1) * kk->drv.p->font_height
  265. - kk->drv.p->font_height / 2,
  266. kk->drv.p->font_width,
  267. kk->drv.p->font_height / 2);
  268. break;
  269. case 0x00002588: /* █ */
  270. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap,
  271. kk->drv.p->gc,
  272. x * kk->drv.p->font_width,
  273. y * kk->drv.p->font_height,
  274. kk->drv.p->font_width,
  275. kk->drv.p->font_height);
  276. break;
  277. case 0x0000258c: /* ▌ */
  278. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap,
  279. kk->drv.p->gc,
  280. x * kk->drv.p->font_width,
  281. y * kk->drv.p->font_height,
  282. kk->drv.p->font_width / 2,
  283. kk->drv.p->font_height);
  284. break;
  285. case 0x00002590: /* ▐ */
  286. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap,
  287. kk->drv.p->gc,
  288. (x + 1) * kk->drv.p->font_width
  289. - kk->drv.p->font_width / 2,
  290. y * kk->drv.p->font_height,
  291. kk->drv.p->font_width / 2,
  292. kk->drv.p->font_height);
  293. break;
  294. case 0x00002593: /* ▓ */
  295. case 0x00002592: /* ▒ */
  296. case 0x00002591: /* ░ */
  297. {
  298. /* FIXME: this sucks utterly */
  299. int i, j, k = *chars - 0x00002591;
  300. for(j = kk->drv.p->font_height; j--; )
  301. for(i = kk->drv.p->font_width; i--; )
  302. {
  303. if(((i + 2 * (j & 1)) & 3) > k)
  304. continue;
  305. XDrawPoint(kk->drv.p->dpy, kk->drv.p->pixmap,
  306. kk->drv.p->gc,
  307. x * kk->drv.p->font_width + i,
  308. y * kk->drv.p->font_height + j);
  309. }
  310. break;
  311. }
  312. default:
  313. {
  314. char c;
  315. c = '?';
  316. XDrawString(kk->drv.p->dpy, kk->drv.p->pixmap,
  317. kk->drv.p->gc,
  318. x * kk->drv.p->font_width, yoff, &c, 1);
  319. break;
  320. }
  321. }
  322. }
  323. }
  324. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->window,
  325. kk->drv.p->gc, 0, 0,
  326. kk->qq->width * kk->drv.p->font_width,
  327. kk->qq->height * kk->drv.p->font_height,
  328. 0, 0);
  329. XFlush(kk->drv.p->dpy);
  330. }
  331. static void x11_handle_resize(caca_t *kk)
  332. {
  333. Pixmap new_pixmap;
  334. new_pixmap = XCreatePixmap(kk->drv.p->dpy, kk->drv.p->window,
  335. kk->resize.w * kk->drv.p->font_width,
  336. kk->resize.h * kk->drv.p->font_height,
  337. DefaultDepth(kk->drv.p->dpy,
  338. DefaultScreen(kk->drv.p->dpy)));
  339. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, new_pixmap,
  340. kk->drv.p->gc, 0, 0,
  341. kk->resize.w * kk->drv.p->font_width,
  342. kk->resize.h * kk->drv.p->font_height, 0, 0);
  343. XFreePixmap(kk->drv.p->dpy, kk->drv.p->pixmap);
  344. kk->drv.p->pixmap = new_pixmap;
  345. }
  346. static unsigned int x11_get_event(caca_t *kk)
  347. {
  348. unsigned int event = 0;
  349. XEvent xevent;
  350. char key;
  351. while(XCheckWindowEvent(kk->drv.p->dpy, kk->drv.p->window,
  352. kk->drv.p->event_mask, &xevent) == True)
  353. {
  354. KeySym keysym;
  355. /* Expose event */
  356. if(xevent.type == Expose)
  357. {
  358. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap,
  359. kk->drv.p->window, kk->drv.p->gc, 0, 0,
  360. kk->qq->width * kk->drv.p->font_width,
  361. kk->qq->height * kk->drv.p->font_height, 0, 0);
  362. continue;
  363. }
  364. /* Resize event */
  365. if(xevent.type == ConfigureNotify)
  366. {
  367. unsigned int w, h;
  368. w = (xevent.xconfigure.width + kk->drv.p->font_width / 3)
  369. / kk->drv.p->font_width;
  370. h = (xevent.xconfigure.height + kk->drv.p->font_height / 3)
  371. / kk->drv.p->font_height;
  372. if(!w || !h || (w == kk->qq->width && h == kk->qq->height))
  373. continue;
  374. kk->resize.w = w;
  375. kk->resize.h = h;
  376. kk->resize.resized = 1;
  377. continue;
  378. }
  379. /* Check for mouse motion events */
  380. if(xevent.type == MotionNotify)
  381. {
  382. unsigned int newx = xevent.xmotion.x / kk->drv.p->font_width;
  383. unsigned int newy = xevent.xmotion.y / kk->drv.p->font_height;
  384. if(newx >= kk->qq->width)
  385. newx = kk->qq->width - 1;
  386. if(newy >= kk->qq->height)
  387. newy = kk->qq->height - 1;
  388. if(kk->mouse.x == newx && kk->mouse.y == newy)
  389. continue;
  390. kk->mouse.x = newx;
  391. kk->mouse.y = newy;
  392. return CACA_EVENT_MOUSE_MOTION | (kk->mouse.x << 12) | kk->mouse.y;
  393. }
  394. /* Check for mouse press and release events */
  395. if(xevent.type == ButtonPress)
  396. return CACA_EVENT_MOUSE_PRESS
  397. | ((XButtonEvent *)&xevent)->button;
  398. if(xevent.type == ButtonRelease)
  399. return CACA_EVENT_MOUSE_RELEASE
  400. | ((XButtonEvent *)&xevent)->button;
  401. /* Check for key press and release events */
  402. if(xevent.type == KeyPress)
  403. event |= CACA_EVENT_KEY_PRESS;
  404. else if(xevent.type == KeyRelease)
  405. event |= CACA_EVENT_KEY_RELEASE;
  406. else
  407. continue;
  408. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  409. return event | key;
  410. keysym = XKeycodeToKeysym(kk->drv.p->dpy, xevent.xkey.keycode, 0);
  411. switch(keysym)
  412. {
  413. case XK_F1: return event | CACA_KEY_F1;
  414. case XK_F2: return event | CACA_KEY_F2;
  415. case XK_F3: return event | CACA_KEY_F3;
  416. case XK_F4: return event | CACA_KEY_F4;
  417. case XK_F5: return event | CACA_KEY_F5;
  418. case XK_F6: return event | CACA_KEY_F6;
  419. case XK_F7: return event | CACA_KEY_F7;
  420. case XK_F8: return event | CACA_KEY_F8;
  421. case XK_F9: return event | CACA_KEY_F9;
  422. case XK_F10: return event | CACA_KEY_F10;
  423. case XK_F11: return event | CACA_KEY_F11;
  424. case XK_F12: return event | CACA_KEY_F12;
  425. case XK_F13: return event | CACA_KEY_F13;
  426. case XK_F14: return event | CACA_KEY_F14;
  427. case XK_F15: return event | CACA_KEY_F15;
  428. case XK_Left: return event | CACA_KEY_LEFT;
  429. case XK_Right: return event | CACA_KEY_RIGHT;
  430. case XK_Up: return event | CACA_KEY_UP;
  431. case XK_Down: return event | CACA_KEY_DOWN;
  432. default: return CACA_EVENT_NONE;
  433. }
  434. }
  435. return CACA_EVENT_NONE;
  436. }
  437. /*
  438. * XXX: following functions are local
  439. */
  440. static int x11_error_handler(Display *dpy, XErrorEvent *xevent)
  441. {
  442. /* Ignore the error */
  443. return 0;
  444. }
  445. /*
  446. * Driver initialisation
  447. */
  448. void x11_init_driver(caca_t *kk)
  449. {
  450. kk->drv.driver = CACA_DRIVER_X11;
  451. kk->drv.init_graphics = x11_init_graphics;
  452. kk->drv.end_graphics = x11_end_graphics;
  453. kk->drv.set_window_title = x11_set_window_title;
  454. kk->drv.get_window_width = x11_get_window_width;
  455. kk->drv.get_window_height = x11_get_window_height;
  456. kk->drv.display = x11_display;
  457. kk->drv.handle_resize = x11_handle_resize;
  458. kk->drv.get_event = x11_get_event;
  459. }
  460. #endif /* USE_X11 */