From ee91e6c00e8934b27343e357b70b2dc44b0225ec Mon Sep 17 00:00:00 2001 From: wrothgaril <211408787+wrothgaril@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:39:18 +0100 Subject: [PATCH 1/2] Host: put our windows on the interactive desktop Started from another desktop (a sandbox's own, say), our windows landed where nobody sees them. The process now switches to the WinSta0 window station and each window-making thread (main, the host window thread, the stand-in thread, title threads at CreateWindowEx) to its Default desktop before its first window. Failures leave things as they were. Co-Authored-By: Claude Opus 5.5 --- apps/victory/src/main.rs | 3 + crates/victory-gpu/Cargo.toml | 2 +- crates/victory-gpu/src/desktop.rs | 61 +++++++++++++++++++++ crates/victory-gpu/src/host.rs | 1 + crates/victory-gpu/src/lib.rs | 2 + crates/victory-hle/src/modules/lifecycle.rs | 3 + 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 crates/victory-gpu/src/desktop.rs diff --git a/apps/victory/src/main.rs b/apps/victory/src/main.rs index 152414f0187..6447cb20470 100644 --- a/apps/victory/src/main.rs +++ b/apps/victory/src/main.rs @@ -148,6 +148,9 @@ enum Console { fn main() -> Result<()> { victory_core::logging::init("info"); + // Our windows go to the interactive desktop even when started elsewhere. + #[cfg(windows)] + victory_gpu::desktop::attach(); // The audio recorder `run` starts for `VICTORY_AUDIO_CAPTURE`. #[cfg(all(windows, feature = "diagnostics"))] if victory_hle::modules::audio::capture::child() { diff --git a/crates/victory-gpu/Cargo.toml b/crates/victory-gpu/Cargo.toml index f1cb0d6e893..d4ebb025d82 100644 --- a/crates/victory-gpu/Cargo.toml +++ b/crates/victory-gpu/Cargo.toml @@ -27,7 +27,7 @@ diagnostics = [] workspace = true [target.'cfg(windows)'.dependencies] -windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Security", "Win32_System_Diagnostics_Debug", "Win32_System_Kernel", "Win32_System_LibraryLoader", "Win32_System_Memory", "Win32_System_Performance", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_System_SystemInformation", "Win32_System_Threading"] } +windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Security", "Win32_System_Diagnostics_Debug", "Win32_System_Kernel", "Win32_System_LibraryLoader", "Win32_System_Memory", "Win32_System_Performance", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_System_StationsAndDesktops", "Win32_System_SystemInformation", "Win32_System_Threading"] } windows = { workspace = true, features = [ "Win32_Graphics_Direct3D", "Win32_Graphics_Direct3D_Fxc", diff --git a/crates/victory-gpu/src/desktop.rs b/crates/victory-gpu/src/desktop.rs new file mode 100644 index 00000000000..edd979b94cb --- /dev/null +++ b/crates/victory-gpu/src/desktop.rs @@ -0,0 +1,61 @@ +//! The interactive desktop. Started from another desktop (a sandbox's own, +//! say), our windows would land where nobody sees them. [`attach`] moves the +//! process to the interactive window station (`WinSta0`) and the calling +//! thread to its `Default` desktop. A thread's desktop is its own, so every +//! thread that makes windows calls it before its first one; on a thread that +//! already has windows, or when either object can't be opened, nothing +//! changes. + +use std::cell::Cell; +use std::sync::OnceLock; + +use windows_sys::Win32::Foundation::GENERIC_ALL; +use windows_sys::Win32::System::StationsAndDesktops::{ + HDESK, OpenDesktopW, OpenWindowStationW, SetProcessWindowStation, SetThreadDesktop, +}; +use windows_sys::core::w; + +/// `WINSTA_ALL_ACCESS`. +const WINSTA_ALL_ACCESS: u32 = 0x37f; + +/// The `Default` desktop, opened once (0 if it couldn't be). Kept open for +/// the life of the process: threads keep using it. +static DESKTOP: OnceLock = OnceLock::new(); + +thread_local! { + /// Whether this thread has been attached. + static ATTACHED: Cell = const { Cell::new(false) }; +} + +fn desktop() -> HDESK { + *DESKTOP.get_or_init(|| { + // SAFETY: plain calls with literal names; the handles stay open. + unsafe { + let station = OpenWindowStationW(w!("WinSta0"), 0, WINSTA_ALL_ACCESS); + if !station.is_null() && SetProcessWindowStation(station) == 0 { + tracing::debug!("could not switch to the interactive window station"); + } + let desktop = OpenDesktopW(w!("Default"), 0, 0, GENERIC_ALL); + if desktop.is_null() { + tracing::debug!("could not open the default desktop"); + } + desktop as usize + } + }) as HDESK +} + +/// Attaches the process to `WinSta0` and the calling thread to its +/// `Default` desktop (once per thread). +pub fn attach() { + if ATTACHED.replace(true) { + return; + } + let desktop = desktop(); + if desktop.is_null() { + return; + } + // SAFETY: a desktop handle that stays open for the process's life. + if unsafe { SetThreadDesktop(desktop) } == 0 { + tracing::debug!("this thread stays on its own desktop"); + } +} diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index c09d19f6e3c..db2d0ea00e0 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -3771,6 +3771,7 @@ fn window(width: u32, height: u32) -> windows::core::Result { std::thread::Builder::new() .name("victory-window".into()) .spawn(move || { + crate::desktop::attach(); let made = make_window(width, height); let ok = made.is_ok(); let _ = sender.send(made.map(|h| h.0 as usize)); diff --git a/crates/victory-gpu/src/lib.rs b/crates/victory-gpu/src/lib.rs index 66c3d84fc46..57826f2f880 100644 --- a/crates/victory-gpu/src/lib.rs +++ b/crates/victory-gpu/src/lib.rs @@ -24,6 +24,8 @@ pub mod console; #[cfg(all(windows, feature = "diagnostics"))] mod copystat; pub mod descriptor; +#[cfg(windows)] +pub mod desktop; pub mod device; #[cfg(windows)] mod draw; diff --git a/crates/victory-hle/src/modules/lifecycle.rs b/crates/victory-hle/src/modules/lifecycle.rs index 193d513a733..b7596b5aa09 100644 --- a/crates/victory-hle/src/modules/lifecycle.rs +++ b/crates/victory-hle/src/modules/lifecycle.rs @@ -124,6 +124,7 @@ pub extern "system" fn CreateWindowExW( instance: HINSTANCE, param: *const c_void, ) -> HWND { + victory_gpu::desktop::attach(); let top = parent.is_null(); // SAFETY: the title's own arguments, forwarded (a top-level window // made message-only). @@ -165,6 +166,7 @@ pub extern "system" fn CreateWindowExA( instance: HINSTANCE, param: *const c_void, ) -> HWND { + victory_gpu::desktop::attach(); let top = parent.is_null(); // SAFETY: the title's own arguments, forwarded (a top-level window // made message-only). @@ -290,6 +292,7 @@ fn stand_in(title: HWND, ex_style: u32, style: u32, x: i32, y: i32, width: i32, let spawned = std::thread::Builder::new() .name("victory-window".into()) .spawn(move || { + victory_gpu::desktop::attach(); let window = make_stand_in(&text, ex_style, style, (x, y, width, height), icon); let _ = sender.send(window as usize); if !window.is_null() { From 96ffa5f36dfbfb2d7f354d66a558e0e5c124f1f9 Mon Sep 17 00:00:00 2001 From: wrothgaril <211408787+wrothgaril@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:47:05 +0100 Subject: [PATCH 2/2] Host: look for windows and read keys on the interactive desktop EnumWindows, GetForegroundWindow and GetAsyncKeyState answer only for the calling thread's desktop. Started elsewhere, the presenting thread missed the title's stand-in and made a second window, and the input thread saw no focus and no keys. Both threads now attach to the Default desktop too. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/host.rs | 3 +++ crates/victory-hle/src/modules/gameinput.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index db2d0ea00e0..a00eef42e22 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -3324,6 +3324,9 @@ fn title_window() -> Option { } true.into() } + // `EnumWindows` sees only the calling thread's desktop: the one the + // title's windows are on. + crate::desktop::attach(); let mut found: Option = None; // SAFETY: the callback only writes `found`, which outlives the call. let _ = unsafe { EnumWindows(Some(visit), LPARAM(&raw mut found as isize)) }; diff --git a/crates/victory-hle/src/modules/gameinput.rs b/crates/victory-hle/src/modules/gameinput.rs index 2e7115d6fcf..417d13cf9cc 100644 --- a/crates/victory-hle/src/modules/gameinput.rs +++ b/crates/victory-hle/src/modules/gameinput.rs @@ -1287,6 +1287,9 @@ mod host { /// The foreground window is one of ours (the title's or our fallback). fn focused() -> bool { + // The foreground window and `GetAsyncKeyState` answer only threads + // on the interactive desktop, where our windows are. + victory_gpu::desktop::attach(); let mut process = 0; // SAFETY: plain queries; a null window gives process 0. unsafe {