controller: give the DualShock 4 the DualSense pad path and release p… #453

Manually merged
github merged 1 commit from gh-pr/453/head into gh-pr/453/base 2026-11-26 09:21:33 +00:00
Owner

@⁠MehmetCambaz on GitHub · 2026-09-02 14:52 UTC · original
MehmetCambaz:fix/ds4-bluetooth-and-pad-release → main · +50 −8 · 5 files · 1 commits · merged 2026-09-03 22:10 UTC

Fixes #441.

What was wrong

Two problems that only appear together, on a real Bluetooth DualShock 4.

1. The DS4 was never given SDL's extended report mode. Rumble, the light bar, touch pad
coordinates and the motion sensors are only reported by a PlayStation pad in the mode SDL calls
"enhanced". window.cpp asked for it for the DualSense (SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE)
and never for the DualShock 4. Over USB this is masked, because HIDAPI_DriverPS4_InitDevice
enables enhanced mode unconditionally for a Sony vendor id. Over Bluetooth the pad stays in the
simple report mode, so a DS4 silently lost all four features — scePadSetVibration and
scePadSetLightBar returned success and did nothing.

2. Host pads were never released at exit. The emulator ends via std::quick_exit(0), which
runs at_quick_exit handlers only, so the Subsystems destructor and every lifecycle shutdown
are skipped. The controller lifecycle declared no emergency_shutdown, so
SDL_GameControllerClose() and SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER) never ran. SDL sends
the stop-rumble command from SDL_JoystickClose(), so a pad vibrating when the window closes
keeps vibrating, with no process left alive to stop it
— only powering the pad off ends it. SDL
also never restores the light bar, and enhanced mode is (in SDL's own comment) "a one-way trip",
so the bar stays lit at the last colour sent.

The second problem is what #441 describes. The first is why it was invisible over Bluetooth: with
effects never enabled, there was nothing left running to notice.

The fix

  • Set SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE alongside the existing PS5 hint.
  • Declare emergency_shutdown on the controller lifecycle and release host pads there: stop the
    motors, put the light bar out, allow SDL's rumble thread a bounded window to drain the queued
    reports, then close the pads.

Hardware validation

DualShock 4 v2 (054C:09CC) over Bluetooth, Windows 11, Tetris Forever. The window was closed
with a real WM_CLOSE every time, which is the SDL_QUIT -> quick_exit path under test.

Light bar after the emulator exits:

enhanced mode pad release light bar
off (before) no sky blue — the pad's own colour, SDL never touched it
off yes sky blue — nothing to release
on no dark blue, i.e. SDL's player-0 {0x00,0x00,0x40}, left lit
on (after) yes off

Rumble, driven through GameController::SetVibration:

pad release after the emulator exits
before still vibrating at full strength, indefinitely
after stops immediately

Capabilities, read from SDL rather than judged by eye:

before after
SDL_GameControllerHasLED 0 1
SDL_GameControllerHasRumble 0 1
SDL_GameControllerSetLED -1 "That operation is not supported" 0
touch pad coordinates never delivered 226 events, x 0.065..0.973, y 0.057..1.000

No regression in what already worked: all four face buttons, D-pad, L1/R1, L3/R3, Options, PS,
touch pad click, both sticks at full range, and L2/R2 analog 0..32767 were verified before and
after by the existing KYTY_DBG_INPUT trace.

Tests

New tests/SubsystemsTests.cpp (ctest -R subsystems) covers the mechanism the fix relies on:
emergency shutdown must run exactly the lifecycles that declare a handler, in reverse
initialization order, must not run them twice, and the controller lifecycle must be one of them.
It fails to compile against the unfixed tree, with the static assertion naming the reason.

ctest goes from 34/36 to 35/37 on Windows. The two failures
(shader_recompiler_compute, texture_cache_image_overlap, both 0xc0000409) are pre-existing
on fc91fd2 and unrelated.

Limitations

  • Motion sensors still do nothing. PadData has the orientation, acceleration and angular
    velocity fields and scePadSetMotionSensorState returns OK, but there is no SDL sensor plumbing
    at all, so the guest still receives zeros. Enhanced mode is a precondition for fixing that, not
    a fix for it.
  • scePadResetLightBar remains a stub that returns OK without touching the pad.
  • Rumble and the light bar were confirmed through SDL's capability bits and return codes plus
    direct observation of the physical pad; no test title was found that calls
    scePadSetVibration, so the guest-initiated path was exercised through
    GameController::SetVibration directly rather than by a game.
  • Tested on a DualShock 4 over Bluetooth. The USB path and the DualSense are unchanged by
    reasoning about the driver source, not by measurement — I have neither to hand.

AI use

This change was produced with AI assistance (Claude). The investigation, the SDL driver analysis,
the patch and the test were AI-generated; I reviewed the diff, ran the build and ctest myself,
and performed all of the physical DualShock 4 testing above by hand.

**@⁠MehmetCambaz** on GitHub · 2026-09-02 14:52 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/453) `MehmetCambaz:fix/ds4-bluetooth-and-pad-release` → `main` · +50 −8 · 5 files · 1 commits · merged 2026-09-03 22:10 UTC Fixes #441. ## What was wrong Two problems that only appear together, on a real Bluetooth DualShock 4. **1. The DS4 was never given SDL's extended report mode.** Rumble, the light bar, touch pad coordinates and the motion sensors are only reported by a PlayStation pad in the mode SDL calls "enhanced". `window.cpp` asked for it for the DualSense (`SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE`) and never for the DualShock 4. Over USB this is masked, because `HIDAPI_DriverPS4_InitDevice` enables enhanced mode unconditionally for a Sony vendor id. Over Bluetooth the pad stays in the simple report mode, so a DS4 silently lost all four features — `scePadSetVibration` and `scePadSetLightBar` returned success and did nothing. **2. Host pads were never released at exit.** The emulator ends via `std::quick_exit(0)`, which runs `at_quick_exit` handlers only, so the `Subsystems` destructor and every lifecycle shutdown are skipped. The controller lifecycle declared no `emergency_shutdown`, so `SDL_GameControllerClose()` and `SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER)` never ran. SDL sends the stop-rumble command from `SDL_JoystickClose()`, so **a pad vibrating when the window closes keeps vibrating, with no process left alive to stop it** — only powering the pad off ends it. SDL also never restores the light bar, and enhanced mode is (in SDL's own comment) "a one-way trip", so the bar stays lit at the last colour sent. The second problem is what #441 describes. The first is why it was invisible over Bluetooth: with effects never enabled, there was nothing left running to notice. ## The fix - Set `SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE` alongside the existing PS5 hint. - Declare `emergency_shutdown` on the controller lifecycle and release host pads there: stop the motors, put the light bar out, allow SDL's rumble thread a bounded window to drain the queued reports, then close the pads. ## Hardware validation DualShock 4 v2 (`054C:09CC`) over **Bluetooth**, Windows 11, Tetris Forever. The window was closed with a real `WM_CLOSE` every time, which is the `SDL_QUIT` -> `quick_exit` path under test. Light bar after the emulator exits: | enhanced mode | pad release | light bar | | --- | --- | --- | | off (before) | no | sky blue — the pad's own colour, SDL never touched it | | off | yes | sky blue — nothing to release | | on | no | **dark blue**, i.e. SDL's player-0 `{0x00,0x00,0x40}`, left lit | | on (after) | yes | **off** | Rumble, driven through `GameController::SetVibration`: | pad release | after the emulator exits | | --- | --- | | before | **still vibrating at full strength**, indefinitely | | after | **stops immediately** | Capabilities, read from SDL rather than judged by eye: | | before | after | | --- | --- | --- | | `SDL_GameControllerHasLED` | 0 | 1 | | `SDL_GameControllerHasRumble` | 0 | 1 | | `SDL_GameControllerSetLED` | -1 "That operation is not supported" | 0 | | touch pad coordinates | never delivered | 226 events, x 0.065..0.973, y 0.057..1.000 | No regression in what already worked: all four face buttons, D-pad, L1/R1, L3/R3, Options, PS, touch pad click, both sticks at full range, and L2/R2 analog 0..32767 were verified before and after by the existing `KYTY_DBG_INPUT` trace. ## Tests New `tests/SubsystemsTests.cpp` (`ctest -R subsystems`) covers the mechanism the fix relies on: emergency shutdown must run exactly the lifecycles that declare a handler, in reverse initialization order, must not run them twice, and the controller lifecycle must be one of them. It fails to compile against the unfixed tree, with the static assertion naming the reason. `ctest` goes from 34/36 to 35/37 on Windows. The two failures (`shader_recompiler_compute`, `texture_cache_image_overlap`, both `0xc0000409`) are pre-existing on `fc91fd2` and unrelated. ## Limitations - Motion sensors still do nothing. `PadData` has the orientation, acceleration and angular velocity fields and `scePadSetMotionSensorState` returns OK, but there is no SDL sensor plumbing at all, so the guest still receives zeros. Enhanced mode is a precondition for fixing that, not a fix for it. - `scePadResetLightBar` remains a stub that returns OK without touching the pad. - Rumble and the light bar were confirmed through SDL's capability bits and return codes plus direct observation of the physical pad; no test title was found that calls `scePadSetVibration`, so the guest-initiated path was exercised through `GameController::SetVibration` directly rather than by a game. - Tested on a DualShock 4 over Bluetooth. The USB path and the DualSense are unchanged by reasoning about the driver source, not by measurement — I have neither to hand. ## AI use This change was produced with AI assistance (Claude). The investigation, the SDL driver analysis, the patch and the test were AI-generated; I reviewed the diff, ran the build and `ctest` myself, and performed all of the physical DualShock 4 testing above by hand.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:21:31 +00:00
github manually merged commit 0ebfad6886 into gh-pr/453/base 2026-11-26 09:21:33 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.