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.

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