summaryrefslogtreecommitdiff
path: root/Source
diff options
context:
space:
mode:
authorJordan Woyak <jordan.woyak@gmail.com>2026-11-19 02:44:34 -0600
committerJordan Woyak <jordan.woyak@gmail.com>2026-11-21 15:05:45 -0600
commit737fd4cbb075095a3bc7a943a87439e10889fdba (patch)
tree78798d92cf16af5412da2cd8fcfb5e7966d92ff7 /Source
parent6071d649e79807dbb796d29988ebb29014c46dda (diff)
ControllerInterface/SDL: Use jthread/stop_token to clean up hotplug thread shutdown and eliminate some members.
Also rename the thread since the name was too long for Linux and "Thread" is redundant.
Diffstat (limited to 'Source')
-rw-r--r--Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp72
1 files changed, 27 insertions, 45 deletions
diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp
index 99e953f146..49b86a7274 100644
--- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp
+++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp
@@ -4,6 +4,7 @@
#include "InputCommon/ControllerInterface/SDL/SDL.h"
#include <span>
+#include <stop_token>
#include <thread>
#include <vector>
@@ -13,8 +14,8 @@
#include <SDL3/SDL.h>
-#include "Common/Event.h"
#include "Common/Logging/Log.h"
+#include "Common/OneShotEvent.h"
#include "Common/ScopeGuard.h"
#include "Common/Thread.h"
@@ -32,18 +33,15 @@ class InputBackend final : public ciface::InputBackend
{
public:
InputBackend(ControllerInterface* controller_interface);
- ~InputBackend() override;
void PopulateDevices() override;
private:
void OpenAndAddDevice(SDL_JoystickID instance_id);
- bool HandleEventAndContinue(const SDL_Event& e);
+ void HandleEventAndContinue(const SDL_Event& e);
- Common::Event m_init_event;
- Uint32 m_stop_event_type;
- Uint32 m_populate_event_type;
- std::thread m_hotplug_thread;
+ Uint32 m_populate_event_type{};
+ std::jthread m_hotplug_thread;
};
std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface)
@@ -187,19 +185,20 @@ InputBackend::InputBackend(ControllerInterface* controller_interface)
SDL_SetHint(location.key.c_str(), value->c_str());
}
- m_hotplug_thread = std::thread([this] {
- Common::SetCurrentThreadName("SDL Hotplug Thread");
+ Common::OneShotEvent init_event;
- Common::ScopeGuard quit_guard([] {
- // TODO: there seems to be some sort of memory leak with SDL, quit isn't freeing everything up
- SDL_Quit();
- });
+ m_hotplug_thread = std::jthread([this, &init_event](std::stop_token stoken) {
+ Common::SetCurrentThreadName("SDL Hotplug");
+
+ Uint32 kick_event_type{};
+
+ Common::ScopeGuard quit_guard([] { SDL_Quit(); });
{
- Common::ScopeGuard init_guard([this] { m_init_event.Set(); });
+ Common::ScopedSetter init_setter(&init_event);
if (!SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMEPAD))
{
- ERROR_LOG_FMT(CONTROLLERINTERFACE, "SDL failed to initialize");
+ ERROR_LOG_FMT(CONTROLLERINTERFACE, "SDL failed to initialize: {}", SDL_GetError());
return;
}
@@ -209,7 +208,8 @@ InputBackend::InputBackend(ControllerInterface* controller_interface)
ERROR_LOG_FMT(CONTROLLERINTERFACE, "SDL failed to register custom events");
return;
}
- m_stop_event_type = custom_events_start;
+
+ kick_event_type = custom_events_start;
m_populate_event_type = custom_events_start + 1;
// Drain all of the events and add the initial joysticks before returning. Otherwise, the
@@ -219,37 +219,25 @@ InputBackend::InputBackend(ControllerInterface* controller_interface)
// hasn't finished initializing yet.
SDL_Event e;
while (SDL_PollEvent(&e))
- {
- if (!HandleEventAndContinue(e))
- return;
- }
+ HandleEventAndContinue(e);
}
+ std::stop_callback stop{stoken, [&] {
+ SDL_Event kick_event{kick_event_type};
+ SDL_PushEvent(&kick_event);
+ }};
+
SDL_Event e;
- while (SDL_WaitEvent(&e))
- {
- if (!HandleEventAndContinue(e))
- return;
- }
+ while (!stoken.stop_requested() && SDL_WaitEvent(&e))
+ HandleEventAndContinue(e);
});
- m_init_event.Wait();
-}
-
-InputBackend::~InputBackend()
-{
- if (!m_hotplug_thread.joinable())
- return;
-
- SDL_Event stop_event{m_stop_event_type};
- SDL_PushEvent(&stop_event);
-
- m_hotplug_thread.join();
+ init_event.Wait();
}
void InputBackend::PopulateDevices()
{
- if (!m_hotplug_thread.joinable())
+ if (m_populate_event_type == 0)
return;
SDL_Event populate_event{m_populate_event_type};
@@ -277,7 +265,7 @@ void InputBackend::OpenAndAddDevice(SDL_JoystickID instance_id)
}
}
-bool InputBackend::HandleEventAndContinue(const SDL_Event& e)
+void InputBackend::HandleEventAndContinue(const SDL_Event& e)
{
if (e.type == SDL_EVENT_JOYSTICK_ADDED)
{
@@ -301,12 +289,6 @@ bool InputBackend::HandleEventAndContinue(const SDL_Event& e)
SDL_free(joystick_ids);
});
}
- else if (e.type == m_stop_event_type)
- {
- return false;
- }
-
- return true;
}
} // namespace ciface::SDL