Hi Andre,
On Wed Nov 2, 2022 at 4:56 PM CET, Dr. André Desgualdo Pereira wrote:
> The swapfocus patch works as intended when we have changed the focus on the
> same tag. But when coming from another tag, the swapfocus does nothing.
>
> Example: suppose the focus is on a window at tag 2, then I manyally focus
> another window on another tag (say 3). Now if I try to swapfocus (Mod+S) the
> focus doesn't change back to the previous window.
>
> Any ideas on how to enable the swapfocus even if the previously focused
> window is in another tag?
>
To make sure I understand you correctly, does the following example apply to
your situation? (I use dwm-6.4 and default keybindings here)
tag 1: st
tag 2: browser
You currently are on tag 1 and switch to tag 2 (Mod+2), now your browser is
focused. Then you press Mod+s and expect st to somehow show up and obtain
the focus, right?
This is a bit ambiguous, since there are multiple ways this could happen:
- move st to tag 2
- view tag 1 alongside with tag 2 (equivalent to Mod+ctrl+1)
- switch to tag 1 (equivalent to Mod+1)
I think the last one is the most useful solution, so I inlined the updated
patch to match this behaviour. Let me know if that's what you meant.
--
Best Regards,
Tom Schwindl
diff --git a/dwm.c b/dwm.c
index 253aba7b6f7d..c5dd6db5c84c 100644
--- a/dwm.c
+++ b/dwm.c
@@ -207,6 +207,7 @@ static void seturgent(Client *c, int urg);
static void showhide(Client *c);
static void sigchld(int unused);
static void spawn(const Arg *arg);
+static void swapfocus(const Arg *arg);
static void tag(const Arg *arg);
static void tagmon(const Arg *arg);
static void tile(Monitor *m);
@@ -236,6 +237,7 @@ static int xerrorstart(Display *dpy, XErrorEvent *ee);
static void zoom(const Arg *arg);
/* variables */
+static Client *prevclient;
static const char broken[] = "broken";
static char stext[256];
static int screen;
@@ -1650,6 +1652,22 @@ spawn(const Arg *arg)
}
}
+void
+swapfocus(const Arg *arg)
+{
+ Client *c;
+ Arg tmp;
+
+ for (c = selmon->clients; c && c != prevclient; c = c->next)
+ ;
+ if (c == prevclient) {
+ tmp.ui = (prevclient->tags & TAGMASK);
+ view(&tmp);
+ focus(prevclient);
+ restack(prevclient->mon);
+ }
+}
+
void
tag(const Arg *arg)
{
@@ -1751,6 +1769,7 @@ unfocus(Client *c, int setfocus)
{
if (!c)
return;
+ prevclient = c;
grabbuttons(c, 0);
XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
if (setfocus) {
@@ -2116,10 +2135,11 @@ void
zoom(const Arg *arg)
{
Client *c = selmon->sel;
+ prevclient = nexttiled(selmon->clients);
if (!selmon->lt[selmon->sellt]->arrange || !c || c->isfloating)
return;
- if (c == nexttiled(selmon->clients) && !(c = nexttiled(c->next)))
+ if (c == nexttiled(selmon->clients) && !(c = prevclient =
nexttiled(c->next)))
return;
pop(c);
}