Merge pull request #12503 from AdmiralCurtiss/sdl-fixes

InputCommon/SDL: Fix potential issues in controller initialization.
This commit is contained in:
Admiral H. Curtiss 2024-01-18 00:29:25 +01:00 committed by GitHub
commit 12318f921f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 35 deletions

View File

@ -338,29 +338,50 @@ GameController::GameController(SDL_GameController* const gamecontroller,
name = SDL_GameControllerName(gamecontroller); name = SDL_GameControllerName(gamecontroller);
else else
name = SDL_JoystickName(joystick); name = SDL_JoystickName(joystick);
m_name = name != NULL ? name : "Unknown"; m_name = name != nullptr ? name : "Unknown";
// If a Joystick Button has a GameController equivalent, don't detect it // If a Joystick Button has a GameController equivalent, don't detect it
int n_legacy_buttons = SDL_JoystickNumButtons(joystick); int n_legacy_buttons = SDL_JoystickNumButtons(joystick);
int n_legacy_axes = SDL_JoystickNumAxes(joystick); if (n_legacy_buttons < 0)
int n_legacy_hats = SDL_JoystickNumHats(joystick); {
std::vector<bool> is_button_mapped(n_legacy_buttons); ERROR_LOG_FMT(CONTROLLERINTERFACE, "Error in SDL_JoystickNumButtons(): {}", SDL_GetError());
std::vector<bool> is_axis_mapped(n_legacy_axes); n_legacy_buttons = 0;
std::vector<bool> is_hat_mapped(n_legacy_hats); }
auto RegisterMapping = [&](SDL_GameControllerButtonBind bind) { int n_legacy_axes = SDL_JoystickNumAxes(joystick);
if (n_legacy_axes < 0)
{
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Error in SDL_JoystickNumAxes(): {}", SDL_GetError());
n_legacy_axes = 0;
}
int n_legacy_hats = SDL_JoystickNumHats(joystick);
if (n_legacy_hats < 0)
{
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Error in SDL_JoystickNumHats(): {}", SDL_GetError());
n_legacy_hats = 0;
}
std::vector<bool> is_button_mapped(static_cast<size_t>(n_legacy_buttons), false);
std::vector<bool> is_axis_mapped(static_cast<size_t>(n_legacy_axes), false);
std::vector<bool> is_hat_mapped(static_cast<size_t>(n_legacy_hats), false);
const auto register_mapping = [&](const SDL_GameControllerButtonBind& bind) {
switch (bind.bindType) switch (bind.bindType)
{ {
case SDL_CONTROLLER_BINDTYPE_NONE: case SDL_CONTROLLER_BINDTYPE_NONE:
return; return;
case SDL_CONTROLLER_BINDTYPE_BUTTON: case SDL_CONTROLLER_BINDTYPE_BUTTON:
is_button_mapped.assign(bind.value.button, true); if (bind.value.button >= 0 && bind.value.button < n_legacy_buttons)
is_button_mapped[bind.value.button] = true;
break; break;
case SDL_CONTROLLER_BINDTYPE_AXIS: case SDL_CONTROLLER_BINDTYPE_AXIS:
is_axis_mapped.assign(bind.value.axis, true); if (bind.value.axis >= 0 && bind.value.axis < n_legacy_axes)
is_axis_mapped[bind.value.axis] = true;
break; break;
case SDL_CONTROLLER_BINDTYPE_HAT: case SDL_CONTROLLER_BINDTYPE_HAT:
is_hat_mapped.assign(bind.value.hat.hat, true); if (bind.value.hat.hat >= 0 && bind.value.hat.hat < n_legacy_hats)
is_hat_mapped[bind.value.hat.hat] = true;
break; break;
} }
}; };
@ -376,7 +397,7 @@ GameController::GameController(SDL_GameController* const gamecontroller,
if (SDL_GameControllerHasButton(m_gamecontroller, button)) if (SDL_GameControllerHasButton(m_gamecontroller, button))
{ {
AddInput(new Button(gamecontroller, button)); AddInput(new Button(gamecontroller, button));
RegisterMapping(SDL_GameControllerGetBindForButton(gamecontroller, button)); register_mapping(SDL_GameControllerGetBindForButton(gamecontroller, button));
} }
} }
@ -397,7 +418,7 @@ GameController::GameController(SDL_GameController* const gamecontroller,
{ {
AddInput(new Axis(m_gamecontroller, 32767, axis)); AddInput(new Axis(m_gamecontroller, 32767, axis));
} }
RegisterMapping(SDL_GameControllerGetBindForAxis(gamecontroller, axis)); register_mapping(SDL_GameControllerGetBindForAxis(gamecontroller, axis));
} }
} }
// Rumble // Rumble
@ -409,8 +430,8 @@ GameController::GameController(SDL_GameController* const gamecontroller,
} }
// Motion // Motion
auto AddSensor = [this](SDL_SensorType type, std::string_view name, const auto add_sensor = [this](SDL_SensorType type, std::string_view name,
const SDLMotionAxisList& axes) { const SDLMotionAxisList& axes) {
if (SDL_GameControllerSetSensorEnabled(m_gamecontroller, type, SDL_TRUE) == 0) if (SDL_GameControllerSetSensorEnabled(m_gamecontroller, type, SDL_TRUE) == 0)
{ {
for (const SDLMotionAxis& axis : axes) for (const SDLMotionAxis& axis : axes)
@ -421,22 +442,22 @@ GameController::GameController(SDL_GameController* const gamecontroller,
} }
}; };
AddSensor(SDL_SENSOR_ACCEL, "Accel", SDL_AXES_ACCELEROMETER); add_sensor(SDL_SENSOR_ACCEL, "Accel", SDL_AXES_ACCELEROMETER);
AddSensor(SDL_SENSOR_GYRO, "Gyro", SDL_AXES_GYRO); add_sensor(SDL_SENSOR_GYRO, "Gyro", SDL_AXES_GYRO);
AddSensor(SDL_SENSOR_ACCEL_L, "Accel L", SDL_AXES_ACCELEROMETER); add_sensor(SDL_SENSOR_ACCEL_L, "Accel L", SDL_AXES_ACCELEROMETER);
AddSensor(SDL_SENSOR_GYRO_L, "Gyro L", SDL_AXES_GYRO); add_sensor(SDL_SENSOR_GYRO_L, "Gyro L", SDL_AXES_GYRO);
AddSensor(SDL_SENSOR_ACCEL_R, "Accel R", SDL_AXES_ACCELEROMETER); add_sensor(SDL_SENSOR_ACCEL_R, "Accel R", SDL_AXES_ACCELEROMETER);
AddSensor(SDL_SENSOR_GYRO_R, "Gyro R", SDL_AXES_GYRO); add_sensor(SDL_SENSOR_GYRO_R, "Gyro R", SDL_AXES_GYRO);
} }
// Legacy inputs // Legacy inputs
// Buttons // Buttons
for (u8 i = 0; i != n_legacy_buttons; ++i) for (int i = 0; i != n_legacy_buttons; ++i)
AddInput(new LegacyButton(m_joystick, i, !is_button_mapped[i])); AddInput(new LegacyButton(m_joystick, i, !is_button_mapped[i]));
// Axes // Axes
for (u8 i = 0; i != n_legacy_axes; ++i) for (int i = 0; i != n_legacy_axes; ++i)
{ {
// each axis gets a negative and a positive input instance associated with it // each axis gets a negative and a positive input instance associated with it
AddAnalogInputs(new LegacyAxis(m_joystick, i, -32768, !is_axis_mapped[i]), AddAnalogInputs(new LegacyAxis(m_joystick, i, -32768, !is_axis_mapped[i]),
@ -444,7 +465,7 @@ GameController::GameController(SDL_GameController* const gamecontroller,
} }
// Hats // Hats
for (u8 i = 0; i != n_legacy_hats; ++i) for (int i = 0; i != n_legacy_hats; ++i)
{ {
// each hat gets 4 input instances associated with it, (up down left right) // each hat gets 4 input instances associated with it, (up down left right)
for (u8 d = 0; d != 4; ++d) for (u8 d = 0; d != 4; ++d)

View File

@ -50,7 +50,7 @@ private:
{ {
public: public:
std::string GetName() const override; std::string GetName() const override;
LegacyButton(SDL_Joystick* js, u8 index, bool is_detectable) LegacyButton(SDL_Joystick* js, int index, bool is_detectable)
: m_js(js), m_index(index), m_is_detectable(is_detectable) : m_js(js), m_index(index), m_is_detectable(is_detectable)
{ {
} }
@ -67,8 +67,8 @@ private:
{ {
public: public:
std::string GetName() const override; std::string GetName() const override;
LegacyAxis(SDL_Joystick* js, u8 index, Sint16 range, bool is_detectable) LegacyAxis(SDL_Joystick* js, int index, s16 range, bool is_detectable)
: m_js(js), m_range(range), m_index(index), m_is_detectable(is_detectable) : m_js(js), m_index(index), m_range(range), m_is_detectable(is_detectable)
{ {
} }
bool IsDetectable() const override { return m_is_detectable; } bool IsDetectable() const override { return m_is_detectable; }
@ -76,8 +76,8 @@ private:
private: private:
SDL_Joystick* const m_js; SDL_Joystick* const m_js;
const Sint16 m_range; const int m_index;
const u8 m_index; const s16 m_range;
const bool m_is_detectable; const bool m_is_detectable;
}; };
@ -85,8 +85,8 @@ private:
{ {
public: public:
std::string GetName() const override; std::string GetName() const override;
LegacyHat(SDL_Joystick* js, u8 index, u8 direction, bool is_detectable) LegacyHat(SDL_Joystick* js, int index, u8 direction, bool is_detectable)
: m_js(js), m_direction(direction), m_index(index), m_is_detectable(is_detectable) : m_js(js), m_index(index), m_direction(direction), m_is_detectable(is_detectable)
{ {
} }
bool IsDetectable() const override { return m_is_detectable; } bool IsDetectable() const override { return m_is_detectable; }
@ -94,8 +94,8 @@ private:
private: private:
SDL_Joystick* const m_js; SDL_Joystick* const m_js;
const int m_index;
const u8 m_direction; const u8 m_direction;
const u8 m_index;
const bool m_is_detectable; const bool m_is_detectable;
}; };
@ -103,7 +103,7 @@ private:
class Motor : public Output class Motor : public Output
{ {
public: public:
explicit Motor(SDL_GameController* gc) : m_gc(gc){}; explicit Motor(SDL_GameController* gc) : m_gc(gc) {}
std::string GetName() const override; std::string GetName() const override;
void SetState(ControlState state) override; void SetState(ControlState state) override;
@ -114,7 +114,7 @@ private:
class MotorL : public Output class MotorL : public Output
{ {
public: public:
explicit MotorL(SDL_GameController* gc) : m_gc(gc){}; explicit MotorL(SDL_GameController* gc) : m_gc(gc) {}
std::string GetName() const override; std::string GetName() const override;
void SetState(ControlState state) override; void SetState(ControlState state) override;
@ -122,11 +122,10 @@ private:
SDL_GameController* const m_gc; SDL_GameController* const m_gc;
}; };
// Rumble
class MotorR : public Output class MotorR : public Output
{ {
public: public:
explicit MotorR(SDL_GameController* gc) : m_gc(gc){}; explicit MotorR(SDL_GameController* gc) : m_gc(gc) {}
std::string GetName() const override; std::string GetName() const override;
void SetState(ControlState state) override; void SetState(ControlState state) override;