mirror of
https://github.com/swaywm/sway.git
synced 2024-11-23 00:11:28 +00:00
Merge pull request #2283 from RyanDwyer/no-focus
Implement no_focus command
This commit is contained in:
commit
6a9ca6efa0
|
@ -9,6 +9,7 @@ enum criteria_type {
|
||||||
CT_COMMAND = 1 << 0,
|
CT_COMMAND = 1 << 0,
|
||||||
CT_ASSIGN_OUTPUT = 1 << 1,
|
CT_ASSIGN_OUTPUT = 1 << 1,
|
||||||
CT_ASSIGN_WORKSPACE = 1 << 2,
|
CT_ASSIGN_WORKSPACE = 1 << 2,
|
||||||
|
CT_NO_FOCUS = 1 << 3,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct criteria {
|
struct criteria {
|
||||||
|
|
|
@ -114,6 +114,7 @@ static struct cmd_handler handlers[] = {
|
||||||
{ "input", cmd_input },
|
{ "input", cmd_input },
|
||||||
{ "mode", cmd_mode },
|
{ "mode", cmd_mode },
|
||||||
{ "mouse_warping", cmd_mouse_warping },
|
{ "mouse_warping", cmd_mouse_warping },
|
||||||
|
{ "no_focus", cmd_no_focus },
|
||||||
{ "output", cmd_output },
|
{ "output", cmd_output },
|
||||||
{ "seat", cmd_seat },
|
{ "seat", cmd_seat },
|
||||||
{ "set", cmd_set },
|
{ "set", cmd_set },
|
||||||
|
|
26
sway/commands/no_focus.c
Normal file
26
sway/commands/no_focus.c
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
#define _XOPEN_SOURCE 500
|
||||||
|
#include <string.h>
|
||||||
|
#include "sway/commands.h"
|
||||||
|
#include "sway/criteria.h"
|
||||||
|
#include "list.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
|
struct cmd_results *cmd_no_focus(int argc, char **argv) {
|
||||||
|
struct cmd_results *error = NULL;
|
||||||
|
if ((error = checkarg(argc, "no_focus", EXPECTED_AT_LEAST, 1))) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *err_str = NULL;
|
||||||
|
struct criteria *criteria = criteria_parse(argv[0], &err_str);
|
||||||
|
if (!criteria) {
|
||||||
|
error = cmd_results_new(CMD_INVALID, "no_focus", err_str);
|
||||||
|
free(err_str);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
criteria->type = CT_NO_FOCUS;
|
||||||
|
list_add(config->criteria, criteria);
|
||||||
|
|
||||||
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
|
@ -59,6 +59,7 @@ sway_sources = files(
|
||||||
'commands/mode.c',
|
'commands/mode.c',
|
||||||
'commands/mouse_warping.c',
|
'commands/mouse_warping.c',
|
||||||
'commands/move.c',
|
'commands/move.c',
|
||||||
|
'commands/no_focus.c',
|
||||||
'commands/output.c',
|
'commands/output.c',
|
||||||
'commands/reload.c',
|
'commands/reload.c',
|
||||||
'commands/rename.c',
|
'commands/rename.c',
|
||||||
|
|
|
@ -504,20 +504,38 @@ void view_execute_criteria(struct sway_view *view) {
|
||||||
}
|
}
|
||||||
wlr_log(WLR_DEBUG, "for_window '%s' matches view %p, cmd: '%s'",
|
wlr_log(WLR_DEBUG, "for_window '%s' matches view %p, cmd: '%s'",
|
||||||
criteria->raw, view, criteria->cmdlist);
|
criteria->raw, view, criteria->cmdlist);
|
||||||
|
seat_set_focus(seat, view->swayc);
|
||||||
list_add(view->executed_criteria, criteria);
|
list_add(view->executed_criteria, criteria);
|
||||||
struct cmd_results *res = execute_command(criteria->cmdlist, NULL);
|
struct cmd_results *res = execute_command(criteria->cmdlist, NULL);
|
||||||
if (res->status != CMD_SUCCESS) {
|
if (res->status != CMD_SUCCESS) {
|
||||||
wlr_log(WLR_ERROR, "Command '%s' failed: %s", res->input, res->error);
|
wlr_log(WLR_ERROR, "Command '%s' failed: %s", res->input, res->error);
|
||||||
}
|
}
|
||||||
free_cmd_results(res);
|
free_cmd_results(res);
|
||||||
// view must be focused for commands to affect it,
|
|
||||||
// so always refocus in-between command lists
|
|
||||||
seat_set_focus(seat, view->swayc);
|
|
||||||
}
|
}
|
||||||
list_free(criterias);
|
list_free(criterias);
|
||||||
seat_set_focus(seat, prior_focus);
|
seat_set_focus(seat, prior_focus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool should_focus(struct sway_view *view) {
|
||||||
|
// If the view is the only one in the focused workspace, it'll get focus
|
||||||
|
// regardless of any no_focus criteria.
|
||||||
|
struct sway_container *parent = view->swayc->parent;
|
||||||
|
struct sway_seat *seat = input_manager_current_seat(input_manager);
|
||||||
|
if (parent->type == C_WORKSPACE && seat_get_focus(seat) == parent) {
|
||||||
|
size_t num_children = parent->children->length +
|
||||||
|
parent->sway_workspace->floating->children->length;
|
||||||
|
if (num_children == 1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check no_focus criteria
|
||||||
|
list_t *criterias = criteria_for_view(view, CT_NO_FOCUS);
|
||||||
|
size_t len = criterias->length;
|
||||||
|
list_free(criterias);
|
||||||
|
return len == 0;
|
||||||
|
}
|
||||||
|
|
||||||
void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) {
|
void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) {
|
||||||
if (!sway_assert(view->surface == NULL, "cannot map mapped view")) {
|
if (!sway_assert(view->surface == NULL, "cannot map mapped view")) {
|
||||||
return;
|
return;
|
||||||
|
@ -571,10 +589,12 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) {
|
||||||
view_set_tiled(view, true);
|
view_set_tiled(view, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (should_focus(view)) {
|
||||||
input_manager_set_focus(input_manager, cont);
|
input_manager_set_focus(input_manager, cont);
|
||||||
if (workspace) {
|
if (workspace) {
|
||||||
workspace_switch(workspace);
|
workspace_switch(workspace);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
view_update_title(view, false);
|
view_update_title(view, false);
|
||||||
container_notify_subtree_changed(view->swayc->parent);
|
container_notify_subtree_changed(view->swayc->parent);
|
||||||
|
|
Loading…
Reference in a new issue