evdev: improve latency

This commit is contained in:
Megamouse 2022-03-20 00:19:44 +01:00
parent 39201e5042
commit 3972022fa2
3 changed files with 60 additions and 23 deletions

View File

@ -294,13 +294,24 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
}
libevdev* dev = device->device;
// Try to query the latest event from the joystick.
// Try to fetch all new events from the joystick.
input_event evt;
int ret = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &evt);
bool has_new_event = false;
int ret = LIBEVDEV_READ_STATUS_SUCCESS;
while (ret >= 0)
{
if (ret == LIBEVDEV_READ_STATUS_SYNC)
{
// Grab any pending sync event.
ret = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_SYNC, &evt);
}
else
{
ret = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &evt);
}
// Grab any pending sync event.
if (ret == LIBEVDEV_READ_STATUS_SYNC)
ret = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL | LIBEVDEV_READ_FLAG_SYNC, &evt);
has_new_event |= ret == LIBEVDEV_READ_STATUS_SUCCESS;
}
auto data = GetButtonValues(device);
@ -329,14 +340,18 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
}
// return if nothing new has happened. ignore this to get the current state for blacklist
if (!get_blacklist && ret < 0)
if (!get_blacklist && !has_new_event)
{
if (callback)
callback(0, "", padId, 0, preview_values);
return;
}
std::pair<u16, std::string> pressed_button = { 0, "" };
struct
{
u16 value = 0;
std::string name;
} pressed_button;
for (const auto& [code, name] : button_list)
{
@ -359,8 +374,10 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
m_blacklist.emplace_back(name);
evdev_log.error("Evdev Calibration: Added button [ %d = %s = %s ] to blacklist. Value = %d", code, libevdev_event_code_get_name(EV_KEY, code), name, value);
}
else if (value > pressed_button.first)
else if (value > pressed_button.value)
{
pressed_button = { value, name };
}
}
}
@ -382,8 +399,10 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
m_blacklist.emplace_back(name);
evdev_log.error("Evdev Calibration: Added axis [ %d = %s = %s ] to blacklist. [ Value = %d ] [ Min = %d ] [ Max = %d ]", code, libevdev_event_code_get_name(EV_ABS, code), name, value, min, max);
}
else if (value > pressed_button.first)
else if (value > pressed_button.value)
{
pressed_button = { value, name };
}
}
}
@ -405,8 +424,10 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
m_blacklist.emplace_back(name);
evdev_log.error("Evdev Calibration: Added rev axis [ %d = %s = %s ] to blacklist. [ Value = %d ] [ Min = %d ] [ Max = %d ]", code, libevdev_event_code_get_name(EV_ABS, code), name, value, min, max);
}
else if (value > pressed_button.first)
else if (value > pressed_button.value)
{
pressed_button = { value, name };
}
}
}
@ -419,8 +440,8 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
if (callback)
{
if (pressed_button.first > 0)
return callback(pressed_button.first, pressed_button.second, padId, 0, preview_values);
if (pressed_button.value > 0)
return callback(pressed_button.value, pressed_button.name, padId, 0, preview_values);
else
return callback(0, "", padId, 0, preview_values);
}
@ -725,15 +746,25 @@ void evdev_joystick_handler::get_mapping(const std::shared_ptr<PadDevice>& devic
if (dev == nullptr)
return;
// Try to query the latest event from the joystick.
// Try to fetch all new events from the joystick.
input_event evt;
int ret = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &evt);
// Grab any pending sync event.
if (ret == LIBEVDEV_READ_STATUS_SYNC)
int ret = LIBEVDEV_READ_STATUS_SUCCESS;
while (ret >= 0)
{
evdev_log.notice("Captured sync event");
ret = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL | LIBEVDEV_READ_FLAG_SYNC, &evt);
if (ret == LIBEVDEV_READ_STATUS_SYNC)
{
// Grab any pending sync event.
ret = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL | LIBEVDEV_READ_FLAG_SYNC, &evt);
}
else
{
ret = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &evt);
}
if (ret == LIBEVDEV_READ_STATUS_SUCCESS)
{
handle_input_event(evt, pad);
}
}
if (ret < 0)
@ -743,6 +774,12 @@ void evdev_joystick_handler::get_mapping(const std::shared_ptr<PadDevice>& devic
evdev_log.error("Failed to read latest event from joystick: %s [errno %d]", strerror(-ret), -ret);
return;
}
}
void evdev_joystick_handler::handle_input_event(const input_event& evt, const std::shared_ptr<Pad>& pad)
{
if (!pad)
return;
m_dev->cur_type = evt.type;
@ -875,8 +912,6 @@ void evdev_joystick_handler::get_mapping(const std::shared_ptr<PadDevice>& devic
pad->m_sticks[1].m_value = 255 - ly;
pad->m_sticks[2].m_value = rx;
pad->m_sticks[3].m_value = 255 - ry;
return;
}
void evdev_joystick_handler::apply_pad_data(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad)

View File

@ -392,6 +392,8 @@ private:
bool check_button(const EvdevButton& b, const u32 code);
bool check_buttons(const std::vector<EvdevButton>& b, const u32 code);
void handle_input_event(const input_event& evt, const std::shared_ptr<Pad>& pad);
protected:
PadHandlerBase::connection update_connection(const std::shared_ptr<PadDevice>& device) override;
void get_mapping(const std::shared_ptr<PadDevice>& device, const std::shared_ptr<Pad>& pad) override;

View File

@ -362,7 +362,7 @@ void pad_settings_dialog::InitButtons()
});
// Enable Button Remapping
const auto& callback = [this](u16 val, std::string name, std::string pad_name, u32 battery_level, pad_preview_values preview_values)
const auto callback = [this](u16 val, std::string name, std::string pad_name, u32 battery_level, pad_preview_values preview_values)
{
SwitchPadInfo(pad_name, true);
@ -408,7 +408,7 @@ void pad_settings_dialog::InitButtons()
};
// Disable Button Remapping
const auto& fail_callback = [this](const std::string& pad_name)
const auto fail_callback = [this](const std::string& pad_name)
{
SwitchPadInfo(pad_name, false);