No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

467 líneas
14 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. for(x = 0; x < kk->qq->width; x += len)
  229. {
  230. char buffer[BUFSIZ]; /* FIXME: use a smaller buffer */
  231. uint32_t *chars = kk->qq->chars + x + y * kk->qq->width;
  232. uint8_t *attr = kk->qq->attr + x + y * kk->qq->width;
  233. len = 1;
  234. /* Skip spaces */
  235. if(chars[0] == ' ')
  236. continue;
  237. buffer[0] = chars[0] & 0x7f;
  238. while(x + len < kk->qq->width
  239. && (attr[len] & 0xf) == (attr[0] & 0xf))
  240. {
  241. buffer[len] = chars[len] & 0x7f;
  242. len++;
  243. }
  244. XSetForeground(kk->drv.p->dpy, kk->drv.p->gc, kk->drv.p->colors[attr[0] & 0xf]);
  245. XDrawString(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->gc,
  246. x * kk->drv.p->font_width,
  247. (y + 1) * kk->drv.p->font_height - kk->drv.p->font_offset,
  248. buffer, len);
  249. }
  250. }
  251. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->window, kk->drv.p->gc, 0, 0,
  252. kk->qq->width * kk->drv.p->font_width,
  253. kk->qq->height * kk->drv.p->font_height,
  254. 0, 0);
  255. XFlush(kk->drv.p->dpy);
  256. }
  257. static void x11_handle_resize(caca_t *kk)
  258. {
  259. Pixmap new_pixmap;
  260. new_pixmap = XCreatePixmap(kk->drv.p->dpy, kk->drv.p->window,
  261. kk->resize.w * kk->drv.p->font_width,
  262. kk->resize.h * kk->drv.p->font_height,
  263. DefaultDepth(kk->drv.p->dpy,
  264. DefaultScreen(kk->drv.p->dpy)));
  265. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, new_pixmap,
  266. kk->drv.p->gc, 0, 0,
  267. kk->resize.w * kk->drv.p->font_width,
  268. kk->resize.h * kk->drv.p->font_height, 0, 0);
  269. XFreePixmap(kk->drv.p->dpy, kk->drv.p->pixmap);
  270. kk->drv.p->pixmap = new_pixmap;
  271. }
  272. static unsigned int x11_get_event(caca_t *kk)
  273. {
  274. unsigned int event = 0;
  275. XEvent xevent;
  276. char key;
  277. while(XCheckWindowEvent(kk->drv.p->dpy, kk->drv.p->window,
  278. kk->drv.p->event_mask, &xevent) == True)
  279. {
  280. KeySym keysym;
  281. /* Expose event */
  282. if(xevent.type == Expose)
  283. {
  284. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap,
  285. kk->drv.p->window, kk->drv.p->gc, 0, 0,
  286. kk->qq->width * kk->drv.p->font_width,
  287. kk->qq->height * kk->drv.p->font_height, 0, 0);
  288. continue;
  289. }
  290. /* Resize event */
  291. if(xevent.type == ConfigureNotify)
  292. {
  293. unsigned int w, h;
  294. w = (xevent.xconfigure.width + kk->drv.p->font_width / 3)
  295. / kk->drv.p->font_width;
  296. h = (xevent.xconfigure.height + kk->drv.p->font_height / 3)
  297. / kk->drv.p->font_height;
  298. if(!w || !h || (w == kk->qq->width && h == kk->qq->height))
  299. continue;
  300. kk->resize.w = w;
  301. kk->resize.h = h;
  302. kk->resize.resized = 1;
  303. continue;
  304. }
  305. /* Check for mouse motion events */
  306. if(xevent.type == MotionNotify)
  307. {
  308. unsigned int newx = xevent.xmotion.x / kk->drv.p->font_width;
  309. unsigned int newy = xevent.xmotion.y / kk->drv.p->font_height;
  310. if(newx >= kk->qq->width)
  311. newx = kk->qq->width - 1;
  312. if(newy >= kk->qq->height)
  313. newy = kk->qq->height - 1;
  314. if(kk->mouse.x == newx && kk->mouse.y == newy)
  315. continue;
  316. kk->mouse.x = newx;
  317. kk->mouse.y = newy;
  318. return CACA_EVENT_MOUSE_MOTION | (kk->mouse.x << 12) | kk->mouse.y;
  319. }
  320. /* Check for mouse press and release events */
  321. if(xevent.type == ButtonPress)
  322. return CACA_EVENT_MOUSE_PRESS
  323. | ((XButtonEvent *)&xevent)->button;
  324. if(xevent.type == ButtonRelease)
  325. return CACA_EVENT_MOUSE_RELEASE
  326. | ((XButtonEvent *)&xevent)->button;
  327. /* Check for key press and release events */
  328. if(xevent.type == KeyPress)
  329. event |= CACA_EVENT_KEY_PRESS;
  330. else if(xevent.type == KeyRelease)
  331. event |= CACA_EVENT_KEY_RELEASE;
  332. else
  333. continue;
  334. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  335. return event | key;
  336. keysym = XKeycodeToKeysym(kk->drv.p->dpy, xevent.xkey.keycode, 0);
  337. switch(keysym)
  338. {
  339. case XK_F1: return event | CACA_KEY_F1;
  340. case XK_F2: return event | CACA_KEY_F2;
  341. case XK_F3: return event | CACA_KEY_F3;
  342. case XK_F4: return event | CACA_KEY_F4;
  343. case XK_F5: return event | CACA_KEY_F5;
  344. case XK_F6: return event | CACA_KEY_F6;
  345. case XK_F7: return event | CACA_KEY_F7;
  346. case XK_F8: return event | CACA_KEY_F8;
  347. case XK_F9: return event | CACA_KEY_F9;
  348. case XK_F10: return event | CACA_KEY_F10;
  349. case XK_F11: return event | CACA_KEY_F11;
  350. case XK_F12: return event | CACA_KEY_F12;
  351. case XK_F13: return event | CACA_KEY_F13;
  352. case XK_F14: return event | CACA_KEY_F14;
  353. case XK_F15: return event | CACA_KEY_F15;
  354. case XK_Left: return event | CACA_KEY_LEFT;
  355. case XK_Right: return event | CACA_KEY_RIGHT;
  356. case XK_Up: return event | CACA_KEY_UP;
  357. case XK_Down: return event | CACA_KEY_DOWN;
  358. default: return CACA_EVENT_NONE;
  359. }
  360. }
  361. return CACA_EVENT_NONE;
  362. }
  363. /*
  364. * XXX: following functions are local
  365. */
  366. static int x11_error_handler(Display *dpy, XErrorEvent *xevent)
  367. {
  368. /* Ignore the error */
  369. return 0;
  370. }
  371. /*
  372. * Driver initialisation
  373. */
  374. void x11_init_driver(caca_t *kk)
  375. {
  376. kk->drv.driver = CACA_DRIVER_X11;
  377. kk->drv.init_graphics = x11_init_graphics;
  378. kk->drv.end_graphics = x11_end_graphics;
  379. kk->drv.set_window_title = x11_set_window_title;
  380. kk->drv.get_window_width = x11_get_window_width;
  381. kk->drv.get_window_height = x11_get_window_height;
  382. kk->drv.display = x11_display;
  383. kk->drv.handle_resize = x11_handle_resize;
  384. kk->drv.get_event = x11_get_event;
  385. }
  386. #endif /* USE_X11 */