2 * vim:ts=4:sw=4:expandtab
4 * © 2010 Michael Stapelberg
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.
11 #include <xcb/xcb_keysyms.h>
12 #include <xcb/xcb_image.h>
23 static uint32_t get_colorpixel(char *hex) {
24 char strgroups[3][3] = {{hex[0], hex[1], '\0'},
25 {hex[2], hex[3], '\0'},
26 {hex[4], hex[5], '\0'}};
27 uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
28 (strtol(strgroups[1], NULL, 16)),
29 (strtol(strgroups[2], NULL, 16))};
31 return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
34 xcb_visualtype_t *get_root_visual_type(xcb_screen_t *screen) {
35 xcb_visualtype_t *visual_type = NULL;
36 xcb_depth_iterator_t depth_iter;
37 xcb_visualtype_iterator_t visual_iter;
39 for (depth_iter = xcb_screen_allowed_depths_iterator(screen);
41 xcb_depth_next(&depth_iter)) {
43 for (visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
45 xcb_visualtype_next(&visual_iter)) {
46 if (screen->root_visual != visual_iter.data->visual_id)
49 visual_type = visual_iter.data;
57 xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, char *color) {
60 xcb_window_t win = xcb_generate_id(conn);
62 mask |= XCB_CW_BACK_PIXEL;
63 values[0] = get_colorpixel(color);
65 mask |= XCB_CW_OVERRIDE_REDIRECT;
68 mask |= XCB_CW_EVENT_MASK;
69 values[2] = XCB_EVENT_MASK_EXPOSURE |
70 XCB_EVENT_MASK_KEY_PRESS |
71 XCB_EVENT_MASK_KEY_RELEASE |
72 XCB_EVENT_MASK_VISIBILITY_CHANGE;
74 xcb_create_window(conn,
76 win, /* the window id */
77 scr->root, /* parent == root */
80 scr->height_in_pixels, /* dimensions */
81 0, /* border = 0, we draw our own */
82 XCB_WINDOW_CLASS_INPUT_OUTPUT,
83 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
87 /* Map the window (= make it visible) */
88 xcb_map_window(conn, win);
90 /* Raise window (put it on top) */
91 values[0] = XCB_STACK_MODE_ABOVE;
92 xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
98 * Returns the mask for Mode_switch (to be used for looking up keysymbols by
102 uint32_t get_mod_mask(xcb_connection_t *conn, xcb_key_symbols_t *symbols, uint32_t keycode) {
103 xcb_get_modifier_mapping_reply_t *modmap_r;
104 xcb_keycode_t *modmap, kc;
105 xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(symbols, keycode);
106 if (modeswitchcodes == NULL)
109 modmap_r = xcb_get_modifier_mapping_reply(conn, xcb_get_modifier_mapping(conn), NULL);
110 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
112 for (int i = 0; i < 8; i++)
113 for (int j = 0; j < modmap_r->keycodes_per_modifier; j++) {
114 kc = modmap[i * modmap_r->keycodes_per_modifier + j];
115 for (xcb_keycode_t *ktest = modeswitchcodes; *ktest; ktest++) {
119 free(modeswitchcodes);
128 void dpms_turn_off_screen(xcb_connection_t *conn) {
129 xcb_dpms_enable(conn);
130 xcb_dpms_force_level(conn, XCB_DPMS_DPMS_MODE_OFF);
135 * Repeatedly tries to grab pointer and keyboard (up to 1000 times).
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;
142 xcb_grab_keyboard_cookie_t kcookie;
143 xcb_grab_keyboard_reply_t *kreply;
147 while (tries-- > 0) {
148 pcookie = xcb_grab_pointer(
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 */
160 if ((preply = xcb_grab_pointer_reply(conn, pcookie, NULL)) &&
161 preply->status == XCB_GRAB_STATUS_SUCCESS) {
166 /* Make this quite a bit slower */
170 if (cursor != XCB_NONE)
171 xcb_free_cursor(conn, cursor);
173 while (tries-- > 0) {
174 kcookie = xcb_grab_keyboard(
176 true, /* report events */
177 screen->root, /* grab the root window */
179 XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */
183 if ((kreply = xcb_grab_keyboard_reply(conn, kcookie, NULL)) &&
184 kreply->status == XCB_GRAB_STATUS_SUCCESS) {
189 /* Make this quite a bit slower */
194 errx(EXIT_FAILURE, "Cannot grab pointer/keyboard");
197 xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_window_t win, int choice) {
202 unsigned char* curs_bits;
203 unsigned char* mask_bits;
208 curs_bits = curs_invisible_bits;
209 mask_bits = curs_invisible_bits;
210 curs_w = curs_invisible_width;
211 curs_h = curs_invisible_height;
214 curs_bits = curs_windows_bits;
215 mask_bits = mask_windows_bits;
216 curs_w = curs_windows_width;
217 curs_h = curs_windows_height;
221 return XCB_NONE; /* XCB_NONE is xcb's way of saying "don't change the cursor" */
224 bitmap = xcb_create_pixmap_from_bitmap_data(conn,
234 mask = xcb_create_pixmap_from_bitmap_data(conn,
244 cursor = xcb_generate_id(conn);
246 xcb_create_cursor(conn,
254 xcb_free_pixmap(conn, bitmap);
255 xcb_free_pixmap(conn, mask);