mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-06-28 01:49:33 -06:00
InputCommon: Update to use SDL3 and bump the SDL submodule in Externals to release-3.2.16.
This commit is contained in:
@ -4,9 +4,11 @@
|
||||
#include "InputCommon/ControllerInterface/SDL/SDLGamepad.h"
|
||||
|
||||
#include <array>
|
||||
#include <span>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/ScopeGuard.h"
|
||||
|
||||
namespace ciface::SDL
|
||||
{
|
||||
@ -17,16 +19,12 @@ bool IsTriggerAxis(int index)
|
||||
return index >= 4;
|
||||
}
|
||||
|
||||
GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
SDL_Joystick* const joystick)
|
||||
GameController::GameController(SDL_Gamepad* const gamecontroller, SDL_Joystick* const joystick)
|
||||
: m_gamecontroller(gamecontroller), m_joystick(joystick)
|
||||
{
|
||||
const char* name;
|
||||
if (gamecontroller)
|
||||
name = SDL_GameControllerName(gamecontroller);
|
||||
else
|
||||
name = SDL_JoystickName(joystick);
|
||||
m_name = name != nullptr ? name : "Unknown";
|
||||
const char* const sdl_name = (gamecontroller != nullptr) ? SDL_GetGamepadName(gamecontroller) :
|
||||
SDL_GetJoystickName(joystick);
|
||||
m_name = (sdl_name != nullptr) ? sdl_name : "Unknown";
|
||||
|
||||
// If a Joystick input has a GameController equivalent button/hat we don't add it.
|
||||
// "Equivalent" axes are still added as hidden/undetectable inputs to handle
|
||||
@ -35,17 +33,17 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
std::unordered_set<int> registered_buttons;
|
||||
std::unordered_set<int> registered_hats;
|
||||
std::unordered_set<int> registered_axes;
|
||||
const auto register_mapping = [&](const SDL_GameControllerButtonBind& bind) {
|
||||
switch (bind.bindType)
|
||||
const auto register_mapping = [&](const SDL_GamepadBinding& bind) {
|
||||
switch (bind.input_type)
|
||||
{
|
||||
case SDL_CONTROLLER_BINDTYPE_BUTTON:
|
||||
registered_buttons.insert(bind.value.button);
|
||||
case SDL_GAMEPAD_BINDTYPE_BUTTON:
|
||||
registered_buttons.insert(bind.input.button);
|
||||
break;
|
||||
case SDL_CONTROLLER_BINDTYPE_HAT:
|
||||
registered_hats.insert(bind.value.hat.hat);
|
||||
case SDL_GAMEPAD_BINDTYPE_HAT:
|
||||
registered_hats.insert(bind.input.hat.hat);
|
||||
break;
|
||||
case SDL_CONTROLLER_BINDTYPE_AXIS:
|
||||
registered_axes.insert(bind.value.axis);
|
||||
case SDL_GAMEPAD_BINDTYPE_AXIS:
|
||||
registered_axes.insert(bind.input.axis.axis);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -55,25 +53,22 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
if (gamecontroller != nullptr)
|
||||
{
|
||||
// Inputs
|
||||
int binding_count = 0;
|
||||
auto** bindings = SDL_GetGamepadBindings(gamecontroller, &binding_count);
|
||||
Common::ScopeGuard free_bindings([&] { SDL_free(bindings); });
|
||||
|
||||
// Buttons
|
||||
for (u8 i = 0; i != size(s_sdl_button_names); ++i)
|
||||
for (auto* const binding : std::span(bindings, binding_count))
|
||||
{
|
||||
SDL_GameControllerButton button = static_cast<SDL_GameControllerButton>(i);
|
||||
if (SDL_GameControllerHasButton(m_gamecontroller, button))
|
||||
{
|
||||
AddInput(new Button(gamecontroller, button));
|
||||
register_mapping(*binding);
|
||||
|
||||
register_mapping(SDL_GameControllerGetBindForButton(gamecontroller, button));
|
||||
}
|
||||
}
|
||||
|
||||
// Axes
|
||||
for (u8 i = 0; i != size(s_sdl_axis_names); ++i)
|
||||
{
|
||||
SDL_GameControllerAxis axis = static_cast<SDL_GameControllerAxis>(i);
|
||||
if (SDL_GameControllerHasAxis(m_gamecontroller, axis))
|
||||
switch (binding->output_type)
|
||||
{
|
||||
case SDL_GAMEPAD_BINDTYPE_BUTTON:
|
||||
AddInput(new Button(gamecontroller, *binding));
|
||||
break;
|
||||
case SDL_GAMEPAD_BINDTYPE_AXIS:
|
||||
{
|
||||
const auto axis = binding->output.axis.axis;
|
||||
if (IsTriggerAxis(axis))
|
||||
{
|
||||
AddInput(new Axis(m_gamecontroller, 32767, axis));
|
||||
@ -84,19 +79,23 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
AddInput(new Axis(m_gamecontroller, -32768, axis));
|
||||
AddInput(new Axis(m_gamecontroller, 32767, axis));
|
||||
}
|
||||
|
||||
register_mapping(SDL_GameControllerGetBindForAxis(gamecontroller, axis));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const auto properties = SDL_GetGamepadProperties(m_gamecontroller);
|
||||
|
||||
// Rumble
|
||||
if (SDL_GameControllerHasRumble(m_gamecontroller))
|
||||
if (SDL_GetBooleanProperty(properties, SDL_PROP_GAMEPAD_CAP_RUMBLE_BOOLEAN, false))
|
||||
{
|
||||
AddOutput(new CombinedMotor(*this, &m_low_freq_rumble, &m_high_freq_rumble));
|
||||
AddOutput(new Rumble("Motor L", *this, &m_low_freq_rumble, &GameController::UpdateRumble));
|
||||
AddOutput(new Rumble("Motor R", *this, &m_high_freq_rumble, &GameController::UpdateRumble));
|
||||
}
|
||||
if (SDL_GameControllerHasRumbleTriggers(m_gamecontroller))
|
||||
if (SDL_GetBooleanProperty(properties, SDL_PROP_GAMEPAD_CAP_TRIGGER_RUMBLE_BOOLEAN, false))
|
||||
{
|
||||
AddOutput(new Rumble("Trigger L", *this, &m_trigger_l_rumble,
|
||||
&GameController::UpdateRumbleTriggers));
|
||||
@ -105,7 +104,7 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
}
|
||||
|
||||
// Touchpad
|
||||
if (SDL_GameControllerGetNumTouchpads(m_gamecontroller) > 0)
|
||||
if (SDL_GetNumGamepadTouchpads(m_gamecontroller) > 0)
|
||||
{
|
||||
const char* const name_x = "Touchpad X";
|
||||
AddInput(new NonDetectableDirectionalInput<-1>(name_x, &m_touchpad_x));
|
||||
@ -119,7 +118,7 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
// Motion
|
||||
const auto add_sensor = [this](SDL_SensorType type, std::string_view sensor_name,
|
||||
const SDLMotionAxisList& axes) {
|
||||
if (SDL_GameControllerSetSensorEnabled(m_gamecontroller, type, SDL_TRUE) == 0)
|
||||
if (SDL_SetGamepadSensorEnabled(m_gamecontroller, type, true))
|
||||
{
|
||||
for (const SDLMotionAxis& axis : axes)
|
||||
{
|
||||
@ -140,7 +139,7 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
// Legacy inputs
|
||||
|
||||
// Buttons
|
||||
int n_legacy_buttons = SDL_JoystickNumButtons(joystick);
|
||||
int n_legacy_buttons = SDL_GetNumJoystickButtons(joystick);
|
||||
if (n_legacy_buttons < 0)
|
||||
{
|
||||
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Error in SDL_JoystickNumButtons(): {}", SDL_GetError());
|
||||
@ -155,7 +154,7 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
}
|
||||
|
||||
// Axes
|
||||
int n_legacy_axes = SDL_JoystickNumAxes(joystick);
|
||||
int n_legacy_axes = SDL_GetNumJoystickAxes(joystick);
|
||||
if (n_legacy_axes < 0)
|
||||
{
|
||||
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Error in SDL_JoystickNumAxes(): {}", SDL_GetError());
|
||||
@ -171,7 +170,7 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
}
|
||||
|
||||
// Hats
|
||||
int n_legacy_hats = SDL_JoystickNumHats(joystick);
|
||||
int n_legacy_hats = SDL_GetNumJoystickHats(joystick);
|
||||
if (n_legacy_hats < 0)
|
||||
{
|
||||
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Error in SDL_JoystickNumHats(): {}", SDL_GetError());
|
||||
@ -188,16 +187,16 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
}
|
||||
|
||||
// Haptics
|
||||
if (SDL_JoystickIsHaptic(m_joystick))
|
||||
if (SDL_IsJoystickHaptic(m_joystick))
|
||||
{
|
||||
m_haptic = SDL_HapticOpenFromJoystick(m_joystick);
|
||||
m_haptic = SDL_OpenHapticFromJoystick(m_joystick);
|
||||
if (m_haptic)
|
||||
{
|
||||
const unsigned int supported_effects = SDL_HapticQuery(m_haptic);
|
||||
const unsigned int supported_effects = SDL_GetMaxHapticEffects(m_haptic);
|
||||
|
||||
// Disable autocenter:
|
||||
if (supported_effects & SDL_HAPTIC_AUTOCENTER)
|
||||
SDL_HapticSetAutocenter(m_haptic, 0);
|
||||
SDL_SetHapticAutocenter(m_haptic, 0);
|
||||
|
||||
// Constant
|
||||
if (supported_effects & SDL_HAPTIC_CONSTANT)
|
||||
@ -224,16 +223,19 @@ GameController::GameController(SDL_GameController* const gamecontroller,
|
||||
}
|
||||
}
|
||||
|
||||
// Needed to make the below power level not "UNKNOWN".
|
||||
SDL_JoystickUpdate();
|
||||
|
||||
// Battery
|
||||
if (SDL_JoystickPowerLevel const power_level = SDL_JoystickCurrentPowerLevel(m_joystick);
|
||||
power_level != SDL_JOYSTICK_POWER_UNKNOWN)
|
||||
{
|
||||
m_battery_value = GetBatteryValueFromSDLPowerLevel(power_level);
|
||||
if (UpdateBatteryLevel())
|
||||
AddInput(new BatteryInput{&m_battery_value});
|
||||
}
|
||||
}
|
||||
|
||||
bool GameController::UpdateBatteryLevel()
|
||||
{
|
||||
int battery_percent = 0;
|
||||
if (SDL_GetJoystickPowerInfo(m_joystick, &battery_percent) == SDL_POWERSTATE_ERROR)
|
||||
return false;
|
||||
|
||||
m_battery_value = std::max(0, battery_percent);
|
||||
return true;
|
||||
}
|
||||
|
||||
GameController::~GameController()
|
||||
@ -241,20 +243,18 @@ GameController::~GameController()
|
||||
if (m_haptic)
|
||||
{
|
||||
// stop/destroy all effects
|
||||
SDL_HapticStopAll(m_haptic);
|
||||
SDL_StopHapticEffects(m_haptic);
|
||||
// close haptic before joystick
|
||||
SDL_HapticClose(m_haptic);
|
||||
SDL_CloseHaptic(m_haptic);
|
||||
m_haptic = nullptr;
|
||||
}
|
||||
if (m_gamecontroller)
|
||||
{
|
||||
// stop all rumble
|
||||
SDL_GameControllerRumble(m_gamecontroller, 0, 0, 0);
|
||||
// close game controller
|
||||
SDL_GameControllerClose(m_gamecontroller);
|
||||
SDL_RumbleGamepad(m_gamecontroller, 0, 0, 0);
|
||||
SDL_CloseGamepad(m_gamecontroller);
|
||||
}
|
||||
// close joystick
|
||||
SDL_JoystickClose(m_joystick);
|
||||
SDL_CloseJoystick(m_joystick);
|
||||
}
|
||||
|
||||
std::string GameController::GetName() const
|
||||
@ -267,20 +267,28 @@ std::string GameController::GetSource() const
|
||||
return "SDL";
|
||||
}
|
||||
|
||||
int GameController::GetSDLInstanceID() const
|
||||
SDL_JoystickID GameController::GetSDLInstanceID() const
|
||||
{
|
||||
return SDL_JoystickInstanceID(m_joystick);
|
||||
return SDL_GetJoystickID(m_joystick);
|
||||
}
|
||||
|
||||
std::string GameController::Button::GetName() const
|
||||
{
|
||||
return s_sdl_button_names[m_button];
|
||||
const auto button = m_binding.output.button;
|
||||
|
||||
if (std::size_t(button) >= std::size(s_sdl_button_names))
|
||||
return GetLegacyButtonName(button);
|
||||
|
||||
return s_sdl_button_names[button];
|
||||
}
|
||||
|
||||
std::string GameController::Axis::GetName() const
|
||||
{
|
||||
if (std::size_t(m_axis) >= std::size(s_sdl_axis_names))
|
||||
return GetLegacyAxisName(m_axis, m_range);
|
||||
|
||||
if (IsTriggerAxis(m_axis))
|
||||
return std::string(s_sdl_axis_names[m_axis]);
|
||||
return s_sdl_axis_names[m_axis];
|
||||
|
||||
bool negative = m_range < 0;
|
||||
|
||||
@ -293,12 +301,12 @@ std::string GameController::Axis::GetName() const
|
||||
|
||||
ControlState GameController::Button::GetState() const
|
||||
{
|
||||
return SDL_GameControllerGetButton(m_gc, m_button);
|
||||
return SDL_GetGamepadButton(m_gc, m_binding.output.button);
|
||||
}
|
||||
|
||||
ControlState GameController::Axis::GetState() const
|
||||
{
|
||||
return ControlState(SDL_GameControllerGetAxis(m_gc, m_axis)) / m_range;
|
||||
return ControlState(SDL_GetGamepadAxis(m_gc, m_axis)) / m_range;
|
||||
}
|
||||
|
||||
bool GameController::Button::IsMatchingName(std::string_view name) const
|
||||
@ -306,25 +314,26 @@ bool GameController::Button::IsMatchingName(std::string_view name) const
|
||||
if (GetName() == name)
|
||||
return true;
|
||||
|
||||
// So that SDL can be a superset of XInput
|
||||
if (name == "Button A")
|
||||
return GetName() == "Button S";
|
||||
if (name == "Button B")
|
||||
return GetName() == "Button E";
|
||||
if (name == "Button X")
|
||||
return GetName() == "Button W";
|
||||
if (name == "Button Y")
|
||||
return GetName() == "Button N";
|
||||
// Positionally match XInput button names.
|
||||
// e.g. Switch Pro controller A-button matches "Button B".
|
||||
// e.g. PlayStation controller Circle-button matches "Button B".
|
||||
if (m_binding.output.button == SDL_GAMEPAD_BUTTON_SOUTH && name == "Button A")
|
||||
return true;
|
||||
if (m_binding.output.button == SDL_GAMEPAD_BUTTON_EAST && name == "Button B")
|
||||
return true;
|
||||
if (m_binding.output.button == SDL_GAMEPAD_BUTTON_WEST && name == "Button X")
|
||||
return true;
|
||||
if (m_binding.output.button == SDL_GAMEPAD_BUTTON_NORTH && name == "Button Y")
|
||||
return true;
|
||||
|
||||
// Match legacy names.
|
||||
const auto bind = SDL_GameControllerGetBindForButton(m_gc, m_button);
|
||||
switch (bind.bindType)
|
||||
// Match the old "Button 0"-like names.
|
||||
switch (m_binding.input_type)
|
||||
{
|
||||
case SDL_CONTROLLER_BINDTYPE_BUTTON:
|
||||
return name == GetLegacyButtonName(bind.value.button);
|
||||
case SDL_CONTROLLER_BINDTYPE_HAT:
|
||||
return name == GetLegacyHatName(bind.value.hat.hat,
|
||||
GetDirectionFromHatMask(u8(bind.value.hat.hat_mask)));
|
||||
case SDL_GAMEPAD_BINDTYPE_BUTTON:
|
||||
return name == GetLegacyButtonName(m_binding.input.button);
|
||||
case SDL_GAMEPAD_BINDTYPE_HAT:
|
||||
return name == GetLegacyHatName(m_binding.input.hat.hat,
|
||||
GetDirectionFromHatMask(m_binding.input.hat.hat_mask));
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@ -333,24 +342,24 @@ bool GameController::Button::IsMatchingName(std::string_view name) const
|
||||
ControlState GameController::MotionInput::GetState() const
|
||||
{
|
||||
std::array<float, 3> data{};
|
||||
SDL_GameControllerGetSensorData(m_gc, m_type, data.data(), (int)data.size());
|
||||
SDL_GetGamepadSensorData(m_gc, m_type, data.data(), (int)data.size());
|
||||
return m_scale * data[m_index];
|
||||
}
|
||||
|
||||
// Legacy input
|
||||
ControlState GameController::LegacyButton::GetState() const
|
||||
{
|
||||
return SDL_JoystickGetButton(m_js, m_index);
|
||||
return SDL_GetJoystickButton(m_js, m_index);
|
||||
}
|
||||
|
||||
ControlState GameController::LegacyAxis::GetState() const
|
||||
{
|
||||
return ControlState(SDL_JoystickGetAxis(m_js, m_index)) / m_range;
|
||||
return ControlState(SDL_GetJoystickAxis(m_js, m_index)) / m_range;
|
||||
}
|
||||
|
||||
ControlState GameController::LegacyHat::GetState() const
|
||||
{
|
||||
return (SDL_JoystickGetHat(m_js, m_index) & (1 << m_direction)) > 0;
|
||||
return (SDL_GetJoystickHat(m_js, m_index) & (1 << m_direction)) > 0;
|
||||
}
|
||||
|
||||
void GameController::HapticEffect::UpdateEffect()
|
||||
@ -360,22 +369,22 @@ void GameController::HapticEffect::UpdateEffect()
|
||||
if (m_id < 0)
|
||||
{
|
||||
// Upload and try to play the effect.
|
||||
m_id = SDL_HapticNewEffect(m_haptic, &m_effect);
|
||||
m_id = SDL_CreateHapticEffect(m_haptic, &m_effect);
|
||||
|
||||
if (m_id >= 0)
|
||||
SDL_HapticRunEffect(m_haptic, m_id, 1);
|
||||
SDL_RunHapticEffect(m_haptic, m_id, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Effect is already playing. Update parameters.
|
||||
SDL_HapticUpdateEffect(m_haptic, m_id, &m_effect);
|
||||
SDL_UpdateHapticEffect(m_haptic, m_id, &m_effect);
|
||||
}
|
||||
}
|
||||
else if (m_id >= 0)
|
||||
{
|
||||
// Stop and remove the effect.
|
||||
SDL_HapticStopEffect(m_haptic, m_id);
|
||||
SDL_HapticDestroyEffect(m_haptic, m_id);
|
||||
SDL_StopHapticEffect(m_haptic, m_id);
|
||||
SDL_DestroyHapticEffect(m_haptic, m_id);
|
||||
m_id = -1;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user