From eadbdd6bc389cd89c3e3e57a31dabaeadf161865 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 9 Mar 2019 09:57:37 -0600 Subject: ControllerInterface/Win32: Prevent devcies from losing their "id" on a hotplug event. --- .../ControllerInterface/ControllerInterface.cpp | 37 ++++++++++++++-------- 1 file changed, 24 insertions(+), 13 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 3a4f374c9d..bb5ccb3f0e 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -93,6 +93,9 @@ void ControllerInterface::RefreshDevices() m_is_populating_devices = true; + // Make sure shared_ptr objects are released before repopulating. + InvokeDevicesChangedCallbacks(); + #ifdef CIFACE_USE_WIN32 ciface::Win32::PopulateDevices(m_wsi.render_surface); #endif @@ -179,21 +182,29 @@ 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 is_id_in_use = [&device, this](int id) { + return std::any_of(m_devices.begin(), m_devices.end(), [&device, &id](const auto& d) { + return d->GetSource() == device->GetSource() && d->GetName() == device->GetName() && + d->GetId() == id; + }); + }; + + const auto preferred_id = device->GetPreferredId(); + if (preferred_id.has_value() && !is_id_in_use(*preferred_id)) { - 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++; + // Use the device's preferred ID if available. + device->SetId(*preferred_id); + } + else + { + // Find the first available ID to use. + int id = 0; + while (is_id_in_use(id)) + ++id; + + device->SetId(id); } - device->SetId(id); NOTICE_LOG(SERIALINTERFACE, "Added device: %s", device->GetQualifiedName().c_str()); m_devices.emplace_back(std::move(device)); -- cgit v1.2.3