Add reset mechanism to logid::Device

This commit is contained in:
pixl 2020-07-05 16:16:38 -04:00
parent 5bf5dc75b5
commit 71b0ddd279
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
2 changed files with 32 additions and 0 deletions

View File

@ -21,6 +21,7 @@
#include "Device.h"
#include "features/SmartShift.h"
#include "features/RemapButton.h"
#include "backend/hidpp20/features/Reset.h"
using namespace logid;
using namespace logid::backend;
@ -49,6 +50,9 @@ void Device::_init()
_addFeature<features::SmartShift>("smartshift");
_addFeature<features::RemapButton>("remapbutton");
_makeResetMechanism();
reset();
for(auto& feature: _features) {
feature.second->configure();
feature.second->listen();
@ -76,10 +80,22 @@ void Device::wakeup()
{
logPrintf(INFO, "%s:%d woke up.", _path.c_str(), _index);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
reset();
for(auto& feature: _features)
feature.second->configure();
}
void Device::reset()
{
if(_reset_mechanism)
(*_reset_mechanism)();
else
logPrintf(DEBUG, "%s:%d tried to reset, but no reset mechanism was "
"available.", _path.c_str(), _index);
}
DeviceConfig& Device::config()
{
return _config;
@ -90,6 +106,17 @@ hidpp20::Device& Device::hidpp20()
return _hidpp20;
}
void Device::_makeResetMechanism()
{
try {
hidpp20::Reset reset(&_hidpp20);
_reset_mechanism = std::make_unique<std::function<void()>>(
[dev=&this->_hidpp20]{ hidpp20::Reset(dev).reset(); });
} catch(hidpp20::UnsupportedFeature& e) {
// Reset unsupported, ignore.
}
}
DeviceConfig::DeviceConfig(const std::shared_ptr<Configuration>& config, Device*
device) : _device (device), _config (config)
{

View File

@ -62,6 +62,8 @@ namespace logid
void wakeup();
void sleep();
void reset();
template<typename T>
std::shared_ptr<T> getFeature(std::string name) {
auto it = _features.find(name);
@ -96,6 +98,9 @@ namespace logid
std::map<std::string, std::shared_ptr<features::DeviceFeature>>
_features;
DeviceConfig _config;
void _makeResetMechanism();
std::unique_ptr<std::function<void()>> _reset_mechanism;
};
}