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-20 01:58:33 +00:00
|
|
|
#include "Receiver.h"
|
2020-06-24 22:10:25 +00:00
|
|
|
#include "util/log.h"
|
2020-06-21 09:33:33 +00:00
|
|
|
#include "backend/hidpp10/Error.h"
|
|
|
|
#include "backend/hidpp20/Error.h"
|
2020-07-12 08:42:44 +00:00
|
|
|
#include "backend/Error.h"
|
2020-06-20 01:58:33 +00:00
|
|
|
|
|
|
|
using namespace logid;
|
2020-06-21 09:33:33 +00:00
|
|
|
using namespace logid::backend;
|
2020-06-20 01:58:33 +00:00
|
|
|
|
2020-07-14 04:12:40 +00:00
|
|
|
Receiver::Receiver(const std::string& path) :
|
|
|
|
dj::ReceiverMonitor(path), _path (path)
|
2020-06-20 01:58:33 +00:00
|
|
|
{
|
2020-06-21 09:33:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Receiver::addDevice(hidpp::DeviceConnectionEvent event)
|
|
|
|
{
|
2020-07-12 07:04:07 +00:00
|
|
|
std::unique_lock<std::mutex> lock(_devices_change);
|
2020-06-21 09:33:33 +00:00
|
|
|
try {
|
2020-07-14 20:21:14 +00:00
|
|
|
// Check if device is ignored before continuing
|
|
|
|
if(global_config->isIgnored(event.pid)) {
|
|
|
|
logPrintf(DEBUG, "%s:%d: Device 0x%04x ignored.",
|
|
|
|
_path.c_str(), event.index, event.pid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-24 04:53:04 +00:00
|
|
|
auto dev = _devices.find(event.index);
|
|
|
|
if(dev != _devices.end()) {
|
|
|
|
if(event.linkEstablished)
|
|
|
|
dev->second->wakeup();
|
|
|
|
else
|
|
|
|
dev->second->sleep();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-21 09:33:33 +00:00
|
|
|
if(!event.linkEstablished)
|
2020-06-24 04:53:04 +00:00
|
|
|
return;
|
2020-06-21 09:33:33 +00:00
|
|
|
|
|
|
|
hidpp::Device hidpp_device(receiver(), event);
|
|
|
|
|
|
|
|
auto version = hidpp_device.version();
|
|
|
|
|
|
|
|
if(std::get<0>(version) < 2) {
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(INFO, "Unsupported HID++ 1.0 device on %s:%d connected.",
|
2020-06-21 09:33:33 +00:00
|
|
|
_path.c_str(), event.index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-20 04:25:04 +00:00
|
|
|
std::shared_ptr<Device> device = std::make_shared<Device>(this,
|
|
|
|
event.index);
|
2020-06-21 09:33:33 +00:00
|
|
|
|
|
|
|
_devices.emplace(event.index, device);
|
|
|
|
|
|
|
|
} catch(hidpp10::Error &e) {
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(ERROR,
|
2020-06-24 04:53:04 +00:00
|
|
|
"Caught HID++ 1.0 error while trying to initialize "
|
|
|
|
"%s:%d: %s", _path.c_str(), event.index, e.what());
|
2020-06-21 09:33:33 +00:00
|
|
|
} catch(hidpp20::Error &e) {
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(ERROR, "Caught HID++ 2.0 error while trying to initialize "
|
2020-06-21 09:33:33 +00:00
|
|
|
"%s:%d: %s", _path.c_str(), event.index, e.what());
|
2020-07-12 08:42:44 +00:00
|
|
|
} catch(TimeoutError &e) {
|
|
|
|
if(!event.fromTimeoutCheck)
|
|
|
|
logPrintf(DEBUG, "%s:%d timed out, waiting for input from device to"
|
|
|
|
" initialize.", _path.c_str(), event.index);
|
|
|
|
waitForDevice(event.index);
|
2020-06-21 09:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Receiver::removeDevice(hidpp::DeviceIndex index)
|
|
|
|
{
|
2020-07-12 07:04:07 +00:00
|
|
|
std::unique_lock<std::mutex> lock(_devices_change);
|
2020-06-21 09:33:33 +00:00
|
|
|
_devices.erase(index);
|
2020-07-20 04:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& Receiver::path() const
|
|
|
|
{
|
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<dj::Receiver> Receiver::rawReceiver()
|
|
|
|
{
|
|
|
|
return receiver();
|
2020-06-20 01:58:33 +00:00
|
|
|
}
|