]> git.sur5r.net Git - i3/i3/blobdiff - src/tree.c
Default to a file in /tmp for the restart state.
[i3/i3] / src / tree.c
index cf7f60dd14546e5403fdd185507279eb9d1ff432..1edbf73528881db73cd011d0ca9efeb2e0a28b8e 100644 (file)
@@ -13,8 +13,8 @@ struct all_cons_head all_cons = TAILQ_HEAD_INITIALIZER(all_cons);
  * Loads tree from ~/.i3/_restart.json (used for in-place restarts).
  *
  */
-bool tree_restore() {
-    char *globbed = glob_path("~/.i3/_restart.json");
+bool tree_restore(const char *path) {
+    char *globbed = resolve_tilde(path);
 
     if (!path_exists(globbed)) {
         LOG("%s does not exist, not restoring tree\n", globbed);
@@ -27,11 +27,6 @@ bool tree_restore() {
     focused = croot;
 
     tree_append_json(globbed);
-    char *old_restart = glob_path("~/.i3/_restart.json.old");
-    unlink(old_restart);
-    rename(globbed, old_restart);
-    free(globbed);
-    free(old_restart);
 
     printf("appended tree, using new root\n");
     croot = TAILQ_FIRST(&(croot->nodes_head));
@@ -40,7 +35,6 @@ bool tree_restore() {
     printf("out = %p\n", out);
     Con *ws = TAILQ_FIRST(&(out->nodes_head));
     printf("ws = %p\n", ws);
-    con_focus(ws);
 
     return true;
 }
@@ -59,6 +53,7 @@ void tree_init() {
     croot->type = CT_ROOT;
 
     Con *ws;
+    int c = 1;
     /* add the outputs */
     TAILQ_FOREACH(output, &outputs, outputs) {
         if (!output->active)
@@ -68,11 +63,25 @@ void tree_init() {
         oc->name = strdup(output->name);
         oc->type = CT_OUTPUT;
         oc->rect = output->rect;
+        output->con = oc;
+
+        char *name;
+        asprintf(&name, "[i3 con] output %s", oc->name);
+        x_set_name(oc, name);
+        free(name);
 
         /* add a workspace to this output */
-        ws = con_new(oc);
+        ws = con_new(NULL);
         ws->type = CT_WORKSPACE;
-        ws->name = strdup("1");
+        ws->num = c;
+        asprintf(&(ws->name), "%d", c);
+        c++;
+        con_attach(ws, oc, false);
+
+        asprintf(&name, "[i3 con] workspace %s", ws->name);
+        x_set_name(ws, name);
+        free(name);
+
         ws->fullscreen_mode = CF_OUTPUT;
         ws->orientation = HORIZ;
     }
@@ -92,6 +101,10 @@ Con *tree_open_con(Con *con) {
          * the new container needs to be opened as a leaf of the workspace. */
         if (con->type == CT_OUTPUT)
             con = focused;
+        /* If the currently focused container is a floating container, we
+         * attach the new container to the workspace */
+        if (con->type == CT_FLOATING_CON)
+            con = con->parent;
     }
 
     assert(con != NULL);
@@ -131,27 +144,14 @@ static void fix_floating_parent(Con *con, Con *vanishing) {
  * Closes the given container including all children
  *
  */
-void tree_close(Con *con, bool kill_window) {
+void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
     Con *parent = con->parent;
 
     /* check floating clients and adjust old_parent if necessary */
     fix_floating_parent(croot, con);
 
     /* Get the container which is next focused */
-    Con *next;
-    if (con->type == CT_FLOATING_CON) {
-        next = TAILQ_NEXT(con, floating_windows);
-        if (next == TAILQ_END(&(parent->floating_head)))
-            next = con_get_workspace(con);
-    } else {
-        next = TAILQ_NEXT(con, focused);
-        if (next == TAILQ_END(&(parent->nodes_head))) {
-            next = parent;
-            while (!TAILQ_EMPTY(&(next->focus_head)) &&
-                   TAILQ_FIRST(&(next->focus_head)) != con)
-                next = TAILQ_FIRST(&(next->focus_head));
-        }
-    }
+    Con *next = con_next_focused(con);
 
     DLOG("closing %p, kill_window = %d\n", con, kill_window);
     Con *child;
@@ -159,7 +159,8 @@ void tree_close(Con *con, bool kill_window) {
      * in their parent’s nodes_head */
     while (!TAILQ_EMPTY(&(con->nodes_head))) {
         child = TAILQ_FIRST(&(con->nodes_head));
-        tree_close(child, kill_window);
+        DLOG("killing child=%p\n", child);
+        tree_close(child, kill_window, true);
     }
 
     if (con->window != NULL) {
@@ -182,10 +183,7 @@ void tree_close(Con *con, bool kill_window) {
 
     if (con_is_floating(con)) {
         DLOG("Container was floating, killing floating container\n");
-
-        TAILQ_REMOVE(&(parent->parent->floating_head), parent, floating_windows);
-        TAILQ_REMOVE(&(parent->parent->focus_head), parent, focused);
-        tree_close(parent, false);
+        tree_close(parent, false, false);
         next = NULL;
     }
 
@@ -203,12 +201,13 @@ void tree_close(Con *con, bool kill_window) {
     con_focus(next);
 
     /* check if the parent container is empty now and close it */
-    if (parent->type != CT_WORKSPACE &&
+    if (!dont_kill_parent &&
+        parent->type != CT_WORKSPACE &&
         TAILQ_EMPTY(&(parent->nodes_head))) {
         DLOG("Closing empty parent container\n");
         /* TODO: check if this container would swallow any other client and
          * don’t close it automatically. */
-        tree_close(parent, false);
+        tree_close(parent, false, false);
     }
 }
 
@@ -224,7 +223,7 @@ void tree_close_con() {
     }
 
     /* Kill con */
-    tree_close(focused, true);
+    tree_close(focused, true, false);
 }
 
 /*
@@ -242,11 +241,11 @@ void tree_split(Con *con, orientation_t orientation) {
 
     Con *parent = con->parent;
     /* if we are in a container whose parent contains only one
-     * child and has the same orientation like we are trying to
-     * set, this operation is a no-op to not confuse the user */
-    if (parent->orientation == orientation &&
-        TAILQ_NEXT(con, nodes) == TAILQ_END(&(parent->nodes_head))) {
-        DLOG("Not splitting the same way again\n");
+     * child (its split functionality is unused so far), we just change the
+     * orientation (more intuitive than splitting again) */
+    if (con_num_children(parent) == 1) {
+        parent->orientation = orientation;
+        DLOG("Just changing orientation of existing container\n");
         return;
     }
 
@@ -259,8 +258,12 @@ void tree_split(Con *con, orientation_t orientation) {
     new->parent = parent;
     new->orientation = orientation;
 
-    /* 3: add it as a child to the new Con */
-    con_attach(con, new);
+    /* 3: swap 'percent' (resize factor) */
+    new->percent = con->percent;
+    con->percent = 0.0;
+
+    /* 4: add it as a child to the new Con */
+    con_attach(con, new, false);
 }
 
 /*
@@ -297,6 +300,13 @@ static void mark_unmapped(Con *con) {
     con->mapped = false;
     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
         mark_unmapped(current);
+    if (con->type == CT_WORKSPACE) {
+        TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
+            current->mapped = false;
+            Con *child = TAILQ_FIRST(&(current->nodes_head));
+            child->mapped = false;
+        }
+    }
 }
 
 /*
@@ -318,7 +328,7 @@ void tree_render() {
     Con *output;
     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
         printf("output %p / %s\n", output, output->name);
-        render_con(output);
+        render_con(output, false);
     }
     x_push_changes(croot);
     printf("-- END RENDERING --\n");
@@ -332,7 +342,8 @@ void tree_render() {
 void tree_next(char way, orientation_t orientation) {
     /* 1: get the first parent with the same orientation */
     Con *parent = focused->parent;
-    while (con_orientation(parent) != orientation) {
+    while (focused->type != CT_WORKSPACE &&
+           con_orientation(parent) != orientation) {
         LOG("need to go one level further up\n");
         /* if the current parent is an output, we are at a workspace
          * and the orientation still does not match */
@@ -363,6 +374,7 @@ void tree_next(char way, orientation_t orientation) {
     while (!TAILQ_EMPTY(&(next->focus_head)))
         next = TAILQ_FIRST(&(next->focus_head));
 
+    DLOG("focusing %p\n", next);
     con_focus(next);
 }
 
@@ -374,15 +386,52 @@ void tree_next(char way, orientation_t orientation) {
 void tree_move(char way, orientation_t orientation) {
     /* 1: get the first parent with the same orientation */
     Con *parent = focused->parent;
+    Con *old_parent = parent;
     if (focused->type == CT_WORKSPACE)
         return;
     bool level_changed = false;
     while (con_orientation(parent) != orientation) {
-        LOG("need to go one level further up\n");
-        /* if the current parent is an output, we are at a workspace
-         * and the orientation still does not match */
-        if (parent->type == CT_WORKSPACE)
-            return;
+        DLOG("need to go one level further up\n");
+        /* If the current parent is an output, we are at a workspace
+         * and the orientation still does not match. In this case, we split the
+         * workspace to have the same look & feel as in older i3 releases. */
+        if (parent->type == CT_WORKSPACE) {
+            DLOG("Arrived at workspace, splitting...\n");
+            /* 1: create a new split container */
+            Con *new = con_new(NULL);
+            new->parent = parent;
+
+            /* 2: copy layout and orientation from workspace */
+            new->layout = parent->layout;
+            new->orientation = parent->orientation;
+
+            Con *old_focused = TAILQ_FIRST(&(parent->focus_head));
+            if (old_focused == TAILQ_END(&(parent->focus_head)))
+                old_focused = NULL;
+
+            /* 3: move the existing cons of this workspace below the new con */
+            DLOG("Moving cons\n");
+            Con *child;
+            while (!TAILQ_EMPTY(&(parent->nodes_head))) {
+                child = TAILQ_FIRST(&(parent->nodes_head));
+                con_detach(child);
+                con_attach(child, new, true);
+            }
+
+            /* 4: switch workspace orientation */
+            parent->orientation = orientation;
+
+            /* 4: attach the new split container to the workspace */
+            DLOG("Attaching new split to ws\n");
+            con_attach(new, parent, false);
+
+            if (old_focused)
+                con_focus(old_focused);
+
+            level_changed = true;
+
+            break;
+        }
         parent = parent->parent;
         level_changed = true;
     }
@@ -393,16 +442,23 @@ void tree_move(char way, orientation_t orientation) {
     Con *next = current;
     if (way == 'n') {
         LOG("i would insert it after %p / %s\n", next, next->name);
-        if (!level_changed) {
-            next = TAILQ_NEXT(next, nodes);
-            if (next == TAILQ_END(&(next->parent->nodes_head))) {
-                LOG("cannot move further to the right\n");
+
+        /* Have a look at the next container: If there is no next container or
+         * if it is a leaf node, we move the focused one left to it. However,
+         * for split containers, we descend into it. */
+        next = TAILQ_NEXT(next, nodes);
+        if (next == TAILQ_END(&(next->parent->nodes_head))) {
+            if (focused == current)
                 return;
+            next = current;
+        } else {
+            if (level_changed && con_is_leaf(next)) {
+                next = current;
+            } else {
+                /* if this is a split container, we need to go down */
+                while (!TAILQ_EMPTY(&(next->focus_head)))
+                    next = TAILQ_FIRST(&(next->focus_head));
             }
-
-            /* if this is a split container, we need to go down */
-            while (!TAILQ_EMPTY(&(next->focus_head)))
-                next = TAILQ_FIRST(&(next->focus_head));
         }
 
         con_detach(focused);
@@ -414,17 +470,20 @@ void tree_move(char way, orientation_t orientation) {
     } else {
         LOG("i would insert it before %p / %s\n", current, current->name);
         bool gone_down = false;
-        if (!level_changed) {
-            next = TAILQ_PREV(next, nodes_head, nodes);
-            if (next == TAILQ_END(&(next->parent->nodes_head))) {
-                LOG("cannot move further\n");
+        next = TAILQ_PREV(next, nodes_head, nodes);
+        if (next == TAILQ_END(&(next->parent->nodes_head))) {
+            if (focused == current)
                 return;
-            }
-
-            /* if this is a split container, we need to go down */
-            while (!TAILQ_EMPTY(&(next->focus_head))) {
-                gone_down = true;
-                next = TAILQ_FIRST(&(next->focus_head));
+            next = current;
+        } else {
+            if (level_changed && con_is_leaf(next)) {
+                next = current;
+            } else {
+                /* if this is a split container, we need to go down */
+                while (!TAILQ_EMPTY(&(next->focus_head))) {
+                    gone_down = true;
+                    next = TAILQ_FIRST(&(next->focus_head));
+                }
             }
         }
 
@@ -441,4 +500,14 @@ void tree_move(char way, orientation_t orientation) {
         TAILQ_INSERT_HEAD(&(next->parent->focus_head), focused, focused);
         /* TODO: don’t influence focus handling? */
     }
+
+    /* We need to call con_focus() to fix the focus stack "above" the container
+     * we just inserted the focused container into (otherwise, the parent
+     * container(s) would still point to the old container(s)). */
+    con_focus(focused);
+
+    if (con_num_children(old_parent) == 0) {
+        DLOG("Old container empty after moving. Let's close it\n");
+        tree_close(old_parent, false, false);
+    }
 }