From 3972022fa239a776715a1f21380ad278559111be Mon Sep 17 00:00:00 2001 From: Megamouse Date: Sun, 20 Mar 2022 00:19:44 +0100 Subject: [PATCH] evdev: improve latency --- rpcs3/Input/evdev_joystick_handler.cpp | 77 +++++++++++++++++++------- rpcs3/Input/evdev_joystick_handler.h | 2 + rpcs3/rpcs3qt/pad_settings_dialog.cpp | 4 +- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/rpcs3/Input/evdev_joystick_handler.cpp b/rpcs3/Input/evdev_joystick_handler.cpp index 98d51c2fdd..04bd0f228b 100644 --- a/rpcs3/Input/evdev_joystick_handler.cpp +++ b/rpcs3/Input/evdev_joystick_handler.cpp @@ -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 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& 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& 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) +{ + if (!pad) + return; m_dev->cur_type = evt.type; @@ -875,8 +912,6 @@ void evdev_joystick_handler::get_mapping(const std::shared_ptr& 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& device, const std::shared_ptr& pad) diff --git a/rpcs3/Input/evdev_joystick_handler.h b/rpcs3/Input/evdev_joystick_handler.h index 9312adc5e6..bf242bb968 100644 --- a/rpcs3/Input/evdev_joystick_handler.h +++ b/rpcs3/Input/evdev_joystick_handler.h @@ -392,6 +392,8 @@ private: bool check_button(const EvdevButton& b, const u32 code); bool check_buttons(const std::vector& b, const u32 code); + void handle_input_event(const input_event& evt, const std::shared_ptr& pad); + protected: PadHandlerBase::connection update_connection(const std::shared_ptr& device) override; void get_mapping(const std::shared_ptr& device, const std::shared_ptr& pad) override; diff --git a/rpcs3/rpcs3qt/pad_settings_dialog.cpp b/rpcs3/rpcs3qt/pad_settings_dialog.cpp index d43de66779..99054ca5a1 100644 --- a/rpcs3/rpcs3qt/pad_settings_dialog.cpp +++ b/rpcs3/rpcs3qt/pad_settings_dialog.cpp @@ -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);