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.

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