]> git.sur5r.net Git - i3/i3/blobdiff - src/x.c
cmdparse: s/direction/split_direction to be more clear
[i3/i3] / src / x.c
diff --git a/src/x.c b/src/x.c
index 0fcb74ee5245d03c1755272da3d0636db6693527..0228cd23100e802cfc82c9bf5ff660177bb8d018 100644 (file)
--- a/src/x.c
+++ b/src/x.c
@@ -5,7 +5,7 @@
 #include "all.h"
 
 /* Stores the X11 window ID of the currently focused window */
-static xcb_window_t focused_id = XCB_NONE;
+xcb_window_t focused_id = XCB_NONE;
 
 /*
  * Describes the X11 state we may modify (map state, position, window stack).
@@ -162,6 +162,8 @@ void x_con_kill(Con *con) {
     con_state *state;
 
     xcb_destroy_window(conn, con->frame);
+    xcb_free_pixmap(conn, con->pixmap);
+    xcb_free_gc(conn, con->pm_gc);
     state = state_for_frame(con->frame);
     CIRCLEQ_REMOVE(&state_head, state, state);
     CIRCLEQ_REMOVE(&old_state_head, state, old_state);
@@ -199,11 +201,16 @@ bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
  * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
  *
  */
-void x_window_kill(xcb_window_t window) {
+void x_window_kill(xcb_window_t window, kill_window_t kill_window) {
     /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
     if (!window_supports_protocol(window, A_WM_DELETE_WINDOW)) {
-        LOG("Killing window the hard way\n");
-        xcb_kill_client(conn, window);
+        if (kill_window == KILL_WINDOW) {
+            LOG("Killing specific window 0x%08x\n", window);
+            xcb_destroy_window(conn, window);
+        } else {
+            LOG("Killing the X11 client which owns window 0x%08x\n", window);
+            xcb_kill_client(conn, window);
+        }
         return;
     }
 
@@ -228,7 +235,7 @@ void x_window_kill(xcb_window_t window) {
  *
  */
 void x_draw_decoration(Con *con) {
-    const Con *parent = con->parent;
+    Con *parent = con->parent;
     /* This code needs to run for:
      *  • leaf containers
      *  • non-leaf containers which are in a stacked/tabbed container
@@ -268,50 +275,67 @@ void x_draw_decoration(Con *con) {
     Rect *w = &(con->window_rect);
     p->con_rect = (struct width_height){ r->width, r->height };
     p->con_window_rect = (struct width_height){ w->width, w->height };
-    p->con_deco_rect = (struct width_height){ con->deco_rect.width, con->deco_rect.height };
+    p->con_deco_rect = con->deco_rect;
     p->background = config.client.background;
     p->con_is_leaf = con_is_leaf(con);
     p->font = config.font.id;
 
     if (con->deco_render_params != NULL &&
         (con->window == NULL || !con->window->name_x_changed) &&
-        !con->parent->pixmap_recreated &&
+        !parent->pixmap_recreated &&
+        !con->pixmap_recreated &&
         memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
-        DLOG("CACHE HIT, copying existing pixmaps\n");
+        DLOG("CACHE HIT, not re-rendering\n");
         free(p);
-        goto copy_pixmaps;
+        return;
     }
 
     DLOG("CACHE MISS\n");
+    Con *next = con;
+    while ((next = TAILQ_NEXT(next, nodes))) {
+        DLOG("Also invalidating cache of %p\n", next);
+        FREE(next->deco_render_params);
+    }
+
     FREE(con->deco_render_params);
     con->deco_render_params = p;
 
     if (con->window != NULL && con->window->name_x_changed)
         con->window->name_x_changed = false;
 
-    con->parent->pixmap_recreated = false;
+    parent->pixmap_recreated = false;
+    con->pixmap_recreated = false;
+
+    /* If the con is in fullscreen mode, the decoration height we work with is set to 0 */
+    Rect deco_rect = con->deco_rect;
+    if (con_get_fullscreen_con(parent) == con)
+        deco_rect.height = 0;
 
     /* 2: draw the client.background, but only for the parts around the client_rect */
-    xcb_rectangle_t background[] = {
-        /* top area */
-        { 0, con->deco_rect.height, r->width, w->y },
-        /* bottom area */
-        { 0, (w->y + w->height), r->width, r->height - (w->y + w->height) },
-        /* right area */
-        { w->width, con->deco_rect.height, r->width - (w->x + w->width), r->height }
-    };
+    if (con->window != NULL) {
+        xcb_rectangle_t background[] = {
+            /* top area */
+            { 0, 0, r->width, w->y },
+            /* bottom area */
+            { 0, (w->y + w->height), r->width, r->height - (w->y + w->height) },
+            /* left area */
+            { 0, 0, w->x, r->height },
+            /* right area */
+            { w->x + w->width, 0, r->width - (w->x + w->width), r->height }
+        };
 #if 0
-    for (int i = 0; i < 3; i++)
-        DLOG("rect is (%d, %d) with %d x %d\n",
-                background[i].x,
-                background[i].y,
-                background[i].width,
-                background[i].height
-            );
+        for (int i = 0; i < 4; i++)
+            DLOG("rect is (%d, %d) with %d x %d\n",
+                    background[i].x,
+                    background[i].y,
+                    background[i].width,
+                    background[i].height
+                );
 #endif
 
-    xcb_change_gc_single(conn, con->pm_gc, XCB_GC_FOREGROUND, config.client.background);
-    xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, sizeof(background) / sizeof(xcb_rectangle_t), background);
+        xcb_change_gc_single(conn, con->pm_gc, XCB_GC_FOREGROUND, config.client.background);
+        xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, sizeof(background) / sizeof(xcb_rectangle_t), background);
+    }
 
     /* 3: draw a rectangle in border color around the client */
     if (p->border_style != BS_NONE && p->con_is_leaf) {
@@ -344,16 +368,13 @@ void x_draw_decoration(Con *con) {
      * decoration. */
     if (p->border_style != BS_NORMAL) {
         DLOG("border style not BS_NORMAL, aborting rendering of decoration\n");
-        goto copy_pixmaps;
+        goto update_pixmaps;
     }
 
     /* 4: paint the bar */
     xcb_change_gc_single(conn, parent->pm_gc, XCB_GC_FOREGROUND, p->color->background);
-            free(xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL));
     xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
-            free(xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL));
     xcb_poly_fill_rectangle(conn, parent->pixmap, parent->pm_gc, 1, &drect);
-            free(xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL));
 
     /* 5: draw the two lines in border color */
     xcb_draw_line(conn, parent->pixmap, parent->pm_gc, p->color->border,
@@ -361,13 +382,11 @@ void x_draw_decoration(Con *con) {
             con->deco_rect.y, /* y */
             con->deco_rect.x + con->deco_rect.width, /* to_x */
             con->deco_rect.y); /* to_y */
-            free(xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL));
     xcb_draw_line(conn, parent->pixmap, parent->pm_gc, p->color->border,
             con->deco_rect.x, /* x */
             con->deco_rect.y + con->deco_rect.height - 1, /* y */
             con->deco_rect.x + con->deco_rect.width, /* to_x */
             con->deco_rect.y + con->deco_rect.height - 1); /* to_y */
-            free(xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL));
 
     /* 6: draw the title */
     uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
