]> git.sur5r.net Git - i3/i3lock/blob - xcb.c
grabbing: make the retry loop much slower (waits up to half a second)
[i3/i3lock] / xcb.c
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_keysyms.h>
12 #include <xcb/dpms.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <unistd.h>
17 #include <assert.h>
18 #include <err.h>
19
20 static uint32_t get_colorpixel(char *hex) {
21     char strgroups[3][3] = {{hex[0], hex[1], '\0'},
22                             {hex[2], hex[3], '\0'},
23                             {hex[4], hex[5], '\0'}};
24     uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
25                          (strtol(strgroups[1], NULL, 16)),
26                          (strtol(strgroups[2], NULL, 16))};
27
28     return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
29 }       
30
31 xcb_visualtype_t *get_root_visual_type(xcb_screen_t *screen) {
32     xcb_visualtype_t *visual_type = NULL;
33     xcb_depth_iterator_t depth_iter;
34     xcb_visualtype_iterator_t visual_iter;
35
36     for (depth_iter = xcb_screen_allowed_depths_iterator(screen);
37          depth_iter.rem;
38          xcb_depth_next(&depth_iter)) {
39
40         for (visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
41              visual_iter.rem;
42              xcb_visualtype_next(&visual_iter)) {
43             if (screen->root_visual != visual_iter.data->visual_id)
44                 continue;
45
46             visual_type = visual_iter.data;
47             return visual_type;
48         }
49     }
50
51     return NULL;
52 }
53
54 xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, char *color) {
55     uint32_t mask = 0;
56     uint32_t values[3];
57     xcb_window_t win = xcb_generate_id(conn);
58
59     mask |= XCB_CW_BACK_PIXEL;
60     values[0] = get_colorpixel(color);
61
62     mask |= XCB_CW_OVERRIDE_REDIRECT;
63     values[1] = 1;
64
65     mask |= XCB_CW_EVENT_MASK;
66     values[2] = XCB_EVENT_MASK_EXPOSURE |
67                 XCB_EVENT_MASK_KEY_PRESS |
68                 XCB_EVENT_MASK_KEY_RELEASE |
69                 XCB_EVENT_MASK_VISIBILITY_CHANGE;
70
71     xcb_create_window(conn,
72                       24,
73                       win, /* the window id */
74                       scr->root, /* parent == root */
75                       0, 0,
76                       scr->width_in_pixels,
77                       scr->height_in_pixels, /* dimensions */
78                       0, /* border = 0, we draw our own */
79                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
80                       XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
81                       mask,
82                       values);
83
84     /* Map the window (= make it visible) */
85     xcb_map_window(conn, win);
86
87     /* Raise window (put it on top) */
88     values[0] = XCB_STACK_MODE_ABOVE;
89     xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
90
91     return win;
92 }
93
94 /*
95  * Returns the mask for Mode_switch (to be used for looking up keysymbols by
96  * keycode).
97  *
98  */
99 uint32_t get_mod_mask(xcb_connection_t *conn, xcb_key_symbols_t *symbols, uint32_t keycode) {
100     xcb_get_modifier_mapping_reply_t *modmap_r;
101     xcb_keycode_t *modmap, kc;
102     xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(symbols, keycode);
103     if (modeswitchcodes == NULL)
104         return 0;
105
106     modmap_r = xcb_get_modifier_mapping_reply(conn, xcb_get_modifier_mapping(conn), NULL);
107     modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
108
109     for (int i = 0; i < 8; i++)
110         for (int j = 0; j < modmap_r->keycodes_per_modifier; j++) {
111             kc = modmap[i * modmap_r->keycodes_per_modifier + j];
112             for (xcb_keycode_t *ktest = modeswitchcodes; *ktest; ktest++) {
113                 if (*ktest != kc)
114                     continue;
115
116                 free(modeswitchcodes);
117                 free(modmap_r);
118                 return (1 << i);
119             }
120         }
121
122     return 0;
123 }
124
125 void dpms_turn_off_screen(xcb_connection_t *conn) {
126     xcb_dpms_enable(conn);
127     xcb_dpms_force_level(conn, XCB_DPMS_DPMS_MODE_OFF);
128     xcb_flush(conn);
129 }
130
131 /*
132  * Repeatedly tries to grab pointer and keyboard (up to 1000 times).
133  *
134  */
135 void grab_pointer_and_keyboard(xcb_connection_t *conn, xcb_screen_t *screen) {
136     xcb_grab_pointer_cookie_t pcookie;
137     xcb_grab_pointer_reply_t *preply;
138
139     xcb_grab_keyboard_cookie_t kcookie;
140     xcb_grab_keyboard_reply_t *kreply;
141
142     int tries = 10000;
143
144     while (tries-- > 0) {
145         pcookie = xcb_grab_pointer(
146             conn,
147             false,               /* get all pointer events specified by the following mask */
148             screen->root,        /* grab the root window */
149             XCB_NONE,            /* which events to let through */
150             XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
151             XCB_GRAB_MODE_ASYNC, /* keyboard mode */
152             XCB_NONE,            /* confine_to = in which window should the cursor stay */
153             XCB_NONE,            /* don’t display a special cursor */
154             XCB_CURRENT_TIME
155         );
156
157         if ((preply = xcb_grab_pointer_reply(conn, pcookie, NULL)) &&
158             preply->status == XCB_GRAB_STATUS_SUCCESS) {
159             free(preply);
160             break;
161         }
162
163         /* Make this quite a bit slower */
164         usleep(50);
165     }
166
167     while (tries-- > 0) {
168         kcookie = xcb_grab_keyboard(
169             conn,
170             true,                /* report events */
171             screen->root,        /* grab the root window */
172             XCB_CURRENT_TIME,
173             XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */
174             XCB_GRAB_MODE_ASYNC
175         );
176
177         if ((kreply = xcb_grab_keyboard_reply(conn, kcookie, NULL)) &&
178             kreply->status == XCB_GRAB_STATUS_SUCCESS) {
179             free(kreply);
180             break;
181         }
182
183         /* Make this quite a bit slower */
184         usleep(50);
185     }
186
187     if (tries <= 0)
188         errx(EXIT_FAILURE, "Cannot grab pointer/keyboard");
189 }