From c382ba1c0b2d61f15d36e0c9967ff74fcf051ac9 Mon Sep 17 00:00:00 2001 From: pixl Date: Thu, 2 Jul 2020 16:04:18 -0400 Subject: [PATCH] Add getFeature function to Device --- src/logid/Device.cpp | 6 +++--- src/logid/Device.h | 24 +++++++++++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/logid/Device.cpp b/src/logid/Device.cpp index dfde69f..9e9212c 100644 --- a/src/logid/Device.cpp +++ b/src/logid/Device.cpp @@ -44,11 +44,11 @@ void Device::_init() logPrintf(INFO, "Device found: %s on %s:%d", name().c_str(), hidpp20().devicePath().c_str(), _index); - _addFeature(); - _addFeature(); + _addFeature("dpi"); + _addFeature("smartshift"); for(auto& feature: _features) - feature->configure(); + feature.second->configure(); } std::string Device::name() diff --git a/src/logid/Device.h b/src/logid/Device.h index 1206097..ad047f8 100644 --- a/src/logid/Device.h +++ b/src/logid/Device.h @@ -24,6 +24,7 @@ #include "backend/hidpp20/Feature.h" #include "features/DeviceFeature.h" #include "Configuration.h" +#include "util/log.h" namespace logid { @@ -60,15 +61,31 @@ namespace logid void wakeup(); void sleep(); + + template + std::shared_ptr getFeature(std::string name) { + auto it = _features.find(name); + if(it == _features.end()) + return nullptr; + try { + return std::dynamic_pointer_cast> + (it->second); + } catch(std::bad_cast& e) { + logPrintf(ERROR, "bad_cast while getting device feature %s: " + "%s", name.c_str(), e.what()); + return nullptr; + } + } + private: void _init(); /* Adds a feature without calling an error if unsupported */ template - void _addFeature() + void _addFeature(std::string name) { try { - _features.push_back(std::make_shared(this)); + _features.emplace(name, std::make_shared(this)); } catch (backend::hidpp20::UnsupportedFeature& e) { } } @@ -76,7 +93,8 @@ namespace logid backend::hidpp20::Device _hidpp20; std::string _path; backend::hidpp::DeviceIndex _index; - std::vector> _features; + std::map> + _features; DeviceConfig _config; }; }