videoout: bridge SDL sensor/haptics to pad output alongside existing input-mapping flow

Reconcile our SDL gamepad motion/haptics work with upstream's
configurable keyboard/mouse binding feature (PadInput now built around
Pad::LoadInputMapping() and a std::vector<Pad::InputBinding>). Upstream's
openFirstAvailableController()/openController(deviceIndex) flow and
HandleEvent/Update/publish logic for keyboard, mouse, and wheel
bindings are kept as-is (including APS5_NO_PAD_INPUT handling); the
sensor-enabling and host-haptics steps are grafted on as additional
work done once a controller is open:

- openController(deviceIndex) now also calls enableSensors() and
  marks a re-apply of the current guest output request pending.
- New sampleController()/applyOutput()/enableSensors() bring in real
  gamepad sampling, motion sensor reads, DualSense touchpad finger
  tracking, and pushing rumble/light bar/adaptive trigger state out via
  SDL_GameControllerRumble/SetLED/SendEffect. sampleController()'s
  button list and trigger/stick scaling are an exact copy of the
  button/trigger/stick reading that used to be inlined in publish()
  under the SDL_GameControllerGetAttached check (no deadzone added,
  no extra button remapped) -- only the motion, touchpad, and
  device-kind sampling on top of it are new.
- Update() now also samples the controller and applies pending output
  every tick, in addition to upstream's SDL_GameControllerUpdate() call
  and its keyboard/mouse/wheel timing logic.
- publish() seeds PadInputState from the sampled gamepad state first
  (buttons/sticks/analog triggers/motion/touch/device kind), then
  layers upstream's keyboard/mouse binding loop on top exactly as
  upstream had it, so a bound key still overrides the gamepad the same
  way it did before this merge. Upstream's L2/R2 button-flag derivation
  is preserved, just computed once at the end from whichever value
  (gamepad or a keyboard/mouse override) ends up in analogButtonsL2/R2.
- The background-events SDL hint is now set unconditionally (previously
  only inside the "not yet initialized" branch), since upstream's own
  VideoOutDriver already brings up SDL_INIT_GAMECONTROLLER before
  PadInput is constructed, which meant the hint would never run.

No changes were needed in VideoOutDriver.cpp: PadInput lazily
initializes SDL_INIT_GAMECONTROLLER itself in
openFirstAvailableController() (already a no-op if the subsystem is up),
and the class's public interface (default-constructible, HandleEvent/
Update) is unchanged, so upstream's existing `PadInput padInput;` /
HandleEvent / Update call sites keep working without modification.
VideoOutDriver.cpp's own independent flip-queue/vblank-telemetry rework
was left untouched as out of scope.

