Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

562 rader
18 KiB

  1. /*
  2. * libcaca Colour 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. uint32_t *chars = kk->qq->chars + y * kk->qq->width;
  231. for(x = 0; x < kk->qq->width; x++, chars++)
  232. {
  233. uint8_t *attr = kk->qq->attr + x + y * kk->qq->width;
  234. /* Skip spaces */
  235. if(*chars == 0x00000020)
  236. continue;
  237. XSetForeground(kk->drv.p->dpy, kk->drv.p->gc,
  238. kk->drv.p->colors[*attr & 0xf]);
  239. /* Plain ASCII, no problem. */
  240. if(*chars > 0x00000020 && *chars < 0x00000080)
  241. {
  242. char c = (uint8_t)*chars;
  243. XDrawString(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->gc,
  244. x * kk->drv.p->font_width, yoff, &c, 1);
  245. continue;
  246. }
  247. /* We want to be able to print a few special Unicode characters
  248. * such as the CP437 gradients and half blocks. For unknown
  249. * characters, just print '?'. */
  250. switch(*chars)
  251. {
  252. case 0x00002580: /* ▀ */
  253. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap,
  254. kk->drv.p->gc,
  255. x * kk->drv.p->font_width,
  256. y * kk->drv.p->font_height,
  257. kk->drv.p->font_width,
  258. kk->drv.p->font_height / 2);
  259. break;
  260. case 0x00002584: /* ▄ */
  261. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap,
  262. kk->drv.p->gc,
  263. x * kk->drv.p->font_width,
  264. (y + 1) * kk->drv.p->font_height
  265. - kk->drv.p->font_height / 2,
  266. kk->drv.p->font_width,
  267. kk->drv.p->font_height / 2);
  268. break;
  269. case 0x00002588: /* █ */
  270. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap,
  271. kk->drv.p->gc,
  272. x * kk->drv.p->font_width,
  273. y * kk->drv.p->font_height,
  274. kk->drv.p->font_width,
  275. kk->drv.p->font_height);
  276. break;
  277. case 0x0000258c: /* ▌ */
  278. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap,
  279. kk->drv.p->gc,
  280. x * kk->drv.p->font_width,
  281. y * kk->drv.p->font_height,
  282. kk->drv.p->font_width / 2,
  283. kk->drv.p->font_height);
  284. break;
  285. case 0x00002590: /* ▐ */
  286. XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap,
  287. kk->drv.p->gc,
  288. (x + 1) * kk->drv.p->font_width
  289. - kk->drv.p->font_width / 2,
  290. y * kk->drv.p->font_height,
  291. kk->drv.p->font_width / 2,
  292. kk->drv.p->font_height);
  293. break;
  294. case 0x00002593: /* ▓ */
  295. case 0x00002592: /* ▒ */
  296. case 0x00002591: /* ░ */
  297. {
  298. /* FIXME: this sucks utterly */
  299. int i, j, k = *chars - 0x00002591;
  300. for(j = kk->drv.p->font_height; j--; )
  301. for(i = kk->drv.p->font_width; i--; )
  302. {
  303. if(((i + 2 * (j & 1)) & 3) > k)
  304. continue;
  305. XDrawPoint(kk->drv.p->dpy, kk->drv.p->pixmap,
  306. kk->drv.p->gc,
  307. x * kk->drv.p->font_width + i,
  308. y * kk->drv.p->font_height + j);
  309. }
  310. break;
  311. }
  312. default:
  313. {
  314. char c;
  315. c = '?';
  316. XDrawString(kk->drv.p->dpy, kk->drv.p->pixmap,
  317. kk->drv.p->gc,
  318. x * kk->drv.p->font_width, yoff, &c, 1);
  319. break;
  320. }
  321. }
  322. }
  323. }
  324. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->window,
  325. kk->drv.p->gc, 0, 0,
  326. kk->qq->width * kk->drv.p->font_width,
  327. kk->qq->height * kk->drv.p->font_height,
  328. 0, 0);
  329. XFlush(kk->drv.p->dpy);
  330. }
  331. static void x11_handle_resize(caca_t *kk)
  332. {
  333. Pixmap new_pixmap;
  334. new_pixmap = XCreatePixmap(kk->drv.p->dpy, kk->drv.p->window,
  335. kk->resize.w * kk->drv.p->font_width,
  336. kk->resize.h * kk->drv.p->font_height,
  337. DefaultDepth(kk->drv.p->dpy,
  338. DefaultScreen(kk->drv.p->dpy)));
  339. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, new_pixmap,
  340. kk->drv.p->gc, 0, 0,
  341. kk->resize.w * kk->drv.p->font_width,
  342. kk->resize.h * kk->drv.p->font_height, 0, 0);
  343. XFreePixmap(kk->drv.p->dpy, kk->drv.p->pixmap);
  344. kk->drv.p->pixmap = new_pixmap;
  345. }
  346. static int x11_get_event(caca_t *kk, struct caca_event *ev)
  347. {
  348. XEvent xevent;
  349. char key;
  350. while(XCheckWindowEvent(kk->drv.p->dpy, kk->drv.p->window,
  351. kk->drv.p->event_mask, &xevent) == True)
  352. {
  353. KeySym keysym;
  354. /* Expose event */
  355. if(xevent.type == Expose)
  356. {
  357. XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap,
  358. kk->drv.p->window, kk->drv.p->gc, 0, 0,
  359. kk->qq->width * kk->drv.p->font_width,
  360. kk->qq->height * kk->drv.p->font_height, 0, 0);
  361. continue;
  362. }
  363. /* Resize event */
  364. if(xevent.type == ConfigureNotify)
  365. {
  366. unsigned int w, h;
  367. w = (xevent.xconfigure.width + kk->drv.p->font_width / 3)
  368. / kk->drv.p->font_width;
  369. h = (xevent.xconfigure.height + kk->drv.p->font_height / 3)
  370. / kk->drv.p->font_height;
  371. if(!w || !h || (w == kk->qq->width && h == kk->qq->height))
  372. continue;
  373. kk->resize.w = w;
  374. kk->resize.h = h;
  375. kk->resize.resized = 1;
  376. continue;
  377. }
  378. /* Check for mouse motion events */
  379. if(xevent.type == MotionNotify)
  380. {
  381. unsigned int newx = xevent.xmotion.x / kk->drv.p->font_width;
  382. unsigned int newy = xevent.xmotion.y / kk->drv.p->font_height;
  383. if(newx >= kk->qq->width)
  384. newx = kk->qq->width - 1;
  385. if(newy >= kk->qq->height)
  386. newy = kk->qq->height - 1;
  387. if(kk->mouse.x == newx && kk->mouse.y == newy)
  388. continue;
  389. kk->mouse.x = newx;
  390. kk->mouse.y = newy;
  391. ev->type = CACA_EVENT_MOUSE_MOTION;
  392. ev->data.mouse.x = kk->mouse.x;
  393. ev->data.mouse.y = kk->mouse.y;
  394. return 1;
  395. }
  396. /* Check for mouse press and release events */
  397. if(xevent.type == ButtonPress)
  398. {
  399. ev->type = CACA_EVENT_MOUSE_PRESS;
  400. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  401. return 1;
  402. }
  403. if(xevent.type == ButtonRelease)
  404. {
  405. ev->type = CACA_EVENT_MOUSE_RELEASE;
  406. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  407. return 1;
  408. }
  409. /* Check for key press and release events */
  410. if(xevent.type == KeyPress)
  411. ev->type = CACA_EVENT_KEY_PRESS;
  412. else if(xevent.type == KeyRelease)
  413. ev->type = CACA_EVENT_KEY_RELEASE;
  414. else
  415. continue;
  416. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  417. {
  418. ev->data.key.c = key;
  419. ev->data.key.ucs4 = key;
  420. ev->data.key.utf8[0] = key;
  421. ev->data.key.utf8[1] = '\0';
  422. return 1;
  423. }
  424. keysym = XKeycodeToKeysym(kk->drv.p->dpy, xevent.xkey.keycode, 0);
  425. switch(keysym)
  426. {
  427. case XK_F1: ev->data.key.c = CACA_KEY_F1; break;
  428. case XK_F2: ev->data.key.c = CACA_KEY_F2; break;
  429. case XK_F3: ev->data.key.c = CACA_KEY_F3; break;
  430. case XK_F4: ev->data.key.c = CACA_KEY_F4; break;
  431. case XK_F5: ev->data.key.c = CACA_KEY_F5; break;
  432. case XK_F6: ev->data.key.c = CACA_KEY_F6; break;
  433. case XK_F7: ev->data.key.c = CACA_KEY_F7; break;
  434. case XK_F8: ev->data.key.c = CACA_KEY_F8; break;
  435. case XK_F9: ev->data.key.c = CACA_KEY_F9; break;
  436. case XK_F10: ev->data.key.c = CACA_KEY_F10; break;
  437. case XK_F11: ev->data.key.c = CACA_KEY_F11; break;
  438. case XK_F12: ev->data.key.c = CACA_KEY_F12; break;
  439. case XK_F13: ev->data.key.c = CACA_KEY_F13; break;
  440. case XK_F14: ev->data.key.c = CACA_KEY_F14; break;
  441. case XK_F15: ev->data.key.c = CACA_KEY_F15; break;
  442. case XK_Left: ev->data.key.c = CACA_KEY_LEFT; break;
  443. case XK_Right: ev->data.key.c = CACA_KEY_RIGHT; break;
  444. case XK_Up: ev->data.key.c = CACA_KEY_UP; break;
  445. case XK_Down: ev->data.key.c = CACA_KEY_DOWN; break;
  446. default: ev->type = CACA_EVENT_NONE; return 0;
  447. }
  448. ev->data.key.ucs4 = 0;
  449. ev->data.key.utf8[0] = '\0';
  450. return 1;
  451. }
  452. ev->type = CACA_EVENT_NONE;
  453. return 0;
  454. }
  455. /*
  456. * XXX: following functions are local
  457. */
  458. static int x11_error_handler(Display *dpy, XErrorEvent *xevent)
  459. {
  460. /* Ignore the error */
  461. return 0;
  462. }
  463. /*
  464. * Driver initialisation
  465. */
  466. void x11_init_driver(caca_t *kk)
  467. {
  468. kk->drv.driver = CACA_DRIVER_X11;
  469. kk->drv.init_graphics = x11_init_graphics;
  470. kk->drv.end_graphics = x11_end_graphics;
  471. kk->drv.set_window_title = x11_set_window_title;
  472. kk->drv.get_window_width = x11_get_window_width;
  473. kk->drv.get_window_height = x11_get_window_height;
  474. kk->drv.display = x11_display;
  475. kk->drv.handle_resize = x11_handle_resize;
  476. kk->drv.get_event = x11_get_event;
  477. }
  478. #endif /* USE_X11 */