Switched device removal and insertion responsibility to DeviceFinder
This commit is contained in:
parent
13bce5cc76
commit
ba0bf93b80
|
@ -248,19 +248,7 @@ void ReceiverHandler::handleEvent(const HIDPP::Report &event)
|
||||||
{
|
{
|
||||||
case HIDPP10::IReceiver::DeviceUnpaired:
|
case HIDPP10::IReceiver::DeviceUnpaired:
|
||||||
{
|
{
|
||||||
// Find device, stop it, and delete it
|
finder->stopAndDeleteDevice(dev->path, event.deviceIndex());
|
||||||
auto it = finder->devices.begin();
|
|
||||||
while (it != finder->devices.end())
|
|
||||||
{
|
|
||||||
if(it->first->path == dev->path && it->first->index == event.deviceIndex())
|
|
||||||
{
|
|
||||||
log_printf(INFO, "%s (Device %d on %s) unpaired.", it->first->name.c_str(), event.deviceIndex(), dev->path.c_str());
|
|
||||||
it->first->stop();
|
|
||||||
it->second.join();
|
|
||||||
finder->devices.erase(it);
|
|
||||||
}
|
|
||||||
else it++;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HIDPP10::IReceiver::DevicePaired:
|
case HIDPP10::IReceiver::DevicePaired:
|
||||||
|
|
|
@ -14,6 +14,76 @@
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "Device.h"
|
#include "Device.h"
|
||||||
|
|
||||||
|
void stopAndDeletePairedDevice (PairedDevice &pairedDevice)
|
||||||
|
{
|
||||||
|
log_printf(INFO, "%s (Device %d on %s) disconnected", pairedDevice.device->name.c_str(), pairedDevice.device->index, pairedDevice.device->path.c_str());
|
||||||
|
pairedDevice.device->stop();
|
||||||
|
pairedDevice.associatedThread.join();
|
||||||
|
delete(pairedDevice.device);
|
||||||
|
}
|
||||||
|
|
||||||
|
DeviceFinder::~DeviceFinder()
|
||||||
|
{
|
||||||
|
this->devicesMutex.lock();
|
||||||
|
for (auto it = this->devices.begin(); it != this->devices.end(); it++) {
|
||||||
|
for (auto jt = it->second.begin(); jt != it->second.end(); jt++) {
|
||||||
|
stopAndDeletePairedDevice(jt->second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this->devicesMutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeviceFinder::insertNewDevice(const std::string &path, HIDPP::DeviceIndex index)
|
||||||
|
{
|
||||||
|
Device *device = new Device(path, index);
|
||||||
|
|
||||||
|
this->devicesMutex.lock();
|
||||||
|
log_printf(INFO, "%s detected: device %d on %s", device->name.c_str(), index, path.c_str());
|
||||||
|
auto pathBucket = this->devices.emplace(path, std::map<HIDPP::DeviceIndex, PairedDevice>()).first;
|
||||||
|
pathBucket->second.emplace(index, PairedDevice{
|
||||||
|
device,
|
||||||
|
std::thread([device]() {
|
||||||
|
device->start();
|
||||||
|
})
|
||||||
|
});
|
||||||
|
this->devicesMutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeviceFinder::stopAndDeleteAllDevicesIn (const std::string &path)
|
||||||
|
{
|
||||||
|
this->devicesMutex.lock();
|
||||||
|
auto pathBucket = this->devices.find(path);
|
||||||
|
if (pathBucket != this->devices.end())
|
||||||
|
{
|
||||||
|
for (auto& indexBucket : pathBucket->second) {
|
||||||
|
stopAndDeletePairedDevice(indexBucket.second);
|
||||||
|
}
|
||||||
|
this->devices.erase(pathBucket);
|
||||||
|
}
|
||||||
|
this->devicesMutex.unlock();
|
||||||
|
|
||||||
|
log_printf(WARN, "Attempted to disconnect not previously connected devices on %s", path.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeviceFinder::stopAndDeleteDevice (const std::string &path, HIDPP::DeviceIndex index)
|
||||||
|
{
|
||||||
|
this->devicesMutex.lock();
|
||||||
|
auto pathBucket = this->devices.find(path);
|
||||||
|
if (pathBucket != this->devices.end())
|
||||||
|
{
|
||||||
|
auto indexBucket = pathBucket->second.find(index);
|
||||||
|
if (indexBucket != pathBucket->second.end())
|
||||||
|
{
|
||||||
|
stopAndDeletePairedDevice(indexBucket->second);
|
||||||
|
pathBucket->second.erase(indexBucket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this->devicesMutex.unlock();
|
||||||
|
|
||||||
|
log_printf(WARN, "Attempted to disconnect not previously connected device %d on %s", index, path.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void DeviceFinder::addDevice(const char *path)
|
void DeviceFinder::addDevice(const char *path)
|
||||||
{
|
{
|
||||||
std::string string_path(path);
|
std::string string_path(path);
|
||||||
|
@ -34,6 +104,7 @@ void DeviceFinder::addDevice(const char *path)
|
||||||
HIDPP::WirelessDevice3, HIDPP::WirelessDevice4,
|
HIDPP::WirelessDevice3, HIDPP::WirelessDevice4,
|
||||||
HIDPP::WirelessDevice5, HIDPP::WirelessDevice6})
|
HIDPP::WirelessDevice5, HIDPP::WirelessDevice6})
|
||||||
{
|
{
|
||||||
|
|
||||||
if(!has_receiver_index && index == HIDPP::WirelessDevice1)
|
if(!has_receiver_index && index == HIDPP::WirelessDevice1)
|
||||||
break;
|
break;
|
||||||
for(int i = 0; i < max_tries; i++)
|
for(int i = 0; i < max_tries; i++)
|
||||||
|
@ -48,18 +119,7 @@ void DeviceFinder::addDevice(const char *path)
|
||||||
has_receiver_index = true;
|
has_receiver_index = true;
|
||||||
if(major > 1) // HID++ 2.0 devices only
|
if(major > 1) // HID++ 2.0 devices only
|
||||||
{
|
{
|
||||||
auto dev = new Device(string_path, index);
|
this->insertNewDevice(string_path, index);
|
||||||
|
|
||||||
this->devicesMutex.lock();
|
|
||||||
this->devices.insert({
|
|
||||||
dev,
|
|
||||||
std::thread{[dev]() {
|
|
||||||
dev->start();
|
|
||||||
}}
|
|
||||||
});
|
|
||||||
this->devicesMutex.unlock();
|
|
||||||
|
|
||||||
log_printf(INFO, "%s detected: device %d on %s", d.name().c_str(), index, string_path.c_str());
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -106,18 +166,5 @@ void DeviceFinder::addDevice(const char *path)
|
||||||
|
|
||||||
void DeviceFinder::removeDevice(const char* path)
|
void DeviceFinder::removeDevice(const char* path)
|
||||||
{
|
{
|
||||||
devicesMutex.lock();
|
this->stopAndDeleteAllDevicesIn(std::string(path));
|
||||||
// Iterate through Devices, stop all in path
|
|
||||||
for (auto it = devices.begin(); it != devices.end(); it++)
|
|
||||||
{
|
|
||||||
if(it->first->path == path)
|
|
||||||
{
|
|
||||||
log_printf(INFO, "%s on %s disconnected.", it->first->name.c_str(), path);
|
|
||||||
it->first->stop();
|
|
||||||
it->second.join();
|
|
||||||
delete(it->first);
|
|
||||||
devices.erase(it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
devicesMutex.unlock();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,22 +5,34 @@
|
||||||
#include <hidpp10/Device.h>
|
#include <hidpp10/Device.h>
|
||||||
#include <hidpp10/IReceiver.h>
|
#include <hidpp10/IReceiver.h>
|
||||||
#include <hidpp20/IReprogControls.h>
|
#include <hidpp20/IReprogControls.h>
|
||||||
#include <map>
|
#include <unordered_map>
|
||||||
|
#include <list>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include "Device.h"
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
|
#include "Device.h"
|
||||||
|
|
||||||
class Device;
|
class Device;
|
||||||
|
|
||||||
|
struct PairedDevice {
|
||||||
|
Device *device;
|
||||||
|
std::thread associatedThread;
|
||||||
|
};
|
||||||
|
|
||||||
class DeviceFinder : public HID::DeviceMonitor
|
class DeviceFinder : public HID::DeviceMonitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
std::map<Device*, std::thread> devices;
|
~DeviceFinder();
|
||||||
|
|
||||||
|
void insertNewDevice (const std::string &path, HIDPP::DeviceIndex index);
|
||||||
|
void stopAndDeleteAllDevicesIn (const std::string &path);
|
||||||
|
void stopAndDeleteDevice (const std::string &path, HIDPP::DeviceIndex index);
|
||||||
protected:
|
protected:
|
||||||
void addDevice(const char* path);
|
void addDevice(const char* path);
|
||||||
void removeDevice(const char* path);
|
void removeDevice(const char* path);
|
||||||
private:
|
private:
|
||||||
std::mutex devicesMutex;
|
std::mutex devicesMutex;
|
||||||
|
std::map<std::string, std::map<HIDPP::DeviceIndex, PairedDevice>> devices;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern DeviceFinder* finder;
|
extern DeviceFinder* finder;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user