Merge pull request #13775 from jordan-woyak/sdl-gamepad-rename

InputCommon: Rename SDL input backend GameController to Gamepad.
This commit is contained in:
Jordan Woyak
2025-07-12 16:50:04 -05:00
committed by GitHub
3 changed files with 76 additions and 77 deletions

View File

@ -251,9 +251,9 @@ void InputBackend::OpenAndAddDevice(SDL_JoystickID instance_id)
// SDL tries parsing these as Joysticks
return;
}
auto gamecontroller = std::make_shared<GameController>(gc, js);
if (!gamecontroller->Inputs().empty() || !gamecontroller->Outputs().empty())
GetControllerInterface().AddDevice(std::move(gamecontroller));
auto gamepad = std::make_shared<Gamepad>(gc, js);
if (!gamepad->Inputs().empty() || !gamepad->Outputs().empty())
GetControllerInterface().AddDevice(std::move(gamepad));
}
}
@ -267,7 +267,7 @@ bool InputBackend::HandleEventAndContinue(const SDL_Event& e)
{
GetControllerInterface().RemoveDevice([&e](const auto* device) {
return device->GetSource() == "SDL" &&
static_cast<const GameController*>(device)->GetSDLInstanceID() == e.jdevice.which;
static_cast<const Gamepad*>(device)->GetSDLInstanceID() == e.jdevice.which;
});
}
else if (e.type == m_populate_event_type)

View File

