From b425f86121c8c9f3b4a510e522d03e432677dcea Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Thu, 10 Jan 2019 09:02:38 -0600 Subject: ControllerInterface: Allow hotplug callbacks to be unregistered and don't reload the entire config from the ini file on hotplug, just update the control references. This should fix a crash on shutdown on Android. --- .../InputCommon/ControllerInterface/ControllerInterface.cpp | 12 +++++++++++- .../InputCommon/ControllerInterface/ControllerInterface.h | 9 ++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'Source/Core/InputCommon/ControllerInterface') diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 318b288434..3da92e8b75 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -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 callback) +// Returns a handle for later removing the callback. +ControllerInterface::HotplugCallbackHandle +ControllerInterface::RegisterDevicesChangedCallback(std::function callback) { std::lock_guard 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 lk(m_callbacks_mutex); + m_devices_changed_callbacks.erase(handle); } // Invoke all callbacks that were registered diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h index 0d712e5245..7330172421 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h @@ -6,9 +6,9 @@ #include #include +#include #include #include -#include #include "Common/WindowSystemInfo.h" #include "InputCommon/ControllerInterface/Device.h" @@ -40,6 +40,8 @@ class ControllerInterface : public ciface::Core::DeviceContainer { public: + using HotplugCallbackHandle = std::list>::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 callback); + HotplugCallbackHandle RegisterDevicesChangedCallback(std::function callback); + void UnregisterDevicesChangedCallback(const HotplugCallbackHandle& handle); void InvokeDevicesChangedCallbacks() const; private: - std::vector> m_devices_changed_callbacks; + std::list> m_devices_changed_callbacks; mutable std::mutex m_callbacks_mutex; std::atomic m_is_init; std::atomic m_is_populating_devices{false}; -- cgit v1.2.3