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.

431 lines
14 KiB

12 years ago
12 years ago
12 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. xcb_change_property(conn,
  151. XCB_PROP_MODE_REPLACE,
  152. win,
  153. XCB_ATOM_WM_CLASS,
  154. XCB_ATOM_STRING,
  155. 8,
  156. 2 * (strlen("i3lock") + 1),
  157. "i3lock\0i3lock\0");
  158. /* Map the window (= make it visible) */
  159. xcb_map_window(conn, win);
  160. /* Raise window (put it on top) */
  161. values[0] = XCB_STACK_MODE_ABOVE;
  162. xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
  163. /* Ensure that the window is created and set up before returning */
  164. xcb_aux_sync(conn);
  165. return win;
  166. }
  167. /*
  168. * Repeatedly tries to grab pointer and keyboard (up to the specified number of
  169. * tries).
  170. *
  171. * Returns true if the grab succeeded, false if not.
  172. *
  173. */
  174. bool grab_pointer_and_keyboard(xcb_connection_t *conn, xcb_screen_t *screen, xcb_cursor_t cursor, int tries) {
  175. xcb_grab_pointer_cookie_t pcookie;
  176. xcb_grab_pointer_reply_t *preply;
  177. xcb_grab_keyboard_cookie_t kcookie;
  178. xcb_grab_keyboard_reply_t *kreply;
  179. const suseconds_t screen_redraw_timeout = 100000; /* 100ms */
  180. /* Using few variables to trigger a redraw_screen() if too many tries */
  181. bool redrawn = false;
  182. struct timeval start;
  183. if (gettimeofday(&start, NULL) == -1) {
  184. err(EXIT_FAILURE, "gettimeofday");
  185. }
  186. while (tries-- > 0) {
  187. pcookie = xcb_grab_pointer(
  188. conn,
  189. false, /* get all pointer events specified by the following mask */
  190. screen->root, /* grab the root window */
  191. XCB_NONE, /* which events to let through */
  192. XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
  193. XCB_GRAB_MODE_ASYNC, /* keyboard mode */
  194. XCB_NONE, /* confine_to = in which window should the cursor stay */
  195. cursor, /* we change the cursor to whatever the user wanted */
  196. XCB_CURRENT_TIME);
  197. if ((preply = xcb_grab_pointer_reply(conn, pcookie, NULL)) &&
  198. preply->status == XCB_GRAB_STATUS_SUCCESS) {
  199. free(preply);
  200. break;
  201. }
  202. /* In case the grab failed, we still need to free the reply */
  203. free(preply);
  204. /* Make this quite a bit slower */
  205. usleep(50);
  206. struct timeval now;
  207. if (gettimeofday(&now, NULL) == -1) {
  208. err(EXIT_FAILURE, "gettimeofday");
  209. }
  210. struct timeval elapsed;
  211. timersub(&now, &start, &elapsed);
  212. if (!redrawn &&
  213. (tries % 100) == 0 &&
  214. elapsed.tv_usec >= screen_redraw_timeout) {
  215. redraw_screen();
  216. redrawn = true;
  217. }
  218. }
  219. while (tries-- > 0) {
  220. kcookie = xcb_grab_keyboard(
  221. conn,
  222. true, /* report events */
  223. screen->root, /* grab the root window */
  224. XCB_CURRENT_TIME,
  225. XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */
  226. XCB_GRAB_MODE_ASYNC);
  227. if ((kreply = xcb_grab_keyboard_reply(conn, kcookie, NULL)) &&
  228. kreply->status == XCB_GRAB_STATUS_SUCCESS) {
  229. free(kreply);
  230. break;
  231. }
  232. /* In case the grab failed, we still need to free the reply */
  233. free(kreply);
  234. /* Make this quite a bit slower */
  235. usleep(50);
  236. struct timeval now;
  237. if (gettimeofday(&now, NULL) == -1) {
  238. err(EXIT_FAILURE, "gettimeofday");
  239. }
  240. struct timeval elapsed;
  241. timersub(&now, &start, &elapsed);
  242. /* Trigger a screen redraw if 100ms elapsed */
  243. if (!redrawn &&
  244. (tries % 100) == 0 &&
  245. elapsed.tv_usec >= screen_redraw_timeout) {
  246. redraw_screen();
  247. redrawn = true;
  248. }
  249. }
  250. return (tries > 0);
  251. }
  252. xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_window_t win, int choice) {
  253. xcb_pixmap_t bitmap;
  254. xcb_pixmap_t mask;
  255. xcb_cursor_t cursor;
  256. unsigned char *curs_bits;
  257. unsigned char *mask_bits;
  258. int curs_w, curs_h;
  259. switch (choice) {
  260. case CURS_NONE:
  261. curs_bits = curs_invisible_bits;
  262. mask_bits = curs_invisible_bits;
  263. curs_w = curs_invisible_width;
  264. curs_h = curs_invisible_height;
  265. break;
  266. case CURS_WIN:
  267. curs_bits = curs_windows_bits;
  268. mask_bits = mask_windows_bits;
  269. curs_w = curs_windows_width;
  270. curs_h = curs_windows_height;
  271. break;
  272. case CURS_DEFAULT:
  273. default:
  274. return XCB_NONE; /* XCB_NONE is xcb's way of saying "don't change the cursor" */
  275. }
  276. bitmap = xcb_create_pixmap_from_bitmap_data(conn,
  277. win,
  278. curs_bits,
  279. curs_w,
  280. curs_h,
  281. 1,
  282. screen->white_pixel,
  283. screen->black_pixel,
  284. NULL);
  285. mask = xcb_create_pixmap_from_bitmap_data(conn,
  286. win,
  287. mask_bits,
  288. curs_w,
  289. curs_h,
  290. 1,
  291. screen->white_pixel,
  292. screen->black_pixel,
  293. NULL);
  294. cursor = xcb_generate_id(conn);
  295. xcb_create_cursor(conn,
  296. cursor,
  297. bitmap,
  298. mask,
  299. 65535, 65535, 65535,
  300. 0, 0, 0,
  301. 0, 0);
  302. xcb_free_pixmap(conn, bitmap);
  303. xcb_free_pixmap(conn, mask);
  304. return cursor;
  305. }
  306. static xcb_atom_t _NET_ACTIVE_WINDOW = XCB_NONE;
  307. void _init_net_active_window(xcb_connection_t *conn) {
  308. if (_NET_ACTIVE_WINDOW != XCB_NONE) {
  309. /* already initialized */
  310. return;
  311. }
  312. xcb_generic_error_t *err;
  313. xcb_intern_atom_reply_t *atom_reply = xcb_intern_atom_reply(
  314. conn,
  315. xcb_intern_atom(conn, 0, strlen("_NET_ACTIVE_WINDOW"), "_NET_ACTIVE_WINDOW"),
  316. &err);
  317. if (atom_reply == NULL) {
  318. fprintf(stderr, "X11 Error %d\n", err->error_code);
  319. free(err);
  320. return;
  321. }
  322. _NET_ACTIVE_WINDOW = atom_reply->atom;
  323. free(atom_reply);
  324. }
  325. xcb_window_t find_focused_window(xcb_connection_t *conn, const xcb_window_t root) {
  326. xcb_window_t result = XCB_NONE;
  327. _init_net_active_window(conn);
  328. xcb_get_property_reply_t *prop_reply = xcb_get_property_reply(
  329. conn,
  330. xcb_get_property_unchecked(
  331. conn, false, root, _NET_ACTIVE_WINDOW, XCB_GET_PROPERTY_TYPE_ANY, 0, 1 /* word */),
  332. NULL);
  333. if (prop_reply == NULL) {
  334. goto out;
  335. }
  336. if (xcb_get_property_value_length(prop_reply) == 0) {
  337. goto out_prop;
  338. }
  339. if (prop_reply->type != XCB_ATOM_WINDOW) {
  340. goto out_prop;
  341. }
  342. result = *((xcb_window_t *)xcb_get_property_value(prop_reply));
  343. out_prop:
  344. free(prop_reply);
  345. out:
  346. return result;
  347. }
  348. void set_focused_window(xcb_connection_t *conn, const xcb_window_t root, const xcb_window_t window) {
  349. xcb_client_message_event_t ev;
  350. memset(&ev, '\0', sizeof(xcb_client_message_event_t));
  351. _init_net_active_window(conn);
  352. ev.response_type = XCB_CLIENT_MESSAGE;
  353. ev.window = window;
  354. ev.type = _NET_ACTIVE_WINDOW;
  355. ev.format = 32;
  356. ev.data.data32[0] = 2; /* 2 = pager */
  357. xcb_send_event(conn, false, root, XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (char *)&ev);
  358. xcb_flush(conn);
  359. }