Handle race condition in InputDevice

Lock mutex when sending events and changing input device.
This commit is contained in:
pixl 2023-05-02 18:08:49 -04:00
parent 27b6a2fd8f
commit 5e47bc6ae4
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
2 changed files with 5 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#include <InputDevice.h>
#include <system_error>
#include <mutex>
extern "C"
{
@ -159,6 +160,7 @@ uint InputDevice::_toEventCode(uint type, const std::string& name) {
}
void InputDevice::_enableEvent(const uint type, const uint code) {
std::unique_lock lock(_input_mutex);
libevdev_uinput_destroy(ui_device);
libevdev_enable_event_code(device, type, code, nullptr);
@ -175,6 +177,7 @@ void InputDevice::_enableEvent(const uint type, const uint code) {
}
void InputDevice::_sendEvent(uint type, uint code, int value) {
std::unique_lock lock(_input_mutex);
libevdev_uinput_write_event(ui_device, type, code, value);
libevdev_uinput_write_event(ui_device, EV_SYN, SYN_REPORT, 0);
}

View File

@ -80,6 +80,8 @@ namespace logid {
bool registered_axis[REL_CNT]{};
libevdev* device;
libevdev_uinput* ui_device{};
std::mutex _input_mutex;
};
}