|
|
|
@ -43,10 +43,19 @@ InputDevice::InputDevice(const char* name) |
|
|
|
|
device = libevdev_new(); |
|
|
|
|
libevdev_set_name(device, name); |
|
|
|
|
|
|
|
|
|
///TODO: Is it really a good idea to enable all events?
|
|
|
|
|
libevdev_enable_event_type(device, EV_KEY); |
|
|
|
|
for(unsigned int i = 0; i < KEY_CNT; i++) |
|
|
|
|
libevdev_enable_event_code(device, EV_KEY, i, nullptr); |
|
|
|
|
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); |
|
|
|
|
} else { |
|
|
|
|
registered_keys[i] = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
libevdev_enable_event_type(device, EV_REL); |
|
|
|
|
for(unsigned int i = 0; i < REL_CNT; i++) |
|
|
|
|
libevdev_enable_event_code(device, EV_REL, i, nullptr); |
|
|
|
@ -54,8 +63,10 @@ InputDevice::InputDevice(const char* name) |
|
|
|
|
int err = libevdev_uinput_create_from_device(device, |
|
|
|
|
LIBEVDEV_UINPUT_OPEN_MANAGED, &ui_device); |
|
|
|
|
|
|
|
|
|
if(err != 0) |
|
|
|
|
if(err != 0) { |
|
|
|
|
libevdev_free(device); |
|
|
|
|
throw std::system_error(-err, std::generic_category()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
InputDevice::~InputDevice() |
|
|
|
@ -64,6 +75,28 @@ InputDevice::~InputDevice() |
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
_sendEvent(EV_REL, axis, movement); |
|
|
|
@ -119,4 +152,4 @@ void InputDevice::_sendEvent(uint type, uint code, int value) |
|
|
|
|
{ |
|
|
|
|
libevdev_uinput_write_event(ui_device, type, code, value); |
|
|
|
|
libevdev_uinput_write_event(ui_device, EV_SYN, SYN_REPORT, 0); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|