Input: read DualSense pads over HID when XInput has none #150

Open
github-bot wants to merge 2 commits from github-bot/gh-150 into main AGit
Owner

@⁠Kam471 on GitHub · 2026-11-24 00:58 UTC · original
Kam471:input/dualsense-hid → main · +550 −13 · 5 files · 2 commits · open

A DualSense is not an XInput device, so the title never saw one even though GameInput reports pads. This reads the pad's HID input reports on a background thread and exposes them in the same shape as the XInput source, on slot 0 when no XInput pad answers there.

USB input reports (0x01, 64 bytes) and Bluetooth reports (0x31, 78 bytes) place the buttons and triggers at different offsets, so the fields are read through Windows' HID parser (HidP_GetUsages / HidP_GetUsageValue) by declared usage instead of fixed offsets. That also covers the DualSense Edge's report. Mapping: square/cross/circle/triangle to X/A/B/Y, L1/R1, create/options, L3/R3, PS to Menu, hat to D-pad, sticks, analog triggers.

Not included: touchpad, gyro and lightbar (the title's pad surface has no axes for them), and rumble back to the pad.

Keyboard O now also plays Circle (B) next to Enter's Cross; the README input table is updated.

Tests run:

  • cargo test -p victory-hle --lib dualsense (6 passed, 1 ignored hardware probe)
  • cargo test -p victory-hle (92 passed, 1 ignored)
  • cargo build --release -p victory

Verified live on Windows with a DualSense connected: the title registered a gamepad callback (kind 0x40000), pad 0 connected, and real button presses and analog triggers arrived.

**@⁠Kam471** on GitHub · 2026-11-24 00:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/150) `Kam471:input/dualsense-hid` → `main` · +550 −13 · 5 files · 2 commits · open A DualSense is not an XInput device, so the title never saw one even though GameInput reports pads. This reads the pad's HID input reports on a background thread and exposes them in the same shape as the XInput source, on slot 0 when no XInput pad answers there. USB input reports (`0x01`, 64 bytes) and Bluetooth reports (`0x31`, 78 bytes) place the buttons and triggers at different offsets, so the fields are read through Windows' HID parser (`HidP_GetUsages` / `HidP_GetUsageValue`) by declared usage instead of fixed offsets. That also covers the DualSense Edge's report. Mapping: square/cross/circle/triangle to X/A/B/Y, L1/R1, create/options, L3/R3, PS to Menu, hat to D-pad, sticks, analog triggers. Not included: touchpad, gyro and lightbar (the title's pad surface has no axes for them), and rumble back to the pad. Keyboard `O` now also plays Circle (B) next to Enter's Cross; the README input table is updated. Tests run: - `cargo test -p victory-hle --lib dualsense` (6 passed, 1 ignored hardware probe) - `cargo test -p victory-hle` (92 passed, 1 ignored) - `cargo build --release -p victory` Verified live on Windows with a DualSense connected: the title registered a gamepad callback (kind 0x40000), pad 0 connected, and real button presses and analog triggers arrived.
A DualSense is not an XInput device, so the title never saw one even though
GameInput reports pads. Read the pad's HID input reports on a thread and
expose them in the shape the XInput source returns, on slot 0 when no XInput
pad answers there.

USB input reports (0x01, 64 bytes) and Bluetooth reports (0x31, 78 bytes)
place the buttons and triggers at different offsets. Read the fields through
Windows' HID parser (HidP_GetUsages/HidP_GetUsageValue) by declared usage
instead of by fixed offsets, so both transports and the DualSense Edge share
one mapping. Square, cross, circle and triangle become X, A, B and Y;
shoulders, create/options, stick clicks and the hat map to their pad bits;
sticks are centered and Y is inverted. Touchpad, gyro and lightbar have no
pad surface in the title and rumble is not sent back yet.

The keyboard also plays Circle on O, next to Enter's Cross.

