InputCommon: Rename SDL input backend GameController to Gamepad.

This commit is contained in:
Jordan Woyak
2025-06-25 00:16:27 -05:00
parent 43aa7e9b96
commit 609d91156a
3 changed files with 76 additions and 77 deletions

View File

@ -19,14 +19,14 @@ 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;