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 --- .../hle/service/hid/controllers/touchscreen.cpp | 30 +++++++++++++--------- src/core/hle/service/hid/controllers/touchscreen.h | 14 +++++----- 2 files changed, 25 insertions(+), 19 deletions(-) (limited to 'src/core/hle/service/hid/controllers') 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 -- cgit v1.2.3