sites

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

dwm-bindtagrule-6.8.diff (3654B)


      1 From 52f10bb87286cce076d24246742f61bdd9834938 Mon Sep 17 00:00:00 2001
      2 From: Jaye Shovelton <Beanie.github@proton.me>
      3 Date: Tue, 1 Dec 2026 07:11:54 +0000
      4 Subject: [PATCH] Add a patch to spawn and switch to a fixed-tag window in one
      5  bind.
      6 
      7 ---
      8  dwm.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++------
      9  1 file changed, 50 insertions(+), 6 deletions(-)
     10 
     11 diff --git a/dwm.c b/dwm.c
     12 index 53b393e..1cdf86c 100644
     13 --- a/dwm.c
     14 +++ b/dwm.c
     15 @@ -151,6 +151,7 @@ static void buttonpress(XEvent *e);
     16  static void checkotherwm(void);
     17  static void cleanup(void);
     18  static void cleanupmon(Monitor *mon);
     19 +static int clientexists(const Rule *r);
     20  static void clientmessage(XEvent *e);
     21  static void configure(Client *c);
     22  static void configurenotify(XEvent *e);
     23 @@ -192,6 +193,7 @@ static void resize(Client *c, int x, int y, int w, int h, int interact);
     24  static void resizeclient(Client *c, int x, int y, int w, int h);
     25  static void resizemouse(const Arg *arg);
     26  static void restack(Monitor *m);
     27 +static int rulematches(const Rule *r, const char *name, const char *class, const char *instance);
     28  static void run(void);
     29  static void scan(void);
     30  static int sendevent(Client *c, Atom proto);
     31 @@ -292,10 +294,7 @@ applyrules(Client *c)
     32  
     33  	for (i = 0; i < LENGTH(rules); i++) {
     34  		r = &rules[i];
     35 -		if ((!r->title || strstr(c->name, r->title))
     36 -		&& (!r->class || strstr(class, r->class))
     37 -		&& (!r->instance || strstr(instance, r->instance)))
     38 -		{
     39 +		if (rulematches(r, c->name, class, instance)) {
     40  			c->isfloating = r->isfloating;
     41  			c->tags |= r->tags;
     42  			for (m = mons; m && m->num != r->monitor; m = m->next);
     43 @@ -511,6 +510,22 @@ cleanupmon(Monitor *mon)
     44  	free(mon);
     45  }
     46  
     47 +int
     48 +clientexists(const Rule *r)
     49 +{
     50 +	const char *class, *instance;
     51 +	XClassHint ch = { NULL, NULL };
     52 +	for (Client *c = selmon->clients; c; c = c->next) {
     53 +		XGetClassHint(dpy, c->win, &ch);
     54 +		class    = ch.res_class ? ch.res_class : broken;
     55 +		instance = ch.res_name  ? ch.res_name  : broken;
     56 +		if (rulematches(r, c->name, class, instance))
     57 +			return 1;
     58 +	}
     59 +
     60 +	return 0;
     61 +}
     62 +
     63  void
     64  clientmessage(XEvent *e)
     65  {
     66 @@ -1379,6 +1394,14 @@ restack(Monitor *m)
     67  	while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
     68  }
     69  
     70 +int
     71 +rulematches(const Rule *r, const char *name, const char *class, const char *instance)
     72 +{
     73 +	return (!r->title || strcasestr(name, r->title))
     74 +	    && (!r->class || strcasestr(class, r->class))
     75 +	    && (!r->instance || strcasestr(instance, r->instance));
     76 +}
     77 +
     78  void
     79  run(void)
     80  {
     81 @@ -1648,6 +1671,27 @@ void
     82  spawn(const Arg *arg)
     83  {
     84  	struct sigaction sa;
     85 +	char **strarr = (char **)arg->v;
     86 +
     87 +	/* slightly hacky: if spawning a program via `SHCMD`, check if any of the
     88 +	 * rules are matched. If they are, check if the tag is specified.  If it
     89 +	 * is, switch to that tag. This can't happen more cleanly or anywhere else,
     90 +	 * since this has to happen even if no window is spawned, and we have no
     91 +	 * way of already knowing if the window exists
     92 +	 */
     93 +	if (!strcmp(strarr[0], "/bin/sh")) {
     94 +		char *progname = strarr[2];
     95 +
     96 +		for (int i = 0; i < LENGTH(rules); i++) {
     97 +			const Rule *r = &rules[i];
     98 +			if (r->tags && rulematches(r, progname, progname, progname)) {
     99 +				Arg tag = { .ui = r->tags };
    100 +				view(&tag);
    101 +				if (clientexists(r))
    102 +					return;
    103 +			}
    104 +		}
    105 +	}
    106  
    107  	if (arg->v == dmenucmd)
    108  		dmenumon[0] = '0' + selmon->num;
    109 @@ -1661,8 +1705,8 @@ spawn(const Arg *arg)
    110  		sa.sa_handler = SIG_DFL;
    111  		sigaction(SIGCHLD, &sa, NULL);
    112  
    113 -		execvp(((char **)arg->v)[0], (char **)arg->v);
    114 -		die("dwm: execvp '%s' failed:", ((char **)arg->v)[0]);
    115 +		execvp(strarr[0], strarr);
    116 +		die("dwm: execvp '%s' failed:", strarr[0]);
    117  	}
    118  }
    119  
    120 -- 
    121 2.56.1
    122