From 3a912ae2b4e99507d8c3b1ce0add52f9043fa055 Mon Sep 17 00:00:00 2001 From: wochap Date: Thu, 11 Apr 2024 12:45:47 -0500 Subject: implement regex support in rules for app_id and title Enables the use of regular expressions for window rules "app_id" and "title" --- config.def.h | 1 + dwl.c | 19 ++++++++++-- patches/regexrules.patch | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 patches/regexrules.patch diff --git a/config.def.h b/config.def.h index 59066f8..7bccc8c 100644 --- a/config.def.h +++ b/config.def.h @@ -34,6 +34,7 @@ static const Rule rules[] = { /* examples: */ { "Gimp_EXAMPLE", NULL, 0, 1, -1 }, /* Start on currently visible tags floating, not tiled */ { "firefox_EXAMPLE", NULL, 1 << 8, 0, -1 }, /* Start on ONLY tag "9" */ + { "^kitty_EXAMPLE$", NULL, 0, 0, -1 }, }; /* tearing */ diff --git a/dwl.c b/dwl.c index 44796b9..6d4ba98 100644 --- a/dwl.c +++ b/dwl.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -402,6 +403,7 @@ static Monitor *xytomon(double x, double y); static void xytonode(double x, double y, struct wlr_surface **psurface, Client **pc, LayerSurface **pl, double *nx, double *ny); static void zoom(const Arg *arg); +static int regex_match(const char *pattern, const char *str); /* variables */ static pid_t child_pid = -1; @@ -551,8 +553,8 @@ applyrules(Client *c) title = client_get_title(c); for (r = rules; r < END(rules); r++) { - if ((!r->title || strstr(title, r->title)) - && (!r->id || strstr(appid, r->id))) { + if ((!r->title || regex_match(r->title, title)) + && (!r->id || regex_match(r->id, appid))) { c->isfloating = r->isfloating; newtags |= r->tags; i = 0; @@ -3519,6 +3521,19 @@ zoom(const Arg *arg) arrange(selmon); } +int +regex_match(const char *pattern, const char *str) { + regex_t regex; + int reti; + if (regcomp(®ex, pattern, REG_EXTENDED) != 0) + return 0; + reti = regexec(®ex, str, (size_t)0, NULL, 0); + regfree(®ex); + if (reti == 0) + return 1; + return 0; +} + #ifdef XWAYLAND void activatex11(struct wl_listener *listener, void *data) diff --git a/patches/regexrules.patch b/patches/regexrules.patch new file mode 100644 index 0000000..f7207d3 --- /dev/null +++ b/patches/regexrules.patch @@ -0,0 +1,76 @@ +From 7fed9449575b1e4f58d519d2f87b7e66e2056125 Mon Sep 17 00:00:00 2001 +From: wochap +Date: Thu, 11 Apr 2024 12:45:47 -0500 +Subject: [PATCH] implement regex support in rules for app_id and title Enables + the use of regular expressions for window rules "app_id" and "title" + +--- + config.def.h | 1 + + dwl.c | 19 +++++++++++++++++-- + 2 files changed, 18 insertions(+), 2 deletions(-) + +diff --git a/config.def.h b/config.def.h +index 8847e58..89f5b60 100644 +--- a/config.def.h ++++ b/config.def.h +@@ -25,6 +25,7 @@ static const Rule rules[] = { + /* examples: */ + { "Gimp_EXAMPLE", NULL, 0, 1, -1 }, /* Start on currently visible tags floating, not tiled */ + { "firefox_EXAMPLE", NULL, 1 << 8, 0, -1 }, /* Start on ONLY tag "9" */ ++ { "^kitty_EXAMPLE$", NULL, 0, 0, -1 }, + }; + + /* layout(s) */ +diff --git a/dwl.c b/dwl.c +index bf763df..fc185af 100644 +--- a/dwl.c ++++ b/dwl.c +@@ -10,6 +10,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -347,6 +348,7 @@ static Monitor *xytomon(double x, double y); + static void xytonode(double x, double y, struct wlr_surface **psurface, + Client **pc, LayerSurface **pl, double *nx, double *ny); + static void zoom(const Arg *arg); ++static int regex_match(const char *pattern, const char *str); + + /* variables */ + static const char broken[] = "broken"; +@@ -459,8 +461,8 @@ applyrules(Client *c) + title = broken; + + for (r = rules; r < END(rules); r++) { +- if ((!r->title || strstr(title, r->title)) +- && (!r->id || strstr(appid, r->id))) { ++ if ((!r->title || regex_match(r->title, title)) ++ && (!r->id || regex_match(r->id, appid))) { + c->isfloating = r->isfloating; + newtags |= r->tags; + i = 0; +@@ -2929,6 +2931,19 @@ zoom(const Arg *arg) + arrange(selmon); + } + ++int ++regex_match(const char *pattern, const char *str) { ++ regex_t regex; ++ int reti; ++ if (regcomp(®ex, pattern, REG_EXTENDED) != 0) ++ return 0; ++ reti = regexec(®ex, str, (size_t)0, NULL, 0); ++ regfree(®ex); ++ if (reti == 0) ++ return 1; ++ return 0; ++} ++ + #ifdef XWAYLAND + void + activatex11(struct wl_listener *listener, void *data) +-- +2.43.2 -- cgit v1.2.1