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.

321 lines
10 KiB

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