λ

swayaway

a drop-in replacement for swayidle(1)

settings

swayaway.h

3384 bytes raw

/* swayaway.h - shared state and declarations for swayaway
 *
 * swayaway is a drop-in replacement for swayidle(1) that depends ONLY on
 * libwayland-client and libdbus-1.  No elogind, no libsystemd.
 *
 * Idle timeout handling is driven by a monotonic clock (CLOCK_MONOTONIC)
 * fed by a single ext-idle-notify-v1 tracking notification, and idle
 * inhibition is understood natively by exporting org.freedesktop.ScreenSaver
 * on the session bus.
 *
 * Everything runs in one process on a single poll() loop watching:
 *   - the wayland fd (wl_display_get_fd)
 *   - the session bus fd (the inhibitor service)
 *   - the system bus fd (logind PrepareForSleep sleep events)
 *   - a signalfd (SIGINT/SIGTERM/SIGUSR1)
 */
#ifndef SWAYAWAY_H
#define SWAYAWAY_H

#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L

#include <stdbool.h>
#include <stdint.h>
#include <dbus/dbus.h>

struct wl_display;
struct wl_registry;
struct wl_seat;
struct ext_idle_notifier_v1;

struct timeout_cmd {
	struct timeout_cmd *next;
	int seconds; // idle timeout in seconds (>0), -1 = disabled
	bool fired; // already fired during the current idle cycle
	char *idle_cmd; // shell command run on timeout, may be NULL
	char *resume_cmd; // shell command run on resume, may be NULL
};

// legacy top-level `resume <cmd>` event
struct resume_cmd {
	struct resume_cmd *next;
	char *cmd;
};

// one held org.freedesktop.ScreenSaver cookie
struct inhibitor {
	struct inhibitor *next;
	uint32_t cookie;
	char *sender; // unique bus name, e.g. ":1.42"
	char *app;
	char *reason;
};

struct swayaway {
	// options
	bool wait;
	bool debug;
	char *seat_name;

	// wayland
	struct wl_display *display;
	struct ext_idle_notifier_v1 *notifier;
	struct wl_seat *seat;

	// idle state
	struct timeout_cmd *timeouts;
	struct resume_cmd *resume_cmds;
	bool is_idle; // compositor reported idle
	int64_t idle_since_ms; // CLOCK_MONOTONIC at idle start
	bool inhibited; // any inhibitor active
	bool screensaver_inhibited; // someone holds a ScreenSaver lock

	// session bus / inhibitor service
	DBusConnection *session_conn;
	int session_fd;
	uint32_t next_cookie;
	struct inhibitor *inhibitors;

	// system bus / sleep events
	DBusConnection *system_conn;
	int system_fd;
	int sleep_lock_fd; // held logind sleep delay lock

	// sleep events
	char *before_sleep_cmd;
	char *after_resume_cmd;

	bool running;
};

extern struct swayaway state;

// log.c
void log_init(bool debug);
void sway_log(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
void sway_log_debug(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
void cmd_exec(const char *cmd);

// idle.c
void idle_add_timeout(int seconds, char *idle_cmd, char *resume_cmd);
void idle_add_resume_cmd(char *cmd);
void idle_on_idled(void);
void idle_on_resumed(void);
void idle_check_timeouts(void);
int idle_next_timeout_ms(void);
void idle_force_idle(void);
void idle_terminate(void);
void idle_recompute_inhibited(void);
bool idle_has_work(void);
bool idle_any_timeout_fired(void);
int64_t mono_ms(void);

// wayland.c
int wayland_init(void);
void wayland_create_tracking_notification(void);

// screensaver.c
void screensaver_init(void);
bool screensaver_have_service(void);
void screensaver_emit_active(void);

// logind.c
void logind_init(void);
void logind_acquire_sleep_lock(void);
void logind_release_sleep_lock(void);

#endif /* SWAYAWAY_H */