From d583e01f54a7f53d1955f8bc76169c02b807df2e Mon Sep 17 00:00:00 2001 From: german Date: Thu, 31 Dec 2020 20:40:55 -0600 Subject: Add multitouch support --- .../hle/service/hid/controllers/touchscreen.cpp | 97 +++++++++++++++++----- src/core/hle/service/hid/controllers/touchscreen.h | 19 ++++- 2 files changed, 93 insertions(+), 23 deletions(-) diff --git a/src/core/hle/service/hid/controllers/touchscreen.cpp b/src/core/hle/service/hid/controllers/touchscreen.cpp index 0df395e85..de8315ce4 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.cpp +++ b/src/core/hle/service/hid/controllers/touchscreen.cpp @@ -40,29 +40,43 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; - bool pressed = false; - float x, y; - std::tie(x, y, pressed) = touch_device->GetStatus(); - auto& touch_entry = cur_entry.states[0]; - touch_entry.attribute.raw = 0; - if (!pressed && touch_btn_device) { - std::tie(x, y, pressed) = touch_btn_device->GetStatus(); - } - if (pressed && Settings::values.touchscreen.enabled) { - touch_entry.x = static_cast(x * Layout::ScreenUndocked::Width); - touch_entry.y = static_cast(y * Layout::ScreenUndocked::Height); - touch_entry.diameter_x = Settings::values.touchscreen.diameter_x; - touch_entry.diameter_y = Settings::values.touchscreen.diameter_y; - touch_entry.rotation_angle = Settings::values.touchscreen.rotation_angle; - const u64 tick = core_timing.GetCPUTicks(); - touch_entry.delta_time = tick - last_touch; - last_touch = tick; - touch_entry.finger = Settings::values.touchscreen.finger; - cur_entry.entry_count = 1; - } else { - cur_entry.entry_count = 0; + updateTouchInputEvent(touch_device->GetStatus(), mouse_finger_id); + updateTouchInputEvent(touch_btn_device->GetStatus(), keyboar_finger_id); + + std::array sorted_fingers; + s32_le active_fingers = 0; + for (Finger finger : fingers) { + if (finger.pressed) { + sorted_fingers[active_fingers++] = finger; + } } + const u64 tick = core_timing.GetCPUTicks(); + cur_entry.entry_count = active_fingers; + for (size_t id = 0; id < MAX_FINGERS; id++) { + auto& touch_entry = cur_entry.states[id]; + if (id < active_fingers) { + touch_entry.x = static_cast(sorted_fingers[id].x * Layout::ScreenUndocked::Width); + touch_entry.y = static_cast(sorted_fingers[id].y * Layout::ScreenUndocked::Height); + touch_entry.diameter_x = Settings::values.touchscreen.diameter_x; + touch_entry.diameter_y = Settings::values.touchscreen.diameter_y; + touch_entry.rotation_angle = Settings::values.touchscreen.rotation_angle; + touch_entry.delta_time = tick - sorted_fingers[id].last_touch; + sorted_fingers[id].last_touch = tick; + touch_entry.finger = sorted_fingers[id].id; + touch_entry.attribute.raw = sorted_fingers[id].attribute.raw; + } else { + // Clear touch entry + touch_entry.attribute.raw = 0; + touch_entry.x = 0; + touch_entry.y = 0; + touch_entry.diameter_x = 0; + touch_entry.diameter_y = 0; + touch_entry.rotation_angle = 0; + touch_entry.delta_time = 0; + touch_entry.finger = 0; + } + } std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(TouchScreenSharedMemory)); } @@ -74,4 +88,45 @@ void Controller_Touchscreen::OnLoadInputDevices() { touch_btn_device.reset(); } } + +void Controller_Touchscreen::updateTouchInputEvent( + const std::tuple& touch_input, int& finger_id) { + bool pressed = false; + float x, y; + std::tie(x, y, pressed) = touch_input; + if (pressed) { + if (finger_id == -1) { + int first_free_id = 0; + int found = false; + while (!found && first_free_id < MAX_FINGERS) { + if (!fingers[first_free_id].pressed) { + found = true; + } else { + first_free_id++; + } + } + if (found) { + finger_id = first_free_id; + fingers[finger_id].x = x; + fingers[finger_id].y = y; + fingers[finger_id].pressed = true; + fingers[finger_id].id = finger_id; + fingers[finger_id].attribute.start_touch.Assign(1); + } + } else { + fingers[finger_id].x = x; + fingers[finger_id].y = y; + fingers[finger_id].attribute.raw = 0; + } + } else if (finger_id != -1) { + if (!fingers[finger_id].attribute.end_touch) { + fingers[finger_id].attribute.end_touch.Assign(1); + fingers[finger_id].attribute.start_touch.Assign(0); + } else { + fingers[finger_id].pressed = false; + finger_id = -1; + } + } +} + } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/touchscreen.h b/src/core/hle/service/hid/controllers/touchscreen.h index 4d9042adc..6c7620420 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.h +++ b/src/core/hle/service/hid/controllers/touchscreen.h @@ -30,6 +30,9 @@ public: void OnLoadInputDevices() override; private: + void updateTouchInputEvent(const std::tuple& touch_input, int& finger_id); + static const size_t MAX_FINGERS = 16; + struct Attributes { union { u32 raw{}; @@ -55,7 +58,7 @@ private: s64_le sampling_number; s64_le sampling_number2; s32_le entry_count; - std::array states; + std::array states; }; static_assert(sizeof(TouchScreenEntry) == 0x298, "TouchScreenEntry is an invalid size"); @@ -66,9 +69,21 @@ private: }; static_assert(sizeof(TouchScreenSharedMemory) == 0x3000, "TouchScreenSharedMemory is an invalid size"); + + struct Finger { + u64_le last_touch{}; + float x{}; + float y{}; + u32_le id{}; + bool pressed{}; + Attributes attribute; + }; + TouchScreenSharedMemory shared_memory{}; std::unique_ptr touch_device; std::unique_ptr touch_btn_device; - s64_le last_touch{}; + int mouse_finger_id{-1}; + int keyboar_finger_id{-1}; + std::array fingers; }; } // namespace Service::HID -- cgit v1.2.3 From 390ee10eefea4249aff94eb5351a908e3cafe228 Mon Sep 17 00:00:00 2001 From: german Date: Fri, 1 Jan 2021 10:40:02 -0600 Subject: Allow all touch inputs at the same time and remove config options that are not longer necesary --- .../hle/service/hid/controllers/touchscreen.cpp | 16 ++++---- src/core/hle/service/hid/controllers/touchscreen.h | 15 ++++++-- src/yuzu/configuration/config.cpp | 9 ----- src/yuzu/configuration/configure_motion_touch.cpp | 43 +++++----------------- src/yuzu/configuration/configure_motion_touch.ui | 16 +------- .../configure_touchscreen_advanced.cpp | 3 -- .../configure_touchscreen_advanced.ui | 29 +++------------ src/yuzu_cmd/config.cpp | 4 -- 8 files changed, 36 insertions(+), 99 deletions(-) diff --git a/src/core/hle/service/hid/controllers/touchscreen.cpp b/src/core/hle/service/hid/controllers/touchscreen.cpp index de8315ce4..13f75b48a 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.cpp +++ b/src/core/hle/service/hid/controllers/touchscreen.cpp @@ -40,11 +40,12 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; - updateTouchInputEvent(touch_device->GetStatus(), mouse_finger_id); - updateTouchInputEvent(touch_btn_device->GetStatus(), keyboar_finger_id); + updateTouchInputEvent(touch_mouse_device->GetStatus(), mouse_finger_id); + updateTouchInputEvent(touch_btn_device->GetStatus(), keyboard_finger_id); + updateTouchInputEvent(touch_udp_device->GetStatus(), udp_finger_id); std::array sorted_fingers; - s32_le active_fingers = 0; + size_t active_fingers = 0; for (Finger finger : fingers) { if (finger.pressed) { sorted_fingers[active_fingers++] = finger; @@ -52,7 +53,7 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin } const u64 tick = core_timing.GetCPUTicks(); - cur_entry.entry_count = active_fingers; + cur_entry.entry_count = static_cast(active_fingers); for (size_t id = 0; id < MAX_FINGERS; id++) { auto& touch_entry = cur_entry.states[id]; if (id < active_fingers) { @@ -81,7 +82,8 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin } void Controller_Touchscreen::OnLoadInputDevices() { - touch_device = Input::CreateDevice(Settings::values.touchscreen.device); + touch_mouse_device = Input::CreateDevice("engine:emu_window"); + touch_udp_device = Input::CreateDevice("engine:cemuhookudp"); if (Settings::values.use_touch_from_button) { touch_btn_device = Input::CreateDevice("engine:touch_from_button"); } else { @@ -90,7 +92,7 @@ void Controller_Touchscreen::OnLoadInputDevices() { } void Controller_Touchscreen::updateTouchInputEvent( - const std::tuple& touch_input, int& finger_id) { + const std::tuple& touch_input, size_t& finger_id) { bool pressed = false; float x, y; std::tie(x, y, pressed) = touch_input; @@ -110,7 +112,7 @@ void Controller_Touchscreen::updateTouchInputEvent( fingers[finger_id].x = x; fingers[finger_id].y = y; fingers[finger_id].pressed = true; - fingers[finger_id].id = finger_id; + fingers[finger_id].id = static_cast(finger_id); fingers[finger_id].attribute.start_touch.Assign(1); } } else { diff --git a/src/core/hle/service/hid/controllers/touchscreen.h b/src/core/hle/service/hid/controllers/touchscreen.h index 6c7620420..03f399344 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.h +++ b/src/core/hle/service/hid/controllers/touchscreen.h @@ -30,7 +30,12 @@ public: void OnLoadInputDevices() override; private: - void updateTouchInputEvent(const std::tuple& touch_input, int& finger_id); + // If the touch is new it tries to assing a new finger id, if there is no fingers avaliable no + // changes will be made. Updates the coordinates if the finger id it's already set. If the touch + // ends delays the output by one frame to set the end_touch flag before finally freeing the + // finger id + void updateTouchInputEvent(const std::tuple& touch_input, + size_t& finger_id); static const size_t MAX_FINGERS = 16; struct Attributes { @@ -80,10 +85,12 @@ private: }; TouchScreenSharedMemory shared_memory{}; - std::unique_ptr touch_device; + std::unique_ptr touch_mouse_device; + std::unique_ptr touch_udp_device; std::unique_ptr touch_btn_device; - int mouse_finger_id{-1}; - int keyboar_finger_id{-1}; + size_t mouse_finger_id{-1}; + size_t keyboard_finger_id{-1}; + size_t udp_finger_id{-1}; std::array fingers; }; } // namespace Service::HID diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index cda448718..0ec358225 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -464,13 +464,7 @@ void Config::ReadMouseValues() { void Config::ReadTouchscreenValues() { Settings::values.touchscreen.enabled = ReadSetting(QStringLiteral("touchscreen_enabled"), true).toBool(); - Settings::values.touchscreen.device = - ReadSetting(QStringLiteral("touchscreen_device"), QStringLiteral("engine:emu_window")) - .toString() - .toStdString(); - Settings::values.touchscreen.finger = - ReadSetting(QStringLiteral("touchscreen_finger"), 0).toUInt(); Settings::values.touchscreen.rotation_angle = ReadSetting(QStringLiteral("touchscreen_angle"), 0).toUInt(); Settings::values.touchscreen.diameter_x = @@ -1087,10 +1081,7 @@ void Config::SaveTouchscreenValues() { const auto& touchscreen = Settings::values.touchscreen; WriteSetting(QStringLiteral("touchscreen_enabled"), touchscreen.enabled, true); - WriteSetting(QStringLiteral("touchscreen_device"), QString::fromStdString(touchscreen.device), - QStringLiteral("engine:emu_window")); - WriteSetting(QStringLiteral("touchscreen_finger"), touchscreen.finger, 0); WriteSetting(QStringLiteral("touchscreen_angle"), touchscreen.rotation_angle, 0); WriteSetting(QStringLiteral("touchscreen_diameter_x"), touchscreen.diameter_x, 15); WriteSetting(QStringLiteral("touchscreen_diameter_y"), touchscreen.diameter_y, 15); diff --git a/src/yuzu/configuration/configure_motion_touch.cpp b/src/yuzu/configuration/configure_motion_touch.cpp index caaa85930..1f2b792e4 100644 --- a/src/yuzu/configuration/configure_motion_touch.cpp +++ b/src/yuzu/configuration/configure_motion_touch.cpp @@ -81,19 +81,11 @@ void CalibrationConfigurationDialog::UpdateButtonText(const QString& text) { cancel_button->setText(text); } -constexpr std::array, 2> TouchProviders = {{ - {"emu_window", QT_TRANSLATE_NOOP("ConfigureMotionTouch", "Emulator Window")}, - {"cemuhookudp", QT_TRANSLATE_NOOP("ConfigureMotionTouch", "CemuhookUDP")}, -}}; - ConfigureMotionTouch::ConfigureMotionTouch(QWidget* parent, InputCommon::InputSubsystem* input_subsystem_) : QDialog(parent), input_subsystem{input_subsystem_}, ui(std::make_unique()) { ui->setupUi(this); - for (const auto& [provider, name] : TouchProviders) { - ui->touch_provider->addItem(tr(name), QString::fromUtf8(provider)); - } ui->udp_learn_more->setOpenExternalLinks(true); ui->udp_learn_more->setText( @@ -112,10 +104,7 @@ ConfigureMotionTouch::~ConfigureMotionTouch() = default; void ConfigureMotionTouch::SetConfiguration() { const Common::ParamPackage motion_param(Settings::values.motion_device); const Common::ParamPackage touch_param(Settings::values.touch_device); - const std::string touch_engine = touch_param.Get("engine", "emu_window"); - ui->touch_provider->setCurrentIndex( - ui->touch_provider->findData(QString::fromStdString(touch_engine))); ui->touch_from_button_checkbox->setChecked(Settings::values.use_touch_from_button); touch_from_button_maps = Settings::values.touch_from_button_maps; for (const auto& touch_map : touch_from_button_maps) { @@ -148,30 +137,21 @@ void ConfigureMotionTouch::SetConfiguration() { } void ConfigureMotionTouch::UpdateUiDisplay() { - const QString touch_engine = ui->touch_provider->currentData().toString(); const QString cemuhook_udp = QStringLiteral("cemuhookudp"); ui->motion_sensitivity_label->setVisible(true); ui->motion_sensitivity->setVisible(true); - if (touch_engine == cemuhook_udp) { - ui->touch_calibration->setVisible(true); - ui->touch_calibration_config->setVisible(true); - ui->touch_calibration_label->setVisible(true); - ui->touch_calibration->setText( - QStringLiteral("(%1, %2) - (%3, %4)").arg(min_x).arg(min_y).arg(max_x).arg(max_y)); - } else { - ui->touch_calibration->setVisible(false); - ui->touch_calibration_config->setVisible(false); - ui->touch_calibration_label->setVisible(false); - } + ui->touch_calibration->setVisible(true); + ui->touch_calibration_config->setVisible(true); + ui->touch_calibration_label->setVisible(true); + ui->touch_calibration->setText( + QStringLiteral("(%1, %2) - (%3, %4)").arg(min_x).arg(min_y).arg(max_x).arg(max_y)); ui->udp_config_group_box->setVisible(true); } void ConfigureMotionTouch::ConnectEvents() { - connect(ui->touch_provider, qOverload(&QComboBox::currentIndexChanged), this, - [this](int index) { UpdateUiDisplay(); }); connect(ui->udp_test, &QPushButton::clicked, this, &ConfigureMotionTouch::OnCemuhookUDPTest); connect(ui->udp_add, &QPushButton::clicked, this, &ConfigureMotionTouch::OnUDPAddServer); connect(ui->udp_remove, &QPushButton::clicked, this, &ConfigureMotionTouch::OnUDPDeleteServer); @@ -327,16 +307,11 @@ void ConfigureMotionTouch::ApplyConfiguration() { return; } - std::string touch_engine = ui->touch_provider->currentData().toString().toStdString(); - Common::ParamPackage touch_param{}; - if (touch_engine == "cemuhookudp") { - touch_param.Set("min_x", min_x); - touch_param.Set("min_y", min_y); - touch_param.Set("max_x", max_x); - touch_param.Set("max_y", max_y); - } - touch_param.Set("engine", std::move(touch_engine)); + touch_param.Set("min_x", min_x); + touch_param.Set("min_y", min_y); + touch_param.Set("max_x", max_x); + touch_param.Set("max_y", max_y); Settings::values.touch_device = touch_param.Serialize(); Settings::values.use_touch_from_button = ui->touch_from_button_checkbox->isChecked(); diff --git a/src/yuzu/configuration/configure_motion_touch.ui b/src/yuzu/configuration/configure_motion_touch.ui index ebca835ac..1e35ea946 100644 --- a/src/yuzu/configuration/configure_motion_touch.ui +++ b/src/yuzu/configuration/configure_motion_touch.ui @@ -65,26 +65,12 @@ Touch - - - - - - Touch Provider: - - - - - - - - - Calibration: + UDP Calibration: diff --git a/src/yuzu/configuration/configure_touchscreen_advanced.cpp b/src/yuzu/configuration/configure_touchscreen_advanced.cpp index 7d7cc00b7..29c86c7bc 100644 --- a/src/yuzu/configuration/configure_touchscreen_advanced.cpp +++ b/src/yuzu/configuration/configure_touchscreen_advanced.cpp @@ -33,21 +33,18 @@ void ConfigureTouchscreenAdvanced::RetranslateUI() { } void ConfigureTouchscreenAdvanced::ApplyConfiguration() { - Settings::values.touchscreen.finger = ui->finger_box->value(); Settings::values.touchscreen.diameter_x = ui->diameter_x_box->value(); Settings::values.touchscreen.diameter_y = ui->diameter_y_box->value(); Settings::values.touchscreen.rotation_angle = ui->angle_box->value(); } void ConfigureTouchscreenAdvanced::LoadConfiguration() { - ui->finger_box->setValue(Settings::values.touchscreen.finger); ui->diameter_x_box->setValue(Settings::values.touchscreen.diameter_x); ui->diameter_y_box->setValue(Settings::values.touchscreen.diameter_y); ui->angle_box->setValue(Settings::values.touchscreen.rotation_angle); } void ConfigureTouchscreenAdvanced::RestoreDefaults() { - ui->finger_box->setValue(0); ui->diameter_x_box->setValue(15); ui->diameter_y_box->setValue(15); ui->angle_box->setValue(0); diff --git a/src/yuzu/configuration/configure_touchscreen_advanced.ui b/src/yuzu/configuration/configure_touchscreen_advanced.ui index 30ceccddb..88e7cf050 100644 --- a/src/yuzu/configuration/configure_touchscreen_advanced.ui +++ b/src/yuzu/configuration/configure_touchscreen_advanced.ui @@ -65,20 +65,13 @@ - + Touch Diameter Y - - - - Finger - - - @@ -92,37 +85,27 @@ - + Touch Diameter X - - - - - 80 - 0 - - - - - + Rotational Angle - + - + - + diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp index 41ef6f6b8..f76102459 100644 --- a/src/yuzu_cmd/config.cpp +++ b/src/yuzu_cmd/config.cpp @@ -296,10 +296,6 @@ void Config::ReadValues() { sdl2_config->GetBoolean("ControlsGeneral", "motion_enabled", true)); Settings::values.touchscreen.enabled = sdl2_config->GetBoolean("ControlsGeneral", "touch_enabled", true); - Settings::values.touchscreen.device = - sdl2_config->Get("ControlsGeneral", "touch_device", "engine:emu_window"); - Settings::values.touchscreen.finger = - sdl2_config->GetInteger("ControlsGeneral", "touch_finger", 0); Settings::values.touchscreen.rotation_angle = sdl2_config->GetInteger("ControlsGeneral", "touch_angle", 0); Settings::values.touchscreen.diameter_x = -- cgit v1.2.3 From d8df9a16bd4f4517b024c17446a94915493d7f3d Mon Sep 17 00:00:00 2001 From: german Date: Fri, 1 Jan 2021 12:32:29 -0600 Subject: Allow to return up to 16 touch inputs per engine --- src/core/frontend/emu_window.cpp | 8 +- src/core/frontend/input.h | 7 +- .../hle/service/hid/controllers/touchscreen.cpp | 114 ++++++++++--------- src/core/hle/service/hid/controllers/touchscreen.h | 16 ++- src/input_common/touch_from_button.cpp | 15 +-- src/input_common/udp/client.cpp | 121 +++++++++++++-------- src/input_common/udp/client.h | 24 +++- src/input_common/udp/protocol.h | 16 +-- src/input_common/udp/udp.cpp | 32 +----- src/yuzu/configuration/config.cpp | 3 +- 10 files changed, 202 insertions(+), 154 deletions(-) diff --git a/src/core/frontend/emu_window.cpp b/src/core/frontend/emu_window.cpp index 8c1193894..589842917 100644 --- a/src/core/frontend/emu_window.cpp +++ b/src/core/frontend/emu_window.cpp @@ -30,12 +30,14 @@ private: class Device : public Input::TouchDevice { public: explicit Device(std::weak_ptr&& touch_state) : touch_state(touch_state) {} - std::tuple GetStatus() const override { + Input::TouchStatus GetStatus() const override { + Input::TouchStatus touch_status = {}; if (auto state = touch_state.lock()) { std::lock_guard guard{state->mutex}; - return std::make_tuple(state->touch_x, state->touch_y, state->touch_pressed); + touch_status[0] = + std::make_tuple(state->touch_x, state->touch_y, state->touch_pressed); } - return std::make_tuple(0.0f, 0.0f, false); + return touch_status; } private: diff --git a/src/core/frontend/input.h b/src/core/frontend/input.h index de51a754e..f014dfea3 100644 --- a/src/core/frontend/input.h +++ b/src/core/frontend/input.h @@ -163,10 +163,11 @@ using MotionStatus = std::tuple, Common::Vec3, Common using MotionDevice = InputDevice; /** - * A touch status is an object that returns a tuple of two floats and a bool. The floats are - * x and y coordinates in the range 0.0 - 1.0, and the bool indicates whether it is pressed. + * A touch status is an object that returns an array of 16 tuple elements of two floats and a bool. + * The floats are x and y coordinates in the range 0.0 - 1.0, and the bool indicates whether it is + * pressed. */ -using TouchStatus = std::tuple; +using TouchStatus = std::array, 16>; /** * A touch device is an input device that returns a touch status object diff --git a/src/core/hle/service/hid/controllers/touchscreen.cpp b/src/core/hle/service/hid/controllers/touchscreen.cpp index 13f75b48a..dc0d2c712 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.cpp +++ b/src/core/hle/service/hid/controllers/touchscreen.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include #include "common/common_types.h" #include "core/core_timing.h" @@ -16,7 +17,13 @@ constexpr std::size_t SHARED_MEMORY_OFFSET = 0x400; Controller_Touchscreen::Controller_Touchscreen(Core::System& system) : ControllerBase(system) {} Controller_Touchscreen::~Controller_Touchscreen() = default; -void Controller_Touchscreen::OnInit() {} +void Controller_Touchscreen::OnInit() { + for (size_t id = 0; id < MAX_FINGERS; id++) { + mouse_finger_id[id] = MAX_FINGERS; + keyboard_finger_id[id] = MAX_FINGERS; + udp_finger_id[id] = MAX_FINGERS; + } +} void Controller_Touchscreen::OnRelease() {} @@ -40,32 +47,35 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; - updateTouchInputEvent(touch_mouse_device->GetStatus(), mouse_finger_id); - updateTouchInputEvent(touch_btn_device->GetStatus(), keyboard_finger_id); - updateTouchInputEvent(touch_udp_device->GetStatus(), udp_finger_id); - - std::array sorted_fingers; - size_t active_fingers = 0; - for (Finger finger : fingers) { - if (finger.pressed) { - sorted_fingers[active_fingers++] = finger; - } + const Input::TouchStatus& mouse_status = touch_mouse_device->GetStatus(); + const Input::TouchStatus& keyboard_status = touch_btn_device->GetStatus(); + const Input::TouchStatus& udp_status = touch_udp_device->GetStatus(); + for (size_t id = 0; id < mouse_status.size(); id++) { + mouse_finger_id[id] = UpdateTouchInputEvent(mouse_status[id], mouse_finger_id[id]); + keyboard_finger_id[id] = UpdateTouchInputEvent(keyboard_status[id], keyboard_finger_id[id]); + udp_finger_id[id] = UpdateTouchInputEvent(udp_status[id], udp_finger_id[id]); } + std::array active_fingers; + const auto end_iter = std::copy_if(fingers.begin(), fingers.end(), active_fingers.begin(), + [](const auto& finger) { return finger.pressed; }); + const auto active_fingers_count = + static_cast(std::distance(active_fingers.begin(), end_iter)); + const u64 tick = core_timing.GetCPUTicks(); - cur_entry.entry_count = static_cast(active_fingers); + cur_entry.entry_count = static_cast(active_fingers_count); for (size_t id = 0; id < MAX_FINGERS; id++) { auto& touch_entry = cur_entry.states[id]; - if (id < active_fingers) { - touch_entry.x = static_cast(sorted_fingers[id].x * Layout::ScreenUndocked::Width); - touch_entry.y = static_cast(sorted_fingers[id].y * Layout::ScreenUndocked::Height); + if (id < active_fingers_count) { + touch_entry.x = static_cast(active_fingers[id].x * Layout::ScreenUndocked::Width); + touch_entry.y = static_cast(active_fingers[id].y * Layout::ScreenUndocked::Height); touch_entry.diameter_x = Settings::values.touchscreen.diameter_x; touch_entry.diameter_y = Settings::values.touchscreen.diameter_y; touch_entry.rotation_angle = Settings::values.touchscreen.rotation_angle; - touch_entry.delta_time = tick - sorted_fingers[id].last_touch; - sorted_fingers[id].last_touch = tick; - touch_entry.finger = sorted_fingers[id].id; - touch_entry.attribute.raw = sorted_fingers[id].attribute.raw; + touch_entry.delta_time = tick - active_fingers[id].last_touch; + active_fingers[id].last_touch = tick; + touch_entry.finger = active_fingers[id].id; + touch_entry.attribute.raw = active_fingers[id].attribute.raw; } else { // Clear touch entry touch_entry.attribute.raw = 0; @@ -91,44 +101,50 @@ void Controller_Touchscreen::OnLoadInputDevices() { } } -void Controller_Touchscreen::updateTouchInputEvent( - const std::tuple& touch_input, size_t& finger_id) { - bool pressed = false; - float x, y; - std::tie(x, y, pressed) = touch_input; +std::optional Controller_Touchscreen::GetUnusedFingerID() const { + size_t first_free_id = 0; + while (first_free_id < MAX_FINGERS) { + if (!fingers[first_free_id].pressed) { + return first_free_id; + } else { + first_free_id++; + } + } + return std::nullopt; +} + +size_t Controller_Touchscreen::UpdateTouchInputEvent( + const std::tuple& touch_input, size_t finger_id) { + const auto& [x, y, pressed] = touch_input; if (pressed) { - if (finger_id == -1) { - int first_free_id = 0; - int found = false; - while (!found && first_free_id < MAX_FINGERS) { - if (!fingers[first_free_id].pressed) { - found = true; - } else { - first_free_id++; - } + Attributes attribute = {}; + if (finger_id == MAX_FINGERS) { + const auto first_free_id = GetUnusedFingerID(); + if (!first_free_id) { + // Invalid finger id do nothing + return MAX_FINGERS; } - if (found) { - finger_id = first_free_id; - fingers[finger_id].x = x; - fingers[finger_id].y = y; - fingers[finger_id].pressed = true; - fingers[finger_id].id = static_cast(finger_id); - fingers[finger_id].attribute.start_touch.Assign(1); - } - } else { - fingers[finger_id].x = x; - fingers[finger_id].y = y; - fingers[finger_id].attribute.raw = 0; + finger_id = first_free_id.value(); + fingers[finger_id].pressed = true; + fingers[finger_id].id = static_cast(finger_id); + attribute.start_touch.Assign(1); } - } else if (finger_id != -1) { + fingers[finger_id].x = x; + fingers[finger_id].y = y; + fingers[finger_id].attribute = attribute; + return finger_id; + } + + if (finger_id != MAX_FINGERS) { if (!fingers[finger_id].attribute.end_touch) { fingers[finger_id].attribute.end_touch.Assign(1); fingers[finger_id].attribute.start_touch.Assign(0); - } else { - fingers[finger_id].pressed = false; - finger_id = -1; + return finger_id; } + fingers[finger_id].pressed = false; } + + return MAX_FINGERS; } } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/touchscreen.h b/src/core/hle/service/hid/controllers/touchscreen.h index 03f399344..e39ab89ee 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.h +++ b/src/core/hle/service/hid/controllers/touchscreen.h @@ -30,13 +30,17 @@ public: void OnLoadInputDevices() override; private: + static constexpr size_t MAX_FINGERS = 16; + + // Returns an unused finger id, if there is no fingers available std::nullopt will be returned + std::optional GetUnusedFingerID() const; + // If the touch is new it tries to assing a new finger id, if there is no fingers avaliable no // changes will be made. Updates the coordinates if the finger id it's already set. If the touch // ends delays the output by one frame to set the end_touch flag before finally freeing the // finger id - void updateTouchInputEvent(const std::tuple& touch_input, - size_t& finger_id); - static const size_t MAX_FINGERS = 16; + size_t UpdateTouchInputEvent(const std::tuple& touch_input, + size_t finger_id); struct Attributes { union { @@ -88,9 +92,9 @@ private: std::unique_ptr touch_mouse_device; std::unique_ptr touch_udp_device; std::unique_ptr touch_btn_device; - size_t mouse_finger_id{-1}; - size_t keyboard_finger_id{-1}; - size_t udp_finger_id{-1}; + std::array mouse_finger_id; + std::array keyboard_finger_id; + std::array udp_finger_id; std::array fingers; }; } // namespace Service::HID diff --git a/src/input_common/touch_from_button.cpp b/src/input_common/touch_from_button.cpp index a07124a86..5226e70df 100644 --- a/src/input_common/touch_from_button.cpp +++ b/src/input_common/touch_from_button.cpp @@ -25,18 +25,19 @@ public: } } - std::tuple GetStatus() const override { - for (const auto& m : map) { - const bool state = std::get<0>(m)->GetStatus(); + Input::TouchStatus GetStatus() const override { + Input::TouchStatus touch_status = {}; + for (size_t id = 0; id < map.size() && id < touch_status.size(); id++) { + const bool state = std::get<0>(map[id])->GetStatus(); if (state) { - const float x = static_cast(std::get<1>(m)) / + const float x = static_cast(std::get<1>(map[id])) / static_cast(Layout::ScreenUndocked::Width); - const float y = static_cast(std::get<2>(m)) / + const float y = static_cast(std::get<2>(map[id])) / static_cast(Layout::ScreenUndocked::Height); - return {x, y, true}; + touch_status[id] = {x, y, true}; } } - return {}; + return touch_status; } private: diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index 412d57896..53648cb53 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp @@ -136,6 +136,9 @@ static void SocketLoop(Socket* socket) { Client::Client() { LOG_INFO(Input, "Udp Initialization started"); + for (size_t id = 0; id < MAX_TOUCH_FINGERS; id++) { + finger_id[id] = MAX_UDP_CLIENTS * 2; + } ReloadSockets(); } @@ -176,7 +179,7 @@ void Client::ReloadSockets() { std::string server_token; std::size_t client = 0; while (std::getline(servers_ss, server_token, ',')) { - if (client == max_udp_clients) { + if (client == MAX_UDP_CLIENTS) { break; } std::stringstream server_ss(server_token); @@ -194,7 +197,7 @@ void Client::ReloadSockets() { for (std::size_t pad = 0; pad < 4; ++pad) { const std::size_t client_number = GetClientNumber(udp_input_address, udp_input_port, pad); - if (client_number != max_udp_clients) { + if (client_number != MAX_UDP_CLIENTS) { LOG_ERROR(Input, "Duplicated UDP servers found"); continue; } @@ -213,7 +216,7 @@ std::size_t Client::GetClientNumber(std::string_view host, u16 port, std::size_t return client; } } - return max_udp_clients; + return MAX_UDP_CLIENTS; } void Client::OnVersion([[maybe_unused]] Response::Version data) { @@ -259,33 +262,14 @@ void Client::OnPadData(Response::PadData data, std::size_t client) { std::lock_guard guard(clients[client].status.update_mutex); clients[client].status.motion_status = clients[client].motion.GetMotion(); - // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates - // between a simple "tap" and a hard press that causes the touch screen to click. - const bool is_active = data.touch_1.is_active != 0; - - float x = 0; - float y = 0; - - if (is_active && clients[client].status.touch_calibration) { - const u16 min_x = clients[client].status.touch_calibration->min_x; - const u16 max_x = clients[client].status.touch_calibration->max_x; - const u16 min_y = clients[client].status.touch_calibration->min_y; - const u16 max_y = clients[client].status.touch_calibration->max_y; - - x = static_cast(std::clamp(static_cast(data.touch_1.x), min_x, max_x) - - min_x) / - static_cast(max_x - min_x); - y = static_cast(std::clamp(static_cast(data.touch_1.y), min_y, max_y) - - min_y) / - static_cast(max_y - min_y); + for (size_t id = 0; id < data.touch.size(); id++) { + UpdateTouchInput(data.touch[id], client, id); } - clients[client].status.touch_status = {x, y, is_active}; - if (configuring) { const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope(); const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration(); - UpdateYuzuSettings(client, accelerometer, gyroscope, is_active); + UpdateYuzuSettings(client, accelerometer, gyroscope); } } } @@ -320,20 +304,16 @@ void Client::Reset() { } void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3& acc, - const Common::Vec3& gyro, bool touch) { + const Common::Vec3& gyro) { if (gyro.Length() > 0.2f) { - LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {}), touch={}", - client, gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2], touch); + LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {})", client, + gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2]); } UDPPadStatus pad{ .host = clients[client].host, .port = clients[client].port, .pad_index = clients[client].pad_index, }; - if (touch) { - pad.touch = PadTouch::Click; - pad_queue.Push(pad); - } for (size_t i = 0; i < 3; ++i) { if (gyro[i] > 5.0f || gyro[i] < -5.0f) { pad.motion = static_cast(i); @@ -348,6 +328,53 @@ void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3& a } } +std::optional Client::GetUnusedFingerID() const { + size_t first_free_id = 0; + while (first_free_id < MAX_TOUCH_FINGERS) { + if (!std::get<2>(touch_status[first_free_id])) { + return first_free_id; + } else { + first_free_id++; + } + } + return std::nullopt; +} + +void Client::UpdateTouchInput(Response::TouchPad& touch_pad, size_t client, size_t id) { + // TODO: Use custom calibration per device + const Common::ParamPackage touch_param(Settings::values.touch_device); + const u16 min_x = static_cast(touch_param.Get("min_x", 100)); + const u16 min_y = static_cast(touch_param.Get("min_y", 50)); + const u16 max_x = static_cast(touch_param.Get("max_x", 1800)); + const u16 max_y = static_cast(touch_param.Get("max_y", 850)); + + if (touch_pad.is_active) { + if (finger_id[client * 2 + id] == MAX_TOUCH_FINGERS) { + const auto first_free_id = GetUnusedFingerID(); + if (!first_free_id) { + // Invalid finger id skip to next input + return; + } + finger_id[client * 2 + id] = first_free_id.value(); + } + auto& [x, y, pressed] = touch_status[finger_id[client * 2 + id]]; + x = static_cast(std::clamp(static_cast(touch_pad.x), min_x, max_x) - min_x) / + static_cast(max_x - min_x); + y = static_cast(std::clamp(static_cast(touch_pad.y), min_y, max_y) - min_y) / + static_cast(max_y - min_y); + pressed = true; + return; + } + + if (finger_id[client * 2 + id] != MAX_TOUCH_FINGERS) { + auto& [x, y, pressed] = touch_status[finger_id[client * 2 + id]]; + x = 0; + y = 0; + pressed = false; + finger_id[client * 2 + id] = MAX_TOUCH_FINGERS; + } +} + void Client::BeginConfiguration() { pad_queue.Clear(); configuring = true; @@ -360,7 +387,7 @@ void Client::EndConfiguration() { DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) { const std::size_t client_number = GetClientNumber(host, port, pad); - if (client_number == max_udp_clients) { + if (client_number == MAX_UDP_CLIENTS) { return clients[0].status; } return clients[client_number].status; @@ -368,12 +395,20 @@ DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t const DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) const { const std::size_t client_number = GetClientNumber(host, port, pad); - if (client_number == max_udp_clients) { + if (client_number == MAX_UDP_CLIENTS) { return clients[0].status; } return clients[client_number].status; } +Input::TouchStatus& Client::GetTouchState() { + return touch_status; +} + +const Input::TouchStatus& Client::GetTouchState() const { + return touch_status; +} + Common::SPSCQueue& Client::GetPadQueue() { return pad_queue; } @@ -426,24 +461,24 @@ CalibrationConfigurationJob::CalibrationConfigurationJob( current_status = Status::Ready; status_callback(current_status); } - if (data.touch_1.is_active == 0) { + if (data.touch[0].is_active == 0) { return; } - LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, - data.touch_1.y); - min_x = std::min(min_x, static_cast(data.touch_1.x)); - min_y = std::min(min_y, static_cast(data.touch_1.y)); + LOG_DEBUG(Input, "Current touch: {} {}", data.touch[0].x, + data.touch[0].y); + min_x = std::min(min_x, static_cast(data.touch[0].x)); + min_y = std::min(min_y, static_cast(data.touch[0].y)); if (current_status == Status::Ready) { // First touch - min data (min_x/min_y) current_status = Status::Stage1Completed; status_callback(current_status); } - if (data.touch_1.x - min_x > CALIBRATION_THRESHOLD && - data.touch_1.y - min_y > CALIBRATION_THRESHOLD) { + if (data.touch[0].x - min_x > CALIBRATION_THRESHOLD && + data.touch[0].y - min_y > CALIBRATION_THRESHOLD) { // Set the current position as max value and finishes // configuration - max_x = data.touch_1.x; - max_y = data.touch_1.y; + max_x = data.touch[0].x; + max_y = data.touch[0].y; current_status = Status::Completed; data_callback(min_x, min_y, max_x, max_y); status_callback(current_status); diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h index 00c8b09f5..1cd251ec8 100644 --- a/src/input_common/udp/client.h +++ b/src/input_common/udp/client.h @@ -29,6 +29,7 @@ namespace Response { struct PadData; struct PortInfo; struct Version; +struct TouchPad; } // namespace Response enum class PadMotion { @@ -50,7 +51,6 @@ struct UDPPadStatus { std::string host{"127.0.0.1"}; u16 port{26760}; std::size_t pad_index{}; - PadTouch touch{PadTouch::Undefined}; PadMotion motion{PadMotion::Undefined}; f32 motion_value{0.0f}; }; @@ -93,6 +93,9 @@ public: DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad); const DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad) const; + Input::TouchStatus& GetTouchState(); + const Input::TouchStatus& GetTouchState() const; + private: struct ClientData { std::string host{"127.0.0.1"}; @@ -122,14 +125,25 @@ private: void StartCommunication(std::size_t client, const std::string& host, u16 port, std::size_t pad_index, u32 client_id); void UpdateYuzuSettings(std::size_t client, const Common::Vec3& acc, - const Common::Vec3& gyro, bool touch); + const Common::Vec3& gyro); + + // Returns an unused finger id, if there is no fingers available std::nullopt will be + // returned + std::optional GetUnusedFingerID() const; + + // Merges and updates all touch inputs into the touch_status array + void UpdateTouchInput(Response::TouchPad& touch_pad, size_t client, size_t id); bool configuring = false; // Allocate clients for 8 udp servers - const std::size_t max_udp_clients = 32; - std::array clients; - Common::SPSCQueue pad_queue; + static constexpr std::size_t MAX_UDP_CLIENTS = 4 * 8; + // Each client can have up 2 touch inputs + static constexpr std::size_t MAX_TOUCH_FINGERS = MAX_UDP_CLIENTS * 2; + std::array clients{}; + Common::SPSCQueue pad_queue{}; + Input::TouchStatus touch_status{}; + std::array finger_id{}; }; /// An async job allowing configuration of the touchpad calibration. diff --git a/src/input_common/udp/protocol.h b/src/input_common/udp/protocol.h index fc1aea4b9..a3d276697 100644 --- a/src/input_common/udp/protocol.h +++ b/src/input_common/udp/protocol.h @@ -140,6 +140,14 @@ static_assert(sizeof(PortInfo) == 12, "UDP Response PortInfo struct has wrong si static_assert(std::is_trivially_copyable_v, "UDP Response PortInfo is not trivially copyable"); +struct TouchPad { + u8 is_active{}; + u8 id{}; + u16_le x{}; + u16_le y{}; +}; +static_assert(sizeof(TouchPad) == 6, "UDP Response TouchPad struct has wrong size "); + #pragma pack(push, 1) struct PadData { PortInfo info{}; @@ -190,12 +198,7 @@ struct PadData { u8 button_13{}; } analog_button; - struct TouchPad { - u8 is_active{}; - u8 id{}; - u16_le x{}; - u16_le y{}; - } touch_1, touch_2; + std::array touch; u64_le motion_timestamp; @@ -222,7 +225,6 @@ static_assert(sizeof(Message) == MAX_PACKET_SIZE, static_assert(sizeof(PadData::AnalogButton) == 12, "UDP Response AnalogButton struct has wrong size "); -static_assert(sizeof(PadData::TouchPad) == 6, "UDP Response TouchPad struct has wrong size "); static_assert(sizeof(PadData::Accelerometer) == 12, "UDP Response Accelerometer struct has wrong size "); static_assert(sizeof(PadData::Gyroscope) == 12, "UDP Response Gyroscope struct has wrong size "); diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp index c5da27a38..b630281a0 100644 --- a/src/input_common/udp/udp.cpp +++ b/src/input_common/udp/udp.cpp @@ -78,8 +78,8 @@ public: explicit UDPTouch(std::string ip_, u16 port_, u16 pad_, CemuhookUDP::Client* client_) : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} - std::tuple GetStatus() const override { - return client->GetPadState(ip, port, pad).touch_status; + Input::TouchStatus GetStatus() const override { + return client->GetTouchState(); } private: @@ -107,32 +107,4 @@ std::unique_ptr UDPTouchFactory::Create(const Common::ParamP return std::make_unique(std::move(ip), port, pad, client.get()); } -void UDPTouchFactory::BeginConfiguration() { - polling = true; - client->BeginConfiguration(); -} - -void UDPTouchFactory::EndConfiguration() { - polling = false; - client->EndConfiguration(); -} - -Common::ParamPackage UDPTouchFactory::GetNextInput() { - Common::ParamPackage params; - CemuhookUDP::UDPPadStatus pad; - auto& queue = client->GetPadQueue(); - while (queue.Pop(pad)) { - if (pad.touch == CemuhookUDP::PadTouch::Undefined) { - continue; - } - params.Set("engine", "cemuhookudp"); - params.Set("ip", pad.host); - params.Set("port", static_cast(pad.port)); - params.Set("pad_index", static_cast(pad.pad_index)); - params.Set("touch", static_cast(pad.touch)); - return params; - } - return params; -} - } // namespace InputCommon diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index 0ec358225..5e4d22299 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -557,7 +557,8 @@ void Config::ReadMotionTouchValues() { .toString() .toStdString(); Settings::values.touch_device = - ReadSetting(QStringLiteral("touch_device"), QStringLiteral("engine:emu_window")) + ReadSetting(QStringLiteral("touch_device"), + QStringLiteral("min_x:100,min_y:50,max_x:1800,max_y:850")) .toString() .toStdString(); Settings::values.use_touch_from_button = -- cgit v1.2.3 From 8495e1bd8373fed993975e40c360c87409455e9e Mon Sep 17 00:00:00 2001 From: german Date: Sat, 2 Jan 2021 22:04:50 -0600 Subject: Add mutitouch support for touch screens --- src/core/frontend/emu_window.cpp | 42 +++++++------ src/core/frontend/emu_window.h | 12 ++-- .../hle/service/hid/controllers/touchscreen.cpp | 30 +++++---- src/core/hle/service/hid/controllers/touchscreen.h | 14 ++--- src/input_common/touch_from_button.cpp | 4 +- src/input_common/udp/client.cpp | 19 +++--- src/input_common/udp/client.h | 8 +-- src/yuzu/bootmanager.cpp | 73 ++++++++++++++++------ src/yuzu/bootmanager.h | 8 ++- src/yuzu_cmd/emu_window/emu_window_sdl2.cpp | 12 ++-- 10 files changed, 137 insertions(+), 85 deletions(-) diff --git a/src/core/frontend/emu_window.cpp b/src/core/frontend/emu_window.cpp index 589842917..af6c1633a 100644 --- a/src/core/frontend/emu_window.cpp +++ b/src/core/frontend/emu_window.cpp @@ -21,21 +21,17 @@ public: std::mutex mutex; - bool touch_pressed = false; ///< True if touchpad area is currently pressed, otherwise false - - float touch_x = 0.0f; ///< Touchpad X-position - float touch_y = 0.0f; ///< Touchpad Y-position + Input::TouchStatus status; private: class Device : public Input::TouchDevice { public: explicit Device(std::weak_ptr&& touch_state) : touch_state(touch_state) {} Input::TouchStatus GetStatus() const override { - Input::TouchStatus touch_status = {}; + Input::TouchStatus touch_status{}; if (auto state = touch_state.lock()) { std::lock_guard guard{state->mutex}; - touch_status[0] = - std::make_tuple(state->touch_x, state->touch_y, state->touch_pressed); + touch_status = state->status; } return touch_status; } @@ -81,36 +77,44 @@ std::tuple EmuWindow::ClipToTouchScreen(unsigned new_x, unsi return std::make_tuple(new_x, new_y); } -void EmuWindow::TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y) { - if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) +void EmuWindow::TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y, std::size_t id) { + if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) { return; + } + if (id >= touch_state->status.size()) { + return; + } std::lock_guard guard{touch_state->mutex}; - touch_state->touch_x = + const float x = static_cast(framebuffer_x - framebuffer_layout.screen.left) / static_cast(framebuffer_layout.screen.right - framebuffer_layout.screen.left); - touch_state->touch_y = + const float y = static_cast(framebuffer_y - framebuffer_layout.screen.top) / static_cast(framebuffer_layout.screen.bottom - framebuffer_layout.screen.top); - touch_state->touch_pressed = true; + touch_state->status[id] = std::make_tuple(x, y, true); } -void EmuWindow::TouchReleased() { +void EmuWindow::TouchReleased(std::size_t id) { + if (id >= touch_state->status.size()) { + return; + } std::lock_guard guard{touch_state->mutex}; - touch_state->touch_pressed = false; - touch_state->touch_x = 0; - touch_state->touch_y = 0; + touch_state->status[id] = std::make_tuple(0.0f, 0.0f, false); } -void EmuWindow::TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y) { - if (!touch_state->touch_pressed) +void EmuWindow::TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y, std::size_t id) { + if (id >= touch_state->status.size()) { + return; + } + if (!std::get<2>(touch_state->status[id])) return; if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) std::tie(framebuffer_x, framebuffer_y) = ClipToTouchScreen(framebuffer_x, framebuffer_y); - TouchPressed(framebuffer_x, framebuffer_y); + TouchPressed(framebuffer_x, framebuffer_y, id); } void EmuWindow::UpdateCurrentFramebufferLayout(unsigned width, unsigned height) { diff --git a/src/core/frontend/emu_window.h b/src/core/frontend/emu_window.h index 276d2b906..f8db42ab4 100644 --- a/src/core/frontend/emu_window.h +++ b/src/core/frontend/emu_window.h @@ -117,18 +117,22 @@ public: * Signal that a touch pressed event has occurred (e.g. mouse click pressed) * @param framebuffer_x Framebuffer x-coordinate that was pressed * @param framebuffer_y Framebuffer y-coordinate that was pressed + * @param id Touch event id */ - void TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y); + void TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y, std::size_t id); - /// Signal that a touch released event has occurred (e.g. mouse click released) - void TouchReleased(); + /** Signal that a touch released event has occurred (e.g. mouse click released) + *@param id Touch event id + */ + void TouchReleased(std::size_t id); /** * Signal that a touch movement event has occurred (e.g. mouse was moved over the emu window) * @param framebuffer_x Framebuffer x-coordinate * @param framebuffer_y Framebuffer y-coordinate + * @param id Touch event id */ - void TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y); + void TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y, std::size_t id); /** * Returns currently active configuration. diff --git a/src/core/hle/service/hid/controllers/touchscreen.cpp b/src/core/hle/service/hid/controllers/touchscreen.cpp index dc0d2c712..cd318f25b 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.cpp +++ b/src/core/hle/service/hid/controllers/touchscreen.cpp @@ -18,7 +18,7 @@ Controller_Touchscreen::Controller_Touchscreen(Core::System& system) : Controlle Controller_Touchscreen::~Controller_Touchscreen() = default; void Controller_Touchscreen::OnInit() { - for (size_t id = 0; id < MAX_FINGERS; id++) { + for (std::size_t id = 0; id < MAX_FINGERS; ++id) { mouse_finger_id[id] = MAX_FINGERS; keyboard_finger_id[id] = MAX_FINGERS; udp_finger_id[id] = MAX_FINGERS; @@ -48,23 +48,29 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin cur_entry.sampling_number2 = cur_entry.sampling_number; const Input::TouchStatus& mouse_status = touch_mouse_device->GetStatus(); - const Input::TouchStatus& keyboard_status = touch_btn_device->GetStatus(); const Input::TouchStatus& udp_status = touch_udp_device->GetStatus(); - for (size_t id = 0; id < mouse_status.size(); id++) { + for (std::size_t id = 0; id < mouse_status.size(); ++id) { mouse_finger_id[id] = UpdateTouchInputEvent(mouse_status[id], mouse_finger_id[id]); - keyboard_finger_id[id] = UpdateTouchInputEvent(keyboard_status[id], keyboard_finger_id[id]); udp_finger_id[id] = UpdateTouchInputEvent(udp_status[id], udp_finger_id[id]); } + if (Settings::values.use_touch_from_button) { + const Input::TouchStatus& keyboard_status = touch_btn_device->GetStatus(); + for (std::size_t id = 0; id < mouse_status.size(); ++id) { + keyboard_finger_id[id] = + UpdateTouchInputEvent(keyboard_status[id], keyboard_finger_id[id]); + } + } + std::array active_fingers; const auto end_iter = std::copy_if(fingers.begin(), fingers.end(), active_fingers.begin(), [](const auto& finger) { return finger.pressed; }); const auto active_fingers_count = - static_cast(std::distance(active_fingers.begin(), end_iter)); + static_cast(std::distance(active_fingers.begin(), end_iter)); const u64 tick = core_timing.GetCPUTicks(); cur_entry.entry_count = static_cast(active_fingers_count); - for (size_t id = 0; id < MAX_FINGERS; id++) { + for (std::size_t id = 0; id < MAX_FINGERS; ++id) { auto& touch_entry = cur_entry.states[id]; if (id < active_fingers_count) { touch_entry.x = static_cast(active_fingers[id].x * Layout::ScreenUndocked::Width); @@ -73,7 +79,7 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin touch_entry.diameter_y = Settings::values.touchscreen.diameter_y; touch_entry.rotation_angle = Settings::values.touchscreen.rotation_angle; touch_entry.delta_time = tick - active_fingers[id].last_touch; - active_fingers[id].last_touch = tick; + fingers[active_fingers[id].id].last_touch = tick; touch_entry.finger = active_fingers[id].id; touch_entry.attribute.raw = active_fingers[id].attribute.raw; } else { @@ -101,8 +107,8 @@ void Controller_Touchscreen::OnLoadInputDevices() { } } -std::optional Controller_Touchscreen::GetUnusedFingerID() const { - size_t first_free_id = 0; +std::optional Controller_Touchscreen::GetUnusedFingerID() const { + std::size_t first_free_id = 0; while (first_free_id < MAX_FINGERS) { if (!fingers[first_free_id].pressed) { return first_free_id; @@ -113,11 +119,11 @@ std::optional Controller_Touchscreen::GetUnusedFingerID() const { return std::nullopt; } -size_t Controller_Touchscreen::UpdateTouchInputEvent( - const std::tuple& touch_input, size_t finger_id) { +std::size_t Controller_Touchscreen::UpdateTouchInputEvent( + const std::tuple& touch_input, std::size_t finger_id) { const auto& [x, y, pressed] = touch_input; if (pressed) { - Attributes attribute = {}; + Attributes attribute{}; if (finger_id == MAX_FINGERS) { const auto first_free_id = GetUnusedFingerID(); if (!first_free_id) { diff --git a/src/core/hle/service/hid/controllers/touchscreen.h b/src/core/hle/service/hid/controllers/touchscreen.h index e39ab89ee..784124e25 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.h +++ b/src/core/hle/service/hid/controllers/touchscreen.h @@ -30,17 +30,17 @@ public: void OnLoadInputDevices() override; private: - static constexpr size_t MAX_FINGERS = 16; + static constexpr std::size_t MAX_FINGERS = 16; // Returns an unused finger id, if there is no fingers available std::nullopt will be returned - std::optional GetUnusedFingerID() const; + std::optional GetUnusedFingerID() const; // If the touch is new it tries to assing a new finger id, if there is no fingers avaliable no // changes will be made. Updates the coordinates if the finger id it's already set. If the touch // ends delays the output by one frame to set the end_touch flag before finally freeing the // finger id - size_t UpdateTouchInputEvent(const std::tuple& touch_input, - size_t finger_id); + std::size_t UpdateTouchInputEvent(const std::tuple& touch_input, + std::size_t finger_id); struct Attributes { union { @@ -92,9 +92,9 @@ private: std::unique_ptr touch_mouse_device; std::unique_ptr touch_udp_device; std::unique_ptr touch_btn_device; - std::array mouse_finger_id; - std::array keyboard_finger_id; - std::array udp_finger_id; + std::array mouse_finger_id; + std::array keyboard_finger_id; + std::array udp_finger_id; std::array fingers; }; } // namespace Service::HID diff --git a/src/input_common/touch_from_button.cpp b/src/input_common/touch_from_button.cpp index 5226e70df..ffbe4f2ed 100644 --- a/src/input_common/touch_from_button.cpp +++ b/src/input_common/touch_from_button.cpp @@ -26,8 +26,8 @@ public: } Input::TouchStatus GetStatus() const override { - Input::TouchStatus touch_status = {}; - for (size_t id = 0; id < map.size() && id < touch_status.size(); id++) { + Input::TouchStatus touch_status{}; + for (std::size_t id = 0; id < map.size() && id < touch_status.size(); ++id) { const bool state = std::get<0>(map[id])->GetStatus(); if (state) { const float x = static_cast(std::get<1>(map[id])) / diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index 53648cb53..5e39fdce2 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp @@ -136,8 +136,8 @@ static void SocketLoop(Socket* socket) { Client::Client() { LOG_INFO(Input, "Udp Initialization started"); - for (size_t id = 0; id < MAX_TOUCH_FINGERS; id++) { - finger_id[id] = MAX_UDP_CLIENTS * 2; + for (std::size_t id = 0; id < MAX_TOUCH_FINGERS; ++id) { + finger_id[id] = MAX_TOUCH_FINGERS; } ReloadSockets(); } @@ -262,7 +262,7 @@ void Client::OnPadData(Response::PadData data, std::size_t client) { std::lock_guard guard(clients[client].status.update_mutex); clients[client].status.motion_status = clients[client].motion.GetMotion(); - for (size_t id = 0; id < data.touch.size(); id++) { + for (std::size_t id = 0; id < data.touch.size(); ++id) { UpdateTouchInput(data.touch[id], client, id); } @@ -314,7 +314,7 @@ void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3& a .port = clients[client].port, .pad_index = clients[client].pad_index, }; - for (size_t i = 0; i < 3; ++i) { + for (std::size_t i = 0; i < 3; ++i) { if (gyro[i] > 5.0f || gyro[i] < -5.0f) { pad.motion = static_cast(i); pad.motion_value = gyro[i]; @@ -328,8 +328,8 @@ void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3& a } } -std::optional Client::GetUnusedFingerID() const { - size_t first_free_id = 0; +std::optional Client::GetUnusedFingerID() const { + std::size_t first_free_id = 0; while (first_free_id < MAX_TOUCH_FINGERS) { if (!std::get<2>(touch_status[first_free_id])) { return first_free_id; @@ -340,7 +340,7 @@ std::optional Client::GetUnusedFingerID() const { return std::nullopt; } -void Client::UpdateTouchInput(Response::TouchPad& touch_pad, size_t client, size_t id) { +void Client::UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, std::size_t id) { // TODO: Use custom calibration per device const Common::ParamPackage touch_param(Settings::values.touch_device); const u16 min_x = static_cast(touch_param.Get("min_x", 100)); @@ -367,10 +367,7 @@ void Client::UpdateTouchInput(Response::TouchPad& touch_pad, size_t client, size } if (finger_id[client * 2 + id] != MAX_TOUCH_FINGERS) { - auto& [x, y, pressed] = touch_status[finger_id[client * 2 + id]]; - x = 0; - y = 0; - pressed = false; + touch_status[finger_id[client * 2 + id]] = {}; finger_id[client * 2 + id] = MAX_TOUCH_FINGERS; } } diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h index 1cd251ec8..822f9c550 100644 --- a/src/input_common/udp/client.h +++ b/src/input_common/udp/client.h @@ -28,8 +28,8 @@ class Socket; namespace Response { struct PadData; struct PortInfo; -struct Version; struct TouchPad; +struct Version; } // namespace Response enum class PadMotion { @@ -129,10 +129,10 @@ private: // Returns an unused finger id, if there is no fingers available std::nullopt will be // returned - std::optional GetUnusedFingerID() const; + std::optional GetUnusedFingerID() const; // Merges and updates all touch inputs into the touch_status array - void UpdateTouchInput(Response::TouchPad& touch_pad, size_t client, size_t id); + void UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, std::size_t id); bool configuring = false; @@ -143,7 +143,7 @@ private: std::array clients{}; Common::SPSCQueue pad_queue{}; Input::TouchStatus touch_status{}; - std::array finger_id{}; + std::array finger_id{}; }; /// An async job allowing configuration of the touchpad calibration. diff --git a/src/yuzu/bootmanager.cpp b/src/yuzu/bootmanager.cpp index e6c8f18af..1f91514ef 100644 --- a/src/yuzu/bootmanager.cpp +++ b/src/yuzu/bootmanager.cpp @@ -394,7 +394,7 @@ void GRenderWindow::mousePressEvent(QMouseEvent* event) { input_subsystem->GetMouse()->PressButton(x, y, event->button()); if (event->button() == Qt::LeftButton) { - this->TouchPressed(x, y); + this->TouchPressed(x, y, 0); } emit MouseActivity(); @@ -409,7 +409,7 @@ void GRenderWindow::mouseMoveEvent(QMouseEvent* event) { auto pos = event->pos(); const auto [x, y] = ScaleTouch(pos); input_subsystem->GetMouse()->MouseMove(x, y); - this->TouchMoved(x, y); + this->TouchMoved(x, y, 0); emit MouseActivity(); } @@ -423,36 +423,71 @@ void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) { input_subsystem->GetMouse()->ReleaseButton(event->button()); if (event->button() == Qt::LeftButton) { - this->TouchReleased(); + this->TouchReleased(0); } } void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) { - // TouchBegin always has exactly one touch point, so take the .first() - const auto [x, y] = ScaleTouch(event->touchPoints().first().pos()); - this->TouchPressed(x, y); + QList touch_points = event->touchPoints(); + for (const auto& touch_point : touch_points) { + if (!TouchUpdate(touch_point)) { + TouchStart(touch_point); + } + } } void GRenderWindow::TouchUpdateEvent(const QTouchEvent* event) { - QPointF pos; - int active_points = 0; - - // average all active touch points - for (const auto& tp : event->touchPoints()) { - if (tp.state() & (Qt::TouchPointPressed | Qt::TouchPointMoved | Qt::TouchPointStationary)) { - active_points++; - pos += tp.pos(); + QList touch_points = event->touchPoints(); + for (const auto& touch_point : touch_points) { + if (!TouchUpdate(touch_point)) { + TouchStart(touch_point); } } + // Release all inactive points + for (std::size_t id = 0; id < touch_ids.size(); ++id) { + if (!TouchExist(touch_ids[id], touch_points)) { + touch_ids[id] = 0; + this->TouchReleased(id + 1); + } + } +} - pos /= active_points; +void GRenderWindow::TouchEndEvent() { + for (std::size_t id = 0; id < touch_ids.size(); ++id) { + if (touch_ids[id] != 0) { + touch_ids[id] = 0; + this->TouchReleased(id + 1); + } + } +} - const auto [x, y] = ScaleTouch(pos); - this->TouchMoved(x, y); +bool GRenderWindow::TouchStart(const QTouchEvent::TouchPoint& touch_point) { + for (std::size_t id = 0; id < touch_ids.size(); ++id) { + if (touch_ids[id] == 0) { + touch_ids[id] = touch_point.id() + 1; + const auto [x, y] = ScaleTouch(touch_point.pos()); + this->TouchPressed(x, y, id + 1); + return true; + } + } + return false; } -void GRenderWindow::TouchEndEvent() { - this->TouchReleased(); +bool GRenderWindow::TouchUpdate(const QTouchEvent::TouchPoint& touch_point) { + for (std::size_t id = 0; id < touch_ids.size(); ++id) { + if (touch_ids[id] == touch_point.id() + 1) { + const auto [x, y] = ScaleTouch(touch_point.pos()); + this->TouchMoved(x, y, id + 1); + return true; + } + } + return false; +} + +bool GRenderWindow::TouchExist(std::size_t id, + const QList& touch_points) const { + return std::any_of(touch_points.begin(), touch_points.end(), + [id](const auto& point) { return id == point.id() + 1; }); } bool GRenderWindow::event(QEvent* event) { diff --git a/src/yuzu/bootmanager.h b/src/yuzu/bootmanager.h index 339095509..b5ec7de07 100644 --- a/src/yuzu/bootmanager.h +++ b/src/yuzu/bootmanager.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -21,7 +22,6 @@ class GRenderWindow; class GMainWindow; class QKeyEvent; -class QTouchEvent; class QStringList; namespace InputCommon { @@ -191,6 +191,10 @@ private: void TouchUpdateEvent(const QTouchEvent* event); void TouchEndEvent(); + bool TouchStart(const QTouchEvent::TouchPoint& touch_point); + bool TouchUpdate(const QTouchEvent::TouchPoint& touch_point); + bool TouchExist(std::size_t id, const QList& touch_points) const; + void OnMinimalClientAreaChangeRequest(std::pair minimal_size) override; bool InitializeOpenGL(); @@ -215,6 +219,8 @@ private: bool first_frame = false; + std::array touch_ids{}; + protected: void showEvent(QShowEvent* event) override; bool eventFilter(QObject* object, QEvent* event) override; diff --git a/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp b/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp index e32bed5e6..7843d5167 100644 --- a/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp +++ b/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp @@ -29,16 +29,16 @@ EmuWindow_SDL2::~EmuWindow_SDL2() { } void EmuWindow_SDL2::OnMouseMotion(s32 x, s32 y) { - TouchMoved((unsigned)std::max(x, 0), (unsigned)std::max(y, 0)); + TouchMoved((unsigned)std::max(x, 0), (unsigned)std::max(y, 0), 0); input_subsystem->GetMouse()->MouseMove(x, y); } void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) { if (button == SDL_BUTTON_LEFT) { if (state == SDL_PRESSED) { - TouchPressed((unsigned)std::max(x, 0), (unsigned)std::max(y, 0)); + TouchPressed((unsigned)std::max(x, 0), (unsigned)std::max(y, 0), 0); } else { - TouchReleased(); + TouchReleased(0); } } else if (button == SDL_BUTTON_RIGHT) { if (state == SDL_PRESSED) { @@ -66,16 +66,16 @@ void EmuWindow_SDL2::OnFingerDown(float x, float y) { // 3DS does const auto [px, py] = TouchToPixelPos(x, y); - TouchPressed(px, py); + TouchPressed(px, py, 0); } void EmuWindow_SDL2::OnFingerMotion(float x, float y) { const auto [px, py] = TouchToPixelPos(x, y); - TouchMoved(px, py); + TouchMoved(px, py, 0); } void EmuWindow_SDL2::OnFingerUp() { - TouchReleased(); + TouchReleased(0); } void EmuWindow_SDL2::OnKeyEvent(int key, u8 state) { -- cgit v1.2.3 From b483f2d010bf745ab873e8f8bfaca5515e56d39f Mon Sep 17 00:00:00 2001 From: german Date: Sun, 10 Jan 2021 08:36:31 -0600 Subject: Always initialize keyboard input --- src/core/frontend/emu_window.cpp | 5 ++--- src/core/frontend/emu_window.h | 9 +++++---- src/core/hle/service/hid/controllers/touchscreen.cpp | 6 +----- src/input_common/udp/client.cpp | 18 ++++++++---------- src/yuzu/bootmanager.cpp | 7 ++++--- 5 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/core/frontend/emu_window.cpp b/src/core/frontend/emu_window.cpp index af6c1633a..ee7a58b1c 100644 --- a/src/core/frontend/emu_window.cpp +++ b/src/core/frontend/emu_window.cpp @@ -28,12 +28,11 @@ private: public: explicit Device(std::weak_ptr&& touch_state) : touch_state(touch_state) {} Input::TouchStatus GetStatus() const override { - Input::TouchStatus touch_status{}; if (auto state = touch_state.lock()) { std::lock_guard guard{state->mutex}; - touch_status = state->status; + return state->status; } - return touch_status; + return {}; } private: diff --git a/src/core/frontend/emu_window.h b/src/core/frontend/emu_window.h index f8db42ab4..2436c6580 100644 --- a/src/core/frontend/emu_window.h +++ b/src/core/frontend/emu_window.h @@ -117,12 +117,13 @@ public: * Signal that a touch pressed event has occurred (e.g. mouse click pressed) * @param framebuffer_x Framebuffer x-coordinate that was pressed * @param framebuffer_y Framebuffer y-coordinate that was pressed - * @param id Touch event id + * @param id Touch event ID */ void TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y, std::size_t id); - /** Signal that a touch released event has occurred (e.g. mouse click released) - *@param id Touch event id + /** + * Signal that a touch released event has occurred (e.g. mouse click released) + * @param id Touch event ID */ void TouchReleased(std::size_t id); @@ -130,7 +131,7 @@ public: * Signal that a touch movement event has occurred (e.g. mouse was moved over the emu window) * @param framebuffer_x Framebuffer x-coordinate * @param framebuffer_y Framebuffer y-coordinate - * @param id Touch event id + * @param id Touch event ID */ void TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y, std::size_t id); diff --git a/src/core/hle/service/hid/controllers/touchscreen.cpp b/src/core/hle/service/hid/controllers/touchscreen.cpp index cd318f25b..5219f2dad 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.cpp +++ b/src/core/hle/service/hid/controllers/touchscreen.cpp @@ -100,11 +100,7 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin void Controller_Touchscreen::OnLoadInputDevices() { touch_mouse_device = Input::CreateDevice("engine:emu_window"); touch_udp_device = Input::CreateDevice("engine:cemuhookudp"); - if (Settings::values.use_touch_from_button) { - touch_btn_device = Input::CreateDevice("engine:touch_from_button"); - } else { - touch_btn_device.reset(); - } + touch_btn_device = Input::CreateDevice("engine:touch_from_button"); } std::optional Controller_Touchscreen::GetUnusedFingerID() const { diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index 5e39fdce2..e7e50d789 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp @@ -136,9 +136,7 @@ static void SocketLoop(Socket* socket) { Client::Client() { LOG_INFO(Input, "Udp Initialization started"); - for (std::size_t id = 0; id < MAX_TOUCH_FINGERS; ++id) { - finger_id[id] = MAX_TOUCH_FINGERS; - } + finger_id.fill(MAX_TOUCH_FINGERS); ReloadSockets(); } @@ -347,17 +345,17 @@ void Client::UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, const u16 min_y = static_cast(touch_param.Get("min_y", 50)); const u16 max_x = static_cast(touch_param.Get("max_x", 1800)); const u16 max_y = static_cast(touch_param.Get("max_y", 850)); - + const std::size_t touch_id = client * 2 + id; if (touch_pad.is_active) { - if (finger_id[client * 2 + id] == MAX_TOUCH_FINGERS) { + if (finger_id[touch_id] == MAX_TOUCH_FINGERS) { const auto first_free_id = GetUnusedFingerID(); if (!first_free_id) { // Invalid finger id skip to next input return; } - finger_id[client * 2 + id] = first_free_id.value(); + finger_id[touch_id] = *first_free_id; } - auto& [x, y, pressed] = touch_status[finger_id[client * 2 + id]]; + auto& [x, y, pressed] = touch_status[finger_id[touch_id]]; x = static_cast(std::clamp(static_cast(touch_pad.x), min_x, max_x) - min_x) / static_cast(max_x - min_x); y = static_cast(std::clamp(static_cast(touch_pad.y), min_y, max_y) - min_y) / @@ -366,9 +364,9 @@ void Client::UpdateTouchInput(Response::TouchPad& touch_pad, std::size_t client, return; } - if (finger_id[client * 2 + id] != MAX_TOUCH_FINGERS) { - touch_status[finger_id[client * 2 + id]] = {}; - finger_id[client * 2 + id] = MAX_TOUCH_FINGERS; + if (finger_id[touch_id] != MAX_TOUCH_FINGERS) { + touch_status[finger_id[touch_id]] = {}; + finger_id[touch_id] = MAX_TOUCH_FINGERS; } } diff --git a/src/yuzu/bootmanager.cpp b/src/yuzu/bootmanager.cpp index 1f91514ef..4528eb196 100644 --- a/src/yuzu/bootmanager.cpp +++ b/src/yuzu/bootmanager.cpp @@ -475,7 +475,7 @@ bool GRenderWindow::TouchStart(const QTouchEvent::TouchPoint& touch_point) { bool GRenderWindow::TouchUpdate(const QTouchEvent::TouchPoint& touch_point) { for (std::size_t id = 0; id < touch_ids.size(); ++id) { - if (touch_ids[id] == touch_point.id() + 1) { + if (touch_ids[id] == static_cast(touch_point.id() + 1)) { const auto [x, y] = ScaleTouch(touch_point.pos()); this->TouchMoved(x, y, id + 1); return true; @@ -486,8 +486,9 @@ bool GRenderWindow::TouchUpdate(const QTouchEvent::TouchPoint& touch_point) { bool GRenderWindow::TouchExist(std::size_t id, const QList& touch_points) const { - return std::any_of(touch_points.begin(), touch_points.end(), - [id](const auto& point) { return id == point.id() + 1; }); + return std::any_of(touch_points.begin(), touch_points.end(), [id](const auto& point) { + return id == static_cast(point.id() + 1); + }); } bool GRenderWindow::event(QEvent* event) { -- cgit v1.2.3