From 3f48f346dc4e47065af61b52c2813e07dfe5ecac Mon Sep 17 00:00:00 2001
From: Daniel Mueller <deso@posteo.net>
Date: Sat, 18 Nov 2017 11:24:55 -0800
Subject: [PATCH 08/14] Correctly sort in workspaces when disabling outputs

When an output (i.e., monitor) that is managed by i3 is disabled, all
workspaces get moved to the "first" output. That becomes a problem with
the existing logic in the context of duplicated workspace numbers as
the number is used to determine where the insertion should happen.
This change addresses the problem by adjusting the number (and name) of
the workspace before the insertion takes place in a way that existing
workspaces from the old output are appended to the list of existing
workspaces.
---
 src/randr.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/randr.c b/src/randr.c
index 26ee13..c416fa 100644
--- a/src/randr.c
+++ b/src/randr.c
@@ -865,6 +865,7 @@ static void move_content(Con *con) {
     /* 1: Get the con to focus next */
     Con *next = focused;
 
+    int num_children = first_content != NULL ? con_num_children(first_content) : 0;
     /* 2: iterate through workspaces and re-assign them, fixing the coordinates
      * of floating containers as we go */
     const Con *old_content = output_get_content(con);
@@ -879,6 +880,10 @@ static void move_content(Con *con) {
         DLOG("Detaching current = %p / %s\n", current, current->name);
         con_detach(current);
         DLOG("Re-attaching current = %p / %s\n", current, current->name);
+        /* Adjust the con's number to make it be sorted in properly. */
+        current->num = ++num_children;
+        free(current->name);
+        sasprintf(&current->name, "%d", current->num);
         con_attach(current, first_content, false);
         DLOG("Fixing the coordinates of floating containers\n");
         Con *floating_con;
@@ -894,6 +899,7 @@ static void move_content(Con *con) {
         workspace_show(con_get_workspace(next));
     }
 
+    num_children = first_content != NULL ? con_num_children(first_content) : 0;
     /* 3: move the dock clients to the first output */
     Con *child;
     TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
@@ -908,6 +914,10 @@ static void move_content(Con *con) {
             DLOG("Moving dock client %p to nc %p\n", dock, nc);
             con_detach(dock);
             DLOG("Re-attaching\n");
+            /* Adjust the con's number to make it be sorted in properly. */
+            dock->num = ++num_children;
+            free(dock->name);
+            sasprintf(&dock->name, "%d", dock->num);
             con_attach(dock, nc, false);
             DLOG("Done\n");
         }
-- 
2.52.0

