Merge pull request #185 from aragon999/feature/only-register-axes-if-used

Enable axis in virtual_input device only when needed
This commit is contained in:
pixl 2021-01-06 20:58:46 -05:00 committed by GitHub
commit 1f1a306334
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 15 deletions

View File

@ -77,26 +77,28 @@ InputDevice::~InputDevice()
void InputDevice::registerKey(uint code) 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; 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);
if(err != 0) {
libevdev_free(device);
device = nullptr;
ui_device = nullptr;
throw std::system_error(-err, std::generic_category());
}
registered_keys[code] = true; registered_keys[code] = true;
} }
void InputDevice::registerAxis(uint axis)
{
// TODO: Maybe print error message, if wrong code is passed?
if(registered_axis[axis] || axis > REL_CNT) {
return;
}
_enableEvent(EV_REL, axis);
registered_axis[axis] = true;
}
void InputDevice::moveAxis(uint axis, int movement) void InputDevice::moveAxis(uint axis, int movement)
{ {
_sendEvent(EV_REL, axis, movement); _sendEvent(EV_REL, axis, movement);
@ -148,6 +150,23 @@ uint InputDevice::_toEventCode(uint type, const std::string& name)
return code; 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) void InputDevice::_sendEvent(uint type, uint code, int value)
{ {
libevdev_uinput_write_event(ui_device, type, code, value); libevdev_uinput_write_event(ui_device, type, code, value);

View File

@ -45,6 +45,7 @@ namespace logid
~InputDevice(); ~InputDevice();
void registerKey(uint code); void registerKey(uint code);
void registerAxis(uint axis);
void moveAxis(uint axis, int movement); void moveAxis(uint axis, int movement);
void pressKey(uint code); void pressKey(uint code);
void releaseKey(uint code); void releaseKey(uint code);
@ -55,10 +56,12 @@ namespace logid
private: private:
void _sendEvent(uint type, uint code, int value); void _sendEvent(uint type, uint code, int value);
void _enableEvent(uint type, uint name);
static uint _toEventCode(uint type, const std::string& name); static uint _toEventCode(uint type, const std::string& name);
bool registered_keys[KEY_CNT]; bool registered_keys[KEY_CNT];
bool registered_axis[REL_CNT];
libevdev* device; libevdev* device;
libevdev_uinput* ui_device{}; libevdev_uinput* ui_device{};
}; };

View File

@ -104,9 +104,11 @@ AxisGesture::Config::Config(Device *device, libconfig::Setting &setting) :
auto& axis = setting.lookup("axis"); auto& axis = setting.lookup("axis");
if(axis.isNumber()) { if(axis.isNumber()) {
_axis = axis; _axis = axis;
virtual_input->registerAxis(_axis);
} else if(axis.getType() == libconfig::Setting::TypeString) { } else if(axis.getType() == libconfig::Setting::TypeString) {
try { try {
_axis = virtual_input->toAxisCode(axis); _axis = virtual_input->toAxisCode(axis);
virtual_input->registerAxis(_axis);
} catch(InputDevice::InvalidEventCode& e) { } catch(InputDevice::InvalidEventCode& e) {
logPrintf(WARN, "Line %d: Invalid axis %s, skipping." logPrintf(WARN, "Line %d: Invalid axis %s, skipping."
, axis.getSourceLine(), axis.c_str()); , axis.getSourceLine(), axis.c_str());
@ -138,8 +140,11 @@ AxisGesture::Config::Config(Device *device, libconfig::Setting &setting) :
// Ignore // Ignore
} }
if(InputDevice::getLowResAxis(_axis) != -1) int low_res_axis = InputDevice::getLowResAxis(_axis);
if(low_res_axis != -1) {
_multiplier *= 120; _multiplier *= 120;
virtual_input->registerAxis(low_res_axis);
}
} }
unsigned int AxisGesture::Config::axis() const unsigned int AxisGesture::Config::axis() const
@ -168,4 +173,4 @@ void AxisGesture::Config::setHiresMultiplier(double multiplier)
} }
_hires_multiplier = multiplier; _hires_multiplier = multiplier;
} }