]> git.sur5r.net Git - i3/i3/blob - src/xcb.c
Move get_colorpixel to libi3, use it everywhere else
[i3/i3] / src / xcb.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * xcb.c: Helper functions for easier usage of XCB
8  *
9  */
10 #include "all.h"
11
12 TAILQ_HEAD(cached_fonts_head, Font) cached_fonts = TAILQ_HEAD_INITIALIZER(cached_fonts);
13 unsigned int xcb_numlock_mask;
14
15 /*
16  * Loads a font for usage, also getting its height. If fallback is true,
17  * i3 loads 'fixed' or '-misc-*' if the font cannot be found instead of
18  * exiting.
19  *
20  */
21 i3Font load_font(const char *pattern, bool fallback) {
22     i3Font new;
23     xcb_void_cookie_t font_cookie;
24     xcb_list_fonts_with_info_cookie_t info_cookie;
25
26     /* Send all our requests first */
27     new.id = xcb_generate_id(conn);
28     font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
29     info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
30
31     /* Check for errors. If errors, fall back to default font. */
32     xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
33
34     /* If we fail to open font, fall back to 'fixed'. If opening 'fixed' fails fall back to '-misc-*' */
35     if (error != NULL) {
36         ELOG("Could not open font %s (X error %d). Reverting to backup font.\n", pattern, error->error_code);
37         pattern = "fixed";
38         font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
39         info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
40
41         /* Check if we managed to open 'fixed' */
42         xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
43
44         /* Fall back to '-misc-*' if opening 'fixed' fails. */
45         if (error != NULL) {
46             ELOG("Could not open fallback font '%s', trying with '-misc-*'\n",pattern);
47             pattern = "-misc-*";
48             font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
49             info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
50
51             check_error(conn, font_cookie, "Could open neither requested font nor fallback (fixed or -misc-*");
52         }
53     }
54
55     /* Get information (height/name) for this font */
56     xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
57     exit_if_null(reply, "Could not load font \"%s\"\n", pattern);
58
59     new.height = reply->font_ascent + reply->font_descent;
60
61     free(reply);
62
63     return new;
64 }
65
66 /*
67  * Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking
68  * for errors.
69  *
70  */
71 xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_class,
72         enum xcursor_cursor_t cursor, bool map, uint32_t mask, uint32_t *values) {
73     xcb_window_t result = xcb_generate_id(conn);
74
75     /* If the window class is XCB_WINDOW_CLASS_INPUT_ONLY, depth has to be 0 */
76     uint16_t depth = (window_class == XCB_WINDOW_CLASS_INPUT_ONLY ? 0 : XCB_COPY_FROM_PARENT);
77
78     xcb_create_window(conn,
79             depth,
80             result, /* the window id */
81             root, /* parent == root */
82             dims.x, dims.y, dims.width, dims.height, /* dimensions */
83             0, /* border = 0, we draw our own */
84             window_class,
85             XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
86             mask,
87             values);
88
89     /* Set the cursor */
90     if (xcursor_supported) {
91         mask = XCB_CW_CURSOR;
92         values[0] = xcursor_get_cursor(cursor);
93         xcb_change_window_attributes(conn, result, mask, values);
94     } else {
95         xcb_cursor_t cursor_id = xcb_generate_id(conn);
96         i3Font cursor_font = load_font("cursor", false);
97         int xcb_cursor = xcursor_get_xcb_cursor(cursor);
98         xcb_create_glyph_cursor(conn, cursor_id, cursor_font.id, cursor_font.id,
99                 xcb_cursor, xcb_cursor + 1, 0, 0, 0, 65535, 65535, 65535);
100         xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
101         xcb_free_cursor(conn, cursor_id);
102     }
103
104     /* Map the window (= make it visible) */
105     if (map)
106         xcb_map_window(conn, result);
107
108     return result;
109 }
110
111 /*
112  * Changes a single value in the graphic context (so one doesn’t have to define an array of values)
113  *
114  */
115 void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value) {
116     xcb_change_gc(conn, gc, mask, &value);
117 }
118
119 /*
120  * Draws a line from x,y to to_x,to_y using the given color
121  *
122  */
123 void xcb_draw_line(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc,
124                    uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t to_x, uint32_t to_y) {
125     xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, colorpixel);
126     xcb_point_t points[] = {{x, y}, {to_x, to_y}};
127     xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, drawable, gc, 2, points);
128 }
129
130 /*
131  * Draws a rectangle from x,y with width,height using the given color
132  *
133  */
134 void xcb_draw_rect(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc,
135                    uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
136     xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, colorpixel);
137     xcb_rectangle_t rect = {x, y, width, height};
138     xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
139 }
140
141 /*
142  * Generates a configure_notify_event with absolute coordinates (relative to the X root
143  * window, not to the client’s frame) for the given client.
144  *
145  */
146 void fake_absolute_configure_notify(Con *con) {
147     xcb_rectangle_t absolute;
148     if (con->window == NULL)
149         return;
150
151     absolute.x = con->rect.x + con->window_rect.x;
152     absolute.y = con->rect.y + con->window_rect.y;
153     absolute.width = con->window_rect.width;
154     absolute.height = con->window_rect.height;
155
156     DLOG("fake rect = (%d, %d, %d, %d)\n", absolute.x, absolute.y, absolute.width, absolute.height);
157
158     fake_configure_notify(conn, absolute, con->window->id, con->border_width);
159 }
160
161 /*
162  * Sends the WM_TAKE_FOCUS ClientMessage to the given window
163  *
164  */
165 void send_take_focus(xcb_window_t window) {
166     /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
167      * In order to properly initialize these bytes, we allocate 32 bytes even
168      * though we only need less for an xcb_configure_notify_event_t */
169     void *event = scalloc(32);
170     xcb_client_message_event_t *ev = event;
171
172     ev->response_type = XCB_CLIENT_MESSAGE;
173     ev->window = window;
174     ev->type = A_WM_PROTOCOLS;
175     ev->format = 32;
176     ev->data.data32[0] = A_WM_TAKE_FOCUS;
177     ev->data.data32[1] = XCB_CURRENT_TIME;
178
179     DLOG("Sending WM_TAKE_FOCUS to the client\n");
180     xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)ev);
181     free(event);
182 }
183
184 /*
185  * Finds out which modifier mask is the one for numlock, as the user may change this.
186  *
187  */
188 void xcb_get_numlock_mask(xcb_connection_t *conn) {
189     xcb_key_symbols_t *keysyms;
190     xcb_get_modifier_mapping_cookie_t cookie;
191     xcb_get_modifier_mapping_reply_t *reply;
192     xcb_keycode_t *modmap;
193     int mask, i;
194     const int masks[8] = { XCB_MOD_MASK_SHIFT,
195                            XCB_MOD_MASK_LOCK,
196                            XCB_MOD_MASK_CONTROL,
197                            XCB_MOD_MASK_1,
198                            XCB_MOD_MASK_2,
199                            XCB_MOD_MASK_3,
200                            XCB_MOD_MASK_4,
201                            XCB_MOD_MASK_5 };
202
203     /* Request the modifier map */
204     cookie = xcb_get_modifier_mapping(conn);
205
206     /* Get the keysymbols */
207     keysyms = xcb_key_symbols_alloc(conn);
208
209     if ((reply = xcb_get_modifier_mapping_reply(conn, cookie, NULL)) == NULL) {
210         xcb_key_symbols_free(keysyms);
211         return;
212     }
213
214     modmap = xcb_get_modifier_mapping_keycodes(reply);
215
216     /* Get the keycode for numlock */
217 #ifdef OLD_XCB_KEYSYMS_API
218     xcb_keycode_t numlock = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK);
219 #else
220     /* For now, we only use the first keysymbol. */
221     xcb_keycode_t *numlock_syms = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK);
222     if (numlock_syms == NULL)
223         return;
224     xcb_keycode_t numlock = *numlock_syms;
225     free(numlock_syms);
226 #endif
227
228     /* Check all modifiers (Mod1-Mod5, Shift, Control, Lock) */
229     for (mask = 0; mask < 8; mask++)
230         for (i = 0; i < reply->keycodes_per_modifier; i++)
231             if (modmap[(mask * reply->keycodes_per_modifier) + i] == numlock)
232                 xcb_numlock_mask = masks[mask];
233
234     xcb_key_symbols_free(keysyms);
235     free(reply);
236 }
237
238 /*
239  * Raises the given window (typically client->frame) above all other windows
240  *
241  */
242 void xcb_raise_window(xcb_connection_t *conn, xcb_window_t window) {
243     uint32_t values[] = { XCB_STACK_MODE_ABOVE };
244     xcb_configure_window(conn, window, XCB_CONFIG_WINDOW_STACK_MODE, values);
245 }
246
247 /*
248  * Query the width of the given text (16-bit characters, UCS) with given real
249  * length (amount of glyphs) using the given font.
250  *
251  */
252 int predict_text_width(char *text, int length) {
253     xcb_query_text_extents_cookie_t cookie;
254     xcb_query_text_extents_reply_t *reply;
255     xcb_generic_error_t *error;
256     int width;
257
258     cookie = xcb_query_text_extents(conn, config.font.id, length, (xcb_char2b_t*)text);
259     if ((reply = xcb_query_text_extents_reply(conn, cookie, &error)) == NULL) {
260         ELOG("Could not get text extents (X error code %d)\n",
261              error->error_code);
262         /* We return the rather safe guess of 7 pixels, because a
263          * rendering error is better than a crash. Plus, the user will
264          * see the error in his log. */
265         return 7;
266     }
267
268     width = reply->overall_width;
269     free(reply);
270     return width;
271 }
272
273 /*
274  * Configures the given window to have the size/position specified by given rect
275  *
276  */
277 void xcb_set_window_rect(xcb_connection_t *conn, xcb_window_t window, Rect r) {
278     xcb_void_cookie_t cookie;
279     cookie = xcb_configure_window(conn, window,
280                          XCB_CONFIG_WINDOW_X |
281                          XCB_CONFIG_WINDOW_Y |
282                          XCB_CONFIG_WINDOW_WIDTH |
283                          XCB_CONFIG_WINDOW_HEIGHT,
284                          &(r.x));
285     /* ignore events which are generated because we configured a window */
286     add_ignore_event(cookie.sequence, -1);
287 }
288
289 /*
290  * Returns true if the given reply contains the given atom.
291  *
292  */
293 bool xcb_reply_contains_atom(xcb_get_property_reply_t *prop, xcb_atom_t atom) {
294     if (prop == NULL || xcb_get_property_value_length(prop) == 0)
295         return false;
296
297     xcb_atom_t *atoms;
298     if ((atoms = xcb_get_property_value(prop)) == NULL)
299         return false;
300
301     for (int i = 0; i < xcb_get_property_value_length(prop) / (prop->format / 8); i++)
302         if (atoms[i] == atom)
303             return true;
304
305     return false;
306
307 }
308
309 /**
310  * Moves the mouse pointer into the middle of rect.
311  *
312  */
313 void xcb_warp_pointer_rect(xcb_connection_t *conn, Rect *rect) {
314     int mid_x = rect->x + (rect->width / 2);
315     int mid_y = rect->y + (rect->height / 2);
316
317     LOG("warp pointer to: %d %d\n", mid_x, mid_y);
318     xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
319 }
320
321 /*
322  * Set the cursor of the root window to the given cursor id.
323  * This function should only be used if xcursor_supported == false.
324  * Otherwise, use xcursor_set_root_cursor().
325  *
326  */
327 void xcb_set_root_cursor(int cursor) {
328     xcb_cursor_t cursor_id = xcb_generate_id(conn);
329     i3Font cursor_font = load_font("cursor", false);
330     int xcb_cursor = xcursor_get_xcb_cursor(cursor);
331     xcb_create_glyph_cursor(conn, cursor_id, cursor_font.id, cursor_font.id,
332             xcb_cursor, xcb_cursor + 1, 0, 0, 0, 65535, 65535, 65535);
333     xcb_change_window_attributes(conn, root, XCB_CW_CURSOR, &cursor_id);
334     xcb_free_cursor(conn, cursor_id);
335     xcb_flush(conn);
336 }