Implement no_focus

Ref #2
master
Drew DeVault 8 years ago
parent 3c1fc00f12
commit 51143a75af

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

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

@ -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

@ -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 },

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

@ -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++) {

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

Loading…
Cancel
Save