diff --git a/crates/xcl/src/config.rs b/crates/xcl/src/config.rs index a49fa9e..928f6f6 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 @@ -171,6 +175,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"); + } + } +} diff --git a/crates/xcl/src/hle/kbm.rs b/crates/xcl/src/hle/kbm.rs index efaa480..6d18b1c 100644 --- a/crates/xcl/src/hle/kbm.rs +++ b/crates/xcl/src/hle/kbm.rs @@ -187,6 +187,8 @@ const WM_SETCURSOR: u32 = 0x0020; const WM_SIZE: u32 = 0x0005; const WM_MOVE: u32 = 0x0003; const WM_LBUTTONDOWN: u32 = 0x0201; +const WM_ENTERSIZEMOVE: u32 = 0x0231; +const WM_EXITSIZEMOVE: u32 = 0x0232; const RID_INPUT: u32 = 0x1000_0003; const RIM_TYPEMOUSE: u32 = 0; const MOUSE_MOVE_ABSOLUTE: u16 = 1; @@ -207,6 +209,9 @@ const SC_D: (u32, i32) = (0x20, 0x44); static WINDOW: AtomicIsize = AtomicIsize::new(0); /// the cursor is held in the window (it has the focus) static CAPTURED: AtomicBool = AtomicBool::new(false); +/// the user is dragging or resizing the window (WM_ENTERSIZEMOVE … WM_EXITSIZEMOVE): +/// the cursor must stay free, or the 1-pixel clip teleports it back mid-drag +static SIZING: AtomicBool = AtomicBool::new(false); /// mouse counts not yet taken by `apply` static DX: AtomicI32 = AtomicI32::new(0); static DY: AtomicI32 = AtomicI32::new(0); @@ -261,6 +266,14 @@ pub fn attach(hwnd: *mut c_void) { } unsafe fn hold_cursor(hwnd: *mut c_void, on: bool) { + // a drag/resize loop owns the cursor: clipping it to the window's centre would teleport + // it back on every WM_MOVE of the drag + if on && SIZING.load(Ordering::Acquire) { + if CAPTURED.swap(false, Ordering::AcqRel) { + ClipCursor(std::ptr::null()); + } + return; + } if !on { if CAPTURED.swap(false, Ordering::AcqRel) { ClipCursor(std::ptr::null()); @@ -311,6 +324,19 @@ pub unsafe fn on_message(hwnd: *mut c_void, msg: u32, wp: usize, lp: isize) -> b } false } + WM_ENTERSIZEMOVE => { + SIZING.store(true, Ordering::Release); + hold_cursor(hwnd, false); + reset_mouse_state(); + false + } + WM_EXITSIZEMOVE => { + SIZING.store(false, Ordering::Release); + if GetForegroundWindow() == hwnd { + hold_cursor(hwnd, true); + } + false + } WM_LBUTTONDOWN | WM_SIZE | WM_MOVE => { if GetForegroundWindow() == hwnd { hold_cursor(hwnd, true);