Merge pull request #183 from aragon999/fix/keypress-systemd-issue

Register keys for keypress only when needed.

TODO: Apply this for axes too?
This commit is contained in:
pixl 2020-12-20 19:46:44 -05:00 committed by GitHub
commit ffcfb3da82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 6 deletions

View File

@ -43,10 +43,19 @@ InputDevice::InputDevice(const char* name)
device = libevdev_new(); device = libevdev_new();
libevdev_set_name(device, name); libevdev_set_name(device, name);
///TODO: Is it really a good idea to enable all events?
libevdev_enable_event_type(device, EV_KEY); libevdev_enable_event_type(device, EV_KEY);
for(unsigned int i = 0; i < KEY_CNT; i++) for (unsigned int i = 0; i < KEY_CNT; i++) {
// Enable some keys which a normal keyboard should have
// by default, i.e. a-z, modifier keys and so on, see:
// /usr/include/linux/input-event-codes.h
if (i < 128) {
registered_keys[i] = true;
libevdev_enable_event_code(device, EV_KEY, i, nullptr); libevdev_enable_event_code(device, EV_KEY, i, nullptr);
} else {
registered_keys[i] = false;
}
}
libevdev_enable_event_type(device, EV_REL); libevdev_enable_event_type(device, EV_REL);
for(unsigned 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); libevdev_enable_event_code(device, EV_REL, i, nullptr);
@ -54,9 +63,11 @@ InputDevice::InputDevice(const char* name)
int err = libevdev_uinput_create_from_device(device, int err = libevdev_uinput_create_from_device(device,
LIBEVDEV_UINPUT_OPEN_MANAGED, &ui_device); LIBEVDEV_UINPUT_OPEN_MANAGED, &ui_device);
if(err != 0) if(err != 0) {
libevdev_free(device);
throw std::system_error(-err, std::generic_category()); throw std::system_error(-err, std::generic_category());
} }
}
InputDevice::~InputDevice() InputDevice::~InputDevice()
{ {
@ -64,6 +75,28 @@ InputDevice::~InputDevice()
libevdev_free(device); libevdev_free(device);
} }
void InputDevice::registerKey(uint code)
{
if (registered_keys[code]) {
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());
}
registered_keys[code] = 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);

View File

@ -20,6 +20,7 @@
#define LOGID_INPUTDEVICE_H #define LOGID_INPUTDEVICE_H
#include <memory> #include <memory>
#include <string>
extern "C" extern "C"
{ {
@ -43,6 +44,7 @@ namespace logid
explicit InputDevice(const char *name); explicit InputDevice(const char *name);
~InputDevice(); ~InputDevice();
void registerKey(uint code);
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);
@ -56,6 +58,7 @@ namespace logid
static uint _toEventCode(uint type, const std::string& name); static uint _toEventCode(uint type, const std::string& name);
bool registered_keys[KEY_CNT];
libevdev* device; libevdev* device;
libevdev_uinput* ui_device{}; libevdev_uinput* ui_device{};
}; };

View File

@ -64,9 +64,11 @@ KeypressAction::Config::Config(Device* device, libconfig::Setting& config) :
auto& key = keys[i]; auto& key = keys[i];
if(key.isNumber()) { if(key.isNumber()) {
_keys.push_back(key); _keys.push_back(key);
virtual_input->registerKey(key);
} else if(key.getType() == libconfig::Setting::TypeString) { } else if(key.getType() == libconfig::Setting::TypeString) {
try { try {
_keys.push_back(virtual_input->toKeyCode(key)); _keys.push_back(virtual_input->toKeyCode(key));
virtual_input->registerKey(virtual_input->toKeyCode(key));
} catch(InputDevice::InvalidEventCode& e) { } catch(InputDevice::InvalidEventCode& e) {
logPrintf(WARN, "Line %d: Invalid keycode %s, skipping." logPrintf(WARN, "Line %d: Invalid keycode %s, skipping."
, key.getSourceLine(), key.c_str()); , key.getSourceLine(), key.c_str());