Implement receiver HID++ connect/disconnect events

Many changes were made here but that was the biggest one.

There's currently a bug where std::system_error: Broken pipe is thrown
after launching the daemon with a receiver connector.

A workaround for this bug is to simply shake the mouse while starting
the daemon. I will investigate this soon.
This commit is contained in:
pixl
2020-06-21 05:33:33 -04:00
parent b05e525bbc
commit e40da5f0c0
24 changed files with 689 additions and 181 deletions
+74 -36
View File
@@ -1,9 +1,12 @@
#include <assert.h>
#include <cassert>
#include <utility>
#include "Device.h"
#include "Report.h"
#include "../hidpp20/features/Root.h"
#include "../hidpp20/features/DeviceName.h"
#include "../hidpp20/Error.h"
#include "../hidpp10/Error.h"
#include "../dj/Receiver.h"
using namespace logid::backend;
using namespace logid::backend::hidpp;
@@ -16,6 +19,10 @@ const char* Device::InvalidDevice::what() const noexcept
return "Invalid HID++ device";
case InvalidRawDevice:
return "Invalid raw device";
case Asleep:
return "Device asleep";
default:
return "Invalid device";
}
}
@@ -26,36 +33,41 @@ Device::InvalidDevice::Reason Device::InvalidDevice::code() const noexcept
/// TODO: Initialize a single RawDevice for each path.
Device::Device(const std::string& path, DeviceIndex index):
raw_device (std::make_shared<raw::RawDevice>(path)), path (path),
_index (index)
_raw_device (std::make_shared<raw::RawDevice>(path)), _receiver (nullptr),
_path (path), _index (index)
{
_init();
}
Device::Device(std::shared_ptr<raw::RawDevice> raw_device, DeviceIndex index) :
raw_device (raw_device), _index (index)
_raw_device (std::move(raw_device)), _receiver (nullptr),
_path (_raw_device->hidrawPath()), _index (index)
{
_init();
}
Device::Device(std::shared_ptr<dj::Receiver> receiver,
hidpp::DeviceConnectionEvent event) :
_raw_device (receiver->rawDevice()), _index (event.index)
{
// Device will throw an error soon, just do it now
if(!event.linkEstablished)
throw InvalidDevice(InvalidDevice::Asleep);
_pid = event.pid;
_init();
}
void Device::_init()
{
supported_reports = getSupportedReports(raw_device->reportDescriptor());
supported_reports = getSupportedReports(_raw_device->reportDescriptor());
if(!supported_reports)
throw InvalidDevice(InvalidDevice::NoHIDPPReport);
try
{
Report versionRequest(Report::Type::Short, _index,
hidpp20::FeatureID::ROOT,hidpp20::Root::Ping,
LOGID_HIDPP_SOFTWARE_ID);
auto versionResponse = sendReport(versionRequest);
auto versionResponse_params = versionResponse.paramBegin();
_version = std::make_tuple(versionResponse_params[0], versionResponse_params[1]);
}
catch(hidpp10::Error &e)
{
try {
hidpp20::EssentialRoot root(this);
_version = root.getVersion();
} catch(hidpp10::Error &e) {
// Valid HID++ 1.0 devices should send an InvalidSubID error
if(e.code() != hidpp10::Error::InvalidSubID)
throw;
@@ -64,29 +76,30 @@ void Device::_init()
_version = std::make_tuple(1, 0);
}
// Pass all HID++ events with device index to this device.
RawEventHandler rawEventHandler;
rawEventHandler.condition = [this](std::vector<uint8_t>& report)->bool
{
return (report[Offset::Type] == Report::Type::Short ||
report[Offset::Type] == Report::Type::Long) &&
(report[Offset::DeviceIndex] == this->_index);
};
rawEventHandler.callback = [this](std::vector<uint8_t>& report)->void
{
Report _report(report);
this->handleEvent(_report);
};
raw_device->addEventHandler("DEV_" + std::to_string(_index), rawEventHandler);
if(!_receiver) {
_pid = _raw_device->productId();
if(std::get<0>(_version) >= 2) {
try {
hidpp20::EssentialDeviceName deviceName(this);
_name = deviceName.getName();
} catch(hidpp20::UnsupportedFeature &e) {
_name = _raw_device->name();
}
} else {
_name = _raw_device->name();
}
} else {
_name = _receiver->getDeviceName(_index);
}
}
Device::~Device()
{
raw_device->removeEventHandler("DEV_" + std::to_string(_index));
_raw_device->removeEventHandler("DEV_" + std::to_string(_index));
}
void Device::addEventHandler(const std::string& nickname, const std::shared_ptr<EventHandler>& handler)
void Device::addEventHandler(const std::string& nickname,
const std::shared_ptr<EventHandler>& handler)
{
auto it = event_handlers.find(nickname);
assert(it == event_handlers.end());
@@ -119,7 +132,7 @@ Report Device::sendReport(Report& report)
assert(supported_reports & HIDPP_REPORT_LONG_SUPPORTED);
}
auto raw_response = raw_device->sendReport(report.rawReport());
auto raw_response = _raw_device->sendReport(report.rawReport());
Report response(raw_response);
@@ -136,5 +149,30 @@ Report Device::sendReport(Report& report)
void Device::listen()
{
raw_device->listen();
if(!_raw_device->isListening())
std::thread{[=]() { _raw_device->listen(); }}.detach();
// Pass all HID++ events with device index to this device.
std::shared_ptr<RawEventHandler> rawEventHandler;
rawEventHandler->condition = [this](std::vector<uint8_t>& report)->bool
{
return (report[Offset::Type] == Report::Type::Short ||
report[Offset::Type] == Report::Type::Long) &&
(report[Offset::DeviceIndex] == this->_index);
};
rawEventHandler->callback = [this](std::vector<uint8_t>& report)->void
{
Report _report(report);
this->handleEvent(_report);
};
_raw_device->addEventHandler("DEV_" + std::to_string(_index), rawEventHandler);
}
void Device::stopListening()
{
_raw_device->removeEventHandler("DEV_" + std::to_string(_index));
if(!_raw_device->eventHandlers().empty())
_raw_device->stopListener();
}