diff options
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands.c | 1 | ||||
-rw-r--r-- | sway/commands/no_focus.c | 41 | ||||
-rw-r--r-- | sway/config.c | 6 | ||||
-rw-r--r-- | sway/criteria.c | 10 | ||||
-rw-r--r-- | sway/handlers.c | 12 |
5 files changed, 67 insertions, 3 deletions
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 <string.h> +#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..5e031321 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 @@ -458,7 +460,11 @@ static bool handle_view_created(wlc_handle handle) { if (newview) { ipc_event_window(newview, "new"); - set_focused_container(newview); + swayc_t *workspace = swayc_parent_by_type(newview, C_WORKSPACE); + if ((workspace && workspace->children->length == 1) + || !criteria_any(newview, config->no_focus)) { + set_focused_container(newview); + } wlc_view_set_mask(handle, VISIBLE); swayc_t *output = swayc_parent_by_type(newview, C_OUTPUT); arrange_windows(output, -1, -1); @@ -477,7 +483,7 @@ static bool handle_view_created(wlc_handle handle) { // refocus in-between command lists set_focused_container(newview); } - swayc_t *workspace = swayc_parent_by_type(focused, C_WORKSPACE); + workspace = swayc_parent_by_type(focused, C_WORKSPACE); if (workspace && workspace->fullscreen) { set_focused_container(workspace->fullscreen); } |