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.

преди 18 години
преди 19 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. XSetForeground(dp->drv.p->dpy, dp->drv.p->gc,
  232. dp->drv.p->colors[_cucul_argb32_to_rgb12fg(*attr)]);
  233. /* Plain ASCII, no problem. */
  234. if(*chars > 0x00000020 && *chars < 0x00000080)
  235. {
  236. char ch = (uint8_t)*chars;
  237. XDrawString(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc,
  238. x * dp->drv.p->font_width, yoff, &ch, 1);
  239. continue;
  240. }
  241. /* We want to be able to print a few special Unicode characters
  242. * such as the CP437 gradients and half blocks. For unknown
  243. * characters, just print '?'. */
  244. switch(*chars)
  245. {
  246. case 0x000000b7: /* · */
  247. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  248. dp->drv.p->gc,
  249. x * dp->drv.p->font_width
  250. + dp->drv.p->font_width / 2,
  251. y * dp->drv.p->font_height
  252. + dp->drv.p->font_height / 2, 2, 2);
  253. break;
  254. case 0x00002500: /* ─ */
  255. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  256. dp->drv.p->gc,
  257. x * dp->drv.p->font_width,
  258. y * dp->drv.p->font_height
  259. + dp->drv.p->font_height / 2 + 1,
  260. dp->drv.p->font_width, 1);
  261. break;
  262. case 0x00002580: /* ▀ */
  263. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  264. dp->drv.p->gc,
  265. x * dp->drv.p->font_width,
  266. y * dp->drv.p->font_height,
  267. dp->drv.p->font_width,
  268. dp->drv.p->font_height / 2);
  269. break;
  270. case 0x00002584: /* ▄ */
  271. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  272. dp->drv.p->gc,
  273. x * dp->drv.p->font_width,
  274. (y + 1) * dp->drv.p->font_height
  275. - dp->drv.p->font_height / 2,
  276. dp->drv.p->font_width,
  277. dp->drv.p->font_height / 2);
  278. break;
  279. case 0x00002588: /* █ */
  280. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  281. dp->drv.p->gc,
  282. x * dp->drv.p->font_width,
  283. y * dp->drv.p->font_height,
  284. dp->drv.p->font_width,
  285. dp->drv.p->font_height);
  286. break;
  287. case 0x0000258c: /* ▌ */
  288. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  289. dp->drv.p->gc,
  290. x * dp->drv.p->font_width,
  291. y * dp->drv.p->font_height,
  292. dp->drv.p->font_width / 2,
  293. dp->drv.p->font_height);
  294. break;
  295. case 0x00002590: /* ▐ */
  296. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  297. dp->drv.p->gc,
  298. (x + 1) * dp->drv.p->font_width
  299. - dp->drv.p->font_width / 2,
  300. y * dp->drv.p->font_height,
  301. dp->drv.p->font_width / 2,
  302. dp->drv.p->font_height);
  303. break;
  304. case 0x000025a0: /* ■ */
  305. XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap,
  306. dp->drv.p->gc,
  307. x * dp->drv.p->font_width,
  308. y * dp->drv.p->font_height
  309. + dp->drv.p->font_height / 4,
  310. dp->drv.p->font_width,
  311. dp->drv.p->font_height / 2);
  312. break;
  313. case 0x00002593: /* ▓ */
  314. case 0x00002592: /* ▒ */
  315. case 0x00002591: /* ░ */
  316. {
  317. /* FIXME: this sucks utterly */
  318. int i, j, k = *chars - 0x00002591;
  319. for(j = dp->drv.p->font_height; j--; )
  320. for(i = dp->drv.p->font_width; i--; )
  321. {
  322. if(((i + 2 * (j & 1)) & 3) > k)
  323. continue;
  324. XDrawPoint(dp->drv.p->dpy, dp->drv.p->pixmap,
  325. dp->drv.p->gc,
  326. x * dp->drv.p->font_width + i,
  327. y * dp->drv.p->font_height + j);
  328. }
  329. break;
  330. }
  331. default:
  332. {
  333. char ch;
  334. ch = '?';
  335. XDrawString(dp->drv.p->dpy, dp->drv.p->pixmap,
  336. dp->drv.p->gc,
  337. x * dp->drv.p->font_width, yoff, &ch, 1);
  338. break;
  339. }
  340. }
  341. }
  342. }
  343. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->window,
  344. dp->drv.p->gc, 0, 0,
  345. dp->cv->width * dp->drv.p->font_width,
  346. dp->cv->height * dp->drv.p->font_height,
  347. 0, 0);
  348. XFlush(dp->drv.p->dpy);
  349. }
  350. static void x11_handle_resize(caca_display_t *dp)
  351. {
  352. Pixmap new_pixmap;
  353. new_pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window,
  354. dp->resize.w * dp->drv.p->font_width,
  355. dp->resize.h * dp->drv.p->font_height,
  356. DefaultDepth(dp->drv.p->dpy,
  357. DefaultScreen(dp->drv.p->dpy)));
  358. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, new_pixmap,
  359. dp->drv.p->gc, 0, 0,
  360. dp->resize.w * dp->drv.p->font_width,
  361. dp->resize.h * dp->drv.p->font_height, 0, 0);
  362. XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap);
  363. dp->drv.p->pixmap = new_pixmap;
  364. }
  365. static int x11_get_event(caca_display_t *dp, caca_event_t *ev)
  366. {
  367. XEvent xevent;
  368. char key;
  369. while(XCheckWindowEvent(dp->drv.p->dpy, dp->drv.p->window,
  370. dp->drv.p->event_mask, &xevent) == True)
  371. {
  372. KeySym keysym;
  373. /* Expose event */
  374. if(xevent.type == Expose)
  375. {
  376. XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap,
  377. dp->drv.p->window, dp->drv.p->gc, 0, 0,
  378. dp->cv->width * dp->drv.p->font_width,
  379. dp->cv->height * dp->drv.p->font_height, 0, 0);
  380. continue;
  381. }
  382. /* Resize event */
  383. if(xevent.type == ConfigureNotify)
  384. {
  385. unsigned int w, h;
  386. w = (xevent.xconfigure.width + dp->drv.p->font_width / 3)
  387. / dp->drv.p->font_width;
  388. h = (xevent.xconfigure.height + dp->drv.p->font_height / 3)
  389. / dp->drv.p->font_height;
  390. if(!w || !h || (w == dp->cv->width && h == dp->cv->height))
  391. continue;
  392. dp->resize.w = w;
  393. dp->resize.h = h;
  394. dp->resize.resized = 1;
  395. continue;
  396. }
  397. /* Check for mouse motion events */
  398. if(xevent.type == MotionNotify)
  399. {
  400. unsigned int newx = xevent.xmotion.x / dp->drv.p->font_width;
  401. unsigned int newy = xevent.xmotion.y / dp->drv.p->font_height;
  402. if(newx >= dp->cv->width)
  403. newx = dp->cv->width - 1;
  404. if(newy >= dp->cv->height)
  405. newy = dp->cv->height - 1;
  406. if(dp->mouse.x == newx && dp->mouse.y == newy)
  407. continue;
  408. dp->mouse.x = newx;
  409. dp->mouse.y = newy;
  410. ev->type = CACA_EVENT_MOUSE_MOTION;
  411. ev->data.mouse.x = dp->mouse.x;
  412. ev->data.mouse.y = dp->mouse.y;
  413. return 1;
  414. }
  415. /* Check for mouse press and release events */
  416. if(xevent.type == ButtonPress)
  417. {
  418. ev->type = CACA_EVENT_MOUSE_PRESS;
  419. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  420. return 1;
  421. }
  422. if(xevent.type == ButtonRelease)
  423. {
  424. ev->type = CACA_EVENT_MOUSE_RELEASE;
  425. ev->data.mouse.button = ((XButtonEvent *)&xevent)->button;
  426. return 1;
  427. }
  428. /* Check for key press and release events */
  429. if(xevent.type == KeyPress)
  430. ev->type = CACA_EVENT_KEY_PRESS;
  431. else if(xevent.type == KeyRelease)
  432. ev->type = CACA_EVENT_KEY_RELEASE;
  433. else
  434. continue;
  435. if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
  436. {
  437. ev->data.key.ch = key;
  438. ev->data.key.utf32 = key;
  439. ev->data.key.utf8[0] = key;
  440. ev->data.key.utf8[1] = '\0';
  441. return 1;
  442. }
  443. keysym = XKeycodeToKeysym(dp->drv.p->dpy, xevent.xkey.keycode, 0);
  444. switch(keysym)
  445. {
  446. case XK_F1: ev->data.key.ch = CACA_KEY_F1; break;
  447. case XK_F2: ev->data.key.ch = CACA_KEY_F2; break;
  448. case XK_F3: ev->data.key.ch = CACA_KEY_F3; break;
  449. case XK_F4: ev->data.key.ch = CACA_KEY_F4; break;
  450. case XK_F5: ev->data.key.ch = CACA_KEY_F5; break;
  451. case XK_F6: ev->data.key.ch = CACA_KEY_F6; break;
  452. case XK_F7: ev->data.key.ch = CACA_KEY_F7; break;
  453. case XK_F8: ev->data.key.ch = CACA_KEY_F8; break;
  454. case XK_F9: ev->data.key.ch = CACA_KEY_F9; break;
  455. case XK_F10: ev->data.key.ch = CACA_KEY_F10; break;
  456. case XK_F11: ev->data.key.ch = CACA_KEY_F11; break;
  457. case XK_F12: ev->data.key.ch = CACA_KEY_F12; break;
  458. case XK_F13: ev->data.key.ch = CACA_KEY_F13; break;
  459. case XK_F14: ev->data.key.ch = CACA_KEY_F14; break;
  460. case XK_F15: ev->data.key.ch = CACA_KEY_F15; break;
  461. case XK_Left: ev->data.key.ch = CACA_KEY_LEFT; break;
  462. case XK_Right: ev->data.key.ch = CACA_KEY_RIGHT; break;
  463. case XK_Up: ev->data.key.ch = CACA_KEY_UP; break;
  464. case XK_Down: ev->data.key.ch = CACA_KEY_DOWN; break;
  465. case XK_KP_Page_Up:
  466. case XK_Page_Up: ev->data.key.ch = CACA_KEY_PAGEUP; break;
  467. case XK_KP_Page_Down:
  468. case XK_Page_Down: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
  469. case XK_KP_Home:
  470. case XK_Home: ev->data.key.ch = CACA_KEY_HOME; break;
  471. case XK_KP_End:
  472. case XK_End: ev->data.key.ch = CACA_KEY_END; break;
  473. default: ev->type = CACA_EVENT_NONE; return 0;
  474. }
  475. ev->data.key.utf32 = 0;
  476. ev->data.key.utf8[0] = '\0';
  477. return 1;
  478. }
  479. while(XCheckTypedEvent(dp->drv.p->dpy, ClientMessage, &xevent))
  480. {
  481. if(xevent.xclient.message_type != dp->drv.p->wm_protocols)
  482. continue;
  483. if((Atom)xevent.xclient.data.l[0] == dp->drv.p->wm_delete_window)
  484. {
  485. ev->type = CACA_EVENT_QUIT;
  486. return 1;
  487. }
  488. }
  489. ev->type = CACA_EVENT_NONE;
  490. return 0;
  491. }
  492. static void x11_set_mouse(caca_display_t *dp, int flags)
  493. {
  494. Cursor no_ptr;
  495. Pixmap bm_no;
  496. XColor black, dummy;
  497. Colormap colormap;
  498. static char const empty[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  499. if(flags)
  500. {
  501. XDefineCursor(dp->drv.p->dpy,dp->drv.p->window, 0);
  502. return;
  503. }
  504. colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy));
  505. if(!XAllocNamedColor(dp->drv.p->dpy, colormap, "black", &black, &dummy))
  506. {
  507. return;
  508. }
  509. bm_no = XCreateBitmapFromData(dp->drv.p->dpy, dp->drv.p->window,
  510. empty, 8, 8);
  511. no_ptr = XCreatePixmapCursor(dp->drv.p->dpy, bm_no, bm_no,
  512. &black, &black, 0, 0);
  513. XDefineCursor(dp->drv.p->dpy, dp->drv.p->window, no_ptr);
  514. XFreeCursor(dp->drv.p->dpy, no_ptr);
  515. if(bm_no != None)
  516. XFreePixmap(dp->drv.p->dpy, bm_no);
  517. XFreeColors(dp->drv.p->dpy, colormap, &black.pixel, 1, 0);
  518. XSync(dp->drv.p->dpy, False);
  519. }
  520. /*
  521. * XXX: following functions are local
  522. */
  523. static int x11_error_handler(Display *dpy, XErrorEvent *xevent)
  524. {
  525. /* Ignore the error */
  526. return 0;
  527. }
  528. /*
  529. * Driver initialisation
  530. */
  531. int x11_install(caca_display_t *dp)
  532. {
  533. #if defined(HAVE_GETENV)
  534. if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
  535. return -1;
  536. #endif
  537. dp->drv.driver = CACA_DRIVER_X11;
  538. dp->drv.init_graphics = x11_init_graphics;
  539. dp->drv.end_graphics = x11_end_graphics;
  540. dp->drv.set_display_title = x11_set_display_title;
  541. dp->drv.get_display_width = x11_get_display_width;
  542. dp->drv.get_display_height = x11_get_display_height;
  543. dp->drv.display = x11_display;
  544. dp->drv.handle_resize = x11_handle_resize;
  545. dp->drv.get_event = x11_get_event;
  546. dp->drv.set_mouse = x11_set_mouse;
  547. return 0;
  548. }
  549. #endif /* USE_X11 */