From 176d24f02dfaa0a7866dd4ea278d0cbf30fa23bb Mon Sep 17 00:00:00 2001
From: Tony Crisci <tony@dubstepdish.com>
Date: Sat, 17 Feb 2018 19:03:21 -0500
Subject: [PATCH] choose adjacent container between outputs

---
 sway/tree/layout.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index 6a574303..be494791 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -364,7 +364,12 @@ static swayc_t *get_swayc_in_output_direction(swayc_t *output,
 		ws = swayc_parent_by_type(ws, C_WORKSPACE);
 	}
 
-	if (ws && ws->children->length > 0) {
+	if (ws == NULL) {
+		wlr_log(L_ERROR, "got an output without a workspace");
+		return NULL;
+	}
+
+	if (ws->children->length > 0) {
 		switch (dir) {
 		case MOVE_LEFT:
 			// get most right child of new output
@@ -395,7 +400,7 @@ static swayc_t *get_swayc_in_output_direction(swayc_t *output,
 		}
 	}
 
-	return output;
+	return ws;
 }
 
 static void get_layout_center_position(swayc_t *container, int *x, int *y) {
@@ -525,12 +530,13 @@ static swayc_t *get_swayc_in_direction_under(swayc_t *container,
 			if (!adjacent || adjacent == container) {
 				return wrap_candidate;
 			}
-			// TODO descend into the focus-inactive of the physically closest
-			// view of the output
-			//swayc_t *new_con = get_swayc_in_output_direction(adjacent, dir, seat);
-			swayc_t *new_con = sway_seat_get_focus_inactive(seat, adjacent);
-			return new_con;
-
+			swayc_t *next = get_swayc_in_output_direction(adjacent, dir, seat);
+			if (next->children->length) {
+				// TODO consider floating children as well
+				return sway_seat_get_focus_inactive(seat, next);
+			} else {
+				return next;
+			}
 		} else {
 			if (dir == MOVE_LEFT || dir == MOVE_RIGHT) {
 				if (parent->layout == L_HORIZ || parent->layout == L_TABBED) {