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.

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