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.

driver_x11.c 14 KiB

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