From a5fe9aa73633594b443e2a1a990cdf23addbb28b Mon Sep 17 00:00:00 2001 From: Greg V Date: Tue, 10 Oct 2017 01:23:43 +0300 Subject: [PATCH] Add FreeBSD compatibility --- README.md | 2 + backend/meson.build | 7 +- backend/session/direct-freebsd.c | 217 +++++++++++++++++++++++++++++ backend/session/direct-ipc.c | 13 +- backend/session/session.c | 6 + backend/wayland/os-compatibility.c | 2 + backend/x11/backend.c | 6 +- examples/output-layout.c | 2 +- meson.build | 3 + protocol/meson.build | 3 +- rootston/cursor.c | 7 +- rootston/output.c | 2 +- types/wlr_output.c | 2 +- util/log.c | 2 +- xcursor/meson.build | 1 + xwayland/sockets.c | 9 +- xwayland/xwayland.c | 12 ++ 17 files changed, 283 insertions(+), 13 deletions(-) create mode 100644 backend/session/direct-freebsd.c diff --git a/README.md b/README.md index d232804a..0f221e3c 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,5 @@ Run these commands: meson build ninja -C build + +(On FreeBSD, you need to pass an extra flag to prevent a linking error: `meson build -D b_lundef=false`) diff --git a/backend/meson.build b/backend/meson.build index cf62a56f..5ed7b227 100644 --- a/backend/meson.build +++ b/backend/meson.build @@ -1,7 +1,6 @@ backend_files = files( 'backend.c', 'session/direct-ipc.c', - 'session/direct.c', 'session/session.c', 'drm/atomic.c', 'drm/backend.c', @@ -26,6 +25,12 @@ backend_files = files( 'x11/backend.c', ) +if host_machine.system().startswith('freebsd') + backend_files += files('session/direct-freebsd.c') +else + backend_files += files('session/direct.c') +endif + if systemd.found() backend_files += files('session/logind.c') endif diff --git a/backend/session/direct-freebsd.c b/backend/session/direct-freebsd.c new file mode 100644 index 00000000..fc4bab04 --- /dev/null +++ b/backend/session/direct-freebsd.c @@ -0,0 +1,217 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "backend/session/direct-ipc.h" + +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; + } + + 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; + } + + 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_destroy(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); + ioctl(session->tty_fd, VT_RELDISP, 1); + } else { + ioctl(session->tty_fd, VT_RELDISP, VT_ACKACQ); + 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 = -1, tty = -1, tty0_fd = -1; + if ((tty0_fd = open("/dev/ttyv0", O_RDWR | O_CLOEXEC)) < 0) { + wlr_log_errno(L_ERROR, "Could not open /dev/ttyv0 to find a free vt"); + goto error; + } + if (ioctl(tty0_fd, VT_OPENQRY, &tty) != 0) { + wlr_log_errno(L_ERROR, "Could not find a free vt"); + goto error; + } + close(tty0_fd); + char tty_path[64]; + snprintf(tty_path, sizeof(tty_path), "/dev/ttyv%d", tty - 1); + wlr_log(L_INFO, "Using tty %s", tty_path); + fd = open(tty_path, O_RDWR | O_NOCTTY | O_CLOEXEC); + + if (fd == -1) { + wlr_log_errno(L_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(L_ERROR, "Failed to read tty %d keyboard mode", tty); + goto error; + } + + if (ioctl(fd, KDSKBMODE, K_CODE)) { + wlr_log_errno(L_ERROR, "Failed to set keyboard mode K_CODE on tty %d", tty); + goto error; + } + + if (ioctl(fd, KDSETMODE, KD_GRAPHICS)) { + wlr_log_errno(L_ERROR, "Failed to set graphics mode on tty %d", tty); + goto error; + } + + struct vt_mode mode = { + .mode = VT_PROCESS, + .relsig = SIGUSR1, + .acqsig = SIGUSR1, + .frsig = SIGIO, // has to be set + }; + + if (ioctl(fd, VT_SETMODE, &mode) < 0) { + wlr_log(L_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, 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: + // Drop back to tty 1, better than hanging in a useless blank console + ioctl(fd, VT_ACTIVATE, 1); + 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(L_ERROR, "Allocation failed"); + return NULL; + } + + session->sock = direct_ipc_init(&session->child); + if (session->sock == -1) { + goto error_session; + } + + if (!setup_tty(session, disp)) { + goto error_ipc; + } + + wlr_log(L_INFO, "Successfully loaded direct session"); + + snprintf(session->base.seat, sizeof(session->base.seat), "seat0"); + session->base.impl = &session_direct; + 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, +}; diff --git a/backend/session/direct-ipc.c b/backend/session/direct-ipc.c index 5f1494a6..0ea51e2f 100644 --- a/backend/session/direct-ipc.c +++ b/backend/session/direct-ipc.c @@ -1,4 +1,8 @@ #define _POSIX_C_SOURCE 200809L +#ifdef __FreeBSD__ +#define __BSD_VISIBLE 1 +#define INPUT_MAJOR 0 +#endif #include #include #include @@ -8,10 +12,12 @@ #include #include #include -#include #include -#include +#ifdef __linux__ +#include #include +#endif +#include #include #include "backend/session/direct-ipc.h" @@ -35,11 +41,12 @@ static bool have_permissions(void) { } #else static bool have_permissions(void) { +#ifdef __linux__ if (geteuid() != 0) { wlr_log(L_ERROR, "Do not have root privileges; cannot become DRM master"); return false; } - +#endif return true; } #endif diff --git a/backend/session/session.c b/backend/session/session.c index b73952fd..b14ca4d0 100644 --- a/backend/session/session.c +++ b/backend/session/session.c @@ -263,6 +263,11 @@ size_t wlr_session_find_gpus(struct wlr_session *session, return explicit_find_gpus(session, ret_len, ret, explicit); } +#ifdef __FreeBSD__ + // XXX: libudev-devd does not return any GPUs (yet?) + return explicit_find_gpus(session, ret_len, ret, "/dev/drm/0"); +#else + struct udev_enumerate *en = udev_enumerate_new(session->udev); if (!en) { wlr_log(L_ERROR, "Failed to create udev enumeration"); @@ -329,4 +334,5 @@ size_t wlr_session_find_gpus(struct wlr_session *session, udev_enumerate_unref(en); return i; +#endif } diff --git a/backend/wayland/os-compatibility.c b/backend/wayland/os-compatibility.c index cd268539..14125793 100644 --- a/backend/wayland/os-compatibility.c +++ b/backend/wayland/os-compatibility.c @@ -29,7 +29,9 @@ #include #include #include +#ifdef __linux__ #include +#endif #include #include diff --git a/backend/x11/backend.c b/backend/x11/backend.c index 1eb5a952..bcc2e970 100644 --- a/backend/x11/backend.c +++ b/backend/x11/backend.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #include #include #include @@ -8,7 +8,11 @@ #include #include #include +#ifdef __linux__ #include +#elif __FreeBSD__ +#include +#endif #include #include #include diff --git a/examples/output-layout.c b/examples/output-layout.c index 560a7113..771ccd77 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -1,5 +1,5 @@ #define _POSIX_C_SOURCE 199309L -#define _XOPEN_SOURCE 500 +#define _XOPEN_SOURCE 700 #include #include #include diff --git a/meson.build b/meson.build index d885484b..3e4cab38 100644 --- a/meson.build +++ b/meson.build @@ -34,6 +34,9 @@ if cc.get_id() == 'clang' add_project_arguments('-Wno-missing-braces', language: 'c') endif +# Avoid wl_buffer deprecation warnings +add_project_arguments('-DWL_HIDE_DEPRECATED', language: 'c') + wayland_server = dependency('wayland-server') wayland_client = dependency('wayland-client') wayland_egl = dependency('wayland-egl') diff --git a/protocol/meson.build b/protocol/meson.build index 79871fea..2cbb5b6d 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -46,7 +46,8 @@ foreach p : client_protocols wl_protos_headers += wayland_scanner_client.process(xml) endforeach -lib_wl_protos = static_library('wl_protos', wl_protos_src + wl_protos_headers) +lib_wl_protos = static_library('wl_protos', wl_protos_src + wl_protos_headers, + dependencies: [wayland_client]) # for the include directory wlr_protos = declare_dependency( link_with: lib_wl_protos, diff --git a/rootston/cursor.c b/rootston/cursor.c index eeb657ad..8790934c 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -1,10 +1,13 @@ -#define _XOPEN_SOURCE 500 +#define _XOPEN_SOURCE 700 #include #include #include #include -// TODO: BSD et al +#ifdef __linux__ #include +#elif __FreeBSD__ +#include +#endif #include #include #include diff --git a/rootston/output.c b/rootston/output.c index 6c7fbf51..4ce10ea2 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200809L #include #include #include diff --git a/types/wlr_output.c b/types/wlr_output.c index 13a6adbd..723dd00b 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200809L #include #include #include diff --git a/util/log.c b/util/log.c index 78dc97f9..3661b73c 100644 --- a/util/log.c +++ b/util/log.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 1 +#define _POSIX_C_SOURCE 199506L #include #include #include diff --git a/xcursor/meson.build b/xcursor/meson.build index e8d456cb..afaab915 100644 --- a/xcursor/meson.build +++ b/xcursor/meson.build @@ -5,4 +5,5 @@ lib_wlr_xcursor = static_library( 'wlr_xcursor.c', ), include_directories: wlr_inc, + dependencies: [egl] # header required via include/wlr/render.h ) diff --git a/xwayland/sockets.c b/xwayland/sockets.c index 48bcc822..dd732dd7 100644 --- a/xwayland/sockets.c +++ b/xwayland/sockets.c @@ -1,4 +1,8 @@ #define _XOPEN_SOURCE 700 +#ifdef __FreeBSD__ +// for SOCK_CLOEXEC +#define __BSD_VISIBLE 1 +#endif #include #include #include @@ -65,9 +69,12 @@ static bool open_sockets(int socks[2], int display) { mkdir(socket_dir, 0777); - // TODO: non-linux apparently want another format +#ifdef __linux__ addr.sun_path[0] = 0; path_size = snprintf(addr.sun_path + 1, sizeof(addr.sun_path) - 1, socket_fmt, display); +#else + path_size = snprintf(addr.sun_path, sizeof(addr.sun_path), socket_fmt, display); +#endif socks[0] = open_socket(&addr, path_size); if (socks[0] < 0) { return false; diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index 2bec1b63..f1599911 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -1,5 +1,9 @@ #define _XOPEN_SOURCE 700 #define _DEFAULT_SOURCE +#ifdef __FreeBSD__ +// for SOCK_CLOEXEC +#define __BSD_VISIBLE 1 +#endif #include #include #include @@ -17,6 +21,14 @@ #include "sockets.h" #include "xwm.h" +#ifdef __FreeBSD__ +static inline int clearenv(void) { + extern char **environ; + environ[0] = NULL; + return 0; +} +#endif + static void safe_close(int fd) { if (fd >= 0) { close(fd);