]> git.sur5r.net Git - i3/i3/blobdiff - src/xcb.c
Only send WM_TAKE_FOCUS when the client supports it in the protocols atom
[i3/i3] / src / xcb.c
index 4b400ae5613bb3d8cec03427f78790379ef6333b..7382cc615efe91f718890c812e2b9e38e91a86f3 100644 (file)
--- a/src/xcb.c
+++ b/src/xcb.c
@@ -17,40 +17,50 @@ TAILQ_HEAD(cached_fonts_head, Font) cached_fonts = TAILQ_HEAD_INITIALIZER(cached
 unsigned int xcb_numlock_mask;
 
 /*
- * Loads a font for usage, getting its height. This function is used very often, so it
- * maintains a cache.
+ * Loads a font for usage, also getting its height. If fallback is true,
+ * i3 loads 'fixed' or '-misc-*' if the font cannot be found instead of
+ * exiting.
  *
  */
-i3Font *load_font(xcb_connection_t *conn, const char *pattern) {
-    /* Check if we got the font cached */
-    i3Font *font;
-    TAILQ_FOREACH(font, &cached_fonts, fonts)
-        if (strcmp(font->pattern, pattern) == 0)
-            return font;
-
-    i3Font *new = smalloc(sizeof(i3Font));
+i3Font load_font(const char *pattern, bool fallback) {
+    i3Font new;
     xcb_void_cookie_t font_cookie;
     xcb_list_fonts_with_info_cookie_t info_cookie;
 
     /* Send all our requests first */
-    new->id = xcb_generate_id(conn);
-    font_cookie = xcb_open_font_checked(conn, new->id, strlen(pattern), pattern);
+    new.id = xcb_generate_id(conn);
+    font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
     info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
 
-    check_error(conn, font_cookie, "Could not open font");
+    /* Check for errors. If errors, fall back to default font. */
+    xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
+
+    /* If we fail to open font, fall back to 'fixed'. If opening 'fixed' fails fall back to '-misc-*' */
+    if (error != NULL) {
+        ELOG("Could not open font %s (X error %d). Reverting to backup font.\n", pattern, error->error_code);
+        pattern = "fixed";
+        font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
+        info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
+
+        /* Check if we managed to open 'fixed' */
+        xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
+
+        /* Fall back to '-misc-*' if opening 'fixed' fails. */
+        if (error != NULL) {
+            ELOG("Could not open fallback font '%s', trying with '-misc-*'\n",pattern);
+            pattern = "-misc-*";
+            font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
+            info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
+
+            check_error(conn, font_cookie, "Could open neither requested font nor fallback (fixed or -misc-*");
+        }
+    }
 
     /* Get information (height/name) for this font */
     xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
     exit_if_null(reply, "Could not load font \"%s\"\n", pattern);
 
-    if (asprintf(&(new->name), "%.*s", xcb_list_fonts_with_info_name_length(reply),
-                                       xcb_list_fonts_with_info_name(reply)) == -1)
-        die("asprintf() failed\n");
-    new->pattern = sstrdup(pattern);
-    new->height = reply->font_ascent + reply->font_descent;
-
-    /* Insert into cache */
-    TAILQ_INSERT_TAIL(&cached_fonts, new, fonts);
+    new.height = reply->font_ascent + reply->font_descent;
 
     return new;
 }
@@ -82,7 +92,6 @@ uint32_t get_colorpixel(char *hex) {
  */
 xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_class,
         enum xcursor_cursor_t cursor, bool map, uint32_t mask, uint32_t *values) {
-    xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
     xcb_window_t result = xcb_generate_id(conn);
     xcb_cursor_t cursor_id = xcb_generate_id(conn);
 
@@ -106,9 +115,9 @@ xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_cl
         values[0] = xcursor_get_cursor(cursor);
         xcb_change_window_attributes(conn, result, mask, values);
     } else {
-        i3Font *cursor_font = load_font(conn, "cursor");
+        i3Font cursor_font = load_font("cursor", false);
         int xcb_cursor = xcursor_get_xcb_cursor(cursor);
-        xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id,
+        xcb_create_glyph_cursor(conn, cursor_id, cursor_font.id, cursor_font.id,
                 xcb_cursor, xcb_cursor + 1, 0, 0, 0, 65535, 65535, 65535);
         xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
         xcb_free_cursor(conn, cursor_id);
@@ -192,9 +201,31 @@ void fake_absolute_configure_notify(Con *con) {
     absolute.width = con->window_rect.width;
     absolute.height = con->window_rect.height;
 
+    DLOG("fake rect = (%d, %d, %d, %d)\n", absolute.x, absolute.y, absolute.width, absolute.height);
+
     fake_configure_notify(conn, absolute, con->window->id);
 }
 
+/*
+ * Sends the WM_TAKE_FOCUS ClientMessage to the given window
+ *
+ */
+void send_take_focus(xcb_window_t window) {
+    xcb_client_message_event_t ev;
+
+    memset(&ev, 0, sizeof(xcb_client_message_event_t));
+
+    ev.response_type = XCB_CLIENT_MESSAGE;
+    ev.window = window;
+    ev.type = A_WM_PROTOCOLS;
+    ev.format = 32;
+    ev.data.data32[0] = A_WM_TAKE_FOCUS;
+    ev.data.data32[1] = XCB_CURRENT_TIME;
+
+    DLOG("Sending WM_TAKE_FOCUS to the client\n");
+    xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
+}
+
 /*
  * Finds out which modifier mask is the one for numlock, as the user may change this.
  *
@@ -295,15 +326,13 @@ void cached_pixmap_prepare(xcb_connection_t *conn, struct Cached_Pixmap *pixmap)
  * length (amount of glyphs) using the given font.
  *
  */
-int predict_text_width(xcb_connection_t *conn, const char *font_pattern, char *text, int length) {
-    i3Font *font = load_font(conn, font_pattern);
-
+int predict_text_width(char *text, int length) {
     xcb_query_text_extents_cookie_t cookie;
     xcb_query_text_extents_reply_t *reply;
     xcb_generic_error_t *error;
     int width;
 
-    cookie = xcb_query_text_extents(conn, font->id, length, (xcb_char2b_t*)text);
+    cookie = xcb_query_text_extents(conn, config.font.id, length, (xcb_char2b_t*)text);
     if ((reply = xcb_query_text_extents_reply(conn, cookie, &error)) == NULL) {
         ELOG("Could not get text extents (X error code %d)\n",
              error->error_code);
@@ -346,7 +375,7 @@ bool xcb_reply_contains_atom(xcb_get_property_reply_t *prop, xcb_atom_t atom) {
     if ((atoms = xcb_get_property_value(prop)) == NULL)
         return false;
 
-    for (int i = 0; i < xcb_get_property_value_length(prop); i++)
+    for (int i = 0; i < xcb_get_property_value_length(prop) / (prop->format / 8); i++)
         if (atoms[i] == atom)
             return true;