parent
95b657ba80
commit
d037c2dddc
@ -1,319 +0,0 @@
|
||||
#include <assert.h>
|
||||
#include <linux/input.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/consio.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/kbio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <wayland-server-core.h>
|
||||
#include <wlr/backend/session/interface.h>
|
||||
#include <wlr/util/log.h>
|
||||
#include <xf86drm.h>
|
||||
#include "backend/session/direct-ipc.h"
|
||||
#include "backend/session/session.h"
|
||||
#include "util/signal.h"
|
||||
|
||||
const struct session_impl session_direct;
|
||||
|
||||
struct direct_session {
|
||||
struct wlr_session base;
|
||||
int tty_fd;
|
||||
int old_tty;
|
||||
int old_kbmode;
|
||||
int sock;
|
||||
pid_t child;
|
||||
|
||||
struct wl_event_source *vt_source;
|
||||
};
|
||||
|
||||
static struct direct_session *direct_session_from_session(
|
||||
struct wlr_session *base) {
|
||||
assert(base->impl == &session_direct);
|
||||
return (struct direct_session *)base;
|
||||
}
|
||||
|
||||
static int direct_session_open(struct wlr_session *base, const char *path) {
|
||||
struct direct_session *session = direct_session_from_session(base);
|
||||
|
||||
int fd = direct_ipc_open(session->sock, path);
|
||||
if (fd < 0) {
|
||||
wlr_log(WLR_ERROR, "Failed to open %s: %s%s", path, strerror(-fd),
|
||||
fd == -EINVAL ? "; is another display server running?" : "");
|
||||
return fd;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
static void direct_session_close(struct wlr_session *base, int fd) {
|
||||
struct direct_session *session = direct_session_from_session(base);
|
||||
|
||||
int ev;
|
||||
struct drm_version dv = {0};
|
||||
if (ioctl(fd, DRM_IOCTL_VERSION, &dv) == 0) {
|
||||
direct_ipc_dropmaster(session->sock, fd);
|
||||
} else if (ioctl(fd, EVIOCGVERSION, &ev) == 0) {
|
||||
ioctl(fd, EVIOCREVOKE, 0);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static bool direct_change_vt(struct wlr_session *base, unsigned vt) {
|
||||
struct direct_session *session = direct_session_from_session(base);
|
||||
|
||||
// Only seat0 has VTs associated with it
|
||||
if (strcmp(session->base.seat, "seat0") != 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ioctl(session->tty_fd, VT_ACTIVATE, (int)vt) == 0;
|
||||
}
|
||||
|
||||
static void direct_session_destroy(struct wlr_session *base) {
|
||||
struct direct_session *session = direct_session_from_session(base);
|
||||
|
||||
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);
|
||||
|
||||
ioctl(session->tty_fd, VT_ACTIVATE, session->old_tty);
|
||||
|
||||
if (errno) {
|
||||
wlr_log(WLR_ERROR, "Failed to restore tty");
|
||||
}
|
||||
|
||||
wl_event_source_remove(session->vt_source);
|
||||
close(session->tty_fd);
|
||||
}
|
||||
|
||||
direct_ipc_finish(session->sock, session->child);
|
||||
close(session->sock);
|
||||
|
||||
free(session);
|
||||
}
|
||||
|
||||
static int vt_handler(int signo, void *data) {
|
||||
struct direct_session *session = data;
|
||||
struct drm_version dv = {0};
|
||||
struct wlr_device *dev;
|
||||
|
||||
if (session->base.active) {
|
||||
session->base.active = false;
|
||||
wlr_signal_emit_safe(&session->base.events.active, NULL);
|
||||
|
||||
wl_list_for_each(dev, &session->base.devices, link) {
|
||||
if (ioctl(dev->fd, DRM_IOCTL_VERSION, &dv) == 0) {
|
||||
direct_ipc_dropmaster(session->sock, dev->fd);
|
||||
}
|
||||
}
|
||||
|
||||
ioctl(session->tty_fd, VT_RELDISP, 1);
|
||||
} else {
|
||||
ioctl(session->tty_fd, VT_RELDISP, VT_ACKACQ);
|
||||
|
||||
wl_list_for_each(dev, &session->base.devices, link) {
|
||||
if (ioctl(dev->fd, DRM_IOCTL_VERSION, &dv) == 0) {
|
||||
direct_ipc_setmaster(session->sock, dev->fd);
|
||||
}
|
||||
}
|
||||
|
||||
session->base.active = true;
|
||||
wlr_signal_emit_safe(&session->base.events.active, NULL);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool get_tty_path(int tty, char path[static 11], size_t len) {
|
||||
assert(tty > 0);
|
||||
|
||||
const char prefix[] = "/dev/ttyv";
|
||||
static_assert(sizeof(prefix) + 1 <= 11, "TTY path prefix is too long");
|
||||
|
||||
const size_t prefix_len = sizeof(prefix) - 1;
|
||||
strcpy(path, prefix);
|
||||
|
||||
size_t offset = prefix_len;
|
||||
const int num = tty - 1;
|
||||
if (num == 0) {
|
||||
path[offset++] = '0';
|
||||
path[offset++] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
const int base = 32;
|
||||
for (int remaning = num; remaning > 0; remaning /= base, offset++) {
|
||||
// Return early if the buffer is too small.
|
||||
if (offset + 1 >= len) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int value = remaning % base;
|
||||
if (value >= 10) {
|
||||
path[offset] = 'a' + value - 10;
|
||||
} else {
|
||||
path[offset] = '0' + value;
|
||||
}
|
||||
}
|
||||
|
||||
const size_t num_len = offset - prefix_len;
|
||||
for (size_t i = 0; i < num_len / 2; i++) {
|
||||
const size_t p1 = prefix_len + i;
|
||||
const size_t p2 = offset - 1 - i;
|
||||
const char tmp = path[p1];
|
||||
path[p1] = path[p2];
|
||||
path[p2] = tmp;
|
||||
}
|
||||
|
||||
path[offset++] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool setup_tty(struct direct_session *session, struct wl_display *display) {
|
||||
int fd = -1, tty = -1, tty0_fd = -1, old_tty = 1;
|
||||
if ((tty0_fd = open("/dev/ttyv0", O_RDWR | O_CLOEXEC)) < 0) {
|
||||
wlr_log_errno(WLR_ERROR, "Could not open /dev/ttyv0 to find a free vt");
|
||||
goto error;
|
||||
}
|
||||
if (ioctl(tty0_fd, VT_GETACTIVE, &old_tty) != 0) {
|
||||
wlr_log_errno(WLR_ERROR, "Could not get active vt");
|
||||
goto error;
|
||||
}
|
||||
if (ioctl(tty0_fd, VT_OPENQRY, &tty) != 0) {
|
||||
wlr_log_errno(WLR_ERROR, "Could not find a free vt");
|
||||
goto error;
|
||||
}
|
||||
close(tty0_fd);
|
||||
char tty_path[64];
|
||||
if (!get_tty_path(tty, tty_path, sizeof(tty_path))) {
|
||||
wlr_log(WLR_ERROR, "Could not get tty %d path", tty);
|
||||
goto error;
|
||||
}
|
||||
wlr_log(WLR_INFO, "Using tty %s", tty_path);
|
||||
fd = open(tty_path, O_RDWR | O_NOCTTY | O_CLOEXEC);
|
||||
|
||||
if (fd == -1) {
|
||||
wlr_log_errno(WLR_ERROR, "Cannot open tty");
|
||||
return false;
|
||||
}
|
||||
|
||||
ioctl(fd, VT_ACTIVATE, tty);
|
||||
ioctl(fd, VT_WAITACTIVE, tty);
|
||||
|
||||
int old_kbmode;
|
||||
if (ioctl(fd, KDGKBMODE, &old_kbmode)) {
|
||||
wlr_log_errno(WLR_ERROR, "Failed to read tty %d keyboard mode", tty);
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (ioctl(fd, KDSKBMODE, K_CODE)) {
|
||||
wlr_log_errno(WLR_ERROR,
|
||||
"Failed to set keyboard mode K_CODE on tty %d", tty);
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (ioctl(fd, KDSETMODE, KD_GRAPHICS)) {
|
||||
wlr_log_errno(WLR_ERROR, "Failed to set graphics mode on tty %d", tty);
|
||||
goto error;
|
||||
}
|
||||
|
||||
struct vt_mode mode = {
|
||||
.mode = VT_PROCESS,
|
||||
.relsig = SIGUSR2,
|
||||
.acqsig = SIGUSR2,
|
||||
.frsig = SIGIO, // has to be set
|
||||
};
|
||||
|
||||
if (ioctl(fd, VT_SETMODE, &mode) < 0) {
|
||||
wlr_log(WLR_ERROR, "Failed to take control of tty %d", tty);
|
||||
goto error;
|
||||
}
|
||||
|
||||
struct wl_event_loop *loop = wl_display_get_event_loop(display);
|
||||
session->vt_source = wl_event_loop_add_signal(loop, SIGUSR2,
|
||||
vt_handler, session);
|
||||
if (!session->vt_source) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
session->base.vtnr = tty;
|
||||
session->tty_fd = fd;
|
||||
session->old_tty = old_tty;
|
||||
session->old_kbmode = old_kbmode;
|
||||
|
||||
return true;
|
||||
|
||||
error:
|
||||
// In case we could not get the last active one, drop back to tty 1,
|
||||
// better than hanging in a useless blank console. Otherwise activate the
|
||||
// last active.
|
||||
ioctl(fd, VT_ACTIVATE, old_tty);
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
static struct wlr_session *direct_session_create(struct wl_display *disp) {
|
||||
struct direct_session *session = calloc(1, sizeof(*session));
|
||||
if (!session) {
|
||||
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
session_init(&session->base);
|
||||
session->sock = direct_ipc_init(&session->child);
|
||||
if (session->sock == -1) {
|
||||
goto error_session;
|
||||
}
|
||||
|
||||
const char *seat = getenv("XDG_SEAT");
|
||||
if (!seat) {
|
||||
seat = "seat0";
|
||||
}
|
||||
|
||||
if (strcmp(seat, "seat0") == 0) {
|
||||
if (!setup_tty(session, disp)) {
|
||||
goto error_ipc;
|
||||
}
|
||||
} else {
|
||||
session->base.vtnr = 0;
|
||||
session->tty_fd = -1;
|
||||
}
|
||||
|
||||
wlr_log(WLR_INFO, "Successfully loaded direct session");
|
||||
|
||||
snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat);
|
||||
session->base.impl = &session_direct;
|
||||
session->base.active = true;
|
||||
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 = {
|
||||
.create = direct_session_create,
|
||||
.destroy = direct_session_destroy,
|
||||
.open = direct_session_open,
|
||||
.close = direct_session_close,
|
||||
.change_vt = direct_change_vt,
|
||||
};
|
@ -1,237 +0,0 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#ifdef __FreeBSD__
|
||||
#define __BSD_VISIBLE 1
|
||||
#include <linux/input.h>
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <wlr/config.h>
|
||||
#include <wlr/util/log.h>
|
||||
#include <xf86drm.h>
|
||||
#ifdef __linux__
|
||||
#include <sys/sysmacros.h>
|
||||
#include <linux/major.h>
|
||||
#endif
|
||||
#include "backend/session/direct-ipc.h"
|
||||
|
||||
enum { DRM_MAJOR = 226 };
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#ifndef __FreeBSD__
|
||||
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;
|
||||
}
|
||||
#else
|
||||
int ev;
|
||||
struct drm_version dv = {0};
|
||||
if (ioctl(fd, EVIOCGVERSION, &ev) == -1 &&
|
||||
ioctl(fd, DRM_IOCTL_VERSION, &dv) == -1) {
|
||||
ret = ENOTSUP;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (dv.version_major != 0 && drmSetMaster(fd)) {
|
||||
ret = errno;
|
||||
}
|
||||
#endif
|
||||
|
||||
error:
|
||||
send_msg(sock, ret ? -1 : fd, &ret, sizeof(ret));
|
||||
if (fd >= 0) {
|
||||
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, ret;
|
||||
int retry = 0;
|
||||
do {
|
||||
ret = recv_msg(sock, &fd, &err, sizeof(err));
|
||||
} while (ret == 0 && retry++ < 3);
|
||||
|
||||
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_init(pid_t *pid_out) {
|
||||
int sock[2];
|
||||
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock) < 0) {
|
||||
wlr_log_errno(WLR_ERROR, "Failed to create socket pair");
|
||||
return -1;
|
||||
}
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
wlr_log_errno(WLR_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];
|
||||
}
|
@ -1,291 +0,0 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/input.h>
|
||||
#include <linux/kd.h>
|
||||
#include <linux/major.h>
|
||||
#include <linux/vt.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <unistd.h>
|
||||
#include <wayland-server-core.h>
|
||||
#include <wlr/backend/session/interface.h>
|
||||
#include <wlr/util/log.h>
|
||||
#include "backend/session/direct-ipc.h"
|
||||
#include "backend/session/session.h"
|
||||
#include "util/signal.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 struct direct_session *direct_session_from_session(
|
||||
struct wlr_session *base) {
|
||||
assert(base->impl == &session_direct);
|
||||
return (struct direct_session *)base;
|
||||
}
|
||||
|
||||
static int direct_session_open(struct wlr_session *base, const char *path) {
|
||||
struct direct_session *session = direct_session_from_session(base);
|
||||
|
||||
int fd = direct_ipc_open(session->sock, path);
|
||||
if (fd < 0) {
|
||||
wlr_log(WLR_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) {
|
||||
direct_ipc_setmaster(session->sock, fd);
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
static void direct_session_close(struct wlr_session *base, int fd) {
|
||||
struct direct_session *session = direct_session_from_session(base);
|
||||
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) < 0) {
|
||||
wlr_log_errno(WLR_ERROR, "Stat failed");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
if (major(st.st_rdev) == DRM_MAJOR) {
|
||||
direct_ipc_dropmaster(session->sock, fd);
|
||||
} 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 = direct_session_from_session(base);
|
||||
|
||||
// Only seat0 has VTs associated with it
|
||||
if (strcmp(session->base.seat, "seat0") != 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ioctl(session->tty_fd, VT_ACTIVATE, (int)vt) == 0;
|
||||
}
|
||||
|
||||
static void direct_session_destroy(struct wlr_session *base) {
|
||||
struct direct_session *session = direct_session_from_session(base);
|
||||
|
||||
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);
|
||||
|
||||
if (errno) {
|
||||
wlr_log(WLR_ERROR, "Failed to restore tty");
|
||||
}
|
||||
|
||||
wl_event_source_remove(session->vt_source);
|
||||
close(session->tty_fd);
|
||||
}
|
||||
|
||||
direct_ipc_finish(session->sock, session->child);
|
||||
close(session->sock);
|
||||
|
||||
free(session);
|
||||
}
|
||||
|
||||
static int vt_handler(int signo, void *data) {
|
||||
struct direct_session *session = data;
|
||||
|
||||
if (session->base.active) {
|
||||
session->base.active = false;
|
||||
wlr_signal_emit_safe(&session->base.events.active, NULL);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
session->base.active = true;
|
||||
wlr_signal_emit_safe(&session->base.events.active, NULL);
|
||||
}
|
||||
|
||||
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);
|
||||
if (fd == -1) {
|
||||
wlr_log_errno(WLR_ERROR, "Cannot open %s", tty_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
struct vt_stat vt_stat;
|
||||
if (ioctl(fd, VT_GETSTATE, &vt_stat)) {
|
||||
wlr_log_errno(WLR_ERROR, "Could not get current tty number");
|
||||
goto error;
|
||||
}
|
||||
|
||||
int tty = vt_stat.v_active;
|
||||
int ret, kd_mode, old_kbmode;
|
||||
|
||||
ret = ioctl(fd, KDGETMODE, &kd_mode);
|
||||
if (ret) {
|
||||
wlr_log_errno(WLR_ERROR, "Failed to get tty mode");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (default_tty && kd_mode != KD_TEXT) {
|
||||
wlr_log(WLR_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(WLR_ERROR, "Failed to read keyboard mode");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (ioctl(fd, KDSKBMODE, K_OFF)) {
|
||||
wlr_log_errno(WLR_ERROR, "Failed to set keyboard mode");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (ioctl(fd, KDSETMODE, KD_GRAPHICS)) {
|
||||
wlr_log_errno(WLR_ERROR, "Failed to set graphics mode on tty");
|
||||
goto error;
|
||||
}
|
||||
|
||||
struct vt_mode mode = {
|
||||
.mode = VT_PROCESS,
|
||||
.relsig = SIGUSR2,
|
||||
.acqsig = SIGUSR2,
|
||||
};
|
||||
|
||||
if (ioctl(fd, VT_SETMODE, &mode) < 0) {
|
||||
wlr_log(WLR_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, SIGUSR2,
|
||||
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_create(struct wl_display *disp) {
|
||||
struct direct_session *session = calloc(1, sizeof(*session));
|
||||
if (!session) {
|
||||
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
session_init(&session->base);
|
||||
session->sock = direct_ipc_init(&session->child);
|
||||
if (session->sock == -1) {
|
||||
goto error_session;
|
||||
}
|
||||
|
||||
const char *seat = getenv("XDG_SEAT");
|
||||
if (!seat) {
|
||||
seat = "seat0";
|
||||
}
|
||||
|
||||
if (strcmp(seat, "seat0") == 0) {
|
||||
if (!setup_tty(session, disp)) {
|
||||
goto error_ipc;
|
||||
}
|
||||
} else {
|
||||
session->base.vtnr = 0;
|
||||
session->tty_fd = -1;
|
||||
}
|
||||
|
||||
snprintf(session->base.seat, sizeof(session->base.seat), "%s", seat);
|
||||
session->base.impl = &session_direct;
|
||||
session->base.active = true;
|
||||
|
||||
wlr_log(WLR_INFO, "Successfully loaded direct session");
|
||||
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 = {
|
||||
.create = direct_session_create,
|
||||
.destroy = direct_session_destroy,
|
||||
.open = direct_session_open,
|
||||
.close = direct_session_close,
|
||||
.change_vt = direct_change_vt,
|
||||
};
|
@ -1,12 +0,0 @@
|
||||
#ifndef BACKEND_SESSION_DIRECT_IPC_H
|
||||
#define BACKEND_SESSION_DIRECT_IPC_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
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_init(pid_t *pid_out);
|
||||
|
||||
#endif
|
Loading…
Reference in new issue