From da16b075618b792458bbb9ca19e856e9c4189c66 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 18:00:25 +0400 Subject: fix(input): keep absolute mouse positions out of cockpit look --- adapters/fparkan-platform-winit/Cargo.toml | 2 +- adapters/fparkan-platform-winit/src/lib.rs | 19 +++++- adapters/fparkan-platform-winit/src/raw_mouse.rs | 79 ++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 adapters/fparkan-platform-winit/src/raw_mouse.rs (limited to 'adapters/fparkan-platform-winit') diff --git a/adapters/fparkan-platform-winit/Cargo.toml b/adapters/fparkan-platform-winit/Cargo.toml index 7a6a21a6..6523dd5a 100644 --- a/adapters/fparkan-platform-winit/Cargo.toml +++ b/adapters/fparkan-platform-winit/Cargo.toml @@ -11,4 +11,4 @@ raw-window-handle = "0.6" winit = { version = "0.30", default-features = false, features = ["rwh_06"] } [target.'cfg(windows)'.dependencies] -windows-sys = { version = "0.101", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_System_LibraryLoader", "Win32_UI_WindowsAndMessaging"] } +windows-sys = { version = "0.101", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_System_LibraryLoader", "Win32_UI_Input", "Win32_UI_WindowsAndMessaging"] } diff --git a/adapters/fparkan-platform-winit/src/lib.rs b/adapters/fparkan-platform-winit/src/lib.rs index 5307ee68..57ca827f 100644 --- a/adapters/fparkan-platform-winit/src/lib.rs +++ b/adapters/fparkan-platform-winit/src/lib.rs @@ -9,6 +9,10 @@ pub mod text; #[allow(unsafe_code)] pub mod startup_window; +#[cfg(windows)] +#[allow(unsafe_code)] +mod raw_mouse; + use fparkan_platform::{ EventSource, MonotonicClock, MonotonicInstant, NativeWindowHandles, PhysicalSize, PlatformError, PlatformEvent, RenderRequest, WindowHandle, WindowPort, @@ -19,6 +23,7 @@ use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::OnceLock; use std::time::Instant; use winit::event::{Event, MouseButton, WindowEvent}; +use winit::event_loop::EventLoop; use winit::platform::scancode::PhysicalKeyExtScancode; use winit::window::Window; @@ -31,6 +36,18 @@ fn next_window_id() -> u64 { NEXT_WINDOW_HANDLE_ID.fetch_add(1, Ordering::Relaxed) } +/// Builds the event loop used by the game and its relative camera controls. +pub fn new_game_event_loop() -> Result, winit::error::EventLoopError> { + #[cfg(windows)] + { + let mut builder = EventLoop::builder(); + raw_mouse::ignore_absolute_motion(&mut builder); + builder.build() + } + #[cfg(not(windows))] + EventLoop::new() +} + /// Simple monotonic clock for windowing abstractions. #[derive(Clone, Copy, Debug)] pub struct WinitClock; @@ -561,5 +578,3 @@ mod tests { assert!(window.is_occluded()); } } - -// SAFETY: no unsafe usage in this crate. diff --git a/adapters/fparkan-platform-winit/src/raw_mouse.rs b/adapters/fparkan-platform-winit/src/raw_mouse.rs new file mode 100644 index 00000000..091823c2 --- /dev/null +++ b/adapters/fparkan-platform-winit/src/raw_mouse.rs @@ -0,0 +1,79 @@ +use std::mem::size_of; +use windows_sys::Win32::UI::{ + Input::{ + GetRawInputData, MOUSE_MOVE_ABSOLUTE, RAWINPUT, RAWINPUTHEADER, RID_INPUT, RIM_TYPEMOUSE, + }, + WindowsAndMessaging::{DefWindowProcW, MSG, WM_INPUT}, +}; +use winit::{event_loop::EventLoopBuilder, platform::windows::EventLoopBuilderExtWindows}; + +// winit 0.30 tests for MOUSE_MOVE_RELATIVE as a set flag, although its value +// is zero. Keep absolute positions out of the relative-look stream. Windows +// still sends the pointer movement, button and wheel messages to the window. +pub(super) fn ignore_absolute_motion(builder: &mut EventLoopBuilder<()>) { + builder.with_msg_hook(|message| { + // SAFETY: The winit hook supplies a pointer to a live Win32 MSG until + // the callback returns. + let message = unsafe { &*message.cast::() }; + if message.message != WM_INPUT { + return false; + } + let mut input = RAWINPUT::default(); + let mut capacity = size_of::() as u32; + // SAFETY: lParam of WM_INPUT is a live HRAWINPUT. The output is + // aligned and capacity describes the complete writable buffer. + let read = unsafe { + GetRawInputData( + message.lParam as _, + RID_INPUT, + (&mut input as *mut RAWINPUT).cast(), + &mut capacity, + size_of::() as u32, + ) + }; + if read != size_of::() as u32 || !is_absolute_mouse(&input) { + return false; + } + // SAFETY: This is the original, live WM_INPUT message. Since the hook + // consumes it, pass it to DefWindowProc to release the input handle. + unsafe { + DefWindowProcW( + message.hwnd, + message.message, + message.wParam, + message.lParam, + ) + }; + true + }); +} + +fn is_absolute_mouse(input: &RAWINPUT) -> bool { + if input.header.dwType != RIM_TYPEMOUSE { + return false; + } + // SAFETY: The packet type selects the mouse member of the Win32 union. + let flags = unsafe { input.data.mouse.usFlags }; + flags & MOUSE_MOVE_ABSOLUTE != 0 +} + +#[cfg(test)] +mod tests { + use super::*; + use windows_sys::Win32::UI::Input::{ + MOUSE_MOVE_NOCOALESCE, MOUSE_VIRTUAL_DESKTOP, RIM_TYPEKEYBOARD, + }; + + #[test] + fn only_absolute_mouse_packets_are_removed_from_raw_look() { + let mut input = RAWINPUT::default(); + input.header.dwType = RIM_TYPEMOUSE; + assert!(!is_absolute_mouse(&input)); + input.data.mouse.usFlags = MOUSE_MOVE_NOCOALESCE; + assert!(!is_absolute_mouse(&input)); + input.data.mouse.usFlags = MOUSE_MOVE_ABSOLUTE | MOUSE_VIRTUAL_DESKTOP; + assert!(is_absolute_mouse(&input)); + input.header.dwType = RIM_TYPEKEYBOARD; + assert!(!is_absolute_mouse(&input)); + } +} -- cgit v1.2.3