diff --git a/include/sway/criteria.h b/include/sway/criteria.h index 59f57f94f..2205f0ddd 100644 --- a/include/sway/criteria.h +++ b/include/sway/criteria.h @@ -35,6 +35,7 @@ struct criteria { struct pattern *shell; struct pattern *app_id; struct pattern *con_mark; + char *trigger; // event that triggered this criteria match uint32_t con_id; // internal ID #if HAVE_XWAYLAND struct pattern *class; @@ -72,11 +73,11 @@ struct criteria *criteria_parse(char *raw, char **error); * * Criteria types can be bitwise ORed. */ -list_t *criteria_for_view(struct sway_view *view, enum criteria_type types); +list_t *criteria_for_view(struct sway_view *view, enum criteria_type types, const char* trigger); /** * Compile a list of containers matching the given criteria. */ -list_t *criteria_get_containers(struct criteria *criteria); +list_t *criteria_get_containers(struct criteria *criteria, const char* trigger); #endif diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index 0dcbf1aa6..c1d771e60 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -354,7 +354,7 @@ void view_update_title(struct sway_view *view, bool force); * Run any criteria that match the view and haven't been run on this view * before. */ -void view_execute_criteria(struct sway_view *view); +void view_execute_criteria(struct sway_view *view, const char* trigger); /** * Returns true if there's a possibility the view may be rendered on screen. diff --git a/sway/commands.c b/sway/commands.c index 2160a970f..b0074e301 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -241,7 +241,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat, goto cleanup; } list_free(containers); - containers = criteria_get_containers(criteria); + containers = criteria_get_containers(criteria, "command"); head += strlen(criteria->raw); criteria_destroy(criteria); using_criteria = true; diff --git a/sway/commands/mark.c b/sway/commands/mark.c index aa5f185c8..bc68586dc 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -61,7 +61,7 @@ struct cmd_results *cmd_mark(int argc, char **argv) { free(mark); container_update_marks_textures(container); if (container->view) { - view_execute_criteria(container->view); + view_execute_criteria(container->view, "mark"); } return cmd_results_new(CMD_SUCCESS, NULL); diff --git a/sway/criteria.c b/sway/criteria.c index 97cf667e7..458f51a21 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -21,6 +21,7 @@ bool criteria_is_empty(struct criteria *criteria) { && !criteria->shell && !criteria->app_id && !criteria->con_mark + && !criteria->trigger && !criteria->con_id #if HAVE_XWAYLAND && !criteria->class @@ -96,6 +97,7 @@ void criteria_destroy(struct criteria *criteria) { pattern_destroy(criteria->window_role); #endif pattern_destroy(criteria->con_mark); + free(criteria->trigger); free(criteria->workspace); free(criteria->cmdlist); free(criteria->raw); @@ -181,11 +183,15 @@ static bool criteria_matches_container(struct criteria *criteria, } static bool criteria_matches_view(struct criteria *criteria, - struct sway_view *view) { + struct sway_view *view, const char* trigger) { struct sway_seat *seat = input_manager_current_seat(); struct sway_container *focus = seat_get_focused_container(seat); struct sway_view *focused = focus ? focus->view : NULL; + if (criteria->trigger && trigger && strcmp(criteria->trigger, trigger)) { + return false; + } + if (criteria->title) { const char *title = view_get_title(view); if (!title) { @@ -386,12 +392,12 @@ static bool criteria_matches_view(struct criteria *criteria, return true; } -list_t *criteria_for_view(struct sway_view *view, enum criteria_type types) { +list_t *criteria_for_view(struct sway_view *view, enum criteria_type types, const char* trigger) { list_t *criterias = config->criteria; list_t *matches = create_list(); for (int i = 0; i < criterias->length; ++i) { struct criteria *criteria = criterias->items[i]; - if ((criteria->type & types) && criteria_matches_view(criteria, view)) { + if ((criteria->type & types) && criteria_matches_view(criteria, view, trigger)) { list_add(matches, criteria); } } @@ -401,13 +407,14 @@ list_t *criteria_for_view(struct sway_view *view, enum criteria_type types) { struct match_data { struct criteria *criteria; list_t *matches; + const char* trigger; }; static void criteria_get_containers_iterator(struct sway_container *container, void *data) { struct match_data *match_data = data; if (container->view) { - if (criteria_matches_view(match_data->criteria, container->view)) { + if (criteria_matches_view(match_data->criteria, container->view, match_data->trigger)) { list_add(match_data->matches, container); } } else if (has_container_criteria(match_data->criteria)) { @@ -417,11 +424,12 @@ static void criteria_get_containers_iterator(struct sway_container *container, } } -list_t *criteria_get_containers(struct criteria *criteria) { +list_t *criteria_get_containers(struct criteria *criteria, const char* trigger) { list_t *matches = create_list(); struct match_data data = { .criteria = criteria, .matches = matches, + .trigger = trigger, }; root_for_each_container(criteria_get_containers_iterator, &data); return matches; @@ -472,6 +480,7 @@ enum criteria_token { T_URGENT, T_WORKSPACE, T_PID, + T_TRIGGER, T_INVALID, }; @@ -481,6 +490,8 @@ static enum criteria_token token_from_name(char *name) { return T_APP_ID; } else if (strcmp(name, "con_id") == 0) { return T_CON_ID; + } else if (strcmp(name, "trigger") == 0) { + return T_TRIGGER; } else if (strcmp(name, "con_mark") == 0) { return T_CON_MARK; #if HAVE_XWAYLAND @@ -556,6 +567,9 @@ static bool parse_token(struct criteria *criteria, char *name, char *value) { } } break; + case T_TRIGGER: + criteria->trigger = strdup(value); + break; case T_CON_MARK: pattern_create(&criteria->con_mark, value); break; diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c index 8da922d50..46bf73cbf 100644 --- a/sway/desktop/xdg_shell.c +++ b/sway/desktop/xdg_shell.c @@ -308,14 +308,14 @@ static void handle_set_title(struct wl_listener *listener, void *data) { wl_container_of(listener, xdg_shell_view, set_title); struct sway_view *view = &xdg_shell_view->view; view_update_title(view, false); - view_execute_criteria(view); + view_execute_criteria(view, "title"); } static void handle_set_app_id(struct wl_listener *listener, void *data) { struct sway_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, set_app_id); struct sway_view *view = &xdg_shell_view->view; - view_execute_criteria(view); + view_execute_criteria(view, "app_id"); } static void handle_new_popup(struct wl_listener *listener, void *data) { diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 7c5dde530..66121d50c 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -641,7 +641,7 @@ static void handle_set_title(struct wl_listener *listener, void *data) { return; } view_update_title(view, false); - view_execute_criteria(view); + view_execute_criteria(view, "title"); } static void handle_set_class(struct wl_listener *listener, void *data) { @@ -652,7 +652,7 @@ static void handle_set_class(struct wl_listener *listener, void *data) { if (!xsurface->mapped) { return; } - view_execute_criteria(view); + view_execute_criteria(view, "class"); } static void handle_set_role(struct wl_listener *listener, void *data) { @@ -663,7 +663,7 @@ static void handle_set_role(struct wl_listener *listener, void *data) { if (!xsurface->mapped) { return; } - view_execute_criteria(view); + view_execute_criteria(view, "window_role"); } static void handle_set_window_type(struct wl_listener *listener, void *data) { @@ -674,7 +674,7 @@ static void handle_set_window_type(struct wl_listener *listener, void *data) { if (!xsurface->mapped) { return; } - view_execute_criteria(view); + view_execute_criteria(view, "window_type"); } static void handle_set_hints(struct wl_listener *listener, void *data) { diff --git a/sway/tree/view.c b/sway/tree/view.c index 7d9e038d2..6d2662045 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -490,8 +490,8 @@ static bool view_has_executed_criteria(struct sway_view *view, return false; } -void view_execute_criteria(struct sway_view *view) { - list_t *criterias = criteria_for_view(view, CT_COMMAND); +void view_execute_criteria(struct sway_view *view, const char* trigger) { + list_t *criterias = criteria_for_view(view, CT_COMMAND, trigger); for (int i = 0; i < criterias->length; i++) { struct criteria *criteria = criterias->items[i]; sway_log(SWAY_DEBUG, "Checking criteria %s", criteria->raw); @@ -538,7 +538,7 @@ static struct sway_workspace *select_workspace(struct sway_view *view) { // Check if there's any `assign` criteria for the view list_t *criterias = criteria_for_view(view, - CT_ASSIGN_WORKSPACE | CT_ASSIGN_WORKSPACE_NUMBER | CT_ASSIGN_OUTPUT); + CT_ASSIGN_WORKSPACE | CT_ASSIGN_WORKSPACE_NUMBER | CT_ASSIGN_OUTPUT, NULL); struct sway_workspace *ws = NULL; for (int i = 0; i < criterias->length; ++i) { struct criteria *criteria = criterias->items[i]; @@ -623,7 +623,7 @@ static bool should_focus(struct sway_view *view) { } // Check no_focus criteria - list_t *criterias = criteria_for_view(view, CT_NO_FOCUS); + list_t *criterias = criteria_for_view(view, CT_NO_FOCUS, NULL); size_t len = criterias->length; list_free(criterias); return len == 0; @@ -820,7 +820,7 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface, } } - view_execute_criteria(view); + view_execute_criteria(view, "map"); bool set_focus = should_focus(view);