From 229708a71fe6d3fb5e46fd1de97b1b055855b776 Mon Sep 17 00:00:00 2001 From: ryosen0 Date: Mon, 23 Nov 2026 21:58:42 +0300 Subject: [PATCH 1/2] =?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"); + } + } +} From a6e52be347a870b85e40ffa39dec235b5918210e Mon Sep 17 00:00:00 2001 From: ryosen0 Date: Mon, 23 Nov 2026 22:05:50 +0300 Subject: [PATCH 2/2] kbm: free the cursor while the window is dragged or resized While the XCL window has the focus its cursor is clipped to the client centre (the mouse plays the right stick). WM_MOVE re-clipped it during a drag: the cursor teleported back to the (moving) centre mid-drag, breaking the drag. Track WM_ENTERSIZEMOVE/WM_EXITSIZEMOVE: release the clip for the whole drag/resize loop, restore it after. kbm tests pass (7). --- crates/xcl/src/hle/kbm.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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);