swayaway
a drop-in replacement for swayidle(1)
swayaway.h
3384 bytes raw
1/* swayaway.h - shared state and declarations for swayaway
2 *
3 * swayaway is a drop-in replacement for swayidle(1) that depends ONLY on
4 * libwayland-client and libdbus-1. No elogind, no libsystemd.
5 *
6 * Idle timeout handling is driven by a monotonic clock (CLOCK_MONOTONIC)
7 * fed by a single ext-idle-notify-v1 tracking notification, and idle
8 * inhibition is understood natively by exporting org.freedesktop.ScreenSaver
9 * on the session bus.
10 *
11 * Everything runs in one process on a single poll() loop watching:
12 * - the wayland fd (wl_display_get_fd)
13 * - the session bus fd (the inhibitor service)
14 * - the system bus fd (logind PrepareForSleep sleep events)
15 * - a signalfd (SIGINT/SIGTERM/SIGUSR1)
16 */
17#ifndef SWAYAWAY_H
18#define SWAYAWAY_H
19
20#define _GNU_SOURCE
21#define _POSIX_C_SOURCE 200809L
22
23#include <stdbool.h>
24#include <stdint.h>
25#include <dbus/dbus.h>
26
27struct wl_display;
28struct wl_registry;
29struct wl_seat;
30struct ext_idle_notifier_v1;
31
32struct timeout_cmd {
33 struct timeout_cmd *next;
34 int seconds; // idle timeout in seconds (>0), -1 = disabled
35 bool fired; // already fired during the current idle cycle
36 char *idle_cmd; // shell command run on timeout, may be NULL
37 char *resume_cmd; // shell command run on resume, may be NULL
38};
39
40// legacy top-level `resume <cmd>` event
41struct resume_cmd {
42 struct resume_cmd *next;
43 char *cmd;
44};
45
46// one held org.freedesktop.ScreenSaver cookie
47struct inhibitor {
48 struct inhibitor *next;
49 uint32_t cookie;
50 char *sender; // unique bus name, e.g. ":1.42"
51 char *app;
52 char *reason;
53};
54
55struct swayaway {
56 // options
57 bool wait;
58 bool debug;
59 char *seat_name;
60
61 // wayland
62 struct wl_display *display;
63 struct ext_idle_notifier_v1 *notifier;
64 struct wl_seat *seat;
65
66 // idle state
67 struct timeout_cmd *timeouts;
68 struct resume_cmd *resume_cmds;
69 bool is_idle; // compositor reported idle
70 int64_t idle_since_ms; // CLOCK_MONOTONIC at idle start
71 bool inhibited; // any inhibitor active
72 bool screensaver_inhibited; // someone holds a ScreenSaver lock
73
74 // session bus / inhibitor service
75 DBusConnection *session_conn;
76 int session_fd;
77 uint32_t next_cookie;
78 struct inhibitor *inhibitors;
79
80 // system bus / sleep events
81 DBusConnection *system_conn;
82 int system_fd;
83 int sleep_lock_fd; // held logind sleep delay lock
84
85 // sleep events
86 char *before_sleep_cmd;
87 char *after_resume_cmd;
88
89 bool running;
90};
91
92extern struct swayaway state;
93
94// log.c
95void log_init(bool debug);
96void sway_log(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
97void sway_log_debug(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
98void cmd_exec(const char *cmd);
99
100// idle.c
101void idle_add_timeout(int seconds, char *idle_cmd, char *resume_cmd);
102void idle_add_resume_cmd(char *cmd);
103void idle_on_idled(void);
104void idle_on_resumed(void);
105void idle_check_timeouts(void);
106int idle_next_timeout_ms(void);
107void idle_force_idle(void);
108void idle_terminate(void);
109void idle_recompute_inhibited(void);
110bool idle_has_work(void);
111bool idle_any_timeout_fired(void);
112int64_t mono_ms(void);
113
114// wayland.c
115int wayland_init(void);
116void wayland_create_tracking_notification(void);
117
118// screensaver.c
119void screensaver_init(void);
120bool screensaver_have_service(void);
121void screensaver_emit_active(void);
122
123// logind.c
124void logind_init(void);
125void logind_acquire_sleep_lock(void);
126void logind_release_sleep_lock(void);
127
128#endif /* SWAYAWAY_H */