From 82ff2bdf380127b95a01c1d6c53a5b7fe3f534c3 Mon Sep 17 00:00:00 2001 From: pixl Date: Sat, 18 Jul 2020 21:15:39 -0400 Subject: [PATCH] Create features::UnsupportedFeature exception Creates an abstraction layer for hidpp10/hidpp20/etc. UnsupportedFeature exceptions. --- src/logid/Device.h | 3 +-- src/logid/features/DPI.cpp | 23 ++++++++++++++--------- src/logid/features/DPI.h | 2 +- src/logid/features/DeviceFeature.h | 10 ++++++++++ src/logid/features/HiresScroll.cpp | 16 ++++++++++------ src/logid/features/HiresScroll.h | 2 +- src/logid/features/RemapButton.cpp | 10 ++++++++-- src/logid/features/SmartShift.cpp | 13 +++++++++---- src/logid/features/SmartShift.h | 2 +- 9 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/logid/Device.h b/src/logid/Device.h index 2a0cd42..8d3238f 100644 --- a/src/logid/Device.h +++ b/src/logid/Device.h @@ -21,7 +21,6 @@ #include "backend/hidpp/defs.h" #include "backend/hidpp20/Device.h" -#include "backend/hidpp20/Feature.h" #include "features/DeviceFeature.h" #include "Configuration.h" #include "util/log.h" @@ -87,7 +86,7 @@ namespace logid { try { _features.emplace(name, std::make_shared(this)); - } catch (backend::hidpp20::UnsupportedFeature& e) { + } catch (features::UnsupportedFeature& e) { } } diff --git a/src/logid/features/DPI.cpp b/src/logid/features/DPI.cpp index 435cdb8..ea6c762 100644 --- a/src/logid/features/DPI.cpp +++ b/src/logid/features/DPI.cpp @@ -58,18 +58,23 @@ uint16_t getClosestDPI(hidpp20::AdjustableDPI::SensorDPIList& dpi_list, } } -DPI::DPI(Device* device) : DeviceFeature(device), _config (device), - _adjustable_dpi (&device->hidpp20()) +DPI::DPI(Device* device) : DeviceFeature(device), _config (device) { + try { + _adjustable_dpi = std::make_shared + (&device->hidpp20()); + } catch (hidpp20::UnsupportedFeature& e) { + throw UnsupportedFeature(); + } } 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++) { hidpp20::AdjustableDPI::SensorDPIList dpi_list; if(_dpi_lists.size() <= i) { - dpi_list = _adjustable_dpi.getSensorDPIList(i); + dpi_list = _adjustable_dpi->getSensorDPIList(i); _dpi_lists.push_back(dpi_list); } else { dpi_list = _dpi_lists[i]; @@ -77,7 +82,7 @@ void DPI::configure() if(i < sensors) { auto dpi = _config.getDPI(i); if(dpi) { - _adjustable_dpi.setSensorDPI(i, getClosestDPI(dpi_list, + _adjustable_dpi->setSensorDPI(i, getClosestDPI(dpi_list, dpi)); } } @@ -90,20 +95,20 @@ void DPI::listen() 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) { hidpp20::AdjustableDPI::SensorDPIList dpi_list; 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++) { - _dpi_lists.push_back(_adjustable_dpi.getSensorDPIList(i)); + _dpi_lists.push_back(_adjustable_dpi->getSensorDPIList(i)); } } 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 diff --git a/src/logid/features/DPI.h b/src/logid/features/DPI.h index 8bc2468..f2f6c33 100644 --- a/src/logid/features/DPI.h +++ b/src/logid/features/DPI.h @@ -45,7 +45,7 @@ namespace features }; private: Config _config; - backend::hidpp20::AdjustableDPI _adjustable_dpi; + std::shared_ptr _adjustable_dpi; std::vector _dpi_lists; }; }} diff --git a/src/logid/features/DeviceFeature.h b/src/logid/features/DeviceFeature.h index 5bccee1..e4bbfc1 100644 --- a/src/logid/features/DeviceFeature.h +++ b/src/logid/features/DeviceFeature.h @@ -25,6 +25,16 @@ namespace logid { class Device; namespace features { + class UnsupportedFeature : public std::exception + { + public: + UnsupportedFeature() = default; + virtual const char* what() const noexcept + { + return "Unsupported feature"; + } + }; + class DeviceFeature { public: diff --git a/src/logid/features/HiresScroll.cpp b/src/logid/features/HiresScroll.cpp index e21a8b0..7ed285f 100644 --- a/src/logid/features/HiresScroll.cpp +++ b/src/logid/features/HiresScroll.cpp @@ -21,17 +21,21 @@ using namespace logid::features; using namespace logid::backend; -HiresScroll::HiresScroll(Device *dev) : DeviceFeature(dev), - _hires_scroll(&dev->hidpp20()), _config(dev) +HiresScroll::HiresScroll(Device *dev) : DeviceFeature(dev), _config(dev) { + try { + _hires_scroll = std::make_shared(&dev->hidpp20()); + } catch(hidpp20::UnsupportedFeature& e) { + throw UnsupportedFeature(); + } } void HiresScroll::configure() { - auto mode = _hires_scroll.getMode(); + auto mode = _hires_scroll->getMode(); mode &= ~_config.getMask(); mode |= (_config.getMode() & _config.getMask()); - _hires_scroll.setMode(mode); + _hires_scroll->setMode(mode); } void HiresScroll::listen() @@ -41,12 +45,12 @@ void HiresScroll::listen() uint8_t HiresScroll::getMode() { - return _hires_scroll.getMode(); + return _hires_scroll->getMode(); } void HiresScroll::setMode(uint8_t mode) { - _hires_scroll.setMode(mode); + _hires_scroll->setMode(mode); } HiresScroll::Config::Config(Device *dev) : DeviceFeature::Config(dev) diff --git a/src/logid/features/HiresScroll.h b/src/logid/features/HiresScroll.h index 7e81c97..f413d56 100644 --- a/src/logid/features/HiresScroll.h +++ b/src/logid/features/HiresScroll.h @@ -45,7 +45,7 @@ namespace features uint8_t _mask; }; private: - backend::hidpp20::HiresScroll _hires_scroll; + std::shared_ptr _hires_scroll; Config _config; }; }} diff --git a/src/logid/features/RemapButton.cpp b/src/logid/features/RemapButton.cpp index 9f8f813..a72fb4f 100644 --- a/src/logid/features/RemapButton.cpp +++ b/src/logid/features/RemapButton.cpp @@ -29,9 +29,15 @@ using namespace logid::actions; #define EVENTHANDLER_NAME "REMAP_BUTTON" -RemapButton::RemapButton(Device *dev): DeviceFeature(dev), _config (dev), - _reprog_controls (hidpp20::ReprogControls::autoVersion(&dev->hidpp20())) +RemapButton::RemapButton(Device *dev): DeviceFeature(dev), _config (dev) { + try { + _reprog_controls = hidpp20::ReprogControls::autoVersion( + &dev->hidpp20()); + } catch(hidpp20::UnsupportedFeature& e) { + throw UnsupportedFeature(); + } + _reprog_controls->initCidMap(); if(global_loglevel <= DEBUG) { diff --git a/src/logid/features/SmartShift.cpp b/src/logid/features/SmartShift.cpp index fd357f0..5f219e7 100644 --- a/src/logid/features/SmartShift.cpp +++ b/src/logid/features/SmartShift.cpp @@ -23,13 +23,18 @@ using namespace logid::features; using namespace logid::backend; SmartShift::SmartShift(Device* device) : DeviceFeature(device), _config - (device), _smartshift(&device->hidpp20()) + (device) { + try { + _smartshift = std::make_shared(&device->hidpp20()); + } catch (hidpp20::UnsupportedFeature& e) { + throw UnsupportedFeature(); + } } void SmartShift::configure() { - _smartshift.setStatus(_config.getSettings()); + _smartshift->setStatus(_config.getSettings()); } void SmartShift::listen() @@ -38,13 +43,13 @@ void SmartShift::listen() hidpp20::SmartShift::SmartshiftStatus SmartShift::getStatus() { - return _smartshift.getStatus(); + return _smartshift->getStatus(); } void SmartShift::setStatus(backend::hidpp20::SmartShift::SmartshiftStatus status) { - _smartshift.setStatus(status); + _smartshift->setStatus(status); } SmartShift::Config::Config(Device *dev) : DeviceFeature::Config(dev), _status() diff --git a/src/logid/features/SmartShift.h b/src/logid/features/SmartShift.h index 06bb280..ed7ac35 100644 --- a/src/logid/features/SmartShift.h +++ b/src/logid/features/SmartShift.h @@ -44,7 +44,7 @@ namespace features }; private: Config _config; - backend::hidpp20::SmartShift _smartshift; + std::shared_ptr _smartshift; }; }}