Apply clang-tidy inspections

This commit is contained in:
pixl 2020-07-14 00:12:40 -04:00
parent 937225b6f2
commit d3d4a2755a
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
23 changed files with 94 additions and 90 deletions

View File

@ -42,15 +42,7 @@ Configuration::Configuration(const std::string& config_file)
}
const Setting &root = _config.getRoot();
Setting* devices;
try { devices = &root["devices"]; }
catch(const SettingNotFoundException &e) {
logPrintf(WARN, "No devices listed in config file.");
return;
}
_worker_threads = LOGID_DEFAULT_WORKER_COUNT;
try {
auto& worker_count = root["workers"];
if(worker_count.getType() == Setting::TypeInt) {
@ -66,7 +58,6 @@ Configuration::Configuration(const std::string& config_file)
// Ignore
}
_io_timeout = LOGID_DEFAULT_RAWDEVICE_TIMEOUT;
try {
auto& timeout = root["io_timeout"];
if(timeout.isNumber()) {
@ -82,8 +73,11 @@ Configuration::Configuration(const std::string& config_file)
// Ignore
}
for(int i = 0; i < devices->getLength(); i++) {
const Setting &device = (*devices)[i];
try {
auto& devices = root["devices"];
for(int i = 0; i < devices.getLength(); i++) {
const Setting& device = devices[i];
std::string name;
try {
if(!device.lookupValue("name", name)) {
@ -92,20 +86,24 @@ Configuration::Configuration(const std::string& config_file)
continue;
}
} catch(SettingNotFoundException &e) {
logPrintf(WARN, "Line %d: Missing 'name' field, skipping device."
logPrintf(WARN, "Line %d: Missing name field, skipping device."
, device.getSourceLine());
continue;
}
_device_paths.insert({name, device.getPath()});
}
}
catch(const SettingNotFoundException &e) {
logPrintf(WARN, "No devices listed in config file.");
}
}
libconfig::Setting& Configuration::getSetting(std::string path)
libconfig::Setting& Configuration::getSetting(const std::string& path)
{
return _config.lookup(path);
}
std::string Configuration::getDevice(std::string name)
std::string Configuration::getDevice(const std::string& name)
{
auto it = _device_paths.find(name);
if(it == _device_paths.end())

View File

@ -24,7 +24,7 @@
#include <memory>
#include <chrono>
#define LOGID_DEFAULT_RAWDEVICE_TIMEOUT std::chrono::seconds(2)
#define LOGID_DEFAULT_IO_TIMEOUT std::chrono::seconds(2)
#define LOGID_DEFAULT_WORKER_COUNT 4
namespace logid
@ -34,8 +34,8 @@ namespace logid
public:
explicit Configuration(const std::string& config_file);
Configuration() = default;
libconfig::Setting& getSetting(std::string path);
std::string getDevice(std::string name);
libconfig::Setting& getSetting(const std::string& path);
std::string getDevice(const std::string& name);
class DeviceNotFound : public std::exception
{
@ -50,8 +50,8 @@ namespace logid
int workerCount() const;
private:
std::map<std::string, std::string> _device_paths;
std::chrono::milliseconds _io_timeout = LOGID_DEFAULT_RAWDEVICE_TIMEOUT;
int _worker_threads;
std::chrono::milliseconds _io_timeout = LOGID_DEFAULT_IO_TIMEOUT;
int _worker_threads = LOGID_DEFAULT_WORKER_COUNT;
libconfig::Config _config;
};

View File

@ -113,7 +113,10 @@ void Device::_makeResetMechanism()
try {
hidpp20::Reset reset(&_hidpp20);
_reset_mechanism = std::make_unique<std::function<void()>>(
[dev=&this->_hidpp20]{ hidpp20::Reset(dev).reset(); });
[dev=&this->_hidpp20]{
hidpp20::Reset reset(dev);
reset.reset(reset.getProfile());
});
} catch(hidpp20::UnsupportedFeature& e) {
// Reset unsupported, ignore.
}
@ -130,7 +133,7 @@ DeviceConfig::DeviceConfig(const std::shared_ptr<Configuration>& config, Device*
}
}
libconfig::Setting& DeviceConfig::getSetting(std::string path)
libconfig::Setting& DeviceConfig::getSetting(const std::string& path)
{
return _config->getSetting(_root_setting + '/' + path);
}

View File

@ -35,7 +35,7 @@ namespace logid
public:
DeviceConfig(const std::shared_ptr<Configuration>& config, Device*
device);
libconfig::Setting& getSetting(std::string path);
libconfig::Setting& getSetting(const std::string& path);
private:
Device* _device;
std::string _root_setting;
@ -72,8 +72,8 @@ namespace logid
try {
return std::dynamic_pointer_cast<T>(it->second);
} catch(std::bad_cast& e) {
logPrintf(ERROR, "bad_cast while getting device feature %s: "
"%s", name.c_str(), e.what());
logPrintf(ERROR, "bad_cast while getting device feature %s: %s",
name.c_str(), e.what());
return nullptr;
}
}

View File

@ -23,11 +23,8 @@
#include "Receiver.h"
#include "util/log.h"
#include "backend/hidpp10/Error.h"
#include "backend/dj/Receiver.h"
#include "backend/Error.h"
#define NON_WIRELESS_DEV(index) (index) == HIDPP::DefaultDevice ? "default" : "corded"
using namespace logid;
using namespace logid::backend;

View File

@ -17,7 +17,6 @@
*/
#include <system_error>
#include <utility>
#include "InputDevice.h"
@ -25,13 +24,13 @@ extern "C"
{
#include <libevdev/libevdev.h>
#include <libevdev/libevdev-uinput.h>
};
}
using namespace logid;
InputDevice::InvalidEventCode::InvalidEventCode(std::string name)
InputDevice::InvalidEventCode::InvalidEventCode(const std::string& name) :
_what ("Invalid event code " + name)
{
_what = "Invalid event code " + name;
}
const char* InputDevice::InvalidEventCode::what() const noexcept
@ -46,10 +45,10 @@ InputDevice::InputDevice(const char* name)
///TODO: Is it really a good idea to enable all events?
libevdev_enable_event_type(device, EV_KEY);
for(int i = 0; i < KEY_CNT; i++)
for(unsigned int i = 0; i < KEY_CNT; i++)
libevdev_enable_event_code(device, EV_KEY, i, nullptr);
libevdev_enable_event_type(device, EV_REL);
for(int i = 0; i < REL_CNT; i++)
for(unsigned int i = 0; i < REL_CNT; i++)
libevdev_enable_event_code(device, EV_REL, i, nullptr);
int err = libevdev_uinput_create_from_device(device,
@ -80,14 +79,14 @@ void InputDevice::releaseKey(uint code)
_sendEvent(EV_KEY, code, 0);
}
uint InputDevice::toKeyCode(std::string name)
uint InputDevice::toKeyCode(const std::string& name)
{
return _toEventCode(EV_KEY, std::move(name));
return _toEventCode(EV_KEY, name);
}
uint InputDevice::toAxisCode(std::string name)
uint InputDevice::toAxisCode(const std::string& name)
{
return _toEventCode(EV_REL, std::move(name));
return _toEventCode(EV_REL, name);
}
uint InputDevice::_toEventCode(uint type, const std::string& name)

