diff options
Diffstat (limited to 'Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp')
| -rw-r--r-- | Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp | 85 |
1 files changed, 57 insertions, 28 deletions
diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 1a72fa172c..328258a8b2 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -2,10 +2,12 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. -#include <mutex> - #include "InputCommon/ControllerInterface/ControllerInterface.h" +#include <algorithm> + +#include "Common/Logging/Log.h" + #ifdef CIFACE_USE_XINPUT #include "InputCommon/ControllerInterface/XInput/XInput.h" #endif @@ -45,6 +47,7 @@ void ControllerInterface::Initialize(void* const hwnd) return; m_hwnd = hwnd; + m_is_populating_devices = true; #ifdef CIFACE_USE_DINPUT // nothing needed @@ -86,6 +89,8 @@ void ControllerInterface::RefreshDevices() m_devices.clear(); } + m_is_populating_devices = true; + #ifdef CIFACE_USE_DINPUT ciface::DInput::PopulateDevices(reinterpret_cast<HWND>(m_hwnd)); #endif @@ -111,6 +116,9 @@ void ControllerInterface::RefreshDevices() #ifdef CIFACE_USE_PIPES ciface::Pipes::PopulateDevices(); #endif + + m_is_populating_devices = false; + InvokeDevicesChangedCallbacks(); } // @@ -165,30 +173,49 @@ void ControllerInterface::Shutdown() void ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device) { - std::lock_guard<std::mutex> lk(m_devices_mutex); - // Try to find an ID for this device - int id = 0; - while (true) { - const auto it = std::find_if(m_devices.begin(), m_devices.end(), [&device, &id](const auto& d) { - return d->GetSource() == device->GetSource() && d->GetName() == device->GetName() && - d->GetId() == id; - }); - if (it == m_devices.end()) // no device with the same name with this ID, so we can use it - break; - else - id++; + std::lock_guard<std::mutex> lk(m_devices_mutex); + // Try to find an ID for this device + int id = 0; + while (true) + { + const auto it = + std::find_if(m_devices.begin(), m_devices.end(), [&device, &id](const auto& d) { + return d->GetSource() == device->GetSource() && d->GetName() == device->GetName() && + d->GetId() == id; + }); + if (it == m_devices.end()) // no device with the same name with this ID, so we can use it + break; + else + id++; + } + device->SetId(id); + + NOTICE_LOG(SERIALINTERFACE, "Added device: %s", device->GetQualifiedName().c_str()); + m_devices.emplace_back(std::move(device)); } - device->SetId(id); - m_devices.emplace_back(std::move(device)); + + if (!m_is_populating_devices) + InvokeDevicesChangedCallbacks(); } void ControllerInterface::RemoveDevice(std::function<bool(const ciface::Core::Device*)> callback) { - std::lock_guard<std::mutex> lk(m_devices_mutex); - m_devices.erase(std::remove_if(m_devices.begin(), m_devices.end(), - [&callback](const auto& dev) { return callback(dev.get()); }), - m_devices.end()); + { + std::lock_guard<std::mutex> lk(m_devices_mutex); + auto it = std::remove_if(m_devices.begin(), m_devices.end(), [&callback](const auto& dev) { + if (callback(dev.get())) + { + NOTICE_LOG(SERIALINTERFACE, "Removed device: %s", dev->GetQualifiedName().c_str()); + return true; + } + return false; + }); + m_devices.erase(it, m_devices.end()); + } + + if (!m_is_populating_devices) + InvokeDevicesChangedCallbacks(); } // @@ -208,23 +235,25 @@ void ControllerInterface::UpdateInput() } // -// RegisterHotplugCallback +// RegisterDevicesChangedCallback // -// Register a callback to be called from the input backends' hotplug thread -// when there is a new device +// 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::RegisterHotplugCallback(std::function<void()> callback) +void ControllerInterface::RegisterDevicesChangedCallback(std::function<void()> callback) { - m_hotplug_callbacks.emplace_back(std::move(callback)); + std::lock_guard<std::mutex> lk(m_callbacks_mutex); + m_devices_changed_callbacks.emplace_back(std::move(callback)); } // -// InvokeHotplugCallbacks +// InvokeDevicesChangedCallbacks // // Invoke all callbacks that were registered // -void ControllerInterface::InvokeHotplugCallbacks() const +void ControllerInterface::InvokeDevicesChangedCallbacks() const { - for (const auto& callback : m_hotplug_callbacks) + std::lock_guard<std::mutex> lk(m_callbacks_mutex); + for (const auto& callback : m_devices_changed_callbacks) callback(); } |
