mirror of
				https://github.com/DreamMaoMao/maomaowm.git
				synced 2025-10-29 05:40:21 -04:00 
			
		
		
		
	feat: use regex to match no strstr
This commit is contained in:
		
							parent
							
								
									aa0e1e1fe9
								
							
						
					
					
						commit
						64b15fe4e7
					
				
					 5 changed files with 59 additions and 18 deletions
				
			
		|  | @ -57,7 +57,7 @@ https://github.com/user-attachments/assets/c9bf9415-fad1-4400-bcdc-3ad2d76de85a | |||
| ## depend | ||||
| 
 | ||||
| ```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 | ||||
|  |  | |||
|  | @ -37,6 +37,7 @@ wlroots_dep = dependency('wlroots-0.19') | |||
| xkbcommon_dep = dependency('xkbcommon') | ||||
| libinput_dep = dependency('libinput') | ||||
| libwayland_client_dep = dependency('wayland-client') | ||||
| pcre2_dep = dependency('libpcre2-8') | ||||
| 
 | ||||
| # 获取 Git Commit Hash 和最新的 tag | ||||
| git = find_program('git', required : false) | ||||
|  | @ -79,6 +80,7 @@ executable('maomao', | |||
|     xkbcommon_dep, | ||||
|     libinput_dep, | ||||
|     libwayland_client_dep, | ||||
|     pcre2_dep, | ||||
|   ], | ||||
|   install : true, | ||||
|   c_args : c_args | ||||
|  |  | |||
|  | @ -7,6 +7,12 @@ | |||
| 
 | ||||
| #include "util.h" | ||||
| 
 | ||||
| #define PCRE2_CODE_UNIT_WIDTH 8 | ||||
| #include <pcre2.h> | ||||
| #include <wchar.h> | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| void die(const char *fmt, ...) { | ||||
|   va_list ap; | ||||
| 
 | ||||
|  | @ -45,3 +51,34 @@ int fd_set_nonblock(int fd) { | |||
| 
 | ||||
|   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; | ||||
| } | ||||
|  |  | |||
|  | @ -1,5 +1,7 @@ | |||
| /* See LICENSE.dwm file for copyright and license details. */ | ||||
| 
 | ||||
| 
 | ||||
| void die(const char *fmt, ...); | ||||
| void *ecalloc(size_t nmemb, size_t size); | ||||
| int fd_set_nonblock(int fd); | ||||
| int regex_match(const char *pattern_mb, const char *str_mb); | ||||
|  |  | |||
							
								
								
									
										34
									
								
								src/maomao.c
									
										
									
									
									
								
							
							
						
						
									
										34
									
								
								src/maomao.c
									
										
									
									
									
								
							|  | @ -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) | ||||
|       arg_title = NULL; | ||||
| 
 | ||||
|     if ((arg_title && strstr(title, arg_title) && !arg_id) || | ||||
|         (arg_id && strstr(appid, arg_id) && !arg_title) || | ||||
|         (arg_id && strstr(appid, arg_id) && arg_title && | ||||
|          strstr(title, arg_title))) { | ||||
|     if ((arg_title && regex_match(arg_title, title) && !arg_id) || | ||||
|         (arg_id && regex_match(arg_id, appid) && !arg_title) || | ||||
|         (arg_id && regex_match(arg_id, appid) && arg_title && | ||||
|          regex_match(arg_title, title))) { | ||||
|       target_client = c; | ||||
|       break; | ||||
|     } | ||||
|  | @ -1774,10 +1774,10 @@ applyrulesgeom(Client *c) { | |||
|     if (config.window_rules_count < 1) | ||||
|       break; | ||||
|     r = &config.window_rules[ji]; | ||||
|     if ((r->title && strstr(title, r->title) && !r->id) || | ||||
|         (r->id && strstr(appid, r->id) && !r->title) || | ||||
|         (r->id && strstr(appid, r->id) && r->title && | ||||
|          strstr(title, r->title))) { | ||||
|     if ((regex_match(r->title,title) && !r->id) || | ||||
|         (r->id && regex_match(r->id,appid) && !r->title) || | ||||
|         (r->id && regex_match(r->id,appid) && r->title && | ||||
|          regex_match(r->title,title))) { | ||||
|       c->geom.width = r->width > 0 ? r->width : c->geom.width; | ||||
|       c->geom.height = r->height > 0 ? r->height : c->geom.height; | ||||
|       // 重新计算居中的坐标
 | ||||
|  | @ -1814,10 +1814,10 @@ applyrules(Client *c) { | |||
|       break; | ||||
|     r = &config.window_rules[ji]; | ||||
| 
 | ||||
|     if ((r->title && strstr(title, r->title) && !r->id) || | ||||
|         (r->id && strstr(appid, r->id) && !r->title) || | ||||
|         (r->id && strstr(appid, r->id) && r->title && | ||||
|          strstr(title, r->title))) { | ||||
|     if ((r->title && regex_match(r->title,title) && !r->id) || | ||||
|         (r->id && regex_match(r->id,appid) && !r->title) || | ||||
|         (r->id && regex_match(r->id,appid) && r->title && | ||||
|          regex_match(r->title,title))) { | ||||
|       c->isterm = r->isterm > 0 ? r->isterm : c->isterm; | ||||
|       c->noswallow = r->noswallow > 0 ? r->noswallow : c->noswallow; | ||||
|       c->scratchpad_geom.width = | ||||
|  | @ -3244,7 +3244,7 @@ void createmon(struct wl_listener *listener, void *data) { | |||
|       break; | ||||
| 
 | ||||
|     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->nmaster = r->nmaster; | ||||
|       m->m.x = r->x; | ||||
|  | @ -4125,10 +4125,10 @@ bool keypressglobal(struct wlr_surface *last_surface, | |||
|           appid = client_get_appid(c); | ||||
|           title = client_get_title(c); | ||||
| 
 | ||||
|           if ((r->title && strstr(title, r->title) && !r->id) || | ||||
|               (r->id && strstr(appid, r->id) && !r->title) || | ||||
|               (r->id && strstr(appid, r->id) && r->title && | ||||
|                strstr(title, r->title))) { | ||||
|           if ((r->title && regex_match(r->title, title) && !r->id) || | ||||
|               (r->id && regex_match(r->id, appid) && !r->title) || | ||||
|               (r->id && regex_match(r->id, appid) && r->title && | ||||
|                regex_match(r->title, title))) { | ||||
|             reset = true; | ||||
|             wlr_seat_keyboard_enter(seat, client_surface(c), keycodes, 0, | ||||
|                                     &keyboard->modifiers); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 DreamMaoMao
						DreamMaoMao