]> git.sur5r.net Git - i3/i3/blob - src/xcb.c
Bugfix: Calculation of width in fake_absoute_configure_notify was wrong for windows...
[i3/i3] / src / xcb.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * xcb.c: Helper functions for easier usage of XCB
11  *
12  */
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include <xcb/xcb.h>
19 #include <xcb/xcb_keysyms.h>
20
21 #include "util.h"
22 #include "xcb.h"
23
24 TAILQ_HEAD(cached_fonts_head, Font) cached_fonts = TAILQ_HEAD_INITIALIZER(cached_fonts);
25 SLIST_HEAD(colorpixel_head, Colorpixel) colorpixels;
26 unsigned int xcb_numlock_mask;
27
28 /*
29  * Loads a font for usage, getting its height. This function is used very often, so it
30  * maintains a cache.
31  *
32  */
33 i3Font *load_font(xcb_connection_t *conn, const char *pattern) {
34         /* Check if we got the font cached */
35         i3Font *font;
36         TAILQ_FOREACH(font, &cached_fonts, fonts)
37                 if (strcmp(font->pattern, pattern) == 0)
38                         return font;
39
40         i3Font *new = smalloc(sizeof(i3Font));
41         xcb_void_cookie_t font_cookie;
42         xcb_list_fonts_with_info_cookie_t info_cookie;
43
44         /* Send all our requests first */
45         new->id = xcb_generate_id(conn);
46         font_cookie = xcb_open_font_checked(conn, new->id, strlen(pattern), pattern);
47         info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
48
49         check_error(conn, font_cookie, "Could not open font");
50
51         /* Get information (height/name) for this font */
52         xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
53         exit_if_null(reply, "Could not load font \"%s\"\n", pattern);
54
55         if (asprintf(&(new->name), "%.*s", xcb_list_fonts_with_info_name_length(reply),
56                                            xcb_list_fonts_with_info_name(reply)) == -1)
57                 die("asprintf() failed\n");
58         new->pattern = sstrdup(pattern);
59         new->height = reply->font_ascent + reply->font_descent;
60
61         /* Insert into cache */
62         TAILQ_INSERT_TAIL(&cached_fonts, new, fonts);
63
64         return new;
65 }
66
67 /*
68  * Returns the colorpixel to use for the given hex color (think of HTML).
69  *
70  * The hex_color has to start with #, for example #FF00FF.
71  *
72  * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
73  * This has to be done by the caller.
74  *
75  */
76 uint32_t get_colorpixel(xcb_connection_t *conn, char *hex) {
77         /* Lookup this colorpixel in the cache */
78         struct Colorpixel *colorpixel;
79         SLIST_FOREACH(colorpixel, &(colorpixels), colorpixels)
80                 if (strcmp(colorpixel->hex, hex) == 0)
81                         return colorpixel->pixel;
82
83         #define RGB_8_TO_16(i) (65535 * ((i) & 0xFF) / 255)
84         char strgroups[3][3] = {{hex[1], hex[2], '\0'},
85                                 {hex[3], hex[4], '\0'},
86                                 {hex[5], hex[6], '\0'}};
87         int rgb16[3] = {RGB_8_TO_16(strtol(strgroups[0], NULL, 16)),
88                         RGB_8_TO_16(strtol(strgroups[1], NULL, 16)),
89                         RGB_8_TO_16(strtol(strgroups[2], NULL, 16))};
90
91         xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
92         xcb_alloc_color_reply_t *reply;
93         
94         reply = xcb_alloc_color_reply(conn, xcb_alloc_color(conn, root_screen->default_colormap,
95                                                             rgb16[0], rgb16[1], rgb16[2]), NULL);
96
97         if (!reply) {
98                 LOG("Could not allocate color\n");
99                 exit(1);
100         }
101
102         uint32_t pixel = reply->pixel;
103         free(reply);
104
105         /* Store the result in the cache */
106         struct Colorpixel *cache_pixel = scalloc(sizeof(struct Colorpixel));
107         cache_pixel->hex = sstrdup(hex);
108         cache_pixel->pixel = pixel;
109
110         SLIST_INSERT_HEAD(&(colorpixels), cache_pixel, colorpixels);
111
112         return pixel;
113 }
114
115 /*
116  * Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking
117  * for errors.
118  *
119  */
120 xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_class, int cursor,
121                            uint32_t mask, uint32_t *values) {
122         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
123         xcb_window_t result = xcb_generate_id(conn);
124         xcb_cursor_t cursor_id = xcb_generate_id(conn);
125
126         /* If the window class is XCB_WINDOW_CLASS_INPUT_ONLY, depth has to be 0 */
127         uint16_t depth = (window_class == XCB_WINDOW_CLASS_INPUT_ONLY ? 0 : XCB_COPY_FROM_PARENT);
128
129         /* Use the default cursor (left pointer) */
130         if (cursor > -1) {
131                 i3Font *cursor_font = load_font(conn, "cursor");
132                 xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id,
133                                 XCB_CURSOR_LEFT_PTR, XCB_CURSOR_LEFT_PTR + 1,
134                                 0, 0, 0, 65535, 65535, 65535);
135         }
136
137         xcb_create_window(conn,
138                           depth,
139                           result, /* the window id */
140                           root, /* parent == root */
141                           dims.x, dims.y, dims.width, dims.height, /* dimensions */
142                           0, /* border = 0, we draw our own */
143                           window_class,
144                           XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
145                           mask,
146                           values);
147
148         if (cursor > -1)
149                 xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
150
151         /* Map the window (= make it visible) */
152         xcb_map_window(conn, result);
153
154         return result;
155 }
156
157 /*
158  * Changes a single value in the graphic context (so one doesn’t have to define an array of values)
159  *
160  */
161 void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value) {
162         xcb_change_gc(conn, gc, mask, &value);
163 }
164
165 /*
166  * Draws a line from x,y to to_x,to_y using the given color
167  *
168  */
169 void xcb_draw_line(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc,
170                    uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t to_x, uint32_t to_y) {
171         xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, colorpixel);
172         xcb_point_t points[] = {{x, y}, {to_x, to_y}};
173         xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, drawable, gc, 2, points);
174 }
175
176 /*
177  * Draws a rectangle from x,y with width,height using the given color
178  *
179  */
180 void xcb_draw_rect(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc,
181                    uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
182         xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, colorpixel);
183         xcb_rectangle_t rect = {x, y, width, height};
184         xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
185 }
186
187 /*
188  * Generates a configure_notify event and sends it to the given window
189  * Applications need this to think they’ve configured themselves correctly.
190  * The truth is, however, that we will manage them.
191  *
192  */
193 void fake_configure_notify(xcb_connection_t *conn, Rect r, xcb_window_t window) {
194         xcb_configure_notify_event_t generated_event;
195
196         generated_event.event = window;
197         generated_event.window = window;
198         generated_event.response_type = XCB_CONFIGURE_NOTIFY;
199
200         generated_event.x = r.x;
201         generated_event.y = r.y;
202         generated_event.width = r.width;
203         generated_event.height = r.height;
204
205         generated_event.border_width = 0;
206         generated_event.above_sibling = XCB_NONE;
207         generated_event.override_redirect = false;
208
209         xcb_send_event(conn, false, window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*)&generated_event);
210         xcb_flush(conn);
211
212         LOG("Told the client it is at %dx%d with %dx%d\n", r.x, r.y, r.width, r.height);
213 }
214
215 /*
216  * Generates a configure_notify_event with absolute coordinates (relative to the X root
217  * window, not to the client’s frame) for the given client.
218  *
219  */
220 void fake_absolute_configure_notify(xcb_connection_t *conn, Client *client) {
221         Rect absolute;
222
223         absolute.x = client->rect.x;
224         absolute.y = client->rect.y;
225         absolute.width = client->rect.width - (2 * client->child_rect.x);
226         absolute.height = client->rect.height - (2 * client->child_rect.y);
227
228         fake_configure_notify(conn, absolute, client->child);
229 }
230
231 /*
232  * Finds out which modifier mask is the one for numlock, as the user may change this.
233  *
234  */
235 void xcb_get_numlock_mask(xcb_connection_t *conn) {
236         xcb_key_symbols_t *keysyms;
237         xcb_get_modifier_mapping_cookie_t cookie;
238         xcb_get_modifier_mapping_reply_t *reply;
239         xcb_keycode_t *modmap, numlock;
240         int mask, i;
241         const int masks[8] = { XCB_MOD_MASK_SHIFT,
242                                XCB_MOD_MASK_LOCK,
243                                XCB_MOD_MASK_CONTROL,
244                                XCB_MOD_MASK_1,
245                                XCB_MOD_MASK_2,
246                                XCB_MOD_MASK_3,
247                                XCB_MOD_MASK_4,
248                                XCB_MOD_MASK_5 };
249
250         /* Request the modifier map */
251         cookie = xcb_get_modifier_mapping_unchecked(conn);
252
253         /* Get the keysymbols */
254         keysyms = xcb_key_symbols_alloc(conn);
255
256         if ((reply = xcb_get_modifier_mapping_reply(conn, cookie, NULL)) == NULL) {
257                 xcb_key_symbols_free(keysyms);
258                 return;
259         }
260
261         modmap = xcb_get_modifier_mapping_keycodes(reply);
262
263         /* Get the keycode for numlock */
264         numlock = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK);
265
266         /* Check all modifiers (Mod1-Mod5, Shift, Control, Lock) */
267         for (mask = 0; mask < sizeof(masks); mask++)
268                 for (i = 0; i < reply->keycodes_per_modifier; i++)
269                         if (modmap[(mask * reply->keycodes_per_modifier) + i] == numlock)
270                                 xcb_numlock_mask = masks[mask];
271
272         xcb_key_symbols_free(keysyms);
273         free(reply);
274 }