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.

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