Code cleanup and smaller fixes

This commit is contained in:
Exzap 2026-05-08 12:44:45 +02:00
parent 3c7ccf809e
commit f0b44e7dca
2 changed files with 4 additions and 71 deletions

View File

@ -49,24 +49,18 @@ bool SDLController::is_connected()
bool SDLController::connect() bool SDLController::connect()
{ {
if (is_connected()) if (is_connected())
{
return true; return true;
}
m_has_rumble = false; m_has_rumble = false;
const auto index = m_provider->get_index(m_guid_index, m_guid); const auto index = m_provider->get_index(m_guid_index, m_guid);
std::scoped_lock lock(m_controller_mutex); std::scoped_lock lock(m_controller_mutex);
int gamepad_count = 0; int gamepad_count = 0;
SDL_JoystickID *gamepad_ids = SDL_GetGamepads(&gamepad_count); SDL_JoystickID *gamepad_ids = SDL_GetGamepads(&gamepad_count);
if (!gamepad_ids) if (!gamepad_ids || index < 0 || index >= gamepad_count)
{
return false; return false;
}
m_diid = gamepad_ids[index]; m_diid = gamepad_ids[index];
SDL_free(gamepad_ids); SDL_free(gamepad_ids);
@ -74,137 +68,79 @@ bool SDLController::connect()
m_controller = SDL_OpenGamepad(m_diid); m_controller = SDL_OpenGamepad(m_diid);
if (!m_controller) if (!m_controller)
{
return false; return false;
}
if (const char* name = SDL_GetGamepadName(m_controller)) if (const char* name = SDL_GetGamepadName(m_controller))
{
m_display_name = name; m_display_name = name;
}
for (size_t i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; ++i) for (size_t i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; ++i)
{
m_buttons[i] = SDL_GamepadHasButton(m_controller, (SDL_GamepadButton)i); m_buttons[i] = SDL_GamepadHasButton(m_controller, (SDL_GamepadButton)i);
}
for (size_t i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i) for (size_t i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i)
{
m_axis[i] = SDL_GamepadHasAxis(m_controller, (SDL_GamepadAxis)i); m_axis[i] = SDL_GamepadHasAxis(m_controller, (SDL_GamepadAxis)i);
}
if (SDL_GamepadHasSensor(m_controller, SDL_SENSOR_ACCEL)) if (SDL_GamepadHasSensor(m_controller, SDL_SENSOR_ACCEL))
{
m_has_accel = SDL_SetGamepadSensorEnabled(m_controller, SDL_SENSOR_ACCEL, true); m_has_accel = SDL_SetGamepadSensorEnabled(m_controller, SDL_SENSOR_ACCEL, true);
}
if (SDL_GamepadHasSensor(m_controller, SDL_SENSOR_GYRO)) if (SDL_GamepadHasSensor(m_controller, SDL_SENSOR_GYRO))
{
m_has_gyro = SDL_SetGamepadSensorEnabled(m_controller, SDL_SENSOR_GYRO, true); m_has_gyro = SDL_SetGamepadSensorEnabled(m_controller, SDL_SENSOR_GYRO, true);
}
m_has_rumble = SDL_RumbleGamepad(m_controller, 0, 0, 0); m_has_rumble = SDL_RumbleGamepad(m_controller, 0, 0, 0);
return true; return true;
} }
void SDLController::start_rumble() void SDLController::start_rumble()
{ {
std::scoped_lock lock(m_controller_mutex); std::scoped_lock lock(m_controller_mutex);
if (is_connected() && !m_has_rumble) if (is_connected() && !m_has_rumble)
{
return; return;
}
if (m_settings.rumble <= 0) if (m_settings.rumble <= 0)
{
return; return;
}
SDL_RumbleGamepad(m_controller, (Uint16)(m_settings.rumble * 0xFFFF), (Uint16)(m_settings.rumble * 0xFFFF), 5 * 1000); SDL_RumbleGamepad(m_controller, (Uint16)(m_settings.rumble * 0xFFFF), (Uint16)(m_settings.rumble * 0xFFFF), 5 * 1000);
} }
void SDLController::stop_rumble() void SDLController::stop_rumble()
{ {
std::scoped_lock lock(m_controller_mutex); std::scoped_lock lock(m_controller_mutex);
if (is_connected() && !m_has_rumble) if (is_connected() && !m_has_rumble)
{
return; return;
}
SDL_RumbleGamepad(m_controller, 0, 0, 0); SDL_RumbleGamepad(m_controller, 0, 0, 0);
} }
MotionSample SDLController::get_motion_sample() MotionSample SDLController::get_motion_sample()
{ {
if (is_connected() && has_motion()) if (is_connected() && has_motion())
{
return m_provider->motion_sample(m_diid); return m_provider->motion_sample(m_diid);
}
return {}; return {};
} }
std::string SDLController::get_button_name(uint64 button) const std::string SDLController::get_button_name(uint64 button) const
{ {
if (const char* name = SDL_GetGamepadStringForButton((SDL_GamepadButton)button)) if (const char* name = SDL_GetGamepadStringForButton((SDL_GamepadButton)button))
{
return name; return name;
}
return base_type::get_button_name(button); return base_type::get_button_name(button);
} }
ControllerState SDLController::raw_state() ControllerState SDLController::raw_state()
{ {
ControllerState result{}; ControllerState result{};
std::scoped_lock lock(m_controller_mutex); std::scoped_lock lock(m_controller_mutex);
if (!is_connected()) if (!is_connected())
{
return result; return result;
}
for (size_t i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; ++i) for (size_t i = 0; i < SDL_GAMEPAD_BUTTON_COUNT; ++i)
{ {
if (m_buttons[i] && SDL_GetGamepadButton(m_controller, (SDL_GamepadButton)i)) if (m_buttons[i] && SDL_GetGamepadButton(m_controller, (SDL_GamepadButton)i))
{
result.buttons.SetButtonState(i, true); result.buttons.SetButtonState(i, true);
}
} }
if (m_axis[SDL_GAMEPAD_AXIS_LEFTX]) if (m_axis[SDL_GAMEPAD_AXIS_LEFTX])
{
result.axis.x = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_LEFTX) / 32767.0f; result.axis.x = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_LEFTX) / 32767.0f;
}
if (m_axis[SDL_GAMEPAD_AXIS_LEFTY]) if (m_axis[SDL_GAMEPAD_AXIS_LEFTY])
{
result.axis.y = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_LEFTY) / 32767.0f; result.axis.y = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_LEFTY) / 32767.0f;
}
if (m_axis[SDL_GAMEPAD_AXIS_RIGHTX]) if (m_axis[SDL_GAMEPAD_AXIS_RIGHTX])
{
result.rotation.x = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_RIGHTX) / 32767.0f; result.rotation.x = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_RIGHTX) / 32767.0f;
}
if (m_axis[SDL_GAMEPAD_AXIS_RIGHTY]) if (m_axis[SDL_GAMEPAD_AXIS_RIGHTY])
{
result.rotation.y = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_RIGHTY) / 32767.0f; result.rotation.y = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_RIGHTY) / 32767.0f;
}
if (m_axis[SDL_GAMEPAD_AXIS_LEFT_TRIGGER]) if (m_axis[SDL_GAMEPAD_AXIS_LEFT_TRIGGER])
{
result.trigger.x = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) / 32767.0f; result.trigger.x = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) / 32767.0f;
}
if (m_axis[SDL_GAMEPAD_AXIS_RIGHT_TRIGGER]) if (m_axis[SDL_GAMEPAD_AXIS_RIGHT_TRIGGER])
{
result.trigger.y = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) / 32767.0f; result.trigger.y = (float)SDL_GetGamepadAxis(m_controller, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) / 32767.0f;
}
return result; return result;
} }

View File

@ -6,10 +6,6 @@ class ScreenSaver
public: public:
static void SetInhibit(bool inhibit) static void SetInhibit(bool inhibit)
{ {
// temporary workaround because feature crashes on macOS
#if BOOST_OS_MACOS
return;
#endif
bool* inhibitArg = new bool(inhibit); bool* inhibitArg = new bool(inhibit);
if (!SDL_RunOnMainThread(SetInhibitCallback, inhibitArg, false)) if (!SDL_RunOnMainThread(SetInhibitCallback, inhibitArg, false))
@ -27,8 +23,9 @@ private:
return; return;
} }
bool inhibit = *static_cast<bool*>(userdata); auto* inhibitArg = static_cast<bool*>(userdata);
SDL_free(userdata); bool inhibit = *inhibitArg;
delete inhibitArg;
if (SDL_WasInit(SDL_INIT_VIDEO) == 0) if (SDL_WasInit(SDL_INIT_VIDEO) == 0)
{ {