i3
|
00001 /* 00002 * vim:ts=8:expandtab 00003 * 00004 * i3 - an improved dynamic tiling window manager 00005 * 00006 * © 2009-2010 Michael Stapelberg and contributors 00007 * 00008 * See file LICENSE for license information. 00009 * 00010 * xcb.c: Helper functions for easier usage of XCB 00011 * 00012 */ 00013 #include <stdint.h> 00014 #include <stdio.h> 00015 #include <stdlib.h> 00016 #include <string.h> 00017 00018 #include <xcb/xcb.h> 00019 #include <xcb/xcb_keysyms.h> 00020 00021 #include "i3.h" 00022 #include "util.h" 00023 #include "xcb.h" 00024 #include "log.h" 00025 00026 TAILQ_HEAD(cached_fonts_head, Font) cached_fonts = TAILQ_HEAD_INITIALIZER(cached_fonts); 00027 unsigned int xcb_numlock_mask; 00028 00029 /* 00030 * Loads a font for usage, getting its height. This function is used very often, so it 00031 * maintains a cache. 00032 * 00033 */ 00034 i3Font *load_font(xcb_connection_t *conn, const char *pattern) { 00035 /* Check if we got the font cached */ 00036 i3Font *font; 00037 TAILQ_FOREACH(font, &cached_fonts, fonts) 00038 if (strcmp(font->pattern, pattern) == 0) 00039 return font; 00040 00041 i3Font *new = smalloc(sizeof(i3Font)); 00042 xcb_void_cookie_t font_cookie; 00043 xcb_list_fonts_with_info_cookie_t info_cookie; 00044 00045 /* Send all our requests first */ 00046 new->id = xcb_generate_id(conn); 00047 font_cookie = xcb_open_font_checked(conn, new->id, strlen(pattern), pattern); 00048 info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern); 00049 00050 check_error(conn, font_cookie, "Could not open font"); 00051 00052 /* Get information (height/name) for this font */ 00053 xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL); 00054 exit_if_null(reply, "Could not load font \"%s\"\n", pattern); 00055 00056 if (asprintf(&(new->name), "%.*s", xcb_list_fonts_with_info_name_length(reply), 00057 xcb_list_fonts_with_info_name(reply)) == -1) 00058 die("asprintf() failed\n"); 00059 new->pattern = sstrdup(pattern); 00060 new->height = reply->font_ascent + reply->font_descent; 00061 00062 /* Insert into cache */ 00063 TAILQ_INSERT_TAIL(&cached_fonts, new, fonts); 00064 00065 return new; 00066 } 00067 00068 /* 00069 * Returns the colorpixel to use for the given hex color (think of HTML). 00070 * 00071 * The hex_color has to start with #, for example #FF00FF. 00072 * 00073 * NOTE that get_colorpixel() does _NOT_ check the given color code for validity. 00074 * This has to be done by the caller. 00075 * 00076 */ 00077 uint32_t get_colorpixel(xcb_connection_t *conn, char *hex) { 00078 char strgroups[3][3] = {{hex[1], hex[2], '\0'}, 00079 {hex[3], hex[4], '\0'}, 00080 {hex[5], hex[6], '\0'}}; 00081 uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)), 00082 (strtol(strgroups[1], NULL, 16)), 00083 (strtol(strgroups[2], NULL, 16))}; 00084 00085 return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2]; 00086 } 00087 00088 /* 00089 * Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking 00090 * for errors. 00091 * 00092 */ 00093 xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_class, int cursor, 00094 bool map, uint32_t mask, uint32_t *values) { 00095 xcb_window_t result = xcb_generate_id(conn); 00096 xcb_cursor_t cursor_id = xcb_generate_id(conn); 00097 00098 /* If the window class is XCB_WINDOW_CLASS_INPUT_ONLY, depth has to be 0 */ 00099 uint16_t depth = (window_class == XCB_WINDOW_CLASS_INPUT_ONLY ? 0 : XCB_COPY_FROM_PARENT); 00100 00101 xcb_create_window(conn, 00102 depth, 00103 result, /* the window id */ 00104 root, /* parent == root */ 00105 dims.x, dims.y, dims.width, dims.height, /* dimensions */ 00106 0, /* border = 0, we draw our own */ 00107 window_class, 00108 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */ 00109 mask, 00110 values); 00111 00112 /* Set the cursor */ 00113 i3Font *cursor_font = load_font(conn, "cursor"); 00114 xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id, 00115 (cursor == -1 ? XCB_CURSOR_LEFT_PTR : cursor), 00116 (cursor == -1 ? XCB_CURSOR_LEFT_PTR : cursor) + 1, 00117 0, 0, 0, 65535, 65535, 65535); 00118 xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id); 00119 xcb_free_cursor(conn, cursor_id); 00120 00121 /* Map the window (= make it visible) */ 00122 if (map) 00123 xcb_map_window(conn, result); 00124 00125 return result; 00126 } 00127 00128 /* 00129 * Changes a single value in the graphic context (so one doesn’t have to define an array of values) 00130 * 00131 */ 00132 void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value) { 00133 xcb_change_gc(conn, gc, mask, &value); 00134 } 00135 00136 /* 00137 * Draws a line from x,y to to_x,to_y using the given color 00138 * 00139 */ 00140 void xcb_draw_line(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc, 00141 uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t to_x, uint32_t to_y) { 00142 xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, colorpixel); 00143 xcb_point_t points[] = {{x, y}, {to_x, to_y}}; 00144 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, drawable, gc, 2, points); 00145 } 00146 00147 /* 00148 * Draws a rectangle from x,y with width,height using the given color 00149 * 00150 */ 00151 void xcb_draw_rect(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc, 00152 uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t width, uint32_t height) { 00153 xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, colorpixel); 00154 xcb_rectangle_t rect = {x, y, width, height}; 00155 xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect); 00156 } 00157 00158 /* 00159 * Generates a configure_notify event and sends it to the given window 00160 * Applications need this to think they’ve configured themselves correctly. 00161 * The truth is, however, that we will manage them. 00162 * 00163 */ 00164 void fake_configure_notify(xcb_connection_t *conn, Rect r, xcb_window_t window) { 00165 xcb_configure_notify_event_t generated_event; 00166 00167 generated_event.event = window; 00168 generated_event.window = window; 00169 generated_event.response_type = XCB_CONFIGURE_NOTIFY; 00170 00171 generated_event.x = r.x; 00172 generated_event.y = r.y; 00173 generated_event.width = r.width; 00174 generated_event.height = r.height; 00175 00176 generated_event.border_width = 0; 00177 generated_event.above_sibling = XCB_NONE; 00178 generated_event.override_redirect = false; 00179 00180 xcb_send_event(conn, false, window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*)&generated_event); 00181 xcb_flush(conn); 00182 } 00183 00184 /* 00185 * Generates a configure_notify_event with absolute coordinates (relative to the X root 00186 * window, not to the client’s frame) for the given client. 00187 * 00188 */ 00189 void fake_absolute_configure_notify(xcb_connection_t *conn, Client *client) { 00190 Rect absolute; 00191 00192 absolute.x = client->rect.x + client->child_rect.x; 00193 absolute.y = client->rect.y + client->child_rect.y; 00194 absolute.width = client->child_rect.width; 00195 absolute.height = client->child_rect.height; 00196 00197 fake_configure_notify(conn, absolute, client->child); 00198 } 00199 00200 /* 00201 * Finds out which modifier mask is the one for numlock, as the user may change this. 00202 * 00203 */ 00204 void xcb_get_numlock_mask(xcb_connection_t *conn) { 00205 xcb_key_symbols_t *keysyms; 00206 xcb_get_modifier_mapping_cookie_t cookie; 00207 xcb_get_modifier_mapping_reply_t *reply; 00208 xcb_keycode_t *modmap; 00209 int mask, i; 00210 const int masks[8] = { XCB_MOD_MASK_SHIFT, 00211 XCB_MOD_MASK_LOCK, 00212 XCB_MOD_MASK_CONTROL, 00213 XCB_MOD_MASK_1, 00214 XCB_MOD_MASK_2, 00215 XCB_MOD_MASK_3, 00216 XCB_MOD_MASK_4, 00217 XCB_MOD_MASK_5 }; 00218 00219 /* Request the modifier map */ 00220 cookie = xcb_get_modifier_mapping_unchecked(conn); 00221 00222 /* Get the keysymbols */ 00223 keysyms = xcb_key_symbols_alloc(conn); 00224 00225 if ((reply = xcb_get_modifier_mapping_reply(conn, cookie, NULL)) == NULL) { 00226 xcb_key_symbols_free(keysyms); 00227 return; 00228 } 00229 00230 modmap = xcb_get_modifier_mapping_keycodes(reply); 00231 00232 /* Get the keycode for numlock */ 00233 #ifdef OLD_XCB_KEYSYMS_API 00234 xcb_keycode_t numlock = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK); 00235 #else 00236 /* For now, we only use the first keysymbol. */ 00237 xcb_keycode_t *numlock_syms = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK); 00238 if (numlock_syms == NULL) 00239 return; 00240 xcb_keycode_t numlock = *numlock_syms; 00241 free(numlock_syms); 00242 #endif 00243 00244 /* Check all modifiers (Mod1-Mod5, Shift, Control, Lock) */ 00245 for (mask = 0; mask < 8; mask++) 00246 for (i = 0; i < reply->keycodes_per_modifier; i++) 00247 if (modmap[(mask * reply->keycodes_per_modifier) + i] == numlock) 00248 xcb_numlock_mask = masks[mask]; 00249 00250 xcb_key_symbols_free(keysyms); 00251 free(reply); 00252 } 00253 00254 /* 00255 * Raises the given window (typically client->frame) above all other windows 00256 * 00257 */ 00258 void xcb_raise_window(xcb_connection_t *conn, xcb_window_t window) { 00259 uint32_t values[] = { XCB_STACK_MODE_ABOVE }; 00260 xcb_configure_window(conn, window, XCB_CONFIG_WINDOW_STACK_MODE, values); 00261 } 00262 00263 /* 00264 * 00265 * Prepares the given Cached_Pixmap for usage (checks whether the size of the 00266 * object this pixmap is related to (e.g. a window) has changed and re-creates 00267 * the pixmap if so). 00268 * 00269 */ 00270 void cached_pixmap_prepare(xcb_connection_t *conn, struct Cached_Pixmap *pixmap) { 00271 DLOG("preparing pixmap\n"); 00272 00273 /* If the Rect did not change, the pixmap does not need to be recreated */ 00274 if (memcmp(&(pixmap->rect), pixmap->referred_rect, sizeof(Rect)) == 0) 00275 return; 00276 00277 memcpy(&(pixmap->rect), pixmap->referred_rect, sizeof(Rect)); 00278 00279 if (pixmap->id == 0 || pixmap->gc == 0) { 00280 DLOG("Creating new pixmap...\n"); 00281 pixmap->id = xcb_generate_id(conn); 00282 pixmap->gc = xcb_generate_id(conn); 00283 } else { 00284 DLOG("Re-creating this pixmap...\n"); 00285 xcb_free_gc(conn, pixmap->gc); 00286 xcb_free_pixmap(conn, pixmap->id); 00287 } 00288 00289 xcb_create_pixmap(conn, root_depth, pixmap->id, 00290 pixmap->referred_drawable, pixmap->rect.width, pixmap->rect.height); 00291 00292 xcb_create_gc(conn, pixmap->gc, pixmap->id, 0, 0); 00293 } 00294 00295 /* 00296 * Query the width of the given text (16-bit characters, UCS) with given real 00297 * length (amount of glyphs) using the given font. 00298 * 00299 */ 00300 int predict_text_width(xcb_connection_t *conn, const char *font_pattern, char *text, int length) { 00301 i3Font *font = load_font(conn, font_pattern); 00302 00303 xcb_query_text_extents_cookie_t cookie; 00304 xcb_query_text_extents_reply_t *reply; 00305 xcb_generic_error_t *error; 00306 int width; 00307 00308 cookie = xcb_query_text_extents(conn, font->id, length, (xcb_char2b_t*)text); 00309 if ((reply = xcb_query_text_extents_reply(conn, cookie, &error)) == NULL) { 00310 ELOG("Could not get text extents (X error code %d)\n", 00311 error->error_code); 00312 /* We return the rather safe guess of 7 pixels, because a 00313 * rendering error is better than a crash. Plus, the user will 00314 * see the error in his log. */ 00315 return 7; 00316 } 00317 00318 width = reply->overall_width; 00319 free(reply); 00320 return width; 00321 } 00322 00323 /* 00324 * Configures the given window to have the size/position specified by given rect 00325 * 00326 */ 00327 void xcb_set_window_rect(xcb_connection_t *conn, xcb_window_t window, Rect r) { 00328 xcb_configure_window(conn, window, 00329 XCB_CONFIG_WINDOW_X | 00330 XCB_CONFIG_WINDOW_Y | 00331 XCB_CONFIG_WINDOW_WIDTH | 00332 XCB_CONFIG_WINDOW_HEIGHT, 00333 &(r.x)); 00334 }