diff --git a/src/logid/Configuration.cpp b/src/logid/Configuration.cpp index 5f9a7a9..c287441 100644 --- a/src/logid/Configuration.cpp +++ b/src/logid/Configuration.cpp @@ -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,30 +73,37 @@ Configuration::Configuration(const std::string& config_file) // Ignore } - for(int i = 0; i < devices->getLength(); i++) { - const Setting &device = (*devices)[i]; - std::string name; - try { - if(!device.lookupValue("name", name)) { - logPrintf(WARN, "Line %d: 'name' must be a string, skipping " - "device.", device["name"].getSourceLine()); + 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)) { + logPrintf(WARN, "Line %d: 'name' must be a string, skipping" + " device.", device["name"].getSourceLine()); + continue; + } + } catch(SettingNotFoundException &e) { + logPrintf(WARN, "Line %d: Missing name field, skipping device." + , device.getSourceLine()); continue; } - } catch(SettingNotFoundException &e) { - logPrintf(WARN, "Line %d: Missing 'name' field, skipping device." - , device.getSourceLine()); - continue; + _device_paths.insert({name, device.getPath()}); } - _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()) diff --git a/src/logid/Configuration.h b/src/logid/Configuration.h index 3666fbc..7a196ed 100644 --- a/src/logid/Configuration.h +++ b/src/logid/Configuration.h @@ -24,7 +24,7 @@ #include #include -#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 _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; }; diff --git a/src/logid/Device.cpp b/src/logid/Device.cpp index 7f59980..9d32588 100644 --- a/src/logid/Device.cpp +++ b/src/logid/Device.cpp @@ -113,7 +113,10 @@ void Device::_makeResetMechanism() try { hidpp20::Reset reset(&_hidpp20); _reset_mechanism = std::make_unique>( - [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& config, Device* } } -libconfig::Setting& DeviceConfig::getSetting(std::string path) +libconfig::Setting& DeviceConfig::getSetting(const std::string& path) { return _config->getSetting(_root_setting + '/' + path); } diff --git a/src/logid/Device.h b/src/logid/Device.h index 66147b3..2a0cd42 100644 --- a/src/logid/Device.h +++ b/src/logid/Device.h @@ -35,7 +35,7 @@ namespace logid public: DeviceConfig(const std::shared_ptr& 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(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; } } diff --git a/src/logid/DeviceManager.cpp b/src/logid/DeviceManager.cpp index 98a678a..40185df 100644 --- a/src/logid/DeviceManager.cpp +++ b/src/logid/DeviceManager.cpp @@ -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; diff --git a/src/logid/InputDevice.cpp b/src/logid/InputDevice.cpp index 041e3f4..aecf548 100644 --- a/src/logid/InputDevice.cpp +++ b/src/logid/InputDevice.cpp @@ -17,7 +17,6 @@ */ #include -#include #include "InputDevice.h" @@ -25,13 +24,13 @@ extern "C" { #include #include -}; +} 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) diff --git a/src/logid/InputDevice.h b/src/logid/InputDevice.h index 0072271..5b2cd22 100644 --- a/src/logid/InputDevice.h +++ b/src/logid/InputDevice.h @@ -25,22 +25,20 @@ extern "C" { #include #include -}; +} 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 virtual_input; diff --git a/src/logid/Receiver.cpp b/src/logid/Receiver.cpp index a3f8adb..499cd9b 100644 --- a/src/logid/Receiver.cpp +++ b/src/logid/Receiver.cpp @@ -16,7 +16,6 @@ * */ -#include #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) { } diff --git a/src/logid/Receiver.h b/src/logid/Receiver.h index 15c0f9b..844a42d 100644 --- a/src/logid/Receiver.h +++ b/src/logid/Receiver.h @@ -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; diff --git a/src/logid/backend/dj/ReceiverMonitor.cpp b/src/logid/backend/dj/ReceiverMonitor.cpp index d3f9fb2..80a9283 100644 --- a/src/logid/backend/dj/ReceiverMonitor.cpp +++ b/src/logid/backend/dj/ReceiverMonitor.cpp @@ -40,6 +40,11 @@ ReceiverMonitor::ReceiverMonitor(std::string path) : _receiver ( _receiver->enableHidppNotifications(notification_flags); } +ReceiverMonitor::~ReceiverMonitor() +{ + this->stop(); +} + void ReceiverMonitor::run() { _receiver->listen(); diff --git a/src/logid/backend/dj/ReceiverMonitor.h b/src/logid/backend/dj/ReceiverMonitor.h index d5deb98..b56aced 100644 --- a/src/logid/backend/dj/ReceiverMonitor.h +++ b/src/logid/backend/dj/ReceiverMonitor.h @@ -33,6 +33,7 @@ namespace dj { public: explicit ReceiverMonitor(std::string path); + ~ReceiverMonitor(); void enumerate(); void run(); diff --git a/src/logid/backend/hidpp/defs.h b/src/logid/backend/hidpp/defs.h index 1590e06..3a85470 100644 --- a/src/logid/backend/hidpp/defs.h +++ b/src/logid/backend/hidpp/defs.h @@ -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 \ No newline at end of file +#endif //LOGID_BACKEND_HIDPP_DEFS_H \ No newline at end of file diff --git a/src/logid/backend/hidpp20/Device.h b/src/logid/backend/hidpp20/Device.h index 871363d..747d36d 100644 --- a/src/logid/backend/hidpp20/Device.h +++ b/src/logid/backend/hidpp20/Device.h @@ -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 @@ -37,4 +37,4 @@ namespace hidpp20 { }; }}} -#endif //LOGID_HIDPP20_DEVICE_H \ No newline at end of file +#endif //LOGID_BACKEND_HIDPP20_DEVICE_H \ No newline at end of file diff --git a/src/logid/backend/hidpp20/Error.cpp b/src/logid/backend/hidpp20/Error.cpp index 19a0ad8..eea11a9 100644 --- a/src/logid/backend/hidpp20/Error.cpp +++ b/src/logid/backend/hidpp20/Error.cpp @@ -17,7 +17,6 @@ */ #include -#include #include "Error.h" using namespace logid::backend::hidpp20; diff --git a/src/logid/backend/hidpp20/EssentialFeature.h b/src/logid/backend/hidpp20/EssentialFeature.h index f72a244..bfc578f 100644 --- a/src/logid/backend/hidpp20/EssentialFeature.h +++ b/src/logid/backend/hidpp20/EssentialFeature.h @@ -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 \ No newline at end of file +#endif //LOGID_BACKEND_HIDPP20_ESSENTIAL_FEATURE_H \ No newline at end of file diff --git a/src/logid/backend/hidpp20/Feature.h b/src/logid/backend/hidpp20/Feature.h index cc4732b..06ec38f 100644 --- a/src/logid/backend/hidpp20/Feature.h +++ b/src/logid/backend/hidpp20/Feature.h @@ -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 #include "Device.h" @@ -51,4 +51,4 @@ namespace hidpp20 { }; }}} -#endif //LOGID_HIDPP20_FEATURE_H \ No newline at end of file +#endif //LOGID_BACKEND_HIDPP20_FEATURE_H \ No newline at end of file diff --git a/src/logid/backend/hidpp20/features/HiresScroll.h b/src/logid/backend/hidpp20/features/HiresScroll.h index f008c64..69dee11 100644 --- a/src/logid/backend/hidpp20/features/HiresScroll.h +++ b/src/logid/backend/hidpp20/features/HiresScroll.h @@ -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); }; diff --git a/src/logid/backend/hidpp20/features/ReprogControls.cpp b/src/logid/backend/hidpp20/features/ReprogControls.cpp index 34334c4..1bf0fcc 100644 --- a/src/logid/backend/hidpp20/features/ReprogControls.cpp +++ b/src/logid/backend/hidpp20/features/ReprogControls.cpp @@ -44,10 +44,10 @@ DEFINE_REPROG(ReprogControlsV4, ReprogControlsV3) std::shared_ptr 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(dev); diff --git a/src/logid/features/RemapButton.cpp b/src/logid/features/RemapButton.cpp index 13204b0..3078084 100644 --- a/src/logid/features/RemapButton.cpp +++ b/src/logid/features/RemapButton.cpp @@ -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 new_state) +void RemapButton::_buttonEvent(const std::set& new_state) { // Ensure I/O doesn't occur while updating button state std::lock_guard 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); diff --git a/src/logid/features/RemapButton.h b/src/logid/features/RemapButton.h index a45e767..f4aa4b6 100644 --- a/src/logid/features/RemapButton.h +++ b/src/logid/features/RemapButton.h @@ -44,7 +44,7 @@ namespace features std::map> _buttons; }; private: - void _buttonEvent(std::set new_state); + void _buttonEvent(const std::set& new_state); Config _config; std::shared_ptr _reprog_controls; std::set _pressed_buttons; diff --git a/src/logid/logid.cpp b/src/logid/logid.cpp index 42c75e4..ddb77ec 100644 --- a/src/logid/logid.cpp +++ b/src/logid/logid.cpp @@ -37,7 +37,10 @@ using namespace logid; -std::string config_file = DEFAULT_CONFIG_FILE; +struct CmdlineOptions +{ + std::string config_file = DEFAULT_CONFIG_FILE; +}; LogLevel logid::global_loglevel = INFO; std::shared_ptr 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(config_file); + global_config = std::make_shared(options.config_file); } catch (std::exception &e) { global_config = std::make_shared(); } - global_workqueue = std::make_shared(global_config->workerCount()); + global_workqueue = std::make_shared( + global_config->workerCount()); //Create a virtual input device try { diff --git a/src/logid/util/thread.cpp b/src/logid/util/thread.cpp index 3460bd7..be496dc 100644 --- a/src/logid/util/thread.cpp +++ b/src/logid/util/thread.cpp @@ -45,8 +45,8 @@ void thread::spawn(const std::function& function, void thread::run() { - _thread = std::make_shared([f=this->_function, - eh=this->_exception_handler]() { + _thread = std::make_shared( + [f=this->_function,eh=this->_exception_handler]() { try { (*f)(); } catch (std::exception& e) { diff --git a/src/logid/util/workqueue.cpp b/src/logid/util/workqueue.cpp index a7e04f7..2bf409d 100644 --- a/src/logid/util/workqueue.cpp +++ b/src/logid/util/workqueue.cpp @@ -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(this, i)); _manager_thread->run(); }