You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

654 lines
22 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains the libcaca X11 input and output driver
  16. */
  17. #include "config.h"
  18. #include "common.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[4096];
  45. Font font;
  46. XFontStruct *font_struct;
  47. int font_offset;
  48. Cursor pointer;
  49. Atom wm_protocols;
  50. Atom wm_delete_window;
  51. #if defined(HAVE_X11_XKBLIB_H)
  52. Bool autorepeat;
  53. #endif
  54. };
  55. static int x11_init_graphics(caca_display_t *dp)
  56. {
  57. Colormap colormap;
  58. XSetWindowAttributes x11_winattr;
  59. int (*old_error_handler)(Display *, XErrorEvent *);
  60. char const *fonts[] = { NULL, "8x13bold", "fixed" }, **parser;
  61. char const *geometry;
  62. unsigned int width = dp->cv->width, height = dp->cv->height;
  63. int i;
  64. dp->drv.p = malloc(sizeof(struct driver_private));
  65. #if defined(HAVE_GETENV)
  66. geometry = getenv("CACA_GEOMETRY");
  67. if(geometry && *geometry)
  68. sscanf(geometry, "%ux%u", &width, &height);
  69. #endif
  70. _cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32);
  71. dp->drv.p->dpy = XOpenDisplay(NULL);
  72. if(dp->drv.p->dpy == NULL)
  73. return -1;
  74. #if defined(HAVE_GETENV)
  75. fonts[0] = getenv("CACA_FONT");
  76. if(fonts[0] && *fonts[0])
  77. parser = fonts;
  78. else
  79. #endif
  80. parser = fonts + 1;
  81. /* Ignore font errors */
  82. old_error_handler = XSetErrorHandler(x11_error_handler);
  83. /* Parse our font list */
  84. for( ; ; parser++)
  85. {
  86. if(!*parser)
  87. {
  88. XSetErrorHandler(old_error_handler);
  89. XCloseDisplay(dp->drv.p->dpy);
  90. return -1;
  91. }
  92. dp->drv.p->font = XLoadFont(dp->drv.p->dpy, *parser);
  93. if(!dp->drv.p->font)
  94. continue;
  95. dp->drv.p->font_struct = XQueryFont(dp->drv.p->dpy, dp->drv.p->font);
  96. if(!dp->drv.p->font_struct)
  97. {
  98. XUnloadFont(dp->drv.p->dpy, dp->drv.p->font);
  99. continue;
  100. }
  101. break;
  102. }
  103. /* Reset the default X11 error handler */
  104. XSetErrorHandler(old_error_handler);
  105. dp->drv.p->font_width = dp->drv.p->font_struct->max_bounds.width;
  106. dp->drv.p->font_height = dp->drv.p->font_struct->max_bounds.ascent
  107. + dp->drv.p->font_struct->max_bounds.descent;
  108. dp->drv.p->font_offset = dp->drv.p->font_struct->max_bounds.descent;
  109. colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy));
  110. for(i = 0x000; i < 0x1000; i++)
  111. {
  112. XColor color;
  113. color.red = ((i & 0xf00) >> 8) * 0x1111;
  114. color.green = ((i & 0x0f0) >> 4) * 0x1111;
  115. color.blue = (i & 0x00f) * 0x1111;
  116. XAllocColor(dp->drv.p->dpy, colormap, &color);
  117. dp->drv.p->colors[i] = color.pixel;
  118. }
  119. x11_winattr.backing_store = Always;
  120. x11_winattr.background_pixel = dp->drv.p->colors[0x000];
  121. x11_winattr.event_mask = ExposureMask | StructureNotifyMask;
  122. dp->drv.p->window =
  123. XCreateWindow(dp->drv.p->dpy, DefaultRootWindow(dp->drv.p->dpy), 0, 0,
  124. dp->cv->width * dp->drv.p->font_width,
  125. dp->cv->height * dp->drv.p->font_height,
  126. 0, 0, InputOutput, 0,
  127. CWBackingStore | CWBackPixel | CWEventMask,
  128. &x11_winattr);
  129. dp->drv.p->wm_protocols =
  130. XInternAtom(dp->drv.p->dpy, "WM_PROTOCOLS", True);
  131. dp->drv.p->wm_delete_window =
  132. XInternAtom(dp->drv.p->dpy, "WM_DELETE_WINDOW", True);
  133. if(dp->drv.p->wm_protocols != None && dp->drv.p->wm_delete_window != None)
  134. XSetWMProtocols(dp->drv.p->dpy, dp->drv.p->window,
  135. &dp->drv.p->wm_delete_window, 1);
  136. XStoreName(dp->drv.p->dpy, dp->drv.p->window, "caca for X");
  137. XSelectInput(dp->drv.p->dpy, dp->drv.p->window, StructureNotifyMask);
  138. XMapWindow(dp->drv.p->dpy, dp->drv.p->window);
  139. dp->drv.p->gc = XCreateGC(dp->drv.p->dpy, dp->drv.p->window, 0, NULL);
  140. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, dp->drv.p->colors[0x888]);
  141. XSetFont(dp->drv.p->dpy, dp->drv.p->gc, dp->drv.p->font);
  142. for(;;)
  143. {
  144. XEvent xevent;
  145. XNextEvent(dp->drv.p->dpy, &xevent);
  146. if (xevent.type == MapNotify)
  147. break;
  148. }
  149. #if defined(HAVE_X11_XKBLIB_H)
  150. /* Disable autorepeat */
  151. XkbSetDetectableAutoRepeat(dp->drv.p->dpy, True, &dp->drv.p->autorepeat);
  152. if(!dp->drv.p->autorepeat)
  153. XAutoRepeatOff(dp->drv.p->dpy);
  154. #endif
  155. dp->drv.p->event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask
  156. | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask
  157. | ExposureMask;
  158. XSelectInput(dp->drv.p->dpy, dp->drv.p->window, dp->drv.p->event_mask);
  159. XSync(dp->drv.p->dpy, False);
  160. dp->drv.p->pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window,
  161. dp->cv->width * dp->drv.p->font_width,
  162. dp->cv->height * dp->drv.p->font_height,
  163. DefaultDepth(dp->drv.p->dpy,
  164. DefaultScreen(dp->drv.p->dpy)));
  165. dp->drv.p->pointer = None;
  166. return 0;
  167. }
  168. static int x11_end_graphics(caca_display_t *dp)
  169. {
  170. XSync(dp->drv.p->dpy, False);
  171. #if defined(HAVE_X11_XKBLIB_H)
  172. if(!dp->drv.p->autorepeat)
  173. XAutoRepeatOn(dp->drv.p->dpy);
  174. #endif
  175. XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap);
  176. XFreeFont(dp->drv.p->dpy, dp->drv.p->font_struct);
  177. XFreeGC(dp->drv.p->dpy, dp->drv.p->gc);
  178. XUnmapWindow(dp->drv.p->dpy, dp->drv.p->window);
  179. XDestroyWindow(dp->drv.p->dpy, dp->drv.p->window);
  180. XCloseDisplay(dp->drv.p->dpy);
  181. free(dp->drv.p);
  182. return 0;
  183. }
  184. static int x11_set_display_title(caca_display_t *dp, char const *title)
  185. {
  186. XStoreName(dp->drv.p->dpy, dp->drv.p->window, title);
  187. return 0;
  188. }
  189. static unsigned int x11_get_display_width(caca_display_t *dp)
  190. {
  191. return dp->cv->width * dp->drv.p->font_width;
  192. }
  193. static unsigned int x11_get_display_height(caca_display_t *dp)
  194. {
  195. return dp->cv->height * dp->drv.p->font_height;
  196. }
  197. static void x11_display(caca_display_t *dp)
  198. {
  199. unsigned int x, y, len;
  200. /* First draw the background colours. Splitting the process in two
  201. * loops like this is actually slightly faster. */
  202. for(y = 0; y < dp->cv->height; y++)
  203. {
  204. for(x = 0; x < dp->cv->width; x += len)
  205. {
  206. uint32_t *attrs = dp->cv->attrs + x + y * dp->cv->width;
  207. uint16_t bg = _cucul_attr_to_rgb12bg(*attrs);
  208. len = 1;
  209. while(x + len < dp->cv->width
  210. && _cucul_attr_to_rgb12bg(attrs[len]) == bg)
  211. len++;
  212. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc,
  213. dp->drv.p->colors[bg]);
  214. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc,
  215. x * dp->drv.p->font_width, y * dp->drv.p->font_height,
  216. len * dp->drv.p->font_width, dp->drv.p->font_height);
  217. }
  218. }
  219. /* Then print the foreground characters */
  220. for(y = 0; y < dp->cv->height; y++)
  221. {
  222. unsigned int yoff = (y + 1) * dp->drv.p->font_height
  223. - dp->drv.p->font_offset;
  224. uint32_t *chars = dp->cv->chars + y * dp->cv->width;
  225. for(x = 0; x < dp->cv->width; x++, chars++)
  226. {
  227. uint32_t *attrs = dp->cv->attrs + x + y * dp->cv->width;
  228. /* Underline */
  229. if(*attrs & CUCUL_UNDERLINE)
  230. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  231. dp->drv.p->gc,
  232. x * dp->drv.p->font_width,
  233. (y + 1) * dp->drv.p->font_height - 1,
  234. dp->drv.p->font_width, 1);
  235. /* Skip spaces */
  236. if(*chars <= 0x00000020)
  237. continue;
  238. if(*chars == CUCUL_MAGIC_FULLWIDTH)
  239. continue;
  240. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc,
  241. dp->drv.p->colors[_cucul_attr_to_rgb12fg(*attrs)]);
  242. /* Plain ASCII, no problem. */
  243. if(*chars > 0x00000020 && *chars < 0x00000080)
  244. {
  245. char ch = (uint8_t)*chars;
  246. XDrawString(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc,
  247. x * dp->drv.p->font_width, yoff, &ch, 1);
  248. continue;
  249. }
  250. /* We want to be able to print a few special Unicode characters
  251. * such as the CP437 gradients and half blocks. For unknown
  252. * characters, just print '?'. */
  253. switch(*chars)
  254. {
  255. case 0x000000b7: /* · */
  256. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  257. dp->drv.p->gc,
  258. x * dp->drv.p->font_width
  259. + dp->drv.p->font_width / 2,
  260. y * dp->drv.p->font_height
  261. + dp->drv.p->font_height / 2, 2, 2);
  262. break;
  263. case 0x00002500: /* ─ */
  264. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  265. dp->drv.p->gc,
  266. x * dp->drv.p->font_width,
  267. y * dp->drv.p->font_height
  268. + dp->drv.p->font_height / 2 + 1,
  269. dp->drv.p->font_width, 1);
  270. break;
  271. case 0x00002580: /* ▀ */
  272. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  273. dp->drv.p->gc,
  274. x * dp->drv.p->font_width,
  275. y * dp->drv.p->font_height,
  276. dp->drv.p->font_width,
  277. dp->drv.p->font_height / 2);
  278. break;
  279. case 0x00002584: /* ▄ */
  280. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  281. dp->drv.p->gc,
  282. x * dp->drv.p->font_width,
  283. (y + 1) * dp->drv.p->font_height
  284. - dp->drv.p->font_height / 2,
  285. dp->drv.p->font_width,
  286. dp->drv.p->font_height / 2);
  287. break;
  288. case 0x00002588: /* █ */
  289. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  290. dp->drv.p->gc,
  291. x * dp->drv.p->font_width,
  292. y * dp->drv.p->font_height,
  293. dp->drv.p->font_width,
  294. dp->drv.p->font_height);
  295. break;
  296. case 0x0000258c: /* ▌ */
  297. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  298. dp->drv.p->gc,
  299. x * dp->drv.p->font_width,
  300. y * dp->drv.p->font_height,
  301. dp->drv.p->font_width / 2,
  302. dp->drv.p->font_height);
  303. break;
  304. case 0x00002590: /* ▐ */
  305. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  306. dp->drv.p->gc,
  307. (x + 1) * dp->drv.p->font_width
  308. - dp->drv.p->font_width / 2,
  309. y * dp->drv.p->font_height,
  310. dp->drv.p->font_width / 2,
  311. dp->drv.p->font_height);
  312. break;
  313. case 0x000025a0: /* ■ */
  314. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  315. dp->drv.p->gc,
  316. x * dp->drv.p->font_width,
  317. y * dp->drv.p->font_height
  318. + dp->drv.p->font_height / 4,
  319. dp->drv.p->font_width,
  320. dp->drv.p->font_height / 2);
  321. break;
  322. case 0x00002593: /* ▓ */
  323. case 0x00002592: /* ▒ */
  324. case 0x00002591: /* ░ */
  325. {
  326. /* FIXME: this sucks utterly */
  327. int i, j, k = *chars - 0x00002591;
  328. for(j = dp->drv.p->font_height; j--; )
  329. for(i = dp->drv.p->font_width; i--; )
  330. {
  331. if(((i + 2 * (j & 1)) & 3) > k)
  332. continue;
  333. XDrawPoint(dp->drv.p->dpy, dp->drv.p->pixmap,
  334. dp->drv.p->gc,
  335. x * dp->drv.p->font_width + i,
  336. y * dp->drv.p->font_height + j);
  337. }
  338. break;
  339. }
  340. default:
  341. {
  342. char ch;
  343. ch = '?';
  344. XDrawString(dp->drv.p->dpy, dp->drv.p->pixmap,
  345. dp->drv.p->gc,
  346. x * dp->drv.p->font_width, yoff, &ch, 1);
  347. break;
  348. }
  349. }
  350. }
  351. }
  352. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->window,
  353. dp->drv.p->gc, 0, 0,
  354. dp->cv->width * dp->drv.p->font_width,
  355. dp->cv->height * dp->drv.p->font_height,
  356. 0, 0);
  357. XFlush(dp->drv.p->dpy);
  358. }
  359. static void x11_handle_resize(caca_display_t *dp)
  360. {
  361. Pixmap new_pixmap;
  362. new_pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window,
  363. dp->resize.w * dp->drv.p->font_width,
  364. dp->resize.h * dp->drv.p->font_height,
  365. DefaultDepth(dp->drv.p->dpy,
  366. DefaultScreen(dp->drv.p->dpy)));
  367. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, new_pixmap,
  368. dp->drv.p->gc, 0, 0,
  369. dp->resize.w * dp->drv.p->font_width,
  370. dp->resize.h * dp->drv.p->font_height, 0, 0);
  371. XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap);
  372. dp->drv.p->pixmap = new_pixmap;
  373. }
  374. static int x11_get_event(caca_display_t *dp, caca_event_t *ev)
  375. {
  376. XEvent xevent;
  377. char key;
  378. while(XCheckWindowEvent(dp->drv.p->dpy, dp->drv.p->window,
  379. dp->drv.p->event_mask, &xevent) == True)
  380. {
  381. KeySym keysym;
  382. /* Expose event */
  383. if(xevent.type == Expose)
  384. {
  385. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap,
  386. dp->drv.p->window, dp->drv.p->gc, 0, 0,
  387. dp->cv->width * dp->drv.p->font_width,
  388. dp->cv->height * dp->drv.p->font_height, 0, 0);
  389. continue;
  390. }
  391. /* Resize event */
  392. if(xevent.type == ConfigureNotify)
  393. {
  394. unsigned int w, h;
  395. w = (xevent.xconfigure.width + dp->drv.p->font_width / 3)
  396. / dp->drv.p->font_width;
  397. h = (xevent.xconfigure.height + dp->drv.p->font_height / 3)
  398. / dp->drv.p->font_height;
  399. if(!w || !h || (w == dp->cv->width && h == dp->cv->height))
  400. continue;
  401. dp->resize.w = w;
  402. dp->resize.h = h;
  403. dp->resize.resized = 1;
  404. continue;
  405. }
  406. /* Check for mouse motion events */
  407. if(xevent.type == MotionNotify)
  408. {
  409. unsigned int newx = xevent.xmotion.x / dp->drv.p->font_width;
  410. unsigned int newy = xevent.xmotion.y / dp->drv.p->font_height;
  411. if(newx >= dp->cv->width)
  412. newx = dp->cv->width - 1;
  413. if(newy >= dp->cv->height)
  414. newy = dp->cv->height - 1;
  415. if(dp->mouse.x == newx && dp->mouse.y == newy)
  416. continue;
  417. dp->mouse.x = newx;
  418. dp->mouse.y = newy;
  419. ev->type = CACA_EVENT_MOUSE_MOTION;
  420. ev->data.mouse.x = dp->mouse.x;
  421. ev->data.mouse.y = dp->mouse.y;
  422. return 1;
  423. }
  424. /* Check for mouse press and release events */
  425. if(xevent.type == ButtonPress)
  426. {
  427. ev->type = CACA_EVENT_MOUSE_PRESS;
  428. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  429. return 1;
  430. }
  431. if(xevent.type == ButtonRelease)
  432. {
  433. ev->type = CACA_EVENT_MOUSE_RELEASE;
  434. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  435. return 1;
  436. }
  437. /* Check for key press and release events */
  438. if(xevent.type == KeyPress)
  439. ev->type = CACA_EVENT_KEY_PRESS;
  440. else if(xevent.type == KeyRelease)
  441. ev->type = CACA_EVENT_KEY_RELEASE;
  442. else
  443. continue;
  444. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  445. {
  446. ev->data.key.ch = key;
  447. ev->data.key.utf32 = key;
  448. ev->data.key.utf8[0] = key;
  449. ev->data.key.utf8[1] = '\0';
  450. return 1;
  451. }
  452. keysym = XKeycodeToKeysym(dp->drv.p->dpy, xevent.xkey.keycode, 0);
  453. switch(keysym)
  454. {
  455. case XK_F1: ev->data.key.ch = CACA_KEY_F1; break;
  456. case XK_F2: ev->data.key.ch = CACA_KEY_F2; break;
  457. case XK_F3: ev->data.key.ch = CACA_KEY_F3; break;
  458. case XK_F4: ev->data.key.ch = CACA_KEY_F4; break;
  459. case XK_F5: ev->data.key.ch = CACA_KEY_F5; break;
  460. case XK_F6: ev->data.key.ch = CACA_KEY_F6; break;
  461. case XK_F7: ev->data.key.ch = CACA_KEY_F7; break;
  462. case XK_F8: ev->data.key.ch = CACA_KEY_F8; break;
  463. case XK_F9: ev->data.key.ch = CACA_KEY_F9; break;
  464. case XK_F10: ev->data.key.ch = CACA_KEY_F10; break;
  465. case XK_F11: ev->data.key.ch = CACA_KEY_F11; break;
  466. case XK_F12: ev->data.key.ch = CACA_KEY_F12; break;
  467. case XK_F13: ev->data.key.ch = CACA_KEY_F13; break;
  468. case XK_F14: ev->data.key.ch = CACA_KEY_F14; break;
  469. case XK_F15: ev->data.key.ch = CACA_KEY_F15; break;
  470. case XK_Left: ev->data.key.ch = CACA_KEY_LEFT; break;
  471. case XK_Right: ev->data.key.ch = CACA_KEY_RIGHT; break;
  472. case XK_Up: ev->data.key.ch = CACA_KEY_UP; break;
  473. case XK_Down: ev->data.key.ch = CACA_KEY_DOWN; break;
  474. case XK_KP_Page_Up:
  475. case XK_Page_Up: ev->data.key.ch = CACA_KEY_PAGEUP; break;
  476. case XK_KP_Page_Down:
  477. case XK_Page_Down: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
  478. case XK_KP_Home:
  479. case XK_Home: ev->data.key.ch = CACA_KEY_HOME; break;
  480. case XK_KP_End:
  481. case XK_End: ev->data.key.ch = CACA_KEY_END; break;
  482. default: ev->type = CACA_EVENT_NONE; return 0;
  483. }
  484. ev->data.key.utf32 = 0;
  485. ev->data.key.utf8[0] = '\0';
  486. return 1;
  487. }
  488. while(XCheckTypedEvent(dp->drv.p->dpy, ClientMessage, &xevent))
  489. {
  490. if(xevent.xclient.message_type != dp->drv.p->wm_protocols)
  491. continue;
  492. if((Atom)xevent.xclient.data.l[0] == dp->drv.p->wm_delete_window)
  493. {
  494. ev->type = CACA_EVENT_QUIT;
  495. return 1;
  496. }
  497. }
  498. ev->type = CACA_EVENT_NONE;
  499. return 0;
  500. }
  501. static void x11_set_mouse(caca_display_t *dp, int flags)
  502. {
  503. Cursor no_ptr;
  504. Pixmap bm_no;
  505. XColor black, dummy;
  506. Colormap colormap;
  507. static char const empty[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  508. if(flags)
  509. {
  510. XDefineCursor(dp->drv.p->dpy,dp->drv.p->window, 0);
  511. return;
  512. }
  513. colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy));
  514. if(!XAllocNamedColor(dp->drv.p->dpy, colormap, "black", &black, &dummy))
  515. {
  516. return;
  517. }
  518. bm_no = XCreateBitmapFromData(dp->drv.p->dpy, dp->drv.p->window,
  519. empty, 8, 8);
  520. no_ptr = XCreatePixmapCursor(dp->drv.p->dpy, bm_no, bm_no,
  521. &black, &black, 0, 0);
  522. XDefineCursor(dp->drv.p->dpy, dp->drv.p->window, no_ptr);
  523. XFreeCursor(dp->drv.p->dpy, no_ptr);
  524. if(bm_no != None)
  525. XFreePixmap(dp->drv.p->dpy, bm_no);
  526. XFreeColors(dp->drv.p->dpy, colormap, &black.pixel, 1, 0);
  527. XSync(dp->drv.p->dpy, False);
  528. }
  529. /*
  530. * XXX: following functions are local
  531. */
  532. static int x11_error_handler(Display *dpy, XErrorEvent *xevent)
  533. {
  534. /* Ignore the error */
  535. return 0;
  536. }
  537. /*
  538. * Driver initialisation
  539. */
  540. int x11_install(caca_display_t *dp)
  541. {
  542. #if defined(HAVE_GETENV)
  543. if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
  544. return -1;
  545. #endif
  546. dp->drv.driver = CACA_DRIVER_X11;
  547. dp->drv.init_graphics = x11_init_graphics;
  548. dp->drv.end_graphics = x11_end_graphics;
  549. dp->drv.set_display_title = x11_set_display_title;
  550. dp->drv.get_display_width = x11_get_display_width;
  551. dp->drv.get_display_height = x11_get_display_height;
  552. dp->drv.display = x11_display;
  553. dp->drv.handle_resize = x11_handle_resize;
  554. dp->drv.get_event = x11_get_event;
  555. dp->drv.set_mouse = x11_set_mouse;
  556. dp->drv.set_cursor = NULL;
  557. return 0;
  558. }
  559. #endif /* USE_X11 */