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.

275 lines
9.1 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 <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <string.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. char *name = "i3lock";
  114. xcb_change_property(conn,
  115. XCB_PROP_MODE_REPLACE,
  116. win,
  117. XCB_ATOM_WM_NAME,
  118. XCB_ATOM_STRING,
  119. 8,
  120. strlen(name),
  121. name);
  122. /* Map the window (= make it visible) */
  123. xcb_map_window(conn, win);
  124. /* Raise window (put it on top) */
  125. values[0] = XCB_STACK_MODE_ABOVE;
  126. xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
  127. return win;
  128. }
  129. /*
  130. * Repeatedly tries to grab pointer and keyboard (up to 1000 times).
  131. *
  132. */
  133. void grab_pointer_and_keyboard(xcb_connection_t *conn, xcb_screen_t *screen, xcb_cursor_t cursor) {
  134. xcb_grab_pointer_cookie_t pcookie;
  135. xcb_grab_pointer_reply_t *preply;
  136. xcb_grab_keyboard_cookie_t kcookie;
  137. xcb_grab_keyboard_reply_t *kreply;
  138. int tries = 10000;
  139. while (tries-- > 0) {
  140. pcookie = xcb_grab_pointer(
  141. conn,
  142. false, /* get all pointer events specified by the following mask */
  143. screen->root, /* grab the root window */
  144. XCB_NONE, /* which events to let through */
  145. XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
  146. XCB_GRAB_MODE_ASYNC, /* keyboard mode */
  147. XCB_NONE, /* confine_to = in which window should the cursor stay */
  148. cursor, /* we change the cursor to whatever the user wanted */
  149. XCB_CURRENT_TIME);
  150. if ((preply = xcb_grab_pointer_reply(conn, pcookie, NULL)) &&
  151. preply->status == XCB_GRAB_STATUS_SUCCESS) {
  152. free(preply);
  153. break;
  154. }
  155. /* Make this quite a bit slower */
  156. usleep(50);
  157. }
  158. while (tries-- > 0) {
  159. kcookie = xcb_grab_keyboard(
  160. conn,
  161. true, /* report events */
  162. screen->root, /* grab the root window */
  163. XCB_CURRENT_TIME,
  164. XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */
  165. XCB_GRAB_MODE_ASYNC);
  166. if ((kreply = xcb_grab_keyboard_reply(conn, kcookie, NULL)) &&
  167. kreply->status == XCB_GRAB_STATUS_SUCCESS) {
  168. free(kreply);
  169. break;
  170. }
  171. /* Make this quite a bit slower */
  172. usleep(50);
  173. }
  174. if (tries <= 0)
  175. errx(EXIT_FAILURE, "Cannot grab pointer/keyboard");
  176. }
  177. xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_window_t win, int choice) {
  178. xcb_pixmap_t bitmap;
  179. xcb_pixmap_t mask;
  180. xcb_cursor_t cursor;
  181. unsigned char *curs_bits;
  182. unsigned char *mask_bits;
  183. int curs_w, curs_h;
  184. switch (choice) {
  185. case CURS_NONE:
  186. curs_bits = curs_invisible_bits;
  187. mask_bits = curs_invisible_bits;
  188. curs_w = curs_invisible_width;
  189. curs_h = curs_invisible_height;
  190. break;
  191. case CURS_WIN:
  192. curs_bits = curs_windows_bits;
  193. mask_bits = mask_windows_bits;
  194. curs_w = curs_windows_width;
  195. curs_h = curs_windows_height;
  196. break;
  197. case CURS_DEFAULT:
  198. default:
  199. return XCB_NONE; /* XCB_NONE is xcb's way of saying "don't change the cursor" */
  200. }
  201. bitmap = xcb_create_pixmap_from_bitmap_data(conn,
  202. win,
  203. curs_bits,
  204. curs_w,
  205. curs_h,
  206. 1,
  207. screen->white_pixel,
  208. screen->black_pixel,
  209. NULL);
  210. mask = xcb_create_pixmap_from_bitmap_data(conn,
  211. win,
  212. mask_bits,
  213. curs_w,
  214. curs_h,
  215. 1,
  216. screen->white_pixel,
  217. screen->black_pixel,
  218. NULL);
  219. cursor = xcb_generate_id(conn);
  220. xcb_create_cursor(conn,
  221. cursor,
  222. bitmap,
  223. mask,
  224. 65535, 65535, 65535,
  225. 0, 0, 0,
  226. 0, 0);
  227. xcb_free_pixmap(conn, bitmap);
  228. xcb_free_pixmap(conn, mask);
  229. return cursor;
  230. }