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)
{
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);
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());
}
_enableEvent(EV_KEY, code);
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)
{
_sendEvent(EV_REL, axis, 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);

View File

@ -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{};
};

View File

@ -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;
}
}