From 3a327c48c8785bb144f3b47efab6a62a00d90bb2 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Fri, 20 Apr 2018 12:28:07 +0200 Subject: [PATCH] Add DPMS and lock handling --- include/sway/commands.h | 3 ++ include/sway/config.h | 3 ++ include/sway/idle.h | 6 +++ sway/commands.c | 5 +- sway/commands/idle_timeout.c | 17 +++++++ sway/commands/lock_timeout.c | 17 +++++++ sway/commands/swaylock_command.c | 16 +++++++ sway/config.c | 3 ++ sway/idle.c | 79 ++++++++++++++++++++++++++++++++ sway/main.c | 2 + sway/meson.build | 4 ++ sway/server.c | 2 - 12 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 include/sway/idle.h create mode 100644 sway/commands/idle_timeout.c create mode 100644 sway/commands/lock_timeout.c create mode 100644 sway/commands/swaylock_command.c create mode 100644 sway/idle.c diff --git a/include/sway/commands.h b/include/sway/commands.h index dbebaa491..131993f43 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -147,6 +147,9 @@ sway_cmd cmd_unmark; sway_cmd cmd_workspace; sway_cmd cmd_ws_auto_back_and_forth; sway_cmd cmd_workspace_layout; +sway_cmd cmd_idle_timeout; +sway_cmd cmd_lock_timeout; +sway_cmd cmd_swaylock_command; sway_cmd bar_cmd_activate_button; sway_cmd bar_cmd_binding_mode_indicator; diff --git a/include/sway/config.h b/include/sway/config.h index ed49fbbd2..29cef1493 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -296,6 +296,9 @@ struct sway_config { enum sway_container_layout default_layout; char *font; int font_height; + uint32_t idle_timeout; + uint32_t lock_timeout; + char *swaylock_command; // Flags bool focus_follows_mouse; diff --git a/include/sway/idle.h b/include/sway/idle.h new file mode 100644 index 000000000..154e48f20 --- /dev/null +++ b/include/sway/idle.h @@ -0,0 +1,6 @@ +#ifndef _SWAY_IDLE_H +#define _SWAY_IDLE_H +#include + +bool idle_init(struct sway_server *server); +#endif diff --git a/sway/commands.c b/sway/commands.c index 99f425245..d6dbb11a9 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -99,12 +99,15 @@ static struct cmd_handler handlers[] = { { "exec", cmd_exec }, { "exec_always", cmd_exec_always }, { "focus_follows_mouse", cmd_focus_follows_mouse }, + { "idle_timeout", cmd_idle_timeout }, { "include", cmd_include }, { "input", cmd_input }, + { "lock_timeout", cmd_lock_timeout }, { "mode", cmd_mode }, { "mouse_warping", cmd_mouse_warping }, { "output", cmd_output }, { "seat", cmd_seat }, + { "swaylock_command", cmd_swaylock_command }, { "workspace", cmd_workspace }, { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }, }; @@ -416,7 +419,7 @@ struct cmd_results *config_command(char *exec, enum cmd_status block) { struct cmd_handler *handler = find_handler(argv[0], block); if (!handler) { char *input = argv[0] ? argv[0] : "(empty)"; - results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); + results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command %d", block); goto cleanup; } int i; diff --git a/sway/commands/idle_timeout.c b/sway/commands/idle_timeout.c new file mode 100644 index 000000000..e5ae17c1c --- /dev/null +++ b/sway/commands/idle_timeout.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_idle_timeout(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "idle_timeout", EXPECTED_EQUAL_TO, 1))) { + return error; + } + errno = 0; + config->idle_timeout = strtol(argv[0], NULL, 10); + if (errno == EINVAL || errno == ERANGE) + return cmd_results_new(CMD_INVALID, "idle_timeout", "Invalid timeout '%s'", argv[0]); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/lock_timeout.c b/sway/commands/lock_timeout.c new file mode 100644 index 000000000..b85157a22 --- /dev/null +++ b/sway/commands/lock_timeout.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_lock_timeout(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "lock_timeout", EXPECTED_EQUAL_TO, 1))) { + return error; + } + errno = 0; + config->lock_timeout = strtol(argv[0], NULL, 10); + if (errno == EINVAL || errno == ERANGE) + return cmd_results_new(CMD_INVALID, "lock_timeout", "Invalid lock timeout."); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/swaylock_command.c b/sway/commands/swaylock_command.c new file mode 100644 index 000000000..27063cd2e --- /dev/null +++ b/sway/commands/swaylock_command.c @@ -0,0 +1,16 @@ +#include +#include "sway/commands.h" +#include "log.h" +#include "stringop.h" + +struct cmd_results *cmd_swaylock_command(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "swaylock_command", EXPECTED_AT_LEAST, 1))) { + return error; + } + free(config->swaylock_command); + config->swaylock_command = join_args(argv, argc); + wlr_log(L_DEBUG, "Using custom swaylock command: %s", + config->swaylock_command); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config.c b/sway/config.c index 90b833ab7..952e11ef6 100644 --- a/sway/config.c +++ b/sway/config.c @@ -155,6 +155,9 @@ static void config_defaults(struct sway_config *config) { config->floating_mod = 0; config->dragging_key = BTN_LEFT; config->resizing_key = BTN_RIGHT; + config->idle_timeout = 600; + config->lock_timeout = 300; + config->swaylock_command = strdup("swaylock -c 000000"); if (!(config->floating_scroll_up_cmd = strdup(""))) goto cleanup; if (!(config->floating_scroll_down_cmd = strdup(""))) goto cleanup; if (!(config->floating_scroll_left_cmd = strdup(""))) goto cleanup; diff --git a/sway/idle.c b/sway/idle.c new file mode 100644 index 000000000..1212225be --- /dev/null +++ b/sway/idle.c @@ -0,0 +1,79 @@ +#define _POSIX_C_SOURCE 200112L +#include +#include +#include +#include +#include +#include +#include +#include + +void invoke_swaylock() { + int pid = fork(); + if (pid == 0) { + char *const cmd[] = { "sh", "-c", config->swaylock_command, NULL, }; + execvp(cmd[0], cmd); + exit(1); + } + wlr_log(L_DEBUG, "Spawned swaylock %d", pid); +} + +static int handle_idle(void* data) { + wlr_log(L_ERROR, "Idle state"); + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *cont = root_container.children->items[i]; + if (cont->type != C_OUTPUT) { + continue; + } + if (cont->sway_output && cont->sway_output->wlr_output) { + wlr_output_enable(cont->sway_output->wlr_output, false); + } + } + return 0; +} + +static int handle_resume(void *data) { + wlr_log(L_ERROR, "Active state"); + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *cont = root_container.children->items[i]; + if (cont->type != C_OUTPUT) { + continue; + } + if (cont->sway_output && cont->sway_output->wlr_output) { + wlr_output_enable(cont->sway_output->wlr_output, true); + } + } + return 0; +} + +static const struct wlr_idle_timeout_listener idle_listener = { + .idle = handle_idle, + .resumed = handle_resume, +}; + +static int handle_lock(void* data) { + wlr_log(L_DEBUG, "Lock screen"); + invoke_swaylock(); + return 0; +} + +static int handle_nop(void *data) { + //NOP + return 0; +} + + +static const struct wlr_idle_timeout_listener lock_listener = { + .idle = handle_lock, + .resumed = handle_nop, +}; + +bool idle_init(struct sway_server *server) { + wlr_log(L_DEBUG, "Initializing idle"); + server->idle = wlr_idle_create(server->wl_display); + wlr_log(L_DEBUG, "Setup idle timer %d", config->idle_timeout); + wlr_idle_listen(server->idle, config->idle_timeout * 1000, &idle_listener); + wlr_log(L_DEBUG, "Setup lock timer %d", config->lock_timeout); + wlr_idle_listen(server->idle, config->lock_timeout * 1000, &lock_listener); + return true; +} diff --git a/sway/main.c b/sway/main.c index efb674b6b..d258faae1 100644 --- a/sway/main.c +++ b/sway/main.c @@ -18,6 +18,7 @@ #include #include "sway/config.h" #include "sway/debug.h" +#include "sway/idle.h" #include "sway/server.h" #include "sway/tree/layout.h" #include "sway/ipc-server.h" @@ -413,6 +414,7 @@ int main(int argc, char **argv) { // TODO: wait for server to be ready // TODO: consume config->cmd_queue config->active = true; + idle_init(&server); if (!terminate_request) { server_run(&server); diff --git a/sway/meson.build b/sway/meson.build index 9e55e335f..e3bd80475 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -1,6 +1,7 @@ sway_sources = files( 'main.c', 'server.c', + 'idle.c', 'commands.c', 'config.c', 'criteria.c', @@ -54,6 +55,9 @@ sway_sources = files( 'commands/swaybg_command.c', 'commands/workspace.c', 'commands/ws_auto_back_and_forth.c', + 'commands/idle_timeout.c', + 'commands/lock_timeout.c', + 'commands/swaylock_command.c', 'commands/bar/activate_button.c', 'commands/bar/binding_mode_indicator.c', diff --git a/sway/server.c b/sway/server.c index 06ba8a17f..11cb95c08 100644 --- a/sway/server.c +++ b/sway/server.c @@ -16,7 +16,6 @@ #include #include #include -#include #include // TODO WLR: make Xwayland optional #include @@ -58,7 +57,6 @@ bool server_init(struct sway_server *server) { server->data_device_manager = wlr_data_device_manager_create(server->wl_display); - server->idle = wlr_idle_create(server->wl_display); wlr_screenshooter_create(server->wl_display); wlr_gamma_control_manager_create(server->wl_display); wlr_primary_selection_device_manager_create(server->wl_display);