• Main Page
  • Data Structures
  • Files
  • File List
  • Globals

src/xcb.c

Go to the documentation of this file.
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 root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
00096         xcb_window_t result = xcb_generate_id(conn);
00097         xcb_cursor_t cursor_id = xcb_generate_id(conn);
00098 
00099         /* If the window class is XCB_WINDOW_CLASS_INPUT_ONLY, depth has to be 0 */
00100         uint16_t depth = (window_class == XCB_WINDOW_CLASS_INPUT_ONLY ? 0 : XCB_COPY_FROM_PARENT);
00101 
00102         xcb_create_window(conn,
00103                           depth,
00104                           result, /* the window id */
00105                           root, /* parent == root */
00106                           dims.x, dims.y, dims.width, dims.height, /* dimensions */
00107                           0, /* border = 0, we draw our own */
00108                           window_class,
00109                           XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
00110                           mask,
00111                           values);
00112 
00113         /* Set the cursor */
00114         i3Font *cursor_font = load_font(conn, "cursor");
00115         xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id,
00116                         (cursor == -1 ? XCB_CURSOR_LEFT_PTR : cursor),
00117                         (cursor == -1 ? XCB_CURSOR_LEFT_PTR : cursor) + 1,
00118                         0, 0, 0, 65535, 65535, 65535);
00119         xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
00120         xcb_free_cursor(conn, cursor_id);
00121 
00122         /* Map the window (= make it visible) */
00123         if (map)
00124                 xcb_map_window(conn, result);
00125 
00126         return result;
00127 }
00128 
00129 /*
00130  * Changes a single value in the graphic context (so one doesn’t have to define an array of values)
00131  *
00132  */
00133 void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value) {
00134         xcb_change_gc(conn, gc, mask, &value);
00135 }
00136 
00137 /*
00138  * Draws a line from x,y to to_x,to_y using the given color
00139  *
00140  */
00141 void xcb_draw_line(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc,
00142                    uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t to_x, uint32_t to_y) {
00143         xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, colorpixel);
00144         xcb_point_t points[] = {{x, y}, {to_x, to_y}};
00145         xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, drawable, gc, 2, points);
00146 }
00147 
00148 /*
00149  * Draws a rectangle from x,y with width,height using the given color
00150  *
00151  */
00152 void xcb_draw_rect(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc,
00153                    uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
00154         xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, colorpixel);
00155         xcb_rectangle_t rect = {x, y, width, height};
00156         xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
00157 }
00158 
00159 /*
00160  * Generates a configure_notify event and sends it to the given window
00161  * Applications need this to think they’ve configured themselves correctly.
00162  * The truth is, however, that we will manage them.
00163  *
00164  */
00165 void fake_configure_notify(xcb_connection_t *conn, Rect r, xcb_window_t window) {
00166         xcb_configure_notify_event_t generated_event;
00167 
00168         generated_event.event = window;
00169         generated_event.window = window;
00170         generated_event.response_type = XCB_CONFIGURE_NOTIFY;
00171 
00172         generated_event.x = r.x;
00173         generated_event.y = r.y;
00174         generated_event.width = r.width;
00175         generated_event.height = r.height;
00176 
00177         generated_event.border_width = 0;
00178         generated_event.above_sibling = XCB_NONE;
00179         generated_event.override_redirect = false;
00180 
00181         xcb_send_event(conn, false, window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*)&generated_event);
00182         xcb_flush(conn);
00183 }
00184 
00185 /*
00186  * Generates a configure_notify_event with absolute coordinates (relative to the X root
00187  * window, not to the client’s frame) for the given client.
00188  *
00189  */
00190 void fake_absolute_configure_notify(xcb_connection_t *conn, Client *client) {
00191         Rect absolute;
00192 
00193         absolute.x = client->rect.x + client->child_rect.x;
00194         absolute.y = client->rect.y + client->child_rect.y;
00195         absolute.width = client->child_rect.width;
00196         absolute.height = client->child_rect.height;
00197 
00198         fake_configure_notify(conn, absolute, client->child);
00199 }
00200 
00201 /*
00202  * Finds out which modifier mask is the one for numlock, as the user may change this.
00203  *
00204  */
00205 void xcb_get_numlock_mask(xcb_connection_t *conn) {
00206         xcb_key_symbols_t *keysyms;
00207         xcb_get_modifier_mapping_cookie_t cookie;
00208         xcb_get_modifier_mapping_reply_t *reply;
00209         xcb_keycode_t *modmap;
00210         int mask, i;
00211         const int masks[8] = { XCB_MOD_MASK_SHIFT,
00212                                XCB_MOD_MASK_LOCK,
00213                                XCB_MOD_MASK_CONTROL,
00214                                XCB_MOD_MASK_1,
00215                                XCB_MOD_MASK_2,
00216                                XCB_MOD_MASK_3,
00217                                XCB_MOD_MASK_4,
00218                                XCB_MOD_MASK_5 };
00219 
00220         /* Request the modifier map */
00221         cookie = xcb_get_modifier_mapping_unchecked(conn);
00222 
00223         /* Get the keysymbols */
00224         keysyms = xcb_key_symbols_alloc(conn);
00225 
00226         if ((reply = xcb_get_modifier_mapping_reply(conn, cookie, NULL)) == NULL) {
00227                 xcb_key_symbols_free(keysyms);
00228                 return;
00229         }
00230 
00231         modmap = xcb_get_modifier_mapping_keycodes(reply);
00232 
00233         /* Get the keycode for numlock */
00234 #ifdef OLD_XCB_KEYSYMS_API
00235         xcb_keycode_t numlock = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK);
00236 #else
00237         /* For now, we only use the first keysymbol. */
00238         xcb_keycode_t *numlock_syms = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK);
00239         if (numlock_syms == NULL)
00240                 return;
00241         xcb_keycode_t numlock = *numlock_syms;
00242         free(numlock_syms);
00243 #endif
00244 
00245         /* Check all modifiers (Mod1-Mod5, Shift, Control, Lock) */
00246         for (mask = 0; mask < 8; mask++)
00247                 for (i = 0; i < reply->keycodes_per_modifier; i++)
00248                         if (modmap[(mask * reply->keycodes_per_modifier) + i] == numlock)
00249                                 xcb_numlock_mask = masks[mask];
00250 
00251         xcb_key_symbols_free(keysyms);
00252         free(reply);
00253 }
00254 
00255 /*
00256  * Raises the given window (typically client->frame) above all other windows
00257  *
00258  */
00259 void xcb_raise_window(xcb_connection_t *conn, xcb_window_t window) {
00260         uint32_t values[] = { XCB_STACK_MODE_ABOVE };
00261         xcb_configure_window(conn, window, XCB_CONFIG_WINDOW_STACK_MODE, values);
00262 }
00263 
00264 /*
00265  *
00266  * Prepares the given Cached_Pixmap for usage (checks whether the size of the
00267  * object this pixmap is related to (e.g. a window) has changed and re-creates
00268  * the pixmap if so).
00269  *
00270  */
00271 void cached_pixmap_prepare(xcb_connection_t *conn, struct Cached_Pixmap *pixmap) {
00272         DLOG("preparing pixmap\n");
00273 
00274         /* If the Rect did not change, the pixmap does not need to be recreated */
00275         if (memcmp(&(pixmap->rect), pixmap->referred_rect, sizeof(Rect)) == 0)
00276                 return;
00277 
00278         memcpy(&(pixmap->rect), pixmap->referred_rect, sizeof(Rect));
00279 
00280         if (pixmap->id == 0 || pixmap->gc == 0) {
00281                 DLOG("Creating new pixmap...\n");
00282                 pixmap->id = xcb_generate_id(conn);
00283                 pixmap->gc = xcb_generate_id(conn);
00284         } else {
00285                 DLOG("Re-creating this pixmap...\n");
00286                 xcb_free_gc(conn, pixmap->gc);
00287                 xcb_free_pixmap(conn, pixmap->id);
00288         }
00289 
00290         xcb_create_pixmap(conn, root_depth, pixmap->id,
00291                           pixmap->referred_drawable, pixmap->rect.width, pixmap->rect.height);
00292 
00293         xcb_create_gc(conn, pixmap->gc, pixmap->id, 0, 0);
00294 }
00295 
00296 /*
00297  * Query the width of the given text (16-bit characters, UCS) with given real
00298  * length (amount of glyphs) using the given font.
00299  *
00300  */
00301 int predict_text_width(xcb_connection_t *conn, const char *font_pattern, char *text, int length) {
00302         i3Font *font = load_font(conn, font_pattern);
00303 
00304         xcb_query_text_extents_cookie_t cookie;
00305         xcb_query_text_extents_reply_t *reply;
00306         xcb_generic_error_t *error;
00307         int width;
00308 
00309         cookie = xcb_query_text_extents(conn, font->id, length, (xcb_char2b_t*)text);
00310         if ((reply = xcb_query_text_extents_reply(conn, cookie, &error)) == NULL) {
00311                 ELOG("Could not get text extents (X error code %d)\n",
00312                      error->error_code);
00313                 /* We return the rather safe guess of 7 pixels, because a
00314                  * rendering error is better than a crash. Plus, the user will
00315                  * see the error in his log. */
00316                 return 7;
00317         }
00318 
00319         width = reply->overall_width;
00320         free(reply);
00321         return width;
00322 }
00323 
00324 /*
00325  * Configures the given window to have the size/position specified by given rect
00326  *
00327  */
00328 void xcb_set_window_rect(xcb_connection_t *conn, xcb_window_t window, Rect r) {
00329         xcb_configure_window(conn, window,
00330                              XCB_CONFIG_WINDOW_X |
00331                              XCB_CONFIG_WINDOW_Y |
00332                              XCB_CONFIG_WINDOW_WIDTH |
00333                              XCB_CONFIG_WINDOW_HEIGHT,
00334                              &(r.x));
00335 }

Generated on Mon Aug 22 2011 for i3 by  doxygen 1.7.1