ControllerInterface: Add InputBackend interface and SDL implementation.

This commit is contained in:
Jordan Woyak 2022-10-22 16:13:35 -05:00
parent dc046a2470
commit 44a4573303
8 changed files with 125 additions and 46 deletions

View file

@ -64,7 +64,7 @@ void ControllerInterface::Initialize(const WindowSystemInfo& wsi)
// nothing needed for OSX and Quartz
#endif
#ifdef CIFACE_USE_SDL
ciface::SDL::Init();
m_input_backends.emplace_back(ciface::SDL::CreateInputBackend(this));
#endif
#ifdef CIFACE_USE_ANDROID
// nothing needed
@ -181,9 +181,6 @@ void ControllerInterface::RefreshDevices(RefreshReason reason)
ciface::Quartz::PopulateDevices(m_wsi.render_window);
}
#endif
#ifdef CIFACE_USE_SDL
ciface::SDL::PopulateDevices();
#endif
#ifdef CIFACE_USE_ANDROID
ciface::Android::PopulateDevices();
#endif
@ -197,6 +194,9 @@ void ControllerInterface::RefreshDevices(RefreshReason reason)
ciface::DualShockUDPClient::PopulateDevices();
#endif
for (auto& backend : m_input_backends)
backend->PopulateDevices();
WiimoteReal::PopulateDevices();
if (m_populating_devices_counter.fetch_sub(1) == 1)
@ -242,9 +242,6 @@ void ControllerInterface::Shutdown()
ciface::OSX::DeInit();
ciface::Quartz::DeInit();
#endif
#ifdef CIFACE_USE_SDL
ciface::SDL::DeInit();
#endif
#ifdef CIFACE_USE_ANDROID
// nothing needed
#endif
@ -255,6 +252,8 @@ void ControllerInterface::Shutdown()
ciface::DualShockUDPClient::DeInit();
#endif
m_input_backends.clear();
// Make sure no devices had been added within Shutdown() in the time
// between checking they checked atomic m_is_init bool and we changed it.
// We couldn't have locked m_devices_population_mutex for the whole Shutdown()
@ -384,15 +383,19 @@ void ControllerInterface::UpdateInput()
// TODO: if we are an emulation input channel, we should probably always lock
// Prefer outdated values over blocking UI or CPU thread (avoids short but noticeable frame drop)
if (m_devices_mutex.try_lock())
if (!m_devices_mutex.try_lock())
return;
std::lock_guard lk(m_devices_mutex, std::adopt_lock);
for (auto& backend : m_input_backends)
backend->UpdateInput();
for (const auto& d : m_devices)
{
std::lock_guard lk(m_devices_mutex, std::adopt_lock);
for (const auto& d : m_devices)
{
// Theoretically we could avoid updating input on devices that don't have any references to
// them, but in practice a few devices types could break in different ways, so we don't
d->UpdateInput();
}
// Theoretically we could avoid updating input on devices that don't have any references to
// them, but in practice a few devices types could break in different ways, so we don't
d->UpdateInput();
}
}