View File

@ -25,22 +25,20 @@ extern "C"
{
#include <libevdev/libevdev.h>
#include <libevdev/libevdev-uinput.h>
};
}
namespace logid
{
typedef uint keycode;
class InputDevice
{
public:
class InvalidEventCode : public std::exception
{
public:
explicit InvalidEventCode(std::string name);
explicit InvalidEventCode(const std::string& name);
const char* what() const noexcept override;
private:
std::string _what;
const std::string _what;
};
explicit InputDevice(const char *name);
~InputDevice();
@ -49,15 +47,15 @@ namespace logid
void pressKey(uint code);
void releaseKey(uint code);
static uint toKeyCode(std::string name);
static uint toAxisCode(std::string name);
static uint toKeyCode(const std::string& name);
static uint toAxisCode(const std::string& name);
private:
void _sendEvent(uint type, uint code, int value);
static uint _toEventCode(uint type, const std::string& name);
libevdev* device;
libevdev_uinput* ui_device;
libevdev_uinput* ui_device{};
};
extern std::unique_ptr<InputDevice> virtual_input;

View File

@ -16,7 +16,6 @@
*
*/
#include <cassert>
#include "Receiver.h"
#include "util/log.h"
#include "backend/hidpp10/Error.h"
@ -26,7 +25,8 @@
using namespace logid;
using namespace logid::backend;
Receiver::Receiver(std::string path) : dj::ReceiverMonitor(path), _path (path)
Receiver::Receiver(const std::string& path) :
dj::ReceiverMonitor(path), _path (path)
{
}

View File

@ -28,7 +28,7 @@ namespace logid
class Receiver : public backend::dj::ReceiverMonitor
{
public:
Receiver(std::string path);
Receiver(const std::string& path);
protected:
void addDevice(backend::hidpp::DeviceConnectionEvent event) override;

View File

@ -40,6 +40,11 @@ ReceiverMonitor::ReceiverMonitor(std::string path) : _receiver (
_receiver->enableHidppNotifications(notification_flags);
}
ReceiverMonitor::~ReceiverMonitor()
{
this->stop();
}
void ReceiverMonitor::run()
{
_receiver->listen();

View File

@ -33,6 +33,7 @@ namespace dj
{
public:
explicit ReceiverMonitor(std::string path);
~ReceiverMonitor();
void enumerate();
void run();

View File

@ -16,8 +16,8 @@
*
*/
#ifndef LOGID_HIDPP_DEFS_H
#define LOGID_HIDPP_DEFS_H
#ifndef LOGID_BACKEND_HIDPP_DEFS_H
#define LOGID_BACKEND_HIDPP_DEFS_H
#define LOGID_HIDPP_SOFTWARE_ID 0
@ -52,4 +52,4 @@ namespace hidpp
static constexpr std::size_t LongParamLength = 16;
} } }
#endif //LOGID_HIDPP_DEFS_H
#endif //LOGID_BACKEND_HIDPP_DEFS_H

View File

@ -16,8 +16,8 @@
*
*/
#ifndef LOGID_HIDPP20_DEVICE_H
#define LOGID_HIDPP20_DEVICE_H
#ifndef LOGID_BACKEND_HIDPP20_DEVICE_H
#define LOGID_BACKEND_HIDPP20_DEVICE_H
#include "../hidpp/Device.h"
#include <cstdint>
@ -37,4 +37,4 @@ namespace hidpp20 {
};
}}}
#endif //LOGID_HIDPP20_DEVICE_H
#endif //LOGID_BACKEND_HIDPP20_DEVICE_H