Tests: cargo test -p victory-hle --lib dualsense, cargo test -p victory-hle,
cargo build --release -p victory. Verified live on Windows: the title
registered a gamepad callback (kind 0x40000), pad 0 connected, and presses
and analog triggers arrived.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:52:17 +00:00
Author
Owner

@⁠CIS-C0 on GitHub · 2026-11-24 02:01 UTC · original

Tested this with a wired DualShock 4 (054C:05C4) after adding its product ids. Its HID descriptor declares the same usages this reads (X/Y/Z/Rz sticks, Rx/Ry triggers, hat, buttons 1–14 in the same order), so nothing else needed to change:

 const DUALSENSE_EDGE: u16 = 0x0df2;
+/// DualShock 4 (both models) and its USB wireless adaptor: their HID
+/// descriptor declares the same usages, so they read the same.
+const DUALSHOCK4: [u16; 3] = [0x05c4, 0x09cc, 0x0ba0];
@@
-            && (attributes.ProductID == DUALSENSE || attributes.ProductID == DUALSENSE_EDGE)
+            && (attributes.ProductID == DUALSENSE
+                || attributes.ProductID == DUALSENSE_EDGE
+                || DUALSHOCK4.contains(&attributes.ProductID))

Results on 34a0823 + that change (i5-11400F, RTX 2060, Windows 11 build 28000, toolchain 1.98.1 msvc):

  • cargo test -p victory-hle: 92 passed, 1 ignored; the ignored probe lists_and_opens_hid_devices passes and opens the DS4 (report length 64).
  • GTA VI with VICTORY_PAD_LOG=1: DualSense connected over HID product=0x05c4 report=64, pad 0 connects and the keyboard stand-in disconnects. Pressing every button in the menu gave all 14 bits (union 0x3fff), triggers reach 1.00, sticks −1 … 0.97, idle sticks within 0.2 of centre.

Not tested: the 09CC model, the USB adaptor, a DS4 over Bluetooth.

If you add it, the log line and module name say "DualSense" for a DS4 too; something like "PlayStation pad" would read better.

**@⁠CIS-C0** on GitHub · 2026-11-24 02:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/150#issuecomment-7119684684) Tested this with a wired **DualShock 4** (054C:05C4) after adding its product ids. Its HID descriptor declares the same usages this reads (X/Y/Z/Rz sticks, Rx/Ry triggers, hat, buttons 1–14 in the same order), so nothing else needed to change: ```diff const DUALSENSE_EDGE: u16 = 0x0df2; +/// DualShock 4 (both models) and its USB wireless adaptor: their HID +/// descriptor declares the same usages, so they read the same. +const DUALSHOCK4: [u16; 3] = [0x05c4, 0x09cc, 0x0ba0]; @@ - && (attributes.ProductID == DUALSENSE || attributes.ProductID == DUALSENSE_EDGE) + && (attributes.ProductID == DUALSENSE + || attributes.ProductID == DUALSENSE_EDGE + || DUALSHOCK4.contains(&attributes.ProductID)) ``` Results on 34a0823 + that change (i5-11400F, RTX 2060, Windows 11 build 28000, toolchain 1.98.1 msvc): - `cargo test -p victory-hle`: 92 passed, 1 ignored; the ignored probe `lists_and_opens_hid_devices` passes and opens the DS4 (report length 64). - GTA VI with `VICTORY_PAD_LOG=1`: `DualSense connected over HID product=0x05c4 report=64`, pad 0 connects and the keyboard stand-in disconnects. Pressing every button in the menu gave all 14 bits (union `0x3fff`), triggers reach 1.00, sticks −1 … 0.97, idle sticks within 0.2 of centre. Not tested: the 09CC model, the USB adaptor, a DS4 over Bluetooth. If you add it, the log line and module name say "DualSense" for a DS4 too; something like "PlayStation pad" would read better.
Author
Owner

@⁠Kam471 on GitHub · 2026-11-24 02:09 UTC · original

