sites

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

dwm-conditional-6.8.diff (3154B)


      1 From e8b312c6e3035560a14789db1a7d47410b62a710 Mon Sep 17 00:00:00 2001
      2 From: Noah Osterholz <osterholznoah@gmail.com>
      3 Date: Fri, 6 Mar 2026 11:32:03 +0100
      4 Subject: [PATCH] Adding conditional keybinding support.
      5 
      6 This patch adds the conditional function to change the behaviour
      7 of a keymap depending on the return value of another function.
      8 ---
      9  config.def.h |  9 ++++++++-
     10  dwm.c        | 21 +++++++++++++++++++++
     11  2 files changed, 29 insertions(+), 1 deletion(-)
     12 
     13 diff --git a/config.def.h b/config.def.h
     14 index 81c3fc0..c1112c9 100644
     15 --- a/config.def.h
     16 +++ b/config.def.h
     17 @@ -61,6 +61,13 @@ static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn()
     18  static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
     19  static const char *termcmd[]  = { "st", NULL };
     20  
     21 +/* condition functions */
     22 +static int 
     23 +isrootwindow(void)
     24 +{
     25 +  return (selmon->sel == NULL); /* selmon->sel == NULL -> root window focused */
     26 +}
     27 +
     28  static const Key keys[] = {
     29  	/* modifier                     key        function        argument */
     30  	{ MODKEY,                       XK_p,      spawn,          {.v = dmenucmd } },
     31 @@ -74,7 +81,7 @@ static const Key keys[] = {
     32  	{ MODKEY,                       XK_l,      setmfact,       {.f = +0.05} },
     33  	{ MODKEY,                       XK_Return, zoom,           {0} },
     34  	{ MODKEY,                       XK_Tab,    view,           {0} },
     35 -	{ MODKEY|ShiftMask,             XK_c,      killclient,     {0} },
     36 +  { MODKEY|ShiftMask,             XK_c,      conditional,    {.v = &(CondFuncPtr){isrootwindow, quit, killclient, {0}, {0} } } },
     37  	{ MODKEY,                       XK_t,      setlayout,      {.v = &layouts[0]} },
     38  	{ MODKEY,                       XK_f,      setlayout,      {.v = &layouts[1]} },
     39  	{ MODKEY,                       XK_m,      setlayout,      {.v = &layouts[2]} },
     40 diff --git a/dwm.c b/dwm.c
     41 index 0a67103..a2dc2f1 100644
     42 --- a/dwm.c
     43 +++ b/dwm.c
     44 @@ -140,6 +140,14 @@ typedef struct {
     45  	int monitor;
     46  } Rule;
     47  
     48 +typedef struct {
     49 +  int (*condition)(void);
     50 +  void (*func_true)(const Arg *arg);
     51 +  void (*func_false)(const Arg *arg);
     52 +  const Arg arg_true;
     53 +  const Arg arg_false;
     54 +} CondFuncPtr;
     55 +
     56  /* function declarations */
     57  static void applyrules(Client *c);
     58  static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
     59 @@ -152,6 +160,7 @@ static void checkotherwm(void);
     60  static void cleanup(void);
     61  static void cleanupmon(Monitor *mon);
     62  static void clientmessage(XEvent *e);
     63 +static void conditional(const Arg *arg);
     64  static void configure(Client *c);
     65  static void configurenotify(XEvent *e);
     66  static void configurerequest(XEvent *e);
     67 @@ -977,6 +986,18 @@ grabkeys(void)
     68  	}
     69  }
     70  
     71 +void
     72 +conditional(const Arg *arg)
     73 +{
     74 +	CondFuncPtr *conditional = (CondFuncPtr*)arg->v;
     75 +	if (conditional->condition() && conditional->func_true != NULL) {
     76 +		conditional->func_true(&(conditional->arg_true));
     77 +		return;
     78 +	} else if (conditional->func_false != NULL) {
     79 +	  conditional->func_false(&(conditional->arg_false));
     80 +  }
     81 +}
     82 +
     83  void
     84  incnmaster(const Arg *arg)
     85  {
     86 -- 
     87 2.53.0
     88