logiops/src/logid/backend/hidpp/Device.h
pixl c21a923ab2
Print version number of device 1 on each raw dev.
Only works on HID++ >=2.0 so far. Also solves a race condition where
the wrong response can be sent to a request.
2020-07-12 16:13:53 -04:00

66 lines
1.7 KiB
C++

#ifndef LOGID_HIDPP_DEVICE_H
#define LOGID_HIDPP_DEVICE_H
#include <string>
#include <memory>
#include <functional>
#include <map>
#include "../raw/RawDevice.h"
#include "Report.h"
#include "defs.h"
namespace logid {
namespace backend {
namespace hidpp
{
struct EventHandler
{
std::function<bool(Report&)> condition;
std::function<void(Report&)> callback;
};
class Device
{
public:
class InvalidDevice : std::exception
{
public:
enum Reason
{
NoHIDPPReport,
InvalidRawDevice
};
InvalidDevice(Reason reason) : _reason (reason) {}
virtual const char *what() const noexcept;
virtual Reason code() const noexcept;
private:
Reason _reason;
};
Device(const std::string& path, DeviceIndex index);
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 removeEventHandler(const std::string& nickname);
Report sendReport(Report& report);
void handleEvent(Report& report);
private:
std::shared_ptr<raw::RawDevice> raw_device;
std::string path;
DeviceIndex index;
uint8_t supported_reports;
std::tuple<uint8_t, uint8_t> _version;
std::map<std::string, std::shared_ptr<EventHandler>> event_handlers;
};
} } }
#endif //LOGID_HIDPP_DEVICE_H