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:
@@ -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();
|
||||
}
|
@@ -11,8 +11,14 @@
|
||||
|
||||
namespace logid {
|
||||
namespace backend {
|
||||
namespace dj
|
||||
{
|
||||
// Need to define here for a constructor
|
||||
class Receiver;
|
||||
}
|
||||
namespace hidpp
|
||||
{
|
||||
struct DeviceConnectionEvent;
|
||||
struct EventHandler
|
||||
{
|
||||
std::function<bool(Report&)> condition;
|
||||
@@ -27,7 +33,8 @@ namespace hidpp
|
||||
enum Reason
|
||||
{
|
||||
NoHIDPPReport,
|
||||
InvalidRawDevice
|
||||
InvalidRawDevice,
|
||||
Asleep
|
||||
};
|
||||
InvalidDevice(Reason reason) : _reason (reason) {}
|
||||
virtual const char *what() const noexcept;
|
||||
@@ -36,18 +43,21 @@ namespace hidpp
|
||||
Reason _reason;
|
||||
};
|
||||
|
||||
Device(const std::string& path, DeviceIndex index);
|
||||
Device(std::shared_ptr<raw::RawDevice> raw_device, DeviceIndex index);
|
||||
explicit Device(const std::string& path, DeviceIndex index);
|
||||
explicit Device(std::shared_ptr<raw::RawDevice> raw_device, DeviceIndex index);
|
||||
explicit Device(std::shared_ptr<dj::Receiver> receiver,
|
||||
hidpp::DeviceConnectionEvent event);
|
||||
~Device();
|
||||
|
||||
std::string devicePath() const { return path; }
|
||||
std::string devicePath() const { return _path; }
|
||||
DeviceIndex deviceIndex() const { return _index; }
|
||||
std::tuple<uint8_t, uint8_t> version() const { return _version; }
|
||||
|
||||
void listen(); // Runs asynchronously
|
||||
void stopListening();
|
||||
|
||||
void addEventHandler(const std::string& nickname, const std::shared_ptr<EventHandler>& handler);
|
||||
void addEventHandler(const std::string& nickname,
|
||||
const std::shared_ptr<EventHandler>& handler);
|
||||
void removeEventHandler(const std::string& nickname);
|
||||
|
||||
Report sendReport(Report& report);
|
||||
@@ -56,12 +66,15 @@ namespace hidpp
|
||||
private:
|
||||
void _init();
|
||||
|
||||
std::shared_ptr<raw::RawDevice> raw_device;
|
||||
std::string path;
|
||||
std::shared_ptr<raw::RawDevice> _raw_device;
|
||||
std::shared_ptr<dj::Receiver> _receiver;
|
||||
std::string _path;
|
||||
DeviceIndex _index;
|
||||
uint8_t supported_reports;
|
||||
|
||||
std::tuple<uint8_t, uint8_t> _version;
|
||||
uint16_t _pid;
|
||||
std::string _name;
|
||||
|
||||
std::map<std::string, std::shared_ptr<EventHandler>> event_handlers;
|
||||
};
|
||||
|
@@ -180,6 +180,16 @@ void Report::setType(Report::Type type)
|
||||
_data[Offset::Type] = type;
|
||||
}
|
||||
|
||||
hidpp::DeviceIndex Report::deviceIndex()
|
||||
{
|
||||
return static_cast<hidpp::DeviceIndex>(_data[Offset::DeviceIndex]);
|
||||
}
|
||||
|
||||
void Report::setDeviceIndex(hidpp::DeviceIndex index)
|
||||
{
|
||||
_data[Offset::DeviceIndex] = index;
|
||||
}
|
||||
|
||||
uint8_t Report::feature() const
|
||||
{
|
||||
return _data[Offset::Feature];
|
||||
|
@@ -60,6 +60,9 @@ namespace logid::backend::hidpp
|
||||
Report::Type type() const;
|
||||
void setType(Report::Type type);
|
||||
|
||||
logid::backend::hidpp::DeviceIndex deviceIndex();
|
||||
void setDeviceIndex(hidpp::DeviceIndex index);
|
||||
|
||||
uint8_t feature() const;
|
||||
void setFeature(uint8_t feature);
|
||||
|
||||
@@ -96,11 +99,6 @@ namespace logid::backend::hidpp
|
||||
|
||||
bool isError20(hidpp20_error* error);
|
||||
|
||||
logid::backend::hidpp::DeviceIndex deviceIndex()
|
||||
{
|
||||
return static_cast<DeviceIndex>(_data[Offset::DeviceIndex]);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> rawReport () const { return _data; }
|
||||
private:
|
||||
static constexpr std::size_t HeaderLength = 4;
|
||||
|
Reference in New Issue
Block a user