@@ -389,12 +408,12 @@ void x_draw_decoration(Con *con) {
             "another container"
         );
 
-        goto copy_pixmaps;
+        goto update_pixmaps;
     }
 
     int indent_level = 0,
         indent_mult = 0;
-    Con *il_parent = con->parent;
+    Con *il_parent = parent;
     if (il_parent->layout != L_STACKED) {
         while (1) {
             DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
@@ -430,9 +449,29 @@ void x_draw_decoration(Con *con) {
             win->name_x
         );
 
-copy_pixmaps:
-    xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
-    xcb_copy_area(conn, parent->pixmap, parent->frame, parent->pm_gc, 0, 0, 0, 0, parent->rect.width, parent->rect.height);
+update_pixmaps:
+    xcb_clear_area(conn, false, con->frame, 0, 0, con->rect.width, con->rect.height);
+    xcb_clear_area(conn, false, parent->frame, 0, 0, parent->rect.width, parent->rect.height);
+}
+
+/*
+ * Recursively calls x_draw_decoration. This cannot be done in x_push_node
+ * because x_push_node uses focus order to recurse (see the comment above)
+ * while drawing the decoration needs to happen in the actual order.
+ *
+ */
+static void x_deco_recurse(Con *con) {
+    Con *current;
+
+    TAILQ_FOREACH(current, &(con->nodes_head), nodes)
+        x_deco_recurse(current);
+
+    TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
+        x_deco_recurse(current);
+
+    if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
+        con->mapped)
+        x_draw_decoration(con);
 }
 
 /*
@@ -441,12 +480,12 @@ copy_pixmaps:
  * It recursively traverses all children of the given node.
  *
  */
