Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

343 wiersze
9.8 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. #if defined(HAVE_X11_XKBLIB_H)
  22. # include <X11/XKBlib.h>
  23. #endif
  24. #include <stdio.h> /* BUFSIZ */
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #if defined(HAVE_UNISTD_H)
  28. # include <unistd.h>
  29. #endif
  30. #include <stdarg.h>
  31. #include "caca.h"
  32. #include "caca_internals.h"
  33. #include "cucul.h"
  34. #include "cucul_internals.h"
  35. /*
  36. * Local functions
  37. */
  38. static int x11_error_handler(Display *, XErrorEvent *);
  39. static int x11_init_graphics(caca_t *kk)
  40. {
  41. static int const x11_palette[] =
  42. {
  43. /* Standard curses colours */
  44. 0x0, 0x0, 0x0,
  45. 0x0, 0x0, 0x8000,
  46. 0x0, 0x8000, 0x0,
  47. 0x0, 0x8000, 0x8000,
  48. 0x8000, 0x0, 0x0,
  49. 0x8000, 0x0, 0x8000,
  50. 0x8000, 0x8000, 0x0,
  51. 0x8000, 0x8000, 0x8000,
  52. /* Extra values for xterm-16color */
  53. 0x4000, 0x4000, 0x4000,
  54. 0x4000, 0x4000, 0xffff,
  55. 0x4000, 0xffff, 0x4000,
  56. 0x4000, 0xffff, 0xffff,
  57. 0xffff, 0x4000, 0x4000,
  58. 0xffff, 0x4000, 0xffff,
  59. 0xffff, 0xffff, 0x4000,
  60. 0xffff, 0xffff, 0xffff,
  61. };
  62. Colormap colormap;
  63. XSetWindowAttributes x11_winattr;
  64. int (*old_error_handler)(Display *, XErrorEvent *);
  65. char const *fonts[] = { NULL, "8x13bold", "fixed" }, **parser;
  66. char const *geometry;
  67. unsigned int width = 0, height = 0;
  68. int i;
  69. geometry = getenv("CACA_GEOMETRY");
  70. if(geometry && *(geometry))
  71. sscanf(geometry, "%ux%u", &width, &height);
  72. if(width && height)
  73. cucul_set_size(kk->qq, width, height);
  74. kk->x11.dpy = XOpenDisplay(NULL);
  75. if(kk->x11.dpy == NULL)
  76. return -1;
  77. fonts[0] = getenv("CACA_FONT");
  78. if(fonts[0] && *fonts[0])
  79. parser = fonts;
  80. else
  81. parser = fonts + 1;
  82. /* Ignore font errors */
  83. old_error_handler = XSetErrorHandler(x11_error_handler);
  84. /* Parse our font list */
  85. for( ; ; parser++)
  86. {
  87. if(!*parser)
  88. {
  89. XSetErrorHandler(old_error_handler);
  90. XCloseDisplay(kk->x11.dpy);
  91. return -1;
  92. }
  93. kk->x11.font = XLoadFont(kk->x11.dpy, *parser);
  94. if(!kk->x11.font)
  95. continue;
  96. kk->x11.font_struct = XQueryFont(kk->x11.dpy, kk->x11.font);
  97. if(!kk->x11.font_struct)
  98. {
  99. XUnloadFont(kk->x11.dpy, kk->x11.font);
  100. continue;
  101. }
  102. break;
  103. }
  104. /* Reset the default X11 error handler */
  105. XSetErrorHandler(old_error_handler);
  106. kk->x11.font_width = kk->x11.font_struct->max_bounds.width;
  107. kk->x11.font_height = kk->x11.font_struct->max_bounds.ascent
  108. + kk->x11.font_struct->max_bounds.descent;
  109. kk->x11.font_offset = kk->x11.font_struct->max_bounds.descent;
  110. colormap = DefaultColormap(kk->x11.dpy, DefaultScreen(kk->x11.dpy));
  111. for(i = 0; i < 16; i++)
  112. {
  113. XColor color;
  114. color.red = x11_palette[i * 3];
  115. color.green = x11_palette[i * 3 + 1];
  116. color.blue = x11_palette[i * 3 + 2];
  117. XAllocColor(kk->x11.dpy, colormap, &color);
  118. kk->x11.colors[i] = color.pixel;
  119. }
  120. x11_winattr.backing_store = Always;
  121. x11_winattr.background_pixel = kk->x11.colors[0];
  122. x11_winattr.event_mask = ExposureMask | StructureNotifyMask;
  123. kk->x11.window =
  124. XCreateWindow(kk->x11.dpy, DefaultRootWindow(kk->x11.dpy), 0, 0,
  125. kk->qq->width * kk->x11.font_width,
  126. kk->qq->height * kk->x11.font_height,
  127. 0, 0, InputOutput, 0,
  128. CWBackingStore | CWBackPixel | CWEventMask,
  129. &x11_winattr);
  130. XStoreName(kk->x11.dpy, kk->x11.window, "caca for X");
  131. XSelectInput(kk->x11.dpy, kk->x11.window, StructureNotifyMask);
  132. XMapWindow(kk->x11.dpy, kk->x11.window);
  133. kk->x11.gc = XCreateGC(kk->x11.dpy, kk->x11.window, 0, NULL);
  134. XSetForeground(kk->x11.dpy, kk->x11.gc, kk->x11.colors[15]);
  135. XSetFont(kk->x11.dpy, kk->x11.gc, kk->x11.font);
  136. for(;;)
  137. {
  138. XEvent event;
  139. XNextEvent(kk->x11.dpy, &event);
  140. if (event.type == MapNotify)
  141. break;
  142. }
  143. #if defined(HAVE_X11_XKBLIB_H)
  144. /* Disable autorepeat */
  145. XkbSetDetectableAutoRepeat(kk->x11.dpy, True, &kk->x11.autorepeat);
  146. if(!kk->x11.autorepeat)
  147. XAutoRepeatOff(kk->x11.dpy);
  148. #endif
  149. kk->x11.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask
  150. | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask
  151. | ExposureMask;
  152. XSelectInput(kk->x11.dpy, kk->x11.window, kk->x11.event_mask);
  153. XSync(kk->x11.dpy, False);
  154. kk->x11.pixmap = XCreatePixmap(kk->x11.dpy, kk->x11.window,
  155. kk->qq->width * kk->x11.font_width,
  156. kk->qq->height * kk->x11.font_height,
  157. DefaultDepth(kk->x11.dpy,
  158. DefaultScreen(kk->x11.dpy)));
  159. kk->x11.new_width = kk->x11.new_height = 0;
  160. return 0;
  161. }
  162. static int x11_end_graphics(caca_t *kk)
  163. {
  164. XSync(kk->x11.dpy, False);
  165. #if defined(HAVE_X11_XKBLIB_H)
  166. if(!kk->x11.autorepeat)
  167. XAutoRepeatOn(kk->x11.dpy);
  168. #endif
  169. XFreePixmap(kk->x11.dpy, kk->x11.pixmap);
  170. XFreeFont(kk->x11.dpy, kk->x11.font_struct);
  171. XFreeGC(kk->x11.dpy, kk->x11.gc);
  172. XUnmapWindow(kk->x11.dpy, kk->x11.window);
  173. XDestroyWindow(kk->x11.dpy, kk->x11.window);
  174. XCloseDisplay(kk->x11.dpy);
  175. return 0;
  176. }
  177. static int x11_set_window_title(caca_t *kk, char const *title)
  178. {
  179. XStoreName(kk->x11.dpy, kk->x11.window, title);
  180. return 0;
  181. }
  182. static unsigned int x11_get_window_width(caca_t *kk)
  183. {
  184. return kk->qq->width * kk->x11.font_width;
  185. }
  186. static unsigned int x11_get_window_height(caca_t *kk)
  187. {
  188. return kk->qq->height * kk->x11.font_height;
  189. }
  190. static void x11_display(caca_t *kk)
  191. {
  192. unsigned int x, y, len;
  193. /* First draw the background colours. Splitting the process in two
  194. * loops like this is actually slightly faster. */
  195. for(y = 0; y < kk->qq->height; y++)
  196. {
  197. for(x = 0; x < kk->qq->width; x += len)
  198. {
  199. uint8_t *attr = kk->qq->attr + x + y * kk->qq->width;
  200. len = 1;
  201. while(x + len < kk->qq->width
  202. && (attr[len] >> 4) == (attr[0] >> 4))
  203. len++;
  204. XSetForeground(kk->x11.dpy, kk->x11.gc,
  205. kk->x11.colors[attr[0] >> 4]);
  206. XFillRectangle(kk->x11.dpy, kk->x11.pixmap, kk->x11.gc,
  207. x * kk->x11.font_width, y * kk->x11.font_height,
  208. len * kk->x11.font_width, kk->x11.font_height);
  209. }
  210. }
  211. /* Then print the foreground characters */
  212. for(y = 0; y < kk->qq->height; y++)
  213. {
  214. for(x = 0; x < kk->qq->width; x += len)
  215. {
  216. char buffer[BUFSIZ]; /* FIXME: use a smaller buffer */
  217. uint32_t *chars = kk->qq->chars + x + y * kk->qq->width;
  218. uint8_t *attr = kk->qq->attr + x + y * kk->qq->width;
  219. len = 1;
  220. /* Skip spaces */
  221. if(chars[0] == ' ')
  222. continue;
  223. buffer[0] = chars[0] & 0x7f;
  224. while(x + len < kk->qq->width
  225. && (attr[len] & 0xf) == (attr[0] & 0xf))
  226. {
  227. buffer[len] = chars[len] & 0x7f;
  228. len++;
  229. }
  230. XSetForeground(kk->x11.dpy, kk->x11.gc, kk->x11.colors[attr[0] & 0xf]);
  231. XDrawString(kk->x11.dpy, kk->x11.pixmap, kk->x11.gc,
  232. x * kk->x11.font_width,
  233. (y + 1) * kk->x11.font_height - kk->x11.font_offset,
  234. buffer, len);
  235. }
  236. }
  237. XCopyArea(kk->x11.dpy, kk->x11.pixmap, kk->x11.window, kk->x11.gc, 0, 0,
  238. kk->qq->width * kk->x11.font_width,
  239. kk->qq->height * kk->x11.font_height,
  240. 0, 0);
  241. XFlush(kk->x11.dpy);
  242. }
  243. static void x11_handle_resize(caca_t *kk, unsigned int *new_width,
  244. unsigned int *new_height)
  245. {
  246. Pixmap new_pixmap;
  247. *new_width = kk->x11.new_width;
  248. *new_height = kk->x11.new_height;
  249. new_pixmap = XCreatePixmap(kk->x11.dpy, kk->x11.window,
  250. kk->qq->width * kk->x11.font_width,
  251. kk->qq->height * kk->x11.font_height,
  252. DefaultDepth(kk->x11.dpy,
  253. DefaultScreen(kk->x11.dpy)));
  254. XCopyArea(kk->x11.dpy, kk->x11.pixmap, new_pixmap, kk->x11.gc, 0, 0,
  255. kk->qq->width * kk->x11.font_width,
  256. kk->qq->height * kk->x11.font_height, 0, 0);
  257. XFreePixmap(kk->x11.dpy, kk->x11.pixmap);
  258. kk->x11.pixmap = new_pixmap;
  259. }
  260. /*
  261. * XXX: following functions are local
  262. */
  263. static int x11_error_handler(Display *dpy, XErrorEvent *event)
  264. {
  265. /* Ignore the error */
  266. return 0;
  267. }
  268. /*
  269. * Driver initialisation
  270. */
  271. void x11_init_driver(caca_t *kk)
  272. {
  273. kk->driver.driver = CACA_DRIVER_X11;
  274. kk->driver.init_graphics = x11_init_graphics;
  275. kk->driver.end_graphics = x11_end_graphics;
  276. kk->driver.set_window_title = x11_set_window_title;
  277. kk->driver.get_window_width = x11_get_window_width;
  278. kk->driver.get_window_height = x11_get_window_height;
  279. kk->driver.display = x11_display;
  280. kk->driver.handle_resize = x11_handle_resize;
  281. }
  282. #endif /* USE_X11 */