summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp
diff options
context:
space:
mode:
authorJordan Woyak <jordan.woyak@gmail.com>2019-03-09 09:57:37 -0600
committerJordan Woyak <jordan.woyak@gmail.com>2019-03-29 09:04:16 -0500
commiteadbdd6bc389cd89c3e3e57a31dabaeadf161865 (patch)
treee64941bcb188067cf5355a9cee1a74b42a5ae6ab /Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp
parentd26c1ce24d9108114d7eab97cfc2ab4f3eea28ee (diff)
ControllerInterface/Win32: Prevent devcies from losing their "id" on a hotplug event.
Diffstat (limited to 'Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp')
-rw-r--r--Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp37
1 files changed, 24 insertions, 13 deletions
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<Device> 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<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 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));