-static void x_push_node(Con *con) {
+void x_push_node(Con *con) {
     Con *current;
     con_state *state;
     Rect rect = con->rect;
 
-    DLOG("Pushing changes for node %p / %s\n", con, con->name);
+    //DLOG("Pushing changes for node %p / %s\n", con, con->name);
     state = state_for_frame(con->frame);
 
     if (state->name != NULL) {
@@ -470,7 +509,7 @@ static void x_push_node(Con *con) {
         }
         rect.height = max_y + max_height;
         if (rect.height == 0) {
-            DLOG("Unmapping container because it does not contain anything atm.\n");
+            DLOG("Unmapping container %p because it does not contain anything.\n", con);
             con->mapped = false;
         }
     }
@@ -505,14 +544,20 @@ static void x_push_node(Con *con) {
     bool fake_notify = false;
     /* set new position if rect changed */
     if (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0) {
-        DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
-        xcb_set_window_rect(conn, con->frame, rect);
+        /* We first create the new pixmap, then render to it, set it as the
+         * background and only afterwards change the window size. This reduces
+         * flickering. */
 
         /* As the pixmap only depends on the size and not on the position, it
-         * is enough to check if width/height have changed */
-        if (state->rect.width != rect.width ||
-            state->rect.height != rect.height) {
-            DLOG("CACHE: creating new pixmap\n");
+         * is enough to check if width/height have changed. Also, we don’t
+         * create a pixmap at all when the window is actually not visible
+         * (height == 0). */
+        if (rect.height > 0 &&
+            (state->rect.width != rect.width ||
+            state->rect.height != rect.height)) {
+            DLOG("CACHE: creating new pixmap for con %p (old: %d x %d, new: %d x %d)\n",
+                    con, state->rect.width, state->rect.height,
+                    rect.width, rect.height);
             if (con->pixmap == 0) {
                 con->pixmap = xcb_generate_id(conn);
                 con->pm_gc = xcb_generate_id(conn);
@@ -521,11 +566,22 @@ static void x_push_node(Con *con) {
                 xcb_free_gc(conn, con->pm_gc);
             }
             xcb_create_pixmap(conn, root_depth, con->pixmap, con->frame, rect.width, rect.height);
-                free(xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL));
             xcb_create_gc(conn, con->pm_gc, con->pixmap, 0, 0);
-                free(xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL));
+
             con->pixmap_recreated = true;
+
+            /* Render the decoration now to make the correct decoration visible
+             * from the very first moment. Later calls will be cached, so this
+             * doesn’t hurt performance. */
+            x_deco_recurse(con);
+
+            uint32_t values[] = { con->pixmap };
+            xcb_change_window_attributes(conn, con->frame, XCB_CW_BACK_PIXMAP, values);
         }
+
+        DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
+        xcb_set_window_rect(conn, con->frame, rect);
+
         memcpy(&(state->rect), &rect, sizeof(Rect));
         fake_notify = true;
     }
@@ -560,14 +616,14 @@ static void x_push_node(Con *con) {
             cookie = xcb_map_window(conn, con->window->id);
             DLOG("mapping child window (serial %d)\n", cookie.sequence);
             /* Ignore enter_notifies which are generated when mapping */
-            add_ignore_event(cookie.sequence);
+            add_ignore_event(cookie.sequence, 0);
             state->child_mapped = true;
         }
 
         cookie = xcb_map_window(conn, con->frame);
         DLOG("mapping container (serial %d)\n", cookie.sequence);
         /* Ignore enter_notifies which are generated when mapping */
-        add_ignore_event(cookie.sequence);
+        add_ignore_event(cookie.sequence, 0);
         state->mapped = con->mapped;
     }
 
@@ -576,15 +632,11 @@ static void x_push_node(Con *con) {
         fake_absolute_configure_notify(con);
     }
 
-    /* handle all children and floating windows of this node */
-    TAILQ_FOREACH(current, &(con->nodes_head), nodes)
-        x_push_node(current);
-
-    TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
+    /* Handle all children and floating windows of this node. We recurse
+     * in focus order to display the focused client in a stack first when
+     * switching workspaces (reduces flickering). */
+    TAILQ_FOREACH(current, &(con->focus_head), focused)
         x_push_node(current);
-
-    if (con->type != CT_ROOT && con->type != CT_OUTPUT)
-        x_draw_decoration(con);
 }
 
 /*
@@ -600,7 +652,7 @@ static void x_push_node_unmaps(Con *con) {
     Con *current;
     con_state *state;
 
-    DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
+    //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
     state = state_for_frame(con->frame);
 
     /* map/unmap if map state changed, also ensure that the child window
@@ -626,7 +678,7 @@ static void x_push_node_unmaps(Con *con) {
             DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame, con->ignore_unmap);
         }
         /* Ignore enter_notifies which are generated when unmapping */
-        add_ignore_event(cookie.sequence);
+        add_ignore_event(cookie.sequence, 0);
         state->mapped = con->mapped;
     }
 
