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.

307 lines
9.9 KiB

  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * © 2010-2012 Michael Stapelberg
  5. *
  6. * xcb.c: contains all functions which use XCB to talk to X11. Mostly wrappers
  7. * around the rather complicated/ugly parts of the XCB API.
  8. *
  9. */
  10. #include <xcb/xcb.h>
  11. #include <xcb/xcb_keysyms.h>
  12. #include <xcb/xcb_image.h>
  13. #include <xcb/dpms.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdbool.h>
  17. #include <unistd.h>
  18. #include <assert.h>
  19. #include <err.h>
  20. #include "cursors.h"
  21. xcb_connection_t *conn;
  22. xcb_screen_t *screen;
  23. #define curs_invisible_width 8
  24. #define curs_invisible_height 8
  25. static unsigned char curs_invisible_bits[] = {
  26. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  27. #define curs_windows_width 11
  28. #define curs_windows_height 19
  29. static unsigned char curs_windows_bits[] = {
  30. 0xfe, 0x07, 0xfc, 0x07, 0xfa, 0x07, 0xf6, 0x07, 0xee, 0x07, 0xde, 0x07,
  31. 0xbe, 0x07, 0x7e, 0x07, 0xfe, 0x06, 0xfe, 0x05, 0x3e, 0x00, 0xb6, 0x07,
  32. 0x6a, 0x07, 0x6c, 0x07, 0xde, 0x06, 0xdf, 0x06, 0xbf, 0x05, 0xbf, 0x05,
  33. 0x7f, 0x06 };
  34. #define mask_windows_width 11
  35. #define mask_windows_height 19
  36. static unsigned char mask_windows_bits[] = {
  37. 0x01, 0x00, 0x03, 0x00, 0x07, 0x00, 0x0f, 0x00, 0x1f, 0x00, 0x3f, 0x00,
  38. 0x7f, 0x00, 0xff, 0x00, 0xff, 0x01, 0xff, 0x03, 0xff, 0x07, 0x7f, 0x00,
  39. 0xf7, 0x00, 0xf3, 0x00, 0xe1, 0x01, 0xe0, 0x01, 0xc0, 0x03, 0xc0, 0x03,
  40. 0x80, 0x01 };
  41. static uint32_t get_colorpixel(char *hex) {
  42. char strgroups[3][3] = {{hex[0], hex[1], '\0'},
  43. {hex[2], hex[3], '\0'},
  44. {hex[4], hex[5], '\0'}};
  45. uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
  46. (strtol(strgroups[1], NULL, 16)),
  47. (strtol(strgroups[2], NULL, 16))};
  48. return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
  49. }
  50. xcb_visualtype_t *get_root_visual_type(xcb_screen_t *screen) {
  51. xcb_visualtype_t *visual_type = NULL;
  52. xcb_depth_iterator_t depth_iter;
  53. xcb_visualtype_iterator_t visual_iter;
  54. for (depth_iter = xcb_screen_allowed_depths_iterator(screen);
  55. depth_iter.rem;
  56. xcb_depth_next(&depth_iter)) {
  57. for (visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
  58. visual_iter.rem;
  59. xcb_visualtype_next(&visual_iter)) {
  60. if (screen->root_visual != visual_iter.data->visual_id)
  61. continue;
  62. visual_type = visual_iter.data;
  63. return visual_type;
  64. }
  65. }
  66. return NULL;
  67. }
  68. xcb_pixmap_t create_bg_pixmap(xcb_connection_t *conn, xcb_screen_t *scr, u_int32_t* resolution, char *color) {
  69. xcb_pixmap_t bg_pixmap = xcb_generate_id(conn);
  70. xcb_create_pixmap(conn, scr->root_depth, bg_pixmap, scr->root,
  71. resolution[0], resolution[1]);
  72. /* Generate a Graphics Context and fill the pixmap with background color
  73. * (for images that are smaller than your screen) */
  74. xcb_gcontext_t gc = xcb_generate_id(conn);
  75. uint32_t values[] = { get_colorpixel(color) };
  76. xcb_create_gc(conn, gc, bg_pixmap, XCB_GC_FOREGROUND, values);
  77. xcb_rectangle_t rect = { 0, 0, resolution[0], resolution[1] };
  78. xcb_poly_fill_rectangle(conn, bg_pixmap, gc, 1, &rect);
  79. xcb_free_gc(conn, gc);
  80. return bg_pixmap;
  81. }
  82. xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, char *color, xcb_pixmap_t pixmap) {
  83. uint32_t mask = 0;
  84. uint32_t values[3];
  85. xcb_window_t win = xcb_generate_id(conn);
  86. if (pixmap == XCB_NONE) {
  87. mask |= XCB_CW_BACK_PIXEL;
  88. values[0] = get_colorpixel(color);
  89. } else {
  90. mask |= XCB_CW_BACK_PIXMAP;
  91. values[0] = pixmap;
  92. }
  93. mask |= XCB_CW_OVERRIDE_REDIRECT;
  94. values[1] = 1;
  95. mask |= XCB_CW_EVENT_MASK;
  96. values[2] = XCB_EVENT_MASK_EXPOSURE |
  97. XCB_EVENT_MASK_KEY_PRESS |
  98. XCB_EVENT_MASK_KEY_RELEASE |
  99. XCB_EVENT_MASK_VISIBILITY_CHANGE |
  100. XCB_EVENT_MASK_STRUCTURE_NOTIFY;
  101. xcb_create_window(conn,
  102. XCB_COPY_FROM_PARENT,
  103. win, /* the window id */
  104. scr->root, /* parent == root */
  105. 0, 0,
  106. scr->width_in_pixels,
  107. scr->height_in_pixels, /* dimensions */
  108. 0, /* border = 0, we draw our own */
  109. XCB_WINDOW_CLASS_INPUT_OUTPUT,
  110. XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
  111. mask,
  112. values);
  113. /* Map the window (= make it visible) */
  114. xcb_map_window(conn, win);
  115. /* Raise window (put it on top) */
  116. values[0] = XCB_STACK_MODE_ABOVE;
  117. xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
  118. return win;
  119. }
  120. /*
  121. * Returns the mask for Mode_switch (to be used for looking up keysymbols by
  122. * keycode).
  123. *
  124. */
  125. uint32_t get_mod_mask(xcb_connection_t *conn, xcb_key_symbols_t *symbols, uint32_t keycode) {
  126. xcb_get_modifier_mapping_reply_t *modmap_r;
  127. xcb_keycode_t *modmap, kc;
  128. xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(symbols, keycode);
  129. if (modeswitchcodes == NULL)
  130. return 0;
  131. modmap_r = xcb_get_modifier_mapping_reply(conn, xcb_get_modifier_mapping(conn), NULL);
  132. modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
  133. for (int i = 0; i < 8; i++)
  134. for (int j = 0; j < modmap_r->keycodes_per_modifier; j++) {
  135. kc = modmap[i * modmap_r->keycodes_per_modifier + j];
  136. for (xcb_keycode_t *ktest = modeswitchcodes; *ktest; ktest++) {
  137. if (*ktest != kc)
  138. continue;
  139. free(modeswitchcodes);
  140. free(modmap_r);
  141. return (1 << i);
  142. }
  143. }
  144. free(modeswitchcodes);
  145. free(modmap_r);
  146. return 0;
  147. }
  148. void dpms_turn_off_screen(xcb_connection_t *conn) {
  149. xcb_dpms_enable(conn);
  150. xcb_dpms_force_level(conn, XCB_DPMS_DPMS_MODE_OFF);
  151. xcb_flush(conn);
  152. }
  153. /*
  154. * Repeatedly tries to grab pointer and keyboard (up to 1000 times).
  155. *
  156. */
  157. void grab_pointer_and_keyboard(xcb_connection_t *conn, xcb_screen_t *screen, xcb_cursor_t cursor) {
  158. xcb_grab_pointer_cookie_t pcookie;
  159. xcb_grab_pointer_reply_t *preply;
  160. xcb_grab_keyboard_cookie_t kcookie;
  161. xcb_grab_keyboard_reply_t *kreply;
  162. int tries = 10000;
  163. while (tries-- > 0) {
  164. pcookie = xcb_grab_pointer(
  165. conn,
  166. false, /* get all pointer events specified by the following mask */
  167. screen->root, /* grab the root window */
  168. XCB_NONE, /* which events to let through */
  169. XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
  170. XCB_GRAB_MODE_ASYNC, /* keyboard mode */
  171. XCB_NONE, /* confine_to = in which window should the cursor stay */
  172. cursor, /* we change the cursor to whatever the user wanted */
  173. XCB_CURRENT_TIME
  174. );
  175. if ((preply = xcb_grab_pointer_reply(conn, pcookie, NULL)) &&
  176. preply->status == XCB_GRAB_STATUS_SUCCESS) {
  177. free(preply);
  178. break;
  179. }
  180. /* Make this quite a bit slower */
  181. usleep(50);
  182. }
  183. while (tries-- > 0) {
  184. kcookie = xcb_grab_keyboard(
  185. conn,
  186. true, /* report events */
  187. screen->root, /* grab the root window */
  188. XCB_CURRENT_TIME,
  189. XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */
  190. XCB_GRAB_MODE_ASYNC
  191. );
  192. if ((kreply = xcb_grab_keyboard_reply(conn, kcookie, NULL)) &&
  193. kreply->status == XCB_GRAB_STATUS_SUCCESS) {
  194. free(kreply);
  195. break;
  196. }
  197. /* Make this quite a bit slower */
  198. usleep(50);
  199. }
  200. if (tries <= 0)
  201. errx(EXIT_FAILURE, "Cannot grab pointer/keyboard");
  202. }
  203. xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_window_t win, int choice) {
  204. xcb_pixmap_t bitmap;
  205. xcb_pixmap_t mask;
  206. xcb_cursor_t cursor;
  207. unsigned char* curs_bits;
  208. unsigned char* mask_bits;
  209. int curs_w, curs_h;
  210. switch (choice) {
  211. case CURS_NONE:
  212. curs_bits = curs_invisible_bits;
  213. mask_bits = curs_invisible_bits;
  214. curs_w = curs_invisible_width;
  215. curs_h = curs_invisible_height;
  216. break;
  217. case CURS_WIN:
  218. curs_bits = curs_windows_bits;
  219. mask_bits = mask_windows_bits;
  220. curs_w = curs_windows_width;
  221. curs_h = curs_windows_height;
  222. break;
  223. case CURS_DEFAULT:
  224. default:
  225. return XCB_NONE; /* XCB_NONE is xcb's way of saying "don't change the cursor" */
  226. }
  227. bitmap = xcb_create_pixmap_from_bitmap_data(conn,
  228. win,
  229. curs_bits,
  230. curs_w,
  231. curs_h,
  232. 1,
  233. screen->white_pixel,
  234. screen->black_pixel,
  235. NULL);
  236. mask = xcb_create_pixmap_from_bitmap_data(conn,
  237. win,
  238. mask_bits,
  239. curs_w,
  240. curs_h,
  241. 1,
  242. screen->white_pixel,
  243. screen->black_pixel,
  244. NULL);
  245. cursor = xcb_generate_id(conn);
  246. xcb_create_cursor(conn,
  247. cursor,
  248. bitmap,
  249. mask,
  250. 65535,65535,65535,
  251. 0,0,0,
  252. 0,0);
  253. xcb_free_pixmap(conn, bitmap);
  254. xcb_free_pixmap(conn, mask);
  255. return cursor;
  256. }