View File

@ -17,7 +17,6 @@
*/
#include <cassert>
#include <cstring>
#include "Error.h"
using namespace logid::backend::hidpp20;

View File

@ -16,8 +16,8 @@
*
*/
#ifndef LOGID_HIDPP20_ESSENTIAL_FEATURE_H
#define LOGID_HIDPP20_ESSENTIAL_FEATURE_H
#ifndef LOGID_BACKEND_HIDPP20_ESSENTIAL_FEATURE_H
#define LOGID_BACKEND_HIDPP20_ESSENTIAL_FEATURE_H
// WARNING: UNSAFE
@ -47,4 +47,4 @@ namespace hidpp20
};
}}}
#endif //LOGID_HIDPP20_ESSENTIAL_FEATURE_H
#endif //LOGID_BACKEND_HIDPP20_ESSENTIAL_FEATURE_H

View File

@ -16,8 +16,8 @@
*
*/
#ifndef LOGID_HIDPP20_FEATURE_H
#define LOGID_HIDPP20_FEATURE_H
#ifndef LOGID_BACKEND_HIDPP20_FEATURE_H
#define LOGID_BACKEND_HIDPP20_FEATURE_H
#include <cstdint>
#include "Device.h"
@ -51,4 +51,4 @@ namespace hidpp20 {
};
}}}
#endif //LOGID_HIDPP20_FEATURE_H
#endif //LOGID_BACKEND_HIDPP20_FEATURE_H

View File

@ -85,7 +85,6 @@ namespace hidpp20
void setMode(uint8_t mode);
bool getRatchetState();
///TODO: Event handlers
static WheelStatus wheelMovementEvent(const hidpp::Report& report);
static RatchetState ratchetSwitchEvent(const hidpp::Report& report);
};

View File