@@ -653,16 +705,16 @@ void x_push_changes(Con *con) {
     con_state *state;
 
     DLOG("-- PUSHING WINDOW STACK --\n");
-    DLOG("Disabling EnterNotify\n");
+    //DLOG("Disabling EnterNotify\n");
     uint32_t values[1] = { XCB_NONE };
     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
         xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
     }
-    DLOG("Done, EnterNotify disabled\n");
+    //DLOG("Done, EnterNotify disabled\n");
     bool order_changed = false;
     /* X11 correctly represents the stack if we push it from bottom to top */
     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
-        DLOG("stack: 0x%08x\n", state->id);
+        //DLOG("stack: 0x%08x\n", state->id);
         con_state *prev = CIRCLEQ_PREV(state, state);
         con_state *old_prev = CIRCLEQ_PREV(state, old_state);
         if (prev != old_prev)
@@ -678,15 +730,16 @@ void x_push_changes(Con *con) {
         }
         state->initial = false;
     }
-    DLOG("Re-enabling EnterNotify\n");
+    //DLOG("Re-enabling EnterNotify\n");
     values[0] = FRAME_EVENT_MASK;
     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
         xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
     }
-    DLOG("Done, EnterNotify re-enabled\n");
+    //DLOG("Done, EnterNotify re-enabled\n");
 
     DLOG("\n\n PUSHING CHANGES\n\n");
     x_push_node(con);
+    x_deco_recurse(con);
 
     xcb_window_t to_focus = focused->frame;
     if (focused->window != NULL)
@@ -700,7 +753,18 @@ void x_push_changes(Con *con) {
             focused_id = XCB_NONE;
         } else {
             DLOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
+            /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
+             * no focus change events for our own focus changes. We only want
+             * these generated by the clients. */
+            if (focused->window != NULL) {
+                values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
+                xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
+            }
             xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
+            if (focused->window != NULL) {
+                values[0] = CHILD_EVENT_MASK;
+                xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
+            }
 
             if (focused->window != NULL &&
                 focused->window->needs_take_focus) {
@@ -728,9 +792,11 @@ void x_push_changes(Con *con) {
         CIRCLEQ_REMOVE(&old_state_head, state, old_state);
         CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
     }
-    CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
-        DLOG("old stack: 0x%08x\n", state->id);
-    }
+    //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
+    //    DLOG("old stack: 0x%08x\n", state->id);
+    //}
+
+    xcb_flush(conn);
 }
 
 /*
@@ -741,7 +807,7 @@ void x_push_changes(Con *con) {
 void x_raise_con(Con *con) {
     con_state *state;
     state = state_for_frame(con->frame);
-    DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
+    //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
 
     CIRCLEQ_REMOVE(&state_head, state, state);
     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
@@ -771,8 +837,8 @@ void x_set_name(Con *con, const char *name) {
  */
 void x_set_i3_atoms() {
     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
-                        (config.ipc_socket_path != NULL ? strlen(config.ipc_socket_path) : 0),
-                        config.ipc_socket_path);
+                        (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
+                        current_socketpath);
     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
                         strlen(current_configpath), current_configpath);
 }