From 161e808d0b1bc1f94851b300858ce619574cdde3 Mon Sep 17 00:00:00 2001
From: David Maciejak <david.maciejak@gmail.com>
Date: Thu, 11 Sep 2014 07:04:12 +0700
Subject: [PATCH 1/5] wmaker: fix focused window list order

This patch is fixing the focused next window list order.
As now, the switching was only working for 2 windows.
For example, taking 3 windows called A,B,C.
If the windows list is A,B,C where A is the current focused window.
Focusing on B will result on switching position A and B, thus C will
never be the next window.
For this example the patch is updating the linked list as B,C,A.
---
 src/actions.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/src/actions.c b/src/actions.c
index b8e4fc4..ed77d3b 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -189,15 +189,27 @@ void wSetFocusTo(WScreen *scr, WWindow *wwin)
 
 	/* if this is not the focused window focus it */
 	if (focused != wwin) {
-		/* change the focus window list order */
-		if (wwin->prev)
-			wwin->prev->next = wwin->next;
+		WWindow *tmp = focused;
+		/* get the last one from the list */
+		while (tmp->prev)
+			tmp = tmp->prev;
 
-		if (wwin->next)
-			wwin->next->prev = wwin->prev;
+		/* change the focus window list order
+		   the current focused window and siblings are moved to the end */
+		focused->next = tmp;
+		tmp->prev = focused;
+
+		/* cut the sibling link to the wwin window we want to focus
+		   as now the last sibling will become the last in the list */
+		tmp = focused;
+		while (tmp->prev) {
+			if (tmp->prev == wwin)
+				tmp->prev = NULL;
+			else
+				tmp = tmp->prev;
+		}
 
-		wwin->prev = focused;
-		focused->next = wwin;
+		/* wwin is becoming the first focused window */
 		wwin->next = NULL;
 		scr->focused_window = wwin;
 
-- 
1.8.3.2

