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(); } // diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h index 23afab7d67..eb94a8de67 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.h @@ -54,9 +54,10 @@ public: void UpdateInput(); void RegisterHotplugCallback(std::function callback); - void InvokeHotplugCallbacks() const; private: + void InvokeHotplugCallbacks() const; + std::vector> m_hotplug_callbacks; bool m_is_init; void* m_hwnd; diff --git a/Source/Core/InputCommon/ControllerInterface/Device.cpp b/Source/Core/InputCommon/ControllerInterface/Device.cpp index f83dae268e..ee28115adc 100644 --- a/Source/Core/InputCommon/ControllerInterface/Device.cpp +++ b/Source/Core/InputCommon/ControllerInterface/Device.cpp @@ -2,12 +2,14 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include "InputCommon/ControllerInterface/Device.h" + #include #include #include #include -#include "InputCommon/ControllerInterface/Device.h" +#include "Common/StringUtil.h" namespace ciface { @@ -39,6 +41,11 @@ void Device::AddOutput(Device::Output* const o) m_outputs.push_back(o); } +std::string Device::GetQualifiedName() const +{ + return StringFromFormat("%s/%i/%s", this->GetSource().c_str(), GetId(), this->GetName().c_str()); +} + Device::Input* Device::FindInput(const std::string& name) const { for (Input* input : m_inputs) diff --git a/Source/Core/InputCommon/ControllerInterface/Device.h b/Source/Core/InputCommon/ControllerInterface/Device.h index 7b601cc775..3bf9621e45 100644 --- a/Source/Core/InputCommon/ControllerInterface/Device.h +++ b/Source/Core/InputCommon/ControllerInterface/Device.h @@ -79,6 +79,7 @@ public: void SetId(int id) { m_id = id; } virtual std::string GetName() const = 0; virtual std::string GetSource() const = 0; + std::string GetQualifiedName() const; virtual void UpdateInput() {} virtual bool IsValid() const { return true; } const std::vector& Inputs() const { return m_inputs; } diff --git a/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm b/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm index b13701209e..a923c1ea26 100644 --- a/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm +++ b/Source/Core/InputCommon/ControllerInterface/OSX/OSX.mm @@ -156,8 +156,6 @@ static void DeviceRemovalCallback(void* inContext, IOReturn inResult, void* inSe return false; }); - g_controller_interface.InvokeHotplugCallbacks(); - NOTICE_LOG(SERIALINTERFACE, "Removed device: %s", GetDeviceRefName(inIOHIDDeviceRef).c_str()); } static void DeviceMatchingCallback(void* inContext, IOReturn inResult, void* inSender, @@ -174,9 +172,6 @@ static void DeviceMatchingCallback(void* inContext, IOReturn inResult, void* inS { g_controller_interface.AddDevice(std::make_shared(inIOHIDDeviceRef, name)); } - - NOTICE_LOG(SERIALINTERFACE, "Added device: %s", name.c_str()); - g_controller_interface.InvokeHotplugCallbacks(); } void Init(void* window) diff --git a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp index 6d9b7c1091..0223637b13 100644 --- a/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp +++ b/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp @@ -92,9 +92,7 @@ static void HotplugThreadFunc() g_controller_interface.RemoveDevice([&name](const auto& device) { return device->GetSource() == "evdev" && device->GetName() == name && !device->IsValid(); }); - NOTICE_LOG(SERIALINTERFACE, "Removed device: %s", name.c_str()); s_devnode_name_map.erase(devnode); - g_controller_interface.InvokeHotplugCallbacks(); } // Only react to "device added" events for evdev devices that we can access. else if (strcmp(action, "add") == 0 && access(devnode, W_OK) == 0) @@ -107,8 +105,6 @@ static void HotplugThreadFunc() { g_controller_interface.AddDevice(std::move(device)); s_devnode_name_map.insert(std::pair(devnode, name)); - NOTICE_LOG(SERIALINTERFACE, "Added new device: %s", name.c_str()); - g_controller_interface.InvokeHotplugCallbacks(); } } udev_device_unref(dev);