Thanks for testing the DualShock 4 and for the exact diff. I've added it: 0x05c4 (DS4), 0x09cc (DS4 v2) and 0x0ba0 (USB wireless adaptor) now go through the same usage-based HID read, with a unit test for the product ids. The log line and module docs now say "PlayStation pad" instead of DualSense, so a DS4 report isn't mislabelled. The 09cc/0ba0 variants are untested on hardware here; your 05c4 result covers the report layout they share.

**@⁠Kam471** on GitHub · 2026-11-24 02:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/150#issuecomment-7119813370) Thanks for testing the DualShock 4 and for the exact diff. I've added it: 0x05c4 (DS4), 0x09cc (DS4 v2) and 0x0ba0 (USB wireless adaptor) now go through the same usage-based HID read, with a unit test for the product ids. The log line and module docs now say "PlayStation pad" instead of DualSense, so a DS4 report isn't mislabelled. The 09cc/0ba0 variants are untested on hardware here; your 05c4 result covers the report layout they share.
Author
Owner

@⁠crissuper20 on GitHub · 2026-11-24 21:30 UTC · original

Crash: the HID device paths passed to CreateFileW aren't NUL-terminated.

In device_paths() (crates/victory-hle/src/modules/dualsense.rs), the path is copied without its terminator:

while unsafe { *path.add(len) } != 0 { len += 1; }
paths.push(unsafe { std::slice::from_raw_parts(path, len) }.to_vec());

Then open_pad() calls CreateFileW(path.as_ptr(), …), which reads until it finds a 0 u16. That runs past the end of the Vec's allocation. It works when the next bytes happen to be zero, and crashes when the allocation ends at a page boundary.

