diff --git a/include/sway/commands.h b/include/sway/commands.h index 91f2ae01..078652e7 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -135,6 +135,7 @@ sway_cmd cmd_mouse_warping; sway_cmd cmd_move; sway_cmd cmd_new_float; sway_cmd cmd_new_window; +sway_cmd cmd_no_focus; sway_cmd cmd_orientation; sway_cmd cmd_output; sway_cmd cmd_permit; diff --git a/include/sway/config.h b/include/sway/config.h index 2de90434..35f8d5f7 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -250,6 +250,7 @@ struct sway_config { list_t *output_configs; list_t *input_configs; list_t *criteria; + list_t *no_focus; list_t *active_bar_modifiers; struct sway_mode *current_mode; struct bar_config *current_bar; diff --git a/include/sway/criteria.h b/include/sway/criteria.h index 022c48a8..c5ed9857 100644 --- a/include/sway/criteria.h +++ b/include/sway/criteria.h @@ -36,4 +36,7 @@ list_t *criteria_for(swayc_t *cont); // Returns a list of all containers that match the given list of tokens. list_t *container_for(list_t *tokens); +// Returns true if any criteria in the given list matches this container +bool criteria_any(swayc_t *cont, list_t *criteria); + #endif diff --git a/sway/commands.c b/sway/commands.c index 4d7af301..01e5e6b5 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -198,6 +198,7 @@ static struct cmd_handler handlers[] = { { "move", cmd_move }, { "new_float", cmd_new_float }, { "new_window", cmd_new_window }, + { "no_focus", cmd_no_focus }, { "output", cmd_output }, { "permit", cmd_permit }, { "reject", cmd_reject }, diff --git a/sway/commands/no_focus.c b/sway/commands/no_focus.c new file mode 100644 index 00000000..b3b88e5a --- /dev/null +++ b/sway/commands/no_focus.c @@ -0,0 +1,41 @@ +#define _XOPEN_SOURCE 500 +#include +#include "sway/commands.h" +#include "sway/criteria.h" +#include "list.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_no_focus(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "no_focus", EXPECTED_EQUAL_TO, 1))) { + return error; + } + // add command to a criteria/command pair that is run against views when they appear. + char *criteria = argv[0]; + + struct criteria *crit = malloc(sizeof(struct criteria)); + if (!crit) { + return cmd_results_new(CMD_FAILURE, "no_focus", "Unable to allocate criteria"); + } + crit->crit_raw = strdup(criteria); + crit->tokens = create_list(); + crit->cmdlist = NULL; + char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); + + if (err_str) { + error = cmd_results_new(CMD_INVALID, "no_focus", err_str); + free(err_str); + free_criteria(crit); + } else if (crit->tokens->length == 0) { + error = cmd_results_new(CMD_INVALID, "no_focus", "Found no name/value pairs in criteria"); + free_criteria(crit); + } else if (list_seq_find(config->no_focus, criteria_cmp, crit) != -1) { + sway_log(L_DEBUG, "no_focus: Duplicate, skipping."); + free_criteria(crit); + } else { + sway_log(L_DEBUG, "no_focus: '%s' added", crit->crit_raw); + list_add(config->no_focus, crit); + } + return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index 0014b33a..19b1882f 100644 --- a/sway/config.c +++ b/sway/config.c @@ -249,6 +249,11 @@ void free_config(struct sway_config *config) { } list_free(config->criteria); + for (i = 0; config->no_focus && i < config->no_focus->length; ++i) { + free_criteria(config->no_focus->items[i]); + } + list_free(config->no_focus); + for (i = 0; config->input_configs && i < config->input_configs->length; ++i) { free_input_config(config->input_configs->items[i]); } @@ -291,6 +296,7 @@ static void config_defaults(struct sway_config *config) { if (!(config->workspace_outputs = create_list())) goto cleanup; if (!(config->pid_workspaces = create_list())) goto cleanup; if (!(config->criteria = create_list())) goto cleanup; + if (!(config->no_focus = create_list())) goto cleanup; if (!(config->input_configs = create_list())) goto cleanup; if (!(config->output_configs = create_list())) goto cleanup; diff --git a/sway/criteria.c b/sway/criteria.c index ee6d4d1c..1ea8311e 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -359,6 +359,16 @@ void free_criteria(struct criteria *crit) { free(crit); } +bool criteria_any(swayc_t *cont, list_t *criteria) { + for (int i = 0; i < criteria->length; i++) { + struct criteria *bc = criteria->items[i]; + if (criteria_test(cont, bc->tokens)) { + return true; + } + } + return false; +} + list_t *criteria_for(swayc_t *cont) { list_t *criteria = config->criteria, *matches = create_list(); for (int i = 0; i < criteria->length; i++) { diff --git a/sway/handlers.c b/sway/handlers.c index a8de135f..da765d6d 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -390,8 +390,10 @@ static bool handle_view_created(wlc_handle handle) { } } + swayc_t *prev_focus = get_focused_container(&root_container); + if (!focused || focused->type == C_OUTPUT) { - focused = get_focused_container(&root_container); + focused = prev_focus; // Move focus from floating view if (focused->is_floating) { // To workspace if there are no children @@ -500,6 +502,16 @@ static bool handle_view_created(wlc_handle handle) { workspace_switch(current_ws); set_focused_container(get_focused_container(current_ws)); } + if (prev_focus && prev_focus->type == C_VIEW + && newview && criteria_any(newview, config->no_focus)) { + // Restore focus + swayc_t *ws = swayc_parent_by_type(newview, C_WORKSPACE); + if (!ws || ws != newview->parent + || ws->children->length + ws->floating->length != 1) { + sway_log(L_DEBUG, "no_focus: restoring focus to %s", prev_focus->name); + set_focused_container(prev_focus); + } + } suspend_workspace_cleanup = false; ws_cleanup(); diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 4bc968e0..1fa0b856 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -336,6 +336,10 @@ The default colors are: When _output_: place mouse at center of newly focused window when changing output. When _none_: don't move mouse. +**no_focus** :: + Prevents windows matching from being focused automatically when + they're created. This does not apply to the first window in a workspace. + **output** :: Configures the specified output to use the given resolution. +