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

Open
Kam471 wants to merge 2 commits from upstream-pr/150 into main
Kam471 commented 2026-11-24 00:58:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Kam471 on 2026-11-24T00:58:08Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572145802 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/150). Originally posted by [Kam471](https://github.com/Kam471) on 2026-11-24T00:58:08Z. --- 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.
CIS-C0 commented 2026-11-24 06:55:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by CIS-C0 on 2026-11-24T02:01:47Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119684684 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/150#issuecomment-7119684684). Originally posted by [CIS-C0](https://github.com/CIS-C0) on 2026-11-24T02:01:47Z. --- 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.
Kam471 commented 2026-11-24 06:55:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Kam471 on 2026-11-24T02:09:31Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119813370 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/150#issuecomment-7119813370). Originally posted by [Kam471](https://github.com/Kam471) on 2026-11-24T02:09:31Z. --- 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.
crissuper20 commented 2026-11-24 22:02:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by crissuper20 on 2026-11-24T21:30:31Z.


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)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7145022096 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/150#issuecomment-7145022096). Originally posted by [crissuper20](https://github.com/crissuper20) on 2026-11-24T21:30:31Z. --- **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)
Marvisak commented 2026-11-25 01:07:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:18:00Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148363934 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/150#issuecomment-7148363934). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:18:00Z. --- <!-- 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)_
WHOISJARDAS commented 2026-11-25 13:02:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by WHOISJARDAS on 2026-11-25T12:35:10Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7162576981 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/150#issuecomment-7162576981). Originally posted by [WHOISJARDAS](https://github.com/WHOISJARDAS) on 2026-11-25T12:35:10Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/150:upstream-pr/150'
git switch 'upstream-pr/150'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/150'
git switch 'upstream-pr/150'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/150'
git switch 'upstream-pr/150'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/150'
git switch 'main'
git merge --squash 'upstream-pr/150'
git switch 'main'
git merge --ff-only 'upstream-pr/150'
git switch 'main'
git merge 'upstream-pr/150'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!150
No description provided.