From ae4478e17f56c1fb52a77d1f034387f154eddef5 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Tue, 11 Jul 2017 18:51:19 +1200 Subject: Changed ownership of wlr_session to the multi backend. Currently breaks VT switching for examples. --- include/wlr/backend.h | 3 +-- include/wlr/backend/multi.h | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'include/wlr') diff --git a/include/wlr/backend.h b/include/wlr/backend.h index db85c169..74cb34a9 100644 --- a/include/wlr/backend.h +++ b/include/wlr/backend.h @@ -19,8 +19,7 @@ struct wlr_backend { } events; }; -struct wlr_backend *wlr_backend_autocreate(struct wl_display *display, - struct wlr_session *session); +struct wlr_backend *wlr_backend_autocreate(struct wl_display *display); bool wlr_backend_init(struct wlr_backend *backend); void wlr_backend_destroy(struct wlr_backend *backend); diff --git a/include/wlr/backend/multi.h b/include/wlr/backend/multi.h index a07ca770..00b34722 100644 --- a/include/wlr/backend/multi.h +++ b/include/wlr/backend/multi.h @@ -2,8 +2,11 @@ #define _WLR_BACKEND_MULTI_H #include +#include +#include -struct wlr_backend *wlr_multi_backend_create(); +struct wlr_backend *wlr_multi_backend_create(struct wlr_session *session, + struct wlr_udev *udev); void wlr_multi_backend_add(struct wlr_backend *multi, struct wlr_backend *backend); -- cgit v1.2.3 From 2ae5cd6539253e03e065f3ecbf55179527bb5ea2 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Tue, 11 Jul 2017 19:03:09 +1200 Subject: Moved session/ into backend/ --- backend/session/direct-ipc.c | 241 +++++++++++++++++++ backend/session/direct.c | 244 ++++++++++++++++++++ backend/session/logind.c | 398 ++++++++++++++++++++++++++++++++ backend/session/session.c | 54 +++++ include/backend/session/direct-ipc.h | 12 + include/session/direct-ipc.h | 12 - include/wlr/backend/session.h | 65 ++++++ include/wlr/backend/session/interface.h | 14 ++ include/wlr/session.h | 65 ------ include/wlr/session/interface.h | 14 -- session/direct-ipc.c | 241 ------------------- session/direct.c | 244 -------------------- session/logind.c | 398 -------------------------------- session/session.c | 54 ----- 14 files changed, 1028 insertions(+), 1028 deletions(-) create mode 100644 backend/session/direct-ipc.c create mode 100644 backend/session/direct.c create mode 100644 backend/session/logind.c create mode 100644 backend/session/session.c create mode 100644 include/backend/session/direct-ipc.h delete mode 100644 include/session/direct-ipc.h create mode 100644 include/wlr/backend/session.h create mode 100644 include/wlr/backend/session/interface.h delete mode 100644 include/wlr/session.h delete mode 100644 include/wlr/session/interface.h delete mode 100644 session/direct-ipc.c delete mode 100644 session/direct.c delete mode 100644 session/logind.c delete mode 100644 session/session.c (limited to 'include/wlr') diff --git a/backend/session/direct-ipc.c b/backend/session/direct-ipc.c new file mode 100644 index 00000000..885662e0 --- /dev/null +++ b/backend/session/direct-ipc.c @@ -0,0 +1,241 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "session/direct-ipc.h" + +enum { DRM_MAJOR = 226 }; + +#ifdef HAS_LIBCAP +#include + +static bool have_permissions(void) { + cap_t cap = cap_get_proc(); + cap_flag_value_t val; + + if (!cap || cap_get_flag(cap, CAP_SYS_ADMIN, CAP_PERMITTED, &val) || val != CAP_SET) { + wlr_log(L_ERROR, "Do not have CAP_SYS_ADMIN; cannot become DRM master"); + cap_free(cap); + return false; + } + + cap_free(cap); + return true; +} +#else +static bool have_permissions(void) { + if (geteuid() != 0) { + wlr_log(L_ERROR, "Do not have root privileges; cannot become DRM master"); + return false; + } + + return true; +} +#endif + +static void send_msg(int sock, int fd, void *buf, size_t buf_len) { + char control[CMSG_SPACE(sizeof(fd))] = {0}; + struct iovec iovec = { .iov_base = buf, .iov_len = buf_len }; + struct msghdr msghdr = {0}; + + if (buf) { + msghdr.msg_iov = &iovec; + msghdr.msg_iovlen = 1; + } + + if (fd >= 0) { + msghdr.msg_control = &control; + msghdr.msg_controllen = sizeof(control); + + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msghdr); + *cmsg = (struct cmsghdr) { + .cmsg_level = SOL_SOCKET, + .cmsg_type = SCM_RIGHTS, + .cmsg_len = CMSG_LEN(sizeof(fd)), + }; + memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); + } + + ssize_t ret; + do { + ret = sendmsg(sock, &msghdr, 0); + } while (ret < 0 && errno == EINTR); +} + +static ssize_t recv_msg(int sock, int *fd_out, void *buf, size_t buf_len) { + char control[CMSG_SPACE(sizeof(*fd_out))] = {0}; + struct iovec iovec = { .iov_base = buf, .iov_len = buf_len }; + struct msghdr msghdr = {0}; + + if (buf) { + msghdr.msg_iov = &iovec; + msghdr.msg_iovlen = 1; + } + + if (fd_out) { + msghdr.msg_control = &control; + msghdr.msg_controllen = sizeof(control); + } + + ssize_t ret; + do { + ret = recvmsg(sock, &msghdr, MSG_CMSG_CLOEXEC); + } while (ret < 0 && errno == EINTR); + + if (fd_out) { + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msghdr); + if (cmsg) { + memcpy(fd_out, CMSG_DATA(cmsg), sizeof(*fd_out)); + } else { + *fd_out = -1; + } + } + + return ret; +} + +enum msg_type { + MSG_OPEN, + MSG_SETMASTER, + MSG_DROPMASTER, + MSG_END, +}; + +struct msg { + enum msg_type type; + char path[256]; +}; + +static void communicate(int sock) { + struct msg msg; + int drm_fd = -1; + bool running = true; + + while (running && recv_msg(sock, &drm_fd, &msg, sizeof(msg)) >= 0) { + switch (msg.type) { + case MSG_OPEN: + errno = 0; + + // These are the same flags that logind opens files with + int fd = open(msg.path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); + int ret = errno; + if (fd == -1) { + goto error; + } + + struct stat st; + if (fstat(fd, &st) < 0) { + ret = errno; + goto error; + } + + uint32_t maj = major(st.st_rdev); + if (maj != INPUT_MAJOR && maj != DRM_MAJOR) { + ret = ENOTSUP; + goto error; + } + + if (maj == DRM_MAJOR && drmSetMaster(fd)) { + ret = errno; + } +error: + send_msg(sock, ret ? -1 : fd, &ret, sizeof(ret)); + close(fd); + + break; + + case MSG_SETMASTER: + drmSetMaster(drm_fd); + close(drm_fd); + send_msg(sock, -1, NULL, 0); + break; + + case MSG_DROPMASTER: + drmDropMaster(drm_fd); + close(drm_fd); + send_msg(sock, -1, NULL, 0); + break; + + case MSG_END: + running = false; + send_msg(sock, -1, NULL, 0); + break; + } + } + + close(sock); +} + +int direct_ipc_open(int sock, const char *path) { + struct msg msg = { .type = MSG_OPEN }; + snprintf(msg.path, sizeof(msg.path), "%s", path); + + send_msg(sock, -1, &msg, sizeof(msg)); + + int fd, err; + recv_msg(sock, &fd, &err, sizeof(err)); + + return err ? -err : fd; +} + +void direct_ipc_setmaster(int sock, int fd) { + struct msg msg = { .type = MSG_SETMASTER }; + + send_msg(sock, fd, &msg, sizeof(msg)); + recv_msg(sock, NULL, NULL, 0); +} + +void direct_ipc_dropmaster(int sock, int fd) { + struct msg msg = { .type = MSG_DROPMASTER }; + + send_msg(sock, fd, &msg, sizeof(msg)); + recv_msg(sock, NULL, NULL, 0); +} + +void direct_ipc_finish(int sock, pid_t pid) { + struct msg msg = { .type = MSG_END }; + + send_msg(sock, -1, &msg, sizeof(msg)); + recv_msg(sock, NULL, NULL, 0); + + waitpid(pid, NULL, 0); +} + +int direct_ipc_start(pid_t *pid_out) { + if (!have_permissions()) { + return -1; + } + + int sock[2]; + if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock) < 0) { + wlr_log_errno(L_ERROR, "Failed to create socket pair"); + return -1; + } + + pid_t pid = fork(); + if (pid < 0) { + wlr_log_errno(L_ERROR, "Fork failed"); + close(sock[0]); + close(sock[1]); + return -1; + } else if (pid == 0) { + close(sock[0]); + communicate(sock[1]); + _Exit(0); + } + + close(sock[1]); + *pid_out = pid; + return sock[0]; +} diff --git a/backend/session/direct.c b/backend/session/direct.c new file mode 100644 index 00000000..1247663b --- /dev/null +++ b/backend/session/direct.c @@ -0,0 +1,244 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "session/direct-ipc.h" + +enum { DRM_MAJOR = 226 }; + +const struct session_impl session_direct; + +struct direct_session { + struct wlr_session base; + int tty_fd; + int old_kbmode; + int sock; + pid_t child; + + struct wl_event_source *vt_source; +}; + +static int direct_session_open(struct wlr_session *base, const char *path) { + struct direct_session *session = wl_container_of(base, session, base); + + int fd = direct_ipc_open(session->sock, path); + if (fd < 0) { + wlr_log(L_ERROR, "Failed to open %s: %s%s", path, strerror(-fd), + fd == -EINVAL ? "; is another display server running?" : ""); + return fd; + } + + struct stat st; + if (fstat(fd, &st) < 0) { + close(fd); + return -errno; + } + + if (major(st.st_rdev) == DRM_MAJOR) { + session->base.drm_fd = fd; + } + + return fd; +} + +static void direct_session_close(struct wlr_session *base, int fd) { + struct direct_session *session = wl_container_of(base, session, base); + + struct stat st; + if (fstat(fd, &st) < 0) { + wlr_log_errno(L_ERROR, "Stat failed"); + close(fd); + return; + } + + if (major(st.st_rdev) == DRM_MAJOR) { + direct_ipc_dropmaster(session->sock, session->base.drm_fd); + session->base.drm_fd = -1; + } else if (major(st.st_rdev) == INPUT_MAJOR) { + ioctl(fd, EVIOCREVOKE, 0); + } + + close(fd); +} + +static bool direct_change_vt(struct wlr_session *base, unsigned vt) { + struct direct_session *session = wl_container_of(base, session, base); + return ioctl(session->tty_fd, VT_ACTIVATE, (int)vt) == 0; +} + +static void direct_session_finish(struct wlr_session *base) { + struct direct_session *session = wl_container_of(base, session, base); + struct vt_mode mode = { + .mode = VT_AUTO, + }; + + errno = 0; + + ioctl(session->tty_fd, KDSKBMODE, session->old_kbmode); + ioctl(session->tty_fd, KDSETMODE, KD_TEXT); + ioctl(session->tty_fd, VT_SETMODE, &mode); + + if (errno) { + wlr_log(L_ERROR, "Failed to restore tty"); + } + + direct_ipc_finish(session->sock, session->child); + close(session->sock); + + wl_event_source_remove(session->vt_source); + close(session->tty_fd); + free(session); +} + +static int vt_handler(int signo, void *data) { + struct direct_session *session = data; + + if (session->base.active) { + session->base.active = false; + wl_signal_emit(&session->base.session_signal, session); + direct_ipc_dropmaster(session->sock, session->base.drm_fd); + ioctl(session->tty_fd, VT_RELDISP, 1); + } else { + ioctl(session->tty_fd, VT_RELDISP, VT_ACKACQ); + direct_ipc_setmaster(session->sock, session->base.drm_fd); + session->base.active = true; + wl_signal_emit(&session->base.session_signal, session); + } + + return 1; +} + +static bool setup_tty(struct direct_session *session, struct wl_display *display) { + int fd = dup(STDIN_FILENO); + if (fd == -1) { + wlr_log_errno(L_ERROR, "Cannot open tty"); + return false; + } + + struct stat st; + if (fstat(fd, &st) == -1 || major(st.st_rdev) != TTY_MAJOR || minor(st.st_rdev) == 0) { + wlr_log(L_ERROR, "Not running from a virtual terminal"); + goto error; + } + + int tty = minor(st.st_rdev); + int ret, kd_mode, old_kbmode; + + ret = ioctl(fd, KDGETMODE, &kd_mode); + if (ret) { + wlr_log_errno(L_ERROR, "Failed to get tty mode"); + goto error; + } + + if (kd_mode != KD_TEXT) { + wlr_log(L_ERROR, + "tty already in graphics mode; is another display server running?"); + goto error; + } + + ioctl(fd, VT_ACTIVATE, tty); + ioctl(fd, VT_WAITACTIVE, tty); + + if (ioctl(fd, KDGKBMODE, &old_kbmode)) { + wlr_log_errno(L_ERROR, "Failed to read keyboard mode"); + goto error; + } + + if (ioctl(fd, KDSKBMODE, K_OFF)) { + wlr_log_errno(L_ERROR, "Failed to set keyboard mode"); + goto error; + } + + if (ioctl(fd, KDSETMODE, KD_GRAPHICS)) { + wlr_log_errno(L_ERROR, "Failed to set graphics mode on tty"); + goto error; + } + + struct vt_mode mode = { + .mode = VT_PROCESS, + .relsig = SIGUSR1, + .acqsig = SIGUSR1, + }; + + if (ioctl(fd, VT_SETMODE, &mode) < 0) { + wlr_log(L_ERROR, "Failed to take control of tty"); + goto error; + } + + struct wl_event_loop *loop = wl_display_get_event_loop(display); + session->vt_source = wl_event_loop_add_signal(loop, SIGUSR1, + vt_handler, session); + if (!session->vt_source) { + goto error; + } + + session->base.vtnr = tty; + session->tty_fd = fd; + session->old_kbmode = old_kbmode; + + return true; + +error: + close(fd); + return false; +} + +static struct wlr_session *direct_session_start(struct wl_display *disp) { + struct direct_session *session = calloc(1, sizeof(*session)); + if (!session) { + wlr_log_errno(L_ERROR, "Allocation failed"); + return NULL; + } + + session->sock = direct_ipc_start(&session->child); + if (session->sock == -1) { + goto error_session; + } + + if (!setup_tty(session, disp)) { + goto error_ipc; + } + + // XXX: Is it okay to trust the environment like this? + const char *seat = getenv("XDG_SEAT"); + if (!seat) { + seat = "seat0"; + } + + wlr_log(L_INFO, "Successfully loaded direct session"); + + snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat); + session->base.drm_fd = -1; + session->base.impl = &session_direct; + session->base.active = true; + wl_signal_init(&session->base.session_signal); + return &session->base; + +error_ipc: + direct_ipc_finish(session->sock, session->child); + close(session->sock); +error_session: + free(session); + return NULL; +} + +const struct session_impl session_direct = { + .start = direct_session_start, + .finish = direct_session_finish, + .open = direct_session_open, + .close = direct_session_close, + .change_vt = direct_change_vt, +}; diff --git a/backend/session/logind.c b/backend/session/logind.c new file mode 100644 index 00000000..6744215a --- /dev/null +++ b/backend/session/logind.c @@ -0,0 +1,398 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +enum { DRM_MAJOR = 226 }; + +const struct session_impl session_logind; + +struct logind_session { + struct wlr_session base; + + sd_bus *bus; + struct wl_event_source *event; + + char *id; + char *path; +}; + +static int logind_take_device(struct wlr_session *base, const char *path) { + struct logind_session *session = wl_container_of(base, session, base); + + int ret; + int fd = -1; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + + struct stat st; + if (stat(path, &st) < 0) { + wlr_log(L_ERROR, "Failed to stat '%s'", path); + return -1; + } + + ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", + session->path, "org.freedesktop.login1.Session", "TakeDevice", + &error, &msg, "uu", major(st.st_rdev), minor(st.st_rdev)); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to take device '%s': %s", path, error.message); + goto error; + } + + int paused = 0; + ret = sd_bus_message_read(msg, "hb", &fd, &paused); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to parse D-Bus response for '%s': %s", + path, strerror(-ret)); + goto error; + } + + // The original fd seems to be closed when the message is freed + // so we just clone it. + fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); + if (fd == -1) { + wlr_log(L_ERROR, "Failed to clone file descriptor for '%s': %s", + path, strerror(errno)); + goto error; + } + + if (major(st.st_rdev) == DRM_MAJOR) { + session->base.drm_fd = fd; + } + +error: + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + return fd; +} + +static void logind_release_device(struct wlr_session *base, int fd) { + struct logind_session *session = wl_container_of(base, session, base); + + int ret; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + + struct stat st; + if (fstat(fd, &st) < 0) { + wlr_log(L_ERROR, "Failed to stat device '%d'", fd); + return; + } + + ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", + session->path, "org.freedesktop.login1.Session", "ReleaseDevice", + &error, &msg, "uu", major(st.st_rdev), minor(st.st_rdev)); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to release device '%d'", fd); + } + + if (major(st.st_rdev) == DRM_MAJOR) { + session->base.drm_fd = -1; + } + + sd_bus_error_free(&error); + sd_bus_message_unref(msg); +} + +static bool logind_change_vt(struct wlr_session *base, unsigned vt) { + struct logind_session *session = wl_container_of(base, session, base); + + int ret; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + + ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", + "/org/freedesktop/login1/seat/self", "org.freedesktop.login1.Seat", "SwitchTo", + &error, &msg, "u", (uint32_t)vt); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to change to vt '%d'", vt); + } + + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + return ret >= 0; +} + +static bool find_sesion_path(struct logind_session *session) { + int ret; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + + ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", + "/org/freedesktop/login1", "org.freedesktop.login1.Manager", + "GetSession", &error, &msg, "s", session->id); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to get session path: %s", strerror(-ret)); + goto out; + } + + const char *path; + + ret = sd_bus_message_read(msg, "o", &path); + if (ret < 0) { + wlr_log(L_ERROR, "Could not parse session path: %s", strerror(-ret)); + goto out; + } + + session->path = strdup(path); + +out: + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + + return ret >= 0; +} + +static bool session_activate(struct logind_session *session) { + int ret; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + + ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", + session->path, "org.freedesktop.login1.Session", "Activate", + &error, &msg, ""); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to activate session"); + } + + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + return ret >= 0; +} + +static bool take_control(struct logind_session *session) { + int ret; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + + ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", + session->path, "org.freedesktop.login1.Session", "TakeControl", + &error, &msg, "b", false); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to take control of session"); + } + + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + return ret >= 0; +} + +static void release_control(struct logind_session *session) { + int ret; + sd_bus_message *msg = NULL; + sd_bus_error error = SD_BUS_ERROR_NULL; + + ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", + session->path, "org.freedesktop.login1.Session", "ReleaseControl", + &error, &msg, ""); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to release control of session"); + } + + sd_bus_error_free(&error); + sd_bus_message_unref(msg); +} + +static void logind_session_finish(struct wlr_session *base) { + struct logind_session *session = wl_container_of(base, session, base); + + release_control(session); + + wl_event_source_remove(session->event); + sd_bus_unref(session->bus); + free(session->id); + free(session->path); + free(session); +} + +static int session_removed(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { + wlr_log(L_INFO, "SessionRemoved signal received"); + return 0; +} + +static int pause_device(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { + struct logind_session *session = userdata; + int ret; + + uint32_t major, minor; + const char *type; + ret = sd_bus_message_read(msg, "uus", &major, &minor, &type); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to parse D-Bus response for PauseDevice: %s", + strerror(-ret)); + goto error; + } + + if (major == DRM_MAJOR) { + session->base.active = false; + wl_signal_emit(&session->base.session_signal, session); + } + + if (strcmp(type, "pause") == 0) { + ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", + session->path, "org.freedesktop.login1.Session", "PauseDeviceComplete", + ret_error, &msg, "uu", major, minor); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to send PauseDeviceComplete signal: %s", + strerror(-ret)); + } + } + +error: + return 0; +} + +static int resume_device(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { + struct logind_session *session = userdata; + int ret; + + int fd; + uint32_t major, minor; + ret = sd_bus_message_read(msg, "uuh", &major, &minor, &fd); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to parse D-Bus response for ResumeDevice: %s", + strerror(-ret)); + goto error; + } + + if (major == DRM_MAJOR) { + dup2(fd, session->base.drm_fd); + session->base.active = true; + wl_signal_emit(&session->base.session_signal, session); + } + +error: + return 0; +} + +static bool add_signal_matches(struct logind_session *session) { + int ret; + + 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", "SesssionRemoved", "/org/freedesktop/login1"); + ret = sd_bus_add_match(session->bus, NULL, str, session_removed, session); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret)); + return false; + } + + snprintf(str, sizeof(str), fmt, "Session", "PauseDevice", session->path); + ret = sd_bus_add_match(session->bus, NULL, str, pause_device, session); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret)); + return false; + } + + snprintf(str, sizeof(str), fmt, "Session", "ResumeDevice", session->path); + ret = sd_bus_add_match(session->bus, NULL, str, resume_device, session); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret)); + return false; + } + + return true; +} + +static int dbus_event(int fd, uint32_t mask, void *data) { + sd_bus *bus = data; + while (sd_bus_process(bus, NULL) > 0); + return 1; +} + +static struct wlr_session *logind_session_start(struct wl_display *disp) { + int ret; + struct logind_session *session = calloc(1, sizeof(*session)); + if (!session) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return NULL; + } + + ret = sd_pid_get_session(getpid(), &session->id); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to get session id: %s", strerror(-ret)); + goto error; + } + + ret = sd_session_get_vt(session->id, &session->base.vtnr); + if (ret < 0) { + wlr_log(L_ERROR, "Session not running in virtual terminal"); + goto error; + } + + char *seat; + ret = sd_session_get_seat(session->id, &seat); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to get seat id: %s", strerror(-ret)); + goto error; + } + snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat); + free(seat); + + ret = sd_bus_default_system(&session->bus); + if (ret < 0) { + wlr_log(L_ERROR, "Failed to open D-Bus connection: %s", strerror(-ret)); + goto error; + } + + if (!find_sesion_path(session)) { + sd_bus_unref(session->bus); + goto error; + } + + if (!add_signal_matches(session)) { + goto error_bus; + } + + struct wl_event_loop *event_loop = wl_display_get_event_loop(disp); + session->event = wl_event_loop_add_fd(event_loop, sd_bus_get_fd(session->bus), + WL_EVENT_READABLE, dbus_event, session->bus); + + if (!session_activate(session)) { + goto error_bus; + } + + if (!take_control(session)) { + goto error_bus; + } + + wlr_log(L_INFO, "Successfully loaded logind session"); + + session->base.drm_fd = -1; + session->base.impl = &session_logind; + session->base.active = true; + wl_signal_init(&session->base.session_signal); + return &session->base; + +error_bus: + sd_bus_unref(session->bus); + free(session->path); + +error: + free(session->id); + return NULL; +} + +const struct session_impl session_logind = { + .start = logind_session_start, + .finish = logind_session_finish, + .open = logind_take_device, + .close = logind_release_device, + .change_vt = logind_change_vt, +}; diff --git a/backend/session/session.c b/backend/session/session.c new file mode 100644 index 00000000..3f4c7910 --- /dev/null +++ b/backend/session/session.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +extern const struct session_impl session_logind; +extern const struct session_impl session_direct; + +static const struct session_impl *impls[] = { +#ifdef HAS_SYSTEMD + &session_logind, +#endif + &session_direct, + NULL, +}; + +struct wlr_session *wlr_session_start(struct wl_display *disp) { + const struct session_impl **iter; + + for (iter = impls; *iter; ++iter) { + struct wlr_session *session = (*iter)->start(disp); + if (session) { + return session; + } + } + + wlr_log(L_ERROR, "Failed to load session backend"); + return NULL; +} + +void wlr_session_finish(struct wlr_session *session) { + if (!session) { + return; + } + + session->impl->finish(session); +}; + +int wlr_session_open_file(struct wlr_session *session, const char *path) { + return session->impl->open(session, path); +} + +void wlr_session_close_file(struct wlr_session *session, int fd) { + session->impl->close(session, fd); +} + +bool wlr_session_change_vt(struct wlr_session *session, unsigned vt) { + if (!session) { + return false; + } + + return session->impl->change_vt(session, vt); +} diff --git a/include/backend/session/direct-ipc.h b/include/backend/session/direct-ipc.h new file mode 100644 index 00000000..96504f04 --- /dev/null +++ b/include/backend/session/direct-ipc.h @@ -0,0 +1,12 @@ +#ifndef SESSION_DIRECT_IPC +#define SESSION_DIRECT_IPC + +#include + +int direct_ipc_open(int sock, const char *path); +void direct_ipc_setmaster(int sock, int fd); +void direct_ipc_dropmaster(int sock, int fd); +void direct_ipc_finish(int sock, pid_t pid); +int direct_ipc_start(pid_t *pid_out); + +#endif diff --git a/include/session/direct-ipc.h b/include/session/direct-ipc.h deleted file mode 100644 index 96504f04..00000000 --- a/include/session/direct-ipc.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef SESSION_DIRECT_IPC -#define SESSION_DIRECT_IPC - -#include - -int direct_ipc_open(int sock, const char *path); -void direct_ipc_setmaster(int sock, int fd); -void direct_ipc_dropmaster(int sock, int fd); -void direct_ipc_finish(int sock, pid_t pid); -int direct_ipc_start(pid_t *pid_out); - -#endif diff --git a/include/wlr/backend/session.h b/include/wlr/backend/session.h new file mode 100644 index 00000000..7961e620 --- /dev/null +++ b/include/wlr/backend/session.h @@ -0,0 +1,65 @@ +#ifndef WLR_SESSION_H +#define WLR_SESSION_H + +#include +#include +#include + +struct session_impl; + +struct wlr_session { + const struct session_impl *impl; + /* + * Signal for when the session becomes active/inactive. + * It's called when we swap virtual terminal. + */ + struct wl_signal session_signal; + bool active; + + int drm_fd; + unsigned vtnr; + char seat[8]; +}; + +/* + * Opens a session, taking control of the current virtual terminal. + * This should not be called if another program is already in control + * of the terminal (Xorg, another Wayland compositor, etc.). + * + * If logind support is not enabled, you must have CAP_SYS_ADMIN or be root. + * It is safe to drop priviledges after this is called. + * + * Returns NULL on error. + */ +struct wlr_session *wlr_session_start(struct wl_display *disp); + +/* + * Closes a previously opened session and restores the virtual terminal. + * You should call wlr_session_close_file on each files you opened + * with wlr_session_open_file before you call this. + */ +void wlr_session_finish(struct wlr_session *session); + +/* + * Opens the file at path. + * This can only be used to open DRM or evdev (input) devices. + * + * When the session becomes inactive: + * - DRM files lose their DRM master status + * - evdev files become invalid and should be closed + * + * Returns -errno on error. + */ +int wlr_session_open_file(struct wlr_session *session, const char *path); + +/* + * Closes a file previously opened with wlr_session_open_file. + */ +void wlr_session_close_file(struct wlr_session *session, int fd); + +/* + * Changes the virtual terminal. + */ +bool wlr_session_change_vt(struct wlr_session *session, unsigned vt); + +#endif diff --git a/include/wlr/backend/session/interface.h b/include/wlr/backend/session/interface.h new file mode 100644 index 00000000..4938110d --- /dev/null +++ b/include/wlr/backend/session/interface.h @@ -0,0 +1,14 @@ +#ifndef WLR_SESSION_INTERFACE_H +#define WLR_SESSION_INTERFACE_H + +#include + +struct session_impl { + struct wlr_session *(*start)(struct wl_display *disp); + void (*finish)(struct wlr_session *session); + int (*open)(struct wlr_session *session, const char *path); + void (*close)(struct wlr_session *session, int fd); + bool (*change_vt)(struct wlr_session *session, unsigned vt); +}; + +#endif diff --git a/include/wlr/session.h b/include/wlr/session.h deleted file mode 100644 index 7961e620..00000000 --- a/include/wlr/session.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef WLR_SESSION_H -#define WLR_SESSION_H - -#include -#include -#include - -struct session_impl; - -struct wlr_session { - const struct session_impl *impl; - /* - * Signal for when the session becomes active/inactive. - * It's called when we swap virtual terminal. - */ - struct wl_signal session_signal; - bool active; - - int drm_fd; - unsigned vtnr; - char seat[8]; -}; - -/* - * Opens a session, taking control of the current virtual terminal. - * This should not be called if another program is already in control - * of the terminal (Xorg, another Wayland compositor, etc.). - * - * If logind support is not enabled, you must have CAP_SYS_ADMIN or be root. - * It is safe to drop priviledges after this is called. - * - * Returns NULL on error. - */ -struct wlr_session *wlr_session_start(struct wl_display *disp); - -/* - * Closes a previously opened session and restores the virtual terminal. - * You should call wlr_session_close_file on each files you opened - * with wlr_session_open_file before you call this. - */ -void wlr_session_finish(struct wlr_session *session); - -/* - * Opens the file at path. - * This can only be used to open DRM or evdev (input) devices. - * - * When the session becomes inactive: - * - DRM files lose their DRM master status - * - evdev files become invalid and should be closed - * - * Returns -errno on error. - */ -int wlr_session_open_file(struct wlr_session *session, const char *path); - -/* - * Closes a file previously opened with wlr_session_open_file. - */ -void wlr_session_close_file(struct wlr_session *session, int fd); - -/* - * Changes the virtual terminal. - */ -bool wlr_session_change_vt(struct wlr_session *session, unsigned vt); - -#endif diff --git a/include/wlr/session/interface.h b/include/wlr/session/interface.h deleted file mode 100644 index 4938110d..00000000 --- a/include/wlr/session/interface.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef WLR_SESSION_INTERFACE_H -#define WLR_SESSION_INTERFACE_H - -#include - -struct session_impl { - struct wlr_session *(*start)(struct wl_display *disp); - void (*finish)(struct wlr_session *session); - int (*open)(struct wlr_session *session, const char *path); - void (*close)(struct wlr_session *session, int fd); - bool (*change_vt)(struct wlr_session *session, unsigned vt); -}; - -#endif diff --git a/session/direct-ipc.c b/session/direct-ipc.c deleted file mode 100644 index 885662e0..00000000 --- a/session/direct-ipc.c +++ /dev/null @@ -1,241 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "session/direct-ipc.h" - -enum { DRM_MAJOR = 226 }; - -#ifdef HAS_LIBCAP -#include - -static bool have_permissions(void) { - cap_t cap = cap_get_proc(); - cap_flag_value_t val; - - if (!cap || cap_get_flag(cap, CAP_SYS_ADMIN, CAP_PERMITTED, &val) || val != CAP_SET) { - wlr_log(L_ERROR, "Do not have CAP_SYS_ADMIN; cannot become DRM master"); - cap_free(cap); - return false; - } - - cap_free(cap); - return true; -} -#else -static bool have_permissions(void) { - if (geteuid() != 0) { - wlr_log(L_ERROR, "Do not have root privileges; cannot become DRM master"); - return false; - } - - return true; -} -#endif - -static void send_msg(int sock, int fd, void *buf, size_t buf_len) { - char control[CMSG_SPACE(sizeof(fd))] = {0}; - struct iovec iovec = { .iov_base = buf, .iov_len = buf_len }; - struct msghdr msghdr = {0}; - - if (buf) { - msghdr.msg_iov = &iovec; - msghdr.msg_iovlen = 1; - } - - if (fd >= 0) { - msghdr.msg_control = &control; - msghdr.msg_controllen = sizeof(control); - - struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msghdr); - *cmsg = (struct cmsghdr) { - .cmsg_level = SOL_SOCKET, - .cmsg_type = SCM_RIGHTS, - .cmsg_len = CMSG_LEN(sizeof(fd)), - }; - memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); - } - - ssize_t ret; - do { - ret = sendmsg(sock, &msghdr, 0); - } while (ret < 0 && errno == EINTR); -} - -static ssize_t recv_msg(int sock, int *fd_out, void *buf, size_t buf_len) { - char control[CMSG_SPACE(sizeof(*fd_out))] = {0}; - struct iovec iovec = { .iov_base = buf, .iov_len = buf_len }; - struct msghdr msghdr = {0}; - - if (buf) { - msghdr.msg_iov = &iovec; - msghdr.msg_iovlen = 1; - } - - if (fd_out) { - msghdr.msg_control = &control; - msghdr.msg_controllen = sizeof(control); - } - - ssize_t ret; - do { - ret = recvmsg(sock, &msghdr, MSG_CMSG_CLOEXEC); - } while (ret < 0 && errno == EINTR); - - if (fd_out) { - struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msghdr); - if (cmsg) { - memcpy(fd_out, CMSG_DATA(cmsg), sizeof(*fd_out)); - } else { - *fd_out = -1; - } - } - - return ret; -} - -enum msg_type { - MSG_OPEN, - MSG_SETMASTER, - MSG_DROPMASTER, - MSG_END, -}; - -struct msg { - enum msg_type type; - char path[256]; -}; - -static void communicate(int sock) { - struct msg msg; - int drm_fd = -1; - bool running = true; - - while (running && recv_msg(sock, &drm_fd, &msg, sizeof(msg)) >= 0) { - switch (msg.type) { - case MSG_OPEN: - errno = 0; - - // These are the same flags that logind opens files with - int fd = open(msg.path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); - int ret = errno; - if (fd == -1) { - goto error; - } - - struct stat st; - if (fstat(fd, &st) < 0) { - ret = errno; - goto error; - } - - uint32_t maj = major(st.st_rdev); - if (maj != INPUT_MAJOR && maj != DRM_MAJOR) { - ret = ENOTSUP; - goto error; - } - - if (maj == DRM_MAJOR && drmSetMaster(fd)) { - ret = errno; - } -error: - send_msg(sock, ret ? -1 : fd, &ret, sizeof(ret)); - close(fd); - - break; - - case MSG_SETMASTER: - drmSetMaster(drm_fd); - close(drm_fd); - send_msg(sock, -1, NULL, 0); - break; - - case MSG_DROPMASTER: - drmDropMaster(drm_fd); - close(drm_fd); - send_msg(sock, -1, NULL, 0); - break; - - case MSG_END: - running = false; - send_msg(sock, -1, NULL, 0); - break; - } - } - - close(sock); -} - -int direct_ipc_open(int sock, const char *path) { - struct msg msg = { .type = MSG_OPEN }; - snprintf(msg.path, sizeof(msg.path), "%s", path); - - send_msg(sock, -1, &msg, sizeof(msg)); - - int fd, err; - recv_msg(sock, &fd, &err, sizeof(err)); - - return err ? -err : fd; -} - -void direct_ipc_setmaster(int sock, int fd) { - struct msg msg = { .type = MSG_SETMASTER }; - - send_msg(sock, fd, &msg, sizeof(msg)); - recv_msg(sock, NULL, NULL, 0); -} - -void direct_ipc_dropmaster(int sock, int fd) { - struct msg msg = { .type = MSG_DROPMASTER }; - - send_msg(sock, fd, &msg, sizeof(msg)); - recv_msg(sock, NULL, NULL, 0); -} - -void direct_ipc_finish(int sock, pid_t pid) { - struct msg msg = { .type = MSG_END }; - - send_msg(sock, -1, &msg, sizeof(msg)); - recv_msg(sock, NULL, NULL, 0); - - waitpid(pid, NULL, 0); -} - -int direct_ipc_start(pid_t *pid_out) { - if (!have_permissions()) { - return -1; - } - - int sock[2]; - if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock) < 0) { - wlr_log_errno(L_ERROR, "Failed to create socket pair"); - return -1; - } - - pid_t pid = fork(); - if (pid < 0) { - wlr_log_errno(L_ERROR, "Fork failed"); - close(sock[0]); - close(sock[1]); - return -1; - } else if (pid == 0) { - close(sock[0]); - communicate(sock[1]); - _Exit(0); - } - - close(sock[1]); - *pid_out = pid; - return sock[0]; -} diff --git a/session/direct.c b/session/direct.c deleted file mode 100644 index 1247663b..00000000 --- a/session/direct.c +++ /dev/null @@ -1,244 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "session/direct-ipc.h" - -enum { DRM_MAJOR = 226 }; - -const struct session_impl session_direct; - -struct direct_session { - struct wlr_session base; - int tty_fd; - int old_kbmode; - int sock; - pid_t child; - - struct wl_event_source *vt_source; -}; - -static int direct_session_open(struct wlr_session *base, const char *path) { - struct direct_session *session = wl_container_of(base, session, base); - - int fd = direct_ipc_open(session->sock, path); - if (fd < 0) { - wlr_log(L_ERROR, "Failed to open %s: %s%s", path, strerror(-fd), - fd == -EINVAL ? "; is another display server running?" : ""); - return fd; - } - - struct stat st; - if (fstat(fd, &st) < 0) { - close(fd); - return -errno; - } - - if (major(st.st_rdev) == DRM_MAJOR) { - session->base.drm_fd = fd; - } - - return fd; -} - -static void direct_session_close(struct wlr_session *base, int fd) { - struct direct_session *session = wl_container_of(base, session, base); - - struct stat st; - if (fstat(fd, &st) < 0) { - wlr_log_errno(L_ERROR, "Stat failed"); - close(fd); - return; - } - - if (major(st.st_rdev) == DRM_MAJOR) { - direct_ipc_dropmaster(session->sock, session->base.drm_fd); - session->base.drm_fd = -1; - } else if (major(st.st_rdev) == INPUT_MAJOR) { - ioctl(fd, EVIOCREVOKE, 0); - } - - close(fd); -} - -static bool direct_change_vt(struct wlr_session *base, unsigned vt) { - struct direct_session *session = wl_container_of(base, session, base); - return ioctl(session->tty_fd, VT_ACTIVATE, (int)vt) == 0; -} - -static void direct_session_finish(struct wlr_session *base) { - struct direct_session *session = wl_container_of(base, session, base); - struct vt_mode mode = { - .mode = VT_AUTO, - }; - - errno = 0; - - ioctl(session->tty_fd, KDSKBMODE, session->old_kbmode); - ioctl(session->tty_fd, KDSETMODE, KD_TEXT); - ioctl(session->tty_fd, VT_SETMODE, &mode); - - if (errno) { - wlr_log(L_ERROR, "Failed to restore tty"); - } - - direct_ipc_finish(session->sock, session->child); - close(session->sock); - - wl_event_source_remove(session->vt_source); - close(session->tty_fd); - free(session); -} - -static int vt_handler(int signo, void *data) { - struct direct_session *session = data; - - if (session->base.active) { - session->base.active = false; - wl_signal_emit(&session->base.session_signal, session); - direct_ipc_dropmaster(session->sock, session->base.drm_fd); - ioctl(session->tty_fd, VT_RELDISP, 1); - } else { - ioctl(session->tty_fd, VT_RELDISP, VT_ACKACQ); - direct_ipc_setmaster(session->sock, session->base.drm_fd); - session->base.active = true; - wl_signal_emit(&session->base.session_signal, session); - } - - return 1; -} - -static bool setup_tty(struct direct_session *session, struct wl_display *display) { - int fd = dup(STDIN_FILENO); - if (fd == -1) { - wlr_log_errno(L_ERROR, "Cannot open tty"); - return false; - } - - struct stat st; - if (fstat(fd, &st) == -1 || major(st.st_rdev) != TTY_MAJOR || minor(st.st_rdev) == 0) { - wlr_log(L_ERROR, "Not running from a virtual terminal"); - goto error; - } - - int tty = minor(st.st_rdev); - int ret, kd_mode, old_kbmode; - - ret = ioctl(fd, KDGETMODE, &kd_mode); - if (ret) { - wlr_log_errno(L_ERROR, "Failed to get tty mode"); - goto error; - } - - if (kd_mode != KD_TEXT) { - wlr_log(L_ERROR, - "tty already in graphics mode; is another display server running?"); - goto error; - } - - ioctl(fd, VT_ACTIVATE, tty); - ioctl(fd, VT_WAITACTIVE, tty); - - if (ioctl(fd, KDGKBMODE, &old_kbmode)) { - wlr_log_errno(L_ERROR, "Failed to read keyboard mode"); - goto error; - } - - if (ioctl(fd, KDSKBMODE, K_OFF)) { - wlr_log_errno(L_ERROR, "Failed to set keyboard mode"); - goto error; - } - - if (ioctl(fd, KDSETMODE, KD_GRAPHICS)) { - wlr_log_errno(L_ERROR, "Failed to set graphics mode on tty"); - goto error; - } - - struct vt_mode mode = { - .mode = VT_PROCESS, - .relsig = SIGUSR1, - .acqsig = SIGUSR1, - }; - - if (ioctl(fd, VT_SETMODE, &mode) < 0) { - wlr_log(L_ERROR, "Failed to take control of tty"); - goto error; - } - - struct wl_event_loop *loop = wl_display_get_event_loop(display); - session->vt_source = wl_event_loop_add_signal(loop, SIGUSR1, - vt_handler, session); - if (!session->vt_source) { - goto error; - } - - session->base.vtnr = tty; - session->tty_fd = fd; - session->old_kbmode = old_kbmode; - - return true; - -error: - close(fd); - return false; -} - -static struct wlr_session *direct_session_start(struct wl_display *disp) { - struct direct_session *session = calloc(1, sizeof(*session)); - if (!session) { - wlr_log_errno(L_ERROR, "Allocation failed"); - return NULL; - } - - session->sock = direct_ipc_start(&session->child); - if (session->sock == -1) { - goto error_session; - } - - if (!setup_tty(session, disp)) { - goto error_ipc; - } - - // XXX: Is it okay to trust the environment like this? - const char *seat = getenv("XDG_SEAT"); - if (!seat) { - seat = "seat0"; - } - - wlr_log(L_INFO, "Successfully loaded direct session"); - - snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat); - session->base.drm_fd = -1; - session->base.impl = &session_direct; - session->base.active = true; - wl_signal_init(&session->base.session_signal); - return &session->base; - -error_ipc: - direct_ipc_finish(session->sock, session->child); - close(session->sock); -error_session: - free(session); - return NULL; -} - -const struct session_impl session_direct = { - .start = direct_session_start, - .finish = direct_session_finish, - .open = direct_session_open, - .close = direct_session_close, - .change_vt = direct_change_vt, -}; diff --git a/session/logind.c b/session/logind.c deleted file mode 100644 index 6744215a..00000000 --- a/session/logind.c +++ /dev/null @@ -1,398 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -enum { DRM_MAJOR = 226 }; - -const struct session_impl session_logind; - -struct logind_session { - struct wlr_session base; - - sd_bus *bus; - struct wl_event_source *event; - - char *id; - char *path; -}; - -static int logind_take_device(struct wlr_session *base, const char *path) { - struct logind_session *session = wl_container_of(base, session, base); - - int ret; - int fd = -1; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - struct stat st; - if (stat(path, &st) < 0) { - wlr_log(L_ERROR, "Failed to stat '%s'", path); - return -1; - } - - ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", - session->path, "org.freedesktop.login1.Session", "TakeDevice", - &error, &msg, "uu", major(st.st_rdev), minor(st.st_rdev)); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to take device '%s': %s", path, error.message); - goto error; - } - - int paused = 0; - ret = sd_bus_message_read(msg, "hb", &fd, &paused); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to parse D-Bus response for '%s': %s", - path, strerror(-ret)); - goto error; - } - - // The original fd seems to be closed when the message is freed - // so we just clone it. - fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); - if (fd == -1) { - wlr_log(L_ERROR, "Failed to clone file descriptor for '%s': %s", - path, strerror(errno)); - goto error; - } - - if (major(st.st_rdev) == DRM_MAJOR) { - session->base.drm_fd = fd; - } - -error: - sd_bus_error_free(&error); - sd_bus_message_unref(msg); - return fd; -} - -static void logind_release_device(struct wlr_session *base, int fd) { - struct logind_session *session = wl_container_of(base, session, base); - - int ret; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - struct stat st; - if (fstat(fd, &st) < 0) { - wlr_log(L_ERROR, "Failed to stat device '%d'", fd); - return; - } - - ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", - session->path, "org.freedesktop.login1.Session", "ReleaseDevice", - &error, &msg, "uu", major(st.st_rdev), minor(st.st_rdev)); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to release device '%d'", fd); - } - - if (major(st.st_rdev) == DRM_MAJOR) { - session->base.drm_fd = -1; - } - - sd_bus_error_free(&error); - sd_bus_message_unref(msg); -} - -static bool logind_change_vt(struct wlr_session *base, unsigned vt) { - struct logind_session *session = wl_container_of(base, session, base); - - int ret; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", - "/org/freedesktop/login1/seat/self", "org.freedesktop.login1.Seat", "SwitchTo", - &error, &msg, "u", (uint32_t)vt); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to change to vt '%d'", vt); - } - - sd_bus_error_free(&error); - sd_bus_message_unref(msg); - return ret >= 0; -} - -static bool find_sesion_path(struct logind_session *session) { - int ret; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", - "/org/freedesktop/login1", "org.freedesktop.login1.Manager", - "GetSession", &error, &msg, "s", session->id); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to get session path: %s", strerror(-ret)); - goto out; - } - - const char *path; - - ret = sd_bus_message_read(msg, "o", &path); - if (ret < 0) { - wlr_log(L_ERROR, "Could not parse session path: %s", strerror(-ret)); - goto out; - } - - session->path = strdup(path); - -out: - sd_bus_error_free(&error); - sd_bus_message_unref(msg); - - return ret >= 0; -} - -static bool session_activate(struct logind_session *session) { - int ret; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", - session->path, "org.freedesktop.login1.Session", "Activate", - &error, &msg, ""); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to activate session"); - } - - sd_bus_error_free(&error); - sd_bus_message_unref(msg); - return ret >= 0; -} - -static bool take_control(struct logind_session *session) { - int ret; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", - session->path, "org.freedesktop.login1.Session", "TakeControl", - &error, &msg, "b", false); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to take control of session"); - } - - sd_bus_error_free(&error); - sd_bus_message_unref(msg); - return ret >= 0; -} - -static void release_control(struct logind_session *session) { - int ret; - sd_bus_message *msg = NULL; - sd_bus_error error = SD_BUS_ERROR_NULL; - - ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", - session->path, "org.freedesktop.login1.Session", "ReleaseControl", - &error, &msg, ""); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to release control of session"); - } - - sd_bus_error_free(&error); - sd_bus_message_unref(msg); -} - -static void logind_session_finish(struct wlr_session *base) { - struct logind_session *session = wl_container_of(base, session, base); - - release_control(session); - - wl_event_source_remove(session->event); - sd_bus_unref(session->bus); - free(session->id); - free(session->path); - free(session); -} - -static int session_removed(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { - wlr_log(L_INFO, "SessionRemoved signal received"); - return 0; -} - -static int pause_device(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { - struct logind_session *session = userdata; - int ret; - - uint32_t major, minor; - const char *type; - ret = sd_bus_message_read(msg, "uus", &major, &minor, &type); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to parse D-Bus response for PauseDevice: %s", - strerror(-ret)); - goto error; - } - - if (major == DRM_MAJOR) { - session->base.active = false; - wl_signal_emit(&session->base.session_signal, session); - } - - if (strcmp(type, "pause") == 0) { - ret = sd_bus_call_method(session->bus, "org.freedesktop.login1", - session->path, "org.freedesktop.login1.Session", "PauseDeviceComplete", - ret_error, &msg, "uu", major, minor); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to send PauseDeviceComplete signal: %s", - strerror(-ret)); - } - } - -error: - return 0; -} - -static int resume_device(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) { - struct logind_session *session = userdata; - int ret; - - int fd; - uint32_t major, minor; - ret = sd_bus_message_read(msg, "uuh", &major, &minor, &fd); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to parse D-Bus response for ResumeDevice: %s", - strerror(-ret)); - goto error; - } - - if (major == DRM_MAJOR) { - dup2(fd, session->base.drm_fd); - session->base.active = true; - wl_signal_emit(&session->base.session_signal, session); - } - -error: - return 0; -} - -static bool add_signal_matches(struct logind_session *session) { - int ret; - - 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", "SesssionRemoved", "/org/freedesktop/login1"); - ret = sd_bus_add_match(session->bus, NULL, str, session_removed, session); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret)); - return false; - } - - snprintf(str, sizeof(str), fmt, "Session", "PauseDevice", session->path); - ret = sd_bus_add_match(session->bus, NULL, str, pause_device, session); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret)); - return false; - } - - snprintf(str, sizeof(str), fmt, "Session", "ResumeDevice", session->path); - ret = sd_bus_add_match(session->bus, NULL, str, resume_device, session); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to add D-Bus match: %s", strerror(-ret)); - return false; - } - - return true; -} - -static int dbus_event(int fd, uint32_t mask, void *data) { - sd_bus *bus = data; - while (sd_bus_process(bus, NULL) > 0); - return 1; -} - -static struct wlr_session *logind_session_start(struct wl_display *disp) { - int ret; - struct logind_session *session = calloc(1, sizeof(*session)); - if (!session) { - wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); - return NULL; - } - - ret = sd_pid_get_session(getpid(), &session->id); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to get session id: %s", strerror(-ret)); - goto error; - } - - ret = sd_session_get_vt(session->id, &session->base.vtnr); - if (ret < 0) { - wlr_log(L_ERROR, "Session not running in virtual terminal"); - goto error; - } - - char *seat; - ret = sd_session_get_seat(session->id, &seat); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to get seat id: %s", strerror(-ret)); - goto error; - } - snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat); - free(seat); - - ret = sd_bus_default_system(&session->bus); - if (ret < 0) { - wlr_log(L_ERROR, "Failed to open D-Bus connection: %s", strerror(-ret)); - goto error; - } - - if (!find_sesion_path(session)) { - sd_bus_unref(session->bus); - goto error; - } - - if (!add_signal_matches(session)) { - goto error_bus; - } - - struct wl_event_loop *event_loop = wl_display_get_event_loop(disp); - session->event = wl_event_loop_add_fd(event_loop, sd_bus_get_fd(session->bus), - WL_EVENT_READABLE, dbus_event, session->bus); - - if (!session_activate(session)) { - goto error_bus; - } - - if (!take_control(session)) { - goto error_bus; - } - - wlr_log(L_INFO, "Successfully loaded logind session"); - - session->base.drm_fd = -1; - session->base.impl = &session_logind; - session->base.active = true; - wl_signal_init(&session->base.session_signal); - return &session->base; - -error_bus: - sd_bus_unref(session->bus); - free(session->path); - -error: - free(session->id); - return NULL; -} - -const struct session_impl session_logind = { - .start = logind_session_start, - .finish = logind_session_finish, - .open = logind_take_device, - .close = logind_release_device, - .change_vt = logind_change_vt, -}; diff --git a/session/session.c b/session/session.c deleted file mode 100644 index 3f4c7910..00000000 --- a/session/session.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include -#include -#include - -extern const struct session_impl session_logind; -extern const struct session_impl session_direct; - -static const struct session_impl *impls[] = { -#ifdef HAS_SYSTEMD - &session_logind, -#endif - &session_direct, - NULL, -}; - -struct wlr_session *wlr_session_start(struct wl_display *disp) { - const struct session_impl **iter; - - for (iter = impls; *iter; ++iter) { - struct wlr_session *session = (*iter)->start(disp); - if (session) { - return session; - } - } - - wlr_log(L_ERROR, "Failed to load session backend"); - return NULL; -} - -void wlr_session_finish(struct wlr_session *session) { - if (!session) { - return; - } - - session->impl->finish(session); -}; - -int wlr_session_open_file(struct wlr_session *session, const char *path) { - return session->impl->open(session, path); -} - -void wlr_session_close_file(struct wlr_session *session, int fd) { - session->impl->close(session, fd); -} - -bool wlr_session_change_vt(struct wlr_session *session, unsigned vt) { - if (!session) { - return false; - } - - return session->impl->change_vt(session, vt); -} -- cgit v1.2.3 From cb4d50e22c217027d641fc36cd022d12bba8d1ad Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Tue, 11 Jul 2017 19:18:34 +1200 Subject: Changed header paths. --- backend/backend.c | 2 +- backend/drm/backend.c | 2 +- backend/libinput/backend.c | 2 +- backend/libinput/events.c | 2 +- backend/libinput/keyboard.c | 2 +- backend/libinput/pointer.c | 2 +- backend/libinput/tablet_pad.c | 2 +- backend/libinput/tablet_tool.c | 2 +- backend/libinput/touch.c | 2 +- backend/multi/backend.c | 2 +- backend/session/direct-ipc.c | 2 +- backend/session/direct.c | 4 ++-- backend/session/logind.c | 2 +- backend/session/session.c | 4 ++-- backend/udev.c | 2 +- examples/compositor/main.c | 2 +- examples/pointer.c | 2 +- examples/rotation.c | 2 +- examples/shared.c | 2 +- examples/shared.h | 2 +- examples/simple.c | 2 +- examples/tablet.c | 2 +- examples/touch.c | 2 +- include/backend/drm.h | 2 +- include/backend/multi.h | 2 +- include/backend/udev.h | 2 +- include/wlr/backend.h | 2 +- include/wlr/backend/drm.h | 2 +- include/wlr/backend/libinput.h | 2 +- include/wlr/backend/multi.h | 2 +- include/wlr/backend/session/interface.h | 2 +- 31 files changed, 33 insertions(+), 33 deletions(-) (limited to 'include/wlr') diff --git a/backend/backend.c b/backend/backend.c index 93f764f6..b47b7f13 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/backend/drm/backend.c b/backend/drm/backend.c index d52adba9..a2c71317 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index a3523dc0..68f1752b 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include "backend/udev.h" diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 6cd33e3d..9fe81782 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c index f19d7120..957cc3f2 100644 --- a/backend/libinput/keyboard.c +++ b/backend/libinput/keyboard.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/backend/libinput/pointer.c b/backend/libinput/pointer.c index 57c417c8..59e219d8 100644 --- a/backend/libinput/pointer.c +++ b/backend/libinput/pointer.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/backend/libinput/tablet_pad.c b/backend/libinput/tablet_pad.c index 5517fa63..9bf45ba6 100644 --- a/backend/libinput/tablet_pad.c +++ b/backend/libinput/tablet_pad.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/backend/libinput/tablet_tool.c b/backend/libinput/tablet_tool.c index 95bdd3fd..9bbb0e34 100644 --- a/backend/libinput/tablet_tool.c +++ b/backend/libinput/tablet_tool.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/backend/libinput/touch.c b/backend/libinput/touch.c index c002b6a0..e0e866b0 100644 --- a/backend/libinput/touch.c +++ b/backend/libinput/touch.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/backend/multi/backend.c b/backend/multi/backend.c index 2e8f117c..8026e970 100644 --- a/backend/multi/backend.c +++ b/backend/multi/backend.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include "backend/multi.h" diff --git a/backend/session/direct-ipc.c b/backend/session/direct-ipc.c index 885662e0..71e22148 100644 --- a/backend/session/direct-ipc.c +++ b/backend/session/direct-ipc.c @@ -13,7 +13,7 @@ #include #include #include -#include "session/direct-ipc.h" +#include "backend/session/direct-ipc.h" enum { DRM_MAJOR = 226 }; diff --git a/backend/session/direct.c b/backend/session/direct.c index 1247663b..82aa8e00 100644 --- a/backend/session/direct.c +++ b/backend/session/direct.c @@ -13,9 +13,9 @@ #include #include #include -#include +#include #include -#include "session/direct-ipc.h" +#include "backend/session/direct-ipc.h" enum { DRM_MAJOR = 226 }; diff --git a/backend/session/logind.c b/backend/session/logind.c index 6744215a..20d9b5ed 100644 --- a/backend/session/logind.c +++ b/backend/session/logind.c @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include enum { DRM_MAJOR = 226 }; diff --git a/backend/session/session.c b/backend/session/session.c index 3f4c7910..a07d3cda 100644 --- a/backend/session/session.c +++ b/backend/session/session.c @@ -1,7 +1,7 @@ #include #include -#include -#include +#include +#include #include extern const struct session_impl session_logind; diff --git a/backend/udev.c b/backend/udev.c index 1fecaee4..1cc53f60 100644 --- a/backend/udev.c +++ b/backend/udev.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include "backend/udev.h" diff --git a/examples/compositor/main.c b/examples/compositor/main.c index e13fda24..c618961d 100644 --- a/examples/compositor/main.c +++ b/examples/compositor/main.c @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/examples/pointer.c b/examples/pointer.c index c7518841..d159caf5 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include "shared.h" diff --git a/examples/rotation.c b/examples/rotation.c index 36e95f50..bcf855d9 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include "shared.h" diff --git a/examples/shared.c b/examples/shared.c index 823d8e0c..bd5f7ec5 100644 --- a/examples/shared.c +++ b/examples/shared.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/examples/shared.h b/examples/shared.h index 1633f2c8..2379eef9 100644 --- a/examples/shared.h +++ b/examples/shared.h @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include diff --git a/examples/simple.c b/examples/simple.c index 45cb2b93..8e098e18 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include "shared.h" diff --git a/examples/tablet.c b/examples/tablet.c index d7f7d042..0be6dcb9 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/examples/touch.c b/examples/touch.c index 97009e81..15ad9d49 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include "shared.h" #include "cat.h" diff --git a/include/backend/drm.h b/include/backend/drm.h index dc12c258..ecdd945b 100644 --- a/include/backend/drm.h +++ b/include/backend/drm.h @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include diff --git a/include/backend/multi.h b/include/backend/multi.h index 617d4c98..5ac9cc1d 100644 --- a/include/backend/multi.h +++ b/include/backend/multi.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include struct wlr_backend_state { struct wlr_backend *backend; diff --git a/include/backend/udev.h b/include/backend/udev.h index a2834063..080422c0 100644 --- a/include/backend/udev.h +++ b/include/backend/udev.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include diff --git a/include/wlr/backend.h b/include/wlr/backend.h index 74cb34a9..3910e406 100644 --- a/include/wlr/backend.h +++ b/include/wlr/backend.h @@ -2,7 +2,7 @@ #define _WLR_BACKEND_H #include -#include +#include struct wlr_backend_impl; struct wlr_backend_state; diff --git a/include/wlr/backend/drm.h b/include/wlr/backend/drm.h index a486757d..7332d608 100644 --- a/include/wlr/backend/drm.h +++ b/include/wlr/backend/drm.h @@ -2,7 +2,7 @@ #define WLR_BACKEND_DRM_H #include -#include +#include #include #include diff --git a/include/wlr/backend/libinput.h b/include/wlr/backend/libinput.h index 29748b77..bba68888 100644 --- a/include/wlr/backend/libinput.h +++ b/include/wlr/backend/libinput.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include diff --git a/include/wlr/backend/multi.h b/include/wlr/backend/multi.h index 00b34722..8b68284d 100644 --- a/include/wlr/backend/multi.h +++ b/include/wlr/backend/multi.h @@ -3,7 +3,7 @@ #include #include -#include +#include struct wlr_backend *wlr_multi_backend_create(struct wlr_session *session, struct wlr_udev *udev); diff --git a/include/wlr/backend/session/interface.h b/include/wlr/backend/session/interface.h index 4938110d..16814446 100644 --- a/include/wlr/backend/session/interface.h +++ b/include/wlr/backend/session/interface.h @@ -1,7 +1,7 @@ #ifndef WLR_SESSION_INTERFACE_H #define WLR_SESSION_INTERFACE_H -#include +#include struct session_impl { struct wlr_session *(*start)(struct wl_display *disp); -- cgit v1.2.3 From 4d4da18437849df850b4ae199fbffb54d31c86b4 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 6 Aug 2017 13:37:49 +1200 Subject: Fixed VT switching --- backend/multi/backend.c | 8 ++++++++ examples/shared.c | 11 +++++++++-- include/wlr/backend/multi.h | 2 ++ 3 files changed, 19 insertions(+), 2 deletions(-) (limited to 'include/wlr') diff --git a/backend/multi/backend.c b/backend/multi/backend.c index 8026e970..a5913f5d 100644 --- a/backend/multi/backend.c +++ b/backend/multi/backend.c @@ -113,3 +113,11 @@ void wlr_multi_backend_add(struct wlr_backend *multi, list_add(multi->state->backends, sub); } + +struct wlr_session *wlr_multi_get_session(struct wlr_backend *base) { + if (base->impl != &backend_impl) + return NULL; + + struct wlr_backend_state *multi = base->state; + return multi->session; +} diff --git a/examples/shared.c b/examples/shared.c index bd5f7ec5..27e5b9ca 100644 --- a/examples/shared.c +++ b/examples/shared.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -44,8 +45,14 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) { } if (sym == XKB_KEY_Escape) { wl_display_terminate(kbstate->compositor->display); - } else if (key_state == WLR_KEY_PRESSED && sym >= XKB_KEY_F1 && sym <= XKB_KEY_F12) { - wlr_session_change_vt(kbstate->compositor->session, sym - XKB_KEY_F1 + 1); + } else if (key_state == WLR_KEY_PRESSED && + sym >= XKB_KEY_XF86Switch_VT_1 && + sym <= XKB_KEY_XF86Switch_VT_12) { + struct wlr_session *session = + wlr_multi_get_session(kbstate->compositor->backend); + if (session) { + wlr_session_change_vt(session, sym - XKB_KEY_XF86Switch_VT_1 + 1); + } } } xkb_state_update_key(kbstate->xkb_state, keycode, diff --git a/include/wlr/backend/multi.h b/include/wlr/backend/multi.h index 8b68284d..048c4043 100644 --- a/include/wlr/backend/multi.h +++ b/include/wlr/backend/multi.h @@ -10,4 +10,6 @@ struct wlr_backend *wlr_multi_backend_create(struct wlr_session *session, void wlr_multi_backend_add(struct wlr_backend *multi, struct wlr_backend *backend); +struct wlr_session *wlr_multi_get_session(struct wlr_backend *base); + #endif -- cgit v1.2.3 From 5bf61ca7edb658737f3ccbaed0a99f4779d329bb Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 5 Aug 2017 23:08:35 -0400 Subject: Check for multi backend before using it --- backend/multi/backend.c | 4 ++++ examples/shared.c | 10 ++++++---- include/wlr/backend/multi.h | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) (limited to 'include/wlr') diff --git a/backend/multi/backend.c b/backend/multi/backend.c index a5913f5d..19839629 100644 --- a/backend/multi/backend.c +++ b/backend/multi/backend.c @@ -66,6 +66,10 @@ struct wlr_backend *wlr_multi_backend_create(struct wlr_session *session, return backend; } +bool wlr_backend_is_multi(struct wlr_backend *b) { + return b->impl == &backend_impl; +} + static void input_add_reemit(struct wl_listener *listener, void *data) { struct subbackend_state *state = wl_container_of(listener, state, input_add); diff --git a/examples/shared.c b/examples/shared.c index 27e5b9ca..c66b8a23 100644 --- a/examples/shared.c +++ b/examples/shared.c @@ -48,10 +48,12 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) { } else if (key_state == WLR_KEY_PRESSED && sym >= XKB_KEY_XF86Switch_VT_1 && sym <= XKB_KEY_XF86Switch_VT_12) { - struct wlr_session *session = - wlr_multi_get_session(kbstate->compositor->backend); - if (session) { - wlr_session_change_vt(session, sym - XKB_KEY_XF86Switch_VT_1 + 1); + if (wlr_backend_is_multi(kbstate->compositor->backend)) { + struct wlr_session *session = + wlr_multi_get_session(kbstate->compositor->backend); + if (session) { + wlr_session_change_vt(session, sym - XKB_KEY_XF86Switch_VT_1 + 1); + } } } } diff --git a/include/wlr/backend/multi.h b/include/wlr/backend/multi.h index 048c4043..e8e46bed 100644 --- a/include/wlr/backend/multi.h +++ b/include/wlr/backend/multi.h @@ -10,6 +10,8 @@ struct wlr_backend *wlr_multi_backend_create(struct wlr_session *session, void wlr_multi_backend_add(struct wlr_backend *multi, struct wlr_backend *backend); +bool wlr_backend_is_multi(struct wlr_backend *backend); + struct wlr_session *wlr_multi_get_session(struct wlr_backend *base); #endif -- cgit v1.2.3