From 229708a71fe6d3fb5e46fd1de97b1b055855b776 Mon Sep 17 00:00:00 2001 From: ryosen0 Date: Mon, 23 Nov 2026 21:58:42 +0300 Subject: [PATCH] =?UTF-8?q?XCL=20window:=20window=5Fpos=20setting=20?= =?UTF-8?q?=E2=80=93=20center=20on=20the=20primary=20monitor=20or=20a=20cl?= =?UTF-8?q?amped=20x,y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The window was created with CW_USEDEFAULT for its position, so on a multi-monitor desktop Windows may place it on any monitor (observed: a 4-monitor setup, the window landed on the narrow portrait side monitor). window_pos (XCL_WINDOW_POS overrides): auto keeps the old behavior, center centers on the primary work area, x,y clamps into the nearest monitor work area. Test: window_position_modes covers all modes, the clamp, and the env override. --- crates/xcl/src/config.rs | 5 ++ crates/xcl/src/hle/d3d12x/present.rs | 92 ++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/crates/xcl/src/config.rs b/crates/xcl/src/config.rs index 0f5f4e8..840f113 100644 --- a/crates/xcl/src/config.rs +++ b/crates/xcl/src/config.rs @@ -56,6 +56,10 @@ pub struct Config { pub raytracing: String, pub window_width: u32, pub window_height: u32, + /// The XCL window's position: "auto" (CW_USEDEFAULT – Windows may place it on any monitor), + /// "center" (the primary monitor's work area), or "x,y" (top-left of the window, screen + /// coordinates; clamped into the nearest monitor's work area). `XCL_WINDOW_POS` overrides. + pub window_pos: String, /// Answer `XUserGetTokenAndSignature*` with XCL's local placeholder token (for a backend of /// your own that accepts it). Off: the request fails as on a console that cannot reach Xbox /// Live (0x80072EFD), and titles go their offline way – Xbox Live's and the titles' own @@ -169,6 +173,7 @@ impl Default for Config { raytracing: "auto".into(), window_width: 1280, window_height: 720, + window_pos: "auto".into(), xbox_live_tokens: false, keyboard_mouse: true, mouse_sensitivity: 1.0, diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs index 81a53f4..06b1eea 100644 --- a/crates/xcl/src/hle/d3d12x/present.rs +++ b/crates/xcl/src/hle/d3d12x/present.rs @@ -22,10 +22,11 @@ use std::sync::{Mutex, Once}; use std::time::{Duration, Instant}; use windows::core::{Interface, HSTRING, PCWSTR}; -use windows::Win32::Foundation::{CloseHandle, HANDLE, HWND, LPARAM, LRESULT, RECT, WPARAM}; +use windows::Win32::Foundation::{CloseHandle, HANDLE, HWND, LPARAM, LRESULT, POINT, RECT, WPARAM}; use windows::Win32::Graphics::Direct3D12::*; use windows::Win32::Graphics::Dxgi::Common::*; use windows::Win32::Graphics::Dxgi::*; +use windows::Win32::Graphics::Gdi::{GetMonitorInfoW, MonitorFromPoint, MONITORINFO, MONITOR_DEFAULTTONEAREST, MONITOR_DEFAULTTOPRIMARY, MONITOR_FROM_FLAGS}; use windows::Win32::System::LibraryLoader::GetModuleHandleW; use windows::Win32::System::Threading::CreateEventW; use windows::Win32::UI::WindowsAndMessaging::*; @@ -392,15 +393,17 @@ fn window_thread() { let title = window_name(); let mut rc = RECT { left: 0, top: 0, right: cfg.window_width as i32, bottom: cfg.window_height as i32 }; let _ = AdjustWindowRect(&mut rc, WS_OVERLAPPEDWINDOW, false); + let (w, h) = (rc.right - rc.left, rc.bottom - rc.top); + let (x, y) = window_position(&cfg.window_pos, w, h); let hwnd = CreateWindowExW( WINDOW_EX_STYLE(0), class, &HSTRING::from(format!("XCL – {title}")), WS_OVERLAPPEDWINDOW | WS_VISIBLE, - CW_USEDEFAULT, - CW_USEDEFAULT, - rc.right - rc.left, - rc.bottom - rc.top, + x, + y, + w, + h, None, None, Some(inst.into()), @@ -422,6 +425,45 @@ fn window_thread() { } } +/// The XCL window's top-left from `window_pos` (`XCL_WINDOW_POS` overrides): "auto" keeps +/// CW_USEDEFAULT (Windows may pick any monitor), "center" centers on the primary monitor's +/// work area, "x,y" is clamped into the nearest monitor's work area — a multi-monitor desktop +/// otherwise gets the window on whichever monitor Windows' cascade picks. +unsafe fn window_position(setting: &str, w: i32, h: i32) -> (i32, i32) { + let env = std::env::var("XCL_WINDOW_POS").ok(); + let mode = env.as_deref().unwrap_or(setting).trim(); + if mode.is_empty() || mode.eq_ignore_ascii_case("auto") { + return (CW_USEDEFAULT, CW_USEDEFAULT); + } + let work = |pt: POINT, flags: u32| -> Option { + let mon = MonitorFromPoint(pt, MONITOR_FROM_FLAGS(flags)); + if mon.is_invalid() { + return None; + } + let mut mi = MONITORINFO { cbSize: std::mem::size_of::() as u32, ..Default::default() }; + GetMonitorInfoW(mon, &mut mi).as_bool().then_some(mi.rcWork) + }; + if mode.eq_ignore_ascii_case("center") { + let Some(r) = work(POINT { x: 0, y: 0 }, MONITOR_DEFAULTTOPRIMARY.0) else { + return (CW_USEDEFAULT, CW_USEDEFAULT); + }; + return (r.left + ((r.right - r.left) - w).max(0) / 2, r.top + ((r.bottom - r.top) - h).max(0) / 2); + } + let Some((xs, ys)) = mode.split_once(',') else { + crate::xlog_warn!("window_pos: expected \"auto\", \"center\" or \"x,y\", got {mode:?} – using auto"); + return (CW_USEDEFAULT, CW_USEDEFAULT); + }; + let (Ok(x), Ok(y)) = (xs.trim().parse::(), ys.trim().parse::()) else { + crate::xlog_warn!("window_pos: expected \"auto\", \"center\" or \"x,y\", got {mode:?} – using auto"); + return (CW_USEDEFAULT, CW_USEDEFAULT); + }; + // clamp into the work area of the monitor nearest the requested top-left + let Some(r) = work(POINT { x, y }, MONITOR_DEFAULTTONEAREST.0) else { + return (x, y); + }; + (x.clamp(r.left, (r.right - w).max(r.left)), y.clamp(r.top, (r.bottom - h).max(r.top))) +} + fn ensure_window() -> Option { static START: Once = Once::new(); START.call_once(|| { @@ -887,3 +929,43 @@ impl Drop for Presenter { } } } + +#[cfg(test)] +mod tests { + use super::*; + + // XCL_WINDOW_POS is process-global; one test covers all modes in sequence. + #[test] + fn window_position_modes() { + unsafe { + std::env::remove_var("XCL_WINDOW_POS"); + assert_eq!(window_position("auto", 1280, 720), (CW_USEDEFAULT, CW_USEDEFAULT)); + assert_eq!(window_position("", 1280, 720), (CW_USEDEFAULT, CW_USEDEFAULT)); + assert_eq!(window_position("nonsense", 1280, 720), (CW_USEDEFAULT, CW_USEDEFAULT)); + assert_eq!(window_position("12,abc", 1280, 720), (CW_USEDEFAULT, CW_USEDEFAULT)); + + let (cx, cy) = window_position("center", 1280, 720); + if cx != CW_USEDEFAULT { + // centered on the primary work area: inside it, and not off its left/top + let mi = { + let mon = MonitorFromPoint(POINT { x: 0, y: 0 }, MONITOR_DEFAULTTOPRIMARY); + let mut mi = MONITORINFO { cbSize: std::mem::size_of::() as u32, ..Default::default() }; + assert!(GetMonitorInfoW(mon, &mut mi).as_bool()); + mi + }; + assert!(cx >= mi.rcWork.left && cy >= mi.rcWork.top); + assert!(cx + 1280 <= mi.rcWork.right || mi.rcWork.right - mi.rcWork.left < 1280); + } + + // a position far outside every monitor clamps into the nearest work area + let (x, y) = window_position("100000,100000", 1280, 720); + let mon = MonitorFromPoint(POINT { x, y }, MONITOR_DEFAULTTONEAREST); + assert!(!mon.is_invalid()); + + // the env override wins over the setting + std::env::set_var("XCL_WINDOW_POS", "center"); + assert_eq!(window_position("auto", 1280, 720), window_position("center", 1280, 720)); + std::env::remove_var("XCL_WINDOW_POS"); + } + } +}