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.

189 lines
5.8 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/dpms.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <unistd.h>
  17. #include <assert.h>
  18. #include <err.h>
  19. static uint32_t get_colorpixel(char *hex) {
  20. char strgroups[3][3] = {{hex[0], hex[1], '\0'},
  21. {hex[2], hex[3], '\0'},
  22. {hex[4], hex[5], '\0'}};
  23. uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
  24. (strtol(strgroups[1], NULL, 16)),
  25. (strtol(strgroups[2], NULL, 16))};
  26. return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
  27. }
  28. xcb_visualtype_t *get_root_visual_type(xcb_screen_t *screen) {
  29. xcb_visualtype_t *visual_type = NULL;
  30. xcb_depth_iterator_t depth_iter;
  31. xcb_visualtype_iterator_t visual_iter;
  32. for (depth_iter = xcb_screen_allowed_depths_iterator(screen);
  33. depth_iter.rem;
  34. xcb_depth_next(&depth_iter)) {
  35. for (visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
  36. visual_iter.rem;
  37. xcb_visualtype_next(&visual_iter)) {
  38. if (screen->root_visual != visual_iter.data->visual_id)
  39. continue;
  40. visual_type = visual_iter.data;
  41. return visual_type;
  42. }
  43. }
  44. return NULL;
  45. }
  46. xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, char *color) {
  47. uint32_t mask = 0;
  48. uint32_t values[3];
  49. xcb_window_t win = xcb_generate_id(conn);
  50. mask |= XCB_CW_BACK_PIXEL;
  51. values[0] = get_colorpixel(color);
  52. mask |= XCB_CW_OVERRIDE_REDIRECT;
  53. values[1] = 1;
  54. mask |= XCB_CW_EVENT_MASK;
  55. values[2] = XCB_EVENT_MASK_EXPOSURE |
  56. XCB_EVENT_MASK_KEY_PRESS |
  57. XCB_EVENT_MASK_KEY_RELEASE |
  58. XCB_EVENT_MASK_VISIBILITY_CHANGE;
  59. xcb_create_window(conn,
  60. 24,
  61. win, /* the window id */
  62. scr->root, /* parent == root */
  63. 0, 0,
  64. scr->width_in_pixels,
  65. scr->height_in_pixels, /* dimensions */
  66. 0, /* border = 0, we draw our own */
  67. XCB_WINDOW_CLASS_INPUT_OUTPUT,
  68. XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
  69. mask,
  70. values);
  71. /* Map the window (= make it visible) */
  72. xcb_map_window(conn, win);
  73. /* Raise window (put it on top) */
  74. values[0] = XCB_STACK_MODE_ABOVE;
  75. xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
  76. return win;
  77. }
  78. /*
  79. * Returns the mask for Mode_switch (to be used for looking up keysymbols by
  80. * keycode).
  81. *
  82. */
  83. uint32_t get_mod_mask(xcb_connection_t *conn, xcb_key_symbols_t *symbols, uint32_t keycode) {
  84. xcb_get_modifier_mapping_reply_t *modmap_r;
  85. xcb_keycode_t *modmap, kc;
  86. xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(symbols, keycode);
  87. if (modeswitchcodes == NULL)
  88. return 0;
  89. modmap_r = xcb_get_modifier_mapping_reply(conn, xcb_get_modifier_mapping(conn), NULL);
  90. modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
  91. for (int i = 0; i < 8; i++)
  92. for (int j = 0; j < modmap_r->keycodes_per_modifier; j++) {
  93. kc = modmap[i * modmap_r->keycodes_per_modifier + j];
  94. for (xcb_keycode_t *ktest = modeswitchcodes; *ktest; ktest++) {
  95. if (*ktest != kc)
  96. continue;
  97. free(modeswitchcodes);
  98. free(modmap_r);
  99. return (1 << i);
  100. }
  101. }
  102. return 0;
  103. }
  104. void dpms_turn_off_screen(xcb_connection_t *conn) {
  105. xcb_dpms_enable(conn);
  106. xcb_dpms_force_level(conn, XCB_DPMS_DPMS_MODE_OFF);
  107. xcb_flush(conn);
  108. }
  109. /*
  110. * Repeatedly tries to grab pointer and keyboard (up to 1000 times).
  111. *
  112. */
  113. void grab_pointer_and_keyboard(xcb_connection_t *conn, xcb_screen_t *screen) {
  114. xcb_grab_pointer_cookie_t pcookie;
  115. xcb_grab_pointer_reply_t *preply;
  116. xcb_grab_keyboard_cookie_t kcookie;
  117. xcb_grab_keyboard_reply_t *kreply;
  118. int tries = 10000;
  119. while (tries-- > 0) {
  120. pcookie = xcb_grab_pointer(
  121. conn,
  122. false, /* get all pointer events specified by the following mask */
  123. screen->root, /* grab the root window */
  124. XCB_NONE, /* which events to let through */
  125. XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
  126. XCB_GRAB_MODE_ASYNC, /* keyboard mode */
  127. XCB_NONE, /* confine_to = in which window should the cursor stay */
  128. XCB_NONE, /* don’t display a special cursor */
  129. XCB_CURRENT_TIME
  130. );
  131. if ((preply = xcb_grab_pointer_reply(conn, pcookie, NULL)) &&
  132. preply->status == XCB_GRAB_STATUS_SUCCESS) {
  133. free(preply);
  134. break;
  135. }
  136. /* Make this quite a bit slower */
  137. usleep(50);
  138. }
  139. while (tries-- > 0) {
  140. kcookie = xcb_grab_keyboard(
  141. conn,
  142. true, /* report events */
  143. screen->root, /* grab the root window */
  144. XCB_CURRENT_TIME,
  145. XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */
  146. XCB_GRAB_MODE_ASYNC
  147. );
  148. if ((kreply = xcb_grab_keyboard_reply(conn, kcookie, NULL)) &&
  149. kreply->status == XCB_GRAB_STATUS_SUCCESS) {
  150. free(kreply);
  151. break;
  152. }
  153. /* Make this quite a bit slower */
  154. usleep(50);
  155. }
  156. if (tries <= 0)
  157. errx(EXIT_FAILURE, "Cannot grab pointer/keyboard");
  158. }