Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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