@ -19,14 +19,14 @@ static bool IsTriggerAxis(int index)
return index >= 4;
}
GameController::GameController(SDL_Gamepad* const gamecontroller, SDL_Joystick* const joystick)
: m_gamecontroller(gamecontroller), m_joystick(joystick)
Gamepad::Gamepad(SDL_Gamepad* const gamepad, SDL_Joystick* const joystick)
: m_gamepad(gamepad), m_joystick(joystick)
{
const char* const sdl_name = (gamecontroller != nullptr) ? SDL_GetGamepadName(gamecontroller) :
SDL_GetJoystickName(joystick);
const char* const sdl_name =
(gamepad != nullptr) ? SDL_GetGamepadName(gamepad) : 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.
// If a Joystick input has a Gamepad equivalent button/hat we don't add it.
// "Equivalent" axes are still added as hidden/undetectable inputs to handle
// loading of existing configs which may use "full surface" inputs.
// Otherwise handling those would require dealing with gamepad specific quirks.
@ -50,11 +50,11 @@ GameController::GameController(SDL_Gamepad* const gamecontroller, SDL_Joystick*
}
};
if (gamecontroller != nullptr)
if (gamepad != nullptr)
{
// Inputs
int binding_count = 0;
auto** bindings = SDL_GetGamepadBindings(gamecontroller, &binding_count);
auto** bindings = SDL_GetGamepadBindings(gamepad, &binding_count);
Common::ScopeGuard free_bindings([&] { SDL_free(bindings); });
for (auto* const binding : std::span(bindings, binding_count))
@ -64,20 +64,20 @@ GameController::GameController(SDL_Gamepad* const gamecontroller, SDL_Joystick*
switch (binding->output_type)
{
case SDL_GAMEPAD_BINDTYPE_BUTTON:
AddInput(new Button(gamecontroller, *binding));
AddInput(new Button(gamepad, *binding));
break;
case SDL_GAMEPAD_BINDTYPE_AXIS:
{
const auto axis = binding->output.axis.axis;
if (IsTriggerAxis(axis))
{
AddInput(new Axis(m_gamecontroller, 32767, axis));
AddInput(new Axis(m_gamepad, 32767, axis));
}
else
{
// Each axis gets a negative and a positive input instance associated with it
AddInput(new Axis(m_gamecontroller, -32768, axis));
AddInput(new Axis(m_gamecontroller, 32767, axis));
AddInput(new Axis(m_gamepad, -32768, axis));
AddInput(new Axis(m_gamepad, 32767, axis));
}
break;
}
@ -86,25 +86,25 @@ GameController::GameController(SDL_Gamepad* const gamecontroller, SDL_Joystick*
}
}
const auto properties = SDL_GetGamepadProperties(m_gamecontroller);
const auto properties = SDL_GetGamepadProperties(m_gamepad);
// Rumble
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));
AddOutput(new Rumble("Motor L", *this, &m_low_freq_rumble, &Gamepad::UpdateRumble));
AddOutput(new Rumble("Motor R", *this, &m_high_freq_rumble, &Gamepad::UpdateRumble));
}
if (SDL_GetBooleanProperty(properties, SDL_PROP_GAMEPAD_CAP_TRIGGER_RUMBLE_BOOLEAN, false))
{
AddOutput(new Rumble("Trigger L", *this, &m_trigger_l_rumble,
&GameController::UpdateRumbleTriggers));
AddOutput(new Rumble("Trigger R", *this, &m_trigger_r_rumble,
&GameController::UpdateRumbleTriggers));
AddOutput(
new Rumble("Trigger L", *this, &m_trigger_l_rumble, &Gamepad::UpdateRumbleTriggers));
AddOutput(
new Rumble("Trigger R", *this, &m_trigger_r_rumble, &Gamepad::UpdateRumbleTriggers));
}
// Touchpad
if (SDL_GetNumGamepadTouchpads(m_gamecontroller) > 0)
if (SDL_GetNumGamepadTouchpads(m_gamepad) > 0)
{
const char* const name_x = "Touchpad X";
AddInput(new NonDetectableDirectionalInput<-1>(name_x, &m_touchpad_x));
@ -118,12 +118,12 @@ GameController::GameController(SDL_Gamepad* const gamecontroller, SDL_Joystick*
// Motion
const auto add_sensor = [this](SDL_SensorType type, std::string_view sensor_name,
const SDLMotionAxisList& axes) {
if (SDL_SetGamepadSensorEnabled(m_gamecontroller, type, true))
if (SDL_SetGamepadSensorEnabled(m_gamepad, type, true))
{
for (const SDLMotionAxis& axis : axes)
{
AddInput(new MotionInput(fmt::format("{} {}", sensor_name, axis.name), m_gamecontroller,
type, axis.index, axis.scale));
AddInput(new MotionInput(fmt::format("{} {}", sensor_name, axis.name), m_gamepad, type,
axis.index, axis.scale));
}
}
};
@ -228,7 +228,7 @@ GameController::GameController(SDL_Gamepad* const gamecontroller, SDL_Joystick*
AddInput(new BatteryInput{&m_battery_value});
}
bool GameController::UpdateBatteryLevel()
bool Gamepad::UpdateBatteryLevel()
{
int battery_percent = 0;
if (SDL_GetJoystickPowerInfo(m_joystick, &battery_percent) == SDL_POWERSTATE_ERROR)
@ -238,7 +238,7 @@ bool GameController::UpdateBatteryLevel()
return true;
}
GameController::~GameController()
Gamepad::~Gamepad()
{
if (m_haptic)
{
@ -248,31 +248,31 @@ GameController::~GameController()
SDL_CloseHaptic(m_haptic);
m_haptic = nullptr;
}
if (m_gamecontroller)
if (m_gamepad)
{
// stop all rumble
SDL_RumbleGamepad(m_gamecontroller, 0, 0, 0);
SDL_CloseGamepad(m_gamecontroller);
SDL_RumbleGamepad(m_gamepad, 0, 0, 0);
SDL_CloseGamepad(m_gamepad);
}
SDL_CloseJoystick(m_joystick);
}
std::string GameController::GetName() const
std::string Gamepad::GetName() const
{
return m_name;
}
std::string GameController::GetSource() const
std::string Gamepad::GetSource() const
{
return "SDL";
}
SDL_JoystickID GameController::GetSDLInstanceID() const
SDL_JoystickID Gamepad::GetSDLInstanceID() const
{
return SDL_GetJoystickID(m_joystick);
}
std::string GameController::Button::GetName() const
std::string Gamepad::Button::GetName() const
{
const auto button = m_binding.output.button;
@ -282,7 +282,7 @@ std::string GameController::Button::GetName() const
return s_sdl_button_names[button];
}
std::string GameController::Axis::GetName() const
std::string Gamepad::Axis::GetName() const
{
if (std::size_t(m_axis) >= std::size(s_sdl_axis_names))
return GetLegacyAxisName(m_axis, m_range);
@ -299,17 +299,17 @@ std::string GameController::Axis::GetName() const
return std::string(s_sdl_axis_names[m_axis]) + (negative ? '-' : '+');
}
ControlState GameController::Button::GetState() const
ControlState Gamepad::Button::GetState() const
{
return SDL_GetGamepadButton(m_gc, m_binding.output.button);
}
ControlState GameController::Axis::GetState() const
ControlState Gamepad::Axis::GetState() const
{
return ControlState(SDL_GetGamepadAxis(m_gc, m_axis)) / m_range;
}
bool GameController::Button::IsMatchingName(std::string_view name) const
bool Gamepad::Button::IsMatchingName(std::string_view name) const
{
if (GetName() == name)
return true;
@ -339,7 +339,7 @@ bool GameController::Button::IsMatchingName(std::string_view name) const
}
}
ControlState GameController::MotionInput::GetState() const
ControlState Gamepad::MotionInput::GetState() const
{
std::array<float, 3> data{};
SDL_GetGamepadSensorData(m_gc, m_type, data.data(), (int)data.size());
@ -347,22 +347,22 @@ ControlState GameController::MotionInput::GetState() const
}
// Legacy input
ControlState GameController::LegacyButton::GetState() const
ControlState Gamepad::LegacyButton::GetState() const
{
return SDL_GetJoystickButton(m_js, m_index);
}
ControlState GameController::LegacyAxis::GetState() const
ControlState Gamepad::LegacyAxis::GetState() const
{
return ControlState(SDL_GetJoystickAxis(m_js, m_index)) / m_range;
}
ControlState GameController::LegacyHat::GetState() const
ControlState Gamepad::LegacyHat::GetState() const
{
return (SDL_GetJoystickHat(m_js, m_index) & (1 << m_direction)) > 0;
}
void GameController::HapticEffect::UpdateEffect()
void Gamepad::HapticEffect::UpdateEffect()
{
if (m_effect.type != DISABLED_EFFECT_TYPE)
{
@ -389,40 +389,40 @@ void GameController::HapticEffect::UpdateEffect()
}
}
GameController::HapticEffect::HapticEffect(SDL_Haptic* haptic) : m_haptic(haptic)
Gamepad::HapticEffect::HapticEffect(SDL_Haptic* haptic) : m_haptic(haptic)
{
// FYI: type is set within UpdateParameters.
m_effect.type = DISABLED_EFFECT_TYPE;
}
GameController::HapticEffect::~HapticEffect()
Gamepad::HapticEffect::~HapticEffect()
{
m_effect.type = DISABLED_EFFECT_TYPE;
UpdateEffect();
}
void GameController::HapticEffect::SetDirection(SDL_HapticDirection* dir)
void Gamepad::HapticEffect::SetDirection(SDL_HapticDirection* dir)
{
// Left direction (for wheels)
dir->type = SDL_HAPTIC_CARTESIAN;
dir->dir[0] = -1;
}
GameController::ConstantEffect::ConstantEffect(SDL_Haptic* haptic) : HapticEffect(haptic)
Gamepad::ConstantEffect::ConstantEffect(SDL_Haptic* haptic) : HapticEffect(haptic)
{
m_effect.constant = {};
SetDirection(&m_effect.constant.direction);
m_effect.constant.length = RUMBLE_LENGTH_MS;
}
GameController::RampEffect::RampEffect(SDL_Haptic* haptic) : HapticEffect(haptic)
Gamepad::RampEffect::RampEffect(SDL_Haptic* haptic) : HapticEffect(haptic)
{
m_effect.ramp = {};
SetDirection(&m_effect.ramp.direction);
m_effect.ramp.length = RUMBLE_LENGTH_MS;
}
GameController::PeriodicEffect::PeriodicEffect(SDL_Haptic* haptic, u16 waveform)
Gamepad::PeriodicEffect::PeriodicEffect(SDL_Haptic* haptic, u16 waveform)
: HapticEffect(haptic), m_waveform(waveform)
{
m_effect.periodic = {};
@ -433,24 +433,24 @@ GameController::PeriodicEffect::PeriodicEffect(SDL_Haptic* haptic, u16 waveform)
m_effect.periodic.phase = 0;
}
GameController::LeftRightEffect::LeftRightEffect(SDL_Haptic* haptic, Motor motor)
Gamepad::LeftRightEffect::LeftRightEffect(SDL_Haptic* haptic, Motor motor)
: HapticEffect(haptic), m_motor(motor)
{
m_effect.leftright = {};
m_effect.leftright.length = RUMBLE_LENGTH_MS;
}
std::string GameController::ConstantEffect::GetName() const
std::string Gamepad::ConstantEffect::GetName() const
{
return "Constant";
}
std::string GameController::RampEffect::GetName() const
std::string Gamepad::RampEffect::GetName() const
{
return "Ramp";
}
std::string GameController::PeriodicEffect::GetName() const
std::string Gamepad::PeriodicEffect::GetName() const
{
switch (m_waveform)
{
@ -467,12 +467,12 @@ std::string GameController::PeriodicEffect::GetName() const
}
}
std::string GameController::LeftRightEffect::GetName() const
std::string Gamepad::LeftRightEffect::GetName() const
{
return (Motor::Strong == m_motor) ? "Strong" : "Weak";
}
void GameController::HapticEffect::SetState(ControlState state)
void Gamepad::HapticEffect::SetState(ControlState state)
{
// Maximum force value for all SDL effects:
constexpr s16 MAX_FORCE_VALUE = 0x7fff;
@ -483,7 +483,7 @@ void GameController::HapticEffect::SetState(ControlState state)
}
}
bool GameController::ConstantEffect::UpdateParameters(s16 value)
bool Gamepad::ConstantEffect::UpdateParameters(s16 value)
{
s16& level = m_effect.constant.level;
const s16 old_level = level;
@ -494,7 +494,7 @@ bool GameController::ConstantEffect::UpdateParameters(s16 value)
return level != old_level;
}
bool GameController::RampEffect::UpdateParameters(s16 value)
bool Gamepad::RampEffect::UpdateParameters(s16 value)
{
s16& level = m_effect.ramp.start;
const s16 old_level = level;
@ -508,7 +508,7 @@ bool GameController::RampEffect::UpdateParameters(s16 value)
return level != old_level;
}
bool GameController::PeriodicEffect::UpdateParameters(s16 value)
bool Gamepad::PeriodicEffect::UpdateParameters(s16 value)
{
s16& level = m_effect.periodic.magnitude;
const s16 old_level = level;
@ -519,7 +519,7 @@ bool GameController::PeriodicEffect::UpdateParameters(s16 value)
return level != old_level;
}
bool GameController::LeftRightEffect::UpdateParameters(s16 value)
bool Gamepad::LeftRightEffect::UpdateParameters(s16 value)
{
u16& level = (Motor::Strong == m_motor) ? m_effect.leftright.large_magnitude :
m_effect.leftright.small_magnitude;

View File

@ -42,10 +42,10 @@ static_assert(GetDirectionFromHatMask(SDL_HAT_LEFT) == 3);
namespace ciface::SDL
{
class GameController : public Core::Device
class Gamepad : public Core::Device
{
private:
// GameController inputs
// Gamepad inputs
class Button : public Core::Device::Input
{
public:
@ -139,9 +139,9 @@ private:
class Rumble : public Output
{
public:
using UpdateCallback = void (GameController::*)(void);
using UpdateCallback = void (Gamepad::*)(void);
Rumble(const char* name, GameController& gc, Uint16* state, UpdateCallback update_callback)
Rumble(const char* name, Gamepad& gc, Uint16* state, UpdateCallback update_callback)
: m_name{name}, m_gc{gc}, m_state{*state}, m_update_callback{update_callback}
{
}
@ -158,7 +158,7 @@ private:
private:
const char* const m_name;
GameController& m_gc;
Gamepad& m_gc;
Uint16& m_state;
UpdateCallback const m_update_callback;
};
@ -166,7 +166,7 @@ private:
class CombinedMotor : public Output
{
public:
CombinedMotor(GameController& gc, Uint16* low_state, Uint16* high_state)
CombinedMotor(Gamepad& gc, Uint16* low_state, Uint16* high_state)
: m_gc{gc}, m_low_state{*low_state}, m_high_state{*high_state}
{
}
@ -183,7 +183,7 @@ private:
}
private:
GameController& m_gc;
Gamepad& m_gc;
Uint16& m_low_state;
Uint16& m_high_state;
};
@ -316,8 +316,8 @@ private:
};
public:
GameController(SDL_Gamepad* gamecontroller, SDL_Joystick* joystick);
~GameController() override;
Gamepad(SDL_Gamepad* gamepad, SDL_Joystick* joystick);
~Gamepad() override;
std::string GetName() const override;
std::string GetSource() const override;
@ -330,11 +330,11 @@ public:
const int touchpad_index = 0;
const int finger_index = 0;
if (SDL_GetNumGamepadTouchpads(m_gamecontroller) > touchpad_index &&
SDL_GetNumGamepadTouchpadFingers(m_gamecontroller, touchpad_index) > finger_index)
if (SDL_GetNumGamepadTouchpads(m_gamepad) > touchpad_index &&
SDL_GetNumGamepadTouchpadFingers(m_gamepad, touchpad_index) > finger_index)
{
SDL_GetGamepadTouchpadFinger(m_gamecontroller, touchpad_index, finger_index, nullptr,
&m_touchpad_x, &m_touchpad_y, &m_touchpad_pressure);
SDL_GetGamepadTouchpadFinger(m_gamepad, touchpad_index, finger_index, nullptr, &m_touchpad_x,
&m_touchpad_y, &m_touchpad_pressure);
m_touchpad_x = m_touchpad_x * 2 - 1;
m_touchpad_y = m_touchpad_y * 2 - 1;
}
@ -345,13 +345,12 @@ public:
private:
void UpdateRumble()
{
SDL_RumbleGamepad(m_gamecontroller, m_low_freq_rumble, m_high_freq_rumble, RUMBLE_LENGTH_MS);
SDL_RumbleGamepad(m_gamepad, m_low_freq_rumble, m_high_freq_rumble, RUMBLE_LENGTH_MS);
}
void UpdateRumbleTriggers()
{
SDL_RumbleGamepadTriggers(m_gamecontroller, m_trigger_l_rumble, m_trigger_r_rumble,
RUMBLE_LENGTH_MS);
SDL_RumbleGamepadTriggers(m_gamepad, m_trigger_l_rumble, m_trigger_r_rumble, RUMBLE_LENGTH_MS);
}
bool UpdateBatteryLevel();
@ -362,7 +361,7 @@ private:
Uint16 m_trigger_l_rumble = 0;
Uint16 m_trigger_r_rumble = 0;
SDL_Gamepad* const m_gamecontroller;
SDL_Gamepad* const m_gamepad;
std::string m_name;
SDL_Joystick* const m_joystick;
SDL_Haptic* m_haptic = nullptr;