commit 86a646272b2fde5426e5822c23c43a15d376ff0c
parent 39f919872386a9c9464f99ecff47dd7741bc8ed5
Author: uint <abhinav.prsai@gmail.com>
Date: Thu, 5 Feb 2026 22:33:04 +0000
OpenBSD patches
Tested and working with Hevel
NOTES:
- changed signature of swc_mremap
- using regular cp instead of objcpy for openbsd
- ...
Diffstat:
6 files changed, 246 insertions(+), 17 deletions(-)
diff --git a/Makefile b/Makefile
@@ -159,6 +159,8 @@ cursor/convert_font.o: cursor/convert_font.c
LAUNCH_DEVMAJOR=launch/devmajor-linux.c
.if ${UNAME} == "NetBSD"
LAUNCH_DEVMAJOR=launch/devmajor-netbsd.c
+.elif ${UNAME} == "OpenBSD"
+LAUNCH_DEVMAJOR=launch/devmajor-openbsd.c
.endif
launch/swc-launch: launch/launch.o launch/protocol.o launch/${LAUNCH_DEVMAJOR:T:R}.o
@@ -244,7 +246,11 @@ libswc/libswc-internal.o: ${SWC_OBJECTS}
libswc/libswc.o: libswc/libswc-internal.o
@echo " OBJCOPY $@"
+.if ${UNAME} == "OpenBSD"
+ @cp ${.ALLSRC} ${.TARGET}
+.else
@${OBJCOPY} --localize-hidden ${.ALLSRC} ${.TARGET}
+.endif
libswc/libswc.a: libswc/libswc.o
@echo " AR $@"
diff --git a/launch/devmajor-openbsd.c b/launch/devmajor-openbsd.c
@@ -0,0 +1,74 @@
+/* swc: launch/devmajor-openbsd.c
+ *
+ * Copyright (c) 2020 Nia Alarie
+ * Copyright (c) 2026 uint
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <string.h>
+#include "devmajor.h"
+
+static bool
+devname_is(dev_t rdev, const char *prefix)
+{
+ const char *name;
+ size_t len;
+
+ name = devname(rdev, S_IFCHR);
+ if (!name || name[0] == '?' || name[1] == '?')
+ return false;
+
+ len = strlen(prefix);
+ return strncmp(name, prefix, len) == 0;
+}
+
+bool
+device_is_input(dev_t rdev)
+{
+ if (devname_is(rdev, "wskbd"))
+ return true;
+ if (devname_is(rdev, "wsmouse"))
+ return true;
+ if (devname_is(rdev, "wsmux"))
+ return true;
+ return false;
+}
+
+bool
+device_is_tty(dev_t rdev)
+{
+ return devname_is(rdev, "ttyC");
+}
+
+bool
+device_is_drm(dev_t rdev)
+{
+ const char *n;
+
+ n = devname(rdev, S_IFCHR);
+ if (!n)
+ return false;
+
+ return
+ strncmp(n, "drm", 3) == 0 ||
+ strncmp(n, "dri/card", 8) == 0 ||
+ strncmp(n, "dri/renderD", 11) == 0;
+}
diff --git a/launch/launch.c b/launch/launch.c
@@ -46,20 +46,32 @@
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/types.h>
-#ifndef minor
+#ifdef __linux__
#include <sys/sysmacros.h>
#endif
-#ifdef __NetBSD__
+
+#if defined(__NetBSD__)
#include <dev/wscons/wsdisplay_usl_io.h>
-#else
+
+#elif defined(__OpenBSD__)
+#include <dev/wscons/wsdisplay_usl_io.h>
+
+#elif defined(__linux__)
#include <linux/input.h>
#include <linux/kd.h>
#include <linux/vt.h>
+
+#else
+#include <dev/wscons/wsdisplay_usl_io.h>
#endif
+
#include <xf86drm.h>
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0])
+static void activate(void);
+static void deactivate(void);
+
static bool nflag;
static int sigfd[2], sock[2];
static int input_fds[128], num_input_fds;
@@ -132,20 +144,27 @@ stop_devices(bool fatal)
static void
cleanup(void)
{
+#ifndef __OpenBSD__
struct vt_mode mode = {.mode = VT_AUTO};
+#endif
if (!original_vt_state.altered)
return;
+ /* Stop devices before switching the VT to make sure we have released the DRM
+ * device before the next session tries to claim it. */
+ stop_devices(false);
+
/* Cleanup VT */
+#ifndef __OpenBSD__
ioctl(tty_fd, VT_SETMODE, &mode);
- ioctl(tty_fd, KDSETMODE, original_vt_state.console_mode);
ioctl(tty_fd, KDSKBMODE, original_vt_state.kb_mode);
+#endif
+ ioctl(tty_fd, KDSETMODE, original_vt_state.console_mode);
- /* Stop devices before switching the VT to make sure we have released the DRM
- * device before the next session tries to claim it. */
- stop_devices(false);
+#ifndef __OpenBSD__
ioctl(tty_fd, VT_ACTIVATE, original_vt_state.vt);
+#endif
kill(0, SIGTERM);
}
@@ -268,9 +287,16 @@ done:
static void
find_vt(char *vt, size_t size)
{
-#ifdef __NetBSD__
+#if defined(__NetBSD__)
if (snprintf(vt, size, "/dev/ttyE1") >= size)
die("VT number is too large");
+#elif defined(__OpenBSD__)
+ const char *tty;
+ tty = ttyname(STDIN_FILENO);
+ if (!tty || strncmp(tty, "/dev/ttyC", 8) != 0)
+ die("must be run from wscons VT (/dev/ttyC*)");
+ if (snprintf(vt, size, "%s", tty) >= size)
+ die("VT number is too large");
#else
char *vtnr;
int tty0_fd, vt_num;
@@ -315,23 +341,40 @@ setup_tty(int fd)
{
struct stat st;
int vt;
+#ifndef __OpenBSD__
struct vt_stat state;
struct vt_mode mode = {
.mode = VT_PROCESS,
.relsig = SIGUSR1,
.acqsig = SIGUSR2
};
+#endif
if (fstat(fd, &st) == -1)
die("failed to stat TTY fd:");
vt = minor(st.st_rdev);
+#ifdef __OpenBSD__
+ if (!device_is_tty(st.st_rdev))
+ die("not a valid VT");
+#else
if (!device_is_tty(st.st_rdev) || vt == 0)
die("not a valid VT");
+#endif
+#ifdef __OpenBSD__
+ /* OpenBSD wscons has no VT_GETSTATE */
+#else
if (ioctl(fd, VT_GETSTATE, &state) == -1)
die("failed to get the current VT state:");
+#endif
+
+#ifndef __OpenBSD__
original_vt_state.vt = state.v_active;
+#else
+ original_vt_state.vt = vt;
+#endif
+
#ifdef KDGETMODE
if (ioctl(fd, KDGKBMODE, &original_vt_state.kb_mode))
die("failed to get keyboard mode:");
@@ -347,14 +390,40 @@ setup_tty(int fd)
die("failed to set keyboard mode to K_OFF:");
#endif
if (ioctl(fd, KDSETMODE, KD_GRAPHICS) == -1) {
- perror("failed to set console mode to KD_GRAPHICS");
+ perror("KDSETMODE KD_GRAPHICS");
goto error0;
}
+
+#ifndef __OpenBSD__
if (ioctl(fd, VT_SETMODE, &mode) == -1) {
perror("failed to set VT mode");
goto error1;
}
+#endif
+
+#ifdef __OpenBSD__
+ /* OpenBSD wscons has no VT_PROCESS mode; just cont. on current ttyC* */
+ activate();
+#else
+ if (vt == original_vt_state.vt) {
+ activate();
+ } else if (!nflag) {
+ if (ioctl(fd, VT_ACTIVATE, vt) == -1) {
+ perror("failed to activate VT");
+ goto error2;
+ }
+
+ if (ioctl(fd, VT_WAITACTIVE, vt) == -1) {
+ perror("failed to wait for VT to become active");
+ goto error2;
+ }
+ }
+#endif
+#ifdef __OpenBSD__
+ /* OpenBSD wscons already on active VT */
+ activate();
+#else
if (vt == original_vt_state.vt) {
activate();
} else if (!nflag) {
@@ -368,18 +437,21 @@ setup_tty(int fd)
goto error2;
}
}
+#endif
original_vt_state.altered = true;
return;
+#ifndef __OpenBSD__
error2:
mode = (struct vt_mode){.mode = VT_AUTO };
ioctl(fd, VT_SETMODE, &mode);
error1:
- ioctl(fd, KDSETMODE, original_vt_state.console_mode);
-error0:
ioctl(fd, KDSKBMODE, original_vt_state.kb_mode);
+#endif
+error0:
+ ioctl(fd, KDSETMODE, original_vt_state.console_mode);
exit(EXIT_FAILURE);
}
@@ -483,6 +555,16 @@ main(int argc, char *argv[])
sprintf(buf, "%d", sock[1]);
setenv(SWC_LAUNCH_SOCKET_ENV, buf, 1);
+ /* make sure XDG_RUNTIME_DIR is set */
+ if (!getenv("XDG_RUNTIME_DIR")) {
+ uid_t uid = getuid();
+ snprintf(buf, sizeof(buf), "/tmp/XDG_RUNTIME_DIR_%d", uid);
+ if (mkdir(buf, 0700) == -1 && errno != EEXIST)
+ die("mkdir %s:", buf);
+ setenv("XDG_RUNTIME_DIR", buf, 1);
+ fprintf(stderr, "set XDG_RUNTIME_DIR=%s\n", buf);
+ }
+
if ((errno = posix_spawnattr_init(&attr)))
die("posix_spawnattr_init:");
if ((errno = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RESETIDS|POSIX_SPAWN_SETSIGMASK)))
diff --git a/libswc/keyboard.c b/libswc/keyboard.c
@@ -150,11 +150,20 @@ update_keymap(struct xkb *xkb)
unlink(keymap_path);
+#ifdef __linux__
+ /* preallocate if available */
if (posix_fallocate(xkb->keymap.fd, 0, xkb->keymap.size) != 0 &&
ftruncate(xkb->keymap.fd, xkb->keymap.size) != 0) {
WARNING("Could not resize XKB keymap file\n");
goto error2;
}
+#else
+ /* otherwise, ftruncate() is fine */
+ if (ftruncate(xkb->keymap.fd, xkb->keymap.size) != 0) {
+ WARNING("Could not resize XKB keymap file\n");
+ goto error2;
+ }
+#endif
xkb->keymap.area = mmap(NULL, xkb->keymap.size, PROT_READ | PROT_WRITE, MAP_SHARED, xkb->keymap.fd, 0);
diff --git a/libswc/seat-ws.c b/libswc/seat-ws.c
@@ -40,6 +40,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
@@ -56,11 +57,15 @@ struct ws_xkb_map {
static const struct ws_xkb_map ws_xkb_encodings[] = {
{ KB_UK, "gb" },
{ KB_BE, "be" },
+#ifdef KB_CZ
{ KB_CZ, "cz" },
+#endif
{ KB_DK, "dk" },
{ KB_NL, "nl" },
{ KB_DE, "de" },
+#ifdef KB_GR
{ KB_GR, "gr" },
+#endif
{ KB_HU, "hu" },
{ KB_IT, "it" },
{ KB_JP, "jp" },
@@ -207,8 +212,11 @@ ws_to_xkb(unsigned type, int key)
case WSKBD_TYPE_PC_AT:
return wsXtMap[key];
case WSKBD_TYPE_USB:
+#ifdef WSKBD_TYPE_MAPLE
case WSKBD_TYPE_MAPLE:
return wsUsbMap[key];
+#endif
+ return wsUsbMap[key];
default:
fprintf(stderr, "Unknown wskbd type %d\n", type);
return key;
@@ -296,23 +304,54 @@ handle_ws_data(int fd, uint32_t mask, void *data)
static bool
initialize_wscons(struct seat *seat)
{
+#ifdef WSMOUSE_EVENT_VERSION
int mouse_ver = WSMOUSE_EVENT_VERSION;
+#endif
+#ifdef WSKBDIO_EVENT_VERSION
int kbd_ver = WSKBDIO_EVENT_VERSION;
+#endif
int encoding_layout;
kbd_t encoding;
unsigned i;
- if ((seat->mouse_fd = launch_open_device("/dev/wsmouse", O_RDONLY | O_NONBLOCK)) == -1) {
+#ifdef WSMOUSE_EVENT_VERSION
+ int mouse_ver = WSMOUSE_EVENT_VERSION;
+#endif
+#ifdef WSKBDIO_EVENT_VERSION
+ int kbd_ver = WSKBDIO_EVENT_VERSION;
+#endif
+
+ if ((seat->mouse_fd = launch_open_device("/dev/wsmouse", O_RDWR | O_NONBLOCK)) == -1) {
ERROR("Could not open mouse device\n");
goto error0;
}
- if ((seat->kbd_fd = launch_open_device("/dev/wskbd", O_RDONLY | O_NONBLOCK)) == -1) {
+ if ((seat->kbd_fd = launch_open_device("/dev/wskbd", O_RDWR | O_NONBLOCK)) == -1) {
ERROR("Could not open keyboard device\n");
goto error1;
}
+#ifdef WSMOUSEIO_SETVERSION
(void)ioctl(seat->mouse_fd, WSMOUSEIO_SETVERSION, &mouse_ver);
+#endif
+#ifdef WSKBDIO_SETVERSION
(void)ioctl(seat->kbd_fd, WSKBDIO_SETVERSION, &kbd_ver);
+#endif
+
+ /* set devices to nativemode to receive events */
+#ifdef WSMOUSEIO_SETMODE
+ {
+ int mode = WSMOUSE_COMPAT; /* use compat mode; it sends events */
+ if (ioctl(seat->mouse_fd, WSMOUSEIO_SETMODE, &mode) == -1)
+ fprintf(stderr, "wscons: WSMOUSEIO_SETMODE failed: %s\n", strerror(errno));
+ }
+#endif /* WSMOUSEIO_SETMODE */
+#ifdef WSKBDIO_SETMODE
+ {
+ int mode = WSKBD_TRANSLATED; /* use translated mode for key events */
+ if (ioctl(seat->kbd_fd, WSKBDIO_SETMODE, &mode) == -1)
+ fprintf(stderr, "wscons: WSKBDIO_SETMODE failed: %s\n", strerror(errno));
+ }
+#endif /* WSKBDIO_SETMODE */
if (ioctl(seat->kbd_fd, WSKBDIO_GTYPE, &seat->kbd_type) == -1) {
ERROR("Could not get keyboard type\n");
@@ -361,6 +400,7 @@ seat_create(struct wl_display *display, const char *seat_name)
if (!seat)
goto error0;
+ seat->ignore = false;
memset(&seat->names, 0, sizeof(seat->names));
seat->names.rules = "base";
seat->names.model = "pc105";
diff --git a/libswc/shm.c b/libswc/shm.c
@@ -32,6 +32,7 @@
#include <errno.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-server.h>
@@ -43,6 +44,7 @@ struct pool {
struct swc_shm *shm;
void *data;
uint32_t size;
+ int fd;
unsigned references;
};
@@ -52,12 +54,21 @@ struct pool_reference {
};
static void *
-swc_mremap(void *oldp, size_t oldsize, size_t newsize)
+swc_mremap(struct pool *pool, void *oldp, size_t oldsize, size_t newsize)
{
#ifdef __NetBSD__
return mremap(oldp, oldsize, NULL, newsize, 0);
-#else /* Linux-style mremap */
+#elif defined(__linux__)
return mremap(oldp, oldsize, newsize, MREMAP_MAYMOVE);
+#else
+ void *newp;
+
+ newp = mmap(NULL, newsize, PROT_READ, MAP_SHARED, pool->fd, 0);
+ if (newp == MAP_FAILED)
+ return MAP_FAILED;
+
+ (void)munmap(oldp, oldsize);
+ return newp;
#endif
}
@@ -68,6 +79,7 @@ unref_pool(struct pool *pool)
return;
munmap(pool->data, pool->size);
+ close(pool->fd);
free(pool);
}
@@ -148,7 +160,12 @@ resize(struct wl_client *client, struct wl_resource *resource, int32_t size)
struct pool *pool = wl_resource_get_user_data(resource);
void *data;
- data = swc_mremap(pool->data, pool->size, size);
+ if (ftruncate(pool->fd, size) != 0) {
+ wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "ftruncate failed: %s", strerror(errno));
+ return;
+ }
+
+ data = swc_mremap(pool, pool->data, pool->size, size);
if (data == MAP_FAILED) {
wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "mremap failed: %s", strerror(errno));
return;
@@ -186,9 +203,10 @@ create_pool(struct wl_client *client, struct wl_resource *resource, uint32_t id,
wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "mmap failed: %s", strerror(errno));
goto error2;
}
- close(fd);
+ /* close(fd); */
pool->size = size;
pool->references = 1;
+ pool->fd = fd;
return;
error2: