gui/input: only init and quit SDL once for all instances

Calling SDL_Quit while two handlers are active can cause controllers to disconnect
This commit is contained in:
Megamouse 2024-02-22 17:38:18 +01:00
parent 96afc15fa2
commit 3067c86d65

View file

@ -5,8 +5,13 @@
#include "Emu/system_utils.hpp" #include "Emu/system_utils.hpp"
#include "Emu/system_config.h" #include "Emu/system_config.h"
#include <mutex>
LOG_CHANNEL(sdl_log, "SDL"); LOG_CHANNEL(sdl_log, "SDL");
std::mutex g_sdl_mutex;
u32 g_sdl_handler_count = 0;
constexpr u32 rumble_duration_ms = 500; // Some high number to keep rumble updates at a minimum. constexpr u32 rumble_duration_ms = 500; // Some high number to keep rumble updates at a minimum.
constexpr u32 rumble_refresh_ms = rumble_duration_ms - 100; // We need to keep updating the rumble. Choose a refresh timeout that is unlikely to run into missed rumble updates. constexpr u32 rumble_refresh_ms = rumble_duration_ms - 100; // We need to keep updating the rumble. Choose a refresh timeout that is unlikely to run into missed rumble updates.
@ -83,7 +88,13 @@ sdl_pad_handler::~sdl_pad_handler()
} }
} }
// Only quit SDL if this is the last instance of the handler. SDL uses a global state internally...
std::lock_guard lock(g_sdl_mutex);
if (g_sdl_handler_count > 0 && --g_sdl_handler_count == 0)
{
sdl_log.notice("Quitting SDL ...");
SDL_Quit(); SDL_Quit();
}
} }
void sdl_pad_handler::init_config(cfg_pad* cfg) void sdl_pad_handler::init_config(cfg_pad* cfg)
@ -146,6 +157,13 @@ bool sdl_pad_handler::Init()
if (m_is_init) if (m_is_init)
return true; return true;
std::lock_guard lock(g_sdl_mutex);
// Only init SDL if this is the first instance of the handler. SDL uses a global state internally...
if (g_sdl_handler_count++ == 0)
{
sdl_log.notice("Initializing SDL ...");
// Set non-dynamic hints before SDL_Init // Set non-dynamic hints before SDL_Init
if (!SDL_SetHint(SDL_HINT_JOYSTICK_THREAD, "1")) if (!SDL_SetHint(SDL_HINT_JOYSTICK_THREAD, "1"))
{ {
@ -218,6 +236,7 @@ bool sdl_pad_handler::Init()
break; break;
} }
}, nullptr); }, nullptr);
}
if (g_cfg.io.load_sdl_mappings) if (g_cfg.io.load_sdl_mappings)
{ {