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.
 
 
 
 
 
 

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