aboutsummaryrefslogtreecommitdiffstats
path: root/swayidle
diff options
context:
space:
mode:
authorLibravatar emersion <contact@emersion.fr>2018-10-28 09:16:42 +0100
committerLibravatar emersion <contact@emersion.fr>2018-10-28 09:16:42 +0100
commit5fcb814a736698f76259904e291650a6d0485f8c (patch)
tree1d86abcfb44d4d33a59160677ab66821b78cb31b /swayidle
parentMerge pull request #2997 from RyanDwyer/fix-cursor-during-operation (diff)
downloadsway-5fcb814a736698f76259904e291650a6d0485f8c.tar.gz
sway-5fcb814a736698f76259904e291650a6d0485f8c.tar.zst
sway-5fcb814a736698f76259904e291650a6d0485f8c.zip
swayidle: enter idle state on SIGUSR1
Diffstat (limited to 'swayidle')
-rw-r--r--swayidle/main.c214
-rw-r--r--swayidle/swayidle.1.scd14
2 files changed, 119 insertions, 109 deletions
diff --git a/swayidle/main.c b/swayidle/main.c
index 93f4c94b..7d0f23f4 100644
--- a/swayidle/main.c
+++ b/swayidle/main.c
@@ -1,4 +1,5 @@
1#define _XOPEN_SOURCE 500 1#define _POSIX_C_SOURCE 200809L
2#include <assert.h>
2#include <errno.h> 3#include <errno.h>
3#include <getopt.h> 4#include <getopt.h>
4#include <pthread.h> 5#include <pthread.h>
@@ -25,38 +26,24 @@
25#include <elogind/sd-login.h> 26#include <elogind/sd-login.h>
26#endif 27#endif
27 28
28typedef void (*timer_callback_func)(void *data);
29
30static struct org_kde_kwin_idle *idle_manager = NULL; 29static struct org_kde_kwin_idle *idle_manager = NULL;
31static struct wl_seat *seat = NULL; 30static struct wl_seat *seat = NULL;
32bool debug = false;
33 31
34struct swayidle_state { 32struct swayidle_state {
35 struct wl_display *display; 33 struct wl_display *display;
36 struct org_kde_kwin_idle_timeout *idle_timer;
37 struct org_kde_kwin_idle_timeout *lock_timer;
38 struct wl_event_loop *event_loop; 34 struct wl_event_loop *event_loop;
39 list_t *timeout_cmds; 35 list_t *timeout_cmds; // struct swayidle_timeout_cmd *
36 char *lock_cmd;
40} state; 37} state;
41 38
42struct swayidle_cmd {
43 timer_callback_func callback;
44 char *param;
45};
46
47struct swayidle_cmd *lock_cmd = NULL;
48
49struct swayidle_timeout_cmd { 39struct swayidle_timeout_cmd {
50 uint32_t timeout; 40 int timeout, registered_timeout;
51 struct swayidle_cmd *idle_cmd; 41 struct org_kde_kwin_idle_timeout *idle_timer;
52 struct swayidle_cmd *resume_cmd; 42 char *idle_cmd;
43 char *resume_cmd;
53}; 44};
54 45
55static void cmd_exec(void *data) { 46static void cmd_exec(char *param) {
56 if (data == NULL) {
57 return;
58 }
59 char *param = (char *)data;
60 wlr_log(WLR_DEBUG, "Cmd exec %s", param); 47 wlr_log(WLR_DEBUG, "Cmd exec %s", param);
61 pid_t pid = fork(); 48 pid_t pid = fork();
62 if (pid == 0) { 49 if (pid == 0) {
@@ -82,6 +69,7 @@ static void cmd_exec(void *data) {
82#if defined(SWAY_IDLE_HAS_SYSTEMD) || defined(SWAY_IDLE_HAS_ELOGIND) 69#if defined(SWAY_IDLE_HAS_SYSTEMD) || defined(SWAY_IDLE_HAS_ELOGIND)
83static int lock_fd = -1; 70static int lock_fd = -1;
84static int ongoing_fd = -1; 71static int ongoing_fd = -1;
72static struct sd_bus *bus = NULL;
85 73
86static int release_lock(void *data) { 74static int release_lock(void *data) {
87 wlr_log(WLR_INFO, "Releasing sleep lock %d", ongoing_fd); 75 wlr_log(WLR_INFO, "Releasing sleep lock %d", ongoing_fd);
@@ -92,19 +80,10 @@ static int release_lock(void *data) {
92 return 0; 80 return 0;
93} 81}
94 82
95void acquire_sleep_lock(void) { 83static void acquire_sleep_lock(void) {
96 sd_bus_message *msg = NULL; 84 sd_bus_message *msg;
97 sd_bus_error error = SD_BUS_ERROR_NULL; 85 sd_bus_error error;
98 struct sd_bus *bus; 86 int ret = sd_bus_call_method(bus, "org.freedesktop.login1",
99 int ret = sd_bus_default_system(&bus);
100
101 if (ret < 0) {
102 wlr_log(WLR_ERROR, "Failed to open D-Bus connection: %s",
103 strerror(-ret));
104 return;
105 }
106
107 ret = sd_bus_call_method(bus, "org.freedesktop.login1",
108 "/org/freedesktop/login1", 87 "/org/freedesktop/login1",
109 "org.freedesktop.login1.Manager", "Inhibit", 88 "org.freedesktop.login1.Manager", "Inhibit",
110 &error, &msg, "ssss", "sleep", "swayidle", 89 &error, &msg, "ssss", "sleep", "swayidle",
@@ -112,14 +91,16 @@ void acquire_sleep_lock(void) {
112 if (ret < 0) { 91 if (ret < 0) {
113 wlr_log(WLR_ERROR, "Failed to send Inhibit signal: %s", 92 wlr_log(WLR_ERROR, "Failed to send Inhibit signal: %s",
114 strerror(-ret)); 93 strerror(-ret));
115 } else { 94 return;
116 ret = sd_bus_message_read(msg, "h", &lock_fd); 95 }
117 if (ret < 0) { 96
118 wlr_log(WLR_ERROR, 97 ret = sd_bus_message_read(msg, "h", &lock_fd);
119 "Failed to parse D-Bus response for Inhibit: %s", 98 if (ret < 0) {
120 strerror(-ret)); 99 wlr_log(WLR_ERROR, "Failed to parse D-Bus response for Inhibit: %s",
121 } 100 strerror(-ret));
101 return;
122 } 102 }
103
123 wlr_log(WLR_INFO, "Got sleep lock: %d", lock_fd); 104 wlr_log(WLR_INFO, "Got sleep lock: %d", lock_fd);
124} 105}
125 106
@@ -140,8 +121,8 @@ static int prepare_for_sleep(sd_bus_message *msg, void *userdata,
140 121
141 ongoing_fd = lock_fd; 122 ongoing_fd = lock_fd;
142 123
143 if (lock_cmd && lock_cmd->callback) { 124 if (state.lock_cmd) {
144 lock_cmd->callback(lock_cmd->param); 125 cmd_exec(state.lock_cmd);
145 } 126 }
146 127
147 if (ongoing_fd >= 0) { 128 if (ongoing_fd >= 0) {
@@ -149,6 +130,7 @@ static int prepare_for_sleep(sd_bus_message *msg, void *userdata,
149 wl_event_loop_add_timer(state.event_loop, release_lock, NULL); 130 wl_event_loop_add_timer(state.event_loop, release_lock, NULL);
150 wl_event_source_timer_update(source, 1000); 131 wl_event_source_timer_update(source, 1000);
151 } 132 }
133
152 wlr_log(WLR_DEBUG, "Prepare for sleep done"); 134 wlr_log(WLR_DEBUG, "Prepare for sleep done");
153 return 0; 135 return 0;
154} 136}
@@ -161,9 +143,7 @@ static int dbus_event(int fd, uint32_t mask, void *data) {
161 return 1; 143 return 1;
162} 144}
163 145
164void setup_sleep_listener(void) { 146static void setup_sleep_listener(void) {
165 struct sd_bus *bus;
166
167 int ret = sd_bus_default_system(&bus); 147 int ret = sd_bus_default_system(&bus);
168 if (ret < 0) { 148 if (ret < 0) {
169 wlr_log(WLR_ERROR, "Failed to open D-Bus connection: %s", 149 wlr_log(WLR_ERROR, "Failed to open D-Bus connection: %s",
@@ -203,6 +183,7 @@ static void handle_global(void *data, struct wl_registry *registry,
203 183
204static void handle_global_remove(void *data, struct wl_registry *registry, 184static void handle_global_remove(void *data, struct wl_registry *registry,
205 uint32_t name) { 185 uint32_t name) {
186 // Who cares
206} 187}
207 188
208static const struct wl_registry_listener registry_listener = { 189static const struct wl_registry_listener registry_listener = {
@@ -210,19 +191,42 @@ static const struct wl_registry_listener registry_listener = {
210 .global_remove = handle_global_remove, 191 .global_remove = handle_global_remove,
211}; 192};
212 193
194static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener;
195
196static void register_timeout(struct swayidle_timeout_cmd *cmd,
197 int timeout) {
198 if (cmd->idle_timer != NULL) {
199 org_kde_kwin_idle_timeout_destroy(cmd->idle_timer);
200 cmd->idle_timer = NULL;
201 }
202 if (timeout < 0) {
203 wlr_log(WLR_DEBUG, "Not registering idle timeout");
204 return;
205 }
206 wlr_log(WLR_DEBUG, "Register with timeout: %d", timeout);
207 cmd->idle_timer =
208 org_kde_kwin_idle_get_idle_timeout(idle_manager, seat, timeout);
209 org_kde_kwin_idle_timeout_add_listener(cmd->idle_timer,
210 &idle_timer_listener, cmd);
211 cmd->registered_timeout = timeout;
212}
213
213static void handle_idle(void *data, struct org_kde_kwin_idle_timeout *timer) { 214static void handle_idle(void *data, struct org_kde_kwin_idle_timeout *timer) {
214 struct swayidle_timeout_cmd *cmd = data; 215 struct swayidle_timeout_cmd *cmd = data;
215 wlr_log(WLR_DEBUG, "idle state"); 216 wlr_log(WLR_DEBUG, "idle state");
216 if (cmd && cmd->idle_cmd && cmd->idle_cmd->callback) { 217 if (cmd->idle_cmd) {
217 cmd->idle_cmd->callback(cmd->idle_cmd->param); 218 cmd_exec(cmd->idle_cmd);
218 } 219 }
219} 220}
220 221
221static void handle_resume(void *data, struct org_kde_kwin_idle_timeout *timer) { 222static void handle_resume(void *data, struct org_kde_kwin_idle_timeout *timer) {
222 struct swayidle_timeout_cmd *cmd = data; 223 struct swayidle_timeout_cmd *cmd = data;
223 wlr_log(WLR_DEBUG, "active state"); 224 wlr_log(WLR_DEBUG, "active state");
224 if (cmd && cmd->resume_cmd && cmd->resume_cmd->callback) { 225 if (cmd->registered_timeout != cmd->timeout) {
225 cmd->resume_cmd->callback(cmd->resume_cmd->param); 226 register_timeout(cmd, cmd->timeout);
227 }
228 if (cmd->resume_cmd) {
229 cmd_exec(cmd->resume_cmd);
226 } 230 }
227} 231}
228 232
@@ -231,20 +235,17 @@ static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = {
231 .resumed = handle_resume, 235 .resumed = handle_resume,
232}; 236};
233 237
234struct swayidle_cmd *parse_command(int argc, char **argv) { 238static char *parse_command(int argc, char **argv) {
235 if (argc < 1) { 239 if (argc < 1) {
236 wlr_log(WLR_ERROR, "Too few parameters for command in parse_command"); 240 wlr_log(WLR_ERROR, "Missing command");
237 return NULL; 241 return NULL;
238 } 242 }
239 243
240 struct swayidle_cmd *cmd = calloc(1, sizeof(struct swayidle_cmd));
241 wlr_log(WLR_DEBUG, "Command: %s", argv[0]); 244 wlr_log(WLR_DEBUG, "Command: %s", argv[0]);
242 cmd->callback = cmd_exec; 245 return strdup(argv[0]);
243 cmd->param = argv[0];
244 return cmd;
245} 246}
246 247
247int parse_timeout(int argc, char **argv) { 248static int parse_timeout(int argc, char **argv) {
248 if (argc < 3) { 249 if (argc < 3) {
249 wlr_log(WLR_ERROR, "Too few parameters to timeout command. " 250 wlr_log(WLR_ERROR, "Too few parameters to timeout command. "
250 "Usage: timeout <seconds> <command>"); 251 "Usage: timeout <seconds> <command>");
@@ -258,9 +259,15 @@ int parse_timeout(int argc, char **argv) {
258 "numeric value representing seconds", optarg); 259 "numeric value representing seconds", optarg);
259 exit(-1); 260 exit(-1);
260 } 261 }
262
261 struct swayidle_timeout_cmd *cmd = 263 struct swayidle_timeout_cmd *cmd =
262 calloc(1, sizeof(struct swayidle_timeout_cmd)); 264 calloc(1, sizeof(struct swayidle_timeout_cmd));
263 cmd->timeout = seconds * 1000; 265
266 if (seconds > 0) {
267 cmd->timeout = seconds * 1000;
268 } else {
269 cmd->timeout = -1;
270 }
264 271
265 wlr_log(WLR_DEBUG, "Register idle timeout at %d ms", cmd->timeout); 272 wlr_log(WLR_DEBUG, "Register idle timeout at %d ms", cmd->timeout);
266 wlr_log(WLR_DEBUG, "Setup idle"); 273 wlr_log(WLR_DEBUG, "Setup idle");
@@ -276,27 +283,27 @@ int parse_timeout(int argc, char **argv) {
276 return result; 283 return result;
277} 284}
278 285
279int parse_sleep(int argc, char **argv) { 286static int parse_sleep(int argc, char **argv) {
280 if (argc < 2) { 287 if (argc < 2) {
281 wlr_log(WLR_ERROR, "Too few parameters to before-sleep command. " 288 wlr_log(WLR_ERROR, "Too few parameters to before-sleep command. "
282 "Usage: before-sleep <command>"); 289 "Usage: before-sleep <command>");
283 exit(-1); 290 exit(-1);
284 } 291 }
285 292
286 lock_cmd = parse_command(argc - 1, &argv[1]); 293 state.lock_cmd = parse_command(argc - 1, &argv[1]);
287 if (lock_cmd) { 294 if (state.lock_cmd) {
288 wlr_log(WLR_DEBUG, "Setup sleep lock: %s", lock_cmd->param); 295 wlr_log(WLR_DEBUG, "Setup sleep lock: %s", state.lock_cmd);
289 } 296 }
290 297
291 return 2; 298 return 2;
292} 299}
293 300
301static int parse_args(int argc, char *argv[]) {
302 bool debug = false;
294 303
295int parse_args(int argc, char *argv[]) {
296 int c; 304 int c;
297 305 while ((c = getopt(argc, argv, "hd")) != -1) {
298 while ((c = getopt(argc, argv, "hs:d")) != -1) { 306 switch (c) {
299 switch(c) {
300 case 'd': 307 case 'd':
301 debug = true; 308 debug = true;
302 break; 309 break;
@@ -311,13 +318,7 @@ int parse_args(int argc, char *argv[]) {
311 } 318 }
312 } 319 }
313 320
314 if (debug) { 321 wlr_log_init(debug ? WLR_DEBUG : WLR_INFO, NULL);
315 wlr_log_init(WLR_DEBUG, NULL);
316 wlr_log(WLR_DEBUG, "Loglevel debug");
317 } else {
318 wlr_log_init(WLR_INFO, NULL);
319 }
320
321 322
322 state.timeout_cmds = create_list(); 323 state.timeout_cmds = create_list();
323 324
@@ -331,24 +332,36 @@ int parse_args(int argc, char *argv[]) {
331 i += parse_sleep(argc - i, &argv[i]); 332 i += parse_sleep(argc - i, &argv[i]);
332 } else { 333 } else {
333 wlr_log(WLR_ERROR, "Unsupported command '%s'", argv[i]); 334 wlr_log(WLR_ERROR, "Unsupported command '%s'", argv[i]);
334 exit(-1); 335 return 1;
335 } 336 }
336 } 337 }
338
337 return 0; 339 return 0;
338} 340}
339 341
340void sway_terminate(int exit_code) { 342void sway_terminate(int exit_code) {
341 if (state.event_loop) { 343 wl_display_disconnect(state.display);
342 wl_event_loop_destroy(state.event_loop); 344 wl_event_loop_destroy(state.event_loop);
343 }
344 if (state.display) {
345 wl_display_disconnect(state.display);
346 }
347 exit(exit_code); 345 exit(exit_code);
348} 346}
349 347
350void sig_handler(int signal) { 348static void register_zero_idle_timeout(void *item) {
351 sway_terminate(0); 349 struct swayidle_timeout_cmd *cmd = item;
350 register_timeout(cmd, 0);
351}
352
353static int handle_signal(int sig, void *data) {
354 switch (sig) {
355 case SIGINT:
356 case SIGTERM:
357 sway_terminate(0);
358 return 0;
359 case SIGUSR1:
360 wlr_log(WLR_DEBUG, "Got SIGUSR1");
361 list_foreach(state.timeout_cmds, register_zero_idle_timeout);
362 return 1;
363 }
364 assert(false); // not reached
352} 365}
353 366
354static int display_event(int fd, uint32_t mask, void *data) { 367static int display_event(int fd, uint32_t mask, void *data) {
@@ -359,33 +372,26 @@ static int display_event(int fd, uint32_t mask, void *data) {
359 wlr_log_errno(WLR_ERROR, "wl_display_dispatch failed, exiting"); 372 wlr_log_errno(WLR_ERROR, "wl_display_dispatch failed, exiting");
360 sway_terminate(0); 373 sway_terminate(0);
361 } 374 }
375 wl_display_flush(state.display);
362 return 0; 376 return 0;
363} 377}
364 378
365void register_idle_timeout(void *item) { 379static void register_idle_timeout(void *item) {
366 struct swayidle_timeout_cmd *cmd = item; 380 struct swayidle_timeout_cmd *cmd = item;
367 if (cmd == NULL || !cmd->timeout) { 381 register_timeout(cmd, cmd->timeout);
368 wlr_log(WLR_ERROR, "Invalid idle cmd, will not register");
369 return;
370 }
371 state.idle_timer =
372 org_kde_kwin_idle_get_idle_timeout(idle_manager, seat, cmd->timeout);
373 if (state.idle_timer != NULL) {
374 org_kde_kwin_idle_timeout_add_listener(state.idle_timer,
375 &idle_timer_listener, cmd);
376 } else {
377 wlr_log(WLR_ERROR, "Could not create idle timer");
378 }
379} 382}
380 383
381int main(int argc, char *argv[]) { 384int main(int argc, char *argv[]) {
382 signal(SIGINT, sig_handler);
383 signal(SIGTERM, sig_handler);
384
385 if (parse_args(argc, argv) != 0) { 385 if (parse_args(argc, argv) != 0) {
386 return -1; 386 return -1;
387 } 387 }
388 388
389 state.event_loop = wl_event_loop_create();
390
391 wl_event_loop_add_signal(state.event_loop, SIGINT, handle_signal, NULL);
392 wl_event_loop_add_signal(state.event_loop, SIGTERM, handle_signal, NULL);
393 wl_event_loop_add_signal(state.event_loop, SIGUSR1, handle_signal, NULL);
394
389 state.display = wl_display_connect(NULL); 395 state.display = wl_display_connect(NULL);
390 if (state.display == NULL) { 396 if (state.display == NULL) {
391 wlr_log(WLR_ERROR, "Unable to connect to the compositor. " 397 wlr_log(WLR_ERROR, "Unable to connect to the compositor. "
@@ -397,7 +403,6 @@ int main(int argc, char *argv[]) {
397 struct wl_registry *registry = wl_display_get_registry(state.display); 403 struct wl_registry *registry = wl_display_get_registry(state.display);
398 wl_registry_add_listener(registry, &registry_listener, NULL); 404 wl_registry_add_listener(registry, &registry_listener, NULL);
399 wl_display_roundtrip(state.display); 405 wl_display_roundtrip(state.display);
400 state.event_loop = wl_event_loop_create();
401 406
402 if (idle_manager == NULL) { 407 if (idle_manager == NULL) {
403 wlr_log(WLR_ERROR, "Display doesn't support idle protocol"); 408 wlr_log(WLR_ERROR, "Display doesn't support idle protocol");
@@ -410,7 +415,7 @@ int main(int argc, char *argv[]) {
410 415
411 bool should_run = state.timeout_cmds->length > 0; 416 bool should_run = state.timeout_cmds->length > 0;
412#if defined(SWAY_IDLE_HAS_SYSTEMD) || defined(SWAY_IDLE_HAS_ELOGIND) 417#if defined(SWAY_IDLE_HAS_SYSTEMD) || defined(SWAY_IDLE_HAS_ELOGIND)
413 if (lock_cmd) { 418 if (state.lock_cmd) {
414 should_run = true; 419 should_run = true;
415 setup_sleep_listener(); 420 setup_sleep_listener();
416 } 421 }
@@ -419,12 +424,15 @@ int main(int argc, char *argv[]) {
419 wlr_log(WLR_INFO, "No command specified! Nothing to do, will exit"); 424 wlr_log(WLR_INFO, "No command specified! Nothing to do, will exit");
420 sway_terminate(0); 425 sway_terminate(0);
421 } 426 }
427
422 list_foreach(state.timeout_cmds, register_idle_timeout); 428 list_foreach(state.timeout_cmds, register_idle_timeout);
423 429
424 wl_display_roundtrip(state.display); 430 wl_display_roundtrip(state.display);
425 431
426 wl_event_loop_add_fd(state.event_loop, wl_display_get_fd(state.display), 432 struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop,
427 WL_EVENT_READABLE, display_event, NULL); 433 wl_display_get_fd(state.display), WL_EVENT_READABLE,
434 display_event, NULL);
435 wl_event_source_check(source);
428 436
429 while (wl_event_loop_dispatch(state.event_loop, -1) != 1) { 437 while (wl_event_loop_dispatch(state.event_loop, -1) != 1) {
430 // This space intentionally left blank 438 // This space intentionally left blank
diff --git a/swayidle/swayidle.1.scd b/swayidle/swayidle.1.scd
index 7c1b138a..3083163f 100644
--- a/swayidle/swayidle.1.scd
+++ b/swayidle/swayidle.1.scd
@@ -22,11 +22,13 @@ swayidle listens for idle activity on your Wayland compositor and executes tasks
22on various idle-related events. You can specify any number of events at the 22on various idle-related events. You can specify any number of events at the
23command line. 23command line.
24 24
25Sending SIGUSR1 to swayidle will immediately enter idle state.
26
25# EVENTS 27# EVENTS
26 28
27*timeout* <timeout> <timeout command> [resume <resume command>] 29*timeout* <timeout> <timeout command> [resume <resume command>]
28 Execute _timeout command_ if there is no activity for <timeout> seconds. 30 Execute _timeout command_ if there is no activity for <timeout> seconds.
29 31
30 If you specify "resume <resume command>", _resume command_ will be run when 32 If you specify "resume <resume command>", _resume command_ will be run when
31 there is activity again. 33 there is activity again.
32 34
@@ -39,11 +41,11 @@ All commands are executed in a shell.
39# EXAMPLE 41# EXAMPLE
40 42
41``` 43```
42 swayidle \ 44swayidle \
43 timeout 300 'swaylock -c 000000' \ 45 timeout 300 'swaylock -c 000000' \
44 timeout 600 'swaymsg "output * dpms off"' \ 46 timeout 600 'swaymsg "output * dpms off"' \
45 resume 'swaymsg "output * dpms on"' \ 47 resume 'swaymsg "output * dpms on"' \
46 before-sleep 'swaylock -c 000000' 48 before-sleep 'swaylock -c 000000'
47``` 49```
48 50
49This will lock your screen after 300 seconds of inactivity, then turn off your 51This will lock your screen after 300 seconds of inactivity, then turn off your
@@ -58,4 +60,4 @@ https://github.com/swaywm/sway.
58 60
59# SEE ALSO 61# SEE ALSO
60 62
61*sway*(5) *swaymsg*(1) *sway-input*(5) *sway-bar*(5) 63*sway*(5) *swaymsg*(1) *sway-input*(5) *sway-output*(5) *sway-bar*(5)