# HG changeset patch # User Atarwn Gard # Date 1760447363 -18000 # Node ID 1f03f3ca1a96c869fd3984ac4b2d97397dc21116 # Parent 90d8c905af50b9062824ab2bf04511674a5c0955 remember last focused window per workspace diff -r 90d8c905af50 -r 1f03f3ca1a96 gbwm.c --- 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(); }