diff gbwm.c @ 12:1f03f3ca1a96

remember last focused window per workspace
author Atarwn Gard <a@qwa.su>
date Tue, 14 Oct 2025 18:09:23 +0500
parents 90d8c905af50
children 7d783901ff7d
line wrap: on
line diff
--- a/gbwm.c	Tue Oct 14 18:01:15 2025 +0500
+++ b/gbwm.c	Tue Oct 14 18:09:23 2025 +0500
@@ -43,6 +43,7 @@
 static Display *dpy;
 static Window root;
 static Client *workspaces[9] = {NULL};  // 9 workspaces
+static Client *last_focused[9] = {NULL};
 static int current_ws = 0;
 static Client *focused = NULL;
 static int sw, sh;
@@ -243,8 +244,10 @@
 	Client *old = focused;
 	focused = c;
 
-	if (old && old != c)
+	if (old && old != c) {
 		updateborder(old);
+		last_focused[old->workspace] = old;
+	}
 
 	updateborder(c);
 	XRaiseWindow(dpy, c->win);
@@ -523,6 +526,11 @@
 	int ws = arg->i;
 	if (ws < 0 || ws >= 9 || ws == current_ws) return;
 	
+	if (focused) {
+		last_focused[current_ws] = focused;
+	}
+
+
 	current_ws = ws;
 	
 	// Hide all windows from all workspaces
@@ -537,8 +545,16 @@
 		XMapWindow(dpy, c->win);
 	}
 	
-	focused = workspaces[current_ws];
-	if (focused) focus(focused, 1);
+	// Restore the last focus on this WS, otherwise the first one
+	focused = last_focused[current_ws];
+	if (!focused) focused = workspaces[current_ws];
+
+	if (focused) {
+		focus(focused, 1);
+	} else {
+		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
+	}
+
 	arrange();
 }