From d2f78118546a325247e7e5631852fce07ec0fcfe Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Fri, 20 Apr 2018 07:50:23 +0200 Subject: [PATCH 01/18] Add idle notifications --- include/sway/server.h | 1 + sway/input/cursor.c | 11 +++++++++++ sway/input/keyboard.c | 2 ++ sway/server.c | 2 ++ 4 files changed, 16 insertions(+) diff --git a/include/sway/server.h b/include/sway/server.h index 296fbf224..ac685bf83 100644 --- a/include/sway/server.h +++ b/include/sway/server.h @@ -21,6 +21,7 @@ struct sway_server { struct wlr_compositor *compositor; struct wlr_data_device_manager *data_device_manager; + struct wlr_idle *idle; struct sway_input_manager *input; diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 15a61cbf8..ac8acc64d 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -6,6 +6,7 @@ #endif #include #include +#include #include "list.h" #include "log.h" #include "sway/input/cursor.h" @@ -151,6 +152,7 @@ void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time) { static void handle_cursor_motion(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, motion); + wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); struct wlr_event_pointer_motion *event = data; wlr_cursor_move(cursor->cursor, event->device, event->delta_x, event->delta_y); @@ -161,6 +163,7 @@ static void handle_cursor_motion_absolute( struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, motion_absolute); + wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); struct wlr_event_pointer_motion_absolute *event = data; wlr_cursor_warp_absolute(cursor->cursor, event->device, event->x, event->y); cursor_send_pointer_motion(cursor, event->time_msec); @@ -206,6 +209,7 @@ void dispatch_cursor_button(struct sway_cursor *cursor, static void handle_cursor_button(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, button); + wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); struct wlr_event_pointer_button *event = data; dispatch_cursor_button(cursor, event->time_msec, event->button, event->state); @@ -213,6 +217,7 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { static void handle_cursor_axis(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, axis); + wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); struct wlr_event_pointer_axis *event = data; wlr_seat_pointer_notify_axis(cursor->seat->wlr_seat, event->time_msec, event->orientation, event->delta); @@ -220,12 +225,14 @@ static void handle_cursor_axis(struct wl_listener *listener, void *data) { static void handle_touch_down(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, touch_down); + wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); struct wlr_event_touch_down *event = data; wlr_log(L_DEBUG, "TODO: handle touch down event: %p", event); } static void handle_touch_up(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, touch_up); + wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); struct wlr_event_touch_up *event = data; wlr_log(L_DEBUG, "TODO: handle touch up event: %p", event); } @@ -233,12 +240,14 @@ static void handle_touch_up(struct wl_listener *listener, void *data) { static void handle_touch_motion(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, touch_motion); + wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); struct wlr_event_touch_motion *event = data; wlr_log(L_DEBUG, "TODO: handle touch motion event: %p", event); } static void handle_tool_axis(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, tool_axis); + wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); struct wlr_event_tablet_tool_axis *event = data; if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X) && @@ -257,6 +266,7 @@ static void handle_tool_axis(struct wl_listener *listener, void *data) { static void handle_tool_tip(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, tool_tip); + wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); struct wlr_event_tablet_tool_tip *event = data; dispatch_cursor_button(cursor, event->time_msec, BTN_LEFT, event->state == WLR_TABLET_TOOL_TIP_DOWN ? @@ -265,6 +275,7 @@ static void handle_tool_tip(struct wl_listener *listener, void *data) { static void handle_tool_button(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, tool_button); + wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); struct wlr_event_tablet_tool_button *event = data; // TODO: the user may want to configure which tool buttons are mapped to // which simulated pointer buttons diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index dbb0c3596..0d73cef0f 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "sway/input/seat.h" #include "sway/input/keyboard.h" #include "sway/input/input-manager.h" @@ -329,6 +330,7 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) { struct wlr_seat *wlr_seat = keyboard->seat_device->sway_seat->wlr_seat; struct wlr_input_device *wlr_device = keyboard->seat_device->input_device->wlr_device; + wlr_idle_notify_activity(keyboard->seat_device->sway_seat->input->server->idle, wlr_seat); struct wlr_event_keyboard_key *event = data; xkb_keycode_t keycode = event->keycode + 8; diff --git a/sway/server.c b/sway/server.c index 11cb95c08..06ba8a17f 100644 --- a/sway/server.c +++ b/sway/server.c @@ -16,6 +16,7 @@ #include #include #include +#include #include // TODO WLR: make Xwayland optional #include @@ -57,6 +58,7 @@ 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); From 3a327c48c8785bb144f3b47efab6a62a00d90bb2 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Fri, 20 Apr 2018 12:28:07 +0200 Subject: [PATCH 02/18] 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); From 73eb752eeeda683e1bb104f414bcbe3bff99ddd8 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Fri, 20 Apr 2018 15:02:15 +0200 Subject: [PATCH 03/18] Fix log level --- sway/idle.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sway/idle.c b/sway/idle.c index 1212225be..5f36361eb 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -19,7 +19,7 @@ void invoke_swaylock() { } static int handle_idle(void* data) { - wlr_log(L_ERROR, "Idle state"); + wlr_log(L_DEBUG, "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) { @@ -33,7 +33,7 @@ static int handle_idle(void* data) { } static int handle_resume(void *data) { - wlr_log(L_ERROR, "Active state"); + wlr_log(L_DEBUG, "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) { From dd1a2922a4c3e3a95ad96ca617fb6716a1a011af Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Fri, 20 Apr 2018 15:12:42 +0200 Subject: [PATCH 04/18] Merge broke idle config --- sway/commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway/commands.c b/sway/commands.c index fb4ffbc2a..752ae33c4 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -99,8 +99,8 @@ 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 }, { "fullscreen", cmd_fullscreen }, + { "idle_timeout", cmd_idle_timeout }, { "include", cmd_include }, { "input", cmd_input }, { "lock_timeout", cmd_lock_timeout }, From 581d24079cdee769ef7cf06a0f0633ae96b51cb3 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Mon, 23 Apr 2018 10:49:55 +0200 Subject: [PATCH 05/18] Make idle handling per seat --- include/sway/idle.h | 1 + sway/idle.c | 24 ++++++++++++++++++++---- sway/input/seat.c | 3 +++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/include/sway/idle.h b/include/sway/idle.h index 154e48f20..7b9228fc2 100644 --- a/include/sway/idle.h +++ b/include/sway/idle.h @@ -2,5 +2,6 @@ #define _SWAY_IDLE_H #include +void idle_setup_seat(struct sway_server *server, struct sway_seat *seat); bool idle_init(struct sway_server *server); #endif diff --git a/sway/idle.c b/sway/idle.c index 5f36361eb..b5fbf248e 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -68,12 +68,28 @@ static const struct wlr_idle_timeout_listener lock_listener = { .resumed = handle_nop, }; +void idle_setup_seat(struct sway_server *server, struct sway_seat *seat) { + if (server->idle == NULL) { + return; + } + if (config != NULL) { + wlr_log(L_DEBUG, "Setup idle timer %d", config->idle_timeout); + wlr_idle_listen(server->idle, config->idle_timeout * 1000, &idle_listener, seat->wlr_seat); + wlr_log(L_DEBUG, "Setup lock timer %d", config->lock_timeout); + wlr_idle_listen(server->idle, config->lock_timeout * 1000, &lock_listener, seat->wlr_seat); + } else { + wlr_log(L_ERROR, "Cant setup idle timers for seat since no config is available!"); + } +} + 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); + struct sway_seat *seat = NULL; + wl_list_for_each(seat, &input_manager->seats, link) { + idle_setup_seat(server, seat); + } + return true; } + diff --git a/sway/input/seat.c b/sway/input/seat.c index 8bba7d8fc..694ae0248 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -19,6 +19,7 @@ #include "sway/tree/container.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" +#include "sway/idle.h" #include "log.h" static void seat_device_destroy(struct sway_seat_device *seat_device) { @@ -257,6 +258,8 @@ struct sway_seat *seat_create(struct sway_input_manager *input, wl_list_insert(&input->seats, &seat->link); + idle_setup_seat(input->server, seat); + return seat; } From 146a22290eb02276af27b7011bd07771a3134ea2 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Tue, 24 Apr 2018 19:55:37 +0200 Subject: [PATCH 06/18] Add lock command for invoking swaylock_command Add lock command to be able to bind a key to invoke the lock command specified by swaylock_command --- include/sway/commands.h | 1 + include/sway/idle.h | 1 + sway/commands.c | 1 + sway/commands/lock.c | 11 +++++++++++ sway/meson.build | 1 + 5 files changed, 15 insertions(+) create mode 100644 sway/commands/lock.c diff --git a/include/sway/commands.h b/include/sway/commands.h index d975876e2..2c5a63d40 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -117,6 +117,7 @@ sway_cmd cmd_input; sway_cmd cmd_seat; sway_cmd cmd_ipc; sway_cmd cmd_kill; +sway_cmd cmd_lock; sway_cmd cmd_layout; sway_cmd cmd_log_colors; sway_cmd cmd_mark; diff --git a/include/sway/idle.h b/include/sway/idle.h index 7b9228fc2..e2a22e7e6 100644 --- a/include/sway/idle.h +++ b/include/sway/idle.h @@ -4,4 +4,5 @@ void idle_setup_seat(struct sway_server *server, struct sway_seat *seat); bool idle_init(struct sway_server *server); +void invoke_swaylock(); #endif diff --git a/sway/commands.c b/sway/commands.c index 40c6ec3da..ce696344f 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -167,6 +167,7 @@ static struct cmd_handler command_handlers[] = { { "focus", cmd_focus }, { "kill", cmd_kill }, { "layout", cmd_layout }, + { "lock", cmd_lock }, { "move", cmd_move }, { "opacity", cmd_opacity }, { "reload", cmd_reload }, diff --git a/sway/commands/lock.c b/sway/commands/lock.c new file mode 100644 index 000000000..b4495ad65 --- /dev/null +++ b/sway/commands/lock.c @@ -0,0 +1,11 @@ +#include +#include "log.h" +#include "sway/commands.h" +#include "sway/idle.h" + +struct cmd_results *cmd_lock(int argc, char **argv) { + + invoke_swaylock(); + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/meson.build b/sway/meson.build index 9c23332a6..f85bc43a3 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -37,6 +37,7 @@ sway_sources = files( 'commands/focus_follows_mouse.c', 'commands/fullscreen.c', 'commands/kill.c', + 'commands/lock.c', 'commands/opacity.c', 'commands/include.c', 'commands/input.c', From ff5f58b0d819aca5b63efc5363a888eb396b4263 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Tue, 24 Apr 2018 21:27:00 +0200 Subject: [PATCH 07/18] Support for PrepareForSleep logind signal --- sway/idle.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/sway/idle.c b/sway/idle.c index b5fbf248e..44dd70cd8 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -7,6 +7,14 @@ #include #include #include +#include +#ifdef WLR_HAS_SYSTEMD + #include + #include +#elif defined(WLR_HAS_ELOGIND) + #include + #include +#endif void invoke_swaylock() { int pid = fork(); @@ -18,6 +26,37 @@ void invoke_swaylock() { wlr_log(L_DEBUG, "Spawned swaylock %d", pid); } +#if defined(WLR_HAS_SYSTEMD) || defined(WLR_HAS_ELOGIND) +static int prepare_for_sleep(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { + wlr_log(L_INFO, "PrepareForSleep signal received"); + invoke_swaylock(); + return 0; +} + +void setup_sleep_listener(struct sway_server *server) { + struct sd_bus *bus; + int ret = sd_bus_default_system(&bus); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to open D-Bus connection: %s", strerror(-ret)); + return; + } + + char str[256]; + const char *fmt = "type='signal'," + "sender='org.freedesktop.login1'," + "interface='org.freedesktop.login1.%s'," + "member='%s'," + "path='%s'"; + + snprintf(str, sizeof(str), fmt, "Manager", "PrepareForSleep", "/org/freedesktop/login1"); + ret = sd_bus_add_match(bus, NULL, str, prepare_for_sleep, NULL); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret)); + return; + } +} +#endif + static int handle_idle(void* data) { wlr_log(L_DEBUG, "Idle state"); for (int i = 0; i < root_container.children->length; ++i) { @@ -89,6 +128,9 @@ bool idle_init(struct sway_server *server) { wl_list_for_each(seat, &input_manager->seats, link) { idle_setup_seat(server, seat); } +#if defined(WLR_HAS_SYSTEMD) || defined(WLR_HAS_ELOGIND) + setup_sleep_listener(server); +#endif return true; } From 913dd235ee812eeff0bfd5be5f4a305b9c48b6df Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Wed, 25 Apr 2018 21:10:38 +0200 Subject: [PATCH 08/18] Inhibit suspend until we have a lockscreen --- sway/idle.c | 118 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 104 insertions(+), 14 deletions(-) diff --git a/sway/idle.c b/sway/idle.c index 44dd70cd8..b6dd62ef6 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -6,14 +6,15 @@ #include #include #include +#include #include #include #ifdef WLR_HAS_SYSTEMD - #include - #include +#include +#include #elif defined(WLR_HAS_ELOGIND) - #include - #include +#include +#include #endif void invoke_swaylock() { @@ -27,9 +28,91 @@ void invoke_swaylock() { } #if defined(WLR_HAS_SYSTEMD) || defined(WLR_HAS_ELOGIND) + +bool have_lock() { + if (root_container.children == NULL) + return false; + if (root_container.children->items == NULL) + return false; + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *output_container = + root_container.children->items[i]; + if (output_container == NULL) + return false; + struct sway_output *output = + output_container->sway_output; + if (output == NULL) + return false; + if (output->layers == NULL) + return false; + struct wl_list *layers = &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]; + if (layers == NULL) + return false; + struct sway_layer_surface *sway_layer; + wl_list_for_each_reverse(sway_layer, layers, link) { + struct wlr_layer_surface *surface = sway_layer->layer_surface; + if (!strcmp("lockscreen", surface->namespace)) + wlr_log(L_DEBUG, "Lockscreen found!"); + return true; + } + } + + wlr_log(L_DEBUG, "No lock"); + return false; +} + +static int fd = 0; +static int inhibit_cnt=0; + +static int cleanup_inhibit(void *data) { + fd = 0; + inhibit_cnt=0; + wlr_log(L_DEBUG, "Cleanup inhibit"); + return 0; +} + static int prepare_for_sleep(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { + struct sway_server *server = userdata; + wlr_log(L_INFO, "PrepareForSleep signal received"); - invoke_swaylock(); + if(inhibit_cnt > 4 || have_lock()) { + wlr_log(L_INFO, "Already have lock, no inhibit"); + cleanup_inhibit(NULL); + return 0; + } + + wlr_log(L_INFO, "No lock, will inhibit"); + + struct sd_bus *bus; + int ret = sd_bus_default_system(&bus); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to open D-Bus connection: %s", strerror(-ret)); + return 0; + } + + ret = sd_bus_call_method(bus, "org.freedesktop.login1", + "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "Inhibit", + ret_error, &msg, "ssss", "sleep", "sway-idle", "Setup Up Lock Screen", "delay"); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to send Inhibit signal: %s", + strerror(-ret)); + } else { + ret = sd_bus_message_read(msg, "h", &fd); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to parse D-Bus response for Inhibit: %s", strerror(-ret)); + } + } + + if (!inhibit_cnt) { + invoke_swaylock(); + + // 3 seconds should be well enough for 5 inhibits to be over and done + struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, cleanup_inhibit, NULL); + wl_event_source_timer_update(source, 3000); + } + inhibit_cnt++; + + wlr_log(L_ERROR, "Inhibit done %d", inhibit_cnt); return 0; } @@ -49,7 +132,7 @@ void setup_sleep_listener(struct sway_server *server) { "path='%s'"; snprintf(str, sizeof(str), fmt, "Manager", "PrepareForSleep", "/org/freedesktop/login1"); - ret = sd_bus_add_match(bus, NULL, str, prepare_for_sleep, NULL); + ret = sd_bus_add_match(bus, NULL, str, prepare_for_sleep, server); if (ret < 0) { wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret)); return; @@ -112,10 +195,21 @@ void idle_setup_seat(struct sway_server *server, struct sway_seat *seat) { return; } if (config != NULL) { - wlr_log(L_DEBUG, "Setup idle timer %d", config->idle_timeout); - wlr_idle_listen(server->idle, config->idle_timeout * 1000, &idle_listener, seat->wlr_seat); - wlr_log(L_DEBUG, "Setup lock timer %d", config->lock_timeout); - wlr_idle_listen(server->idle, config->lock_timeout * 1000, &lock_listener, seat->wlr_seat); + if (config->idle_timeout > 0) { + wlr_log(L_DEBUG, "Setup idle timer %d", config->idle_timeout); + wlr_idle_listen(server->idle, config->idle_timeout * 1000, &idle_listener, seat->wlr_seat); + } else { + wlr_log(L_INFO, "Idle timeout set to 0, will disable screen power management"); + } + if (config->lock_timeout > 0) { + wlr_log(L_DEBUG, "Setup lock timer %d", config->lock_timeout); + wlr_idle_listen(server->idle, config->lock_timeout * 1000, &lock_listener, seat->wlr_seat); +#if defined(WLR_HAS_SYSTEMD) || defined(WLR_HAS_ELOGIND) + setup_sleep_listener(server); +#endif + } else { + wlr_log(L_INFO, "Lock timeout set to 0, will disable auto lock"); + } } else { wlr_log(L_ERROR, "Cant setup idle timers for seat since no config is available!"); } @@ -128,10 +222,6 @@ bool idle_init(struct sway_server *server) { wl_list_for_each(seat, &input_manager->seats, link) { idle_setup_seat(server, seat); } -#if defined(WLR_HAS_SYSTEMD) || defined(WLR_HAS_ELOGIND) - setup_sleep_listener(server); -#endif - return true; } From 1d115ddca9db070187cc17c53253c6ed30e8602a Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Wed, 25 Apr 2018 21:25:29 +0200 Subject: [PATCH 09/18] Extra NULL checks --- sway/idle.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sway/idle.c b/sway/idle.c index b6dd62ef6..dddce1da2 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -51,9 +51,11 @@ bool have_lock() { struct sway_layer_surface *sway_layer; wl_list_for_each_reverse(sway_layer, layers, link) { struct wlr_layer_surface *surface = sway_layer->layer_surface; - if (!strcmp("lockscreen", surface->namespace)) - wlr_log(L_DEBUG, "Lockscreen found!"); - return true; + if (surface != NULL && surface->namespace != NULL) { + if (!strcmp("lockscreen", surface->namespace)) + wlr_log(L_DEBUG, "Lockscreen found!"); + return true; + } } } From 6d5c937247666d7ac9335caff7752aeee2111ead Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Thu, 26 Apr 2018 22:26:03 +0200 Subject: [PATCH 10/18] Move systemd related things to wlroots --- sway/idle.c | 81 ++++++++++++++++------------------------------------- 1 file changed, 24 insertions(+), 57 deletions(-) diff --git a/sway/idle.c b/sway/idle.c index dddce1da2..82e545185 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -9,13 +9,9 @@ #include #include #include -#ifdef WLR_HAS_SYSTEMD -#include -#include -#elif defined(WLR_HAS_ELOGIND) -#include -#include -#endif +#include +#include + void invoke_swaylock() { int pid = fork(); @@ -27,8 +23,6 @@ void invoke_swaylock() { wlr_log(L_DEBUG, "Spawned swaylock %d", pid); } -#if defined(WLR_HAS_SYSTEMD) || defined(WLR_HAS_ELOGIND) - bool have_lock() { if (root_container.children == NULL) return false; @@ -73,38 +67,23 @@ static int cleanup_inhibit(void *data) { return 0; } -static int prepare_for_sleep(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { - struct sway_server *server = userdata; - +static void prepare_for_sleep(struct wlr_session *session, void *data) { + struct sway_server *server = data; wlr_log(L_INFO, "PrepareForSleep signal received"); - if(inhibit_cnt > 4 || have_lock()) { - wlr_log(L_INFO, "Already have lock, no inhibit"); + if(have_lock()) { + wlr_log(L_INFO, "Have lock, no inhibit"); cleanup_inhibit(NULL); - return 0; + return; + } + if(inhibit_cnt > 4) { + wlr_log(L_INFO, "Reached inhibit retry limit, no inhibit"); + cleanup_inhibit(NULL); + return; } wlr_log(L_INFO, "No lock, will inhibit"); - struct sd_bus *bus; - int ret = sd_bus_default_system(&bus); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to open D-Bus connection: %s", strerror(-ret)); - return 0; - } - - ret = sd_bus_call_method(bus, "org.freedesktop.login1", - "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "Inhibit", - ret_error, &msg, "ssss", "sleep", "sway-idle", "Setup Up Lock Screen", "delay"); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to send Inhibit signal: %s", - strerror(-ret)); - } else { - ret = sd_bus_message_read(msg, "h", &fd); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to parse D-Bus response for Inhibit: %s", strerror(-ret)); - } - } - + wlr_session_inhibit_sleep(session); if (!inhibit_cnt) { invoke_swaylock(); @@ -115,32 +94,22 @@ static int prepare_for_sleep(sd_bus_message *msg, void *userdata, sd_bus_error * inhibit_cnt++; wlr_log(L_ERROR, "Inhibit done %d", inhibit_cnt); - return 0; + return; } void setup_sleep_listener(struct sway_server *server) { - struct sd_bus *bus; - int ret = sd_bus_default_system(&bus); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to open D-Bus connection: %s", strerror(-ret)); - return; + + + struct wlr_session *session = NULL; + if (wlr_backend_is_multi(server->backend)) { + session = wlr_multi_get_session(server->backend); + } + if (!session) { + wlr_log(L_INFO, "No supported session found, skipping sleep litener setup"); } - char str[256]; - const char *fmt = "type='signal'," - "sender='org.freedesktop.login1'," - "interface='org.freedesktop.login1.%s'," - "member='%s'," - "path='%s'"; - - snprintf(str, sizeof(str), fmt, "Manager", "PrepareForSleep", "/org/freedesktop/login1"); - ret = sd_bus_add_match(bus, NULL, str, prepare_for_sleep, server); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret)); - return; - } + wlr_session_prepare_for_sleep_listen(session, prepare_for_sleep, server); } -#endif static int handle_idle(void* data) { wlr_log(L_DEBUG, "Idle state"); @@ -206,9 +175,7 @@ void idle_setup_seat(struct sway_server *server, struct sway_seat *seat) { if (config->lock_timeout > 0) { wlr_log(L_DEBUG, "Setup lock timer %d", config->lock_timeout); wlr_idle_listen(server->idle, config->lock_timeout * 1000, &lock_listener, seat->wlr_seat); -#if defined(WLR_HAS_SYSTEMD) || defined(WLR_HAS_ELOGIND) setup_sleep_listener(server); -#endif } else { wlr_log(L_INFO, "Lock timeout set to 0, will disable auto lock"); } From cfee3932340d8c86213a3c7679315fad17ffdbf7 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Fri, 27 Apr 2018 09:09:19 +0200 Subject: [PATCH 11/18] Release lock --- sway/idle.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sway/idle.c b/sway/idle.c index 82e545185..44a80c7a1 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -57,11 +57,11 @@ bool have_lock() { return false; } -static int fd = 0; +static int fd = -1; static int inhibit_cnt=0; static int cleanup_inhibit(void *data) { - fd = 0; + fd = -1; inhibit_cnt=0; wlr_log(L_DEBUG, "Cleanup inhibit"); return 0; @@ -72,6 +72,8 @@ static void prepare_for_sleep(struct wlr_session *session, void *data) { wlr_log(L_INFO, "PrepareForSleep signal received"); if(have_lock()) { wlr_log(L_INFO, "Have lock, no inhibit"); + if (fd >= 0) + close(fd); //Release lock cleanup_inhibit(NULL); return; } @@ -83,7 +85,7 @@ static void prepare_for_sleep(struct wlr_session *session, void *data) { wlr_log(L_INFO, "No lock, will inhibit"); - wlr_session_inhibit_sleep(session); + fd = wlr_session_inhibit_sleep(session); if (!inhibit_cnt) { invoke_swaylock(); From 8f81be64fb16db0a6fd9598c68b2acd3282b8f42 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Fri, 27 Apr 2018 09:11:34 +0200 Subject: [PATCH 12/18] Some debug --- sway/idle.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sway/idle.c b/sway/idle.c index 44a80c7a1..67b8a388f 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -86,6 +86,7 @@ static void prepare_for_sleep(struct wlr_session *session, void *data) { wlr_log(L_INFO, "No lock, will inhibit"); fd = wlr_session_inhibit_sleep(session); + wlr_log(L_DEBUG, "Inhibit lock fd %d", fd); if (!inhibit_cnt) { invoke_swaylock(); @@ -95,7 +96,7 @@ static void prepare_for_sleep(struct wlr_session *session, void *data) { } inhibit_cnt++; - wlr_log(L_ERROR, "Inhibit done %d", inhibit_cnt); + wlr_log(L_DEBUG, "Inhibit done %d", inhibit_cnt); return; } From 1e1899512e25e12a26d6a4e1e919fcad8ff5c390 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Fri, 27 Apr 2018 12:08:29 +0200 Subject: [PATCH 13/18] Fix inhibit logic to use block and only inhibit when goind down --- sway/idle.c | 53 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/sway/idle.c b/sway/idle.c index 67b8a388f..5fd79045a 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -60,43 +60,52 @@ bool have_lock() { static int fd = -1; static int inhibit_cnt=0; -static int cleanup_inhibit(void *data) { +void cleanup_inhibit() { + if (fd >= 0) + close(fd); //Release lock fd = -1; inhibit_cnt=0; wlr_log(L_DEBUG, "Cleanup inhibit"); return 0; } +static int check_for_lock(void *data) { + struct sway_server *server = data; + if(have_lock()) { //If we for some reason already have a lockscreen + wlr_log(L_INFO, "Got lock, will release inhibit lock"); + cleanup_inhibit(); + return 0; + } + + if(inhibit_cnt > 4) { + wlr_log(L_INFO, "Reached inhibit timeout, releasing lock"); + cleanup_inhibit(); + return 0; + } + + inhibit_cnt++; + struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lock, server); + wl_event_source_timer_update(source, 100); + return 0; +} + static void prepare_for_sleep(struct wlr_session *session, void *data) { struct sway_server *server = data; wlr_log(L_INFO, "PrepareForSleep signal received"); - if(have_lock()) { - wlr_log(L_INFO, "Have lock, no inhibit"); - if (fd >= 0) - close(fd); //Release lock - cleanup_inhibit(NULL); - return; - } - if(inhibit_cnt > 4) { - wlr_log(L_INFO, "Reached inhibit retry limit, no inhibit"); - cleanup_inhibit(NULL); + if(have_lock()) { //If we for some reason already have a lockscreen + wlr_log(L_INFO, "Already have lock, no inhibit"); + cleanup_inhibit(); return; } - wlr_log(L_INFO, "No lock, will inhibit"); + wlr_log(L_DEBUG, "Trying to inhibit sleep"); fd = wlr_session_inhibit_sleep(session); - wlr_log(L_DEBUG, "Inhibit lock fd %d", fd); - if (!inhibit_cnt) { - invoke_swaylock(); - - // 3 seconds should be well enough for 5 inhibits to be over and done - struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, cleanup_inhibit, NULL); - wl_event_source_timer_update(source, 3000); + invoke_swaylock(); + if (fd>=0) { + struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lock, server); + wl_event_source_timer_update(source, 100); } - inhibit_cnt++; - - wlr_log(L_DEBUG, "Inhibit done %d", inhibit_cnt); return; } From 3e2af9c4a2f789aaea16d7b329af9a992324468a Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Fri, 27 Apr 2018 12:10:32 +0200 Subject: [PATCH 14/18] Fix return statment --- sway/idle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway/idle.c b/sway/idle.c index 5fd79045a..b11349f74 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -66,7 +66,7 @@ void cleanup_inhibit() { fd = -1; inhibit_cnt=0; wlr_log(L_DEBUG, "Cleanup inhibit"); - return 0; + return; } static int check_for_lock(void *data) { From 72054eba22ee9d2b07cae4b60b5df5b099915b3a Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Fri, 27 Apr 2018 12:29:27 +0200 Subject: [PATCH 15/18] Be more agressive grabbing inhibit lock --- sway/idle.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sway/idle.c b/sway/idle.c index b11349f74..78f0c7588 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -92,15 +92,13 @@ static int check_for_lock(void *data) { static void prepare_for_sleep(struct wlr_session *session, void *data) { struct sway_server *server = data; wlr_log(L_INFO, "PrepareForSleep signal received"); + fd = wlr_session_inhibit_sleep(session); if(have_lock()) { //If we for some reason already have a lockscreen wlr_log(L_INFO, "Already have lock, no inhibit"); cleanup_inhibit(); return; } - - wlr_log(L_DEBUG, "Trying to inhibit sleep"); - fd = wlr_session_inhibit_sleep(session); invoke_swaylock(); if (fd>=0) { struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lock, server); From 20c113952ae30d5250935e5104eea0a42a167e98 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Sat, 28 Apr 2018 09:38:11 +0200 Subject: [PATCH 16/18] Fix lock sequence to actually work --- sway/idle.c | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/sway/idle.c b/sway/idle.c index 78f0c7588..1e9ef48b7 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -57,51 +57,58 @@ bool have_lock() { return false; } -static int fd = -1; +static int lock_fd = -1; +static int ongoing_fd = -1; static int inhibit_cnt=0; -void cleanup_inhibit() { - if (fd >= 0) - close(fd); //Release lock - fd = -1; +void release_lock() { + wlr_log(L_DEBUG, "Release lock %d", ongoing_fd); + if (ongoing_fd >= 0) + close(ongoing_fd); + ongoing_fd = -1; inhibit_cnt=0; - wlr_log(L_DEBUG, "Cleanup inhibit"); return; } -static int check_for_lock(void *data) { +static int check_for_lockscreen(void *data) { struct sway_server *server = data; if(have_lock()) { //If we for some reason already have a lockscreen wlr_log(L_INFO, "Got lock, will release inhibit lock"); - cleanup_inhibit(); + release_lock(); return 0; } if(inhibit_cnt > 4) { wlr_log(L_INFO, "Reached inhibit timeout, releasing lock"); - cleanup_inhibit(); + release_lock(); return 0; } inhibit_cnt++; - struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lock, server); - wl_event_source_timer_update(source, 100); + struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lockscreen, server); + wl_event_source_timer_update(source, 100); return 0; } -static void prepare_for_sleep(struct wlr_session *session, void *data) { +static void prepare_for_sleep(struct wlr_session *session, bool going_down, void *data) { struct sway_server *server = data; - wlr_log(L_INFO, "PrepareForSleep signal received"); - fd = wlr_session_inhibit_sleep(session); - if(have_lock()) { //If we for some reason already have a lockscreen - wlr_log(L_INFO, "Already have lock, no inhibit"); - cleanup_inhibit(); + wlr_log(L_INFO, "PrepareForSleep signal received %d", going_down); + if (!going_down) { + lock_fd = wlr_session_aquire_sleep_lock(session); + wlr_log(L_INFO, "Got new lock %d", lock_fd); return; } + ongoing_fd = lock_fd; + if(have_lock()) { //If we for some reason already have a lockscreen + wlr_log(L_INFO, "Already have lock, no action"); + release_lock(); + return; + } + wlr_log(L_INFO, "Starting lockscreen, holding lock %d", ongoing_fd); invoke_swaylock(); - if (fd>=0) { - struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lock, server); + if (ongoing_fd>=0) { + struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lockscreen, server); wl_event_source_timer_update(source, 100); } return; @@ -119,6 +126,8 @@ void setup_sleep_listener(struct sway_server *server) { } wlr_session_prepare_for_sleep_listen(session, prepare_for_sleep, server); + lock_fd = wlr_session_aquire_sleep_lock(session); + wlr_log(L_INFO, "Got initial lock %d", lock_fd); } static int handle_idle(void* data) { From 944f54da0d3ed9c8032f583c7342363f7a8670bc Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Sat, 28 Apr 2018 09:43:05 +0200 Subject: [PATCH 17/18] Adjusted debug output --- sway/idle.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/sway/idle.c b/sway/idle.c index 1e9ef48b7..2674e21de 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -53,7 +53,7 @@ bool have_lock() { } } - wlr_log(L_DEBUG, "No lock"); + wlr_log(L_DEBUG, "No lockscreen found"); return false; } @@ -62,7 +62,7 @@ static int ongoing_fd = -1; static int inhibit_cnt=0; void release_lock() { - wlr_log(L_DEBUG, "Release lock %d", ongoing_fd); + wlr_log(L_INFO, "Release lock %d", ongoing_fd); if (ongoing_fd >= 0) close(ongoing_fd); ongoing_fd = -1; @@ -73,13 +73,13 @@ void release_lock() { static int check_for_lockscreen(void *data) { struct sway_server *server = data; if(have_lock()) { //If we for some reason already have a lockscreen - wlr_log(L_INFO, "Got lock, will release inhibit lock"); + wlr_log(L_DEBUG, "Got lock, will release inhibit lock"); release_lock(); return 0; } if(inhibit_cnt > 4) { - wlr_log(L_INFO, "Reached inhibit timeout, releasing lock"); + wlr_log(L_DEBUG, "Reached inhibit timeout, releasing lock"); release_lock(); return 0; } @@ -92,20 +92,19 @@ static int check_for_lockscreen(void *data) { static void prepare_for_sleep(struct wlr_session *session, bool going_down, void *data) { struct sway_server *server = data; - wlr_log(L_INFO, "PrepareForSleep signal received %d", going_down); + wlr_log(L_DEBUG, "PrepareForSleep signal received %d", going_down); if (!going_down) { lock_fd = wlr_session_aquire_sleep_lock(session); - wlr_log(L_INFO, "Got new lock %d", lock_fd); + wlr_log(L_DEBUG, "Got new lock %d", lock_fd); return; } ongoing_fd = lock_fd; if(have_lock()) { //If we for some reason already have a lockscreen - wlr_log(L_INFO, "Already have lock, no action"); + wlr_log(L_DEBUG, "Already have lock, no action"); release_lock(); return; } wlr_log(L_INFO, "Starting lockscreen, holding lock %d", ongoing_fd); - invoke_swaylock(); if (ongoing_fd>=0) { struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lockscreen, server); @@ -115,8 +114,6 @@ static void prepare_for_sleep(struct wlr_session *session, bool going_down, void } void setup_sleep_listener(struct sway_server *server) { - - struct wlr_session *session = NULL; if (wlr_backend_is_multi(server->backend)) { session = wlr_multi_get_session(server->backend); @@ -127,7 +124,7 @@ void setup_sleep_listener(struct sway_server *server) { wlr_session_prepare_for_sleep_listen(session, prepare_for_sleep, server); lock_fd = wlr_session_aquire_sleep_lock(session); - wlr_log(L_INFO, "Got initial lock %d", lock_fd); + wlr_log(L_DEBUG, "Got initial lock %d", lock_fd); } static int handle_idle(void* data) { From 9ae03f19bd43c78a5707c1bbf5e06d3a5d8af5b8 Mon Sep 17 00:00:00 2001 From: Mattias Eriksson Date: Sat, 28 Apr 2018 16:19:47 +0200 Subject: [PATCH 18/18] Adapt to wlroots interface change --- sway/idle.c | 61 ++++------------------------------------------------- 1 file changed, 4 insertions(+), 57 deletions(-) diff --git a/sway/idle.c b/sway/idle.c index 2674e21de..d999a8c76 100644 --- a/sway/idle.c +++ b/sway/idle.c @@ -23,7 +23,7 @@ void invoke_swaylock() { wlr_log(L_DEBUG, "Spawned swaylock %d", pid); } -bool have_lock() { +bool have_lock(void *data) { if (root_container.children == NULL) return false; if (root_container.children->items == NULL) @@ -57,60 +57,9 @@ bool have_lock() { return false; } -static int lock_fd = -1; -static int ongoing_fd = -1; -static int inhibit_cnt=0; - -void release_lock() { - wlr_log(L_INFO, "Release lock %d", ongoing_fd); - if (ongoing_fd >= 0) - close(ongoing_fd); - ongoing_fd = -1; - inhibit_cnt=0; - return; -} - -static int check_for_lockscreen(void *data) { - struct sway_server *server = data; - if(have_lock()) { //If we for some reason already have a lockscreen - wlr_log(L_DEBUG, "Got lock, will release inhibit lock"); - release_lock(); - return 0; - } - - if(inhibit_cnt > 4) { - wlr_log(L_DEBUG, "Reached inhibit timeout, releasing lock"); - release_lock(); - return 0; - } - - inhibit_cnt++; - struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lockscreen, server); - wl_event_source_timer_update(source, 100); - return 0; -} - -static void prepare_for_sleep(struct wlr_session *session, bool going_down, void *data) { - struct sway_server *server = data; - wlr_log(L_DEBUG, "PrepareForSleep signal received %d", going_down); - if (!going_down) { - lock_fd = wlr_session_aquire_sleep_lock(session); - wlr_log(L_DEBUG, "Got new lock %d", lock_fd); - return; - } - ongoing_fd = lock_fd; - if(have_lock()) { //If we for some reason already have a lockscreen - wlr_log(L_DEBUG, "Already have lock, no action"); - release_lock(); - return; - } - wlr_log(L_INFO, "Starting lockscreen, holding lock %d", ongoing_fd); +static void prepare_for_sleep(struct wlr_session *session, void *data) { + wlr_log(L_DEBUG, "PrepareForSleep signal received"); invoke_swaylock(); - if (ongoing_fd>=0) { - struct wl_event_source *source = wl_event_loop_add_timer(server->wl_event_loop, check_for_lockscreen, server); - wl_event_source_timer_update(source, 100); - } - return; } void setup_sleep_listener(struct sway_server *server) { @@ -122,9 +71,7 @@ void setup_sleep_listener(struct sway_server *server) { wlr_log(L_INFO, "No supported session found, skipping sleep litener setup"); } - wlr_session_prepare_for_sleep_listen(session, prepare_for_sleep, server); - lock_fd = wlr_session_aquire_sleep_lock(session); - wlr_log(L_DEBUG, "Got initial lock %d", lock_fd); + wlr_session_prepare_for_sleep_listen(session, prepare_for_sleep, have_lock, server); } static int handle_idle(void* data) {