Merge pull request #7693 from jordan-woyak/hotplug-callback-fix

ControllerInterface: Hotplug callback fixes.
This commit is contained in:
Léo Lam
2019-01-16 21:00:48 +01:00
committed by GitHub
10 changed files with 62 additions and 16 deletions

View File

@ -249,10 +249,20 @@ void ControllerInterface::UpdateInput()
// Register a callback to be called when a device is added or removed (as from the input backends'
// hotplug thread), or when devices are refreshed
void ControllerInterface::RegisterDevicesChangedCallback(std::function<void()> callback)
// Returns a handle for later removing the callback.
ControllerInterface::HotplugCallbackHandle
ControllerInterface::RegisterDevicesChangedCallback(std::function<void()> callback)
{
std::lock_guard<std::mutex> lk(m_callbacks_mutex);
m_devices_changed_callbacks.emplace_back(std::move(callback));
return std::prev(m_devices_changed_callbacks.end());
}
// Unregister a device callback.
void ControllerInterface::UnregisterDevicesChangedCallback(const HotplugCallbackHandle& handle)
{
std::lock_guard<std::mutex> lk(m_callbacks_mutex);
m_devices_changed_callbacks.erase(handle);
}
// Invoke all callbacks that were registered

View File

@ -6,9 +6,9 @@
#include <atomic>
#include <functional>
#include <list>
#include <memory>
#include <mutex>
#include <vector>
#include "Common/WindowSystemInfo.h"
#include "InputCommon/ControllerInterface/Device.h"
@ -40,6 +40,8 @@
class ControllerInterface : public ciface::Core::DeviceContainer
{
public:
using HotplugCallbackHandle = std::list<std::function<void()>>::iterator;
ControllerInterface() : m_is_init(false) {}
void Initialize(const WindowSystemInfo& wsi);
void ChangeWindow(void* hwnd);
@ -50,11 +52,12 @@ public:
bool IsInit() const { return m_is_init; }
void UpdateInput();
void RegisterDevicesChangedCallback(std::function<void(void)> callback);
HotplugCallbackHandle RegisterDevicesChangedCallback(std::function<void(void)> callback);
void UnregisterDevicesChangedCallback(const HotplugCallbackHandle& handle);
void InvokeDevicesChangedCallbacks() const;
private:
std::vector<std::function<void()>> m_devices_changed_callbacks;
std::list<std::function<void()>> m_devices_changed_callbacks;
mutable std::mutex m_callbacks_mutex;
std::atomic<bool> m_is_init;
std::atomic<bool> m_is_populating_devices{false};