422 lines
14 KiB

13 years ago
13 years ago
13 years ago
  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_image.h>
  12. #include <xcb/xcb_atom.h>
  13. #include <xcb/xcb_aux.h>
  14. #include <xcb/composite.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdbool.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <assert.h>
  21. #include <err.h>
  22. #include <time.h>
  23. #include <sys/time.h>
  24. #include "cursors.h"
  25. #include "unlock_indicator.h"
  26. extern auth_state_t auth_state;
  27. extern bool use_composite_overlay;
  28. xcb_connection_t *conn;
  29. xcb_screen_t *screen;
  30. #define curs_invisible_width 8
  31. #define curs_invisible_height 8
  32. static unsigned char curs_invisible_bits[] = {
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  34. #define curs_windows_width 11
  35. #define curs_windows_height 19
  36. static unsigned char curs_windows_bits[] = {
  37. 0xfe, 0x07, 0xfc, 0x07, 0xfa, 0x07, 0xf6, 0x07, 0xee, 0x07, 0xde, 0x07,
  38. 0xbe, 0x07, 0x7e, 0x07, 0xfe, 0x06, 0xfe, 0x05, 0x3e, 0x00, 0xb6, 0x07,
  39. 0x6a, 0x07, 0x6c, 0x07, 0xde, 0x06, 0xdf, 0x06, 0xbf, 0x05, 0xbf, 0x05,
  40. 0x7f, 0x06};
  41. #define mask_windows_width 11
  42. #define mask_windows_height 19
  43. static unsigned char mask_windows_bits[] = {
  44. 0x01, 0x00, 0x03, 0x00, 0x07, 0x00, 0x0f, 0x00, 0x1f, 0x00, 0x3f, 0x00,
  45. 0x7f, 0x00, 0xff, 0x00, 0xff, 0x01, 0xff, 0x03, 0xff, 0x07, 0x7f, 0x00,
  46. 0xf7, 0x00, 0xf3, 0x00, 0xe1, 0x01, 0xe0, 0x01, 0xc0, 0x03, 0xc0, 0x03,
  47. 0x80, 0x01};
  48. static uint32_t get_colorpixel(char *hex) {
  49. char strgroups[3][3] = {{hex[0], hex[1], '\0'},
  50. {hex[2], hex[3], '\0'},
  51. {hex[4], hex[5], '\0'}};
  52. uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
  53. (strtol(strgroups[1], NULL, 16)),
  54. (strtol(strgroups[2], NULL, 16))};
  55. return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
  56. }
  57. xcb_visualtype_t *get_root_visual_type(xcb_screen_t *screen) {
  58. xcb_visualtype_t *visual_type = NULL;
  59. xcb_depth_iterator_t depth_iter;
  60. xcb_visualtype_iterator_t visual_iter;
  61. for (depth_iter = xcb_screen_allowed_depths_iterator(screen);
  62. depth_iter.rem;
  63. xcb_depth_next(&depth_iter)) {
  64. for (visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
  65. visual_iter.rem;
  66. xcb_visualtype_next(&visual_iter)) {
  67. if (screen->root_visual != visual_iter.data->visual_id)
  68. continue;
  69. visual_type = visual_iter.data;
  70. return visual_type;
  71. }
  72. }
  73. return NULL;
  74. }
  75. xcb_pixmap_t create_bg_pixmap(xcb_connection_t *conn, xcb_screen_t *scr, u_int32_t *resolution, char *color) {
  76. xcb_pixmap_t bg_pixmap = xcb_generate_id(conn);
  77. xcb_create_pixmap(conn, scr->root_depth, bg_pixmap, scr->root,
  78. resolution[0], resolution[1]);
  79. /* Generate a Graphics Context and fill the pixmap with background color
  80. * (for images that are smaller than your screen) */
  81. xcb_gcontext_t gc = xcb_generate_id(conn);
  82. uint32_t values[] = {get_colorpixel(color)};
  83. xcb_create_gc(conn, gc, bg_pixmap, XCB_GC_FOREGROUND, values);
  84. xcb_rectangle_t rect = {0, 0, resolution[0], resolution[1]};
  85. xcb_poly_fill_rectangle(conn, bg_pixmap, gc, 1, &rect);
  86. xcb_free_gc(conn, gc);
  87. return bg_pixmap;
  88. }
  89. xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, char *color, xcb_pixmap_t pixmap) {
  90. uint32_t mask = 0;
  91. uint32_t values[3];
  92. xcb_window_t win = xcb_generate_id(conn);
  93. xcb_window_t parent_win = scr->root;
  94. /* Check whether the composite extension is available */
  95. if (use_composite_overlay) {
  96. const xcb_query_extension_reply_t *extension_query = NULL;
  97. xcb_generic_error_t *error = NULL;
  98. xcb_composite_get_overlay_window_cookie_t cookie;
  99. xcb_composite_get_overlay_window_reply_t *composite_reply = NULL;
  100. extension_query = xcb_get_extension_data(conn, &xcb_composite_id);
  101. if (extension_query && extension_query->present) {
  102. /* When composition is used, we need to use the composite overlay
  103. * window instead of the normal root window to be able to cover
  104. * composited windows */
  105. cookie = xcb_composite_get_overlay_window(conn, scr->root);
  106. composite_reply = xcb_composite_get_overlay_window_reply(conn, cookie, &error);
  107. if (!error && composite_reply) {
  108. parent_win = composite_reply->overlay_win;
  109. }
  110. free(composite_reply);
  111. free(error);
  112. }
  113. }
  114. if (pixmap == XCB_NONE) {
  115. mask |= XCB_CW_BACK_PIXEL;
  116. values[0] = get_colorpixel(color);
  117. } else {
  118. mask |= XCB_CW_BACK_PIXMAP;
  119. values[0] = pixmap;
  120. }
  121. mask |= XCB_CW_OVERRIDE_REDIRECT;
  122. values[1] = 1;
  123. mask |= XCB_CW_EVENT_MASK;
  124. values[2] = XCB_EVENT_MASK_EXPOSURE |
  125. XCB_EVENT_MASK_KEY_PRESS |
  126. XCB_EVENT_MASK_KEY_RELEASE |
  127. XCB_EVENT_MASK_VISIBILITY_CHANGE |
  128. XCB_EVENT_MASK_STRUCTURE_NOTIFY;
  129. xcb_create_window(conn,
  130. XCB_COPY_FROM_PARENT,
  131. win, /* the window id */
  132. parent_win,
  133. 0, 0,
  134. scr->width_in_pixels,
  135. scr->height_in_pixels, /* dimensions */
  136. 0, /* border = 0, we draw our own */
  137. XCB_WINDOW_CLASS_INPUT_OUTPUT,
  138. XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
  139. mask,
  140. values);
  141. char *name = "i3lock";
  142. xcb_change_property(conn,
  143. XCB_PROP_MODE_REPLACE,
  144. win,
  145. XCB_ATOM_WM_NAME,
  146. XCB_ATOM_STRING,
  147. 8,
  148. strlen(name),
  149. name);
  150. /* Map the window (= make it visible) */
  151. xcb_map_window(conn, win);
  152. /* Raise window (put it on top) */
  153. values[0] = XCB_STACK_MODE_ABOVE;
  154. xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
  155. /* Ensure that the window is created and set up before returning */
  156. xcb_aux_sync(conn);
  157. return win;
  158. }
  159. /*
  160. * Repeatedly tries to grab pointer and keyboard (up to the specified number of
  161. * tries).
  162. *
  163. * Returns true if the grab succeeded, false if not.
  164. *
  165. */
  166. bool grab_pointer_and_keyboard(xcb_connection_t *conn, xcb_screen_t *screen, xcb_cursor_t cursor, int tries) {
  167. xcb_grab_pointer_cookie_t pcookie;
  168. xcb_grab_pointer_reply_t *preply;
  169. xcb_grab_keyboard_cookie_t kcookie;
  170. xcb_grab_keyboard_reply_t *kreply;
  171. const suseconds_t screen_redraw_timeout = 100000; /* 100ms */
  172. /* Using few variables to trigger a redraw_screen() if too many tries */
  173. bool redrawn = false;
  174. struct timeval start;
  175. if (gettimeofday(&start, NULL) == -1) {
  176. err(EXIT_FAILURE, "gettimeofday");
  177. }
  178. while (tries-- > 0) {
  179. pcookie = xcb_grab_pointer(
  180. conn,
  181. false, /* get all pointer events specified by the following mask */
  182. screen->root, /* grab the root window */
  183. XCB_NONE, /* which events to let through */
  184. XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
  185. XCB_GRAB_MODE_ASYNC, /* keyboard mode */
  186. XCB_NONE, /* confine_to = in which window should the cursor stay */
  187. cursor, /* we change the cursor to whatever the user wanted */
  188. XCB_CURRENT_TIME);
  189. if ((preply = xcb_grab_pointer_reply(conn, pcookie, NULL)) &&
  190. preply->status == XCB_GRAB_STATUS_SUCCESS) {
  191. free(preply);
  192. break;
  193. }
  194. /* In case the grab failed, we still need to free the reply */
  195. free(preply);
  196. /* Make this quite a bit slower */
  197. usleep(50);
  198. struct timeval now;
  199. if (gettimeofday(&now, NULL) == -1) {
  200. err(EXIT_FAILURE, "gettimeofday");
  201. }
  202. struct timeval elapsed;
  203. timersub(&now, &start, &elapsed);
  204. if (!redrawn &&
  205. (tries % 100) == 0 &&
  206. elapsed.tv_usec >= screen_redraw_timeout) {
  207. redraw_screen();
  208. redrawn = true;
  209. }
  210. }
  211. while (tries-- > 0) {
  212. kcookie = xcb_grab_keyboard(
  213. conn,
  214. true, /* report events */
  215. screen->root, /* grab the root window */
  216. XCB_CURRENT_TIME,
  217. XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */
  218. XCB_GRAB_MODE_ASYNC);
  219. if ((kreply = xcb_grab_keyboard_reply(conn, kcookie, NULL)) &&
  220. kreply->status == XCB_GRAB_STATUS_SUCCESS) {
  221. free(kreply);
  222. break;
  223. }
  224. /* In case the grab failed, we still need to free the reply */
  225. free(kreply);
  226. /* Make this quite a bit slower */
  227. usleep(50);
  228. struct timeval now;
  229. if (gettimeofday(&now, NULL) == -1) {
  230. err(EXIT_FAILURE, "gettimeofday");
  231. }
  232. struct timeval elapsed;
  233. timersub(&now, &start, &elapsed);
  234. /* Trigger a screen redraw if 100ms elapsed */
  235. if (!redrawn &&
  236. (tries % 100) == 0 &&
  237. elapsed.tv_usec >= screen_redraw_timeout) {
  238. redraw_screen();
  239. redrawn = true;
  240. }
  241. }
  242. return (tries > 0);
  243. }
  244. xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_window_t win, int choice) {
  245. xcb_pixmap_t bitmap;
  246. xcb_pixmap_t mask;
  247. xcb_cursor_t cursor;
  248. unsigned char *curs_bits;
  249. unsigned char *mask_bits;
  250. int curs_w, curs_h;
  251. switch (choice) {
  252. case CURS_NONE:
  253. curs_bits = curs_invisible_bits;
  254. mask_bits = curs_invisible_bits;
  255. curs_w = curs_invisible_width;
  256. curs_h = curs_invisible_height;
  257. break;
  258. case CURS_WIN:
  259. curs_bits = curs_windows_bits;
  260. mask_bits = mask_windows_bits;
  261. curs_w = curs_windows_width;
  262. curs_h = curs_windows_height;
  263. break;
  264. case CURS_DEFAULT:
  265. default:
  266. return XCB_NONE; /* XCB_NONE is xcb's way of saying "don't change the cursor" */
  267. }
  268. bitmap = xcb_create_pixmap_from_bitmap_data(conn,
  269. win,
  270. curs_bits,
  271. curs_w,
  272. curs_h,
  273. 1,
  274. screen->white_pixel,
  275. screen->black_pixel,
  276. NULL);
  277. mask = xcb_create_pixmap_from_bitmap_data(conn,
  278. win,
  279. mask_bits,
  280. curs_w,
  281. curs_h,
  282. 1,
  283. screen->white_pixel,
  284. screen->black_pixel,
  285. NULL);
  286. cursor = xcb_generate_id(conn);
  287. xcb_create_cursor(conn,
  288. cursor,
  289. bitmap,
  290. mask,
  291. 65535, 65535, 65535,
  292. 0, 0, 0,
  293. 0, 0);
  294. xcb_free_pixmap(conn, bitmap);
  295. xcb_free_pixmap(conn, mask);
  296. return cursor;
  297. }
  298. static xcb_atom_t _NET_ACTIVE_WINDOW = XCB_NONE;
  299. void _init_net_active_window(xcb_connection_t *conn) {
  300. if (_NET_ACTIVE_WINDOW != XCB_NONE) {
  301. /* already initialized */
  302. return;
  303. }
  304. xcb_generic_error_t *err;
  305. xcb_intern_atom_reply_t *atom_reply = xcb_intern_atom_reply(
  306. conn,
  307. xcb_intern_atom(conn, 0, strlen("_NET_ACTIVE_WINDOW"), "_NET_ACTIVE_WINDOW"),
  308. &err);
  309. if (atom_reply == NULL) {
  310. fprintf(stderr, "X11 Error %d\n", err->error_code);
  311. free(err);
  312. return;
  313. }
  314. _NET_ACTIVE_WINDOW = atom_reply->atom;
  315. free(atom_reply);
  316. }
  317. xcb_window_t find_focused_window(xcb_connection_t *conn, const xcb_window_t root) {
  318. xcb_window_t result = XCB_NONE;
  319. _init_net_active_window(conn);
  320. xcb_get_property_reply_t *prop_reply = xcb_get_property_reply(
  321. conn,
  322. xcb_get_property_unchecked(
  323. conn, false, root, _NET_ACTIVE_WINDOW, XCB_GET_PROPERTY_TYPE_ANY, 0, 1 /* word */),
  324. NULL);
  325. if (prop_reply == NULL) {
  326. goto out;
  327. }
  328. if (xcb_get_property_value_length(prop_reply) == 0) {
  329. goto out_prop;
  330. }
  331. if (prop_reply->type != XCB_ATOM_WINDOW) {
  332. goto out_prop;
  333. }
  334. result = *((xcb_window_t *)xcb_get_property_value(prop_reply));
  335. out_prop:
  336. free(prop_reply);
  337. out:
  338. return result;
  339. }
  340. void set_focused_window(xcb_connection_t *conn, const xcb_window_t root, const xcb_window_t window) {
  341. xcb_client_message_event_t ev;
  342. memset(&ev, '\0', sizeof(xcb_client_message_event_t));
  343. _init_net_active_window(conn);
  344. ev.response_type = XCB_CLIENT_MESSAGE;
  345. ev.window = window;
  346. ev.type = _NET_ACTIVE_WINDOW;
  347. ev.format = 32;
  348. ev.data.data32[0] = 2; /* 2 = pager */
  349. xcb_send_event(conn, false, root, XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (char *)&ev);
  350. xcb_flush(conn);
  351. }