From 1178e8a37e78cc31c99f2d4e0883c7bebb6e7448 Mon Sep 17 00:00:00 2001 From: pablogonz12 Date: Tue, 24 Nov 2026 03:20:08 +0100 Subject: [PATCH] GameInput: XCL_PRESS_HOLD holds scripted presses longer XCL_PRESS_AT / XCL_PRESS_A_AT hold each button for 200 ms. A title that draws a frame every few seconds (a retail title under Wine here: about one frame in 4 s at the start) reads its pad as seldom and misses such a press. XCL_PRESS_HOLD=s holds each press for s seconds instead; the default stays 200 ms. --- crates/xcl/src/hle/gameinput.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/gameinput.rs b/crates/xcl/src/hle/gameinput.rs index 402d4c712..732d2c994 100644 --- a/crates/xcl/src/hle/gameinput.rs +++ b/crates/xcl/src/hle/gameinput.rs @@ -11,7 +11,8 @@ //! thread for an asynchronous one); reading callbacks get a reading when the state changes //! (polled at 250 Hz). Methods not listed log their slot once and fail. //! -//! `XCL_PRESS_A_AT=s1,s2…`: press A for 200 ms at those seconds after start (tests). +//! `XCL_PRESS_A_AT=s1,s2…`: press A for 200 ms at those seconds after start (tests); +//! `XCL_PRESS_HOLD=s`: hold each press that many seconds instead. #![allow(non_snake_case)] @@ -240,6 +241,8 @@ fn button_named(name: &str) -> Option { /// `XCL_PRESS_AT=s:BUTTON,…` ("14:A,18.5:DOWN,20:A"; buttons A B X Y UP DOWN LEFT RIGHT MENU /// VIEW LB RB) and `XCL_PRESS_A_AT=s,…`: buttons held for 200 ms at those seconds after the /// host started (the log's times) – a title's menus walked without a person at the controls. +/// `XCL_PRESS_HOLD=s` holds them longer: a title drawing a frame every few seconds reads its +/// pad as seldom and misses a 200 ms press. fn parse_presses(script: &str, a_only: &str) -> Vec<(f64, u32)> { let mut v: Vec<(f64, u32)> = a_only.split(',').filter_map(|s| s.trim().parse().ok()).map(|s| (s, A)).collect(); for item in script.split(',').map(str::trim).filter(|s| !s.is_empty()) { @@ -258,8 +261,14 @@ fn scripted() -> u32 { parse_presses(&var("XCL_PRESS_AT"), &var("XCL_PRESS_A_AT")) }); // seconds since the host started, as the log's times (not since the title first polled) - let t = crate::log::uptime(); - at.iter().filter(|&&(s, _)| t >= s && t < s + 0.2).fold(0, |b, &(_, g)| b | g) + static HOLD: OnceLock = OnceLock::new(); + let hold = *HOLD.get_or_init(|| std::env::var("XCL_PRESS_HOLD").ok().and_then(|v| v.trim().parse().ok()).filter(|&h: &f64| h > 0.0).unwrap_or(0.2)); + held(at, crate::log::uptime(), hold) +} + +/// The buttons of `at` held at `t` seconds, each for `hold` seconds from its time. +fn held(at: &[(f64, u32)], t: f64, hold: f64) -> u32 { + at.iter().filter(|&&(s, _)| t >= s && t < s + hold).fold(0, |b, &(_, g)| b | g) } #[link(name = "user32")] @@ -1034,6 +1043,18 @@ mod tests { assert!(parse_presses("", "").is_empty()); } + #[test] + fn a_press_is_held_for_its_hold_time() { + let at = [(10.0, A), (12.0, B)]; + assert_eq!(held(&at, 9.9, 0.2), 0); + assert_eq!(held(&at, 10.1, 0.2), A); + assert_eq!(held(&at, 10.5, 0.2), 0, "200 ms by default"); + assert_eq!(held(&at, 10.5, 8.0), A); + assert_eq!(held(&at, 12.5, 8.0), A | B, "presses that overlap are held together"); + assert_eq!(held(&at, 18.0, 8.0), B); + assert_eq!(held(&at, 20.0, 8.0), 0); + } + #[test] fn keyboard_mouse_config_gates_keyboard_and_kbm_but_keeps_scripted() { let mut s = GamepadState { buttons: B, left_x: 0.25, ..Default::default() };