Monitor all HID++ reports on wireless device 1
Again, many things were done in this commit such as implementing an I/O queue, a mutex_queue, and implementing the hidpp::Report class. I'm expecting commits to be like this until I can get a clean codebase for the backend.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#include <assert.h>
|
||||
#include "Device.h"
|
||||
#include "Report.h"
|
||||
|
||||
@@ -21,10 +22,67 @@ Device::InvalidDevice::Reason Device::InvalidDevice::code() const noexcept
|
||||
}
|
||||
|
||||
/// TODO: Initialize a single RawDevice for each path.
|
||||
Device::Device(std::string path, DeviceIndex index):
|
||||
Device::Device(const std::string& path, DeviceIndex index):
|
||||
raw_device (std::make_shared<raw::RawDevice>(path)), path (path), index (index)
|
||||
{
|
||||
supported_reports = getSupportedReports(raw_device->reportDescriptor());
|
||||
if(!supported_reports)
|
||||
throw InvalidDevice(InvalidDevice::NoHIDPPReport);
|
||||
}
|
||||
|
||||
// Pass all HID++ events with device index to this device.
|
||||
RawEventHandler rawEventHandler;
|
||||
rawEventHandler.condition = [index](std::vector<uint8_t>& report)->bool
|
||||
{
|
||||
return (report[Offset::Type] == Report::Short ||
|
||||
report[Offset::Type] == Report::Long) && (report[Offset::DeviceIndex] == 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::addEventHandler(const std::string& nickname, EventHandler& handler)
|
||||
{
|
||||
auto it = event_handlers.find(nickname);
|
||||
assert(it == event_handlers.end());
|
||||
|
||||
event_handlers.emplace(nickname, handler);
|
||||
}
|
||||
|
||||
void Device::removeEventHandler(const std::string& nickname)
|
||||
{
|
||||
event_handlers.erase(nickname);
|
||||
}
|
||||
|
||||
void Device::handleEvent(Report& report)
|
||||
{
|
||||
for(auto& handler : event_handlers)
|
||||
if(handler.second.condition(report))
|
||||
handler.second.callback(report);
|
||||
}
|
||||
|
||||
Report Device::sendReport(Report& report)
|
||||
{
|
||||
switch(report.type())
|
||||
{
|
||||
case Report::Short:
|
||||
if(!(supported_reports & HIDPP_REPORT_SHORT_SUPPORTED))
|
||||
report.setType(Report::Long);
|
||||
break;
|
||||
case Report::Long:
|
||||
/* Report can be truncated, but that isn't a good idea. */
|
||||
assert(supported_reports & HIDPP_REPORT_LONG_SUPPORTED);
|
||||
}
|
||||
|
||||
auto raw_response = raw_device->sendReport(report.rawReport());
|
||||
return Report(raw_response);
|
||||
}
|
||||
|
||||
void Device::listen()
|
||||
{
|
||||
raw_device->listen();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user