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. unsigned int new_width, new_height;
  45. int colors[16];
  46. Font font;
  47. XFontStruct *font_struct;
  48. int font_offset;
  49. #if defined(HAVE_X11_XKBLIB_H)
  50. Bool autorepeat;
  51. #endif
  52. };
  53. static int x11_init_graphics(caca_t *kk)
  54. {
  55. static int const x11_palette[] =
  56. {
  57. /* Standard curses colours */
  58. 0x0, 0x0, 0x0,
  59. 0x0, 0x0, 0x8000,
  60. 0x0, 0x8000, 0x0,
  61. 0x0, 0x8000, 0x8000,
  62. 0x8000, 0x0, 0x0,
  63. 0x8000, 0x0, 0x8000,
  64. 0x8000, 0x8000, 0x0,
  65. 0x8000, 0x8000, 0x8000,
  66. /* Extra values for xterm-16color */
  67. 0x4000, 0x4000, 0x4000,
  68. 0x4000, 0x4000, 0xffff,
  69. 0x4000, 0xffff, 0x4000,
  70. 0x4000, 0xffff, 0xffff,
  71. 0xffff, 0x4000, 0x4000,
  72. 0xffff, 0x4000, 0xffff,
  73. 0xffff, 0xffff, 0x4000,
  74. 0xffff, 0xffff, 0xffff,
  75. };
  76. Colormap colormap;
  77. XSetWindowAttributes x11_winattr;
  78. int (*old_error_handler)(Display *, XErrorEvent *);
  79. char const *fonts[] = { NULL, "8x13bold", "fixed" }, **parser;
  80. char const *geometry;
  81. unsigned int width = 0, height = 0;
  82. int i;
  83. kk->drv.p = malloc(sizeof(struct driver_private));
  84. geometry = getenv("CACA_GEOMETRY");
  85. if(geometry && *(geometry))
  86. sscanf(geometry, "%ux%u", &width, &height);
  87. if(width && height)
  88. cucul_set_size(kk->qq, width, height);
  89. kk->drv.p->dpy = XOpenDisplay(NULL);
  90. if(kk->drv.p->dpy == NULL)
  91. return -1;
  92. fonts[0] = getenv("CACA_FONT");
  93. if(fonts[0] && *fonts[0])
  94. parser = fonts;
  95. else
  96. parser = fonts + 1;
  97. /* Ignore font errors */
  98. old_error_handler = XSetErrorHandler(x11_error_handler);
  99. /* Parse our font list */
  100. for( ; ; parser++)
  101. {
  102. if(!*parser)
  103. {
  104. XSetErrorHandler(old_error_handler);
  105. XCloseDisplay(kk->drv.p->dpy);
  106. return -1;
  107. }
  108. kk->drv.p->font = XLoadFont(kk->drv.p->dpy, *parser);
  109. if(!kk->drv.p->font)
  110. continue;
  111. kk->drv.p->font_struct = XQueryFont(kk->drv.p->dpy, kk->drv.p->font);
  112. if(!kk->drv.p->font_struct)
  113. {
  114. XUnloadFont(kk->drv.p->dpy, kk->drv.p->font);
  115. continue;
  116. }
  117. break;
  118. }
  119. /* Reset the default X11 error handler */
  120. XSetErrorHandler(old_error_handler);
  121. kk->drv.p->font_width = kk->drv.p->font_struct->max_bounds.width;
  122. kk->drv.p->font_height = kk->drv.p->font_struct->max_bounds.ascent
  123. + kk->drv.p->font_struct->max_bounds.descent;
  124. kk->drv.p->font_offset = kk->drv.p->font_struct->max_bounds.descent;
  125. colormap = DefaultColormap(kk->drv.p->dpy, DefaultScreen(kk->drv.p->dpy));
  126. for(i = 0; i < 16; i++)
  127. {
  128. XColor color;
  129. color.red = x11_palette[i * 3];
  130. color.green = x11_palette[i * 3 + 1];
  131. color.blue = x11_palette[i * 3 + 2];
  132. XAllocColor(kk->drv.p->dpy, colormap, &color);
  133. kk->drv.p->colors[i] = color.pixel;
  134. }
  135. x11_winattr.backing_store = Always;
  136. x11_winattr.background_pixel = kk->drv.p->colors[0];
  137. x11_winattr.event_mask = ExposureMask | StructureNotifyMask;
  138. kk->drv.p->window =
  139. XCreateWindow(kk->drv.p->dpy, DefaultRootWindow(kk->drv.p->dpy), 0, 0,
  140. kk->qq->width * kk->drv.p->font_width,
  141. kk->qq->height * kk->drv.p->font_height,
  142. 0, 0, InputOutput, 0,
  143. CWBackingStore | CWBackPixel | CWEventMask,
  144. &x11_winattr);
  145. XStoreName(kk->drv.p->dpy, kk->drv.p->window, "caca for X");
  146. XSelectInput(kk->drv.p->dpy, kk->drv.p->window, StructureNotifyMask);
  147. XMapWindow(kk->drv.p->dpy, kk->drv.p->window);
  148. kk->drv.p->gc = XCreateGC(kk->drv.p->dpy, kk->drv.p->window, 0, NULL);
  149. XSetForeground(kk->drv.p->dpy, kk->drv.p->gc, kk->drv.p->colors[15]);
  150. XSetFont(kk->drv.p->dpy, kk->drv.p->gc, kk->drv.p->font);
  151. for(;;)
  152. {
  153. XEvent xevent;
  154. XNextEvent(kk->drv.p->dpy, &xevent);
  155. if (xevent.type == MapNotify)
  156. break;
  157. }
  158. #if defined(HAVE_X11_XKBLIB_H)
  159. /* Disable autorepeat */
  160. XkbSetDetectableAutoRepeat(kk->drv.p->dpy, True, &kk->drv.p->autorepeat);
  161. if(!kk->drv.p->autorepeat)
  162. XAutoRepeatOff(kk->drv.p->dpy);
  163. #endif
  164. kk->drv.p->event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask
  165. | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask
  166. | ExposureMask;
  167. XSelectInput(kk->drv.p->dpy, kk->drv.p->window, kk->drv.p->event_mask);
  168. XSync(kk->drv.p->dpy, False);
  169. kk->drv.p->pixmap = XCreatePixmap(kk->drv.p->dpy, kk->drv.p->window,
  170. kk->qq->width * kk->drv.p->font_width,
  171. kk->qq->height * kk->drv.p->font_height,
  172. DefaultDepth(kk->drv.p->dpy,
  173. DefaultScreen(kk->drv.p->dpy)));
  174. kk->drv.p->new_width = kk->drv.p->new_height = 0;
  175. return 0;
  176. }
  177. static int x11_end_graphics(caca_t *kk)
  178. {
  179. XSync(kk->drv.p->dpy, False);
  180. #if defined(HAVE_X11_XKBLIB_H)
  181. if(!kk->drv.p->autorepeat)
  182. XAutoRepeatOn(kk->drv.p->dpy);
  183. #endif
  184. XFreePixmap(kk->drv.p->dpy, kk->drv.p->pixmap);
  185. XFreeFont(kk->drv.p->dpy, kk->drv.p->font_struct);
  186. XFreeGC(kk->drv.p->dpy, kk->drv.p->gc);
  187. XUnmapWindow(kk->drv.p->dpy, kk->drv.p->window);
  188. XDestroyWindow(kk->drv.p->dpy, kk->drv.p->window);
  189. XCloseDisplay(kk->drv.p->dpy);
  190. free(kk->drv.p);
  191. return 0;
  192. }
  193. static int x11_set_window_title(caca_t *kk, char const *title)
  194. {
  195. XStoreName(kk->drv.p->dpy, kk->drv.p->window, title);
  196. return 0;
  197. }
  198. static unsigned int x11_get_window_width(caca_t *kk)
  199. {
  200. return kk->qq->width * kk->drv.p->font_width;
  201. }
  202. static unsigned int x11_get_window_height(caca_t *kk)
  203. {
  204. return kk->qq->height * kk->drv.p->font_height;
  205. }
  206. static void x11_display(caca_t *kk)
  207. {
  208. unsigned int x, y, len;
  209. /* First draw the background colours. Splitting the process in two
  210. * loops like this is actually slightly faster. */
  211. for(y = 0; y < kk->qq->height; y++)
  212. {
  213. for(x = 0; x < kk->qq->width; x += len)
  214. {
  215. uint8_t *attr = kk->qq->attr + x + y * kk->qq->width;
  216. len = 1;
  217. while(x + len < kk->qq->width
  218. && (attr[len] >> 4) == (attr[0] >> 4))
  219. len++;
  220. XSetForeground(kk->drv.p->dpy, kk->drv.p->gc,
  221. kk->drv.p->colors[attr[0] >> 4]);
  222. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->gc,
  223. x * kk->drv.p->font_width, y * kk->drv.p->font_height,
  224. len * kk->drv.p->font_width, kk->drv.p->font_height);
  225. }
  226. }
  227. /* Then print the foreground characters */
  228. for(y = 0; y < kk->qq->height; y++)
  229. {
  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] == ' ')
  238. continue;
  239. buffer[0] = chars[0] & 0x7f;
  240. while(x + len < kk->qq->width
  241. && (attr[len] & 0xf) == (attr[0] & 0xf))
  242. {
  243. buffer[len] = chars[len] & 0x7f;
  244. len++;
  245. }
  246. XSetForeground(kk->drv.p->dpy, kk->drv.p->gc, kk->drv.p->colors[attr[0] & 0xf]);
  247. XDrawString(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->gc,
  248. x * kk->drv.p->font_width,
  249. (y + 1) * kk->drv.p->font_height - kk->drv.p->font_offset,
  250. buffer, len);
  251. }
  252. }
  253. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->window, kk->drv.p->gc, 0, 0,
  254. kk->qq->width * kk->drv.p->font_width,
  255. kk->qq->height * kk->drv.p->font_height,
  256. 0, 0);
  257. XFlush(kk->drv.p->dpy);
  258. }
  259. static void x11_handle_resize(caca_t *kk, unsigned int *new_width,
  260. unsigned int *new_height)
  261. {
  262. Pixmap new_pixmap;
  263. *new_width = kk->drv.p->new_width;
  264. *new_height = kk->drv.p->new_height;
  265. new_pixmap = XCreatePixmap(kk->drv.p->dpy, kk->drv.p->window,
  266. kk->qq->width * kk->drv.p->font_width,
  267. kk->qq->height * kk->drv.p->font_height,
  268. DefaultDepth(kk->drv.p->dpy,
  269. DefaultScreen(kk->drv.p->dpy)));
  270. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, new_pixmap, kk->drv.p->gc, 0, 0,
  271. kk->qq->width * kk->drv.p->font_width,
  272. kk->qq->height * kk->drv.p->font_height, 0, 0);
  273. XFreePixmap(kk->drv.p->dpy, kk->drv.p->pixmap);
  274. kk->drv.p->pixmap = new_pixmap;
  275. }
  276. static unsigned int x11_get_event(caca_t *kk)
  277. {
  278. unsigned int event = 0;
  279. XEvent xevent;
  280. char key;
  281. while(XCheckWindowEvent(kk->drv.p->dpy, kk->drv.p->window,
  282. kk->drv.p->event_mask, &xevent) == True)
  283. {
  284. KeySym keysym;
  285. /* Expose event */
  286. if(xevent.type == Expose)
  287. {
  288. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap,
  289. kk->drv.p->window, kk->drv.p->gc, 0, 0,
  290. kk->qq->width * kk->drv.p->font_width,
  291. kk->qq->height * kk->drv.p->font_height, 0, 0);
  292. continue;
  293. }
  294. /* Resize event */
  295. if(xevent.type == ConfigureNotify)
  296. {
  297. unsigned int w, h;
  298. w = (xevent.xconfigure.width + kk->drv.p->font_width / 3)
  299. / kk->drv.p->font_width;
  300. h = (xevent.xconfigure.height + kk->drv.p->font_height / 3)
  301. / kk->drv.p->font_height;
  302. if(!w || !h || (w == kk->qq->width && h == kk->qq->height))
  303. continue;
  304. kk->drv.p->new_width = w;
  305. kk->drv.p->new_height = h;
  306. /* If we are already resizing, ignore the new signal */
  307. if(kk->resize)
  308. continue;
  309. kk->resize = 1;
  310. return CACA_EVENT_RESIZE;
  311. }
  312. /* Check for mouse motion events */
  313. if(xevent.type == MotionNotify)
  314. {
  315. unsigned int newx = xevent.xmotion.x / kk->drv.p->font_width;
  316. unsigned int newy = xevent.xmotion.y / kk->drv.p->font_height;
  317. if(newx >= kk->qq->width)
  318. newx = kk->qq->width - 1;
  319. if(newy >= kk->qq->height)
  320. newy = kk->qq->height - 1;
  321. if(kk->mouse.x == newx && kk->mouse.y == newy)
  322. continue;
  323. kk->mouse.x = newx;
  324. kk->mouse.y = newy;
  325. return CACA_EVENT_MOUSE_MOTION | (kk->mouse.x << 12) | kk->mouse.y;
  326. }
  327. /* Check for mouse press and release events */
  328. if(xevent.type == ButtonPress)
  329. return CACA_EVENT_MOUSE_PRESS
  330. | ((XButtonEvent *)&xevent)->button;
  331. if(xevent.type == ButtonRelease)
  332. return CACA_EVENT_MOUSE_RELEASE
  333. | ((XButtonEvent *)&xevent)->button;
  334. /* Check for key press and release events */
  335. if(xevent.type == KeyPress)
  336. event |= CACA_EVENT_KEY_PRESS;
  337. else if(xevent.type == KeyRelease)
  338. event |= CACA_EVENT_KEY_RELEASE;
  339. else
  340. continue;
  341. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  342. return event | key;
  343. keysym = XKeycodeToKeysym(kk->drv.p->dpy, xevent.xkey.keycode, 0);
  344. switch(keysym)
  345. {
  346. case XK_F1: return event | CACA_KEY_F1;
  347. case XK_F2: return event | CACA_KEY_F2;
  348. case XK_F3: return event | CACA_KEY_F3;
  349. case XK_F4: return event | CACA_KEY_F4;
  350. case XK_F5: return event | CACA_KEY_F5;
  351. case XK_F6: return event | CACA_KEY_F6;
  352. case XK_F7: return event | CACA_KEY_F7;
  353. case XK_F8: return event | CACA_KEY_F8;
  354. case XK_F9: return event | CACA_KEY_F9;
  355. case XK_F10: return event | CACA_KEY_F10;
  356. case XK_F11: return event | CACA_KEY_F11;
  357. case XK_F12: return event | CACA_KEY_F12;
  358. case XK_F13: return event | CACA_KEY_F13;
  359. case XK_F14: return event | CACA_KEY_F14;
  360. case XK_F15: return event | CACA_KEY_F15;
  361. case XK_Left: return event | CACA_KEY_LEFT;
  362. case XK_Right: return event | CACA_KEY_RIGHT;
  363. case XK_Up: return event | CACA_KEY_UP;
  364. case XK_Down: return event | CACA_KEY_DOWN;
  365. default: return CACA_EVENT_NONE;
  366. }
  367. }
  368. return CACA_EVENT_NONE;
  369. }
  370. /*
  371. * XXX: following functions are local
  372. */
  373. static int x11_error_handler(Display *dpy, XErrorEvent *xevent)
  374. {
  375. /* Ignore the error */
  376. return 0;
  377. }
  378. /*
  379. * Driver initialisation
  380. */
  381. void x11_init_driver(caca_t *kk)
  382. {
  383. kk->drv.driver = CACA_DRIVER_X11;
  384. kk->drv.init_graphics = x11_init_graphics;
  385. kk->drv.end_graphics = x11_end_graphics;
  386. kk->drv.set_window_title = x11_set_window_title;
  387. kk->drv.get_window_width = x11_get_window_width;
  388. kk->drv.get_window_height = x11_get_window_height;
  389. kk->drv.display = x11_display;
  390. kk->drv.handle_resize = x11_handle_resize;
  391. kk->drv.get_event = x11_get_event;
  392. }
  393. #endif /* USE_X11 */