@ -44,10 +44,10 @@ DEFINE_REPROG(ReprogControlsV4, ReprogControlsV3)
std::shared_ptr<ReprogControls> ReprogControls::autoVersion(Device *dev)
{
MAKE_REPROG(ReprogControlsV4, dev);
MAKE_REPROG(ReprogControlsV3, dev);
MAKE_REPROG(ReprogControlsV2_2, dev);
MAKE_REPROG(ReprogControlsV2, dev);
MAKE_REPROG(ReprogControlsV4, dev)
MAKE_REPROG(ReprogControlsV3, dev)
MAKE_REPROG(ReprogControlsV2_2, dev)
MAKE_REPROG(ReprogControlsV2, dev)
// If base version cannot be made, throw error
return std::make_shared<ReprogControls>(dev);

View File

@ -43,7 +43,7 @@ RemapButton::~RemapButton()
void RemapButton::configure()
{
///TODO: DJ reporting trickery if cannot be remapped
for(auto& i : _config.buttons()) {
for(const auto& i : _config.buttons()) {
hidpp20::ReprogControls::ControlInfo info{};
try {
info = _reprog_controls->getControlIdInfo(i.first);
@ -89,23 +89,23 @@ void RemapButton::listen()
report));
else { // RawXY
auto divertedXY = _reprog_controls->divertedRawXYEvent(report);
for(auto& button : this->_config.buttons())
for(const auto& button : this->_config.buttons())
if(button.second->pressed())
button.second->move(divertedXY.x, divertedXY.y);
}
};
_device->hidpp20().addEventHandler(EVENTHANDLER_NAME, handler);
};
}
}
void RemapButton::_buttonEvent(std::set<uint16_t> new_state)
void RemapButton::_buttonEvent(const std::set<uint16_t>& new_state)
{
// Ensure I/O doesn't occur while updating button state
std::lock_guard<std::mutex> lock(_button_lock);
// Press all added buttons
for(auto& i : new_state) {
for(const auto& i : new_state) {
auto old_i = _pressed_buttons.find(i);
if(old_i != _pressed_buttons.end()) {
_pressed_buttons.erase(old_i);

View File

@ -44,7 +44,7 @@ namespace features
std::map<uint8_t, std::shared_ptr<actions::Action>> _buttons;
};
private:
void _buttonEvent(std::set<uint16_t> new_state);
void _buttonEvent(const std::set<uint16_t>& new_state);
Config _config;
std::shared_ptr<backend::hidpp20::ReprogControls> _reprog_controls;
std::set<uint16_t> _pressed_buttons;

View File

@ -37,7 +37,10 @@
using namespace logid;
struct CmdlineOptions
{
std::string config_file = DEFAULT_CONFIG_FILE;
};
LogLevel logid::global_loglevel = INFO;
std::shared_ptr<Configuration> logid::global_config;
@ -72,7 +75,7 @@ void logid::reload()
}
*/
void readCliOptions(int argc, char** argv)
void readCliOptions(const int argc, char** argv, CmdlineOptions& options)
{
for(int i = 1; i < argc; i++) {
Option option = Option::None;
@ -132,7 +135,7 @@ void readCliOptions(int argc, char** argv)
logPrintf(ERROR, "Config file is not specified.");
exit(EXIT_FAILURE);
}
config_file = argv[i];
options.config_file = argv[i];
break;
}
case Option::Help:
@ -157,16 +160,18 @@ Possible options are:
int main(int argc, char** argv)
{
readCliOptions(argc, argv);
CmdlineOptions options{};
readCliOptions(argc, argv, options);
// Read config
try {
global_config = std::make_shared<Configuration>(config_file);
global_config = std::make_shared<Configuration>(options.config_file);
}
catch (std::exception &e) {
global_config = std::make_shared<Configuration>();
}
global_workqueue = std::make_shared<workqueue>(global_config->workerCount());
global_workqueue = std::make_shared<workqueue>(
global_config->workerCount());
//Create a virtual input device
try {

View File

@ -45,8 +45,8 @@ void thread::spawn(const std::function<void()>& function,
void thread::run()
{
_thread = std::make_shared<std::thread>([f=this->_function,
eh=this->_exception_handler]() {
_thread = std::make_shared<std::thread>(
[f=this->_function,eh=this->_exception_handler]() {
try {
(*f)();
} catch (std::exception& e) {

View File

@ -28,7 +28,7 @@ workqueue::workqueue(std::size_t thread_count) : _manager_thread (
)), _continue_run (false), _worker_count (thread_count)
{
_workers.reserve(_worker_count);
for(std::size_t i = 0; i < thread_count; i++)
for(std::size_t i = 0; i < _worker_count; i++)
_workers.push_back(std::make_unique<worker_thread>(this, i));
_manager_thread->run();
}