From 824a3d81aad8e3eb81c80d07bf6554be9fe214cf Mon Sep 17 00:00:00 2001
From: taiyu <taiyu.len@gmail.com>
Date: Sat, 15 Aug 2015 14:32:14 -0700
Subject: [PATCH 1/2] fixed fullscreen & focusing

---
 sway/commands.c |  8 ++++++++
 sway/handlers.c | 28 ++++++++++++++++++----------
 2 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/sway/commands.c b/sway/commands.c
index 742d6b86..20e0fc8d 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -286,6 +286,14 @@ static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) {
 	swayc_t *container = get_focused_container(&root_container);
 	bool current = (wlc_view_get_state(container->handle) & WLC_BIT_FULLSCREEN) > 0;
 	wlc_view_set_state(container->handle, WLC_BIT_FULLSCREEN, !current);
+	//Resize workspace if going from  fullscreen -> notfullscreen
+	//otherwise just resize container
+	if (current) {
+		while (container->type != C_WORKSPACE) {
+			container = container->parent;
+		}
+	}
+	//Only resize container when going into fullscreen
 	arrange_windows(container, -1, -1);
 
 	return true;
diff --git a/sway/handlers.c b/sway/handlers.c
index f95683d0..b02cd988 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -82,16 +82,21 @@ static void handle_output_focused(wlc_handle output, bool focus) {
 }
 
 static bool handle_view_created(wlc_handle handle) {
-	swayc_t *container = get_focused_container(&root_container);
-	swayc_t *view = new_view(container, handle);
-	unfocus_all(&root_container);
+	swayc_t *focused = get_focused_container(&root_container);
+	swayc_t *view = new_view(focused, handle);
 	if (view) {
+		unfocus_all(&root_container);
 		focus_view(view);
 		arrange_windows(view->parent, -1, -1);
 	} else { //Unmanaged view
 		wlc_view_set_state(handle, WLC_BIT_ACTIVATED, true);
 		wlc_view_focus(handle);
 	}
+	if (wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN) {
+		unfocus_all(&root_container);
+		focus_view(focused);
+		arrange_windows(focused, -1, -1);
+	}
 	return true;
 }
 
@@ -189,21 +194,24 @@ static bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct w
 	if (!config->focus_follows_mouse) {
 		return true;
 	}
-	swayc_t *c = find_container(&root_container, pointer_test, (void *)origin);
 	swayc_t *focused = get_focused_container(&root_container);
-	if (c && c != focused) {
-		sway_log(L_DEBUG, "Switching focus to %p", c);
-		unfocus_all(&root_container);
-		focus_view(c);
+	if (!(wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)) {
+		swayc_t *c = find_container(&root_container, pointer_test, (void *)origin);
+		if (c && c != focused) {
+			sway_log(L_DEBUG, "Switching focus to %p", c);
+			unfocus_all(&root_container);
+			focus_view(c);
+		}
 	}
 	return true;
 }
 
 static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers,
 		uint32_t button, enum wlc_button_state state) {
-	if (state == WLC_BUTTON_STATE_PRESSED) {
+	swayc_t *focused = get_focused_container(&root_container);
+	if (state == WLC_BUTTON_STATE_PRESSED
+		&& !(wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)) {
 		swayc_t *c = find_container(&root_container, pointer_test, &mouse_origin);
-		swayc_t *focused = get_focused_container(&root_container);
 		if (c && c != focused) {
 			sway_log(L_DEBUG, "Switching focus to %p", c);
 			unfocus_all(&root_container);

From 3a21ba020fd0f0628a128070b72f0cf7a6b64a57 Mon Sep 17 00:00:00 2001
From: taiyu <taiyu.len@gmail.com>
Date: Sat, 15 Aug 2015 15:20:07 -0700
Subject: [PATCH 2/2] made better, reduced code duplication

---
 sway/handlers.c | 43 ++++++++++++++++---------------------------
 sway/handlers.h |  4 ++--
 2 files changed, 18 insertions(+), 29 deletions(-)

diff --git a/sway/handlers.c b/sway/handlers.c
index b02cd988..98d4831f 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -28,15 +28,19 @@ static bool pointer_test(swayc_t *view, void *_origin) {
 	return false;
 }
 
-void focus_pointer(void) {
-	swayc_t *focused = find_container(&root_container, pointer_test, &mouse_origin);
-	if (focused) {
-		sway_log(L_DEBUG, "Switching focus to %p", focused);
-		unfocus_all(&root_container);
-		focus_view(focused);
-	} else {
-		focus_view(active_workspace);
+swayc_t *focus_pointer(void) {
+	swayc_t *focused = get_focused_container(&root_container);
+	if (!(wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)) {
+		swayc_t *pointer = find_container(&root_container, pointer_test, &mouse_origin);
+		if (pointer && focused != pointer) {
+			unfocus_all(&root_container);
+			focus_view(pointer);
+		} else if (!focused){
+			focus_view(active_workspace);
+		}
+		focused = pointer;
 	}
+	return focused;
 }
 
 static bool handle_output_created(wlc_handle output) {
@@ -194,31 +198,16 @@ static bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct w
 	if (!config->focus_follows_mouse) {
 		return true;
 	}
-	swayc_t *focused = get_focused_container(&root_container);
-	if (!(wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)) {
-		swayc_t *c = find_container(&root_container, pointer_test, (void *)origin);
-		if (c && c != focused) {
-			sway_log(L_DEBUG, "Switching focus to %p", c);
-			unfocus_all(&root_container);
-			focus_view(c);
-		}
-	}
+	focus_pointer();
 	return true;
 }
 
 static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers,
 		uint32_t button, enum wlc_button_state state) {
 	swayc_t *focused = get_focused_container(&root_container);
-	if (state == WLC_BUTTON_STATE_PRESSED
-		&& !(wlc_view_get_state(focused->handle) & WLC_BIT_FULLSCREEN)) {
-		swayc_t *c = find_container(&root_container, pointer_test, &mouse_origin);
-		if (c && c != focused) {
-			sway_log(L_DEBUG, "Switching focus to %p", c);
-			unfocus_all(&root_container);
-			focus_view(c);
-			return false;
-		}
-		return true;
+	if (state == WLC_BUTTON_STATE_PRESSED) {
+		swayc_t *pointer = focus_pointer();
+		return !(pointer && pointer != focused);
 	}
 	return true;
 }
diff --git a/sway/handlers.h b/sway/handlers.h
index b8b171c3..d1742cce 100644
--- a/sway/handlers.h
+++ b/sway/handlers.h
@@ -6,7 +6,7 @@
 
 extern struct wlc_interface interface;
 
-//set focus to current pointer location
-void focus_pointer(void);
+//set focus to current pointer location and return focused container
+swayc_t *focus_pointer(void);
 
 #endif