Confidence: high for the keyboard/mouse binding path (untouched) and
for the gamepad button/stick/trigger path (byte-for-byte copy of the
pre-existing mapping); the new motion/haptics/touchpad code is novel
and builds/links cleanly but has not been run against real hardware.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
OverkillLabs
2026-09-28 13:43:27 +03:00
parent 855176796b
commit 1d245cbeb2
2 changed files with 172 additions and 45 deletions
@@ -4,6 +4,7 @@
#include "SDL_events.h"
#include "SDL_gamecontroller.h"
#include "prx/libScePad/include/InputMapping.hpp"
#include "prx/libScePad/include/PadState.hpp"
#include <array>
#include <chrono>
#include <vector>
@@ -23,13 +24,24 @@ private:
void openFirstAvailableController();
void openController(int deviceIndex);
void closeController();
PadInputState sampleController() const;
void applyOutput();
void enableSensors();
std::vector<Pad::InputBinding> bindings;
std::vector<bool> pressed;
std::vector<std::chrono::steady_clock::time_point> wheelReleaseTimes;
std::array<std::uint8_t, 2> mouseStick{128, 128};
std::chrono::steady_clock::time_point nextMousePoll{};
SDL_GameController* controller = nullptr;
bool mouseEnabled = false;
// Host gamepad (Xbox / DualShock / DualSense / Switch Pro ... through SDL's GameController mapping database), merged with keyboard and mouse.
SDL_GameController* controller = nullptr;
PadInputState controllerState{};
// Guest output requests (rumble, light bar, adaptive triggers) mirrored onto the host pad.
std::uint32_t outputSequence = 0;
PadOutputState outputState{};
bool outputPending = false;
std::chrono::steady_clock::time_point nextRumbleRefresh{};
};
#endif
+159 -44
View File
@@ -2,6 +2,7 @@
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <string>
@@ -23,6 +24,15 @@ PadInput::~PadInput() {
void PadInput::openFirstAvailableController() {
if (controller != nullptr) return;
// Read continuously (not just when the SDL hint is checked at subsystem init), so this is set every
// time regardless of whether the video driver already brought SDL_INIT_GAMECONTROLLER up.
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); // keep reading the pad while the window is unfocused
if ((SDL_WasInit(SDL_INIT_GAMECONTROLLER) & SDL_INIT_GAMECONTROLLER) == 0) {
if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0) {
APS5_LOG_ERR("Pad: SDL game controller init failed: %s", SDL_GetError());
return; // no controller support: keyboard and mouse keep working
}
}
for (int deviceIndex = 0; deviceIndex < SDL_NumJoysticks(); ++deviceIndex) {
if (!SDL_IsGameController(deviceIndex)) continue;
openController(deviceIndex);
@@ -38,13 +48,67 @@ void PadInput::openController(int deviceIndex) {
return;
}
const char* name = SDL_GameControllerName(controller);
APS5_LOG_OUT("Pad: connected game controller: %s", name != nullptr ? name : "unknown");
APS5_LOG_OUT("Pad: connected game controller: %s (type %d, sensors accel=%d gyro=%d, touchpads=%d, led=%d, trigger rumble=%d)",
name != nullptr ? name : "unknown", static_cast<int>(SDL_GameControllerGetType(controller)),
SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL) == SDL_TRUE, SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) == SDL_TRUE,
SDL_GameControllerGetNumTouchpads(controller), SDL_GameControllerHasLED(controller) == SDL_TRUE, SDL_GameControllerHasRumbleTriggers(controller) == SDL_TRUE);
enableSensors();
outputPending = true; // re-apply the current guest output request to the newly opened pad
}
void PadInput::enableSensors() {
if (controller == nullptr) return;
const SDL_bool wanted = outputState.motionEnabled ? SDL_TRUE : SDL_FALSE;
if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_ACCEL) == SDL_TRUE) SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, wanted);
if (SDL_GameControllerHasSensor(controller, SDL_SENSOR_GYRO) == SDL_TRUE) SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_GYRO, wanted);
}
void PadInput::closeController() {
if (controller == nullptr) return;
SDL_GameControllerClose(controller);
controller = nullptr;
controllerState = {};
}
// Pushes the rumble / light bar / trigger requests of the guest to the host pad. Runs on the window thread that owns the SDL joystick.
void PadInput::applyOutput() {
PadOutputState fetched;
if (PadFetchOutput_nid_postfix(&outputSequence, &fetched)) {
const bool motionChanged = fetched.motionEnabled != outputState.motionEnabled;
outputState = fetched;
outputPending = true;
if (motionChanged) enableSensors();
}
if (controller == nullptr) return;
const auto now = std::chrono::steady_clock::now();
const bool rumbling = outputState.vibrationLarge != 0 || outputState.vibrationSmall != 0;
const bool triggerRumble = outputState.trigger[0].fallback != 0 || outputState.trigger[1].fallback != 0;
const bool isPs5 = SDL_GameControllerGetType(controller) == SDL_CONTROLLER_TYPE_PS5;
if (!outputPending) {
// Rumble requests expire host-side after a fixed time; keep them alive while the guest holds them.
if ((rumbling || (triggerRumble && !isPs5)) && now >= nextRumbleRefresh) outputPending = true;
else return;
}
outputPending = false;
nextRumbleRefresh = now + std::chrono::milliseconds(700);
constexpr Uint32 rumbleMs = 2000;
SDL_GameControllerRumble(controller, static_cast<Uint16>(outputState.vibrationLarge * 257), static_cast<Uint16>(outputState.vibrationSmall * 257), rumbling ? rumbleMs : 0);
if (SDL_GameControllerHasLED(controller) == SDL_TRUE) {
if (outputState.lightBarValid) SDL_GameControllerSetLED(controller, outputState.lightBar[0], outputState.lightBar[1], outputState.lightBar[2]);
else SDL_GameControllerSetLED(controller, 0, 64, 255);
}
if (outputState.triggerTouched) {
if (isPs5) {
// DS5EffectsState: enable bits 0x04 (right) / 0x08 (left) at byte 0, right effect block at +10, left effect block at +21 (47 bytes).
Uint8 effect[47] = {};
effect[0] = 0x04 | 0x08;
std::memcpy(effect + 10, outputState.trigger[1].effect, 11);
std::memcpy(effect + 21, outputState.trigger[0].effect, 11);
SDL_GameControllerSendEffect(controller, effect, sizeof(effect));
} else if (SDL_GameControllerHasRumbleTriggers(controller) == SDL_TRUE) {
SDL_GameControllerRumbleTriggers(controller, static_cast<Uint16>(outputState.trigger[0].fallback * 257), static_cast<Uint16>(outputState.trigger[1].fallback * 257), triggerRumble ? rumbleMs : 0);
}
}
}
void PadInput::setMouseMode(bool enabled) {
@@ -57,6 +121,82 @@ void PadInput::setMouseMode(bool enabled) {
nextMousePoll = std::chrono::steady_clock::now() + std::chrono::milliseconds(Pad::MousePollIntervalMs);
}
// SDL reports buttons by physical position (A = bottom face button), which on a PlayStation layout is Cross, so the mapping below is the
// PlayStation one for every pad type; a Switch-style layout is normalised by SDL's own mapping database.
PadInputState PadInput::sampleController() const {
PadInputState result;
if (controller == nullptr) return result;
// Button/trigger/stick mapping and scaling kept identical to the pre-existing gamepad support
// (SDL_GameControllerGetAttached block previously inlined in publish()); only motion, touchpad
// finger tracking, and device-kind detection are new here.
const auto readButton = [this](SDL_GameControllerButton button) {
return SDL_GameControllerGetButton(controller, button) != 0;
};
const auto addButton = [&result, &readButton](SDL_GameControllerButton source, Pad::PadButton button) {
if (readButton(source)) result.buttons |= static_cast<std::uint32_t>(button);
};
addButton(SDL_CONTROLLER_BUTTON_A, Pad::PadButton::Cross);
addButton(SDL_CONTROLLER_BUTTON_B, Pad::PadButton::Circle);
addButton(SDL_CONTROLLER_BUTTON_X, Pad::PadButton::Square);
addButton(SDL_CONTROLLER_BUTTON_Y, Pad::PadButton::Triangle);
addButton(SDL_CONTROLLER_BUTTON_LEFTSHOULDER, Pad::PadButton::L1);
addButton(SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, Pad::PadButton::R1);
addButton(SDL_CONTROLLER_BUTTON_START, Pad::PadButton::Options);
addButton(SDL_CONTROLLER_BUTTON_LEFTSTICK, Pad::PadButton::L3);
addButton(SDL_CONTROLLER_BUTTON_RIGHTSTICK, Pad::PadButton::R3);
addButton(SDL_CONTROLLER_BUTTON_DPAD_UP, Pad::PadButton::Up);
addButton(SDL_CONTROLLER_BUTTON_DPAD_RIGHT, Pad::PadButton::Right);
addButton(SDL_CONTROLLER_BUTTON_DPAD_DOWN, Pad::PadButton::Down);
addButton(SDL_CONTROLLER_BUTTON_DPAD_LEFT, Pad::PadButton::Left);
addButton(SDL_CONTROLLER_BUTTON_TOUCHPAD, Pad::PadButton::TouchPad);
const auto triggerValue = [this](SDL_GameControllerAxis axis) {
const auto value = std::clamp<int>(SDL_GameControllerGetAxis(controller, axis), 0, 32767);
return static_cast<std::uint8_t>((value * 255 + 16383) / 32767);
};
result.analogButtonsL2 = triggerValue(SDL_CONTROLLER_AXIS_TRIGGERLEFT);
result.analogButtonsR2 = triggerValue(SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
if (result.analogButtonsL2 != 0) result.buttons |= static_cast<std::uint32_t>(Pad::PadButton::L2);
if (result.analogButtonsR2 != 0) result.buttons |= static_cast<std::uint32_t>(Pad::PadButton::R2);
const auto stickValue = [this](SDL_GameControllerAxis axis) {
const auto value = static_cast<std::int32_t>(SDL_GameControllerGetAxis(controller, axis)) + 32768;
return static_cast<std::uint8_t>((value * 255 + 32767) / 65535);
};
result.sticks = {
stickValue(SDL_CONTROLLER_AXIS_LEFTX),
stickValue(SDL_CONTROLLER_AXIS_LEFTY),
stickValue(SDL_CONTROLLER_AXIS_RIGHTX),
stickValue(SDL_CONTROLLER_AXIS_RIGHTY)
};
switch (SDL_GameControllerGetType(controller)) {
case SDL_CONTROLLER_TYPE_PS5: result.deviceKind = 1; break;
case SDL_CONTROLLER_TYPE_PS4: result.deviceKind = 2; break;
default: result.deviceKind = 3; break;
}
if (SDL_GameControllerIsSensorEnabled(controller, SDL_SENSOR_ACCEL) == SDL_TRUE && SDL_GameControllerIsSensorEnabled(controller, SDL_SENSOR_GYRO) == SDL_TRUE) {
float accel[3];
float gyro[3];
if (SDL_GameControllerGetSensorData(controller, SDL_SENSOR_ACCEL, accel, 3) == 0 && SDL_GameControllerGetSensorData(controller, SDL_SENSOR_GYRO, gyro, 3) == 0) {
result.hasMotion = true;
for (int i = 0; i < 3; ++i) { result.accel[i] = accel[i]; result.gyro[i] = gyro[i]; }
}
}
if (SDL_GameControllerGetNumTouchpads(controller) > 0) {
for (int finger = 0; finger < 2; ++finger) {
Uint8 down = 0;
float x = 0.0f;
float y = 0.0f;
float pressure = 0.0f;
if (SDL_GameControllerGetTouchpadFinger(controller, 0, finger, &down, &x, &y, &pressure) != 0 || down == 0) continue;
result.touch[finger].active = true;
result.touch[finger].x = static_cast<std::uint16_t>(std::clamp(x, 0.0f, 1.0f) * 1919.0f);
result.touch[finger].y = static_cast<std::uint16_t>(std::clamp(y, 0.0f, 1.0f) * 942.0f);
}
}
return result;
}
void PadInput::HandleEvent(const SDL_Event& event, DisplayWindow& window) {
if (event.type == SDL_CONTROLLERDEVICEADDED) {
openController(event.cdevice.which);
@@ -124,11 +264,14 @@ void PadInput::HandleEvent(const SDL_Event& event, DisplayWindow& window) {
}
void PadInput::Update() {
const auto now = std::chrono::steady_clock::now();
if (controller != nullptr) SDL_GameControllerUpdate();
applyOutput();
if (controller != nullptr) {
SDL_GameControllerUpdate();
// Sensors change on every sample, so a pad with motion republishes continuously.
controllerState = sampleController();
publish();
}
const auto now = std::chrono::steady_clock::now();
bool released = false;
for (std::size_t index = 0; index < bindings.size(); ++index) {
if (bindings[index].wheelDirection == 0 || !pressed[index] || now < wheelReleaseTimes[index]) continue;
@@ -161,49 +304,19 @@ void PadInput::Update() {
}
void PadInput::publish() {
// Gamepad is the base layer (like a title reading a real DualSense); keyboard and mouse bindings are
// applied on top and win when they conflict with the gamepad, matching the pre-existing keyboard/mouse behaviour.
PadInputState state;
if (controller != nullptr && SDL_GameControllerGetAttached(controller) == SDL_TRUE) {
const auto readButton = [this](SDL_GameControllerButton button) {
return SDL_GameControllerGetButton(controller, button) != 0;
};
const auto addButton = [&state, &readButton](SDL_GameControllerButton source, Pad::PadButton button) {
if (readButton(source)) state.buttons |= static_cast<std::uint32_t>(button);
};
addButton(SDL_CONTROLLER_BUTTON_A, Pad::PadButton::Cross);
addButton(SDL_CONTROLLER_BUTTON_B, Pad::PadButton::Circle);
addButton(SDL_CONTROLLER_BUTTON_X, Pad::PadButton::Square);
addButton(SDL_CONTROLLER_BUTTON_Y, Pad::PadButton::Triangle);
addButton(SDL_CONTROLLER_BUTTON_LEFTSHOULDER, Pad::PadButton::L1);
addButton(SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, Pad::PadButton::R1);
addButton(SDL_CONTROLLER_BUTTON_START, Pad::PadButton::Options);
addButton(SDL_CONTROLLER_BUTTON_LEFTSTICK, Pad::PadButton::L3);
addButton(SDL_CONTROLLER_BUTTON_RIGHTSTICK, Pad::PadButton::R3);
addButton(SDL_CONTROLLER_BUTTON_DPAD_UP, Pad::PadButton::Up);
addButton(SDL_CONTROLLER_BUTTON_DPAD_RIGHT, Pad::PadButton::Right);
addButton(SDL_CONTROLLER_BUTTON_DPAD_DOWN, Pad::PadButton::Down);
addButton(SDL_CONTROLLER_BUTTON_DPAD_LEFT, Pad::PadButton::Left);
addButton(SDL_CONTROLLER_BUTTON_TOUCHPAD, Pad::PadButton::TouchPad);
state.buttons = controllerState.buttons;
state.sticks = controllerState.sticks;
state.analogButtonsL2 = controllerState.analogButtonsL2;
state.analogButtonsR2 = controllerState.analogButtonsR2;
state.hasMotion = controllerState.hasMotion;
state.accel = controllerState.accel;
state.gyro = controllerState.gyro;
state.touch = controllerState.touch;
state.deviceKind = controllerState.deviceKind;
const auto triggerValue = [this](SDL_GameControllerAxis axis) {
const auto value = std::clamp<int>(SDL_GameControllerGetAxis(controller, axis), 0, 32767);
return static_cast<std::uint8_t>((value * 255 + 16383) / 32767);
};
state.analogButtonsL2 = triggerValue(SDL_CONTROLLER_AXIS_TRIGGERLEFT);
state.analogButtonsR2 = triggerValue(SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
if (state.analogButtonsL2 != 0) state.buttons |= static_cast<std::uint32_t>(Pad::PadButton::L2);
if (state.analogButtonsR2 != 0) state.buttons |= static_cast<std::uint32_t>(Pad::PadButton::R2);
const auto stickValue = [this](SDL_GameControllerAxis axis) {
const auto value = static_cast<std::int32_t>(SDL_GameControllerGetAxis(controller, axis)) + 32768;
return static_cast<std::uint8_t>((value * 255 + 32767) / 65535);
};
state.sticks = {
stickValue(SDL_CONTROLLER_AXIS_LEFTX),
stickValue(SDL_CONTROLLER_AXIS_LEFTY),
stickValue(SDL_CONTROLLER_AXIS_RIGHTX),
stickValue(SDL_CONTROLLER_AXIS_RIGHTY)
};
}
std::array<bool, 4> negative{};
std::array<bool, 4> positive{};
for (std::size_t index = 0; index < bindings.size(); ++index) {
@@ -236,5 +349,7 @@ void PadInput::publish() {
state.sticks[2] = mouseStick[0];
state.sticks[3] = mouseStick[1];
}
if (state.analogButtonsL2 != 0) state.buttons |= static_cast<std::uint32_t>(Pad::PadButton::L2);
if (state.analogButtonsR2 != 0) state.buttons |= static_cast<std::uint32_t>(Pad::PadButton::R2);
PadPublishInput_nid_postfix(state);
}