sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dwm-cyclewindows-20240624-56f64312.diff (3270B)


      1 From 56f643120f082c9fd609f25e3ad9db6eb435fb89 Mon Sep 17 00:00:00 2001
      2 From: elbachir-one <bachiralfa@gmail.com>
      3 Date: Mon, 24 Jun 2024 22:15:50 +0100
      4 Subject: [PATCH] Inspired by a Reddit post from u/PawarShubham3007
      5 shubham-cpp on github, this patch aiming to make dwm focus on windows
      6  of the same program.
      7 
      8 ---
      9  config.def.h |  4 +++-
     10  dwm.c        | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++
     11  2 files changed, 58 insertions(+), 1 deletion(-)
     12 
     13 diff --git a/config.def.h b/config.def.h
     14 index 9efa774..fd0dda6 100644
     15 --- a/config.def.h
     16 +++ b/config.def.h
     17 @@ -85,6 +85,8 @@ static const Key keys[] = {
     18  	{ MODKEY,                       XK_period, focusmon,       {.i = +1 } },
     19  	{ MODKEY|ShiftMask,             XK_comma,  tagmon,         {.i = -1 } },
     20  	{ MODKEY|ShiftMask,             XK_period, tagmon,         {.i = +1 } },
     21 +   { MODKEY,                       XK_n,      focussame,      {.i = +1 } },
     22 +   { MODKEY|ShiftMask,             XK_n,      focussame,      {.i = -1 } },
     23  	TAGKEYS(                        XK_1,                      0)
     24  	TAGKEYS(                        XK_2,                      1)
     25  	TAGKEYS(                        XK_3,                      2)
     26 diff --git a/dwm.c b/dwm.c
     27 index 67c6b2b..c657315 100644
     28 --- a/dwm.c
     29 +++ b/dwm.c
     30 @@ -233,6 +233,8 @@ static int xerror(Display *dpy, XErrorEvent *ee);
     31  static int xerrordummy(Display *dpy, XErrorEvent *ee);
     32  static int xerrorstart(Display *dpy, XErrorEvent *ee);
     33  static void zoom(const Arg *arg);
     34 +static void focussame(const Arg *arg);
     35 +static Window lastfocusedwin = None;
     36  
     37  /* variables */
     38  static const char broken[] = "broken";
     39 @@ -786,6 +788,59 @@ expose(XEvent *e)
     40  		drawbar(m);
     41  }
     42  
     43 +void
     44 +focussame(const Arg *arg) {
     45 +    Client *c;
     46 +    XClassHint ch = { NULL, NULL };
     47 +    char *class_name = NULL;
     48 +    int direction = arg->i;
     49 +
     50 +    if (!selmon->sel)
     51 +        return;
     52 +
     53 +    if (!XGetClassHint(dpy, selmon->sel->win, &ch))
     54 +        return;
     55 +    class_name = ch.res_class;
     56 +
     57 +    Client *clients[32];
     58 +    int num_clients = 0;
     59 +    for (c = selmon->clients; c && num_clients < 32; c = c->next) {
     60 +        if (c->tags & selmon->tagset[selmon->seltags] && XGetClassHint(dpy, c->win, &ch)) {
     61 +            if (strcmp(class_name, ch.res_class) == 0)
     62 +                clients[num_clients++] = c;
     63 +            XFree(ch.res_class);
     64 +            XFree(ch.res_name);
     65 +        }
     66 +    }
     67 +
     68 +    Client *target_client = NULL;
     69 +    if (direction == +1) {
     70 +        for (int i = 0; i < num_clients; ++i) {
     71 +            if (clients[i]->win == lastfocusedwin) {
     72 +                target_client = clients[(i + 1) % num_clients];
     73 +                break;
     74 +            }
     75 +        }
     76 +        if (!target_client)
     77 +            target_client = clients[0];
     78 +    } else if (direction == -1) {
     79 +        for (int i = 0; i < num_clients; ++i) {
     80 +            if (clients[i]->win == lastfocusedwin) {
     81 +                target_client = clients[(i - 1 + num_clients) % num_clients];
     82 +                break;
     83 +            }
     84 +        }
     85 +        if (!target_client)
     86 +            target_client = clients[num_clients - 1];
     87 +    }
     88 +
     89 +    if (target_client) {
     90 +        focus(target_client);
     91 +        restack(selmon);
     92 +        lastfocusedwin = target_client->win;
     93 +    }
     94 +}
     95 +
     96  void
     97  focus(Client *c)
     98  {
     99 -- 
    100 2.45.2
    101