Create features::UnsupportedFeature exception

Creates an abstraction layer for hidpp10/hidpp20/etc. UnsupportedFeature
exceptions.
This commit is contained in:
pixl 2020-07-18 21:15:39 -04:00
parent 0928b39e03
commit 82ff2bdf38
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
9 changed files with 55 additions and 26 deletions

View File

@ -21,7 +21,6 @@
#include "backend/hidpp/defs.h" #include "backend/hidpp/defs.h"
#include "backend/hidpp20/Device.h" #include "backend/hidpp20/Device.h"
#include "backend/hidpp20/Feature.h"
#include "features/DeviceFeature.h" #include "features/DeviceFeature.h"
#include "Configuration.h" #include "Configuration.h"
#include "util/log.h" #include "util/log.h"
@ -87,7 +86,7 @@ namespace logid
{ {
try { try {
_features.emplace(name, std::make_shared<T>(this)); _features.emplace(name, std::make_shared<T>(this));
} catch (backend::hidpp20::UnsupportedFeature& e) { } catch (features::UnsupportedFeature& e) {
} }
} }

View File

@ -58,18 +58,23 @@ uint16_t getClosestDPI(hidpp20::AdjustableDPI::SensorDPIList& dpi_list,
} }
} }
DPI::DPI(Device* device) : DeviceFeature(device), _config (device), DPI::DPI(Device* device) : DeviceFeature(device), _config (device)
_adjustable_dpi (&device->hidpp20())
{ {
try {
_adjustable_dpi = std::make_shared<hidpp20::AdjustableDPI>
(&device->hidpp20());
} catch (hidpp20::UnsupportedFeature& e) {
throw UnsupportedFeature();
}
} }
void DPI::configure() void DPI::configure()
{ {
const uint8_t sensors = _adjustable_dpi.getSensorCount(); const uint8_t sensors = _adjustable_dpi->getSensorCount();
for(uint8_t i = 0; i < _config.getSensorCount(); i++) { for(uint8_t i = 0; i < _config.getSensorCount(); i++) {
hidpp20::AdjustableDPI::SensorDPIList dpi_list; hidpp20::AdjustableDPI::SensorDPIList dpi_list;
if(_dpi_lists.size() <= i) { if(_dpi_lists.size() <= i) {
dpi_list = _adjustable_dpi.getSensorDPIList(i); dpi_list = _adjustable_dpi->getSensorDPIList(i);
_dpi_lists.push_back(dpi_list); _dpi_lists.push_back(dpi_list);
} else { } else {
dpi_list = _dpi_lists[i]; dpi_list = _dpi_lists[i];
@ -77,7 +82,7 @@ void DPI::configure()
if(i < sensors) { if(i < sensors) {
auto dpi = _config.getDPI(i); auto dpi = _config.getDPI(i);
if(dpi) { if(dpi) {
_adjustable_dpi.setSensorDPI(i, getClosestDPI(dpi_list, _adjustable_dpi->setSensorDPI(i, getClosestDPI(dpi_list,
dpi)); dpi));
} }
} }
@ -90,20 +95,20 @@ void DPI::listen()
uint16_t DPI::getDPI(uint8_t sensor) uint16_t DPI::getDPI(uint8_t sensor)
{ {
return _adjustable_dpi.getSensorDPI(sensor); return _adjustable_dpi->getSensorDPI(sensor);
} }
void DPI::setDPI(uint16_t dpi, uint8_t sensor) void DPI::setDPI(uint16_t dpi, uint8_t sensor)
{ {
hidpp20::AdjustableDPI::SensorDPIList dpi_list; hidpp20::AdjustableDPI::SensorDPIList dpi_list;
if(_dpi_lists.size() <= sensor) { if(_dpi_lists.size() <= sensor) {
dpi_list = _adjustable_dpi.getSensorDPIList(sensor); dpi_list = _adjustable_dpi->getSensorDPIList(sensor);
for(std::size_t i = _dpi_lists.size()-1; i <= sensor; i++) { for(std::size_t i = _dpi_lists.size()-1; i <= sensor; i++) {
_dpi_lists.push_back(_adjustable_dpi.getSensorDPIList(i)); _dpi_lists.push_back(_adjustable_dpi->getSensorDPIList(i));
} }
} }
dpi_list = _dpi_lists[sensor]; dpi_list = _dpi_lists[sensor];
_adjustable_dpi.setSensorDPI(sensor, getClosestDPI(dpi_list, dpi)); _adjustable_dpi->setSensorDPI(sensor, getClosestDPI(dpi_list, dpi));
} }
/* Some devices have multiple sensors, but an older config format /* Some devices have multiple sensors, but an older config format

View File

@ -45,7 +45,7 @@ namespace features
}; };
private: private:
Config _config; Config _config;
backend::hidpp20::AdjustableDPI _adjustable_dpi; std::shared_ptr<backend::hidpp20::AdjustableDPI> _adjustable_dpi;
std::vector<backend::hidpp20::AdjustableDPI::SensorDPIList> _dpi_lists; std::vector<backend::hidpp20::AdjustableDPI::SensorDPIList> _dpi_lists;
}; };
}} }}

View File

@ -25,6 +25,16 @@ namespace logid {
class Device; class Device;
namespace features namespace features
{ {
class UnsupportedFeature : public std::exception
{
public:
UnsupportedFeature() = default;
virtual const char* what() const noexcept
{
return "Unsupported feature";
}
};
class DeviceFeature class DeviceFeature
{ {
public: public:

View File

@ -21,17 +21,21 @@
using namespace logid::features; using namespace logid::features;
using namespace logid::backend; using namespace logid::backend;
HiresScroll::HiresScroll(Device *dev) : DeviceFeature(dev), HiresScroll::HiresScroll(Device *dev) : DeviceFeature(dev), _config(dev)
_hires_scroll(&dev->hidpp20()), _config(dev)
{ {
try {
_hires_scroll = std::make_shared<hidpp20::HiresScroll>(&dev->hidpp20());
} catch(hidpp20::UnsupportedFeature& e) {
throw UnsupportedFeature();
}
} }
void HiresScroll::configure() void HiresScroll::configure()
{ {
auto mode = _hires_scroll.getMode(); auto mode = _hires_scroll->getMode();
mode &= ~_config.getMask(); mode &= ~_config.getMask();
mode |= (_config.getMode() & _config.getMask()); mode |= (_config.getMode() & _config.getMask());
_hires_scroll.setMode(mode); _hires_scroll->setMode(mode);
} }
void HiresScroll::listen() void HiresScroll::listen()
@ -41,12 +45,12 @@ void HiresScroll::listen()
uint8_t HiresScroll::getMode() uint8_t HiresScroll::getMode()
{ {
return _hires_scroll.getMode(); return _hires_scroll->getMode();
} }
void HiresScroll::setMode(uint8_t mode) void HiresScroll::setMode(uint8_t mode)
{ {
_hires_scroll.setMode(mode); _hires_scroll->setMode(mode);
} }
HiresScroll::Config::Config(Device *dev) : DeviceFeature::Config(dev) HiresScroll::Config::Config(Device *dev) : DeviceFeature::Config(dev)

View File

@ -45,7 +45,7 @@ namespace features
uint8_t _mask; uint8_t _mask;
}; };
private: private:
backend::hidpp20::HiresScroll _hires_scroll; std::shared_ptr<backend::hidpp20::HiresScroll> _hires_scroll;
Config _config; Config _config;
}; };
}} }}

View File

@ -29,9 +29,15 @@ using namespace logid::actions;
#define EVENTHANDLER_NAME "REMAP_BUTTON" #define EVENTHANDLER_NAME "REMAP_BUTTON"
RemapButton::RemapButton(Device *dev): DeviceFeature(dev), _config (dev), RemapButton::RemapButton(Device *dev): DeviceFeature(dev), _config (dev)
_reprog_controls (hidpp20::ReprogControls::autoVersion(&dev->hidpp20()))
{ {
try {
_reprog_controls = hidpp20::ReprogControls::autoVersion(
&dev->hidpp20());
} catch(hidpp20::UnsupportedFeature& e) {
throw UnsupportedFeature();
}
_reprog_controls->initCidMap(); _reprog_controls->initCidMap();
if(global_loglevel <= DEBUG) { if(global_loglevel <= DEBUG) {

View File

@ -23,13 +23,18 @@ using namespace logid::features;
using namespace logid::backend; using namespace logid::backend;
SmartShift::SmartShift(Device* device) : DeviceFeature(device), _config SmartShift::SmartShift(Device* device) : DeviceFeature(device), _config
(device), _smartshift(&device->hidpp20()) (device)
{ {
try {
_smartshift = std::make_shared<hidpp20::SmartShift>(&device->hidpp20());
} catch (hidpp20::UnsupportedFeature& e) {
throw UnsupportedFeature();
}
} }
void SmartShift::configure() void SmartShift::configure()
{ {
_smartshift.setStatus(_config.getSettings()); _smartshift->setStatus(_config.getSettings());
} }
void SmartShift::listen() void SmartShift::listen()
@ -38,13 +43,13 @@ void SmartShift::listen()
hidpp20::SmartShift::SmartshiftStatus SmartShift::getStatus() hidpp20::SmartShift::SmartshiftStatus SmartShift::getStatus()
{ {
return _smartshift.getStatus(); return _smartshift->getStatus();
} }
void SmartShift::setStatus(backend::hidpp20::SmartShift::SmartshiftStatus void SmartShift::setStatus(backend::hidpp20::SmartShift::SmartshiftStatus
status) status)
{ {
_smartshift.setStatus(status); _smartshift->setStatus(status);
} }
SmartShift::Config::Config(Device *dev) : DeviceFeature::Config(dev), _status() SmartShift::Config::Config(Device *dev) : DeviceFeature::Config(dev), _status()

View File

@ -44,7 +44,7 @@ namespace features
}; };
private: private:
Config _config; Config _config;
backend::hidpp20::SmartShift _smartshift; std::shared_ptr<backend::hidpp20::SmartShift> _smartshift;
}; };
}} }}