summaryrefslogtreecommitdiffstats
path: root/swayidle/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'swayidle/main.c')
-rw-r--r--swayidle/main.c483
1 files changed, 0 insertions, 483 deletions
diff --git a/swayidle/main.c b/swayidle/main.c
deleted file mode 100644
index 41eecc41..00000000
--- a/swayidle/main.c
+++ /dev/null
@@ -1,483 +0,0 @@
1#define _POSIX_C_SOURCE 200809L
2#include <assert.h>
3#include <errno.h>
4#include <fcntl.h>
5#include <getopt.h>
6#include <pthread.h>
7#include <signal.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/wait.h>
12#include <unistd.h>
13#include <wayland-client-protocol.h>
14#include <wayland-client.h>
15#include <wayland-server.h>
16#include <wayland-util.h>
17#include <wlr/config.h>
18#include <wlr/util/log.h>
19#include "config.h"
20#include "idle-client-protocol.h"
21#include "list.h"
22#if HAVE_SYSTEMD
23#include <systemd/sd-bus.h>
24#include <systemd/sd-login.h>
25#elif HAVE_ELOGIND
26#include <elogind/sd-bus.h>
27#include <elogind/sd-login.h>
28#endif
29
30static struct org_kde_kwin_idle *idle_manager = NULL;
31static struct wl_seat *seat = NULL;
32
33struct swayidle_state {
34 struct wl_display *display;
35 struct wl_event_loop *event_loop;
36 list_t *timeout_cmds; // struct swayidle_timeout_cmd *
37 char *lock_cmd;
38} state;
39
40struct swayidle_timeout_cmd {
41 int timeout, registered_timeout;
42 struct org_kde_kwin_idle_timeout *idle_timer;
43 char *idle_cmd;
44 char *resume_cmd;
45};
46
47void sway_terminate(int exit_code) {
48 wl_display_disconnect(state.display);
49 wl_event_loop_destroy(state.event_loop);
50 exit(exit_code);
51}
52
53static void cmd_exec(char *param) {
54 wlr_log(WLR_DEBUG, "Cmd exec %s", param);
55 pid_t pid = fork();
56 if (pid == 0) {
57 pid = fork();
58 if (pid == 0) {
59 char *const cmd[] = { "sh", "-c", param, NULL, };
60 execvp(cmd[0], cmd);
61 wlr_log_errno(WLR_ERROR, "execve failed!");
62 exit(1);
63 } else if (pid < 0) {
64 wlr_log_errno(WLR_ERROR, "fork failed");
65 exit(1);
66 }
67 exit(0);
68 } else if (pid < 0) {
69 wlr_log_errno(WLR_ERROR, "fork failed");
70 } else {
71 wlr_log(WLR_DEBUG, "Spawned process %s", param);
72 waitpid(pid, NULL, 0);
73 }
74}
75
76#if HAVE_SYSTEMD || HAVE_ELOGIND
77static int lock_fd = -1;
78static int ongoing_fd = -1;
79static struct sd_bus *bus = NULL;
80
81static int release_lock(void *data) {
82 wlr_log(WLR_INFO, "Releasing sleep lock %d", ongoing_fd);
83 if (ongoing_fd >= 0) {
84 close(ongoing_fd);
85 }
86 ongoing_fd = -1;
87 return 0;
88}
89
90static void acquire_sleep_lock(void) {
91 sd_bus_message *msg = NULL;
92 sd_bus_error error = SD_BUS_ERROR_NULL;
93 int ret = sd_bus_call_method(bus, "org.freedesktop.login1",
94 "/org/freedesktop/login1",
95 "org.freedesktop.login1.Manager", "Inhibit",
96 &error, &msg, "ssss", "sleep", "swayidle",
97 "Setup Up Lock Screen", "delay");
98 if (ret < 0) {
99 wlr_log(WLR_ERROR, "Failed to send Inhibit signal: %s", error.message);
100 sd_bus_error_free(&error);
101 return;
102 }
103
104 ret = sd_bus_message_read(msg, "h", &lock_fd);
105 if (ret < 0) {
106 wlr_log(WLR_ERROR, "Failed to parse D-Bus response for Inhibit: %s",
107 strerror(-ret));
108 sd_bus_error_free(&error);
109 sd_bus_message_unref(msg);
110 return;
111 } else {
112 wlr_log(WLR_INFO, "Got sleep lock: %d", lock_fd);
113 }
114
115 // sd_bus_message_unref closes the file descriptor so we need
116 // to copy it beforehand
117 lock_fd = fcntl(lock_fd, F_DUPFD_CLOEXEC, 3);
118 if (lock_fd < 0) {
119 wlr_log(WLR_ERROR, "Failed to copy sleep lock fd: %s",
120 strerror(errno));
121 }
122
123 sd_bus_error_free(&error);
124 sd_bus_message_unref(msg);
125}
126
127static int prepare_for_sleep(sd_bus_message *msg, void *userdata,
128 sd_bus_error *ret_error) {
129 /* "b" apparently reads into an int, not a bool */
130 int going_down = 1;
131 int ret = sd_bus_message_read(msg, "b", &going_down);
132 if (ret < 0) {
133 wlr_log(WLR_ERROR, "Failed to parse D-Bus response for Inhibit: %s",
134 strerror(-ret));
135 }
136 wlr_log(WLR_DEBUG, "PrepareForSleep signal received %d", going_down);
137 if (!going_down) {
138 acquire_sleep_lock();
139 return 0;
140 }
141
142 ongoing_fd = lock_fd;
143
144 if (state.lock_cmd) {
145 cmd_exec(state.lock_cmd);
146 }
147
148 if (ongoing_fd >= 0) {
149 struct wl_event_source *source =
150 wl_event_loop_add_timer(state.event_loop, release_lock, NULL);
151 wl_event_source_timer_update(source, 1000);
152 }
153
154 wlr_log(WLR_DEBUG, "Prepare for sleep done");
155 return 0;
156}
157
158static int dbus_event(int fd, uint32_t mask, void *data) {
159 sd_bus *bus = data;
160
161 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
162 sway_terminate(0);
163 }
164
165 int count = 0;
166 if (mask & WL_EVENT_READABLE) {
167 count = sd_bus_process(bus, NULL);
168 }
169 if (mask & WL_EVENT_WRITABLE) {
170 sd_bus_flush(bus);
171 }
172 if (mask == 0) {
173 sd_bus_flush(bus);
174 }
175
176 if (count < 0) {
177 wlr_log_errno(WLR_ERROR, "sd_bus_process failed, exiting");
178 sway_terminate(0);
179 }
180
181 return count;
182}
183
184static void setup_sleep_listener(void) {
185 int ret = sd_bus_default_system(&bus);
186 if (ret < 0) {
187 wlr_log(WLR_ERROR, "Failed to open D-Bus connection: %s",
188 strerror(-ret));
189 return;
190 }
191
192 char str[256];
193 const char *fmt = "type='signal',"
194 "sender='org.freedesktop.login1',"
195 "interface='org.freedesktop.login1.%s',"
196 "member='%s'," "path='%s'";
197
198 snprintf(str, sizeof(str), fmt, "Manager", "PrepareForSleep",
199 "/org/freedesktop/login1");
200 ret = sd_bus_add_match(bus, NULL, str, prepare_for_sleep, NULL);
201 if (ret < 0) {
202 wlr_log(WLR_ERROR, "Failed to add D-Bus match: %s", strerror(-ret));
203 return;
204 }
205 acquire_sleep_lock();
206
207 struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop,
208 sd_bus_get_fd(bus), WL_EVENT_READABLE, dbus_event, bus);
209 wl_event_source_check(source);
210}
211#endif
212
213static void handle_global(void *data, struct wl_registry *registry,
214 uint32_t name, const char *interface, uint32_t version) {
215 if (strcmp(interface, org_kde_kwin_idle_interface.name) == 0) {
216 idle_manager =
217 wl_registry_bind(registry, name, &org_kde_kwin_idle_interface, 1);
218 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
219 seat = wl_registry_bind(registry, name, &wl_seat_interface, 1);
220 }
221}
222
223static void handle_global_remove(void *data, struct wl_registry *registry,
224 uint32_t name) {
225 // Who cares
226}
227
228static const struct wl_registry_listener registry_listener = {
229 .global = handle_global,
230 .global_remove = handle_global_remove,
231};
232
233static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener;
234
235static void register_timeout(struct swayidle_timeout_cmd *cmd,
236 int timeout) {
237 if (cmd->idle_timer != NULL) {
238 org_kde_kwin_idle_timeout_destroy(cmd->idle_timer);
239 cmd->idle_timer = NULL;
240 }
241 if (timeout < 0) {
242 wlr_log(WLR_DEBUG, "Not registering idle timeout");
243 return;
244 }
245 wlr_log(WLR_DEBUG, "Register with timeout: %d", timeout);
246 cmd->idle_timer =
247 org_kde_kwin_idle_get_idle_timeout(idle_manager, seat, timeout);
248 org_kde_kwin_idle_timeout_add_listener(cmd->idle_timer,
249 &idle_timer_listener, cmd);
250 cmd->registered_timeout = timeout;
251}
252
253static void handle_idle(void *data, struct org_kde_kwin_idle_timeout *timer) {
254 struct swayidle_timeout_cmd *cmd = data;
255 wlr_log(WLR_DEBUG, "idle state");
256 if (cmd->idle_cmd) {
257 cmd_exec(cmd->idle_cmd);
258 }
259}
260
261static void handle_resume(void *data, struct org_kde_kwin_idle_timeout *timer) {
262 struct swayidle_timeout_cmd *cmd = data;
263 wlr_log(WLR_DEBUG, "active state");
264 if (cmd->registered_timeout != cmd->timeout) {
265 register_timeout(cmd, cmd->timeout);
266 }
267 if (cmd->resume_cmd) {
268 cmd_exec(cmd->resume_cmd);
269 }
270}
271
272static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = {
273 .idle = handle_idle,
274 .resumed = handle_resume,
275};
276
277static char *parse_command(int argc, char **argv) {
278 if (argc < 1) {
279 wlr_log(WLR_ERROR, "Missing command");
280 return NULL;
281 }
282
283 wlr_log(WLR_DEBUG, "Command: %s", argv[0]);
284 return strdup(argv[0]);
285}
286
287static int parse_timeout(int argc, char **argv) {
288 if (argc < 3) {
289 wlr_log(WLR_ERROR, "Too few parameters to timeout command. "
290 "Usage: timeout <seconds> <command>");
291 exit(-1);
292 }
293 errno = 0;
294 char *endptr;
295 int seconds = strtoul(argv[1], &endptr, 10);
296 if (errno != 0 || *endptr != '\0') {
297 wlr_log(WLR_ERROR, "Invalid timeout parameter '%s', it should be a "
298 "numeric value representing seconds", optarg);
299 exit(-1);
300 }
301
302 struct swayidle_timeout_cmd *cmd =
303 calloc(1, sizeof(struct swayidle_timeout_cmd));
304
305 if (seconds > 0) {
306 cmd->timeout = seconds * 1000;
307 } else {
308 cmd->timeout = -1;
309 }
310
311 wlr_log(WLR_DEBUG, "Register idle timeout at %d ms", cmd->timeout);
312 wlr_log(WLR_DEBUG, "Setup idle");
313 cmd->idle_cmd = parse_command(argc - 2, &argv[2]);
314
315 int result = 3;
316 if (argc >= 5 && !strcmp("resume", argv[3])) {
317 wlr_log(WLR_DEBUG, "Setup resume");
318 cmd->resume_cmd = parse_command(argc - 4, &argv[4]);
319 result = 5;
320 }
321 list_add(state.timeout_cmds, cmd);
322 return result;
323}
324
325static int parse_sleep(int argc, char **argv) {
326 if (argc < 2) {
327 wlr_log(WLR_ERROR, "Too few parameters to before-sleep command. "
328 "Usage: before-sleep <command>");
329 exit(-1);
330 }
331
332 state.lock_cmd = parse_command(argc - 1, &argv[1]);
333 if (state.lock_cmd) {
334 wlr_log(WLR_DEBUG, "Setup sleep lock: %s", state.lock_cmd);
335 }
336
337 return 2;
338}
339
340static int parse_args(int argc, char *argv[]) {
341 bool debug = false;
342
343 int c;
344 while ((c = getopt(argc, argv, "hd")) != -1) {
345 switch (c) {
346 case 'd':
347 debug = true;
348 break;
349 case 'h':
350 case '?':
351 printf("Usage: %s [OPTIONS]\n", argv[0]);
352 printf(" -d\tdebug\n");
353 printf(" -h\tthis help menu\n");
354 return 1;
355 default:
356 return 1;
357 }
358 }
359
360 wlr_log_init(debug ? WLR_DEBUG : WLR_INFO, NULL);
361
362 state.timeout_cmds = create_list();
363
364 int i = optind;
365 while (i < argc) {
366 if (!strcmp("timeout", argv[i])) {
367 wlr_log(WLR_DEBUG, "Got timeout");
368 i += parse_timeout(argc - i, &argv[i]);
369 } else if (!strcmp("before-sleep", argv[i])) {
370 wlr_log(WLR_DEBUG, "Got before-sleep");
371 i += parse_sleep(argc - i, &argv[i]);
372 } else {
373 wlr_log(WLR_ERROR, "Unsupported command '%s'", argv[i]);
374 return 1;
375 }
376 }
377
378 return 0;
379}
380
381static int handle_signal(int sig, void *data) {
382 switch (sig) {
383 case SIGINT:
384 case SIGTERM:
385 sway_terminate(0);
386 return 0;
387 case SIGUSR1:
388 wlr_log(WLR_DEBUG, "Got SIGUSR1");
389 for (int i = 0; i < state.timeout_cmds->length; ++i) {
390 register_timeout(state.timeout_cmds->items[i], 0);
391 }
392 return 1;
393 }
394 assert(false); // not reached
395}
396
397static int display_event(int fd, uint32_t mask, void *data) {
398 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
399 sway_terminate(0);
400 }
401
402 int count = 0;
403 if (mask & WL_EVENT_READABLE) {
404 count = wl_display_dispatch(state.display);
405 }
406 if (mask & WL_EVENT_WRITABLE) {
407 wl_display_flush(state.display);
408 }
409 if (mask == 0) {
410 count = wl_display_dispatch_pending(state.display);
411 wl_display_flush(state.display);
412 }
413
414 if (count < 0) {
415 wlr_log_errno(WLR_ERROR, "wl_display_dispatch failed, exiting");
416 sway_terminate(0);
417 }
418
419 return count;
420}
421
422int main(int argc, char *argv[]) {
423 if (parse_args(argc, argv) != 0) {
424 return -1;
425 }
426
427 state.event_loop = wl_event_loop_create();
428
429 wl_event_loop_add_signal(state.event_loop, SIGINT, handle_signal, NULL);
430 wl_event_loop_add_signal(state.event_loop, SIGTERM, handle_signal, NULL);
431 wl_event_loop_add_signal(state.event_loop, SIGUSR1, handle_signal, NULL);
432
433 state.display = wl_display_connect(NULL);
434 if (state.display == NULL) {
435 wlr_log(WLR_ERROR, "Unable to connect to the compositor. "
436 "If your compositor is running, check or set the "
437 "WAYLAND_DISPLAY environment variable.");
438 return -3;
439 }
440
441 struct wl_registry *registry = wl_display_get_registry(state.display);
442 wl_registry_add_listener(registry, &registry_listener, NULL);
443 wl_display_roundtrip(state.display);
444
445 if (idle_manager == NULL) {
446 wlr_log(WLR_ERROR, "Display doesn't support idle protocol");
447 return -4;
448 }
449 if (seat == NULL) {
450 wlr_log(WLR_ERROR, "Seat error");
451 return -5;
452 }
453
454 bool should_run = state.timeout_cmds->length > 0;
455#if HAVE_SYSTEMD || HAVE_ELOGIND
456 if (state.lock_cmd) {
457 should_run = true;
458 setup_sleep_listener();
459 }
460#endif
461 if (!should_run) {
462 wlr_log(WLR_INFO, "No command specified! Nothing to do, will exit");
463 sway_terminate(0);
464 }
465
466 for (int i = 0; i < state.timeout_cmds->length; ++i) {
467 struct swayidle_timeout_cmd *cmd = state.timeout_cmds->items[i];
468 register_timeout(cmd, cmd->timeout);
469 }
470
471 wl_display_roundtrip(state.display);
472
473 struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop,
474 wl_display_get_fd(state.display), WL_EVENT_READABLE,
475 display_event, NULL);
476 wl_event_source_check(source);
477
478 while (wl_event_loop_dispatch(state.event_loop, -1) != 1) {
479 // This space intentionally left blank
480 }
481
482 sway_terminate(0);
483}