neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

commit 4619c0f507e68b8cfc7382ab1414f53c2ba02e7a
parent d1ec3e24c7e0b7400dc5c0e4dfb7194e805a489d
Author: uint <unsigned@netcat.uk>
Date:   Thu, 29 Oct 2026 11:21:54 +0000

fix OpenBSD wscons not working

launch/launch.c: Add OpenBSD setuid support (since -current 2026),
enable VT switching

libswc/seat-ws.c: Handle key resets, support temporary XKB environment
configs

libswc/keyboard.c: Include NUL byte

libswc/xserver.c: Skip unavailable socket descriptors on OpenBSD too

Diffstat:
Mlaunch/launch.c | 59+++++++++++++++++++++++++++++++++++------------------------
Mlibswc/keyboard.c | 3+--
Mlibswc/seat-ws.c | 54+++++++++++++++++++++++++++++++++++++++++-------------
Mlibswc/xserver.c | 5+++--
4 files changed, 80 insertions(+), 41 deletions(-)

diff --git a/launch/launch.c b/launch/launch.c @@ -74,6 +74,12 @@ #include <xf86drm.h> #endif +#ifdef __OpenBSD__ +/* required by the runtime for setuid executables */ +__attribute__((visibility("default"))) +mode_t _permit_setugid = S_ISUID; +#endif + #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0]) static void @@ -89,6 +95,7 @@ static int drm_fds[16], num_drm_fds; #endif static int tty_fd; static bool active; +static pid_t child_pid = -1; static struct { bool altered; @@ -166,9 +173,7 @@ stop_devices(bool fatal) static void cleanup(void) { -#ifndef __OpenBSD__ struct vt_mode mode = {.mode = VT_AUTO}; -#endif if (!original_vt_state.altered) { return; @@ -178,18 +183,16 @@ cleanup(void) * DRM device before the next session tries to claim it. */ stop_devices(false); - /* Cleanup VT */ -#ifndef __OpenBSD__ + /* stop VT signal delivery before restoring tty */ ioctl(tty_fd, VT_SETMODE, &mode); +#ifndef __OpenBSD__ ioctl(tty_fd, KDSKBMODE, original_vt_state.kb_mode); #endif ioctl(tty_fd, KDSETMODE, original_vt_state.console_mode); - -#ifndef __OpenBSD__ ioctl(tty_fd, VT_ACTIVATE, original_vt_state.vt); -#endif - - kill(0, SIGTERM); + original_vt_state.altered = false; + if (child_pid > 0) + kill(child_pid, SIGTERM); } static void @@ -424,13 +427,16 @@ setup_tty(int fd) { struct stat st; int vt; -#if !defined (__OpenBSD__) && !defined(__FreeBSD__) +#if !defined(__FreeBSD__) struct vt_stat state; - #endif -#if !defined(__OpenBSD__) +#endif struct vt_mode mode = { - .mode = VT_PROCESS, .relsig = SIGUSR1, .acqsig = SIGUSR2}; + .mode = VT_PROCESS, .relsig = SIGUSR1, .acqsig = SIGUSR2, +#ifdef __OpenBSD__ + /* wsdisplay validates frsig but is unused */ + .frsig = SIGUSR1, #endif + }; if (fstat(fd, &st) == -1) { die("failed to stat TTY fd:"); @@ -447,10 +453,7 @@ setup_tty(int fd) } #endif -#if defined(__OpenBSD__) - /* OpenBSD wscons has no VT_GETSTATE */ - original_vt_state.vt=vt; -#elif defined(__FreeBSD__) +#if defined(__FreeBSD__) { int vt_num; if (ioctl(fd, VT_GETACTIVE, &vt_num) == -1) @@ -465,10 +468,12 @@ setup_tty(int fd) } #endif -#if !defined (__OpenBSD__) && !defined(__FreeBSD__) +#if !defined(__FreeBSD__) original_vt_state.vt = state.v_active; -#else - original_vt_state.vt = vt; +#endif +#ifdef __OpenBSD__ + /* VT ioctl api expects it to start from 1 */ + ++vt; #endif #ifdef KDGETMODE @@ -497,7 +502,7 @@ setup_tty(int fd) goto error0; } -#if !defined (__OpenBSD__) && !defined (__FreeBSD__) +#ifndef __FreeBSD__ if (ioctl(fd, VT_SETMODE, &mode) == -1) { perror("failed to set VT mode"); goto error1; @@ -505,7 +510,10 @@ setup_tty(int fd) #endif #ifdef __OpenBSD__ - /* OpenBSD wscons has no VT_PROCESS mode; just cont. on current ttyC* */ + if (vt != original_vt_state.vt) { + fprintf(stderr, "OpenBSD: start on the active ttyC console\n"); + goto error2; + } activate(); #else if (vt == original_vt_state.vt) { @@ -526,11 +534,13 @@ setup_tty(int fd) return; -#ifndef __OpenBSD__ +#ifndef __FreeBSD__ error2: mode = (struct vt_mode){.mode = VT_AUTO}; ioctl(fd, VT_SETMODE, &mode); error1: +#endif +#ifndef __OpenBSD__ ioctl(fd, KDSKBMODE, original_vt_state.kb_mode); #endif error0: @@ -656,7 +666,7 @@ main(int argc, char *argv[]) die("setenv %s:", SWC_LAUNCH_TTY_ENV); } - sprintf(buf, "%d", sock[1]); + snprintf(buf, sizeof(buf), "%d", sock[1]); setenv(SWC_LAUNCH_SOCKET_ENV, buf, 1); /* make sure XDG_RUNTIME_DIR is set */ @@ -685,6 +695,7 @@ main(int argc, char *argv[]) environ))) { die("posix_spawnp %s:", argv[optind]); } + child_pid = pid; posix_spawnattr_destroy(&attr); close(sock[1]); diff --git a/libswc/keyboard.c b/libswc/keyboard.c @@ -378,11 +378,10 @@ keyboard_bind(struct keyboard *keyboard, wl_resource_set_implementation( client_resource, &keyboard_impl, keyboard, &unbind); - /* Subtract one to remove terminating NULL character. */ wl_keyboard_send_keymap(client_resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, keyboard->xkb.keymap.fd, - keyboard->xkb.keymap.size - 1); + keyboard->xkb.keymap.size); input_focus_add_resource(&keyboard->focus, client_resource); diff --git a/libswc/seat-ws.c b/libswc/seat-ws.c @@ -47,6 +47,7 @@ #include <dev/wscons/wsconsio.h> #include <dev/wscons/wsksymdef.h> #include <sys/ioctl.h> +#include <time.h> /* Map wscons encodings to libxkbcommon layout names. */ struct ws_xkb_map { @@ -205,20 +206,29 @@ bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id) static int ws_to_xkb(unsigned type, int key) { + const uint8_t *map; + size_t count; + switch (type) { case WSKBD_TYPE_PC_XT: case WSKBD_TYPE_PC_AT: - return wsXtMap[key]; + map = wsXtMap; + count = sizeof(wsXtMap); + break; case WSKBD_TYPE_USB: #ifdef WSKBD_TYPE_MAPLE case WSKBD_TYPE_MAPLE: - return wsUsbMap[key]; #endif - return wsUsbMap[key]; + map = wsUsbMap; + count = sizeof(wsUsbMap); + break; default: - fprintf(stderr, "Unknown wskbd type %d\n", type); - return key; + return -1; } + + if (key < 0 || (size_t)key >= count || map[key] == KEY_NOTUSED) + return -1; + return map[key]; } static int @@ -241,24 +251,25 @@ handle_ws_data(int fd, uint32_t mask, void *data) struct seat *seat = data; struct wscons_event ev; - while (!seat->ignore && (read(fd, &ev, sizeof(ev))) != -1) { + while (!seat->ignore && read(fd, &ev, sizeof(ev)) == sizeof(ev)) { uint32_t state, time; int key; wl_fixed_t pos; - time = ev.time.tv_sec + (ev.time.tv_nsec / 1000000L); + time = (uint32_t)(ev.time.tv_sec * 1000ULL + + ev.time.tv_nsec / 1000000L); switch (ev.type) { case WSCONS_EVENT_KEY_UP: - state = WL_KEYBOARD_KEY_STATE_RELEASED; - key = ws_to_xkb(seat->kbd_type, ev.value); - keyboard_handle_key(seat->base.keyboard, time, key, state); - break; case WSCONS_EVENT_KEY_DOWN: - state = WL_KEYBOARD_KEY_STATE_PRESSED; + state = ev.type == WSCONS_EVENT_KEY_DOWN ? + WL_KEYBOARD_KEY_STATE_PRESSED : + WL_KEYBOARD_KEY_STATE_RELEASED; key = ws_to_xkb(seat->kbd_type, ev.value); - keyboard_handle_key(seat->base.keyboard, time, key, state); + if (key >= 0) + keyboard_handle_key(seat->base.keyboard, time, key, state); break; case WSCONS_EVENT_ALL_KEYS_UP: + keyboard_reset(seat->base.keyboard); break; case WSCONS_EVENT_MOUSE_UP: state = WL_POINTER_BUTTON_STATE_RELEASED; @@ -358,6 +369,8 @@ initialize_wscons(struct seat *seat) ERROR("Could not get keyboard type\n"); goto error2; } + /* TODO handle mixed keyboard types with wsmux? + see: https://www.mail-archive.com/ports%40openbsd.org/msg134646.html */ if (ioctl(seat->kbd_fd, WSKBDIO_GETENCODING, &encoding) != -1) { encoding_layout = KB_ENCODING(encoding); @@ -419,6 +432,21 @@ seat_create(struct wl_display *display, const char *seat_name) goto error2; } + /* TODO other way */ + { + const char *s; + if ((s = getenv("XKB_DEFAULT_RULES")) && *s) + seat->names.rules = s; + if ((s = getenv("XKB_DEFAULT_MODEL")) && *s) + seat->names.model = s; + if ((s = getenv("XKB_DEFAULT_LAYOUT")) && *s) + seat->names.layout = s; + if ((s = getenv("XKB_DEFAULT_VARIANT")) && *s) + seat->names.variant = s; + if ((s = getenv("XKB_DEFAULT_OPTIONS")) && *s) + seat->names.options = s; + } + seat->global = wl_global_create(display, &wl_seat_interface, 5, seat, &bind_seat); if (!seat->global) { diff --git a/libswc/xserver.c b/libswc/xserver.c @@ -274,8 +274,9 @@ xserver_initialize(void) /* Unset the FD_CLOEXEC flag on the FDs that will get passed to * Xwayland. */ for (index = 0; index < ARRAY_LENGTH(fds); ++index) { -#if defined(__FreeBSD__) - if (fds[index]==-1) continue; +#if defined(__FreeBSD__) || defined(__OpenBSD__) + if (fds[index] == -1) + continue; #endif if (fcntl(fds[index], F_SETFD, 0) != 0) { ERROR("fcntl() failed: %s\n", strerror(errno));