criteria: use a strict regex match string for __focused__ value

This commit is contained in:
Ronan Pigott 2019-09-24 13:41:04 -07:00
parent 08b3eaf930
commit e77c5d2610

View file

@ -55,6 +55,17 @@ static int regex_cmp(const char *item, const pcre *regex) {
return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0); return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0);
} }
static char *strict_regex(const char *str) {
size_t size = snprintf(NULL, 0, "^%s$", str) + 1;
char *reg = malloc(size);
if (!*reg) {
sway_log(SWAY_ERROR, "Unable to allocate regex for criteria");
return NULL;
}
snprintf(reg, size, "^%s$", str);
return reg;
}
#if HAVE_XWAYLAND #if HAVE_XWAYLAND
static bool view_has_window_type(struct sway_view *view, enum atom_name name) { static bool view_has_window_type(struct sway_view *view, enum atom_name name) {
if (view->type != SWAY_VIEW_XWAYLAND) { if (view->type != SWAY_VIEW_XWAYLAND) {
@ -376,31 +387,31 @@ static char *get_focused_prop(enum criteria_token token, bool *autofail) {
struct sway_container *focus = seat_get_focused_container(seat); struct sway_container *focus = seat_get_focused_container(seat);
struct sway_view *view = focus ? focus->view : NULL; struct sway_view *view = focus ? focus->view : NULL;
const char *value = NULL; char *value = NULL;
switch (token) { switch (token) {
case T_APP_ID: case T_APP_ID:
*autofail = true; *autofail = true;
if (view) { if (view) {
value = view_get_app_id(view); value = strict_regex(view_get_app_id(view));
} }
break; break;
case T_SHELL: case T_SHELL:
*autofail = true; *autofail = true;
if (view) { if (view) {
value = view_get_shell(view); value = strict_regex(view_get_shell(view));
} }
break; break;
case T_TITLE: case T_TITLE:
*autofail = true; *autofail = true;
if (view) { if (view) {
value = view_get_title(view); value = strict_regex(view_get_title(view));
} }
break; break;
case T_WORKSPACE: case T_WORKSPACE:
*autofail = true; *autofail = true;
if (focus && focus->workspace) { if (focus && focus->workspace) {
value = focus->workspace->name; value = strict_regex(focus->workspace->name);
} }
break; break;
case T_CON_ID: case T_CON_ID:
@ -417,19 +428,19 @@ static char *get_focused_prop(enum criteria_token token, bool *autofail) {
case T_CLASS: case T_CLASS:
*autofail = true; *autofail = true;
if (view) { if (view) {
value = view_get_class(view); value = strict_regex(view_get_class(view));
} }
break; break;
case T_INSTANCE: case T_INSTANCE:
*autofail = true; *autofail = true;
if (view) { if (view) {
value = view_get_instance(view); value = strict_regex(view_get_instance(view));
} }
break; break;
case T_WINDOW_ROLE: case T_WINDOW_ROLE:
*autofail = true; *autofail = true;
if (view) { if (view) {
value = view_get_window_role(view); value = strict_regex(view_get_window_role(view));
} }
break; break;
case T_WINDOW_TYPE: // These do not support __focused__ case T_WINDOW_TYPE: // These do not support __focused__
@ -444,7 +455,7 @@ static char *get_focused_prop(enum criteria_token token, bool *autofail) {
break; break;
} }
if (value) { if (value) {
return strdup(value); return value;
} }
return NULL; return NULL;
} }