Skip to content

Instantly share code, notes, and snippets.

@koonix
Created September 10, 2022 21:10
Show Gist options
  • Save koonix/c570803102813df875d77498ec2b92cb to your computer and use it in GitHub Desktop.
Save koonix/c570803102813df875d77498ec2b92cb to your computer and use it in GitHub Desktop.
some potentially useful functions for dwm.
void
pushincol(const Arg *arg)
{
Client *sel = selmon->sel, *c;
if (!sel || sel->isfloating)
return;
if (arg->i > 0) {
if ((c = nextofmodeloop(sel))) {
detach(sel);
sel->next = c->next;
c->next = sel;
}
} else if (arg->i < 0) {
if ((c = prevtiled(sel))) {
detach(sel);
sel->next = c;
c = prevtiled(c);
}
}
focus(sel);
arrange(selmon);
}
void
focusstackmode(const Arg *arg)
{
Client *c = selmon->sel;
if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen))
return;
if (arg->i > 0)
c = nextofmodeloop(c);
else
c = prevofmodeloop(c);
if (c) {
focus(c);
restack(selmon);
}
}
void
switchmode(const Arg *arg)
{
if (!selmon->sel)
return;
Client *c;
if ((c = stackfirstofothermodes(selmon))) {
focus(c);
restack(selmon);
}
}
Client *
nextofmodeloop(Client *c)
{
Client *t;
if (!(t = nextofmode(c)))
t = firstofmode(c);
return t;
}
Client *
prevofmodeloop(Client *c)
{
Client *t;
if (!(t = prevofmode(c)))
t = lastofmode(c);
return t;
}
Client *
nextofmode(Client *c)
{
int mode = clientmode(c);
for (c = c->next; c && (!ISVISIBLE(c) || clientmode(c) != mode); c = c->next);
return c;
}
Client *
prevofmode(Client *c)
{
int mode = clientmode(c);
Client *p = c->mon->clients, *r = NULL;
for (; p && p != c; p = p->next)
if (ISVISIBLE(p) && clientmode(p) == mode)
r = p;
return r;
}
Client *
firstofmode(Client *c)
{
int mode = clientmode(c);
for (c = c->mon->clients; c && (!ISVISIBLE(c) || clientmode(c) != mode); c = c->next);
return c;
}
Client *
lastofmode(Client *c)
{
int mode = clientmode(c);
Client *p = c->mon->clients, *r = NULL;
for (; p; p = p->next)
if (ISVISIBLE(p) && clientmode(p) == mode)
r = p;
return r;
}
Client *
stackfirstofothermodes(Monitor *m)
{
int mode = clientmode(m->sel);
Client *c;
if (!(c = stackfirstofmode(m, ++mode > 2 ? (mode = 0) : mode)))
c = stackfirstofmode(m, ++mode > 2 ? (mode = 0) : mode);
return c;
}
Client *
stackfirstofmode(Monitor *m, int mode)
{
Client *c = NULL;
for (c = m->stack; c && (!ISVISIBLE(c) || clientmode(c) != mode); c = c->snext);
return c;
}
/* returns 0 if the given visible client is master,
* 1 if it's in the stack area, 2 if it's floating */
int
clientmode(Client *c)
{
if (c->isfloating)
return 2;
if (c->mon->nmaster == 0)
return 1;
int i = 0;
Client *t = firsttiled(c->mon);
for (; t && t != c; t = nexttiled(t), i++);
if (!t)
return -1;
return (i + 1) <= c->mon->nmaster ? 0 : 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment