Merge pull request #1196 from SirCmpwn/no_focus

Implement no_focus
master
Drew DeVault 8 years ago committed by GitHub
commit 11b4c3454c

@ -135,6 +135,7 @@ sway_cmd cmd_mouse_warping;
sway_cmd cmd_move; sway_cmd cmd_move;
sway_cmd cmd_new_float; sway_cmd cmd_new_float;
sway_cmd cmd_new_window; sway_cmd cmd_new_window;
sway_cmd cmd_no_focus;
sway_cmd cmd_orientation; sway_cmd cmd_orientation;
sway_cmd cmd_output; sway_cmd cmd_output;
sway_cmd cmd_permit; sway_cmd cmd_permit;

@ -250,6 +250,7 @@ struct sway_config {
list_t *output_configs; list_t *output_configs;
list_t *input_configs; list_t *input_configs;
list_t *criteria; list_t *criteria;
list_t *no_focus;
list_t *active_bar_modifiers; list_t *active_bar_modifiers;
struct sway_mode *current_mode; struct sway_mode *current_mode;
struct bar_config *current_bar; struct bar_config *current_bar;

@ -36,4 +36,7 @@ list_t *criteria_for(swayc_t *cont);
// Returns a list of all containers that match the given list of tokens. // Returns a list of all containers that match the given list of tokens.
list_t *container_for(list_t *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 #endif

@ -198,6 +198,7 @@ static struct cmd_handler handlers[] = {
{ "move", cmd_move }, { "move", cmd_move },
{ "new_float", cmd_new_float }, { "new_float", cmd_new_float },
{ "new_window", cmd_new_window }, { "new_window", cmd_new_window },
{ "no_focus", cmd_no_focus },
{ "output", cmd_output }, { "output", cmd_output },
{ "permit", cmd_permit }, { "permit", cmd_permit },
{ "reject", cmd_reject }, { "reject", cmd_reject },

@ -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);
}

@ -249,6 +249,11 @@ void free_config(struct sway_config *config) {
} }
list_free(config->criteria); 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) { for (i = 0; config->input_configs && i < config->input_configs->length; ++i) {
free_input_config(config->input_configs->items[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->workspace_outputs = create_list())) goto cleanup;
if (!(config->pid_workspaces = create_list())) goto cleanup; if (!(config->pid_workspaces = create_list())) goto cleanup;
if (!(config->criteria = 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->input_configs = create_list())) goto cleanup;
if (!(config->output_configs = create_list())) goto cleanup; if (!(config->output_configs = create_list())) goto cleanup;

@ -359,6 +359,16 @@ void free_criteria(struct criteria *crit) {
free(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_for(swayc_t *cont) {
list_t *criteria = config->criteria, *matches = create_list(); list_t *criteria = config->criteria, *matches = create_list();
for (int i = 0; i < criteria->length; i++) { for (int i = 0; i < criteria->length; i++) {

@ -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) { if (!focused || focused->type == C_OUTPUT) {
focused = get_focused_container(&root_container); focused = prev_focus;
// Move focus from floating view // Move focus from floating view
if (focused->is_floating) { if (focused->is_floating) {
// To workspace if there are no children // To workspace if there are no children
@ -500,6 +502,16 @@ static bool handle_view_created(wlc_handle handle) {
workspace_switch(current_ws); workspace_switch(current_ws);
set_focused_container(get_focused_container(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; suspend_workspace_cleanup = false;
ws_cleanup(); ws_cleanup();

@ -336,6 +336,10 @@ The default colors are:
When _output_: place mouse at center of newly focused window when changing When _output_: place mouse at center of newly focused window when changing
output. When _none_: don't move mouse. output. When _none_: don't move mouse.
**no_focus** <criteria>::
Prevents windows matching <criteria> from being focused automatically when
they're created. This does not apply to the first window in a workspace.
**output** <name> <resolution|res> <WIDTHxHEIGHT>:: **output** <name> <resolution|res> <WIDTHxHEIGHT>::
Configures the specified output to use the given resolution. Configures the specified output to use the given resolution.
+ +

Loading…
Cancel
Save