From 1c292fd43abf1c3bc86e91bd348f690aea820c85 Mon Sep 17 00:00:00 2001
From: Ian Fan <ianfan0@gmail.com>
Date: Sat, 15 Sep 2018 01:51:58 +0100
Subject: [PATCH 1/2] ipc: add focus information to root node

---
 sway/ipc-json.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/sway/ipc-json.c b/sway/ipc-json.c
index 8b786145..30c0a8e1 100644
--- a/sway/ipc-json.c
+++ b/sway/ipc-json.c
@@ -314,9 +314,21 @@ struct focus_inactive_data {
 static void focus_inactive_children_iterator(struct sway_node *node,
 		void *_data) {
 	struct focus_inactive_data *data = _data;
-	if (node_get_parent(node) == data->node) {
-		json_object_array_add(data->object, json_object_new_int(node->id));
+	json_object *focus = data->object;
+	if (data->node == &root->node) {
+		int len = json_object_array_length(focus);
+		struct sway_output *output = node_get_output(node);
+		size_t id = output->node.id;
+		for (int i = 0; i < len; ++i) {
+			if ((size_t) json_object_get_int(json_object_array_get_idx(focus, i)) == id) {
+				return;
+			}
+		}
+		node = &output->node;
+	} else if (node_get_parent(node) != data->node) {
+		return;
 	}
+	json_object_array_add(focus, json_object_new_int(node->id));
 }
 
 json_object *ipc_json_describe_node(struct sway_node *node) {

From 34dbbb1314af16e388c7517aec6427baa00fb179 Mon Sep 17 00:00:00 2001
From: Ian Fan <ianfan0@gmail.com>
Date: Sat, 15 Sep 2018 09:42:46 +0100
Subject: [PATCH 2/2] ipc: handle NULL cases for node_get_output

---
 sway/ipc-json.c  | 5 ++++-
 sway/tree/node.c | 6 ++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/sway/ipc-json.c b/sway/ipc-json.c
index 30c0a8e1..52278be2 100644
--- a/sway/ipc-json.c
+++ b/sway/ipc-json.c
@@ -316,9 +316,12 @@ static void focus_inactive_children_iterator(struct sway_node *node,
 	struct focus_inactive_data *data = _data;
 	json_object *focus = data->object;
 	if (data->node == &root->node) {
-		int len = json_object_array_length(focus);
 		struct sway_output *output = node_get_output(node);
+		if (output == NULL) {
+			return;
+		}
 		size_t id = output->node.id;
+		int len = json_object_array_length(focus);
 		for (int i = 0; i < len; ++i) {
 			if ((size_t) json_object_get_int(json_object_array_get_idx(focus, i)) == id) {
 				return;
diff --git a/sway/tree/node.c b/sway/tree/node.c
index 74661c1a..dcab1c9b 100644
--- a/sway/tree/node.c
+++ b/sway/tree/node.c
@@ -74,8 +74,10 @@ void node_get_box(struct sway_node *node, struct wlr_box *box) {
 
 struct sway_output *node_get_output(struct sway_node *node) {
 	switch (node->type) {
-	case N_CONTAINER:
-		return node->sway_container->workspace->output;
+	case N_CONTAINER: {
+		struct sway_workspace *ws = node->sway_container->workspace;
+		return ws ? ws->output : NULL;
+    }
 	case N_WORKSPACE:
 		return node->sway_workspace->output;
 	case N_OUTPUT: