Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/victory/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion crates/victory-gpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 61 additions & 0 deletions crates/victory-gpu/src/desktop.rs
Original file line number Diff line number Diff line change
@@ -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<usize> = OnceLock::new();

thread_local! {
/// Whether this thread has been attached.
static ATTACHED: Cell<bool> = 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");
}
}
4 changes: 4 additions & 0 deletions crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3324,6 +3324,9 @@ fn title_window() -> Option<HWND> {
}
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<HWND> = None;
// SAFETY: the callback only writes `found`, which outlives the call.
let _ = unsafe { EnumWindows(Some(visit), LPARAM(&raw mut found as isize)) };
Expand Down Expand Up @@ -3771,6 +3774,7 @@ fn window(width: u32, height: u32) -> windows::core::Result<HWND> {
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));
Expand Down
2 changes: 2 additions & 0 deletions crates/victory-gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions crates/victory-hle/src/modules/gameinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions crates/victory-hle/src/modules/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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() {
Expand Down