2020-06-16 23:53:38 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <sstream>
|
|
|
|
|
2020-06-20 02:01:14 +00:00
|
|
|
#include "DeviceManager.h"
|
2020-06-20 01:58:33 +00:00
|
|
|
#include "Receiver.h"
|
2020-06-16 23:53:38 +00:00
|
|
|
#include "util.h"
|
2020-06-18 08:47:04 +00:00
|
|
|
#include "backend/hidpp10/Error.h"
|
2020-06-19 07:58:00 +00:00
|
|
|
#include "backend/dj/Receiver.h"
|
2020-06-16 23:53:38 +00:00
|
|
|
|
|
|
|
#define NON_WIRELESS_DEV(index) (index) == HIDPP::DefaultDevice ? "default" : "corded"
|
|
|
|
|
|
|
|
using namespace logid;
|
|
|
|
using namespace logid::backend;
|
|
|
|
|
2020-06-20 02:01:14 +00:00
|
|
|
void DeviceManager::addDevice(std::string path)
|
2020-06-16 23:53:38 +00:00
|
|
|
{
|
2020-06-20 01:58:33 +00:00
|
|
|
bool defaultExists = true;
|
|
|
|
bool isReceiver = false;
|
2020-06-16 23:53:38 +00:00
|
|
|
try {
|
2020-06-20 01:58:33 +00:00
|
|
|
hidpp::Device device(path, hidpp::DefaultDevice);
|
|
|
|
isReceiver = device.version() == std::make_tuple(1, 0);
|
|
|
|
} catch(hidpp10::Error &e) {
|
|
|
|
if(e.code() != hidpp10::Error::UnknownDevice)
|
2020-06-18 08:47:04 +00:00
|
|
|
throw;
|
2020-06-20 01:58:33 +00:00
|
|
|
} catch(hidpp::Device::InvalidDevice &e) { // Ignore
|
|
|
|
defaultExists = false;
|
|
|
|
} catch(std::system_error &e) {
|
|
|
|
log_printf(WARN, "I/O error on %s: %s, skipping device.",
|
|
|
|
path.c_str(), e.what());
|
|
|
|
return;
|
2020-06-18 08:47:04 +00:00
|
|
|
}
|
2020-06-20 01:58:33 +00:00
|
|
|
|
|
|
|
if(isReceiver) {
|
|
|
|
log_printf(INFO, "Detected receiver at %s", path.c_str());
|
|
|
|
auto receiver = std::make_shared<Receiver>(path);
|
|
|
|
_receivers.emplace(path, receiver);
|
|
|
|
} else {
|
|
|
|
/* TODO: Error check?
|
|
|
|
* TODO: Can non-receivers only contain 1 device?
|
|
|
|
* If the device exists, it is guaranteed to be an HID++ 2.0 device */
|
|
|
|
if(defaultExists) {
|
|
|
|
auto device = std::make_shared<Device>(path, hidpp::DefaultDevice);
|
|
|
|
_devices.emplace(path, device);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
auto device = std::make_shared<Device>(path, hidpp::CordedDevice);
|
|
|
|
_devices.emplace(path, device);
|
|
|
|
} catch(hidpp10::Error &e) {
|
|
|
|
if(e.code() != hidpp10::Error::UnknownDevice)
|
|
|
|
throw;
|
|
|
|
} catch(hidpp::Device::InvalidDevice &e) { // Ignore
|
|
|
|
} catch(std::system_error &e) {
|
|
|
|
// This error should have been thrown previously
|
|
|
|
log_printf(WARN, "I/O error on %s: %s", path.c_str(),
|
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 23:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 02:01:14 +00:00
|
|
|
void DeviceManager::removeDevice(std::string path)
|
2020-06-16 23:53:38 +00:00
|
|
|
{
|
2020-06-20 01:58:33 +00:00
|
|
|
auto receiver = _receivers.find(path);
|
|
|
|
|
|
|
|
if(receiver != _receivers.end()) {
|
|
|
|
_receivers.erase(receiver);
|
|
|
|
log_printf(INFO, "Receiver on %s disconnected", path.c_str());
|
|
|
|
} else {
|
|
|
|
auto device = _devices.find(path);
|
|
|
|
if(device != _devices.find(path)) {
|
|
|
|
_devices.erase(device);
|
|
|
|
log_printf(INFO, "Device on %s disconnected", path.c_str());
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 23:53:38 +00:00
|
|
|
}
|