mirror of
https://github.com/readmet3xt-lab/foxhound.git
synced 2026-12-06 08:58:36 +00:00
GameInput: the Sony pad reader takes reports as long as Windows gives them
Over Bluetooth a DualShock 4's input reports are 547 bytes as Windows gives them; the reader's 128-byte buffer made its first read fail, so it left and the pad was found anew every second without a reading (issue #419). The reader, the feature read and the output reports now use the lengths from HidP_GetCaps; a pad whose input reports are longer than 64 bytes is also taken as Bluetooth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -248,7 +248,8 @@ pub(super) mod devices {
|
||||
SetupDiDestroyDeviceInfoList, SetupDiEnumDeviceInterfaces, SetupDiGetClassDevsW, SetupDiGetDeviceInterfaceDetailW,
|
||||
};
|
||||
use windows_sys::Win32::Devices::HumanInterfaceDevice::{
|
||||
GUID_DEVINTERFACE_HID, HIDD_ATTRIBUTES, HidD_GetAttributes, HidD_GetFeature, HidD_SetOutputReport,
|
||||
GUID_DEVINTERFACE_HID, HIDD_ATTRIBUTES, HIDP_CAPS, HIDP_STATUS_SUCCESS, HidD_FreePreparsedData, HidD_GetAttributes,
|
||||
HidD_GetFeature, HidD_GetPreparsedData, HidD_SetOutputReport, HidP_GetCaps,
|
||||
};
|
||||
use windows_sys::Win32::Foundation::{CloseHandle, GENERIC_READ, GENERIC_WRITE, HANDLE, INVALID_HANDLE_VALUE};
|
||||
use windows_sys::Win32::Storage::FileSystem::{
|
||||
@ -277,6 +278,51 @@ pub(super) mod devices {
|
||||
alive: Arc<AtomicBool>,
|
||||
reader: Option<std::thread::JoinHandle<()>>,
|
||||
seq: u8,
|
||||
/// The length Windows wants of an output report to this device (0: not known).
|
||||
output_len: usize,
|
||||
}
|
||||
|
||||
/// The lengths Windows gives this device's input, feature and output
|
||||
/// reports (its top-level collection's), 0 for one that could not be
|
||||
/// read. Over Bluetooth they are longer than the pad's own reports (547
|
||||
/// bytes for a DualShock 4), and Windows refuses a read into a shorter
|
||||
/// buffer: with a fixed 128-byte buffer the reader left at its first read
|
||||
/// and the pad was found anew every second without giving a reading.
|
||||
fn report_lengths(handle: HANDLE) -> (usize, usize, usize) {
|
||||
let mut data = 0;
|
||||
// SAFETY: our open handle and a writable out value.
|
||||
if !unsafe { HidD_GetPreparsedData(handle, &mut data) } {
|
||||
return (0, 0, 0);
|
||||
}
|
||||
let mut caps = HIDP_CAPS::default();
|
||||
// SAFETY: the data obtained above, released after its last use.
|
||||
let status = unsafe { HidP_GetCaps(data, &mut caps) };
|
||||
unsafe { HidD_FreePreparsedData(data) };
|
||||
if status != HIDP_STATUS_SUCCESS {
|
||||
return (0, 0, 0);
|
||||
}
|
||||
(
|
||||
usize::from(caps.InputReportByteLength),
|
||||
usize::from(caps.FeatureReportByteLength),
|
||||
usize::from(caps.OutputReportByteLength),
|
||||
)
|
||||
}
|
||||
|
||||
/// Whether a pad is connected over Bluetooth: its device path says so
|
||||
/// (`bluetooth_path`: `BTHENUM` or the Bluetooth HID service's GUID), or
|
||||
/// its input reports are longer than the 64 bytes these pads send over
|
||||
/// USB, which catches a Bluetooth path of another form.
|
||||
pub(super) fn bluetooth(path: &str, input_len: usize) -> bool {
|
||||
bluetooth_path(path) || input_len > 64
|
||||
}
|
||||
|
||||
/// `report` as long as Windows wants an output report to be (zeros
|
||||
/// after the pad's own bytes, the Bluetooth CRC stays where it is).
|
||||
pub(super) fn padded(mut report: Vec<u8>, output_len: usize) -> Vec<u8> {
|
||||
if report.len() < output_len {
|
||||
report.resize(output_len, 0);
|
||||
}
|
||||
report
|
||||
}
|
||||
|
||||
impl Drop for Pad {
|
||||
@ -316,7 +362,7 @@ pub(super) mod devices {
|
||||
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 data = rumble(pad.kind, pad.bt, left, right, pad.seq);
|
||||
let data = padded(rumble(pad.kind, pad.bt, left, right, pad.seq), pad.output_len);
|
||||
pad.seq = (pad.seq + 1) & 0xF;
|
||||
// SAFETY: our handle and a report of the pad's own size.
|
||||
let ok = unsafe { HidD_SetOutputReport(pad.handle as HANDLE, data.as_ptr().cast(), data.len() as u32) };
|
||||
@ -411,7 +457,8 @@ pub(super) mod devices {
|
||||
unsafe { CloseHandle(handle) };
|
||||
return None;
|
||||
};
|
||||
let bt = bluetooth_path(path);
|
||||
let (input_len, feature_len, output_len) = report_lengths(handle);
|
||||
let bt = bluetooth(path, input_len);
|
||||
if bt {
|
||||
// The feature read that switches the pad to its full reports:
|
||||
// 0x05 (41 bytes) for the DualShock, 0x20 (64) for the DualSense.
|
||||
@ -419,6 +466,8 @@ pub(super) mod devices {
|
||||
Kind::Ds4 => (0x05u8, 41usize),
|
||||
Kind::DualSense => (0x20, 64),
|
||||
};
|
||||
// (As long as Windows says this device's feature reports are, where it says.)
|
||||
let len = len.max(feature_len);
|
||||
let mut feature = vec![0u8; len];
|
||||
feature[0] = id;
|
||||
// SAFETY: our handle and a feature buffer of the report's size.
|
||||
@ -427,8 +476,8 @@ pub(super) mod devices {
|
||||
}
|
||||
// An output report too: the DualShock's sets the 4 ms poll, the
|
||||
// DualSense's starts its Bluetooth reporting.
|
||||
let init = rumble(kind, true, 0, 0, 0);
|
||||
// SAFETY: our handle and a report of the pad's own size.
|
||||
let init = padded(rumble(kind, true, 0, 0, 0), output_len);
|
||||
// SAFETY: our handle and a report of the size Windows wants.
|
||||
unsafe { HidD_SetOutputReport(handle, init.as_ptr().cast(), init.len() as u32) };
|
||||
}
|
||||
let state = Arc::new(Mutex::new(None));
|
||||
@ -438,7 +487,9 @@ pub(super) mod devices {
|
||||
let handle_num = handle as usize;
|
||||
let spawned = std::thread::Builder::new().name("hid-pad".into()).spawn(move || {
|
||||
let handle = handle_num as HANDLE;
|
||||
let mut buf = [0u8; 128];
|
||||
// As long as this device's input reports are for Windows (547 bytes for a
|
||||
// DualShock 4 on Bluetooth): a read into less is refused.
|
||||
let mut buf = vec![0u8; input_len.max(128)];
|
||||
while alive.load(Ordering::Relaxed) {
|
||||
let mut got = 0u32;
|
||||
// SAFETY: our handle and buffer; blocks until a report.
|
||||
@ -461,7 +512,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 { kind, bt, path: path.to_string(), handle: handle as usize, state, alive, reader: Some(reader), seq: 0, output_len })
|
||||
}
|
||||
}
|
||||
|
||||
@ -664,4 +715,55 @@ mod tests {
|
||||
assert_eq!(parse(Kind::Ds4, true, &[0x02; 64]), None);
|
||||
assert_eq!(parse(Kind::DualSense, true, &[0x31; 78]), None); // CRC all zeros
|
||||
}
|
||||
|
||||
/// The path Windows 11 gives a DualShock 4 paired over Bluetooth has the
|
||||
/// Bluetooth HID service's GUID and no `BTHENUM`; a USB path has neither,
|
||||
/// and tells by the length of its input reports.
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn a_paired_pad_is_known_as_bluetooth() {
|
||||
let paired = r"\\?\hid#{00001124-0000-1000-8000-00805f9b34fb}_vid&0002054c_pid&09cc#example";
|
||||
assert!(devices::bluetooth(paired, 547));
|
||||
assert!(devices::bluetooth(paired, 0));
|
||||
assert!(devices::bluetooth(r"\\?\hid#bthenum#x", 0));
|
||||
let usb = r"\\?\hid#vid_054c&pid_09cc&mi_03#example";
|
||||
assert!(!devices::bluetooth(usb, 64));
|
||||
assert!(!devices::bluetooth(usb, 0));
|
||||
assert!(devices::bluetooth(usb, 547));
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn output_reports_are_padded_to_what_windows_wants() {
|
||||
let report = rumble(Kind::Ds4, true, 1, 2, 0);
|
||||
let n = report.len();
|
||||
let padded = devices::padded(report.clone(), 547);
|
||||
assert_eq!(padded.len(), 547);
|
||||
assert_eq!(&padded[..n], &report[..]);
|
||||
assert!(padded[n..].iter().all(|&b| b == 0));
|
||||
assert_eq!(devices::padded(report.clone(), 0), report);
|
||||
}
|
||||
|
||||
/// Needs a Sony pad connected: prints what it reads of it for ten seconds.
|
||||
/// `cargo test -p victory-hle --lib connected_sony_pads -- --ignored --nocapture`
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
#[ignore = "needs a pad"]
|
||||
fn connected_sony_pads() {
|
||||
let mut pads = devices::Pads::default();
|
||||
let mut seen = 0;
|
||||
for _ in 0..100 {
|
||||
if let Some(state) = pads.pad(0) {
|
||||
seen += 1;
|
||||
if seen % 10 == 1 {
|
||||
println!(
|
||||
"pad 0: buttons {:#06x} lt {:.2} rt {:.2} left ({:.2}, {:.2}) right ({:.2}, {:.2})",
|
||||
state.buttons, state.left_trigger, state.right_trigger, state.left_x, state.left_y, state.right_x, state.right_y
|
||||
);
|
||||
}
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
}
|
||||
assert!(seen > 0, "no reading from a Sony pad in ten seconds");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user