|
| 1 | +#include "SDL3.h" |
| 2 | + |
| 3 | +#include "Input/Input.h" |
| 4 | + |
| 5 | +#include <SDL3/SDL.h> |
| 6 | +#include <algorithm> |
| 7 | +#include <cmath> |
| 8 | +#include <string> |
| 9 | + |
| 10 | +#include "ModApi.h" |
| 11 | + |
| 12 | +extern ModApi *api; |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +SDL_Gamepad *activeGamepad = nullptr; |
| 17 | +int deviceIndex; |
| 18 | + |
| 19 | +float stickDeadzone; |
| 20 | +float stickOuterDeadzone; |
| 21 | +float triggerDeadzone; |
| 22 | +float triggerOuterDeadzone; |
| 23 | + |
| 24 | +const float stickScale = 128.0f; |
| 25 | +const float triggerScale = 128.0f; |
| 26 | + |
| 27 | +using std::min, std::max; |
| 28 | + |
| 29 | +template <typename T> |
| 30 | +int sign(T val) { |
| 31 | + return (T(0) < val) - (val < T(0)); |
| 32 | +} |
| 33 | + |
| 34 | +float applyStickDeadzone(float rawInput, float inner, float outer) { |
| 35 | + float absVal = std::abs(rawInput); |
| 36 | + if (absVal <= inner) |
| 37 | + return 0.0f; |
| 38 | + if (absVal >= outer) |
| 39 | + return sign(rawInput) * 1.0f; |
| 40 | + float normalized = (absVal - inner) / (outer - inner); |
| 41 | + return sign(rawInput) * normalized; |
| 42 | +} |
| 43 | + |
| 44 | +float applyTriggerDeadzone(float rawInput, float inner, float outer) { |
| 45 | + if (rawInput <= inner) |
| 46 | + return 0.0f; |
| 47 | + if (rawInput >= outer) |
| 48 | + return 1.0f; |
| 49 | + float normalized = (rawInput - inner) / (outer - inner); |
| 50 | + return normalized; |
| 51 | +} |
| 52 | + |
| 53 | +void RefreshGamepadConnection() { |
| 54 | + if (activeGamepad) |
| 55 | + return; |
| 56 | + |
| 57 | + int count = 0; |
| 58 | + SDL_JoystickID *joysticks = SDL_GetGamepads(&count); |
| 59 | + if (joysticks && count > 0) { |
| 60 | + int targetIndex = min(max(deviceIndex, 0), count - 1); |
| 61 | + activeGamepad = SDL_OpenGamepad(joysticks[targetIndex]); |
| 62 | + // api->LogDebug((std::string("Found ") + std::to_string(count) + " gamepad(s), opening index " + std::to_string(targetIndex)).c_str()); |
| 63 | + } else { |
| 64 | + // api->LogDebug((std::string("No gamepads found. Count: ") + std::to_string(count)).c_str()); |
| 65 | + } |
| 66 | + SDL_free(joysticks); |
| 67 | + |
| 68 | + if (activeGamepad) { |
| 69 | + if (SDL_GamepadHasSensor(activeGamepad, SDL_SENSOR_GYRO)) { |
| 70 | + SDL_SetGamepadSensorEnabled(activeGamepad, SDL_SENSOR_GYRO, true); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +void vibrateImpl(int strength, int durationMs) { |
| 76 | + if (activeGamepad) { |
| 77 | + if (strength < 0) |
| 78 | + strength = 0; |
| 79 | + if (strength > 65535) |
| 80 | + strength = 65535; |
| 81 | + uint16_t vibrationStrength = static_cast<uint16_t>(strength); |
| 82 | + SDL_RumbleGamepad(activeGamepad, vibrationStrength, vibrationStrength, durationMs); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +} // namespace |
| 87 | + |
| 88 | +namespace Input::Backends::SDL3 { |
| 89 | + |
| 90 | +bool enabled; |
| 91 | + |
| 92 | +void Backend::PollInput() { |
| 93 | + SDL_UpdateGamepads(); |
| 94 | + RefreshGamepadConnection(); |
| 95 | + |
| 96 | + input = {}; |
| 97 | + |
| 98 | + if (activeGamepad && SDL_GamepadConnected(activeGamepad)) { |
| 99 | + float leftX = SDL_GetGamepadAxis(activeGamepad, SDL_GAMEPAD_AXIS_LEFTX) / 32768.0f; |
| 100 | + float leftY = SDL_GetGamepadAxis(activeGamepad, SDL_GAMEPAD_AXIS_LEFTY) / -32768.0f; |
| 101 | + float rightX = SDL_GetGamepadAxis(activeGamepad, SDL_GAMEPAD_AXIS_RIGHTX) / 32768.0f; |
| 102 | + float rightY = SDL_GetGamepadAxis(activeGamepad, SDL_GAMEPAD_AXIS_RIGHTY) / -32768.0f; |
| 103 | + |
| 104 | + float leftTrigger = SDL_GetGamepadAxis(activeGamepad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) / 32767.0f; |
| 105 | + float rightTrigger = SDL_GetGamepadAxis(activeGamepad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) / 32767.0f; |
| 106 | + |
| 107 | + input.leftStick.x = -applyStickDeadzone(leftX, stickDeadzone, stickOuterDeadzone) * stickScale; |
| 108 | + input.leftStick.y = applyStickDeadzone(leftY, stickDeadzone, stickOuterDeadzone) * stickScale; |
| 109 | + input.leftStick.click = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_LEFT_STICK); |
| 110 | + |
| 111 | + input.rightStick.x = applyStickDeadzone(rightX, stickDeadzone, stickOuterDeadzone) * stickScale; |
| 112 | + input.rightStick.y = -applyStickDeadzone(rightY, stickDeadzone, stickOuterDeadzone) * stickScale; |
| 113 | + input.rightStick.click = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_RIGHT_STICK); |
| 114 | + |
| 115 | + input.leftTrigger = applyTriggerDeadzone(leftTrigger, triggerDeadzone, triggerOuterDeadzone) * triggerScale; |
| 116 | + input.rightTrigger = applyTriggerDeadzone(rightTrigger, triggerDeadzone, triggerOuterDeadzone) * triggerScale; |
| 117 | + |
| 118 | + input.dpad.up = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_DPAD_UP); |
| 119 | + input.dpad.down = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_DPAD_DOWN); |
| 120 | + input.dpad.left = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_DPAD_LEFT); |
| 121 | + input.dpad.right = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT); |
| 122 | + |
| 123 | + input.leftShoulder = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER); |
| 124 | + input.rightShoulder = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER); |
| 125 | + |
| 126 | + input.aButton = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_SOUTH); |
| 127 | + input.bButton = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_EAST); |
| 128 | + input.xButton = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_WEST); |
| 129 | + input.yButton = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_NORTH); |
| 130 | + |
| 131 | + input.startButton = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_START); |
| 132 | + input.backButton = SDL_GetGamepadButton(activeGamepad, SDL_GAMEPAD_BUTTON_BACK); |
| 133 | + } else { |
| 134 | + if (activeGamepad) { |
| 135 | + SDL_CloseGamepad(activeGamepad); |
| 136 | + activeGamepad = nullptr; |
| 137 | + } |
| 138 | + input = {}; |
| 139 | + } |
| 140 | + input.config.enabled = Input::enabled; |
| 141 | + input.config.stickScale = stickScale; |
| 142 | + input.config.triggerScale = triggerScale; |
| 143 | +} |
| 144 | + |
| 145 | +ModernInput Backend::GetState() { |
| 146 | + return input; |
| 147 | +} |
| 148 | + |
| 149 | +void Backend::Setup() { |
| 150 | + input = {}; |
| 151 | + |
| 152 | + enabled = api->SetupIniBool(L"Input", L"Enabled", true); |
| 153 | + deviceIndex = api->SetupIniInt(L"Input", L"DeviceIndex", 0); |
| 154 | + |
| 155 | + api->LogInfo((std::string("SDL platform: ") + SDL_GetPlatform()).c_str()); |
| 156 | + |
| 157 | + stickDeadzone = api->SetupIniInt(L"Input", L"StickDeadzone", 25) / 100.0f; |
| 158 | + stickDeadzone = min(max(stickDeadzone, 0.0f), 1.0f); |
| 159 | + stickOuterDeadzone = api->SetupIniInt(L"Input", L"StickOuterDeadzone", 75) / 100.0f; |
| 160 | + stickOuterDeadzone = min(max(stickOuterDeadzone, 0.0f), 1.0f); |
| 161 | + triggerDeadzone = api->SetupIniInt(L"Input", L"TriggerDeadzone", 10) / 100.0f; |
| 162 | + triggerDeadzone = min(max(triggerDeadzone, 0.0f), 1.0f); |
| 163 | + triggerOuterDeadzone = api->SetupIniInt(L"Input", L"TriggerOuterDeadzone", 90) / 100.0f; |
| 164 | + triggerOuterDeadzone = min(max(triggerOuterDeadzone, 0.0f), 1.0f); |
| 165 | + |
| 166 | + SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5, "1"); |
| 167 | + SDL_SetHint(SDL_HINT_JOYSTICK_ENHANCED_REPORTS, "1"); |
| 168 | + |
| 169 | + SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); |
| 170 | + SDL_SetHint(SDL_HINT_JOYSTICK_THREAD, "1"); |
| 171 | + SDL_InitSubSystem(SDL_INIT_GAMEPAD | SDL_INIT_EVENTS); |
| 172 | +} |
| 173 | + |
| 174 | +void Backend::vibrate(int strength, int durationMs) { |
| 175 | + vibrateImpl(strength, durationMs); |
| 176 | +} |
| 177 | + |
| 178 | +} // namespace Input::Backends::SDL3 |
0 commit comments