From 1f028829715783642691b701e621a0e5a21a5e22 Mon Sep 17 00:00:00 2001 From: max Date: Mon, 21 Dec 2020 11:47:25 +0100 Subject: [PATCH] Enable axis in virtual_input device only when needed --- src/logid/InputDevice.cpp | 41 +++++++++++++++++------ src/logid/InputDevice.h | 3 ++ src/logid/actions/gesture/AxisGesture.cpp | 9 +++-- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/logid/InputDevice.cpp b/src/logid/InputDevice.cpp index a97daf8..af729a8 100644 --- a/src/logid/InputDevice.cpp +++ b/src/logid/InputDevice.cpp @@ -77,24 +77,26 @@ InputDevice::~InputDevice() void InputDevice::registerKey(uint code) { - if (registered_keys[code]) { + // TODO: Maybe print error message, if wrong code is passed? + if(registered_keys[code] || code > KEY_CNT) { return; } - libevdev_uinput_destroy(ui_device); + _enableEvent(EV_KEY, code); - libevdev_enable_event_code(device, EV_KEY, code, nullptr); - int err = libevdev_uinput_create_from_device(device, - LIBEVDEV_UINPUT_OPEN_MANAGED, &ui_device); + registered_keys[code] = true; +} - if(err != 0) { - libevdev_free(device); - device = nullptr; - ui_device = nullptr; - throw std::system_error(-err, std::generic_category()); +void InputDevice::registerAxis(uint axis) +{ + // TODO: Maybe print error message, if wrong code is passed? + if(registered_axis[axis] || axis > REL_CNT) { + return; } - registered_keys[code] = true; + _enableEvent(EV_REL, axis); + + registered_axis[axis] = true; } void InputDevice::moveAxis(uint axis, int movement) @@ -148,6 +150,23 @@ uint InputDevice::_toEventCode(uint type, const std::string& name) return code; } +void InputDevice::_enableEvent(const uint type, const uint code) +{ + libevdev_uinput_destroy(ui_device); + + libevdev_enable_event_code(device, type, code, nullptr); + + int err = libevdev_uinput_create_from_device(device, + LIBEVDEV_UINPUT_OPEN_MANAGED, &ui_device); + + if(err != 0) { + libevdev_free(device); + device = nullptr; + ui_device = nullptr; + throw std::system_error(-err, std::generic_category()); + } +} + void InputDevice::_sendEvent(uint type, uint code, int value) { libevdev_uinput_write_event(ui_device, type, code, value); diff --git a/src/logid/InputDevice.h b/src/logid/InputDevice.h index 4846a15..c3c803d 100644 --- a/src/logid/InputDevice.h +++ b/src/logid/InputDevice.h @@ -45,6 +45,7 @@ namespace logid ~InputDevice(); void registerKey(uint code); + void registerAxis(uint axis); void moveAxis(uint axis, int movement); void pressKey(uint code); void releaseKey(uint code); @@ -55,10 +56,12 @@ namespace logid private: void _sendEvent(uint type, uint code, int value); + void _enableEvent(uint type, uint name); static uint _toEventCode(uint type, const std::string& name); bool registered_keys[KEY_CNT]; + bool registered_axis[REL_CNT]; libevdev* device; libevdev_uinput* ui_device{}; }; diff --git a/src/logid/actions/gesture/AxisGesture.cpp b/src/logid/actions/gesture/AxisGesture.cpp index 9325e8c..8ec26f5 100644 --- a/src/logid/actions/gesture/AxisGesture.cpp +++ b/src/logid/actions/gesture/AxisGesture.cpp @@ -104,9 +104,11 @@ AxisGesture::Config::Config(Device *device, libconfig::Setting &setting) : auto& axis = setting.lookup("axis"); if(axis.isNumber()) { _axis = axis; + virtual_input->registerAxis(_axis); } else if(axis.getType() == libconfig::Setting::TypeString) { try { _axis = virtual_input->toAxisCode(axis); + virtual_input->registerAxis(_axis); } catch(InputDevice::InvalidEventCode& e) { logPrintf(WARN, "Line %d: Invalid axis %s, skipping." , axis.getSourceLine(), axis.c_str()); @@ -138,8 +140,11 @@ AxisGesture::Config::Config(Device *device, libconfig::Setting &setting) : // Ignore } - if(InputDevice::getLowResAxis(_axis) != -1) + int low_res_axis = InputDevice::getLowResAxis(_axis); + if(low_res_axis != -1) { _multiplier *= 120; + virtual_input->registerAxis(low_res_axis); + } } unsigned int AxisGesture::Config::axis() const @@ -168,4 +173,4 @@ void AxisGesture::Config::setHiresMultiplier(double multiplier) } _hires_multiplier = multiplier; -} \ No newline at end of file +}