ControllerInterface: Make the ID assigning code common

This makes the device ID assigning code common to all backends, by
moving it to AddDevice() instead of copy-pasting or replicating
the logic in the backends.

Also, to prepare for hotplugging, instead of relying on a name usage
count, the new ID assigning system always starts from ID 0 and tries
to assign the first ID that is not used.
This commit is contained in:
Léo Lam 2016-07-14 10:25:52 +02:00
parent 89a03174cc
commit 788e19f54d
25 changed files with 41 additions and 112 deletions

View file

@ -143,6 +143,20 @@ 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++;
}
device->SetId(id);
m_devices.emplace_back(std::move(device));
}