wlroots-hyprland/backend/session/direct.c

289 lines
6.4 KiB
C
Raw Normal View History

2017-05-03 09:26:43 +02:00
#define _POSIX_C_SOURCE 200809L
2018-09-17 22:01:35 +02:00
#include <assert.h>
2017-05-03 09:26:43 +02:00
#include <errno.h>
2018-06-14 10:46:16 +02:00
#include <fcntl.h>
2018-02-12 21:29:23 +01:00
#include <linux/input.h>
#include <linux/kd.h>
#include <linux/major.h>
#include <linux/vt.h>
2017-07-03 04:46:20 +02:00
#include <signal.h>
2018-02-12 21:29:23 +01:00
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
2017-07-03 04:46:20 +02:00
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
2018-02-12 21:29:23 +01:00
#include <unistd.h>
2017-05-03 09:26:43 +02:00
#include <wayland-server.h>
2017-07-11 09:18:34 +02:00
#include <wlr/backend/session/interface.h>
2017-06-21 18:10:07 +02:00
#include <wlr/util/log.h>
2017-07-11 09:18:34 +02:00
#include "backend/session/direct-ipc.h"
2018-02-12 21:29:23 +01:00
#include "util/signal.h"
2017-05-03 09:26:43 +02:00
2017-07-03 04:46:20 +02:00
enum { DRM_MAJOR = 226 };
2017-06-05 01:30:37 +02:00
const struct session_impl session_direct;
2017-05-03 09:26:43 +02:00
struct direct_session {
struct wlr_session base;
2017-07-03 04:46:20 +02:00
int tty_fd;
2017-07-09 07:53:13 +02:00
int old_kbmode;
2017-07-03 14:15:09 +02:00
int sock;
pid_t child;
2017-07-03 04:46:20 +02:00
struct wl_event_source *vt_source;
2017-05-03 09:26:43 +02:00
};
2018-09-17 22:01:35 +02:00
static struct direct_session *direct_session_from_session(
struct wlr_session *base) {
assert(base->impl == &session_direct);
return (struct direct_session *)base;
}
2017-07-09 07:53:13 +02:00
static int direct_session_open(struct wlr_session *base, const char *path) {
2018-09-17 22:01:35 +02:00
struct direct_session *session = direct_session_from_session(base);
2017-07-03 09:56:14 +02:00
2017-07-09 12:12:50 +02:00
int fd = direct_ipc_open(session->sock, path);
2017-07-03 14:15:09 +02:00
if (fd < 0) {
2018-07-09 23:49:54 +02:00
wlr_log(WLR_ERROR, "Failed to open %s: %s%s", path, strerror(-fd),
2017-07-03 14:15:09 +02:00
fd == -EINVAL ? "; is another display server running?" : "");
return fd;
}
2017-07-09 12:12:50 +02:00
struct stat st;
if (fstat(fd, &st) < 0) {
close(fd);
return -errno;
}
if (major(st.st_rdev) == DRM_MAJOR) {
direct_ipc_setmaster(session->sock, fd);
2017-07-03 04:46:20 +02:00
}
return fd;
2017-05-03 09:26:43 +02:00
}
static void direct_session_close(struct wlr_session *base, int fd) {
2018-09-17 22:01:35 +02:00
struct direct_session *session = direct_session_from_session(base);
2017-07-03 04:46:20 +02:00
2017-07-09 12:12:50 +02:00
struct stat st;
if (fstat(fd, &st) < 0) {
2018-07-09 23:49:54 +02:00
wlr_log_errno(WLR_ERROR, "Stat failed");
2017-07-09 12:12:50 +02:00
close(fd);
return;
}
if (major(st.st_rdev) == DRM_MAJOR) {
direct_ipc_dropmaster(session->sock, fd);
2017-07-09 12:12:50 +02:00
} else if (major(st.st_rdev) == INPUT_MAJOR) {
ioctl(fd, EVIOCREVOKE, 0);
2017-07-03 04:46:20 +02:00
}
2017-05-03 09:26:43 +02:00
close(fd);
}
2017-07-09 07:53:13 +02:00
static bool direct_change_vt(struct wlr_session *base, unsigned vt) {
2018-09-17 22:01:35 +02:00
struct direct_session *session = direct_session_from_session(base);
2018-06-14 10:46:16 +02:00
// Only seat0 has VTs associated with it
2018-06-14 11:02:32 +02:00
if (strcmp(session->base.seat, "seat0") != 0) {
2018-06-14 10:46:16 +02:00
return true;
}
2017-07-09 07:53:13 +02:00
return ioctl(session->tty_fd, VT_ACTIVATE, (int)vt) == 0;
2017-05-13 13:56:40 +02:00
}
static void direct_session_destroy(struct wlr_session *base) {
2018-09-17 22:01:35 +02:00
struct direct_session *session = direct_session_from_session(base);
2017-05-03 09:26:43 +02:00
2018-06-14 10:46:16 +02:00
if (strcmp(session->base.seat, "seat0") == 0) {
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);
2017-07-09 07:53:13 +02:00
2018-06-14 10:46:16 +02:00
if (errno) {
2018-07-09 23:49:54 +02:00
wlr_log(WLR_ERROR, "Failed to restore tty");
2018-06-14 10:46:16 +02:00
}
2017-07-03 04:46:20 +02:00
2018-06-14 10:46:16 +02:00
wl_event_source_remove(session->vt_source);
close(session->tty_fd);
2017-07-09 07:53:13 +02:00
}
2017-07-09 12:12:50 +02:00
direct_ipc_finish(session->sock, session->child);
2017-07-03 14:15:09 +02:00
close(session->sock);
2017-05-03 09:26:43 +02:00
free(session);
}
2017-07-03 04:46:20 +02:00
static int vt_handler(int signo, void *data) {
struct direct_session *session = data;
if (session->base.active) {
session->base.active = false;
2018-02-12 09:12:31 +01:00
wlr_signal_emit_safe(&session->base.session_signal, session);
struct wlr_device *dev;
wl_list_for_each(dev, &session->base.devices, link) {
if (major(dev->dev) == DRM_MAJOR) {
direct_ipc_dropmaster(session->sock,
dev->fd);
}
}
2017-07-03 04:46:20 +02:00
ioctl(session->tty_fd, VT_RELDISP, 1);
} else {
ioctl(session->tty_fd, VT_RELDISP, VT_ACKACQ);
struct wlr_device *dev;
wl_list_for_each(dev, &session->base.devices, link) {
if (major(dev->dev) == DRM_MAJOR) {
direct_ipc_setmaster(session->sock,
dev->fd);
}
}
2017-07-03 04:46:20 +02:00
session->base.active = true;
2018-02-12 09:12:31 +01:00
wlr_signal_emit_safe(&session->base.session_signal, session);
2017-07-03 04:46:20 +02:00
}
return 1;
}
static bool setup_tty(struct direct_session *session, struct wl_display *display) {
bool default_tty = false;
const char *tty_path = getenv("WLR_DIRECT_TTY");
if (!tty_path) {
tty_path = "/dev/tty";
default_tty = true;
}
int fd = open(tty_path, O_RDWR | O_CLOEXEC);
2017-07-09 12:12:50 +02:00
if (fd == -1) {
wlr_log_errno(WLR_ERROR, "Cannot open %s", tty_path);
2017-07-09 07:53:13 +02:00
return false;
}
2017-07-03 04:46:20 +02:00
2018-06-14 10:46:16 +02:00
struct vt_stat vt_stat;
if (ioctl(fd, VT_GETSTATE, &vt_stat)) {
2018-07-09 23:49:54 +02:00
wlr_log_errno(WLR_ERROR, "Could not get current tty number");
2017-07-03 04:46:20 +02:00
goto error;
}
2018-06-14 10:46:16 +02:00
int tty = vt_stat.v_active;
2017-07-09 12:12:50 +02:00
int ret, kd_mode, old_kbmode;
2017-07-03 04:46:20 +02:00
2017-07-09 12:12:50 +02:00
ret = ioctl(fd, KDGETMODE, &kd_mode);
2017-07-03 04:46:20 +02:00
if (ret) {
2018-07-09 23:49:54 +02:00
wlr_log_errno(WLR_ERROR, "Failed to get tty mode");
2017-07-03 04:46:20 +02:00
goto error;
}
if (default_tty && kd_mode != KD_TEXT) {
2018-07-09 23:49:54 +02:00
wlr_log(WLR_ERROR,
2017-07-03 04:46:20 +02:00
"tty already in graphics mode; is another display server running?");
goto error;
}
2017-07-09 12:12:50 +02:00
ioctl(fd, VT_ACTIVATE, tty);
ioctl(fd, VT_WAITACTIVE, tty);
2017-07-03 04:46:20 +02:00
2017-07-09 12:12:50 +02:00
if (ioctl(fd, KDGKBMODE, &old_kbmode)) {
2018-07-09 23:49:54 +02:00
wlr_log_errno(WLR_ERROR, "Failed to read keyboard mode");
2017-07-03 04:46:20 +02:00
goto error;
}
2017-07-09 12:12:50 +02:00
if (ioctl(fd, KDSKBMODE, K_OFF)) {
2018-07-09 23:49:54 +02:00
wlr_log_errno(WLR_ERROR, "Failed to set keyboard mode");
2017-07-03 04:46:20 +02:00
goto error;
}
2017-07-09 12:12:50 +02:00
if (ioctl(fd, KDSETMODE, KD_GRAPHICS)) {
2018-07-09 23:49:54 +02:00
wlr_log_errno(WLR_ERROR, "Failed to set graphics mode on tty");
2017-07-03 04:46:20 +02:00
goto error;
}
struct vt_mode mode = {
.mode = VT_PROCESS,
.relsig = SIGUSR2,
.acqsig = SIGUSR2,
2017-07-03 04:46:20 +02:00
};
2017-07-09 12:12:50 +02:00
if (ioctl(fd, VT_SETMODE, &mode) < 0) {
2018-07-09 23:49:54 +02:00
wlr_log(WLR_ERROR, "Failed to take control of tty");
2017-07-03 04:46:20 +02:00
goto error;
}
struct wl_event_loop *loop = wl_display_get_event_loop(display);
session->vt_source = wl_event_loop_add_signal(loop, SIGUSR2,
2017-07-03 04:46:20 +02:00
vt_handler, session);
if (!session->vt_source) {
goto error;
}
2017-07-09 12:12:50 +02:00
session->base.vtnr = tty;
session->tty_fd = fd;
session->old_kbmode = old_kbmode;
2017-07-03 04:46:20 +02:00
return true;
error:
2017-07-09 12:12:50 +02:00
close(fd);
2017-07-03 04:46:20 +02:00
return false;
}
static struct wlr_session *direct_session_create(struct wl_display *disp) {
2017-07-03 09:56:14 +02:00
struct direct_session *session = calloc(1, sizeof(*session));
if (!session) {
2018-07-09 23:49:54 +02:00
wlr_log_errno(WLR_ERROR, "Allocation failed");
2017-07-09 12:12:50 +02:00
return NULL;
2017-07-03 09:56:14 +02:00
}
session->sock = direct_ipc_init(&session->child);
2017-07-09 12:12:50 +02:00
if (session->sock == -1) {
goto error_session;
}
2017-07-03 14:15:09 +02:00
2017-07-09 07:53:13 +02:00
const char *seat = getenv("XDG_SEAT");
if (!seat) {
seat = "seat0";
}
2018-06-14 10:46:16 +02:00
if (strcmp(seat, "seat0") == 0) {
if (!setup_tty(session, disp)) {
goto error_ipc;
}
} else {
session->base.vtnr = 0;
session->tty_fd = -1;
}
2017-05-03 09:26:43 +02:00
2017-07-09 07:53:13 +02:00
snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat);
2017-06-05 01:30:37 +02:00
session->base.impl = &session_direct;
2018-06-14 10:46:16 +02:00
2018-07-09 23:49:54 +02:00
wlr_log(WLR_INFO, "Successfully loaded direct session");
2017-05-03 09:26:43 +02:00
return &session->base;
2017-07-03 04:46:20 +02:00
2017-07-09 12:12:50 +02:00
error_ipc:
direct_ipc_finish(session->sock, session->child);
close(session->sock);
2017-07-03 04:46:20 +02:00
error_session:
free(session);
return NULL;
2017-05-03 09:26:43 +02:00
}
2017-06-05 01:30:37 +02:00
const struct session_impl session_direct = {
.create = direct_session_create,
.destroy = direct_session_destroy,
2017-05-03 09:26:43 +02:00
.open = direct_session_open,
.close = direct_session_close,
2017-05-13 13:56:40 +02:00
.change_vt = direct_change_vt,
2017-05-03 09:26:43 +02:00
};