Add DPI feature

This commit is contained in:
pixl 2020-07-02 03:34:14 -04:00
parent f6b93b94af
commit 07b8fc1af4
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
7 changed files with 199 additions and 14 deletions

View File

@ -15,6 +15,7 @@ add_executable(logid
Device.cpp Device.cpp
Receiver.cpp Receiver.cpp
Configuration.cpp Configuration.cpp
features/DPI.cpp
backend/Error.cpp backend/Error.cpp
backend/raw/DeviceMonitor.cpp backend/raw/DeviceMonitor.cpp
backend/raw/RawDevice.cpp backend/raw/RawDevice.cpp

View File

@ -44,14 +44,12 @@ Configuration::Configuration(const std::string& config_file)
Setting* devices; Setting* devices;
try { devices = &root["devices"]; } try { devices = &root["devices"]; }
catch(const SettingNotFoundException &e) catch(const SettingNotFoundException &e) {
{
logPrintf(WARN, "No devices listed in config file."); logPrintf(WARN, "No devices listed in config file.");
return; return;
} }
for(int i = 0; i < devices->getLength(); i++) for(int i = 0; i < devices->getLength(); i++) {
{
const Setting &device = (*devices)[i]; const Setting &device = (*devices)[i];
std::string name; std::string name;
try { try {

View File

@ -17,6 +17,7 @@
*/ */
#include "util/log.h" #include "util/log.h"
#include "features/DPI.h"
#include "Device.h" #include "Device.h"
using namespace logid; using namespace logid;
@ -26,7 +27,7 @@ Device::Device(std::string path, backend::hidpp::DeviceIndex index) :
_hidpp20 (path, index), _path (std::move(path)), _index (index), _hidpp20 (path, index), _path (std::move(path)), _index (index),
_config (global_config, this) _config (global_config, this)
{ {
///TODO: Initialize features _init();
} }
Device::Device(const std::shared_ptr<backend::raw::RawDevice>& raw_device, Device::Device(const std::shared_ptr<backend::raw::RawDevice>& raw_device,
@ -34,7 +35,19 @@ Device::Device(const std::shared_ptr<backend::raw::RawDevice>& raw_device,
(raw_device->hidrawPath()), _index (index), (raw_device->hidrawPath()), _index (index),
_config (global_config, this) _config (global_config, this)
{ {
///TODO: Initialize features _init();
}
void Device::_init()
{
///TODO: Surely there's a better way of doing this
try {
_features.push_back(std::make_shared<features::DPI>(this));
} catch (backend::hidpp20::UnsupportedFeature& e) {
}
for(auto& feature: _features)
feature->configure();
} }
std::string Device::name() std::string Device::name()
@ -62,7 +75,12 @@ DeviceConfig& Device::config()
return _config; return _config;
} }
DeviceConfig::DeviceConfig(std::shared_ptr<Configuration> config, Device* hidpp20::Device& Device::hidpp20()
{
return _hidpp20;
}
DeviceConfig::DeviceConfig(const std::shared_ptr<Configuration>& config, Device*
device) : _device (device), _config (config) device) : _device (device), _config (config)
{ {
try { try {
@ -73,7 +91,7 @@ DeviceConfig::DeviceConfig(std::shared_ptr<Configuration> config, Device*
} }
} }
std::string DeviceConfig::getSetting(std::string path) libconfig::Setting& DeviceConfig::getSetting(std::string path)
{ {
return _root_setting + '/' + path; return _config->getSetting(_root_setting + '/' + path);
} }

View File

@ -31,8 +31,9 @@ namespace logid
class DeviceConfig class DeviceConfig
{ {
public: public:
DeviceConfig(std::shared_ptr<Configuration> config, Device* device); DeviceConfig(const std::shared_ptr<Configuration>& config, Device*
std::string getSetting(std::string path); device);
libconfig::Setting& getSetting(std::string path);
private: private:
Device* _device; Device* _device;
std::string _root_setting; std::string _root_setting;
@ -54,10 +55,13 @@ namespace logid
uint16_t pid(); uint16_t pid();
DeviceConfig& config(); DeviceConfig& config();
backend::hidpp20::Device& hidpp20();
void wakeup(); void wakeup();
void sleep(); void sleep();
private: private:
void _init();
backend::hidpp20::Device _hidpp20; backend::hidpp20::Device _hidpp20;
std::string _path; std::string _path;
backend::hidpp::DeviceIndex _index; backend::hidpp::DeviceIndex _index;

114
src/logid/features/DPI.cpp Normal file
View File

@ -0,0 +1,114 @@
/*
* Copyright 2019-2020 PixlOne
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <algorithm>
#include <cmath>
#include "DPI.h"
#include "../Device.h"
#include "../util/log.h"
using namespace logid::features;
using namespace logid::backend;
uint16_t getClosestDPI(hidpp20::AdjustableDPI::SensorDPIList& dpi_list,
uint16_t dpi)
{
if(dpi_list.isRange) {
const uint16_t min = *std::min_element(dpi_list.dpis.begin(),
dpi_list.dpis.end());
const uint16_t max = *std::max_element(dpi_list.dpis.begin(),
dpi_list.dpis.end());
if(!((dpi-min) % dpi_list.dpiStep) && dpi >= min && dpi <= max)
return dpi;
else if(dpi > max)
return max;
else if(dpi < min)
return min;
else
return min + round((double)(dpi-min)/dpi_list.dpiStep)*dpi_list
.dpiStep;
} else {
if(std::find(dpi_list.dpis.begin(), dpi_list.dpis.end(), dpi)
!= dpi_list.dpis.end())
return dpi;
else {
auto it = std::min_element(dpi_list.dpis.begin(), dpi_list.dpis
.end(), [dpi](uint16_t a, uint16_t b) {
return (dpi - a) < (dpi - b);
});
if(it == dpi_list.dpis.end())
return 0;
else
return *it;
}
}
}
DPI::DPI(Device* device) : DeviceFeature(device), _config (device),
_adjustable_dpi (&device->hidpp20())
{
}
void DPI::configure()
{
const uint8_t sensors = _adjustable_dpi.getSensorCount();
for(uint8_t i = 0; i < _config.getSensorCount() && i < sensors; i++) {
auto dpi = _config.getDPI(i);
if(dpi) {
auto dpi_list = _adjustable_dpi.getSensorDPIList(i);
_adjustable_dpi.setSensorDPI(i, getClosestDPI(dpi_list,
dpi));
}
}
}
void DPI::listen()
{
}
/* Some devices have multiple sensors, but an older config format
* only supports a single DPI. The dpi setting can be an array or
* an integer.
*/
DPI::Config::Config(Device *dev) : DeviceFeature::Config(dev)
{
try {
auto& config_root = dev->config().getSetting("dpi");
if(config_root.isNumber()) {
int dpi = config_root;
_dpis.push_back(dpi);
} else if(config_root.isArray()) {
for(int i = 0; i < config_root.getLength(); i++)
_dpis.push_back((int)config_root[i]);
} else {
logPrintf(WARN, "Line %d: dpi is improperly formatted",
config_root.getSourceLine());
}
} catch(libconfig::SettingNotFoundException& e) {
// DPI not configured, use default
}
}
uint8_t DPI::Config::getSensorCount()
{
return _dpis.size();
}
uint16_t DPI::Config::getDPI(uint8_t sensor)
{
return _dpis[sensor];
}

49
src/logid/features/DPI.h Normal file
View File

@ -0,0 +1,49 @@
/*
* Copyright 2019-2020 PixlOne
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef LOGID_FEATURES_DPI_H
#define LOGID_FEATURES_DPI_H
#include "../backend/hidpp20/features/AdjustableDPI.h"
#include "DeviceFeature.h"
namespace logid {
namespace features
{
class DPI : public DeviceFeature
{
public:
explicit DPI(Device* dev);
virtual void configure();
virtual void listen();
class Config : public DeviceFeature::Config
{
public:
explicit Config(Device* dev);
uint16_t getDPI(uint8_t sensor);
uint8_t getSensorCount();
protected:
std::vector<uint16_t> _dpis;
};
private:
Config _config;
backend::hidpp20::AdjustableDPI _adjustable_dpi;
};
}}
#endif //LOGID_FEATURE_DPI_H

View File

@ -19,6 +19,8 @@
#ifndef LOGID_FEATURES_DEVICEFEATURE_H #ifndef LOGID_FEATURES_DEVICEFEATURE_H
#define LOGID_FEATURES_DEVICEFEATURE_H #define LOGID_FEATURES_DEVICEFEATURE_H
#include <string>
namespace logid { namespace logid {
class Device; class Device;
namespace features namespace features
@ -38,11 +40,10 @@ namespace features
{ {
} }
protected: protected:
virtual const std::string configPath() = 0;
Device* _device; Device* _device;
std::string root_setting;
}; };
private:
protected:
Device* _device; Device* _device;
}; };
}} }}