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.

258 lines
8.1 KiB

  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * © 2010 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. static uint32_t get_colorpixel(char *hex) {
  22. char strgroups[3][3] = {{hex[0], hex[1], '\0'},
  23. {hex[2], hex[3], '\0'},
  24. {hex[4], hex[5], '\0'}};
  25. uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
  26. (strtol(strgroups[1], NULL, 16)),
  27. (strtol(strgroups[2], NULL, 16))};
  28. return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
  29. }
  30. xcb_visualtype_t *get_root_visual_type(xcb_screen_t *screen) {
  31. xcb_visualtype_t *visual_type = NULL;
  32. xcb_depth_iterator_t depth_iter;
  33. xcb_visualtype_iterator_t visual_iter;
  34. for (depth_iter = xcb_screen_allowed_depths_iterator(screen);
  35. depth_iter.rem;
  36. xcb_depth_next(&depth_iter)) {
  37. for (visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
  38. visual_iter.rem;
  39. xcb_visualtype_next(&visual_iter)) {
  40. if (screen->root_visual != visual_iter.data->visual_id)
  41. continue;
  42. visual_type = visual_iter.data;
  43. return visual_type;
  44. }
  45. }
  46. return NULL;
  47. }
  48. xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, char *color) {
  49. uint32_t mask = 0;
  50. uint32_t values[3];
  51. xcb_window_t win = xcb_generate_id(conn);
  52. mask |= XCB_CW_BACK_PIXEL;
  53. values[0] = get_colorpixel(color);
  54. mask |= XCB_CW_OVERRIDE_REDIRECT;
  55. values[1] = 1;
  56. mask |= XCB_CW_EVENT_MASK;
  57. values[2] = XCB_EVENT_MASK_EXPOSURE |
  58. XCB_EVENT_MASK_KEY_PRESS |
  59. XCB_EVENT_MASK_KEY_RELEASE |
  60. XCB_EVENT_MASK_VISIBILITY_CHANGE;
  61. xcb_create_window(conn,
  62. 24,
  63. win, /* the window id */
  64. scr->root, /* parent == root */
  65. 0, 0,
  66. scr->width_in_pixels,
  67. scr->height_in_pixels, /* dimensions */
  68. 0, /* border = 0, we draw our own */
  69. XCB_WINDOW_CLASS_INPUT_OUTPUT,
  70. XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
  71. mask,
  72. values);
  73. /* Map the window (= make it visible) */
  74. xcb_map_window(conn, win);
  75. /* Raise window (put it on top) */
  76. values[0] = XCB_STACK_MODE_ABOVE;
  77. xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
  78. return win;
  79. }
  80. /*
  81. * Returns the mask for Mode_switch (to be used for looking up keysymbols by
  82. * keycode).
  83. *
  84. */
  85. uint32_t get_mod_mask(xcb_connection_t *conn, xcb_key_symbols_t *symbols, uint32_t keycode) {
  86. xcb_get_modifier_mapping_reply_t *modmap_r;
  87. xcb_keycode_t *modmap, kc;
  88. xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(symbols, keycode);
  89. if (modeswitchcodes == NULL)
  90. return 0;
  91. modmap_r = xcb_get_modifier_mapping_reply(conn, xcb_get_modifier_mapping(conn), NULL);
  92. modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
  93. for (int i = 0; i < 8; i++)
  94. for (int j = 0; j < modmap_r->keycodes_per_modifier; j++) {
  95. kc = modmap[i * modmap_r->keycodes_per_modifier + j];
  96. for (xcb_keycode_t *ktest = modeswitchcodes; *ktest; ktest++) {
  97. if (*ktest != kc)
  98. continue;
  99. free(modeswitchcodes);
  100. free(modmap_r);
  101. return (1 << i);
  102. }
  103. }
  104. return 0;
  105. }
  106. void dpms_turn_off_screen(xcb_connection_t *conn) {
  107. xcb_dpms_enable(conn);
  108. xcb_dpms_force_level(conn, XCB_DPMS_DPMS_MODE_OFF);
  109. xcb_flush(conn);
  110. }
  111. /*
  112. * Repeatedly tries to grab pointer and keyboard (up to 1000 times).
  113. *
  114. */
  115. void grab_pointer_and_keyboard(xcb_connection_t *conn, xcb_screen_t *screen, xcb_cursor_t cursor) {
  116. xcb_grab_pointer_cookie_t pcookie;
  117. xcb_grab_pointer_reply_t *preply;
  118. xcb_grab_keyboard_cookie_t kcookie;
  119. xcb_grab_keyboard_reply_t *kreply;
  120. int tries = 10000;
  121. while (tries-- > 0) {
  122. pcookie = xcb_grab_pointer(
  123. conn,
  124. false, /* get all pointer events specified by the following mask */
  125. screen->root, /* grab the root window */
  126. XCB_NONE, /* which events to let through */
  127. XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
  128. XCB_GRAB_MODE_ASYNC, /* keyboard mode */
  129. XCB_NONE, /* confine_to = in which window should the cursor stay */
  130. cursor, /* we change the cursor to whatever the user wanted */
  131. XCB_CURRENT_TIME
  132. );
  133. if ((preply = xcb_grab_pointer_reply(conn, pcookie, NULL)) &&
  134. preply->status == XCB_GRAB_STATUS_SUCCESS) {
  135. free(preply);
  136. break;
  137. }
  138. /* Make this quite a bit slower */
  139. usleep(50);
  140. }
  141. if (cursor != XCB_NONE)
  142. xcb_free_cursor(conn, cursor);
  143. while (tries-- > 0) {
  144. kcookie = xcb_grab_keyboard(
  145. conn,
  146. true, /* report events */
  147. screen->root, /* grab the root window */
  148. XCB_CURRENT_TIME,
  149. XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */
  150. XCB_GRAB_MODE_ASYNC
  151. );
  152. if ((kreply = xcb_grab_keyboard_reply(conn, kcookie, NULL)) &&
  153. kreply->status == XCB_GRAB_STATUS_SUCCESS) {
  154. free(kreply);
  155. break;
  156. }
  157. /* Make this quite a bit slower */
  158. usleep(50);
  159. }
  160. if (tries <= 0)
  161. errx(EXIT_FAILURE, "Cannot grab pointer/keyboard");
  162. }
  163. xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_window_t win, int choice) {
  164. xcb_pixmap_t bitmap;
  165. xcb_pixmap_t mask;
  166. xcb_cursor_t cursor;
  167. unsigned char* curs_bits;
  168. unsigned char* mask_bits;
  169. int curs_w, curs_h;
  170. switch (choice) {
  171. case CURS_NONE:
  172. curs_bits = curs_invisible_bits;
  173. mask_bits = curs_invisible_bits;
  174. curs_w = curs_invisible_width;
  175. curs_h = curs_invisible_height;
  176. break;
  177. case CURS_WIN:
  178. curs_bits = curs_windows_bits;
  179. mask_bits = mask_windows_bits;
  180. curs_w = curs_windows_width;
  181. curs_h = curs_windows_height;
  182. break;
  183. case CURS_DEFAULT:
  184. default:
  185. return XCB_NONE; /* XCB_NONE is xcb's way of saying "don't change the cursor" */
  186. }
  187. bitmap = xcb_create_pixmap_from_bitmap_data(conn,
  188. win,
  189. curs_bits,
  190. curs_w,
  191. curs_h,
  192. 1,
  193. screen->white_pixel,
  194. screen->black_pixel,
  195. NULL);
  196. mask = xcb_create_pixmap_from_bitmap_data(conn,
  197. win,
  198. mask_bits,
  199. curs_w,
  200. curs_h,
  201. 1,
  202. screen->white_pixel,
  203. screen->black_pixel,
  204. NULL);
  205. cursor = xcb_generate_id(conn);
  206. xcb_create_cursor(conn,
  207. cursor,
  208. bitmap,
  209. mask,
  210. 65535,65535,65535,
  211. 0,0,0,
  212. 0,0);
  213. xcb_free_pixmap(conn, bitmap);
  214. xcb_free_pixmap(conn, mask);
  215. return cursor;
  216. }