2020-06-24 02:49:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019-2020 PixlOne
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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-24 22:10:25 +00:00
|
|
|
#include "util/log.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-07-12 06:51:56 +00:00
|
|
|
#include "backend/Error.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) {
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(WARN, "I/O error on %s: %s, skipping device.",
|
2020-06-20 01:58:33 +00:00
|
|
|
path.c_str(), e.what());
|
|
|
|
return;
|
2020-07-12 06:51:56 +00:00
|
|
|
} catch (TimeoutError &e) {
|
|
|
|
logPrintf(WARN, "Device %s timed out.", path.c_str());
|
|
|
|
defaultExists = false;
|
2020-06-18 08:47:04 +00:00
|
|
|
}
|
2020-06-20 01:58:33 +00:00
|
|
|
|
|
|
|
if(isReceiver) {
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(INFO, "Detected receiver at %s", path.c_str());
|
2020-06-20 01:58:33 +00:00
|
|
|
auto receiver = std::make_shared<Receiver>(path);
|
2020-06-21 09:33:33 +00:00
|
|
|
receiver->run();
|
2020-06-20 01:58:33 +00:00
|
|
|
_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 {
|
2020-06-24 02:35:37 +00:00
|
|
|
auto device = std::make_shared<Device>(path,
|
|
|
|
hidpp::CordedDevice);
|
2020-06-20 01:58:33 +00:00
|
|
|
_devices.emplace(path, device);
|
|
|
|
} catch(hidpp10::Error &e) {
|
|
|
|
if(e.code() != hidpp10::Error::UnknownDevice)
|
|
|
|
throw;
|
2020-06-21 09:33:33 +00:00
|
|
|
else
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(WARN,
|
2020-06-24 02:35:37 +00:00
|
|
|
"HID++ 1.0 error while trying to initialize %s:"
|
|
|
|
"%s", path.c_str(), e.what());
|
2020-06-20 01:58:33 +00:00
|
|
|
} catch(hidpp::Device::InvalidDevice &e) { // Ignore
|
|
|
|
} catch(std::system_error &e) {
|
|
|
|
// This error should have been thrown previously
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(WARN, "I/O error on %s: %s", path.c_str(),
|
2020-06-20 01:58:33 +00:00
|
|
|
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);
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(INFO, "Receiver on %s disconnected", path.c_str());
|
2020-06-20 01:58:33 +00:00
|
|
|
} else {
|
|
|
|
auto device = _devices.find(path);
|
|
|
|
if(device != _devices.find(path)) {
|
|
|
|
_devices.erase(device);
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(INFO, "Device on %s disconnected", path.c_str());
|
2020-06-20 01:58:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 23:53:38 +00:00
|
|
|
}
|