From 16c663ed49e3950388901f220066e4da69956dfb Mon Sep 17 00:00:00 2001
From: Ryan Dwyer <ryandwyer1@gmail.com>
Date: Sat, 18 Aug 2018 21:54:09 +1000
Subject: [PATCH] Rename container_sort_workspaces and container_wrap_children

This commit renames container_sort_workspaces to output_sort_workspaces
and moves it to output.c.

This also renames container_wrap_children to workspace_wrap_children and
moves it to workspace.c. This function is only called with workspaces.
---
 include/sway/output.h         |  2 ++
 include/sway/tree/container.h |  8 --------
 include/sway/tree/layout.h    |  2 --
 include/sway/tree/workspace.h |  7 +++++++
 sway/commands/floating.c      |  3 ++-
 sway/commands/fullscreen.c    |  3 ++-
 sway/commands/move.c          |  6 +++---
 sway/commands/rename.c        |  3 ++-
 sway/tree/container.c         | 14 +-------------
 sway/tree/layout.c            | 23 -----------------------
 sway/tree/output.c            | 23 ++++++++++++++++++++++-
 sway/tree/workspace.c         | 14 +++++++++++++-
 12 files changed, 54 insertions(+), 54 deletions(-)

diff --git a/include/sway/output.h b/include/sway/output.h
index 80dcd37b..8bdd1919 100644
--- a/include/sway/output.h
+++ b/include/sway/output.h
@@ -58,6 +58,8 @@ void output_damage_whole_container(struct sway_output *output,
 
 struct sway_container *output_by_name(const char *name);
 
+void output_sort_workspaces(struct sway_container *output);
+
 void output_enable(struct sway_output *output);
 
 bool output_has_opaque_overlay_layer_surface(struct sway_output *output);
diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h
index fdcc31ec..d866ec03 100644
--- a/include/sway/tree/container.h
+++ b/include/sway/tree/container.h
@@ -361,12 +361,4 @@ bool container_is_floating_or_child(struct sway_container *container);
  */
 bool container_is_fullscreen_or_child(struct sway_container *container);
 
-/**
- * Wrap the children of parent in a new container. The new container will be the
- * only child of parent.
- *
- * The new container is returned.
- */
-struct sway_container *container_wrap_children(struct sway_container *parent);
-
 #endif
diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h
index 77cd954b..5b803dfe 100644
--- a/include/sway/tree/layout.h
+++ b/include/sway/tree/layout.h
@@ -45,8 +45,6 @@ void container_move(struct sway_container *container,
 enum sway_container_layout container_get_default_layout(
 		struct sway_container *con);
 
-void container_sort_workspaces(struct sway_container *output);
-
 struct sway_container *container_get_in_direction(struct sway_container
 		*container, struct sway_seat *seat, enum movement_direction dir);
 
diff --git a/include/sway/tree/workspace.h b/include/sway/tree/workspace.h
index 056f2329..5b43ae87 100644
--- a/include/sway/tree/workspace.h
+++ b/include/sway/tree/workspace.h
@@ -50,4 +50,11 @@ struct sway_container *workspace_output_get_highest_available(
 
 void workspace_detect_urgent(struct sway_container *workspace);
 
+/**
+ * Wrap the workspace's tiling children in a new container.
+ * The new container will be the only direct tiling child of the workspace.
+ * The new container is returned.
+ */
+struct sway_container *workspace_wrap_children(struct sway_container *ws);
+
 #endif
diff --git a/sway/commands/floating.c b/sway/commands/floating.c
index 31de5ec3..c9467ef0 100644
--- a/sway/commands/floating.c
+++ b/sway/commands/floating.c
@@ -8,6 +8,7 @@
 #include "sway/tree/container.h"
 #include "sway/tree/layout.h"
 #include "sway/tree/view.h"
+#include "sway/tree/workspace.h"
 #include "list.h"
 
 struct cmd_results *cmd_floating(int argc, char **argv) {
@@ -24,7 +25,7 @@ struct cmd_results *cmd_floating(int argc, char **argv) {
 	if (container->type == C_WORKSPACE) {
 		// Wrap the workspace's children in a container so we can float it
 		struct sway_container *workspace = container;
-		container = container_wrap_children(container);
+		container = workspace_wrap_children(container);
 		workspace->layout = L_HORIZ;
 		seat_set_focus(config->handler_context.seat, container);
 	}
diff --git a/sway/commands/fullscreen.c b/sway/commands/fullscreen.c
index 5ad06e40..a0661200 100644
--- a/sway/commands/fullscreen.c
+++ b/sway/commands/fullscreen.c
@@ -4,6 +4,7 @@
 #include "sway/tree/arrange.h"
 #include "sway/tree/container.h"
 #include "sway/tree/view.h"
+#include "sway/tree/workspace.h"
 #include "sway/tree/layout.h"
 #include "util.h"
 
@@ -21,7 +22,7 @@ struct cmd_results *cmd_fullscreen(int argc, char **argv) {
 	if (container->type == C_WORKSPACE) {
 		// Wrap the workspace's children in a container so we can fullscreen it
 		struct sway_container *workspace = container;
-		container = container_wrap_children(container);
+		container = workspace_wrap_children(container);
 		workspace->layout = L_HORIZ;
 		seat_set_focus(config->handler_context.seat, container);
 	}
diff --git a/sway/commands/move.c b/sway/commands/move.c
index de6b1b0a..acdc50b5 100644
--- a/sway/commands/move.c
+++ b/sway/commands/move.c
@@ -64,7 +64,7 @@ static struct cmd_results *cmd_move_container(struct sway_container *current,
 			return cmd_results_new(CMD_FAILURE, "move",
 					"Can't move an empty workspace");
 		}
-		current = container_wrap_children(current);
+		current = workspace_wrap_children(current);
 	} else if (current->type != C_CONTAINER && current->type != C_VIEW) {
 		return cmd_results_new(CMD_FAILURE, "move",
 				"Can only move containers and views.");
@@ -245,7 +245,7 @@ static void workspace_move_to_output(struct sway_container *workspace,
 	// Try to remove an empty workspace from the destination output.
 	container_reap_empty_recursive(new_output_focus);
 
-	container_sort_workspaces(output);
+	output_sort_workspaces(output);
 	seat_set_focus(seat, output);
 	workspace_output_raise_priority(workspace, old_output, output);
 	ipc_event_workspace(NULL, workspace, "move");
@@ -437,7 +437,7 @@ static struct cmd_results *move_to_scratchpad(struct sway_container *con) {
 	if (con->type == C_WORKSPACE) {
 		// Wrap the workspace's children in a container
 		struct sway_container *workspace = con;
-		con = container_wrap_children(con);
+		con = workspace_wrap_children(con);
 		workspace->layout = L_HORIZ;
 	}
 
diff --git a/sway/commands/rename.c b/sway/commands/rename.c
index c6952bbb..c69bbdac 100644
--- a/sway/commands/rename.c
+++ b/sway/commands/rename.c
@@ -6,6 +6,7 @@
 #include "sway/commands.h"
 #include "sway/config.h"
 #include "sway/ipc-server.h"
+#include "sway/output.h"
 #include "sway/tree/container.h"
 #include "sway/tree/workspace.h"
 
@@ -82,7 +83,7 @@ struct cmd_results *cmd_rename(int argc, char **argv) {
 	free(workspace->name);
 	workspace->name = new_name;
 
-	container_sort_workspaces(workspace->parent);
+	output_sort_workspaces(workspace->parent);
 	ipc_event_workspace(NULL, workspace, "rename");
 
 	return cmd_results_new(CMD_SUCCESS, NULL, NULL);
diff --git a/sway/tree/container.c b/sway/tree/container.c
index db780270..337245fd 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -270,7 +270,7 @@ static struct sway_container *container_output_destroy(
 					container_destroy(workspace);
 				}
 
-				container_sort_workspaces(new_output);
+				output_sort_workspaces(new_output);
 			}
 		}
 	}
@@ -1305,15 +1305,3 @@ bool container_is_fullscreen_or_child(struct sway_container *container) {
 
 	return false;
 }
-
-struct sway_container *container_wrap_children(struct sway_container *parent) {
-	struct sway_container *middle = container_create(C_CONTAINER);
-	middle->layout = parent->layout;
-	while (parent->children->length) {
-		struct sway_container *child = parent->children->items[0];
-		container_remove_child(child);
-		container_add_child(middle, child);
-	}
-	container_add_child(parent, middle);
-	return middle;
-}
diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index 2b710403..49ec806e 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -1,5 +1,4 @@
 #define _POSIX_C_SOURCE 200809L
-#include <ctype.h>
 #include <math.h>
 #include <stdbool.h>
 #include <stdlib.h>
@@ -591,28 +590,6 @@ enum sway_container_layout container_get_default_layout(
 	}
 }
 
-static int sort_workspace_cmp_qsort(const void *_a, const void *_b) {
-	struct sway_container *a = *(void **)_a;
-	struct sway_container *b = *(void **)_b;
-	int retval = 0;
-
-	if (isdigit(a->name[0]) && isdigit(b->name[0])) {
-		int a_num = strtol(a->name, NULL, 10);
-		int b_num = strtol(b->name, NULL, 10);
-		retval = (a_num < b_num) ? -1 : (a_num > b_num);
-	} else if (isdigit(a->name[0])) {
-		retval = -1;
-	} else if (isdigit(b->name[0])) {
-		retval = 1;
-	}
-
-	return retval;
-}
-
-void container_sort_workspaces(struct sway_container *output) {
-	list_stable_sort(output->children, sort_workspace_cmp_qsort);
-}
-
 /**
  * Get swayc in the direction of newly entered output.
  */
diff --git a/sway/tree/output.c b/sway/tree/output.c
index 31e3bf9b..ab955359 100644
--- a/sway/tree/output.c
+++ b/sway/tree/output.c
@@ -1,4 +1,5 @@
 #define _POSIX_C_SOURCE 200809L
+#include <ctype.h>
 #include <string.h>
 #include <strings.h>
 #include "sway/ipc-server.h"
@@ -28,7 +29,7 @@ static void restore_workspaces(struct sway_container *output) {
 		}
 	}
 
-	container_sort_workspaces(output);
+	output_sort_workspaces(output);
 }
 
 struct sway_container *output_create(
@@ -102,3 +103,23 @@ struct sway_container *output_create(
 	return output;
 }
 
+static int sort_workspace_cmp_qsort(const void *_a, const void *_b) {
+	struct sway_container *a = *(void **)_a;
+	struct sway_container *b = *(void **)_b;
+
+	if (isdigit(a->name[0]) && isdigit(b->name[0])) {
+		int a_num = strtol(a->name, NULL, 10);
+		int b_num = strtol(b->name, NULL, 10);
+		return (a_num < b_num) ? -1 : (a_num > b_num);
+	} else if (isdigit(a->name[0])) {
+		return -1;
+	} else if (isdigit(b->name[0])) {
+		return 1;
+	}
+	return 0;
+}
+
+void output_sort_workspaces(struct sway_container *output) {
+	list_stable_sort(output->children, sort_workspace_cmp_qsort);
+}
+
diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c
index e7383de0..1c0e6515 100644
--- a/sway/tree/workspace.c
+++ b/sway/tree/workspace.c
@@ -75,7 +75,7 @@ struct sway_container *workspace_create(struct sway_container *output,
 	workspace_output_add_priority(workspace, output);
 
 	container_add_child(output, workspace);
-	container_sort_workspaces(output);
+	output_sort_workspaces(output);
 	container_create_notify(workspace);
 
 	return workspace;
@@ -537,3 +537,15 @@ void workspace_detect_urgent(struct sway_container *workspace) {
 		container_damage_whole(workspace);
 	}
 }
+
+struct sway_container *workspace_wrap_children(struct sway_container *ws) {
+	struct sway_container *middle = container_create(C_CONTAINER);
+	middle->layout = ws->layout;
+	while (ws->children->length) {
+		struct sway_container *child = ws->children->items[0];
+		container_remove_child(child);
+		container_add_child(middle, child);
+	}
+	container_add_child(ws, middle);
+	return middle;
+}