From 7355b5f70d2bdf68c356873f2311025a81c7972a Mon Sep 17 00:00:00 2001 From: Michael M Date: Thu, 9 Nov 2017 12:14:21 -0800 Subject: ControllerInterface: invoke callbacks in AddDevice/RemoveDevice Some backends already cause this to happen, so make it consistent across systems. --- .../ControllerInterface/ControllerInterface.cpp | 55 ++++++++++++++-------- 1 file changed, 36 insertions(+), 19 deletions(-) (limited to 'Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp') diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 1a72fa172c..4f35c4a989 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -2,9 +2,11 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include "InputCommon/ControllerInterface/ControllerInterface.h" + #include -#include "InputCommon/ControllerInterface/ControllerInterface.h" +#include "Common/Logging/Log.h" #ifdef CIFACE_USE_XINPUT #include "InputCommon/ControllerInterface/XInput/XInput.h" @@ -165,30 +167,45 @@ void ControllerInterface::Shutdown() void ControllerInterface::AddDevice(std::shared_ptr device) { - std::lock_guard 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 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)); + InvokeHotplugCallbacks(); } void ControllerInterface::RemoveDevice(std::function callback) { - std::lock_guard 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 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()); + } + InvokeHotplugCallbacks(); } // -- cgit v1.2.3 From 1ed7532af850691eea8d7eee502f59ed20a37694 Mon Sep 17 00:00:00 2001 From: Michael M Date: Sat, 4 Nov 2017 07:36:30 -0700 Subject: ControllerInterface: HotplugCallbacks -> DevicesChangedCallbacks --- .../ControllerInterface/ControllerInterface.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp') diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 4f35c4a989..1552cd7dc2 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -188,7 +188,7 @@ void ControllerInterface::AddDevice(std::shared_ptr device NOTICE_LOG(SERIALINTERFACE, "Added device: %s", device->GetQualifiedName().c_str()); m_devices.emplace_back(std::move(device)); } - InvokeHotplugCallbacks(); + InvokeDevicesChangedCallbacks(); } void ControllerInterface::RemoveDevice(std::function callback) @@ -205,7 +205,7 @@ void ControllerInterface::RemoveDevice(std::function callback) +void ControllerInterface::RegisterDevicesChangedCallback(std::function callback) { - m_hotplug_callbacks.emplace_back(std::move(callback)); + 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) + for (const auto& callback : m_devices_changed_callbacks) callback(); } -- cgit v1.2.3 From fd7cbd633edbac9e38592b9253a507410d7d6553 Mon Sep 17 00:00:00 2001 From: Michael M Date: Fri, 10 Nov 2017 12:29:25 -0800 Subject: ControllerInterface: add mutex around callbacks vector --- Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp') diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 1552cd7dc2..9e3c7ba7af 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -4,7 +4,7 @@ #include "InputCommon/ControllerInterface/ControllerInterface.h" -#include +#include #include "Common/Logging/Log.h" @@ -232,6 +232,7 @@ void ControllerInterface::UpdateInput() // void ControllerInterface::RegisterDevicesChangedCallback(std::function callback) { + std::lock_guard lk(m_callbacks_mutex); m_devices_changed_callbacks.emplace_back(std::move(callback)); } @@ -242,6 +243,7 @@ void ControllerInterface::RegisterDevicesChangedCallback(std::function c // void ControllerInterface::InvokeDevicesChangedCallbacks() const { + std::lock_guard lk(m_callbacks_mutex); for (const auto& callback : m_devices_changed_callbacks) callback(); } -- cgit v1.2.3 From 8e6677be90d67a652eb4bcf7d0b76f548e254172 Mon Sep 17 00:00:00 2001 From: Michael M Date: Sat, 4 Nov 2017 07:37:03 -0700 Subject: ControllerInterface: don't call InvokeDevicesChangedCallbacks more than once when refreshing --- .../ControllerInterface/ControllerInterface.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp') diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 9e3c7ba7af..328258a8b2 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -47,6 +47,7 @@ void ControllerInterface::Initialize(void* const hwnd) return; m_hwnd = hwnd; + m_is_populating_devices = true; #ifdef CIFACE_USE_DINPUT // nothing needed @@ -88,6 +89,8 @@ void ControllerInterface::RefreshDevices() m_devices.clear(); } + m_is_populating_devices = true; + #ifdef CIFACE_USE_DINPUT ciface::DInput::PopulateDevices(reinterpret_cast(m_hwnd)); #endif @@ -113,6 +116,9 @@ void ControllerInterface::RefreshDevices() #ifdef CIFACE_USE_PIPES ciface::Pipes::PopulateDevices(); #endif + + m_is_populating_devices = false; + InvokeDevicesChangedCallbacks(); } // @@ -188,7 +194,9 @@ void ControllerInterface::AddDevice(std::shared_ptr device NOTICE_LOG(SERIALINTERFACE, "Added device: %s", device->GetQualifiedName().c_str()); m_devices.emplace_back(std::move(device)); } - InvokeDevicesChangedCallbacks(); + + if (!m_is_populating_devices) + InvokeDevicesChangedCallbacks(); } void ControllerInterface::RemoveDevice(std::function callback) @@ -205,7 +213,9 @@ void ControllerInterface::RemoveDevice(std::function