Create features::UnsupportedFeature exception
Creates an abstraction layer for hidpp10/hidpp20/etc. UnsupportedFeature exceptions.
This commit is contained in:
		@@ -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<T>(this));
 | 
			
		||||
            } catch (backend::hidpp20::UnsupportedFeature& e) {
 | 
			
		||||
            } catch (features::UnsupportedFeature& e) {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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<hidpp20::AdjustableDPI>
 | 
			
		||||
                (&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
 | 
			
		||||
 
 | 
			
		||||
@@ -45,7 +45,7 @@ namespace features
 | 
			
		||||
        };
 | 
			
		||||
    private:
 | 
			
		||||
        Config _config;
 | 
			
		||||
        backend::hidpp20::AdjustableDPI _adjustable_dpi;
 | 
			
		||||
        std::shared_ptr<backend::hidpp20::AdjustableDPI> _adjustable_dpi;
 | 
			
		||||
        std::vector<backend::hidpp20::AdjustableDPI::SensorDPIList> _dpi_lists;
 | 
			
		||||
    };
 | 
			
		||||
 }}
 | 
			
		||||
 
 | 
			
		||||
@@ -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:
 | 
			
		||||
 
 | 
			
		||||
@@ -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<hidpp20::HiresScroll>(&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)
 | 
			
		||||
 
 | 
			
		||||
@@ -45,7 +45,7 @@ namespace features
 | 
			
		||||
            uint8_t _mask;
 | 
			
		||||
        };
 | 
			
		||||
    private:
 | 
			
		||||
        backend::hidpp20::HiresScroll _hires_scroll;
 | 
			
		||||
        std::shared_ptr<backend::hidpp20::HiresScroll> _hires_scroll;
 | 
			
		||||
        Config _config;
 | 
			
		||||
    };
 | 
			
		||||
}}
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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<hidpp20::SmartShift>(&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()
 | 
			
		||||
 
 | 
			
		||||
@@ -44,7 +44,7 @@ namespace features
 | 
			
		||||
        };
 | 
			
		||||
    private:
 | 
			
		||||
        Config _config;
 | 
			
		||||
        backend::hidpp20::SmartShift _smartshift;
 | 
			
		||||
        std::shared_ptr<backend::hidpp20::SmartShift> _smartshift;
 | 
			
		||||
    };
 | 
			
		||||
}}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user