diff options
author | 2018-11-25 13:47:21 +0100 | |
---|---|---|
committer | 2018-11-25 13:47:21 +0100 | |
commit | d46d221118f7b4ffeb0fe0fcba296fbbd78a0cbd (patch) | |
tree | 85cc4f46cf1107f30b7444efb46bf97093a21988 /swayidle/main.c | |
parent | Merge pull request #3179 from baloo/baloo/title_format-pango_markup (diff) | |
parent | swayidle: fix dbus_event not to block (diff) | |
download | sway-d46d221118f7b4ffeb0fe0fcba296fbbd78a0cbd.tar.gz sway-d46d221118f7b4ffeb0fe0fcba296fbbd78a0cbd.tar.zst sway-d46d221118f7b4ffeb0fe0fcba296fbbd78a0cbd.zip |
Merge pull request #3049 from ianyfan/swayidle
Swayidle fixes
Diffstat (limited to 'swayidle/main.c')
-rw-r--r-- | swayidle/main.c | 78 |
1 files changed, 55 insertions, 23 deletions
diff --git a/swayidle/main.c b/swayidle/main.c index 2b185949..89ccf671 100644 --- a/swayidle/main.c +++ b/swayidle/main.c | |||
@@ -43,6 +43,12 @@ struct swayidle_timeout_cmd { | |||
43 | char *resume_cmd; | 43 | char *resume_cmd; |
44 | }; | 44 | }; |
45 | 45 | ||
46 | void sway_terminate(int exit_code) { | ||
47 | wl_display_disconnect(state.display); | ||
48 | wl_event_loop_destroy(state.event_loop); | ||
49 | exit(exit_code); | ||
50 | } | ||
51 | |||
46 | static void cmd_exec(char *param) { | 52 | static void cmd_exec(char *param) { |
47 | wlr_log(WLR_DEBUG, "Cmd exec %s", param); | 53 | wlr_log(WLR_DEBUG, "Cmd exec %s", param); |
48 | pid_t pid = fork(); | 54 | pid_t pid = fork(); |
@@ -81,16 +87,16 @@ static int release_lock(void *data) { | |||
81 | } | 87 | } |
82 | 88 | ||
83 | static void acquire_sleep_lock(void) { | 89 | static void acquire_sleep_lock(void) { |
84 | sd_bus_message *msg; | 90 | sd_bus_message *msg = NULL; |
85 | sd_bus_error error; | 91 | sd_bus_error error = SD_BUS_ERROR_NULL; |
86 | int ret = sd_bus_call_method(bus, "org.freedesktop.login1", | 92 | int ret = sd_bus_call_method(bus, "org.freedesktop.login1", |
87 | "/org/freedesktop/login1", | 93 | "/org/freedesktop/login1", |
88 | "org.freedesktop.login1.Manager", "Inhibit", | 94 | "org.freedesktop.login1.Manager", "Inhibit", |
89 | &error, &msg, "ssss", "sleep", "swayidle", | 95 | &error, &msg, "ssss", "sleep", "swayidle", |
90 | "Setup Up Lock Screen", "delay"); | 96 | "Setup Up Lock Screen", "delay"); |
91 | if (ret < 0) { | 97 | if (ret < 0) { |
92 | wlr_log(WLR_ERROR, "Failed to send Inhibit signal: %s", | 98 | wlr_log(WLR_ERROR, "Failed to send Inhibit signal: %s", error.message); |
93 | strerror(-ret)); | 99 | sd_bus_error_free(&error); |
94 | return; | 100 | return; |
95 | } | 101 | } |
96 | 102 | ||
@@ -98,10 +104,11 @@ static void acquire_sleep_lock(void) { | |||
98 | if (ret < 0) { | 104 | if (ret < 0) { |
99 | wlr_log(WLR_ERROR, "Failed to parse D-Bus response for Inhibit: %s", | 105 | wlr_log(WLR_ERROR, "Failed to parse D-Bus response for Inhibit: %s", |
100 | strerror(-ret)); | 106 | strerror(-ret)); |
101 | return; | 107 | } else { |
108 | wlr_log(WLR_INFO, "Got sleep lock: %d", lock_fd); | ||
102 | } | 109 | } |
103 | 110 | sd_bus_error_free(&error); | |
104 | wlr_log(WLR_INFO, "Got sleep lock: %d", lock_fd); | 111 | sd_bus_message_unref(msg); |
105 | } | 112 | } |
106 | 113 | ||
107 | static int prepare_for_sleep(sd_bus_message *msg, void *userdata, | 114 | static int prepare_for_sleep(sd_bus_message *msg, void *userdata, |
@@ -137,10 +144,28 @@ static int prepare_for_sleep(sd_bus_message *msg, void *userdata, | |||
137 | 144 | ||
138 | static int dbus_event(int fd, uint32_t mask, void *data) { | 145 | static int dbus_event(int fd, uint32_t mask, void *data) { |
139 | sd_bus *bus = data; | 146 | sd_bus *bus = data; |
140 | while (sd_bus_process(bus, NULL) > 0) { | 147 | |
141 | // Do nothing. | 148 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { |
149 | sway_terminate(0); | ||
150 | } | ||
151 | |||
152 | int count = 0; | ||
153 | if (mask & WL_EVENT_READABLE) { | ||
154 | count = sd_bus_process(bus, NULL); | ||
155 | } | ||
156 | if (mask & WL_EVENT_WRITABLE) { | ||
157 | sd_bus_flush(bus); | ||
142 | } | 158 | } |
143 | return 1; | 159 | if (mask == 0) { |
160 | sd_bus_flush(bus); | ||
161 | } | ||
162 | |||
163 | if (count < 0) { | ||
164 | wlr_log_errno(WLR_ERROR, "sd_bus_process failed, exiting"); | ||
165 | sway_terminate(0); | ||
166 | } | ||
167 | |||
168 | return count; | ||
144 | } | 169 | } |
145 | 170 | ||
146 | static void setup_sleep_listener(void) { | 171 | static void setup_sleep_listener(void) { |
@@ -166,8 +191,10 @@ static void setup_sleep_listener(void) { | |||
166 | } | 191 | } |
167 | acquire_sleep_lock(); | 192 | acquire_sleep_lock(); |
168 | 193 | ||
169 | wl_event_loop_add_fd(state.event_loop, sd_bus_get_fd(bus), | 194 | struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop, |
170 | WL_EVENT_READABLE, dbus_event, bus); | 195 | sd_bus_get_fd(bus), WL_EVENT_READABLE | WL_EVENT_WRITABLE, |
196 | dbus_event, bus); | ||
197 | wl_event_source_check(source); | ||
171 | } | 198 | } |
172 | #endif | 199 | #endif |
173 | 200 | ||
@@ -339,12 +366,6 @@ static int parse_args(int argc, char *argv[]) { | |||
339 | return 0; | 366 | return 0; |
340 | } | 367 | } |
341 | 368 | ||
342 | void sway_terminate(int exit_code) { | ||
343 | wl_display_disconnect(state.display); | ||
344 | wl_event_loop_destroy(state.event_loop); | ||
345 | exit(exit_code); | ||
346 | } | ||
347 | |||
348 | static void register_zero_idle_timeout(void *item) { | 369 | static void register_zero_idle_timeout(void *item) { |
349 | struct swayidle_timeout_cmd *cmd = item; | 370 | struct swayidle_timeout_cmd *cmd = item; |
350 | register_timeout(cmd, 0); | 371 | register_timeout(cmd, 0); |
@@ -365,15 +386,26 @@ static int handle_signal(int sig, void *data) { | |||
365 | } | 386 | } |
366 | 387 | ||
367 | static int display_event(int fd, uint32_t mask, void *data) { | 388 | static int display_event(int fd, uint32_t mask, void *data) { |
368 | if (mask & WL_EVENT_HANGUP) { | 389 | if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) { |
369 | sway_terminate(0); | 390 | sway_terminate(0); |
370 | } | 391 | } |
371 | if (wl_display_dispatch(state.display) < 0) { | 392 | |
393 | int count = 0; | ||
394 | if (mask & WL_EVENT_READABLE) { | ||
395 | count = wl_display_dispatch(state.display); | ||
396 | } | ||
397 | if (mask & WL_EVENT_WRITABLE) { | ||
398 | wl_display_flush(state.display); | ||
399 | } | ||
400 | if (mask == 0) { | ||
401 | count = wl_display_dispatch_pending(state.display); | ||
402 | wl_display_flush(state.display); | ||
403 | } | ||
404 | if (count < 0) { | ||
372 | wlr_log_errno(WLR_ERROR, "wl_display_dispatch failed, exiting"); | 405 | wlr_log_errno(WLR_ERROR, "wl_display_dispatch failed, exiting"); |
373 | sway_terminate(0); | 406 | sway_terminate(0); |
374 | } | 407 | } |
375 | wl_display_flush(state.display); | 408 | return count; |
376 | return 0; | ||
377 | } | 409 | } |
378 | 410 | ||
379 | static void register_idle_timeout(void *item) { | 411 | static void register_idle_timeout(void *item) { |
@@ -430,7 +462,7 @@ int main(int argc, char *argv[]) { | |||
430 | wl_display_roundtrip(state.display); | 462 | wl_display_roundtrip(state.display); |
431 | 463 | ||
432 | struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop, | 464 | struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop, |
433 | wl_display_get_fd(state.display), WL_EVENT_READABLE, | 465 | wl_display_get_fd(state.display), WL_EVENT_READABLE | WL_EVENT_WRITABLE, |
434 | display_event, NULL); | 466 | display_event, NULL); |
435 | wl_event_source_check(source); | 467 | wl_event_source_check(source); |
436 | 468 | ||