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.
 
 
 
 
 
 

645 rivejä
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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains the libcaca X11 input and output driver
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if defined(USE_X11)
  19. #include <X11/Xlib.h>
  20. #include <X11/Xutil.h>
  21. #include <X11/keysym.h>
  22. #if defined(HAVE_X11_XKBLIB_H)
  23. # include <X11/XKBlib.h>
  24. #endif
  25. #include <stdio.h> /* BUFSIZ */
  26. #include <stdlib.h>
  27. #include "caca.h"
  28. #include "caca_internals.h"
  29. #include "cucul.h"
  30. #include "cucul_internals.h"
  31. /*
  32. * Local functions
  33. */
  34. static int x11_error_handler(Display *, XErrorEvent *);
  35. struct driver_private
  36. {
  37. Display *dpy;
  38. Window window;
  39. Pixmap pixmap;
  40. GC gc;
  41. long int event_mask;
  42. int font_width, font_height;
  43. int colors[4096];
  44. Font font;
  45. XFontStruct *font_struct;
  46. int font_offset;
  47. Cursor pointer;
  48. Atom wm_protocols;
  49. Atom wm_delete_window;
  50. #if defined(HAVE_X11_XKBLIB_H)
  51. Bool autorepeat;
  52. #endif
  53. };
  54. static int x11_init_graphics(caca_display_t *dp)
  55. {
  56. Colormap colormap;
  57. XSetWindowAttributes x11_winattr;
  58. int (*old_error_handler)(Display *, XErrorEvent *);
  59. char const *fonts[] = { NULL, "8x13bold", "fixed" }, **parser;
  60. char const *geometry;
  61. unsigned int width = 0, height = 0;
  62. int i;
  63. dp->drv.p = malloc(sizeof(struct driver_private));
  64. #if defined(HAVE_GETENV)
  65. geometry = getenv("CACA_GEOMETRY");
  66. if(geometry && *geometry)
  67. sscanf(geometry, "%ux%u", &width, &height);
  68. #endif
  69. if(width && height)
  70. _cucul_set_canvas_size(dp->cv, width, height);
  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 *attr = dp->cv->attr + x + y * dp->cv->width;
  207. uint16_t bg = _cucul_argb32_to_rgb12bg(*attr);
  208. len = 1;
  209. while(x + len < dp->cv->width
  210. && _cucul_argb32_to_rgb12bg(attr[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 *attr = dp->cv->attr + x + y * dp->cv->width;
  228. /* Skip spaces */
  229. if(*chars <= 0x00000020)
  230. continue;
  231. if(*chars == CUCUL_MAGIC_FULLWIDTH)
  232. continue;
  233. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc,
  234. dp->drv.p->colors[_cucul_argb32_to_rgb12fg(*attr)]);
  235. /* Plain ASCII, no problem. */
  236. if(*chars > 0x00000020 && *chars < 0x00000080)
  237. {
  238. char ch = (uint8_t)*chars;
  239. XDrawString(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc,
  240. x * dp->drv.p->font_width, yoff, &ch, 1);
  241. continue;
  242. }
  243. /* We want to be able to print a few special Unicode characters
  244. * such as the CP437 gradients and half blocks. For unknown
  245. * characters, just print '?'. */
  246. switch(*chars)
  247. {
  248. case 0x000000b7: /* · */
  249. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  250. dp->drv.p->gc,
  251. x * dp->drv.p->font_width
  252. + dp->drv.p->font_width / 2,
  253. y * dp->drv.p->font_height
  254. + dp->drv.p->font_height / 2, 2, 2);
  255. break;
  256. case 0x00002500: /* ─ */
  257. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  258. dp->drv.p->gc,
  259. x * dp->drv.p->font_width,
  260. y * dp->drv.p->font_height
  261. + dp->drv.p->font_height / 2 + 1,
  262. dp->drv.p->font_width, 1);
  263. break;
  264. case 0x00002580: /* ▀ */
  265. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  266. dp->drv.p->gc,
  267. x * dp->drv.p->font_width,
  268. y * dp->drv.p->font_height,
  269. dp->drv.p->font_width,
  270. dp->drv.p->font_height / 2);
  271. break;
  272. case 0x00002584: /* ▄ */
  273. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  274. dp->drv.p->gc,
  275. x * dp->drv.p->font_width,
  276. (y + 1) * dp->drv.p->font_height
  277. - dp->drv.p->font_height / 2,
  278. dp->drv.p->font_width,
  279. dp->drv.p->font_height / 2);
  280. break;
  281. case 0x00002588: /* █ */
  282. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  283. dp->drv.p->gc,
  284. x * dp->drv.p->font_width,
  285. y * dp->drv.p->font_height,
  286. dp->drv.p->font_width,
  287. dp->drv.p->font_height);
  288. break;
  289. case 0x0000258c: /* ▌ */
  290. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  291. dp->drv.p->gc,
  292. x * dp->drv.p->font_width,
  293. y * dp->drv.p->font_height,
  294. dp->drv.p->font_width / 2,
  295. dp->drv.p->font_height);
  296. break;
  297. case 0x00002590: /* ▐ */
  298. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  299. dp->drv.p->gc,
  300. (x + 1) * dp->drv.p->font_width
  301. - dp->drv.p->font_width / 2,
  302. y * dp->drv.p->font_height,
  303. dp->drv.p->font_width / 2,
  304. dp->drv.p->font_height);
  305. break;
  306. case 0x000025a0: /* ■ */
  307. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  308. dp->drv.p->gc,
  309. x * dp->drv.p->font_width,
  310. y * dp->drv.p->font_height
  311. + dp->drv.p->font_height / 4,
  312. dp->drv.p->font_width,
  313. dp->drv.p->font_height / 2);
  314. break;
  315. case 0x00002593: /* ▓ */
  316. case 0x00002592: /* ▒ */
  317. case 0x00002591: /* ░ */
  318. {
  319. /* FIXME: this sucks utterly */
  320. int i, j, k = *chars - 0x00002591;
  321. for(j = dp->drv.p->font_height; j--; )
  322. for(i = dp->drv.p->font_width; i--; )
  323. {
  324. if(((i + 2 * (j & 1)) & 3) > k)
  325. continue;
  326. XDrawPoint(dp->drv.p->dpy, dp->drv.p->pixmap,
  327. dp->drv.p->gc,
  328. x * dp->drv.p->font_width + i,
  329. y * dp->drv.p->font_height + j);
  330. }
  331. break;
  332. }
  333. default:
  334. {
  335. char ch;
  336. ch = '?';
  337. XDrawString(dp->drv.p->dpy, dp->drv.p->pixmap,
  338. dp->drv.p->gc,
  339. x * dp->drv.p->font_width, yoff, &ch, 1);
  340. break;
  341. }
  342. }
  343. }
  344. }
  345. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->window,
  346. dp->drv.p->gc, 0, 0,
  347. dp->cv->width * dp->drv.p->font_width,
  348. dp->cv->height * dp->drv.p->font_height,
  349. 0, 0);
  350. XFlush(dp->drv.p->dpy);
  351. }
  352. static void x11_handle_resize(caca_display_t *dp)
  353. {
  354. Pixmap new_pixmap;
  355. new_pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window,
  356. dp->resize.w * dp->drv.p->font_width,
  357. dp->resize.h * dp->drv.p->font_height,
  358. DefaultDepth(dp->drv.p->dpy,
  359. DefaultScreen(dp->drv.p->dpy)));
  360. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, new_pixmap,
  361. dp->drv.p->gc, 0, 0,
  362. dp->resize.w * dp->drv.p->font_width,
  363. dp->resize.h * dp->drv.p->font_height, 0, 0);
  364. XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap);
  365. dp->drv.p->pixmap = new_pixmap;
  366. }
  367. static int x11_get_event(caca_display_t *dp, caca_event_t *ev)
  368. {
  369. XEvent xevent;
  370. char key;
  371. while(XCheckWindowEvent(dp->drv.p->dpy, dp->drv.p->window,
  372. dp->drv.p->event_mask, &xevent) == True)
  373. {
  374. KeySym keysym;
  375. /* Expose event */
  376. if(xevent.type == Expose)
  377. {
  378. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap,
  379. dp->drv.p->window, dp->drv.p->gc, 0, 0,
  380. dp->cv->width * dp->drv.p->font_width,
  381. dp->cv->height * dp->drv.p->font_height, 0, 0);
  382. continue;
  383. }
  384. /* Resize event */
  385. if(xevent.type == ConfigureNotify)
  386. {
  387. unsigned int w, h;
  388. w = (xevent.xconfigure.width + dp->drv.p->font_width / 3)
  389. / dp->drv.p->font_width;
  390. h = (xevent.xconfigure.height + dp->drv.p->font_height / 3)
  391. / dp->drv.p->font_height;
  392. if(!w || !h || (w == dp->cv->width && h == dp->cv->height))
  393. continue;
  394. dp->resize.w = w;
  395. dp->resize.h = h;
  396. dp->resize.resized = 1;
  397. continue;
  398. }
  399. /* Check for mouse motion events */
  400. if(xevent.type == MotionNotify)
  401. {
  402. unsigned int newx = xevent.xmotion.x / dp->drv.p->font_width;
  403. unsigned int newy = xevent.xmotion.y / dp->drv.p->font_height;
  404. if(newx >= dp->cv->width)
  405. newx = dp->cv->width - 1;
  406. if(newy >= dp->cv->height)
  407. newy = dp->cv->height - 1;
  408. if(dp->mouse.x == newx && dp->mouse.y == newy)
  409. continue;
  410. dp->mouse.x = newx;
  411. dp->mouse.y = newy;
  412. ev->type = CACA_EVENT_MOUSE_MOTION;
  413. ev->data.mouse.x = dp->mouse.x;
  414. ev->data.mouse.y = dp->mouse.y;
  415. return 1;
  416. }
  417. /* Check for mouse press and release events */
  418. if(xevent.type == ButtonPress)
  419. {
  420. ev->type = CACA_EVENT_MOUSE_PRESS;
  421. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  422. return 1;
  423. }
  424. if(xevent.type == ButtonRelease)
  425. {
  426. ev->type = CACA_EVENT_MOUSE_RELEASE;
  427. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  428. return 1;
  429. }
  430. /* Check for key press and release events */
  431. if(xevent.type == KeyPress)
  432. ev->type = CACA_EVENT_KEY_PRESS;
  433. else if(xevent.type == KeyRelease)
  434. ev->type = CACA_EVENT_KEY_RELEASE;
  435. else
  436. continue;
  437. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  438. {
  439. ev->data.key.ch = key;
  440. ev->data.key.utf32 = key;
  441. ev->data.key.utf8[0] = key;
  442. ev->data.key.utf8[1] = '\0';
  443. return 1;
  444. }
  445. keysym = XKeycodeToKeysym(dp->drv.p->dpy, xevent.xkey.keycode, 0);
  446. switch(keysym)
  447. {
  448. case XK_F1: ev->data.key.ch = CACA_KEY_F1; break;
  449. case XK_F2: ev->data.key.ch = CACA_KEY_F2; break;
  450. case XK_F3: ev->data.key.ch = CACA_KEY_F3; break;
  451. case XK_F4: ev->data.key.ch = CACA_KEY_F4; break;
  452. case XK_F5: ev->data.key.ch = CACA_KEY_F5; break;
  453. case XK_F6: ev->data.key.ch = CACA_KEY_F6; break;
  454. case XK_F7: ev->data.key.ch = CACA_KEY_F7; break;
  455. case XK_F8: ev->data.key.ch = CACA_KEY_F8; break;
  456. case XK_F9: ev->data.key.ch = CACA_KEY_F9; break;
  457. case XK_F10: ev->data.key.ch = CACA_KEY_F10; break;
  458. case XK_F11: ev->data.key.ch = CACA_KEY_F11; break;
  459. case XK_F12: ev->data.key.ch = CACA_KEY_F12; break;
  460. case XK_F13: ev->data.key.ch = CACA_KEY_F13; break;
  461. case XK_F14: ev->data.key.ch = CACA_KEY_F14; break;
  462. case XK_F15: ev->data.key.ch = CACA_KEY_F15; break;
  463. case XK_Left: ev->data.key.ch = CACA_KEY_LEFT; break;
  464. case XK_Right: ev->data.key.ch = CACA_KEY_RIGHT; break;
  465. case XK_Up: ev->data.key.ch = CACA_KEY_UP; break;
  466. case XK_Down: ev->data.key.ch = CACA_KEY_DOWN; break;
  467. case XK_KP_Page_Up:
  468. case XK_Page_Up: ev->data.key.ch = CACA_KEY_PAGEUP; break;
  469. case XK_KP_Page_Down:
  470. case XK_Page_Down: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
  471. case XK_KP_Home:
  472. case XK_Home: ev->data.key.ch = CACA_KEY_HOME; break;
  473. case XK_KP_End:
  474. case XK_End: ev->data.key.ch = CACA_KEY_END; break;
  475. default: ev->type = CACA_EVENT_NONE; return 0;
  476. }
  477. ev->data.key.utf32 = 0;
  478. ev->data.key.utf8[0] = '\0';
  479. return 1;
  480. }
  481. while(XCheckTypedEvent(dp->drv.p->dpy, ClientMessage, &xevent))
  482. {
  483. if(xevent.xclient.message_type != dp->drv.p->wm_protocols)
  484. continue;
  485. if((Atom)xevent.xclient.data.l[0] == dp->drv.p->wm_delete_window)
  486. {
  487. ev->type = CACA_EVENT_QUIT;
  488. return 1;
  489. }
  490. }
  491. ev->type = CACA_EVENT_NONE;
  492. return 0;
  493. }
  494. static void x11_set_mouse(caca_display_t *dp, int flags)
  495. {
  496. Cursor no_ptr;
  497. Pixmap bm_no;
  498. XColor black, dummy;
  499. Colormap colormap;
  500. static char const empty[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  501. if(flags)
  502. {
  503. XDefineCursor(dp->drv.p->dpy,dp->drv.p->window, 0);
  504. return;
  505. }
  506. colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy));
  507. if(!XAllocNamedColor(dp->drv.p->dpy, colormap, "black", &black, &dummy))
  508. {
  509. return;
  510. }
  511. bm_no = XCreateBitmapFromData(dp->drv.p->dpy, dp->drv.p->window,
  512. empty, 8, 8);
  513. no_ptr = XCreatePixmapCursor(dp->drv.p->dpy, bm_no, bm_no,
  514. &black, &black, 0, 0);
  515. XDefineCursor(dp->drv.p->dpy, dp->drv.p->window, no_ptr);
  516. XFreeCursor(dp->drv.p->dpy, no_ptr);
  517. if(bm_no != None)
  518. XFreePixmap(dp->drv.p->dpy, bm_no);
  519. XFreeColors(dp->drv.p->dpy, colormap, &black.pixel, 1, 0);
  520. XSync(dp->drv.p->dpy, False);
  521. }
  522. /*
  523. * XXX: following functions are local
  524. */
  525. static int x11_error_handler(Display *dpy, XErrorEvent *xevent)
  526. {
  527. /* Ignore the error */
  528. return 0;
  529. }
  530. /*
  531. * Driver initialisation
  532. */
  533. int x11_install(caca_display_t *dp)
  534. {
  535. #if defined(HAVE_GETENV)
  536. if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
  537. return -1;
  538. #endif
  539. dp->drv.driver = CACA_DRIVER_X11;
  540. dp->drv.init_graphics = x11_init_graphics;
  541. dp->drv.end_graphics = x11_end_graphics;
  542. dp->drv.set_display_title = x11_set_display_title;
  543. dp->drv.get_display_width = x11_get_display_width;
  544. dp->drv.get_display_height = x11_get_display_height;
  545. dp->drv.display = x11_display;
  546. dp->drv.handle_resize = x11_handle_resize;
  547. dp->drv.get_event = x11_get_event;
  548. dp->drv.set_mouse = x11_set_mouse;
  549. return 0;
  550. }
  551. #endif /* USE_X11 */