4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * xcb.c: Helper functions for easier usage of XCB
19 #include <xcb/xcb_keysyms.h>
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;
29 * Loads a font for usage, getting its height. This function is used very often, so it
33 i3Font *load_font(xcb_connection_t *conn, const char *pattern) {
34 /* Check if we got the font cached */
36 TAILQ_FOREACH(font, &cached_fonts, fonts)
37 if (strcmp(font->pattern, pattern) == 0)
40 i3Font *new = smalloc(sizeof(i3Font));
41 xcb_void_cookie_t font_cookie;
42 xcb_list_fonts_with_info_cookie_t info_cookie;
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);
49 check_error(conn, font_cookie, "Could not open font");
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);
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;
61 /* Insert into cache */
62 TAILQ_INSERT_TAIL(&cached_fonts, new, fonts);
68 * Returns the colorpixel to use for the given hex color (think of HTML).
70 * The hex_color has to start with #, for example #FF00FF.
72 * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
73 * This has to be done by the caller.
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;
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))};
91 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
92 xcb_alloc_color_reply_t *reply;
94 reply = xcb_alloc_color_reply(conn, xcb_alloc_color(conn, root_screen->default_colormap,
95 rgb16[0], rgb16[1], rgb16[2]), NULL);
98 LOG("Could not allocate color\n");
102 uint32_t pixel = reply->pixel;
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;
110 SLIST_INSERT_HEAD(&(colorpixels), cache_pixel, colorpixels);
116 * Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking
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);
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);
129 /* Use the default cursor (left pointer) */
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);
137 xcb_create_window(conn,
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 */
144 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
149 xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
151 /* Map the window (= make it visible) */
152 xcb_map_window(conn, result);
158 * Changes a single value in the graphic context (so one doesn’t have to define an array of values)
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);
166 * Draws a line from x,y to to_x,to_y using the given color
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);
177 * Draws a rectangle from x,y with width,height using the given color
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);
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.
193 void fake_configure_notify(xcb_connection_t *conn, Rect r, xcb_window_t window) {
194 xcb_configure_notify_event_t generated_event;
196 generated_event.event = window;
197 generated_event.window = window;
198 generated_event.response_type = XCB_CONFIGURE_NOTIFY;
200 generated_event.x = r.x;
201 generated_event.y = r.y;
202 generated_event.width = r.width;
203 generated_event.height = r.height;
205 generated_event.border_width = 0;
206 generated_event.above_sibling = XCB_NONE;
207 generated_event.override_redirect = false;
209 xcb_send_event(conn, false, window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*)&generated_event);
212 LOG("Told the client it is at %dx%d with %dx%d\n", r.x, r.y, r.width, r.height);
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.
220 void fake_absolute_configure_notify(xcb_connection_t *conn, Client *client) {
223 absolute.x = client->rect.x + client->child_rect.x;
224 absolute.y = client->rect.y + client->child_rect.y;
225 absolute.width = client->rect.width - (2 * client->child_rect.x);
226 absolute.height = client->rect.height - client->child_rect.y - 1;
228 fake_configure_notify(conn, absolute, client->child);
232 * Finds out which modifier mask is the one for numlock, as the user may change this.
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;
241 const int masks[8] = { XCB_MOD_MASK_SHIFT,
243 XCB_MOD_MASK_CONTROL,
250 /* Request the modifier map */
251 cookie = xcb_get_modifier_mapping_unchecked(conn);
253 /* Get the keysymbols */
254 keysyms = xcb_key_symbols_alloc(conn);
256 if ((reply = xcb_get_modifier_mapping_reply(conn, cookie, NULL)) == NULL) {
257 xcb_key_symbols_free(keysyms);
261 modmap = xcb_get_modifier_mapping_keycodes(reply);
263 /* Get the keycode for numlock */
264 #ifdef OLD_XCB_KEYSYMS_API
265 xcb_keycode_t numlock = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK);
267 /* For now, we only use the first keysymbol. */
268 xcb_keycode_t *numlock_syms = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK);
269 xcb_keycode_t numlock = *numlock_syms;
272 /* Check all modifiers (Mod1-Mod5, Shift, Control, Lock) */
273 for (mask = 0; mask < sizeof(masks); mask++)
274 for (i = 0; i < reply->keycodes_per_modifier; i++)
275 if (modmap[(mask * reply->keycodes_per_modifier) + i] == numlock)
276 xcb_numlock_mask = masks[mask];
278 xcb_key_symbols_free(keysyms);