Seen in a run: Forge fd79f92 (#1624, which includes this PR at fe014c1), Windows 11 LTSC 24H2, i5-4690, no PlayStation pad connected, only a Logitech USB device and the usual HID devices. About 15 s after the world load starts, the playstation-pad thread dies with:

unhandled exception 0xc0000005 at ntdll.dll+0x12a004 (read 0x24f07497000: no access)
  #2  KERNELBASE.dll+0x80744
  #4  victory.exe -> dualsense.rs:115 (open_pad inlined into reader)

The faulting address is page-aligned, which fits a read just past the end of a heap allocation. Every machine enumerates HID devices here, with or without a pad, so this can hit anyone running a build that includes this PR.

Fix: keep the terminator, e.g.

let mut owned = unsafe { std::slice::from_raw_parts(path, len) }.to_vec();
owned.push(0);
paths.push(owned);

Verified: with only that change applied to the same Forge build, the playstation-pad crash is gone in a rerun on the same machine.

(A smaller nit nearby: buffer is a Vec<u8> cast to *mut SP_DEVICE_INTERFACE_DETAIL_DATA_W, which needs 4-byte alignment. A Vec<u32> of required.div_ceil(4) elements would make that cast sound.)

AI-assisted (Claude Code)

**@⁠crissuper20** on GitHub · 2026-11-24 21:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/150#issuecomment-7145022096) **Crash: the HID device paths passed to `CreateFileW` aren't NUL-terminated.** In `device_paths()` (`crates/victory-hle/src/modules/dualsense.rs`), the path is copied without its terminator: ```rust while unsafe { *path.add(len) } != 0 { len += 1; } paths.push(unsafe { std::slice::from_raw_parts(path, len) }.to_vec()); ``` Then `open_pad()` calls `CreateFileW(path.as_ptr(), …)`, which reads until it finds a 0 u16. That runs past the end of the `Vec`'s allocation. It works when the next bytes happen to be zero, and crashes when the allocation ends at a page boundary. **Seen in a run:** Forge `fd79f92` (#1624, which includes this PR at `fe014c1`), Windows 11 LTSC 24H2, i5-4690, no PlayStation pad connected, only a Logitech USB device and the usual HID devices. About 15 s after the world load starts, the `playstation-pad` thread dies with: ``` unhandled exception 0xc0000005 at ntdll.dll+0x12a004 (read 0x24f07497000: no access) #2 KERNELBASE.dll+0x80744 #4 victory.exe -> dualsense.rs:115 (open_pad inlined into reader) ``` The faulting address is page-aligned, which fits a read just past the end of a heap allocation. Every machine enumerates HID devices here, with or without a pad, so this can hit anyone running a build that includes this PR. **Fix:** keep the terminator, e.g. ```rust let mut owned = unsafe { std::slice::from_raw_parts(path, len) }.to_vec(); owned.push(0); paths.push(owned); ``` **Verified:** with only that change applied to the same Forge build, the `playstation-pad` crash is gone in a rerun on the same machine. (A smaller nit nearby: `buffer` is a `Vec<u8>` cast to `*mut SP_DEVICE_INTERFACE_DETAIL_DATA_W`, which needs 4-byte alignment. A `Vec<u32>` of `required.div_ceil(4)` elements would make that cast sound.) AI-assisted (Claude Code)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:18 UTC · original

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-hle/src/modules/mod.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/150#issuecomment-7148363934) <!-- victory-triage v1 head=fe014c1b50ed72e16dca5f101fe1a2219fea6518 verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-hle/src/modules/mod.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠WHOISJARDAS on GitHub · 2026-11-25 12:35 UTC · original

A bug in device_paths (at fe014c1): each path is copied without its NUL (std::slice::from_raw_parts(path, len).to_vec()), and open_pad passes it to CreateFileW, which reads up to a NUL, past the end of the copy. With no DualSense connected the scan runs every 2 s, and once a copy ends a heap page the read faults. It happened once 231 s into a run in a fork that carries this PR (RX 9070 XT, no DualSense):

0xc0000005 at ntdll!wcslen+0x74 (read: no access)
  <- RtlInitUnicodeStringEx <- KERNELBASE!CreateFileInternal <- CreateFileW <- dualsense::reader (open_pad)

Fix: keep the NUL, let mut path = ...to_vec(); path.push(0); paths.push(path); (and drop it where a path is printed). A test that every returned path ends in exactly one NUL fails without that and passes with it: Belobug/VIctory-Belobug-AMD#40.

AI-assisted (Claude Code).

**@⁠WHOISJARDAS** on GitHub · 2026-11-25 12:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/150#issuecomment-7162576981) A bug in `device_paths` (at `fe014c1`): each path is copied without its NUL (`std::slice::from_raw_parts(path, len).to_vec()`), and `open_pad` passes it to `CreateFileW`, which reads up to a NUL, past the end of the copy. With no DualSense connected the scan runs every 2 s, and once a copy ends a heap page the read faults. It happened once 231 s into a run in a fork that carries this PR (RX 9070 XT, no DualSense): ``` 0xc0000005 at ntdll!wcslen+0x74 (read: no access) <- RtlInitUnicodeStringEx <- KERNELBASE!CreateFileInternal <- CreateFileW <- dualsense::reader (open_pad) ``` Fix: keep the NUL, `let mut path = ...to_vec(); path.push(0); paths.push(path);` (and drop it where a path is printed). A test that every returned path ends in exactly one NUL fails without that and passes with it: Belobug/VIctory-Belobug-AMD#40. AI-assisted (Claude Code).
This pull request has changes conflicting with the target branch.
  • README.md
  • crates/victory-hle/src/modules/mod.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/150/head:github-bot/gh-150'
git switch 'github-bot/gh-150'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-150'
git switch 'github-bot/gh-150'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-150'
git switch 'github-bot/gh-150'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-150'
git switch 'main'
git merge --squash 'github-bot/gh-150'
git switch 'main'
git merge --ff-only 'github-bot/gh-150'
git switch 'main'
git merge 'github-bot/gh-150'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!150
No description provided.