diff --git a/crates/victory-hle/src/modules/gameinput.rs b/crates/victory-hle/src/modules/gameinput.rs index a94a49f..e394fcc 100644 --- a/crates/victory-hle/src/modules/gameinput.rs +++ b/crates/victory-hle/src/modules/gameinput.rs @@ -1366,13 +1366,12 @@ mod host { #[derive(Default)] pub(super) struct Host { + /// Slots last reported empty by XInput and the deadline to probe them again. empty_until: [Option; PADS], /// The mouse's velocity (counts a second) and when it was last read. mouse: (f32, f32, Option), /// Sony pads on raw HID, in the slots XInput leaves free. hid: hid::devices::Pads, - /// Which slots a HID pad answered last, so rumble reaches it. - hid_slot: [bool; PADS], } impl Host { @@ -1384,7 +1383,6 @@ mod host { return None; } let mut pad = self.hid.pad(slot)?; - self.hid_slot[slot] = true; if slot == 0 { for vk in scripted() { if let Some(&(_, button)) = super::FALLBACK_BUTTONS.iter().find(|(key, _)| *key == vk) { @@ -1407,8 +1405,9 @@ mod host { self.empty_until[slot] = Some(Instant::now() + EMPTY_SLOT_RECHECK); return self.hid_pad(slot); } - self.empty_until[slot] = None; - self.hid_slot[slot] = false; + if self.empty_until[slot].take().is_some() { + self.hid.release_slot(slot); + } #[allow(unused_mut)] let mut pad = convert(&state.Gamepad); // The scripted keys (`VICTORY_KEYS`) also press the first @@ -1477,7 +1476,7 @@ mod host { } fn rumble(&mut self, slot: usize, low: u16, high: u16) { - if self.hid_slot[slot] { + if self.empty_until[slot].is_some() { self.hid.rumble(slot, (low >> 8) as u8, (high >> 8) as u8); return; } diff --git a/crates/victory-hle/src/modules/gameinput/hid.rs b/crates/victory-hle/src/modules/gameinput/hid.rs index 9a3d06b..9d7da76 100644 --- a/crates/victory-hle/src/modules/gameinput/hid.rs +++ b/crates/victory-hle/src/modules/gameinput/hid.rs @@ -236,6 +236,25 @@ pub(super) fn rumble(kind: Kind, bt: bool, left: u8, right: u8, seq: u8) -> Vec< data } +/// Keep the HID device already assigned to this host slot. Otherwise take the +/// first unassigned device. Store the slot on the device so removing a reader +/// from the list cannot transfer its slot to the next device. +#[cfg(any(windows, test))] +fn claim_slot<'a>(owners: impl Iterator>, slot: usize) -> Option { + let mut first_free = None; + for (index, owner) in owners.enumerate() { + if *owner == Some(slot) { + return Some(index); + } + if owner.is_none() && first_free.is_none() { + first_free = Some((index, owner)); + } + } + let (index, owner) = first_free?; + *owner = Some(slot); + Some(index) +} + /// The pads' device side: enumeration, one reader thread per pad, rumble. #[cfg(windows)] pub(super) mod devices { @@ -256,7 +275,7 @@ pub(super) mod devices { }; use windows_sys::Win32::System::IO::CancelIoEx; - use super::{Kind, PadState, bluetooth_path, kind, parse, rumble}; + use super::{Kind, PadState, bluetooth_path, claim_slot, kind, parse, rumble}; /// How often to look for pads plugged in or taken out. const ENUM_EVERY: std::time::Duration = std::time::Duration::from_secs(1); @@ -268,6 +287,7 @@ pub(super) mod devices { /// writes to. The thread owns the reading loop; the handle stays open /// until the thread has been cancelled and joined. struct Pad { + slot: Option, kind: Kind, bt: bool, path: String, @@ -303,7 +323,8 @@ pub(super) mod devices { /// take the slots XInput leaves free, in enumeration order. pub(in crate::modules::gameinput) fn pad(&mut self, slot: usize) -> Option { self.enumerate(); - let pad = self.pads.get(slot)?; + let index = claim_slot(self.pads.iter_mut().map(|pad| &mut pad.slot), slot)?; + let pad = &self.pads[index]; let state = pad.state.lock().unwrap_or_else(|e| e.into_inner()); let (state, at) = state.as_ref()?; if at.elapsed().as_millis() > STALE_MS { @@ -312,10 +333,19 @@ pub(super) mod devices { Some(*state) } + /// An XInput device has taken this slot; the HID device can move to a free one. + pub(in crate::modules::gameinput) fn release_slot(&mut self, slot: usize) { + if let Some(pad) = self.pads.iter_mut().find(|pad| pad.slot == Some(slot)) { + pad.slot = None; + } + } + /// Motor speeds (0 to 255 each) to the pad in `slot`. pub(in crate::modules::gameinput) fn rumble(&mut self, slot: usize, left: u8, right: u8) { - self.enumerate(); - let Some(pad) = self.pads.get_mut(slot) else { return }; + let Some(pad) = self.pads.iter_mut().find(|pad| pad.slot == Some(slot)) else { return }; + if !pad.alive.load(Ordering::Relaxed) { + return; + } let data = rumble(pad.kind, pad.bt, left, right, pad.seq); pad.seq = (pad.seq + 1) & 0xF; // SAFETY: our handle and a report of the pad's own size. @@ -461,7 +491,7 @@ pub(super) mod devices { } } }; - Some(Pad { kind, bt, path: path.to_string(), handle: handle as usize, state, alive, reader: Some(reader), seq: 0 }) + Some(Pad { slot: None, kind, bt, path: path.to_string(), handle: handle as usize, state, alive, reader: Some(reader), seq: 0 }) } } @@ -469,6 +499,26 @@ pub(super) mod devices { mod tests { use super::*; + #[test] + fn hid_pads_keep_their_host_slots_when_the_device_list_changes() { + let mut owners = vec![None, None]; + // XInput owns slot 0. Both Sony devices must still be found. + assert_eq!(claim_slot(owners.iter_mut(), 1), Some(0)); + assert_eq!(claim_slot(owners.iter_mut(), 2), Some(1)); + assert_eq!(claim_slot(owners.iter_mut(), 3), None); + // XInput takes slot 1. Slot 2 still belongs to the second pad, even + // though the first pad is free. Reassign the displaced pad to slot 3. + owners[0] = None; + assert_eq!(claim_slot(owners.iter_mut(), 2), Some(1)); + assert_eq!(claim_slot(owners.iter_mut(), 3), Some(0)); + // Unplug the first device and plug in a new one. The survivor keeps + // its slot even though its index in the HID device list changed. + owners.remove(0); + owners.push(None); + assert_eq!(claim_slot(owners.iter_mut(), 3), Some(1)); + assert_eq!(owners, [Some(2), Some(3)]); + } + /// The CRC-32 of the standard check string. #[test] fn crc32_matches_the_check_value() {