Merge PR #295: Input: add opt-in GTA V PC-style keyboard and mouse mapping (VICTORY_KBM_GTA)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
beloooo
2026-11-26 17:27:46 -05:00
3 changed files with 575 additions and 13 deletions

View File

@ -71,6 +71,7 @@ describe which lighting paths run and the remaining rendering issues.
| `VICTORY_FRAME_INTERVAL_MS=16.667` | Lets the title go above 30 fps (menus and videos reach 60; the game's clock stays right) |
| `VICTORY_AMBIENT_SETTLE` | How fast light settles after walking indoors (on by default, `250,3`; `0` turns it off) |
| `VICTORY_TEXTURE_STORE=1` | Converted textures kept between runs (about 8 ms a frame for now: off) |
| `VICTORY_KBM_GTA=1` | Optional GTA V PC-style keyboard and mouse mapping on the stand-in pad: `F2` or `` ` `` (`~`) toggles mouse-look and auto-hides the cursor while locked (`Esc`/`P`/focus loss unlocks and restores it), LMB/RMB = RT/LT, MMB/`C` = R3, `Shift` = A, `Space` = X, `R` = B, `F` = Y, `Tab` = LB, `Q` = RB, `Ctrl` = L3, `E` = D-pad Right, `V` = View. Tune with `VICTORY_MOUSE_SENS` (default `1.0`) and `VICTORY_MOUSE_INVERT_Y=1` (or pass via `scripts\play.ps1 -More @{VICTORY_KBM_GTA='1'}`) |
What is known to be wrong:

View File

@ -80,6 +80,172 @@ const FALLBACK_BUTTONS: [(u8, u32); 14] = [
(b'H', RIGHT_STICK),
];
/// Opt-in GTA V PC-style keyboard bindings for the stand-in gamepad
/// (`VICTORY_KBM_GTA=1`). Left stick stays W/A/S/D; right stick is driven by
/// mouse look (toggled with F2 or `` ` `` / `~`) plus I/J/K/L fallback;
/// triggers come from LMB (RT) and RMB (LT). `Host::keys()` reports specific
/// left/right modifiers (`0xA0..=0xA5`) rather than generic `0x10..=0x12`.
const GTA_BUTTONS: [(u8, u32); 26] = [
(0x1B, MENU), // Esc (also unlocks mouse look)
(b'P', MENU), // Pause / Map
(b'V', VIEW), // Change camera perspective
(0xA0, A), // Left Shift: Sprint
(0xA1, A), // Right Shift: Sprint
(0x0D, A), // Enter: Confirm (menus)
(0x20, X), // Space: Jump / Climb / Vault
(b'X', X), // X fallback
(b'R', B), // Reload / Melee
(0x08, B), // Backspace: Back / Cancel (menus)
(b'F', Y), // Enter / Exit vehicle
(0x09, LEFT_SHOULDER), // Tab: Weapon wheel (LB)
(b'Q', RIGHT_SHOULDER), // Q: Take cover / Handbrake (RB)
(0xA2, LEFT_STICK), // Left Ctrl: Crouch / Stealth / Horn (L3)
(0xA3, LEFT_STICK), // Right Ctrl: Crouch / Stealth / Horn (L3)
(b'C', RIGHT_STICK), // C: Look behind / Special (R3)
(0x14, RIGHT_STICK), // Caps Lock: Special (R3)
(b'H', RIGHT_STICK), // H fallback (R3)
(b'E', RIGHT), // E: Interact / Talk (D-pad Right)
(0x27, RIGHT), // Right arrow (D-pad Right)
(0x26, UP), // Up arrow: Phone (D-pad Up)
(b'M', UP), // M: Phone / Menu (D-pad Up)
(0x28, DOWN), // Down arrow: Character wheel / Minimap (D-pad Down)
(b'Z', DOWN), // Z: Expand minimap / Character wheel (D-pad Down)
(0x25, LEFT), // Left arrow (D-pad Left)
(b'G', LEFT), // G: Throw / Context left (D-pad Left)
];
/// Whether GTA mouse-look lock (`~` / `F2`) is currently active; read by
/// `lifecycle` on `WM_SETCURSOR` to auto-hide the Windows cursor inside the
/// game window's client area while locked.
#[cfg(windows)]
static CURSOR_LOCKED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// Returns true while GTA mouse-look lock (`~` / `F2`) is active and the
/// cursor should be hidden inside the client area.
#[cfg(windows)]
pub(crate) fn cursor_locked() -> bool {
CURSOR_LOCKED.load(Ordering::Relaxed)
}
/// Parses an opt-in boolean environment variable (`1`, `true`, `yes`, `on`,
/// ignoring surrounding whitespace and ASCII case).
fn parse_env_flag(value: &str) -> bool {
matches!(
value.trim().to_ascii_lowercase().as_str(),
"1" | "true" | "yes" | "on"
)
}
/// GTA V PC-style keyboard + mouse stand-in pad (`VICTORY_KBM_GTA=1`).
/// Off by default so the existing keyboard table and tests stay unchanged.
fn gta_kbm() -> bool {
static ENABLED: LazyLock<bool> =
LazyLock::new(|| std::env::var("VICTORY_KBM_GTA").is_ok_and(|v| parse_env_flag(&v)));
*ENABLED
}
/// Mouse-look sensitivity multiplier for `VICTORY_KBM_GTA=1`
/// (`VICTORY_MOUSE_SENS`, default `1.0`).
#[cfg(windows)]
fn mouse_sens() -> f32 {
static SENS: LazyLock<f32> = LazyLock::new(|| {
std::env::var("VICTORY_MOUSE_SENS")
.ok()
.and_then(|s| s.trim().parse::<f32>().ok())
.filter(|v| v.is_finite() && *v > 0.0)
.unwrap_or(1.0)
});
*SENS
}
/// Invert mouse Y axis for `VICTORY_KBM_GTA=1` (`VICTORY_MOUSE_INVERT_Y=1`).
#[cfg(windows)]
fn mouse_invert_y() -> bool {
static INVERT: LazyLock<bool> =
LazyLock::new(|| std::env::var("VICTORY_MOUSE_INVERT_Y").is_ok_and(|v| parse_env_flag(&v)));
*INVERT
}
/// Exponentially smoothed mouse-delta to right-stick velocity converter.
///
/// The poll loop runs at 125 Hz (8 ms) while 3D gameplay may render at low
/// framerates (~5 FPS). A 45 ms half-life keeps flicks alive long enough for
/// the next rendered frame to sample them while settling cleanly to zero.
#[cfg(any(windows, test))]
#[derive(Default)]
struct MouseLook {
vx: f32,
vy: f32,
}
#[cfg(any(windows, test))]
impl MouseLook {
const BASE_SENS: f32 = 0.022;
const HALF_LIFE_SECS: f32 = 0.045;
const DEADZONE: f32 = 0.015;
fn reset(&mut self) {
self.vx = 0.0;
self.vy = 0.0;
}
fn step(&mut self, dx: i32, dy: i32, dt: Duration, sens: f32, invert_y: bool) -> (f32, f32) {
let dt_secs = dt.as_secs_f32().clamp(0.001, 0.25);
let decay = 0.5_f32.powf(dt_secs / Self::HALF_LIFE_SECS);
let scale = Self::BASE_SENS * sens;
let y_sign = if invert_y { 1.0 } else { -1.0 };
self.vx = (self.vx * decay + dx as f32 * scale).clamp(-1.0, 1.0);
self.vy = (self.vy * decay + dy as f32 * scale * y_sign).clamp(-1.0, 1.0);
// Only snap tiny residual decay to zero at rest so low-sensitivity
// 1-pixel movements are never dropped.
if dx == 0 && self.vx.abs() < Self::DEADZONE {
self.vx = 0.0;
}
if dy == 0 && self.vy.abs() < Self::DEADZONE {
self.vy = 0.0;
}
(self.vx, self.vy)
}
}
/// Edge-captured state for `[LMB, RMB, MMB]` so holding a button on window
/// chrome or in another app and dragging into the client area never fires
/// triggers or buttons.
#[cfg(any(windows, test))]
#[derive(Default)]
struct MouseButtons {
seeded: bool,
prev: [bool; 3],
captured: [bool; 3],
}
#[cfg(any(windows, test))]
impl MouseButtons {
fn reset(&mut self) {
self.seeded = false;
self.prev = [false; 3];
self.captured = [false; 3];
}
fn update(&mut self, down: [bool; 3], eligible: bool) -> [bool; 3] {
if !self.seeded {
self.seeded = true;
self.prev = down;
self.captured = [false; 3];
return self.captured;
}
for ((&is_down, prev), captured) in down.iter().zip(&mut self.prev).zip(&mut self.captured) {
if !is_down {
*captured = false;
} else if !*prev && eligible {
*captured = true;
}
*prev = is_down;
}
self.captured
}
}
/// A gamepad reading: triggers 0 to 1, sticks -1 to 1 (up positive).
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
@ -113,9 +279,19 @@ trait Source: Send {
fn keys(&mut self) -> Vec<KeyState>;
/// Motor speeds: low frequency (left), high frequency (right).
fn rumble(&mut self, slot: usize, low: u16, high: u16);
/// Extra stand-in pad input from the mouse when `VICTORY_KBM_GTA=1`.
/// When `active` is false (a physical controller owns input), any active
/// mouse-look capture must be released without warping the cursor.
fn mouse_pad(&mut self, _active: bool) -> PadState {
PadState::default()
}
}
fn fallback(keys: &[KeyState]) -> PadState {
fallback_for(keys, gta_kbm())
}
fn fallback_for(keys: &[KeyState], gta: bool) -> PadState {
let on = |vk: u8| {
if keys.iter().any(|k| k.virtual_key == vk) {
1.0
@ -123,13 +299,14 @@ fn fallback(keys: &[KeyState]) -> PadState {
0.0
}
};
let table: &[(u8, u32)] = if gta { &GTA_BUTTONS } else { &FALLBACK_BUTTONS };
PadState {
buttons: FALLBACK_BUTTONS
buttons: table
.iter()
.filter(|(vk, _)| on(*vk) != 0.0)
.fold(0, |all, (_, bit)| all | bit),
left_trigger: on(b'Z'),
right_trigger: on(b'C'),
left_trigger: if gta { 0.0 } else { on(b'Z') },
right_trigger: if gta { 0.0 } else { on(b'C') },
left_x: on(b'D') - on(b'A'),
left_y: on(b'W') - on(b'S'),
right_x: on(b'L') - on(b'J'),
@ -430,12 +607,17 @@ impl Hub {
/// Reads the host once; device callbacks run on the calling thread.
fn poll(&self) {
let (pads, keys) = {
let (pads, keys, mouse) = {
let mut source = lock(&self.source);
let pads: [_; PADS] = std::array::from_fn(|slot| source.pad(slot));
(pads, source.keys())
let keys = source.keys();
let stand_in_active = self.one_pad || pads.iter().all(Option::is_none);
let mouse = source.mouse_pad(stand_in_active);
(pads, keys, mouse)
};
let events = self.state().poll(&self.devices, self.one_pad, pads, keys);
let events = self
.state()
.poll(&self.devices, self.one_pad, pads, keys, mouse);
self.deliver(events);
}
@ -481,11 +663,13 @@ impl State {
one_pad: bool,
pads: [Option<PadState>; PADS],
keys: Vec<KeyState>,
mouse: PadState,
) -> Vec<Event> {
let now = now();
let mut events = std::mem::take(&mut self.pending);
let stand_in = merged(fallback(&keys), mouse);
if one_pad {
let pad = pads.into_iter().flatten().fold(fallback(&keys), merged);
let pad = pads.into_iter().flatten().fold(stand_in, merged);
self.update(devices, FALLBACK, Some(Value::Pad(pad)), now, &mut events);
self.update(devices, KEYBOARD, Some(Value::Keys(keys)), now, &mut events);
return events;
@ -494,7 +678,7 @@ impl State {
for (slot, pad) in pads.into_iter().enumerate() {
self.update(devices, slot, pad.map(Value::Pad), now, &mut events);
}
let fallback = (!any_pad).then(|| Value::Pad(fallback(&keys)));
let fallback = (!any_pad).then_some(Value::Pad(stand_in));
self.update(devices, FALLBACK, fallback, now, &mut events);
self.update(devices, KEYBOARD, Some(Value::Keys(keys)), now, &mut events);
events
@ -1119,6 +1303,7 @@ pub(crate) fn module() -> HleModule {
mod host {
use std::time::{Duration, Instant};
use windows_sys::Win32::Foundation::{HWND, POINT};
use windows_sys::Win32::System::Threading::GetCurrentProcessId;
use windows_sys::Win32::UI::Input::KeyboardAndMouse::{
GetAsyncKeyState, MAPVK_VK_TO_VSC_EX, MapVirtualKeyW,
@ -1132,7 +1317,8 @@ mod host {
XInputSetState,
};
use windows_sys::Win32::UI::WindowsAndMessaging::{
GetForegroundWindow, GetWindowThreadProcessId,
GetCursorPos, GetForegroundWindow, GetWindowInfo, GetWindowThreadProcessId, HTCLIENT,
PostMessageW, SetCursorPos, WINDOWINFO, WM_MOUSEMOVE, WM_SETCURSOR,
};
use super::*;
@ -1177,6 +1363,45 @@ mod host {
#[derive(Default)]
pub(super) struct Host {
empty_until: [Option<Instant>; PADS],
mouse_locked: bool,
just_locked: bool,
toggle_was_down: bool,
anchor: (i32, i32),
mouse_last_poll: Option<Instant>,
mouse_look: MouseLook,
mouse_buttons: MouseButtons,
}
impl Host {
fn set_mouse_locked(&mut self, locked: bool, hwnd: Option<HWND>) {
if self.mouse_locked == locked {
return;
}
self.mouse_locked = locked;
CURSOR_LOCKED.store(locked, Ordering::Release);
if let Some(hwnd) = hwnd {
// Prompt the window's UI thread to run WM_SETCURSOR right away
// so the cursor hides/reappears the instant `~`/`F2`/`Esc`/`P`
// is pressed, even before moving the mouse.
let lparam = (HTCLIENT as isize) | ((WM_MOUSEMOVE as isize) << 16);
// SAFETY: asynchronous post to our own process's foreground window.
unsafe {
let _ = PostMessageW(hwnd, WM_SETCURSOR, hwnd as usize, lparam);
}
}
}
fn disarm_mouse(&mut self, hwnd: Option<HWND>, reason: &'static str) {
if self.mouse_locked {
self.set_mouse_locked(false, hwnd);
tracing::info!(reason, "mouse look unlocked");
}
self.just_locked = false;
self.mouse_look.reset();
self.mouse_last_poll = None;
self.toggle_was_down = false;
self.mouse_buttons.reset();
}
}
impl Source for Host {
@ -1243,6 +1468,123 @@ mod host {
// SAFETY: a valid vibration; an empty slot just fails.
unsafe { XInputSetState(slot as u32, &speeds) };
}
fn mouse_pad(&mut self, active: bool) -> PadState {
if !gta_kbm() || !active {
self.disarm_mouse(foreground_window(), "stand-in inactive");
return PadState::default();
}
let Some(hwnd) = foreground_window() else {
self.disarm_mouse(None, "focus lost");
return PadState::default();
};
let key_down = |vk: i32| {
// SAFETY: plain async key state query.
unsafe { GetAsyncKeyState(vk) < 0 }
};
// Toggle mouse-look lock with F2 (0x71, layout-independent) or
// VK_OEM_3 (0xC0, `` ` `` / `~` on US layouts).
let toggle_down = key_down(0x71) || key_down(0xC0);
if toggle_down && !self.toggle_was_down {
let next = !self.mouse_locked;
self.set_mouse_locked(next, Some(hwnd));
self.just_locked = next;
self.mouse_look.reset();
tracing::info!(locked = next, "mouse look toggled");
}
self.toggle_was_down = toggle_down;
// Esc (0x1B) or P opens the pause menu and frees the mouse so the
// user is never trapped in mouse-look mode.
if self.mouse_locked && (key_down(0x1B) || key_down(i32::from(b'P'))) {
self.set_mouse_locked(false, Some(hwnd));
self.just_locked = false;
self.mouse_look.reset();
tracing::info!("mouse look unlocked (menu key)");
}
let Some((c_left, c_top, c_right, c_bottom)) = client_rect_screen(hwnd) else {
self.disarm_mouse(Some(hwnd), "client unavailable");
return PadState::default();
};
let mut pos = POINT::default();
// SAFETY: valid pointer to our initialized POINT.
if unsafe { GetCursorPos(&mut pos) } == 0 {
self.disarm_mouse(Some(hwnd), "cursor query failed");
return PadState::default();
}
let now = Instant::now();
let dt = self
.mouse_last_poll
.replace(now)
.map_or(POLL, |prev| now.saturating_duration_since(prev));
let (right_x, right_y) = if self.mouse_locked {
let cx = c_left + (c_right - c_left) / 2;
let cy = c_top + (c_bottom - c_top) / 2;
let (dx, dy) = if self.just_locked {
self.just_locked = false;
if let Some(anchor) = recenter_cursor(cx, cy) {
self.anchor = anchor;
} else {
self.set_mouse_locked(false, Some(hwnd));
self.mouse_look.reset();
tracing::warn!("mouse look unlocked (cursor warp failed)");
}
(0, 0)
} else {
let dx = pos.x - self.anchor.0;
let dy = pos.y - self.anchor.1;
if dx != 0 || dy != 0 {
if let Some(anchor) = recenter_cursor(cx, cy) {
self.anchor = anchor;
(dx, dy)
} else {
self.set_mouse_locked(false, Some(hwnd));
self.mouse_look.reset();
tracing::warn!("mouse look unlocked (cursor warp failed)");
(0, 0)
}
} else {
(0, 0)
}
};
if self.mouse_locked {
self.mouse_look
.step(dx, dy, dt, mouse_sens(), mouse_invert_y())
} else {
(0.0, 0.0)
}
} else {
self.mouse_look.reset();
(0.0, 0.0)
};
let in_client =
(c_left..c_right).contains(&pos.x) && (c_top..c_bottom).contains(&pos.y);
let [lmb, rmb, mmb] = self.mouse_buttons.update(
[key_down(0x01), key_down(0x02), key_down(0x04)],
self.mouse_locked || in_client,
);
let mut pad = PadState {
right_x,
right_y,
..Default::default()
};
if lmb {
pad.right_trigger = 1.0; // LMB: Shoot / Attack / Gas (RT)
}
if rmb {
pad.left_trigger = 1.0; // RMB: Aim / Brake-Reverse (LT)
}
if mmb {
pad.buttons |= RIGHT_STICK; // MMB: Look behind / Special (R3)
}
pad
}
}
/// Debugging aid: `VICTORY_KEYS=<seconds>:<key>,...` holds each key for
@ -1287,16 +1629,58 @@ mod host {
.collect()
}
/// The foreground window is one of ours (the title's or our fallback).
fn focused() -> bool {
/// The foreground window if it belongs to our process.
fn foreground_window() -> Option<HWND> {
let mut process = 0;
// SAFETY: plain queries; a null window gives process 0.
unsafe {
GetWindowThreadProcessId(GetForegroundWindow(), &mut process);
process == GetCurrentProcessId()
let hwnd = GetForegroundWindow();
if hwnd.is_null() {
return None;
}
GetWindowThreadProcessId(hwnd, &mut process);
(process == GetCurrentProcessId()).then_some(hwnd)
}
}
/// The foreground window is one of ours (the title's or our fallback).
fn focused() -> bool {
foreground_window().is_some()
}
/// Recenters the cursor at `(cx, cy)` and reads back its actual screen
/// position so fractional DPI scaling never introduces phantom drift.
/// Returns `None` if either Win32 call fails.
fn recenter_cursor(cx: i32, cy: i32) -> Option<(i32, i32)> {
// SAFETY: recenters the cursor inside our own foreground window.
if unsafe { SetCursorPos(cx, cy) } == 0 {
return None;
}
let mut settled = POINT::default();
// SAFETY: valid pointer to our initialized POINT.
if unsafe { GetCursorPos(&mut settled) } == 0 {
return None;
}
Some((settled.x, settled.y))
}
/// Client area in screen coordinates directly from Win32 `GetWindowInfo`.
fn client_rect_screen(hwnd: HWND) -> Option<(i32, i32, i32, i32)> {
let mut info = WINDOWINFO {
cbSize: size_of::<WINDOWINFO>() as u32,
..Default::default()
};
// SAFETY: valid initialized WINDOWINFO with cbSize set.
if unsafe { GetWindowInfo(hwnd, &mut info) } == 0 {
return None;
}
let rc = info.rcClient;
if rc.right - rc.left < 64 || rc.bottom - rc.top < 64 {
return None;
}
Some((rc.left, rc.top, rc.right, rc.bottom))
}
#[test]
fn host_pads_convert_to_console_pads() {
let pad = XINPUT_GAMEPAD {
@ -1334,6 +1718,8 @@ mod tests {
struct FakeHost {
pads: [Option<PadState>; PADS],
keys: Vec<KeyState>,
mouse: PadState,
mouse_active_log: Vec<bool>,
rumble: Vec<(usize, u16, u16)>,
}
@ -1349,6 +1735,15 @@ mod tests {
fn rumble(&mut self, slot: usize, low: u16, high: u16) {
self.0.lock().unwrap().rumble.push((slot, low, high));
}
fn mouse_pad(&mut self, active: bool) -> PadState {
let mut host = self.0.lock().unwrap();
host.mouse_active_log.push(active);
if active {
host.mouse
} else {
PadState::default()
}
}
}
fn fake() -> (&'static Hub, Arc<Mutex<FakeHost>>) {
@ -1711,4 +2106,140 @@ mod tests {
rumble(device, std::ptr::null());
assert_eq!(host.lock().unwrap().rumble, [(2, 32767, 65535), (2, 0, 0)]);
}
#[test]
fn gta_kbm_maps_pc_style_keys_and_smooths_mouse_look() {
let keys = vec![
key(b'W'),
key(0xA0), // Left Shift -> A (Sprint)
key(0x20), // Space -> X (Jump)
key(b'R'), // R -> B (Reload)
key(b'F'), // F -> Y (Enter/Exit vehicle)
key(0x09), // Tab -> LB (Weapon wheel)
key(b'Q'), // Q -> RB (Cover)
key(0xA2), // Left Ctrl -> L3 (Crouch)
key(b'C'), // C -> R3 (Look behind), not RT
key(b'E'), // E -> D-pad Right (Interact)
key(b'Z'), // Z -> D-pad Down (Minimap), not LT
key(b'V'), // V -> View (Camera)
key(0x1B), // Esc -> Menu
];
let mapped = fallback_for(&keys, true);
assert_eq!(
mapped.buttons,
A | X
| B
| Y
| LEFT_SHOULDER
| RIGHT_SHOULDER
| LEFT_STICK
| RIGHT_STICK
| RIGHT
| DOWN
| VIEW
| MENU
);
assert_eq!(mapped.left_y, 1.0);
assert_eq!(mapped.left_trigger, 0.0);
assert_eq!(mapped.right_trigger, 0.0);
// Mouse pad merges cleanly into the stand-in pad.
let (hub, host) = fake_with(true);
{
let mut h = host.lock().unwrap();
h.keys = vec![key(b'W')];
h.mouse = PadState {
buttons: RIGHT_STICK,
left_trigger: 1.0,
right_trigger: 1.0,
right_x: 0.75,
right_y: -0.25,
..Default::default()
};
}
hub.poll();
let r = current(hub, GAMEPAD_KIND, FALLBACK).unwrap();
let got = pad(r);
assert_eq!(got.buttons, RIGHT_STICK);
assert_eq!(
(
got.left_y,
got.left_trigger,
got.right_trigger,
got.right_x,
got.right_y
),
(1.0, 1.0, 1.0, 0.75, -0.25)
);
release(r);
let mut look = MouseLook::default();
let (rx, ry) = look.step(20, -10, POLL, 1.0, false);
assert!(
(0.3..=1.0).contains(&rx),
"mouse right should push +right_x, got {rx}"
);
assert!(
(0.15..=1.0).contains(&ry),
"mouse up (-dy) should push +right_y, got {ry}"
);
// Inverted Y flips the vertical axis.
look.reset();
let (_, ry_inv) = look.step(0, -10, POLL, 1.0, true);
assert!(ry_inv < -0.15, "invert_y should flip right_y, got {ry_inv}");
// Decays back to zero when the mouse stops moving.
for _ in 0..40 {
look.step(0, 0, POLL, 1.0, false);
}
assert_eq!(look.step(0, 0, POLL, 1.0, false), (0.0, 0.0));
// Low-sensitivity 1-pixel movement is not swallowed by the rest deadzone.
let (fine_x, _) = look.step(1, 0, POLL, 0.3, false);
assert!(
fine_x > 0.0,
"1px low-sens motion should stay non-zero, got {fine_x}"
);
// Strict boolean env parsing rejects "false", "0", " 0 ", and "".
assert!(parse_env_flag("1"));
assert!(parse_env_flag(" true "));
assert!(parse_env_flag("YES"));
assert!(!parse_env_flag("0"));
assert!(!parse_env_flag(" 0 "));
assert!(!parse_env_flag("false"));
assert!(!parse_env_flag(""));
// Dragging a held button from outside (chrome / another app) into the
// client area must not fire triggers until released and pressed fresh.
let mut buttons = MouseButtons::default();
assert_eq!(buttons.update([true, false, false], true), [false; 3]);
assert_eq!(buttons.update([true, false, false], true), [false; 3]);
assert_eq!(buttons.update([false, false, false], true), [false; 3]);
assert_eq!(buttons.update([true, false, false], false), [false; 3]);
assert_eq!(buttons.update([true, false, false], true), [false; 3]);
assert_eq!(buttons.update([false, false, false], true), [false; 3]);
assert_eq!(
buttons.update([true, false, true], true),
[true, false, true]
);
assert_eq!(
buttons.update([true, false, true], false),
[true, false, true]
);
assert_eq!(buttons.update([false, false, false], false), [false; 3]);
}
#[test]
fn physical_controller_takeover_disarms_mouse_pad_when_not_one_pad() {
let (hub, host) = fake_with(false);
host.lock().unwrap().pads[0] = Some(PadState {
buttons: A,
..Default::default()
});
hub.poll();
assert_eq!(host.lock().unwrap().mouse_active_log.last(), Some(&false));
host.lock().unwrap().pads[0] = None;
hub.poll();
assert_eq!(host.lock().unwrap().mouse_active_log.last(), Some(&true));
}
}

View File

@ -261,6 +261,36 @@ extern "system" fn window_proc(
if message == wm::WM_SYSCOMMAND && wparam & 0xFFF0 == wm::SC_KEYMENU as usize {
return 0;
}
// Auto-hide the Windows cursor inside the game window's client area while
// GTA mouse-look lock (`~` / `F2`) is active, and restore it immediately
// when unlocked (`~` / `F2` / `Esc` / `P`).
if message == wm::WM_SETCURSOR && (lparam as u32 & 0xFFFF) == wm::HTCLIENT {
static CURSOR_HIDDEN: AtomicBool = AtomicBool::new(false);
if super::gameinput::cursor_locked() {
CURSOR_HIDDEN.store(true, Ordering::Relaxed);
// SAFETY: hides the cursor only inside our client area on the UI thread.
unsafe { wm::SetCursor(std::ptr::null_mut()) };
return 1;
}
if CURSOR_HIDDEN.swap(false, Ordering::Relaxed) {
// SAFETY: let the title's procedure restore its cursor first; if it
// leaves none set, fall back to the standard arrow.
let res = unsafe {
let title = wm::GetPropW(hwnd, TITLE_PROC);
let title = std::mem::transmute::<*mut c_void, wm::WNDPROC>(title);
wm::CallWindowProcW(title, hwnd, message, wparam, lparam)
};
// SAFETY: runs on this window's UI thread; restores the standard
// arrow cursor if the title's window procedure left none set.
unsafe {
if wm::GetCursor().is_null() {
let arrow = wm::LoadCursorW(std::ptr::null_mut(), wm::IDC_ARROW);
wm::SetCursor(arrow);
}
}
return res;
}
}
// SAFETY: the procedure `hook` stored, called the way the system would.
unsafe {
let title = wm::GetPropW(hwnd, TITLE_PROC);