diff options
-rw-r--r-- | include/sway/criteria.h | 5 | ||||
-rw-r--r-- | sway/commands.c | 1 | ||||
-rw-r--r-- | sway/commands/no_focus.c | 26 | ||||
-rw-r--r-- | sway/input/seat.c | 1 | ||||
-rw-r--r-- | sway/meson.build | 1 | ||||
-rw-r--r-- | sway/tree/view.c | 32 | ||||
-rw-r--r-- | swaylock/main.c | 244 | ||||
-rw-r--r-- | swaylock/swaylock.1.scd | 9 |
8 files changed, 254 insertions, 65 deletions
diff --git a/include/sway/criteria.h b/include/sway/criteria.h index bd3ca0ac..6a8337c5 100644 --- a/include/sway/criteria.h +++ b/include/sway/criteria.h @@ -6,9 +6,10 @@ #include "tree/view.h" enum criteria_type { - CT_COMMAND = 1 << 0, - CT_ASSIGN_OUTPUT = 1 << 1, + CT_COMMAND = 1 << 0, + CT_ASSIGN_OUTPUT = 1 << 1, CT_ASSIGN_WORKSPACE = 1 << 2, + CT_NO_FOCUS = 1 << 3, }; struct criteria { diff --git a/sway/commands.c b/sway/commands.c index 3578e748..27329602 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -114,6 +114,7 @@ static struct cmd_handler handlers[] = { { "input", cmd_input }, { "mode", cmd_mode }, { "mouse_warping", cmd_mouse_warping }, + { "no_focus", cmd_no_focus }, { "output", cmd_output }, { "seat", cmd_seat }, { "set", cmd_set }, diff --git a/sway/commands/no_focus.c b/sway/commands/no_focus.c new file mode 100644 index 00000000..61a8de7e --- /dev/null +++ b/sway/commands/no_focus.c @@ -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); +} diff --git a/sway/input/seat.c b/sway/input/seat.c index 7058cc92..12b1fab5 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -655,6 +655,7 @@ void seat_set_focus_warp(struct sway_seat *seat, while (parent) { wl_list_remove(&parent->link); wl_list_insert(&seat->focus_stack, &parent->link); + container_set_dirty(parent->container); parent = seat_container_from_container(seat, diff --git a/sway/meson.build b/sway/meson.build index b64bd137..23e54a62 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -59,6 +59,7 @@ sway_sources = files( 'commands/mode.c', 'commands/mouse_warping.c', 'commands/move.c', + 'commands/no_focus.c', 'commands/output.c', 'commands/reload.c', 'commands/rename.c', diff --git a/sway/tree/view.c b/sway/tree/view.c index b5b73408..76e0f42c 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -505,20 +505,38 @@ void view_execute_criteria(struct sway_view *view) { } wlr_log(WLR_DEBUG, "for_window '%s' matches view %p, cmd: '%s'", criteria->raw, view, criteria->cmdlist); + seat_set_focus(seat, view->swayc); list_add(view->executed_criteria, criteria); struct cmd_results *res = execute_command(criteria->cmdlist, NULL); if (res->status != CMD_SUCCESS) { wlr_log(WLR_ERROR, "Command '%s' failed: %s", res->input, res->error); } 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); 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) { if (!sway_assert(view->surface == NULL, "cannot map mapped view")) { return; @@ -572,9 +590,11 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { view_set_tiled(view, true); } - input_manager_set_focus(input_manager, cont); - if (workspace) { - workspace_switch(workspace); + if (should_focus(view)) { + input_manager_set_focus(input_manager, cont); + if (workspace) { + workspace_switch(workspace); + } } view_update_title(view, false); diff --git a/swaylock/main.c b/swaylock/main.c index faebc757..ae5b86b9 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -21,6 +21,7 @@ #include "pool-buffer.h" #include "cairo.h" #include "log.h" +#include "readline.h" #include "stringop.h" #include "util.h" #include "wlr-input-inhibitor-unstable-v1-client-protocol.h" @@ -412,15 +413,14 @@ static void set_default_colors(struct swaylock_colors *colors) { }; } -static struct swaylock_state state; - -int main(int argc, char **argv) { - enum line_mode { - LM_LINE, - LM_INSIDE, - LM_RING, - }; +enum line_mode { + LM_LINE, + LM_INSIDE, + LM_RING, +}; +static int parse_options(int argc, char **argv, struct swaylock_state *state, + enum line_mode *line_mode) { enum long_option_codes { LO_BS_HL_COLOR = 256, LO_FONT, @@ -447,6 +447,7 @@ int main(int argc, char **argv) { }; static struct option long_options[] = { + {"config", required_argument, NULL, 'C'}, {"color", required_argument, NULL, 'c'}, {"ignore-empty-password", no_argument, NULL, 'e'}, {"daemonize", no_argument, NULL, 'f'}, @@ -487,6 +488,8 @@ int main(int argc, char **argv) { const char usage[] = "Usage: swaylock [options...]\n" "\n" + " -C, --config <config_file> " + "Path to the config file.\n" " -c, --color <color> " "Turn the screen into the given color instead of white.\n" " -e, --ignore-empty-password " @@ -559,58 +562,48 @@ int main(int argc, char **argv) { "\n" "All <color> options are of the form <rrggbb[aa]>.\n"; - enum line_mode line_mode = LM_LINE; - state.args = (struct swaylock_args){ - .mode = BACKGROUND_MODE_SOLID_COLOR, - .font = strdup("sans-serif"), - .radius = 50, - .thickness = 10, - .ignore_empty = false, - .show_indicator = true, - }; - wl_list_init(&state.images); - set_default_colors(&state.args.colors); - - wlr_log_init(WLR_DEBUG, NULL); - int c; + optind = 1; while (1) { int opt_idx = 0; - c = getopt_long(argc, argv, "c:efhi:nrs:tuv", long_options, &opt_idx); + c = getopt_long(argc, argv, "c:efhi:nrs:tuvC:", long_options, &opt_idx); if (c == -1) { break; } switch (c) { + case 'C': + // Config file. This will have already been handled so just ignore. + break; case 'c': - state.args.colors.background = parse_color(optarg); - state.args.mode = BACKGROUND_MODE_SOLID_COLOR; + state->args.colors.background = parse_color(optarg); + state->args.mode = BACKGROUND_MODE_SOLID_COLOR; break; case 'e': - state.args.ignore_empty = true; + state->args.ignore_empty = true; break; case 'f': - state.args.daemonize = true; + state->args.daemonize = true; break; case 'i': - load_image(optarg, &state); + load_image(optarg, state); break; case 'n': - line_mode = LM_INSIDE; + *line_mode = LM_INSIDE; break; case 'r': - line_mode = LM_RING; + *line_mode = LM_RING; break; case 's': - state.args.mode = parse_background_mode(optarg); - if (state.args.mode == BACKGROUND_MODE_INVALID) { + state->args.mode = parse_background_mode(optarg); + if (state->args.mode == BACKGROUND_MODE_INVALID) { return 1; } break; case 't': - state.args.mode = BACKGROUND_MODE_TILE; + state->args.mode = BACKGROUND_MODE_TILE; break; case 'u': - state.args.show_indicator = false; + state->args.show_indicator = false; break; case 'v': #if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE @@ -621,71 +614,71 @@ int main(int argc, char **argv) { #endif return 0; case LO_BS_HL_COLOR: - state.args.colors.bs_highlight = parse_color(optarg); + state->args.colors.bs_highlight = parse_color(optarg); break; case LO_FONT: - free(state.args.font); - state.args.font = strdup(optarg); + free(state->args.font); + state->args.font = strdup(optarg); break; case LO_IND_RADIUS: - state.args.radius = strtol(optarg, NULL, 0); + state->args.radius = strtol(optarg, NULL, 0); break; case LO_IND_THICKNESS: - state.args.thickness = strtol(optarg, NULL, 0); + state->args.thickness = strtol(optarg, NULL, 0); break; case LO_INSIDE_COLOR: - state.args.colors.inside.input = parse_color(optarg); + state->args.colors.inside.input = parse_color(optarg); break; case LO_INSIDE_CLEAR_COLOR: - state.args.colors.inside.cleared = parse_color(optarg); + state->args.colors.inside.cleared = parse_color(optarg); break; case LO_INSIDE_VER_COLOR: - state.args.colors.inside.verifying = parse_color(optarg); + state->args.colors.inside.verifying = parse_color(optarg); break; case LO_INSIDE_WRONG_COLOR: - state.args.colors.inside.wrong = parse_color(optarg); + state->args.colors.inside.wrong = parse_color(optarg); break; case LO_KEY_HL_COLOR: - state.args.colors.key_highlight = parse_color(optarg); + state->args.colors.key_highlight = parse_color(optarg); break; case LO_LINE_COLOR: - state.args.colors.line.input = parse_color(optarg); + state->args.colors.line.input = parse_color(optarg); break; case LO_LINE_CLEAR_COLOR: - state.args.colors.line.cleared = parse_color(optarg); + state->args.colors.line.cleared = parse_color(optarg); break; case LO_LINE_VER_COLOR: - state.args.colors.line.verifying = parse_color(optarg); + state->args.colors.line.verifying = parse_color(optarg); break; case LO_LINE_WRONG_COLOR: - state.args.colors.line.wrong = parse_color(optarg); + state->args.colors.line.wrong = parse_color(optarg); break; case LO_RING_COLOR: - state.args.colors.ring.input = parse_color(optarg); + state->args.colors.ring.input = parse_color(optarg); break; case LO_RING_CLEAR_COLOR: - state.args.colors.ring.cleared = parse_color(optarg); + state->args.colors.ring.cleared = parse_color(optarg); break; case LO_RING_VER_COLOR: - state.args.colors.ring.verifying = parse_color(optarg); + state->args.colors.ring.verifying = parse_color(optarg); break; case LO_RING_WRONG_COLOR: - state.args.colors.ring.wrong = parse_color(optarg); + state->args.colors.ring.wrong = parse_color(optarg); break; case LO_SEP_COLOR: - state.args.colors.separator = parse_color(optarg); + state->args.colors.separator = parse_color(optarg); break; case LO_TEXT_COLOR: - state.args.colors.text.input = parse_color(optarg); + state->args.colors.text.input = parse_color(optarg); break; case LO_TEXT_CLEAR_COLOR: - state.args.colors.text.cleared = parse_color(optarg); + state->args.colors.text.cleared = parse_color(optarg); break; case LO_TEXT_VER_COLOR: - state.args.colors.text.verifying = parse_color(optarg); + state->args.colors.text.verifying = parse_color(optarg); break; case LO_TEXT_WRONG_COLOR: - state.args.colors.text.wrong = parse_color(optarg); + state->args.colors.text.wrong = parse_color(optarg); break; default: fprintf(stderr, "%s", usage); @@ -693,6 +686,143 @@ int main(int argc, char **argv) { } } + return 0; +} + +static bool file_exists(const char *path) { + return path && access(path, R_OK) != -1; +} + +static char *get_config_path(void) { + static const char *config_paths[] = { + "$HOME/.swaylock/config", + "$XDG_CONFIG_HOME/swaylock/config", + SYSCONFDIR "/swaylock/config", + }; + + if (!getenv("XDG_CONFIG_HOME")) { + char *home = getenv("HOME"); + char *config_home = malloc(strlen(home) + strlen("/.config") + 1); + if (!config_home) { + wlr_log(WLR_ERROR, "Unable to allocate $HOME/.config"); + } else { + strcpy(config_home, home); + strcat(config_home, "/.config"); + setenv("XDG_CONFIG_HOME", config_home, 1); + wlr_log(WLR_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); + free(config_home); + } + } + + wordexp_t p; + char *path; + for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { + if (wordexp(config_paths[i], &p, 0) == 0) { + path = strdup(p.we_wordv[0]); + wordfree(&p); + if (file_exists(path)) { + return path; + } + free(path); + } + } + + return NULL; +} + +static int load_config(char *path, struct swaylock_state *state, + enum line_mode *line_mode) { + FILE *config = fopen(path, "r"); + if (!config) { + wlr_log(WLR_ERROR, "Failed to read config. Running without it."); + return 0; + } + char *line; + int line_number = 0; + while (!feof(config)) { + line = read_line(config); + if (!line) { + continue; + } + + line_number++; + if (line[0] == '#') { + free(line); + continue; + } + if (strlen(line) == 0) { + free(line); + continue; + } + + wlr_log(WLR_DEBUG, "Config Line #%d: %s", line_number, line); + char flag[strlen(line) + 3]; + sprintf(flag, "--%s", line); + char *argv[] = {"swaylock", flag}; + int result = parse_options(2, argv, state, line_mode); + if (result != 0) { + free(line); + fclose(config); + return result; + } + free(line); + } + fclose(config); + return 0; +} + +static struct swaylock_state state; + +int main(int argc, char **argv) { + enum line_mode line_mode = LM_LINE; + state.args = (struct swaylock_args){ + .mode = BACKGROUND_MODE_SOLID_COLOR, + .font = strdup("sans-serif"), + .radius = 50, + .thickness = 10, + .ignore_empty = false, + .show_indicator = true, + }; + wl_list_init(&state.images); + set_default_colors(&state.args.colors); + + wlr_log_init(WLR_DEBUG, NULL); + + char *config_path = NULL; + static struct option long_options[] = { + {"config", required_argument, NULL, 'C'}, + {0, 0, 0, 0}, + }; + while (1) { + int c = getopt_long(argc, argv, "C:", long_options, NULL); + if (c == -1) { + break; + } else if (c == 'C') { + config_path = strdup(optarg); + break; + } + } + if (!config_path) { + config_path = get_config_path(); + } + + if (config_path) { + wlr_log(WLR_DEBUG, "Found config at %s", config_path); + int config_status = load_config(config_path, &state, &line_mode); + free(config_path); + if (config_status != 0) { + return config_status; + } + } + + if (argc > 1) { + wlr_log(WLR_DEBUG, "Parsing CLI Args"); + int result = parse_options(argc, argv, &state, &line_mode); + if (result != 0) { + return result; + } + } + if (line_mode == LM_INSIDE) { state.args.colors.line = state.args.colors.inside; } else if (line_mode == LM_RING) { diff --git a/swaylock/swaylock.1.scd b/swaylock/swaylock.1.scd index eea62c2a..3107124f 100644 --- a/swaylock/swaylock.1.scd +++ b/swaylock/swaylock.1.scd @@ -12,6 +12,15 @@ Locks your Wayland session. # OPTIONS +*-C, --config* <path> + The config file to use. By default, the following paths are checked: + _$HOME/.swaylock/config_, _$XDG\_CONFIG\_HOME/swaylock/config_, and + _SYSCONFDIR/swaylock/config_. All flags aside from this one are valid + options in the configuration file using the format _long-option=value_. + For options such as _ignore-empty-password_, just supply the _long-option_. + All leading dashes should be omitted and the equals sign is required for + flags that take an argument. + *-c, --color* <rrggbb[aa]> Turn the screen into the given color. If -i is used, this sets the background of the image to the given color. Defaults to white (FFFFFF), or |