cx-dwm

How I can implement circle switching layouts?


By default in dwm config.h provide one keybinding per layout.

{ MODKEY, XK_t, setlayout, {.v = &layouts[0]} },
{ MODKEY, XK_f, setlayout, {.v = &layouts[1]} },
{ MODKEY, XK_m, setlayout, {.v = &layouts[2]} },

I want to obtain function in my dwm config which will change acceptable layouts in circle manner.

Something like this:

static void circlesetlayout (const Arg *arg);
...
{ MODKEY, XK_space,  circlesetlayout, {0} },
...
void
circlesetlayout (const Arg *arg) {
  Arg finallayout;
  if (Monitor.sellt == 2) {
    finallayout.v = &layouts[0];
  } else {
    finallayout.v = &layouts[1];
  }
  setlayout (&finallayout);
}

But it doesn't work in manner I expect.


Solution

  • This worked for me:

    void
    setnextlayout(const Arg *arg) {
        Arg newarg = {0};
    
        size_t i = 0;
        while(i < LENGTH(layouts) && selmon->lt[selmon->sellt] != &layouts[i])
            i++;
    
        newarg.v = &layouts[(i + 1) % LENGTH(layouts)]; // you can do it without '%'
        setlayout(&newarg);
    }