feat: use regex to match no strstr

This commit is contained in:
DreamMaoMao 2025-05-18 18:26:06 +08:00
parent aa0e1e1fe9
commit 64b15fe4e7
5 changed files with 59 additions and 18 deletions

View file

@ -57,7 +57,7 @@ https://github.com/user-attachments/assets/c9bf9415-fad1-4400-bcdc-3ad2d76de85a
## depend ## depend
```bash ```bash
yay -S glibc wayland libinput libdrm pixman libxkbcommon git meson ninja wayland-protocols libdisplay-info libliftoff hwdata seatd yay -S glibc wayland libinput libdrm pixman libxkbcommon git meson ninja wayland-protocols libdisplay-info libliftoff hwdata seatd pcre2
``` ```
## arch ## arch

View file

@ -37,6 +37,7 @@ wlroots_dep = dependency('wlroots-0.19')
xkbcommon_dep = dependency('xkbcommon') xkbcommon_dep = dependency('xkbcommon')
libinput_dep = dependency('libinput') libinput_dep = dependency('libinput')
libwayland_client_dep = dependency('wayland-client') libwayland_client_dep = dependency('wayland-client')
pcre2_dep = dependency('libpcre2-8')
# 获取 Git Commit Hash 和最新的 tag # 获取 Git Commit Hash 和最新的 tag
git = find_program('git', required : false) git = find_program('git', required : false)
@ -79,6 +80,7 @@ executable('maomao',
xkbcommon_dep, xkbcommon_dep,
libinput_dep, libinput_dep,
libwayland_client_dep, libwayland_client_dep,
pcre2_dep,
], ],
install : true, install : true,
c_args : c_args c_args : c_args

View file

@ -7,6 +7,12 @@
#include "util.h" #include "util.h"
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
#include <wchar.h>
void die(const char *fmt, ...) { void die(const char *fmt, ...) {
va_list ap; va_list ap;
@ -45,3 +51,34 @@ int fd_set_nonblock(int fd) {
return 0; return 0;
} }
int regex_match(const char *pattern, const char *str) {
int errnum;
PCRE2_SIZE erroffset;
pcre2_code *re = pcre2_compile(
(PCRE2_SPTR)pattern,
PCRE2_ZERO_TERMINATED,
PCRE2_UTF, // 启用 UTF-8 支持
&errnum, &erroffset, NULL
);
if (!re) {
PCRE2_UCHAR errbuf[256];
pcre2_get_error_message(errnum, errbuf, sizeof(errbuf));
fprintf(stderr, "PCRE2 error: %s at offset %zu\n", errbuf, erroffset);
return 0;
}
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, NULL);
int ret = pcre2_match(
re,
(PCRE2_SPTR)str,
strlen(str),
0, 0,
match_data,
NULL
);
pcre2_match_data_free(match_data);
pcre2_code_free(re);
return ret >= 0;
}

View file

@ -1,5 +1,7 @@
/* See LICENSE.dwm file for copyright and license details. */ /* See LICENSE.dwm file for copyright and license details. */
void die(const char *fmt, ...); void die(const char *fmt, ...);
void *ecalloc(size_t nmemb, size_t size); void *ecalloc(size_t nmemb, size_t size);
int fd_set_nonblock(int fd); int fd_set_nonblock(int fd);
int regex_match(const char *pattern_mb, const char *str_mb);

View file

@ -1579,10 +1579,10 @@ Client *get_client_by_id_or_title(const char *arg_id, const char *arg_title) {
if (arg_title && strncmp(arg_title, "none", 4) == 0) if (arg_title && strncmp(arg_title, "none", 4) == 0)
arg_title = NULL; arg_title = NULL;
if ((arg_title && strstr(title, arg_title) && !arg_id) || if ((arg_title && regex_match(arg_title, title) && !arg_id) ||
(arg_id && strstr(appid, arg_id) && !arg_title) || (arg_id && regex_match(arg_id, appid) && !arg_title) ||
(arg_id && strstr(appid, arg_id) && arg_title && (arg_id && regex_match(arg_id, appid) && arg_title &&
strstr(title, arg_title))) { regex_match(arg_title, title))) {
target_client = c; target_client = c;
break; break;
} }
@ -1774,10 +1774,10 @@ applyrulesgeom(Client *c) {
if (config.window_rules_count < 1) if (config.window_rules_count < 1)
break; break;
r = &config.window_rules[ji]; r = &config.window_rules[ji];
if ((r->title && strstr(title, r->title) && !r->id) || if ((regex_match(r->title,title) && !r->id) ||
(r->id && strstr(appid, r->id) && !r->title) || (r->id && regex_match(r->id,appid) && !r->title) ||
(r->id && strstr(appid, r->id) && r->title && (r->id && regex_match(r->id,appid) && r->title &&
strstr(title, r->title))) { regex_match(r->title,title))) {
c->geom.width = r->width > 0 ? r->width : c->geom.width; c->geom.width = r->width > 0 ? r->width : c->geom.width;
c->geom.height = r->height > 0 ? r->height : c->geom.height; c->geom.height = r->height > 0 ? r->height : c->geom.height;
// 重新计算居中的坐标 // 重新计算居中的坐标
@ -1814,10 +1814,10 @@ applyrules(Client *c) {
break; break;
r = &config.window_rules[ji]; r = &config.window_rules[ji];
if ((r->title && strstr(title, r->title) && !r->id) || if ((r->title && regex_match(r->title,title) && !r->id) ||
(r->id && strstr(appid, r->id) && !r->title) || (r->id && regex_match(r->id,appid) && !r->title) ||
(r->id && strstr(appid, r->id) && r->title && (r->id && regex_match(r->id,appid) && r->title &&
strstr(title, r->title))) { regex_match(r->title,title))) {
c->isterm = r->isterm > 0 ? r->isterm : c->isterm; c->isterm = r->isterm > 0 ? r->isterm : c->isterm;
c->noswallow = r->noswallow > 0 ? r->noswallow : c->noswallow; c->noswallow = r->noswallow > 0 ? r->noswallow : c->noswallow;
c->scratchpad_geom.width = c->scratchpad_geom.width =
@ -3244,7 +3244,7 @@ void createmon(struct wl_listener *listener, void *data) {
break; break;
r = &config.monitor_rules[ji]; r = &config.monitor_rules[ji];
if (!r->name || strstr(wlr_output->name, r->name)) { if (!r->name || regex_match(r->name, wlr_output->name)) {
m->mfact = r->mfact; m->mfact = r->mfact;
m->nmaster = r->nmaster; m->nmaster = r->nmaster;
m->m.x = r->x; m->m.x = r->x;
@ -4125,10 +4125,10 @@ bool keypressglobal(struct wlr_surface *last_surface,
appid = client_get_appid(c); appid = client_get_appid(c);
title = client_get_title(c); title = client_get_title(c);
if ((r->title && strstr(title, r->title) && !r->id) || if ((r->title && regex_match(r->title, title) && !r->id) ||
(r->id && strstr(appid, r->id) && !r->title) || (r->id && regex_match(r->id, appid) && !r->title) ||
(r->id && strstr(appid, r->id) && r->title && (r->id && regex_match(r->id, appid) && r->title &&
strstr(title, r->title))) { regex_match(r->title, title))) {
reset = true; reset = true;
wlr_seat_keyboard_enter(seat, client_surface(c), keycodes, 0, wlr_seat_keyboard_enter(seat, client_surface(c), keycodes, 0,
&keyboard->modifiers); &keyboard->modifiers);