From 6e2dccc2e96a9e7d5ff7da6ad097c0360ece2693 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Thu, 1 Nov 2018 23:55:41 +0000 Subject: [PATCH 1/4] swayidle: when acquiring sleep lock, initialize and free variables properly --- swayidle/main.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/swayidle/main.c b/swayidle/main.c index 2b185949..4e850c92 100644 --- a/swayidle/main.c +++ b/swayidle/main.c @@ -81,16 +81,16 @@ static int release_lock(void *data) { } static void acquire_sleep_lock(void) { - sd_bus_message *msg; - sd_bus_error error; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; int ret = sd_bus_call_method(bus, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "Inhibit", &error, &msg, "ssss", "sleep", "swayidle", "Setup Up Lock Screen", "delay"); if (ret < 0) { - wlr_log(WLR_ERROR, "Failed to send Inhibit signal: %s", - strerror(-ret)); + wlr_log(WLR_ERROR, "Failed to send Inhibit signal: %s", error.message); + sd_bus_error_free(&error); return; } @@ -98,10 +98,11 @@ static void acquire_sleep_lock(void) { if (ret < 0) { wlr_log(WLR_ERROR, "Failed to parse D-Bus response for Inhibit: %s", strerror(-ret)); - return; + } else { + wlr_log(WLR_INFO, "Got sleep lock: %d", lock_fd); } - - wlr_log(WLR_INFO, "Got sleep lock: %d", lock_fd); + sd_bus_error_free(&error); + sd_bus_message_unref(msg); } static int prepare_for_sleep(sd_bus_message *msg, void *userdata, From 6358de382793a5e28f445c87bb3baf138f4810af Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Thu, 1 Nov 2018 23:57:13 +0000 Subject: [PATCH 2/4] swaylock: fix display_event to not block --- swayidle/main.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/swayidle/main.c b/swayidle/main.c index 4e850c92..2e202ce1 100644 --- a/swayidle/main.c +++ b/swayidle/main.c @@ -366,15 +366,22 @@ static int handle_signal(int sig, void *data) { } static int display_event(int fd, uint32_t mask, void *data) { - if (mask & WL_EVENT_HANGUP) { + if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { sway_terminate(0); } - if (wl_display_dispatch(state.display) < 0) { + + int count = 0; + if (mask & WL_EVENT_READABLE) { + count = wl_display_dispatch(state.display); + } else { + count = wl_display_dispatch_pending(state.display); + wl_display_flush(state.display); + } + if (count < 0) { wlr_log_errno(WLR_ERROR, "wl_display_dispatch failed, exiting"); sway_terminate(0); } - wl_display_flush(state.display); - return 0; + return count; } static void register_idle_timeout(void *item) { From 41ffb5b4d5cb22b670a34ff441a7e069b7a4d175 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 24 Nov 2018 23:18:44 +0100 Subject: [PATCH 3/4] swayidle: listen when display becomes writable --- swayidle/main.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/swayidle/main.c b/swayidle/main.c index 2e202ce1..ab4b4596 100644 --- a/swayidle/main.c +++ b/swayidle/main.c @@ -373,7 +373,11 @@ static int display_event(int fd, uint32_t mask, void *data) { int count = 0; if (mask & WL_EVENT_READABLE) { count = wl_display_dispatch(state.display); - } else { + } + if (mask & WL_EVENT_WRITABLE) { + wl_display_flush(state.display); + } + if (mask == 0) { count = wl_display_dispatch_pending(state.display); wl_display_flush(state.display); } @@ -438,7 +442,7 @@ int main(int argc, char *argv[]) { wl_display_roundtrip(state.display); struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop, - wl_display_get_fd(state.display), WL_EVENT_READABLE, + wl_display_get_fd(state.display), WL_EVENT_READABLE | WL_EVENT_WRITABLE, display_event, NULL); wl_event_source_check(source); From 9b88ee01439a95b922ac3be96a93f16094b06566 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 24 Nov 2018 23:26:57 +0100 Subject: [PATCH 4/4] swayidle: fix dbus_event not to block --- swayidle/main.c | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/swayidle/main.c b/swayidle/main.c index ab4b4596..89ccf671 100644 --- a/swayidle/main.c +++ b/swayidle/main.c @@ -43,6 +43,12 @@ struct swayidle_timeout_cmd { char *resume_cmd; }; +void sway_terminate(int exit_code) { + wl_display_disconnect(state.display); + wl_event_loop_destroy(state.event_loop); + exit(exit_code); +} + static void cmd_exec(char *param) { wlr_log(WLR_DEBUG, "Cmd exec %s", param); pid_t pid = fork(); @@ -138,10 +144,28 @@ static int prepare_for_sleep(sd_bus_message *msg, void *userdata, static int dbus_event(int fd, uint32_t mask, void *data) { sd_bus *bus = data; - while (sd_bus_process(bus, NULL) > 0) { - // Do nothing. + + if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { + sway_terminate(0); } - return 1; + + int count = 0; + if (mask & WL_EVENT_READABLE) { + count = sd_bus_process(bus, NULL); + } + if (mask & WL_EVENT_WRITABLE) { + sd_bus_flush(bus); + } + if (mask == 0) { + sd_bus_flush(bus); + } + + if (count < 0) { + wlr_log_errno(WLR_ERROR, "sd_bus_process failed, exiting"); + sway_terminate(0); + } + + return count; } static void setup_sleep_listener(void) { @@ -167,8 +191,10 @@ static void setup_sleep_listener(void) { } acquire_sleep_lock(); - wl_event_loop_add_fd(state.event_loop, sd_bus_get_fd(bus), - WL_EVENT_READABLE, dbus_event, bus); + struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop, + sd_bus_get_fd(bus), WL_EVENT_READABLE | WL_EVENT_WRITABLE, + dbus_event, bus); + wl_event_source_check(source); } #endif @@ -340,12 +366,6 @@ static int parse_args(int argc, char *argv[]) { return 0; } -void sway_terminate(int exit_code) { - wl_display_disconnect(state.display); - wl_event_loop_destroy(state.event_loop); - exit(exit_code); -} - static void register_zero_idle_timeout(void *item) { struct swayidle_timeout_cmd *cmd = item; register_timeout(cmd, 0);