From 53a20908fa1033dca9fd6761b8e7481965460e5d Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Wed, 14 Jan 2026 15:29:06 +0100 Subject: [PATCH] xfree86: os-support: bsd: factor out console close handling * move console type specific close logic into separate functions * install them into xf86_console_proc_close on init Signed-off-by: Enrico Weigelt, metux IT consult --- hw/xfree86/os-support/bsd/bsd_init.c | 46 ++++----------------- hw/xfree86/os-support/bsd/console_pcvt.c | 43 +++++++++++++++++++ hw/xfree86/os-support/bsd/console_syscons.c | 38 +++++++++++++++++ hw/xfree86/os-support/bsd/console_wscons.c | 21 ++++++++++ hw/xfree86/os-support/bsd/xf86_bsd_priv.h | 11 +++++ hw/xfree86/os-support/meson.build | 5 ++- 6 files changed, 126 insertions(+), 38 deletions(-) create mode 100644 hw/xfree86/os-support/bsd/console_pcvt.c create mode 100644 hw/xfree86/os-support/bsd/console_syscons.c create mode 100644 hw/xfree86/os-support/bsd/console_wscons.c diff --git a/hw/xfree86/os-support/bsd/bsd_init.c b/hw/xfree86/os-support/bsd/bsd_init.c index e18a428282..f2f76fff82 100644 --- a/hw/xfree86/os-support/bsd/bsd_init.c +++ b/hw/xfree86/os-support/bsd/bsd_init.c @@ -47,7 +47,7 @@ static Bool KeepTty = FALSE; #if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT) -static int initialVT = -1; +int initialVT = -1; #endif #ifdef SYSCONS_SUPPORT @@ -342,6 +342,7 @@ static bool xf86OpenSyscons(void) } xf86Info.consoleFd = fd; xf86_bsd_acquire_vt(); + xf86_console_proc_close = xf86_console_syscons_close; return (fd > 0); } @@ -463,6 +464,7 @@ static bool xf86OpenPcvt(void) goto out; out: xf86_bsd_acquire_vt(); + xf86_console_proc_close = xf86_console_pcvt_close; return (fd > 0); } @@ -497,6 +499,8 @@ static bool xf86OpenWScons(void) } xf86Info.consoleFd = fd; + xf86_console_proc_close = xf86_console_wscons_close; + /* nothing special to do for acquiring the VT */ return (fd > 0); } @@ -506,45 +510,13 @@ static bool xf86OpenWScons(void) void xf86CloseConsole(void) { -#if defined(SYSCONS_SUPPORT) || defined(PCVT_SUPPORT) - struct vt_mode VT; -#endif - if (xf86Info.ShareVTs) return; - switch (xf86Info.consType) { -#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT) - case SYSCONS: - case PCVT: - ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT); /* Back to text mode */ - if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) != -1) { - VT.mode = VT_AUTO; - ioctl(xf86Info.consoleFd, VT_SETMODE, &VT); /* dflt vt handling */ - } -#if !defined(__OpenBSD__) && !defined(USE_DEV_IO) && !defined(USE_I386_IOPL) - if (ioctl(xf86Info.consoleFd, KDDISABIO, 0) < 0) { - xf86FatalError("xf86CloseConsole: KDDISABIO failed (%s)", - strerror(errno)); - } -#endif - if (xf86Info.autoVTSwitch && initialVT != -1) - ioctl(xf86Info.consoleFd, VT_ACTIVATE, initialVT); - break; -#endif /* SYSCONS_SUPPORT || PCVT_SUPPORT */ -#ifdef WSCONS_SUPPORT - case WSCONS: - { - int mode = WSDISPLAYIO_MODE_EMUL; - - ioctl(xf86Info.consoleFd, WSDISPLAYIO_SMODE, &mode); - break; - } -#endif - } - - close(xf86Info.consoleFd); - return; + if (xf86_console_proc_close) + xf86_console_proc_close(); + else + LogMessageVerb(X_WARNING, 1, "no xf86_console_proc_close() installed\n"); } int diff --git a/hw/xfree86/os-support/bsd/console_pcvt.c b/hw/xfree86/os-support/bsd/console_pcvt.c new file mode 100644 index 0000000000..9e6c77a8a0 --- /dev/null +++ b/hw/xfree86/os-support/bsd/console_pcvt.c @@ -0,0 +1,43 @@ +#include + +#if defined(PCVT_SUPPORT) + +#include +#include +#include +#include + +#if defined(__NetBSD__) +#include +#endif + +#if defined(__FreeBSD__) || defined(__DragonFly__) +#include +#include +#endif + +#include "xf86Priv.h" +#include "xf86_bsd_priv.h" +#include "xf86_console_priv.h" + +void xf86_console_pcvt_close(void) +{ + struct vt_mode VT = { 0 }; + ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT); /* Back to text mode */ + if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) != -1) { + VT.mode = VT_AUTO; + ioctl(xf86Info.consoleFd, VT_SETMODE, &VT); /* dflt vt handling */ + } +#if !defined(__OpenBSD__) && !defined(USE_DEV_IO) && !defined(USE_I386_IOPL) + if (ioctl(xf86Info.consoleFd, KDDISABIO, 0) < 0) { + FatalError("xf86CloseConsole: KDDISABIO failed (%s)", strerror(errno)); + } +#endif + if (xf86Info.autoVTSwitch && initialVT != -1) + ioctl(xf86Info.consoleFd, VT_ACTIVATE, initialVT); + + close(xf86Info.consoleFd); + xf86Info.consoleFd = -1; +} + +#endif /* PCVT_SUPPORT */ diff --git a/hw/xfree86/os-support/bsd/console_syscons.c b/hw/xfree86/os-support/bsd/console_syscons.c new file mode 100644 index 0000000000..17fd8035c8 --- /dev/null +++ b/hw/xfree86/os-support/bsd/console_syscons.c @@ -0,0 +1,38 @@ +#include + +#if defined(SYSCONS_SUPPORT) + +#if defined(__FreeBSD__) || defined(__DragonFly__) +#include +#include +#endif + +#include +#include +#include + +#include "xf86Priv.h" +#include "xf86_bsd_priv.h" + +void xf86_console_syscons_close(void) +{ + struct vt_mode VT = { 0 }; + ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT); /* Back to text mode */ + if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) != -1) { + VT.mode = VT_AUTO; + ioctl(xf86Info.consoleFd, VT_SETMODE, &VT); /* dflt vt handling */ + } +#if !defined(__OpenBSD__) && !defined(USE_DEV_IO) && !defined(USE_I386_IOPL) + if (ioctl(xf86Info.consoleFd, KDDISABIO, 0) < 0) { + xf86FatalError("xf86CloseConsole: KDDISABIO failed (%s)", + strerror(errno)); + } +#endif + if (xf86Info.autoVTSwitch && initialVT != -1) + ioctl(xf86Info.consoleFd, VT_ACTIVATE, initialVT); + + close(xf86Info.consoleFd); + xf86Info.consoleFd = -1; +} + +#endif /* SYSCONS_SUPPORT */ diff --git a/hw/xfree86/os-support/bsd/console_wscons.c b/hw/xfree86/os-support/bsd/console_wscons.c new file mode 100644 index 0000000000..6e62573415 --- /dev/null +++ b/hw/xfree86/os-support/bsd/console_wscons.c @@ -0,0 +1,21 @@ +#include + +#if defined(WSCONS_SUPPORT) + +#include +#include +#include + +#include "xf86Priv.h" +#include "xf86_bsd_priv.h" + +void xf86_console_wscons_close(void) +{ + int mode = WSDISPLAYIO_MODE_EMUL; + ioctl(xf86Info.consoleFd, WSDISPLAYIO_SMODE, &mode); + + close(xf86Info.consoleFd); + xf86Info.consoleFd = -1; +} + +#endif /* WSCONS_SUPPORT */ diff --git a/hw/xfree86/os-support/bsd/xf86_bsd_priv.h b/hw/xfree86/os-support/bsd/xf86_bsd_priv.h index b6fae7adc1..67f231b35e 100644 --- a/hw/xfree86/os-support/bsd/xf86_bsd_priv.h +++ b/hw/xfree86/os-support/bsd/xf86_bsd_priv.h @@ -21,4 +21,15 @@ void xf86_bsd_acquire_vt(void); +/* PCVT console driver */ +void xf86_console_pcvt_close(void); + +/* SYSCONS console driver */ +void xf86_console_syscons_close(void); + +/* WSCONS console driver */ +void xf86_console_wscons_close(void); + +extern int initialVT; + #endif /* _XSERVER_XFREE86_OS_SUPPORT_BSD_PRIV_H */ diff --git a/hw/xfree86/os-support/meson.build b/hw/xfree86/os-support/meson.build index 18bcc8df6b..dfd6ce1aba 100644 --- a/hw/xfree86/os-support/meson.build +++ b/hw/xfree86/os-support/meson.build @@ -98,8 +98,11 @@ elif host_machine.system().endswith('bsd') or host_machine.system() == 'dragonfl 'bsd/bsd_VTsw.c', 'bsd/bsd_bell.c', 'bsd/bsd_init.c', + 'bsd/console_pcvt.c', + 'bsd/console_syscons.c', + 'bsd/console_wscons.c', 'shared/drm_platform.c', - 'shared/pm_noop.c' + 'shared/pm_noop.c' ] if host_machine.cpu_family() == 'x86_64'