Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions crates/xcl/src/hle/gameinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down Expand Up @@ -240,6 +241,8 @@ fn button_named(name: &str) -> Option<u32> {
/// `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()) {
Expand All @@ -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<f64> = 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")]
Expand Down Expand Up @@ -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() };
Expand Down
Loading