logiops/src/logid/Device.h

110 lines
3.1 KiB
C
Raw Normal View History

/*
* 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/>.
*
*/
2019-10-05 01:56:17 +00:00
#ifndef LOGID_DEVICE_H
#define LOGID_DEVICE_H
2020-06-20 01:58:33 +00:00
#include "backend/hidpp/defs.h"
#include "backend/hidpp20/Device.h"
#include "features/DeviceFeature.h"
#include "Configuration.h"
2020-07-02 20:04:18 +00:00
#include "util/log.h"
2019-10-05 01:56:17 +00:00
namespace logid
{
class Device;
class Receiver;
class DeviceConfig
{
public:
2020-07-02 07:34:14 +00:00
DeviceConfig(const std::shared_ptr<Configuration>& config, Device*
device);
2020-07-14 04:12:40 +00:00
libconfig::Setting& getSetting(const std::string& path);
private:
Device* _device;
std::string _root_setting;
std::shared_ptr<Configuration> _config;
};
2020-06-20 01:58:33 +00:00
/* TODO: Implement HID++ 1.0 support
* Currently, the logid::Device class has a hardcoded requirement
* for an HID++ 2.0 device.
*/
2019-10-05 01:56:17 +00:00
class Device
{
public:
2020-06-20 01:58:33 +00:00
Device(std::string path, backend::hidpp::DeviceIndex index);
Device(const std::shared_ptr<backend::raw::RawDevice>& raw_device,
backend::hidpp::DeviceIndex index);
Device(Receiver* receiver, backend::hidpp::DeviceIndex index);
std::string name();
uint16_t pid();
DeviceConfig& config();
2020-07-02 07:34:14 +00:00
backend::hidpp20::Device& hidpp20();
2020-06-20 01:58:33 +00:00
void wakeup();
void sleep();
2020-07-02 20:04:18 +00:00
2020-07-05 20:16:38 +00:00
void reset();
2020-07-02 20:04:18 +00:00
template<typename T>
std::shared_ptr<T> getFeature(std::string name) {
auto it = _features.find(name);
if(it == _features.end())
return nullptr;
try {
2020-07-08 20:29:20 +00:00
return std::dynamic_pointer_cast<T>(it->second);
2020-07-02 20:04:18 +00:00
} catch(std::bad_cast& e) {
2020-07-14 04:12:40 +00:00
logPrintf(ERROR, "bad_cast while getting device feature %s: %s",
name.c_str(), e.what());
2020-07-02 20:04:18 +00:00
return nullptr;
}
}
2020-06-20 01:58:33 +00:00
private:
2020-07-02 07:34:14 +00:00
void _init();
2020-07-02 19:27:30 +00:00
/* Adds a feature without calling an error if unsupported */
template<typename T>
2020-07-02 20:04:18 +00:00
void _addFeature(std::string name)
2020-07-02 19:27:30 +00:00
{
try {
2020-07-02 20:04:18 +00:00
_features.emplace(name, std::make_shared<T>(this));
} catch (features::UnsupportedFeature& e) {
2020-07-02 19:27:30 +00:00
}
}
2020-06-20 01:58:33 +00:00
backend::hidpp20::Device _hidpp20;
std::string _path;
backend::hidpp::DeviceIndex _index;
2020-07-02 20:04:18 +00:00
std::map<std::string, std::shared_ptr<features::DeviceFeature>>
_features;
DeviceConfig _config;
2020-07-05 20:16:38 +00:00
Receiver* _receiver;
2020-07-05 20:16:38 +00:00
void _makeResetMechanism();
std::unique_ptr<std::function<void()>> _reset_mechanism;
2019-10-05 01:56:17 +00:00
};
}
#endif //LOGID_DEVICE_H