Pads read the moment they change: looked at every millisecond, everything polled every 8 ms as before

The input poll read the pads, keys and mouse together every 8 ms, so a press waited up to 8 ms for its reading.
Now the poll thread looks every millisecond at XInput's packet numbers of the slots that had a pad at the last
poll and at the Sony readers' count of readings unlike the one before (gameinput/hid.rs), and polls at once when
one moved; the keyboard and mouse stay at every 8 ms (the process runs at a 1 ms timer resolution: runtime.rs).
LEONIDA_PAD_QUICK=0 polls everything every 8 ms, as before. After voidopcode/vilocity 3eda937 (GPL-3.0), whose
poll thread does the same.

Tests: the gameinput module's tests (cargo test -p leonida-hle --lib gameinput), passed.

AI-assisted (Claude). Tested on an RTX 4060 Ti 16 GB, driver 616.64, Ryzen 5 7600X.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NotRexed
2026-11-29 23:24:00 -05:00
parent 1244e9a1e8
commit 998fcbcac0
2 changed files with 78 additions and 4 deletions

View File

@ -262,6 +262,23 @@ trait Source: Send {
/// Leonida: end of a poll, with the keys and the mouse look stick it read: where the kind of input in use is
/// followed.
fn observe(&mut self, _keys: &[KeyState], _look: (f32, f32)) {}
/// Leonida: whether a pad's input changed since this was last asked, looked at cheaply between polls (no
/// reading is made) so that a pad is read the moment it moves ([`pad_quick`]). None by default.
fn pads_changed(&mut self) -> bool {
false
}
}
/// How often the pads are looked at between polls ([`pad_quick`]).
const PAD_LOOK: Duration = Duration::from_millis(1);
/// Leonida: whether the poll thread looks at the pads every [`PAD_LOOK`] and reads the host at once when one
/// moved (XInput's packet numbers, the Sony readers' counts), the keyboard and mouse every [`POLL`] as before
/// (`LEONIDA_PAD_QUICK=0`: everything every [`POLL`]). A press waited up to 8 ms for the next poll. After
/// voidopcode/vilocity 3eda937 (GPL-3.0), whose poll thread does the same.
fn pad_quick() -> bool {
static ON: LazyLock<bool> = LazyLock::new(|| std::env::var_os("LEONIDA_PAD_QUICK").is_none_or(|v| v != "0"));
*ON
}
/// The stand-in pad of before `kbm`: one table whatever the player is in (`LEONIDA_KBM=classic`).
@ -571,9 +588,20 @@ impl Hub {
let hub = Hub::with(source, one_pad(), kbm::Config::from_env());
hub.poll();
std::thread::spawn(move || {
if !pad_quick() {
loop {
std::thread::sleep(POLL);
hub.poll();
}
}
// Leonida: the pads looked at every millisecond, everything read when one moved or a poll is due.
let mut polled = Instant::now();
loop {
std::thread::sleep(POLL);
hub.poll();
std::thread::sleep(PAD_LOOK);
if polled.elapsed() >= POLL || lock(&hub.source).pads_changed() {
polled = Instant::now();
hub.poll();
}
}
});
hub
@ -1399,6 +1427,10 @@ mod host {
/// The keys held down for real: not the scripted ones.
real_keys: Vec<KeyState>,
began: bool,
/// Leonida: XInput's packet number of each slot that had a pad, and the Sony readers' count, as
/// [`Source::pads_changed`] last saw them.
xin_packet: [Option<u32>; PADS],
sony_changes: u64,
}
/// `LEONIDA_MOUSE_LOOK=0` turns mouse look off (and with it the click that takes the mouse).
@ -1571,6 +1603,31 @@ mod host {
}
impl Source for Host {
fn pads_changed(&mut self) -> bool {
let mut changed = false;
for slot in 0..PADS {
// Only the slots that had a pad at the last poll: asking XInput about an empty slot is slow, and
// a pad plugged in is found by the next poll.
if !self.xin_connected[slot] {
self.xin_packet[slot] = None;
continue;
}
let mut state = XINPUT_STATE::default();
// SAFETY: room for the state.
let packet = (unsafe { XInputGetState(slot as u32, &mut state) } == 0).then_some(state.dwPacketNumber);
if packet != self.xin_packet[slot] {
self.xin_packet[slot] = packet;
changed = true;
}
}
let sony = self.sony.changes();
if sony != self.sony_changes {
self.sony_changes = sony;
changed = true;
}
changed
}
fn pad(&mut self, slot: usize) -> Option<PadState> {
// Leonida: a Sony pad read over HID takes the first slot XInput leaves empty, unless XInput is that
// same pad through a remapper.

View File

@ -182,7 +182,7 @@ pub(super) use imp::{Sony, View};
#[cfg(windows)]
mod imp {
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
@ -215,6 +215,9 @@ mod imp {
struct Shared {
latest: Mutex<Latest>,
alive: AtomicBool,
/// Readings that differed from the one before: the poll reads the pads at once when it moves
/// ([`Sony::changes`]).
changes: AtomicU64,
}
/// An open Sony pad. The handles are plain numbers so the pad can cross threads.
@ -283,6 +286,12 @@ mod imp {
view
}
/// How many readings of the open pads differed from the one before, all together: a number that moves
/// when a pad's input changes (and when a pad goes), cheap to look at between polls.
pub(in crate::modules::gameinput) fn changes(&self) -> u64 {
self.pads.iter().map(|p| p.shared.changes.load(Ordering::Acquire)).fold(0, u64::wrapping_add)
}
/// Both motors, as XInput speeds. Only what changed is sent.
pub(in crate::modules::gameinput) fn rumble(&mut self, low: u16, high: u16) {
if self.last_rumble == (low, high) {
@ -441,7 +450,8 @@ mod imp {
// SAFETY: a 41-byte buffer, as the report is.
unsafe { HidD_GetFeature(write, feature.as_mut_ptr().cast(), feature.len() as u32) };
}
let shared = Arc::new(Shared { latest: Mutex::default(), alive: AtomicBool::new(true) });
let shared =
Arc::new(Shared { latest: Mutex::default(), alive: AtomicBool::new(true), changes: AtomicU64::new(0) });
let size = usize::from(caps.InputReportByteLength);
let reader = Arc::clone(&shared);
let read = read as usize;
@ -449,6 +459,7 @@ mod imp {
.name("sony-pad".into())
.spawn(move || {
let mut buffer = vec![0u8; size.max(78)];
let mut last: Option<PadState> = None;
loop {
let mut got = 0;
// SAFETY: a buffer of `size` bytes and a handle this thread owns.
@ -458,6 +469,12 @@ mod imp {
}
if let Some(pad) = parse(model, &buffer[..got as usize]) {
reader.latest.lock().unwrap_or_else(|e| e.into_inner()).pad = Some(pad);
// (Pads send reports hundreds of times a second whether or not anything moved:
// only a reading unlike the last is counted.)
if last != Some(pad) {
last = Some(pad);
reader.changes.fetch_add(1, Ordering::Release);
}
}
}
reader.alive.store(false, Ordering::Release);