From 60831eabd98ed3317c3740d7d03445b9afe8a5e2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 17 May 2021 14:59:33 -0400 Subject: [PATCH 1/6] hid/gesture: Rename Points to Point This only represents a single point --- src/core/hle/service/hid/controllers/gesture.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index f46e29411..21123c46c 100644 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h @@ -63,11 +63,11 @@ private: }; static_assert(sizeof(Attribute) == 4, "Attribute is an invalid size"); - struct Points { + struct Point { s32_le x; s32_le y; }; - static_assert(sizeof(Points) == 8, "Points is an invalid size"); + static_assert(sizeof(Point) == 8, "Point is an invalid size"); struct GestureState { s64_le sampling_number; @@ -85,7 +85,7 @@ private: f32 scale; f32 rotation_angle; s32_le point_count; - std::array points; + std::array points; }; static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size"); @@ -102,9 +102,9 @@ private: }; struct GestureProperties { - std::array points{}; + std::array points{}; std::size_t active_points{}; - Points mid_point{}; + Point mid_point{}; s64_le detection_count{}; u64_le delta_time{}; f32 average_distance{}; From 2f1ef3910ba9fa7d6ac412039ff4b6541008a28b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 17 May 2021 15:04:58 -0400 Subject: [PATCH 2/6] hid/gesture: Add default comparators to Point Simplifies some comparisons. --- src/core/hle/service/hid/controllers/gesture.cpp | 11 +++-------- src/core/hle/service/hid/controllers/gesture.h | 6 ++++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index d311f754b..07b3a6095 100644 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp @@ -91,8 +91,7 @@ bool Controller_Gesture::ShouldUpdateGesture(const GestureProperties& gesture, // Update if coordinates change for (size_t id = 0; id < MAX_POINTS; id++) { - if (gesture.points[id].x != last_gesture.points[id].x || - gesture.points[id].y != last_gesture.points[id].y) { + if (gesture.points[id] != last_gesture.points[id]) { return true; } } @@ -179,8 +178,7 @@ void Controller_Gesture::UpdateExistingGesture(GestureProperties& gesture, Touch // Promote to pan type if touch moved for (size_t id = 0; id < MAX_POINTS; id++) { - if (gesture.points[id].x != last_gesture.points[id].x || - gesture.points[id].y != last_gesture.points[id].y) { + if (gesture.points[id] != last_gesture.points[id]) { type = TouchType::Pan; break; } @@ -192,10 +190,7 @@ void Controller_Gesture::UpdateExistingGesture(GestureProperties& gesture, Touch enable_press_and_tap = false; gesture.active_points = 0; gesture.mid_point = {}; - for (size_t id = 0; id < MAX_POINTS; id++) { - gesture.points[id].x = 0; - gesture.points[id].y = 0; - } + gesture.points.fill({}); return; } diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index 21123c46c..619b6f3dc 100644 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h @@ -64,8 +64,10 @@ private: static_assert(sizeof(Attribute) == 4, "Attribute is an invalid size"); struct Point { - s32_le x; - s32_le y; + s32_le x{}; + s32_le y{}; + + friend bool operator==(const Point&, const Point&) = default; }; static_assert(sizeof(Point) == 8, "Point is an invalid size"); From 20699e90fac3195ab1b5d39793b48a5238a5ebc3 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 17 May 2021 15:10:59 -0400 Subject: [PATCH 3/6] hid/gesture: Replace x,y members of GestureState with a Point Simplifies assignments. --- src/core/hle/service/hid/controllers/gesture.cpp | 7 +++---- src/core/hle/service/hid/controllers/gesture.h | 3 +-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index 07b3a6095..86e9f4ea8 100644 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp @@ -145,8 +145,7 @@ void Controller_Gesture::UpdateGestureSharedMemory(u8* data, std::size_t size, cur_entry.detection_count = gesture.detection_count; cur_entry.type = type; cur_entry.attributes = attributes; - cur_entry.x = gesture.mid_point.x; - cur_entry.y = gesture.mid_point.y; + cur_entry.pos = gesture.mid_point; cur_entry.point_count = static_cast(gesture.active_points); for (size_t id = 0; id < MAX_POINTS; id++) { cur_entry.points[id].x = gesture.points[id].x; @@ -262,8 +261,8 @@ void Controller_Gesture::UpdatePanEvent(GestureProperties& gesture, auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; const auto& last_entry = shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; - cur_entry.delta_x = gesture.mid_point.x - last_entry.x; - cur_entry.delta_y = gesture.mid_point.y - last_entry.y; + cur_entry.delta_x = gesture.mid_point.x - last_entry.pos.x; + cur_entry.delta_y = gesture.mid_point.y - last_entry.pos.y; cur_entry.vel_x = static_cast(cur_entry.delta_x) / time_difference; cur_entry.vel_y = static_cast(cur_entry.delta_y) / time_difference; diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index 619b6f3dc..809303131 100644 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h @@ -77,8 +77,7 @@ private: s64_le detection_count; TouchType type; Direction direction; - s32_le x; - s32_le y; + Point pos; s32_le delta_x; s32_le delta_y; f32 vel_x; From 74f30c02239e034e065f5f1bd4e249c091f2d97f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 17 May 2021 15:15:07 -0400 Subject: [PATCH 4/6] hid/gesture: Make Point a template We can now use this in a generic context to reuse it with the finger position. --- .../hle/service/hid/controllers/gesture.cpp | 49 +++++++++---------- .../hle/service/hid/controllers/gesture.h | 35 ++++++++----- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index 86e9f4ea8..2c9330941 100644 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp @@ -123,8 +123,7 @@ void Controller_Gesture::UpdateGestureSharedMemory(u8* data, std::size_t size, cur_entry.sampling_number2 = cur_entry.sampling_number; // Reset values to default - cur_entry.delta_x = 0; - cur_entry.delta_y = 0; + cur_entry.delta = {}; cur_entry.vel_x = 0; cur_entry.vel_y = 0; cur_entry.direction = Direction::None; @@ -147,10 +146,7 @@ void Controller_Gesture::UpdateGestureSharedMemory(u8* data, std::size_t size, cur_entry.attributes = attributes; cur_entry.pos = gesture.mid_point; cur_entry.point_count = static_cast(gesture.active_points); - for (size_t id = 0; id < MAX_POINTS; id++) { - cur_entry.points[id].x = gesture.points[id].x; - cur_entry.points[id].y = gesture.points[id].y; - } + cur_entry.points = gesture.points; last_gesture = gesture; std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); @@ -261,11 +257,10 @@ void Controller_Gesture::UpdatePanEvent(GestureProperties& gesture, auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; const auto& last_entry = shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; - cur_entry.delta_x = gesture.mid_point.x - last_entry.pos.x; - cur_entry.delta_y = gesture.mid_point.y - last_entry.pos.y; + cur_entry.delta = gesture.mid_point - last_entry.pos; - cur_entry.vel_x = static_cast(cur_entry.delta_x) / time_difference; - cur_entry.vel_y = static_cast(cur_entry.delta_y) / time_difference; + cur_entry.vel_x = static_cast(cur_entry.delta.x) / time_difference; + cur_entry.vel_y = static_cast(cur_entry.delta.y) / time_difference; last_pan_time_difference = time_difference; // Promote to pinch type @@ -292,9 +287,9 @@ void Controller_Gesture::EndPanEvent(GestureProperties& gesture, const auto& last_entry = shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; cur_entry.vel_x = - static_cast(last_entry.delta_x) / (last_pan_time_difference + time_difference); + static_cast(last_entry.delta.x) / (last_pan_time_difference + time_difference); cur_entry.vel_y = - static_cast(last_entry.delta_y) / (last_pan_time_difference + time_difference); + static_cast(last_entry.delta.y) / (last_pan_time_difference + time_difference); const f32 curr_vel = std::sqrt((cur_entry.vel_x * cur_entry.vel_x) + (cur_entry.vel_y * cur_entry.vel_y)); @@ -319,17 +314,17 @@ void Controller_Gesture::SetSwipeEvent(GestureProperties& gesture, type = TouchType::Swipe; gesture = last_gesture_props; force_update = true; - cur_entry.delta_x = last_entry.delta_x; - cur_entry.delta_y = last_entry.delta_y; - if (std::abs(cur_entry.delta_x) > std::abs(cur_entry.delta_y)) { - if (cur_entry.delta_x > 0) { + cur_entry.delta = last_entry.delta; + + if (std::abs(cur_entry.delta.x) > std::abs(cur_entry.delta.y)) { + if (cur_entry.delta.x > 0) { cur_entry.direction = Direction::Right; return; } cur_entry.direction = Direction::Left; return; } - if (cur_entry.delta_y > 0) { + if (cur_entry.delta.y > 0) { cur_entry.direction = Direction::Down; return; } @@ -375,8 +370,7 @@ std::size_t Controller_Gesture::UpdateTouchInputEvent( finger_id = first_free_id.value(); fingers[finger_id].pressed = true; } - fingers[finger_id].x = x; - fingers[finger_id].y = y; + fingers[finger_id].pos = {x, y}; return finger_id; } @@ -396,17 +390,18 @@ Controller_Gesture::GestureProperties Controller_Gesture::GetGestureProperties() static_cast(std::distance(active_fingers.begin(), end_iter)); for (size_t id = 0; id < gesture.active_points; ++id) { - gesture.points[id].x = - static_cast(active_fingers[id].x * Layout::ScreenUndocked::Width); - gesture.points[id].y = - static_cast(active_fingers[id].y * Layout::ScreenUndocked::Height); + const auto& [active_x, active_y] = active_fingers[id].pos; + gesture.points[id] = { + .x = static_cast(active_x * Layout::ScreenUndocked::Width), + .y = static_cast(active_y * Layout::ScreenUndocked::Height), + }; // Hack: There is no touch in docked but games still allow it if (Settings::values.use_docked_mode.GetValue()) { - gesture.points[id].x = - static_cast(active_fingers[id].x * Layout::ScreenDocked::Width); - gesture.points[id].y = - static_cast(active_fingers[id].y * Layout::ScreenDocked::Height); + gesture.points[id] = { + .x = static_cast(active_x * Layout::ScreenDocked::Width), + .y = static_cast(active_y * Layout::ScreenDocked::Height), + }; } gesture.mid_point.x += static_cast(gesture.points[id].x / gesture.active_points); diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index 809303131..b2bcae2b7 100644 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h @@ -63,13 +63,28 @@ private: }; static_assert(sizeof(Attribute) == 4, "Attribute is an invalid size"); + template struct Point { - s32_le x{}; - s32_le y{}; + T x{}; + T y{}; + + friend Point operator+(const Point& lhs, const Point& rhs) { + return { + .x = lhs.x + rhs.x, + .y = lhs.y + rhs.y, + }; + } + + friend Point operator-(const Point& lhs, const Point& rhs) { + return { + .x = lhs.x - rhs.x, + .y = lhs.y - rhs.y, + }; + } friend bool operator==(const Point&, const Point&) = default; }; - static_assert(sizeof(Point) == 8, "Point is an invalid size"); + static_assert(sizeof(Point) == 8, "Point is an invalid size"); struct GestureState { s64_le sampling_number; @@ -77,16 +92,15 @@ private: s64_le detection_count; TouchType type; Direction direction; - Point pos; - s32_le delta_x; - s32_le delta_y; + Point pos; + Point delta; f32 vel_x; f32 vel_y; Attribute attributes; f32 scale; f32 rotation_angle; s32_le point_count; - std::array points; + std::array, 4> points; }; static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size"); @@ -97,15 +111,14 @@ private: static_assert(sizeof(SharedMemory) == 0x708, "SharedMemory is an invalid size"); struct Finger { - f32 x{}; - f32 y{}; + Point pos{}; bool pressed{}; }; struct GestureProperties { - std::array points{}; + std::array, MAX_POINTS> points{}; std::size_t active_points{}; - Point mid_point{}; + Point mid_point{}; s64_le detection_count{}; u64_le delta_time{}; f32 average_distance{}; From a9d8e24e4722d26500aaa0cfd4a11ff1380b3bdf Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 17 May 2021 15:45:53 -0400 Subject: [PATCH 5/6] hid/gesture: Ensure all ID arrays are initialized Makes for deterministic initial state. --- src/core/hle/service/hid/controllers/gesture.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index b2bcae2b7..c1578ec25 100644 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h @@ -180,10 +180,10 @@ 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 fingers; + std::array mouse_finger_id{}; + std::array keyboard_finger_id{}; + std::array udp_finger_id{}; + std::array fingers{}; GestureProperties last_gesture{}; s64_le last_update_timestamp{}; s64_le last_tap_timestamp{}; From 44556dc21ac5609e5c60334347baa60701d056b3 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 18 May 2021 03:59:41 -0400 Subject: [PATCH 6/6] hid/gesture: Factor out last gesture retrieval into its own function Deduplicates a commonly repeated expression. --- .../hle/service/hid/controllers/gesture.cpp | 31 +++++++++++-------- .../hle/service/hid/controllers/gesture.h | 6 +++- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index 2c9330941..764abb5b6 100644 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp @@ -154,8 +154,8 @@ void Controller_Gesture::UpdateGestureSharedMemory(u8* data, std::size_t size, void Controller_Gesture::NewGesture(GestureProperties& gesture, TouchType& type, Attribute& attributes) { - const auto& last_entry = - shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; + const auto& last_entry = GetLastGestureEntry(); + gesture.detection_count++; type = TouchType::Touch; @@ -168,8 +168,7 @@ void Controller_Gesture::NewGesture(GestureProperties& gesture, TouchType& type, void Controller_Gesture::UpdateExistingGesture(GestureProperties& gesture, TouchType& type, f32 time_difference) { - const auto& last_entry = - shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; + const auto& last_entry = GetLastGestureEntry(); // Promote to pan type if touch moved for (size_t id = 0; id < MAX_POINTS; id++) { @@ -204,8 +203,8 @@ void Controller_Gesture::UpdateExistingGesture(GestureProperties& gesture, Touch void Controller_Gesture::EndGesture(GestureProperties& gesture, GestureProperties& last_gesture_props, TouchType& type, Attribute& attributes, f32 time_difference) { - const auto& last_entry = - shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; + const auto& last_entry = GetLastGestureEntry(); + if (last_gesture_props.active_points != 0) { switch (last_entry.type) { case TouchType::Touch: @@ -255,10 +254,9 @@ void Controller_Gesture::UpdatePanEvent(GestureProperties& gesture, GestureProperties& last_gesture_props, TouchType& type, f32 time_difference) { auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; - const auto& last_entry = - shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; - cur_entry.delta = gesture.mid_point - last_entry.pos; + const auto& last_entry = GetLastGestureEntry(); + cur_entry.delta = gesture.mid_point - last_entry.pos; cur_entry.vel_x = static_cast(cur_entry.delta.x) / time_difference; cur_entry.vel_y = static_cast(cur_entry.delta.y) / time_difference; last_pan_time_difference = time_difference; @@ -284,8 +282,7 @@ void Controller_Gesture::EndPanEvent(GestureProperties& gesture, GestureProperties& last_gesture_props, TouchType& type, f32 time_difference) { auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; - const auto& last_entry = - shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; + const auto& last_entry = GetLastGestureEntry(); cur_entry.vel_x = static_cast(last_entry.delta.x) / (last_pan_time_difference + time_difference); cur_entry.vel_y = @@ -309,8 +306,8 @@ void Controller_Gesture::EndPanEvent(GestureProperties& gesture, void Controller_Gesture::SetSwipeEvent(GestureProperties& gesture, GestureProperties& last_gesture_props, TouchType& type) { auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; - const auto& last_entry = - shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; + const auto& last_entry = GetLastGestureEntry(); + type = TouchType::Swipe; gesture = last_gesture_props; force_update = true; @@ -353,6 +350,14 @@ std::optional Controller_Gesture::GetUnusedFingerID() const { return std::nullopt; } +Controller_Gesture::GestureState& Controller_Gesture::GetLastGestureEntry() { + return shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; +} + +const Controller_Gesture::GestureState& Controller_Gesture::GetLastGestureEntry() const { + return shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17]; +} + std::size_t Controller_Gesture::UpdateTouchInputEvent( const std::tuple& touch_input, std::size_t finger_id) { const auto& [x, y, pressed] = touch_input; diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index c1578ec25..eecfeaad5 100644 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h @@ -162,7 +162,11 @@ private: TouchType& type); // Returns an unused finger id, if there is no fingers available std::nullopt is returned. - std::optional GetUnusedFingerID() const; + [[nodiscard]] std::optional GetUnusedFingerID() const; + + // Retrieves the last gesture entry, as indicated by shared memory indices. + [[nodiscard]] GestureState& GetLastGestureEntry(); + [[nodiscard]] const GestureState& GetLastGestureEntry() const; /** * If the touch is new it tries to assign a new finger id, if there is no fingers available no