]> git.sur5r.net Git - i3/i3lock/blob - xcb.c
Port the pointer-code to xcb
[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/xcb_image.h>
13 #include <xcb/dpms.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdbool.h>
17 #include <unistd.h>
18 #include <assert.h>
19 #include <err.h>
20
21 #include "cursors.h"
22
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))};
30
31     return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
32 }       
33
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;
38
39     for (depth_iter = xcb_screen_allowed_depths_iterator(screen);
40          depth_iter.rem;
41          xcb_depth_next(&depth_iter)) {
42
43         for (visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
44              visual_iter.rem;
45              xcb_visualtype_next(&visual_iter)) {
46             if (screen->root_visual != visual_iter.data->visual_id)
47                 continue;
48
49             visual_type = visual_iter.data;
50             return visual_type;
51         }
52     }
53
54     return NULL;
55 }
56
57 xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, char *color) {
58     uint32_t mask = 0;
59     uint32_t values[3];
60     xcb_window_t win = xcb_generate_id(conn);
61
62     mask |= XCB_CW_BACK_PIXEL;
63     values[0] = get_colorpixel(color);
64
65     mask |= XCB_CW_OVERRIDE_REDIRECT;
66     values[1] = 1;
67
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;
73
74     xcb_create_window(conn,
75                       24,
76                       win, /* the window id */
77                       scr->root, /* parent == root */
78                       0, 0,
79                       scr->width_in_pixels,
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 */
84                       mask,
85                       values);
86
87     /* Map the window (= make it visible) */
88     xcb_map_window(conn, win);
89
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);
93
94     return win;
95 }
96
97 /*
98  * Returns the mask for Mode_switch (to be used for looking up keysymbols by
99  * keycode).
100  *
101  */
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)
107         return 0;
108
109     modmap_r = xcb_get_modifier_mapping_reply(conn, xcb_get_modifier_mapping(conn), NULL);
110     modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
111
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++) {
116                 if (*ktest != kc)
117                     continue;
118
119                 free(modeswitchcodes);
120                 free(modmap_r);
121                 return (1 << i);
122             }
123         }
124
125     return 0;
126 }
127
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);
131     xcb_flush(conn);
132 }
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
142     xcb_grab_keyboard_cookie_t kcookie;
143     xcb_grab_keyboard_reply_t *kreply;
144
145     int tries = 10000;
146
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         );
159
160         if ((preply = xcb_grab_pointer_reply(conn, pcookie, NULL)) &&
161             preply->status == XCB_GRAB_STATUS_SUCCESS) {
162             free(preply);
163             break;
164         }
165
166         /* Make this quite a bit slower */
167         usleep(50);
168     }
169
170     if (cursor != XCB_NONE)
171         xcb_free_cursor(conn, cursor);
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         );
182
183         if ((kreply = xcb_grab_keyboard_reply(conn, kcookie, NULL)) &&
184             kreply->status == XCB_GRAB_STATUS_SUCCESS) {
185             free(kreply);
186             break;
187         }
188
189         /* Make this quite a bit slower */
190         usleep(50);
191     }
192
193     if (tries <= 0)
194         errx(EXIT_FAILURE, "Cannot grab pointer/keyboard");
195 }
196
197 xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_window_t win, int choice) {
198     xcb_pixmap_t bitmap;
199     xcb_pixmap_t mask;
200     xcb_cursor_t cursor;
201
202     unsigned char* curs_bits;
203     unsigned char* mask_bits;
204     int curs_w, curs_h;
205
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
224     bitmap = xcb_create_pixmap_from_bitmap_data(conn,
225                                                 win,
226                                                 curs_bits,
227                                                 curs_w,
228                                                 curs_h,
229                                                 1,
230                                                 screen->white_pixel,
231                                                 screen->black_pixel,
232                                                 NULL);
233
234     mask = xcb_create_pixmap_from_bitmap_data(conn,
235                                               win,
236                                               mask_bits,
237                                               curs_w,
238                                               curs_h,
239                                               1,
240                                               screen->white_pixel,
241                                               screen->black_pixel,
242                                               NULL);
243
244     cursor = xcb_generate_id(conn);
245
246     xcb_create_cursor(conn,
247                       cursor,
248                       bitmap,
249                       mask,
250                       65535,65535,65535,
251                       0,0,0,
252                       0,0);
253
254     xcb_free_pixmap(conn, bitmap);
255     xcb_free_pixmap(conn, mask);
256
257     return cursor;
258 }