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..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)) }; @@ -3771,6 +3774,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/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 { 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() {