2018-10-28 08:16:42 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <assert.h>
|
2018-07-16 21:20:53 +00:00
|
|
|
#include <errno.h>
|
2018-04-17 07:54:02 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <pthread.h>
|
2018-07-16 21:20:53 +00:00
|
|
|
#include <signal.h>
|
2018-04-17 07:54:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-06-08 13:02:47 +00:00
|
|
|
#include <sys/wait.h>
|
2018-04-17 07:54:02 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wayland-client-protocol.h>
|
|
|
|
#include <wayland-client.h>
|
2018-07-16 21:20:53 +00:00
|
|
|
#include <wayland-server.h>
|
2018-04-17 07:54:02 +00:00
|
|
|
#include <wayland-util.h>
|
|
|
|
#include <wlr/config.h>
|
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include "config.h"
|
2018-07-16 21:20:53 +00:00
|
|
|
#include "idle-client-protocol.h"
|
2018-04-17 07:54:02 +00:00
|
|
|
#include "list.h"
|
|
|
|
#ifdef SWAY_IDLE_HAS_SYSTEMD
|
|
|
|
#include <systemd/sd-bus.h>
|
|
|
|
#include <systemd/sd-login.h>
|
|
|
|
#elif defined(SWAY_IDLE_HAS_ELOGIND)
|
|
|
|
#include <elogind/sd-bus.h>
|
|
|
|
#include <elogind/sd-login.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct org_kde_kwin_idle *idle_manager = NULL;
|
|
|
|
static struct wl_seat *seat = NULL;
|
|
|
|
|
|
|
|
struct swayidle_state {
|
|
|
|
struct wl_display *display;
|
|
|
|
struct wl_event_loop *event_loop;
|
2018-10-28 08:16:42 +00:00
|
|
|
list_t *timeout_cmds; // struct swayidle_timeout_cmd *
|
|
|
|
char *lock_cmd;
|
2018-04-17 07:54:02 +00:00
|
|
|
} state;
|
|
|
|
|
|
|
|
struct swayidle_timeout_cmd {
|
2018-10-28 08:16:42 +00:00
|
|
|
int timeout, registered_timeout;
|
|
|
|
struct org_kde_kwin_idle_timeout *idle_timer;
|
|
|
|
char *idle_cmd;
|
|
|
|
char *resume_cmd;
|
2018-04-17 07:54:02 +00:00
|
|
|
};
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
static void cmd_exec(char *param) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Cmd exec %s", param);
|
2018-06-08 13:02:47 +00:00
|
|
|
pid_t pid = fork();
|
2018-04-17 07:54:02 +00:00
|
|
|
if (pid == 0) {
|
2018-06-08 13:02:47 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
char *const cmd[] = { "sh", "-c", param, NULL, };
|
|
|
|
execvp(cmd[0], cmd);
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log_errno(WLR_ERROR, "execve failed!");
|
2018-06-08 13:02:47 +00:00
|
|
|
exit(1);
|
|
|
|
} else if (pid < 0) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log_errno(WLR_ERROR, "fork failed");
|
2018-06-08 13:02:47 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
} else if (pid < 0) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log_errno(WLR_ERROR, "fork failed");
|
2018-06-08 13:02:47 +00:00
|
|
|
} else {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Spawned process %s", param);
|
2018-06-08 13:02:47 +00:00
|
|
|
waitpid(pid, NULL, 0);
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(SWAY_IDLE_HAS_SYSTEMD) || defined(SWAY_IDLE_HAS_ELOGIND)
|
|
|
|
static int lock_fd = -1;
|
|
|
|
static int ongoing_fd = -1;
|
2018-10-28 08:16:42 +00:00
|
|
|
static struct sd_bus *bus = NULL;
|
2018-04-17 07:54:02 +00:00
|
|
|
|
|
|
|
static int release_lock(void *data) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_INFO, "Releasing sleep lock %d", ongoing_fd);
|
2018-04-17 07:54:02 +00:00
|
|
|
if (ongoing_fd >= 0) {
|
|
|
|
close(ongoing_fd);
|
|
|
|
}
|
|
|
|
ongoing_fd = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
static void acquire_sleep_lock(void) {
|
|
|
|
sd_bus_message *msg;
|
|
|
|
sd_bus_error error;
|
|
|
|
int ret = sd_bus_call_method(bus, "org.freedesktop.login1",
|
2018-04-17 07:54:02 +00:00
|
|
|
"/org/freedesktop/login1",
|
|
|
|
"org.freedesktop.login1.Manager", "Inhibit",
|
|
|
|
&error, &msg, "ssss", "sleep", "swayidle",
|
|
|
|
"Setup Up Lock Screen", "delay");
|
|
|
|
if (ret < 0) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to send Inhibit signal: %s",
|
2018-04-17 07:54:02 +00:00
|
|
|
strerror(-ret));
|
2018-10-28 08:16:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sd_bus_message_read(msg, "h", &lock_fd);
|
|
|
|
if (ret < 0) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to parse D-Bus response for Inhibit: %s",
|
|
|
|
strerror(-ret));
|
|
|
|
return;
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
2018-10-28 08:16:42 +00:00
|
|
|
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_INFO, "Got sleep lock: %d", lock_fd);
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int prepare_for_sleep(sd_bus_message *msg, void *userdata,
|
|
|
|
sd_bus_error *ret_error) {
|
2018-06-17 17:57:40 +00:00
|
|
|
/* "b" apparently reads into an int, not a bool */
|
|
|
|
int going_down = 1;
|
2018-04-17 07:54:02 +00:00
|
|
|
int ret = sd_bus_message_read(msg, "b", &going_down);
|
|
|
|
if (ret < 0) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to parse D-Bus response for Inhibit: %s",
|
2018-04-17 07:54:02 +00:00
|
|
|
strerror(-ret));
|
|
|
|
}
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "PrepareForSleep signal received %d", going_down);
|
2018-04-17 07:54:02 +00:00
|
|
|
if (!going_down) {
|
|
|
|
acquire_sleep_lock();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ongoing_fd = lock_fd;
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
if (state.lock_cmd) {
|
|
|
|
cmd_exec(state.lock_cmd);
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ongoing_fd >= 0) {
|
|
|
|
struct wl_event_source *source =
|
|
|
|
wl_event_loop_add_timer(state.event_loop, release_lock, NULL);
|
|
|
|
wl_event_source_timer_update(source, 1000);
|
|
|
|
}
|
2018-10-28 08:16:42 +00:00
|
|
|
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Prepare for sleep done");
|
2018-04-17 07:54:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dbus_event(int fd, uint32_t mask, void *data) {
|
|
|
|
sd_bus *bus = data;
|
|
|
|
while (sd_bus_process(bus, NULL) > 0) {
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
static void setup_sleep_listener(void) {
|
2018-04-17 07:54:02 +00:00
|
|
|
int ret = sd_bus_default_system(&bus);
|
|
|
|
if (ret < 0) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to open D-Bus connection: %s",
|
2018-04-17 07:54:02 +00:00
|
|
|
strerror(-ret));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char str[256];
|
|
|
|
const char *fmt = "type='signal',"
|
|
|
|
"sender='org.freedesktop.login1',"
|
|
|
|
"interface='org.freedesktop.login1.%s',"
|
|
|
|
"member='%s'," "path='%s'";
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), fmt, "Manager", "PrepareForSleep",
|
|
|
|
"/org/freedesktop/login1");
|
|
|
|
ret = sd_bus_add_match(bus, NULL, str, prepare_for_sleep, NULL);
|
|
|
|
if (ret < 0) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to add D-Bus match: %s", strerror(-ret));
|
2018-04-17 07:54:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
acquire_sleep_lock();
|
|
|
|
|
|
|
|
wl_event_loop_add_fd(state.event_loop, sd_bus_get_fd(bus),
|
|
|
|
WL_EVENT_READABLE, dbus_event, bus);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void handle_global(void *data, struct wl_registry *registry,
|
|
|
|
uint32_t name, const char *interface, uint32_t version) {
|
|
|
|
if (strcmp(interface, org_kde_kwin_idle_interface.name) == 0) {
|
|
|
|
idle_manager =
|
|
|
|
wl_registry_bind(registry, name, &org_kde_kwin_idle_interface, 1);
|
|
|
|
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
|
|
|
seat = wl_registry_bind(registry, name, &wl_seat_interface, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_global_remove(void *data, struct wl_registry *registry,
|
|
|
|
uint32_t name) {
|
2018-10-28 08:16:42 +00:00
|
|
|
// Who cares
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_registry_listener registry_listener = {
|
|
|
|
.global = handle_global,
|
|
|
|
.global_remove = handle_global_remove,
|
|
|
|
};
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener;
|
|
|
|
|
|
|
|
static void register_timeout(struct swayidle_timeout_cmd *cmd,
|
|
|
|
int timeout) {
|
|
|
|
if (cmd->idle_timer != NULL) {
|
|
|
|
org_kde_kwin_idle_timeout_destroy(cmd->idle_timer);
|
|
|
|
cmd->idle_timer = NULL;
|
|
|
|
}
|
|
|
|
if (timeout < 0) {
|
|
|
|
wlr_log(WLR_DEBUG, "Not registering idle timeout");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wlr_log(WLR_DEBUG, "Register with timeout: %d", timeout);
|
|
|
|
cmd->idle_timer =
|
|
|
|
org_kde_kwin_idle_get_idle_timeout(idle_manager, seat, timeout);
|
|
|
|
org_kde_kwin_idle_timeout_add_listener(cmd->idle_timer,
|
|
|
|
&idle_timer_listener, cmd);
|
|
|
|
cmd->registered_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
2018-04-17 07:54:02 +00:00
|
|
|
static void handle_idle(void *data, struct org_kde_kwin_idle_timeout *timer) {
|
|
|
|
struct swayidle_timeout_cmd *cmd = data;
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "idle state");
|
2018-10-28 08:16:42 +00:00
|
|
|
if (cmd->idle_cmd) {
|
|
|
|
cmd_exec(cmd->idle_cmd);
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_resume(void *data, struct org_kde_kwin_idle_timeout *timer) {
|
|
|
|
struct swayidle_timeout_cmd *cmd = data;
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "active state");
|
2018-10-28 08:16:42 +00:00
|
|
|
if (cmd->registered_timeout != cmd->timeout) {
|
|
|
|
register_timeout(cmd, cmd->timeout);
|
|
|
|
}
|
|
|
|
if (cmd->resume_cmd) {
|
|
|
|
cmd_exec(cmd->resume_cmd);
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = {
|
|
|
|
.idle = handle_idle,
|
|
|
|
.resumed = handle_resume,
|
|
|
|
};
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
static char *parse_command(int argc, char **argv) {
|
2018-04-17 07:54:02 +00:00
|
|
|
if (argc < 1) {
|
2018-10-28 08:16:42 +00:00
|
|
|
wlr_log(WLR_ERROR, "Missing command");
|
2018-04-17 07:54:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Command: %s", argv[0]);
|
2018-10-28 08:16:42 +00:00
|
|
|
return strdup(argv[0]);
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
static int parse_timeout(int argc, char **argv) {
|
2018-04-17 07:54:02 +00:00
|
|
|
if (argc < 3) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_ERROR, "Too few parameters to timeout command. "
|
2018-04-17 07:54:02 +00:00
|
|
|
"Usage: timeout <seconds> <command>");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
char *endptr;
|
|
|
|
int seconds = strtoul(argv[1], &endptr, 10);
|
|
|
|
if (errno != 0 || *endptr != '\0') {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_ERROR, "Invalid timeout parameter '%s', it should be a "
|
2018-04-17 07:54:02 +00:00
|
|
|
"numeric value representing seconds", optarg);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2018-10-28 08:16:42 +00:00
|
|
|
|
2018-04-17 07:54:02 +00:00
|
|
|
struct swayidle_timeout_cmd *cmd =
|
|
|
|
calloc(1, sizeof(struct swayidle_timeout_cmd));
|
2018-10-28 08:16:42 +00:00
|
|
|
|
|
|
|
if (seconds > 0) {
|
|
|
|
cmd->timeout = seconds * 1000;
|
|
|
|
} else {
|
|
|
|
cmd->timeout = -1;
|
|
|
|
}
|
2018-04-17 07:54:02 +00:00
|
|
|
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Register idle timeout at %d ms", cmd->timeout);
|
|
|
|
wlr_log(WLR_DEBUG, "Setup idle");
|
2018-04-17 07:54:02 +00:00
|
|
|
cmd->idle_cmd = parse_command(argc - 2, &argv[2]);
|
|
|
|
|
|
|
|
int result = 3;
|
|
|
|
if (argc >= 5 && !strcmp("resume", argv[3])) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Setup resume");
|
2018-04-17 07:54:02 +00:00
|
|
|
cmd->resume_cmd = parse_command(argc - 4, &argv[4]);
|
|
|
|
result = 5;
|
|
|
|
}
|
|
|
|
list_add(state.timeout_cmds, cmd);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
static int parse_sleep(int argc, char **argv) {
|
2018-04-17 07:54:02 +00:00
|
|
|
if (argc < 2) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_ERROR, "Too few parameters to before-sleep command. "
|
2018-04-17 07:54:02 +00:00
|
|
|
"Usage: before-sleep <command>");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
state.lock_cmd = parse_command(argc - 1, &argv[1]);
|
|
|
|
if (state.lock_cmd) {
|
|
|
|
wlr_log(WLR_DEBUG, "Setup sleep lock: %s", state.lock_cmd);
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
static int parse_args(int argc, char *argv[]) {
|
|
|
|
bool debug = false;
|
2018-04-17 07:54:02 +00:00
|
|
|
|
|
|
|
int c;
|
2018-10-28 08:16:42 +00:00
|
|
|
while ((c = getopt(argc, argv, "hd")) != -1) {
|
|
|
|
switch (c) {
|
2018-04-17 07:54:02 +00:00
|
|
|
case 'd':
|
|
|
|
debug = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
printf("Usage: %s [OPTIONS]\n", argv[0]);
|
|
|
|
printf(" -d\tdebug\n");
|
|
|
|
printf(" -h\tthis help menu\n");
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
wlr_log_init(debug ? WLR_DEBUG : WLR_INFO, NULL);
|
2018-04-17 07:54:02 +00:00
|
|
|
|
|
|
|
state.timeout_cmds = create_list();
|
|
|
|
|
|
|
|
int i = optind;
|
|
|
|
while (i < argc) {
|
|
|
|
if (!strcmp("timeout", argv[i])) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Got timeout");
|
2018-04-17 07:54:02 +00:00
|
|
|
i += parse_timeout(argc - i, &argv[i]);
|
|
|
|
} else if (!strcmp("before-sleep", argv[i])) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Got before-sleep");
|
2018-04-17 07:54:02 +00:00
|
|
|
i += parse_sleep(argc - i, &argv[i]);
|
|
|
|
} else {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_ERROR, "Unsupported command '%s'", argv[i]);
|
2018-10-28 08:16:42 +00:00
|
|
|
return 1;
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 08:16:42 +00:00
|
|
|
|
2018-04-17 07:54:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sway_terminate(int exit_code) {
|
2018-10-28 08:16:42 +00:00
|
|
|
wl_display_disconnect(state.display);
|
|
|
|
wl_event_loop_destroy(state.event_loop);
|
2018-04-17 07:54:02 +00:00
|
|
|
exit(exit_code);
|
|
|
|
}
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
static void register_zero_idle_timeout(void *item) {
|
|
|
|
struct swayidle_timeout_cmd *cmd = item;
|
|
|
|
register_timeout(cmd, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_signal(int sig, void *data) {
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
sway_terminate(0);
|
|
|
|
return 0;
|
|
|
|
case SIGUSR1:
|
|
|
|
wlr_log(WLR_DEBUG, "Got SIGUSR1");
|
|
|
|
list_foreach(state.timeout_cmds, register_zero_idle_timeout);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
assert(false); // not reached
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int display_event(int fd, uint32_t mask, void *data) {
|
2018-05-16 02:25:55 +00:00
|
|
|
if (mask & WL_EVENT_HANGUP) {
|
|
|
|
sway_terminate(0);
|
|
|
|
}
|
2018-05-16 07:50:34 +00:00
|
|
|
if (wl_display_dispatch(state.display) < 0) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log_errno(WLR_ERROR, "wl_display_dispatch failed, exiting");
|
2018-05-16 07:50:34 +00:00
|
|
|
sway_terminate(0);
|
2018-07-16 21:20:53 +00:00
|
|
|
}
|
2018-10-28 08:16:42 +00:00
|
|
|
wl_display_flush(state.display);
|
2018-04-17 07:54:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
static void register_idle_timeout(void *item) {
|
2018-04-17 07:54:02 +00:00
|
|
|
struct swayidle_timeout_cmd *cmd = item;
|
2018-10-28 08:16:42 +00:00
|
|
|
register_timeout(cmd, cmd->timeout);
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (parse_args(argc, argv) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
state.event_loop = wl_event_loop_create();
|
|
|
|
|
|
|
|
wl_event_loop_add_signal(state.event_loop, SIGINT, handle_signal, NULL);
|
|
|
|
wl_event_loop_add_signal(state.event_loop, SIGTERM, handle_signal, NULL);
|
|
|
|
wl_event_loop_add_signal(state.event_loop, SIGUSR1, handle_signal, NULL);
|
|
|
|
|
2018-04-17 07:54:02 +00:00
|
|
|
state.display = wl_display_connect(NULL);
|
|
|
|
if (state.display == NULL) {
|
2018-10-15 11:57:59 +00:00
|
|
|
wlr_log(WLR_ERROR, "Unable to connect to the compositor. "
|
|
|
|
"If your compositor is running, check or set the "
|
|
|
|
"WAYLAND_DISPLAY environment variable.");
|
2018-04-17 07:54:02 +00:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wl_registry *registry = wl_display_get_registry(state.display);
|
|
|
|
wl_registry_add_listener(registry, ®istry_listener, NULL);
|
|
|
|
wl_display_roundtrip(state.display);
|
|
|
|
|
|
|
|
if (idle_manager == NULL) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_ERROR, "Display doesn't support idle protocol");
|
2018-04-17 07:54:02 +00:00
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
if (seat == NULL) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_ERROR, "Seat error");
|
2018-04-17 07:54:02 +00:00
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool should_run = state.timeout_cmds->length > 0;
|
|
|
|
#if defined(SWAY_IDLE_HAS_SYSTEMD) || defined(SWAY_IDLE_HAS_ELOGIND)
|
2018-10-28 08:16:42 +00:00
|
|
|
if (state.lock_cmd) {
|
2018-04-17 07:54:02 +00:00
|
|
|
should_run = true;
|
|
|
|
setup_sleep_listener();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!should_run) {
|
2018-07-09 21:54:30 +00:00
|
|
|
wlr_log(WLR_INFO, "No command specified! Nothing to do, will exit");
|
2018-04-17 07:54:02 +00:00
|
|
|
sway_terminate(0);
|
|
|
|
}
|
2018-10-28 08:16:42 +00:00
|
|
|
|
2018-04-17 07:54:02 +00:00
|
|
|
list_foreach(state.timeout_cmds, register_idle_timeout);
|
|
|
|
|
|
|
|
wl_display_roundtrip(state.display);
|
|
|
|
|
2018-10-28 08:16:42 +00:00
|
|
|
struct wl_event_source *source = wl_event_loop_add_fd(state.event_loop,
|
|
|
|
wl_display_get_fd(state.display), WL_EVENT_READABLE,
|
|
|
|
display_event, NULL);
|
|
|
|
wl_event_source_check(source);
|
2018-04-17 07:54:02 +00:00
|
|
|
|
2018-05-14 00:21:37 +00:00
|
|
|
while (wl_event_loop_dispatch(state.event_loop, -1) != 1) {
|
|
|
|
// This space intentionally left blank
|
2018-04-17 07:54:02 +00:00
|
|
|
}
|
2018-05-13 23:32:16 +00:00
|
|
|
|
2018-04-17 07:54:02 +00:00
|
|
|
sway_terminate(0);
|
|
|
|
}
|