diff options
| -rw-r--r-- | src/core/hle/service/hid/controllers/gesture.cpp | 159 | ||||
| -rw-r--r-- | src/core/hle/service/hid/controllers/gesture.h | 92 |
2 files changed, 240 insertions, 11 deletions
diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index b7b7bfeae..e7063f8ef 100644 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp | |||
| @@ -5,15 +5,25 @@ | |||
| 5 | #include <cstring> | 5 | #include <cstring> |
| 6 | #include "common/common_types.h" | 6 | #include "common/common_types.h" |
| 7 | #include "core/core_timing.h" | 7 | #include "core/core_timing.h" |
| 8 | #include "core/frontend/emu_window.h" | ||
| 8 | #include "core/hle/service/hid/controllers/gesture.h" | 9 | #include "core/hle/service/hid/controllers/gesture.h" |
| 10 | #include "core/settings.h" | ||
| 9 | 11 | ||
| 10 | namespace Service::HID { | 12 | namespace Service::HID { |
| 11 | constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3BA00; | 13 | constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3BA00; |
| 14 | constexpr f32 angle_threshold = 0.08f; | ||
| 15 | constexpr f32 pinch_threshold = 100.0f; | ||
| 12 | 16 | ||
| 13 | Controller_Gesture::Controller_Gesture(Core::System& system) : ControllerBase(system) {} | 17 | Controller_Gesture::Controller_Gesture(Core::System& system) : ControllerBase(system) {} |
| 14 | Controller_Gesture::~Controller_Gesture() = default; | 18 | Controller_Gesture::~Controller_Gesture() = default; |
| 15 | 19 | ||
| 16 | void Controller_Gesture::OnInit() {} | 20 | void Controller_Gesture::OnInit() { |
| 21 | for (std::size_t id = 0; id < MAX_FINGERS; ++id) { | ||
| 22 | mouse_finger_id[id] = MAX_FINGERS; | ||
| 23 | keyboard_finger_id[id] = MAX_FINGERS; | ||
| 24 | udp_finger_id[id] = MAX_FINGERS; | ||
| 25 | } | ||
| 26 | } | ||
| 17 | 27 | ||
| 18 | void Controller_Gesture::OnRelease() {} | 28 | void Controller_Gesture::OnRelease() {} |
| 19 | 29 | ||
| @@ -35,10 +45,153 @@ void Controller_Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing, u | |||
| 35 | 45 | ||
| 36 | cur_entry.sampling_number = last_entry.sampling_number + 1; | 46 | cur_entry.sampling_number = last_entry.sampling_number + 1; |
| 37 | cur_entry.sampling_number2 = cur_entry.sampling_number; | 47 | cur_entry.sampling_number2 = cur_entry.sampling_number; |
| 38 | // TODO(ogniK): Update gesture states | 48 | |
| 49 | // TODO(german77): Implement all gesture types | ||
| 50 | |||
| 51 | const Input::TouchStatus& mouse_status = touch_mouse_device->GetStatus(); | ||
| 52 | const Input::TouchStatus& udp_status = touch_udp_device->GetStatus(); | ||
| 53 | for (std::size_t id = 0; id < mouse_status.size(); ++id) { | ||
| 54 | mouse_finger_id[id] = UpdateTouchInputEvent(mouse_status[id], mouse_finger_id[id]); | ||
| 55 | udp_finger_id[id] = UpdateTouchInputEvent(udp_status[id], udp_finger_id[id]); | ||
| 56 | } | ||
| 57 | |||
| 58 | if (Settings::values.use_touch_from_button) { | ||
| 59 | const Input::TouchStatus& keyboard_status = touch_btn_device->GetStatus(); | ||
| 60 | for (std::size_t id = 0; id < mouse_status.size(); ++id) { | ||
| 61 | keyboard_finger_id[id] = | ||
| 62 | UpdateTouchInputEvent(keyboard_status[id], keyboard_finger_id[id]); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | TouchType type = TouchType::Idle; | ||
| 67 | Attribute attributes{}; | ||
| 68 | GestureProperties gesture = GetGestureProperties(); | ||
| 69 | if (last_gesture.active_points != gesture.active_points) { | ||
| 70 | ++last_gesture.detection_count; | ||
| 71 | } | ||
| 72 | if (gesture.active_points > 0) { | ||
| 73 | if (last_gesture.active_points == 0) { | ||
| 74 | attributes.is_new_touch.Assign(true); | ||
| 75 | last_gesture.average_distance = gesture.average_distance; | ||
| 76 | last_gesture.angle = gesture.angle; | ||
| 77 | } | ||
| 78 | |||
| 79 | type = TouchType::Touch; | ||
| 80 | if (gesture.mid_point.x != last_entry.x || gesture.mid_point.y != last_entry.y) { | ||
| 81 | type = TouchType::Pan; | ||
| 82 | } | ||
| 83 | if (std::abs(gesture.average_distance - last_gesture.average_distance) > pinch_threshold) { | ||
| 84 | type = TouchType::Pinch; | ||
| 85 | } | ||
| 86 | if (std::abs(gesture.angle - last_gesture.angle) > angle_threshold) { | ||
| 87 | type = TouchType::Rotate; | ||
| 88 | } | ||
| 89 | |||
| 90 | cur_entry.delta_x = gesture.mid_point.x - last_entry.x; | ||
| 91 | cur_entry.delta_y = gesture.mid_point.y - last_entry.y; | ||
| 92 | // TODO: Find how velocities are calculated | ||
| 93 | cur_entry.vel_x = static_cast<float>(cur_entry.delta_x) * 150.1f; | ||
| 94 | cur_entry.vel_y = static_cast<float>(cur_entry.delta_y) * 150.1f; | ||
| 95 | |||
| 96 | // Slowdown the rate of change for less flapping | ||
| 97 | last_gesture.average_distance = | ||
| 98 | (last_gesture.average_distance * 0.9f) + (gesture.average_distance * 0.1f); | ||
| 99 | last_gesture.angle = (last_gesture.angle * 0.9f) + (gesture.angle * 0.1f); | ||
| 100 | |||
| 101 | } else { | ||
| 102 | cur_entry.delta_x = 0; | ||
| 103 | cur_entry.delta_y = 0; | ||
| 104 | cur_entry.vel_x = 0; | ||
| 105 | cur_entry.vel_y = 0; | ||
| 106 | } | ||
| 107 | last_gesture.active_points = gesture.active_points; | ||
| 108 | cur_entry.detection_count = last_gesture.detection_count; | ||
| 109 | cur_entry.type = type; | ||
| 110 | cur_entry.attributes = attributes; | ||
| 111 | cur_entry.x = gesture.mid_point.x; | ||
| 112 | cur_entry.y = gesture.mid_point.y; | ||
| 113 | cur_entry.point_count = static_cast<s32>(gesture.active_points); | ||
| 114 | for (size_t id = 0; id < MAX_POINTS; id++) { | ||
| 115 | cur_entry.points[id].x = gesture.points[id].x; | ||
| 116 | cur_entry.points[id].y = gesture.points[id].y; | ||
| 117 | } | ||
| 118 | cur_entry.rotation_angle = 0; | ||
| 119 | cur_entry.scale = 0; | ||
| 39 | 120 | ||
| 40 | std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); | 121 | std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); |
| 41 | } | 122 | } |
| 42 | 123 | ||
| 43 | void Controller_Gesture::OnLoadInputDevices() {} | 124 | void Controller_Gesture::OnLoadInputDevices() { |
| 125 | touch_mouse_device = Input::CreateDevice<Input::TouchDevice>("engine:emu_window"); | ||
| 126 | touch_udp_device = Input::CreateDevice<Input::TouchDevice>("engine:cemuhookudp"); | ||
| 127 | touch_btn_device = Input::CreateDevice<Input::TouchDevice>("engine:touch_from_button"); | ||
| 128 | } | ||
| 129 | |||
| 130 | std::optional<std::size_t> Controller_Gesture::GetUnusedFingerID() const { | ||
| 131 | std::size_t first_free_id = 0; | ||
| 132 | while (first_free_id < MAX_POINTS) { | ||
| 133 | if (!fingers[first_free_id].pressed) { | ||
| 134 | return first_free_id; | ||
| 135 | } else { | ||
| 136 | first_free_id++; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | return std::nullopt; | ||
| 140 | } | ||
| 141 | |||
| 142 | std::size_t Controller_Gesture::UpdateTouchInputEvent( | ||
| 143 | const std::tuple<float, float, bool>& touch_input, std::size_t finger_id) { | ||
| 144 | const auto& [x, y, pressed] = touch_input; | ||
| 145 | if (pressed) { | ||
| 146 | if (finger_id == MAX_POINTS) { | ||
| 147 | const auto first_free_id = GetUnusedFingerID(); | ||
| 148 | if (!first_free_id) { | ||
| 149 | // Invalid finger id do nothing | ||
| 150 | return MAX_POINTS; | ||
| 151 | } | ||
| 152 | finger_id = first_free_id.value(); | ||
| 153 | fingers[finger_id].pressed = true; | ||
| 154 | } | ||
| 155 | fingers[finger_id].x = x; | ||
| 156 | fingers[finger_id].y = y; | ||
| 157 | return finger_id; | ||
| 158 | } | ||
| 159 | |||
| 160 | if (finger_id != MAX_POINTS) { | ||
| 161 | fingers[finger_id].pressed = false; | ||
| 162 | } | ||
| 163 | |||
| 164 | return MAX_POINTS; | ||
| 165 | } | ||
| 166 | |||
| 167 | Controller_Gesture::GestureProperties Controller_Gesture::GetGestureProperties() { | ||
| 168 | GestureProperties gesture; | ||
| 169 | std::array<Finger, MAX_POINTS> active_fingers; | ||
| 170 | const auto end_iter = std::copy_if(fingers.begin(), fingers.end(), active_fingers.begin(), | ||
| 171 | [](const auto& finger) { return finger.pressed; }); | ||
| 172 | gesture.active_points = | ||
| 173 | static_cast<std::size_t>(std::distance(active_fingers.begin(), end_iter)); | ||
| 174 | |||
| 175 | for (size_t id = 0; id < gesture.active_points; ++id) { | ||
| 176 | gesture.points[id].x = | ||
| 177 | static_cast<int>(active_fingers[id].x * Layout::ScreenUndocked::Width); | ||
| 178 | gesture.points[id].y = | ||
| 179 | static_cast<int>(active_fingers[id].y * Layout::ScreenUndocked::Height); | ||
| 180 | gesture.mid_point.x += static_cast<int>(gesture.points[id].x / gesture.active_points); | ||
| 181 | gesture.mid_point.y += static_cast<int>(gesture.points[id].y / gesture.active_points); | ||
| 182 | } | ||
| 183 | |||
| 184 | for (size_t id = 0; id < gesture.active_points; ++id) { | ||
| 185 | const double distance = | ||
| 186 | std::pow(static_cast<float>(gesture.mid_point.x - gesture.points[id].x), 2) + | ||
| 187 | std::pow(static_cast<float>(gesture.mid_point.y - gesture.points[id].y), 2); | ||
| 188 | gesture.average_distance += | ||
| 189 | static_cast<float>(distance) / static_cast<float>(gesture.active_points); | ||
| 190 | } | ||
| 191 | |||
| 192 | gesture.angle = std::atan2(static_cast<float>(gesture.mid_point.y - gesture.points[0].y), | ||
| 193 | static_cast<float>(gesture.mid_point.x - gesture.points[0].x)); | ||
| 194 | return gesture; | ||
| 195 | } | ||
| 196 | |||
| 44 | } // namespace Service::HID | 197 | } // namespace Service::HID |
diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index f650b8338..60ecc7822 100644 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h | |||
| @@ -5,8 +5,10 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include "common/bit_field.h" | ||
| 8 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 9 | #include "common/swap.h" | 10 | #include "common/swap.h" |
| 11 | #include "core/frontend/input.h" | ||
| 10 | #include "core/hle/service/hid/controllers/controller_base.h" | 12 | #include "core/hle/service/hid/controllers/controller_base.h" |
| 11 | 13 | ||
| 12 | namespace Service::HID { | 14 | namespace Service::HID { |
| @@ -28,29 +30,64 @@ public: | |||
| 28 | void OnLoadInputDevices() override; | 30 | void OnLoadInputDevices() override; |
| 29 | 31 | ||
| 30 | private: | 32 | private: |
| 31 | struct Locations { | 33 | static constexpr size_t MAX_FINGERS = 16; |
| 34 | static constexpr size_t MAX_POINTS = 4; | ||
| 35 | |||
| 36 | enum class TouchType : u32 { | ||
| 37 | Idle, // Nothing touching the screen | ||
| 38 | Complete, // Unknown. End of touch? | ||
| 39 | Cancel, // Never triggered | ||
| 40 | Touch, // Pressing without movement | ||
| 41 | Press, // Never triggered | ||
| 42 | Tap, // Fast press then release | ||
| 43 | Pan, // All points moving together across the screen | ||
| 44 | Swipe, // Fast press movement and release of a single point | ||
| 45 | Pinch, // All points moving away/closer to the midpoint | ||
| 46 | Rotate, // All points rotating from the midpoint | ||
| 47 | }; | ||
| 48 | |||
| 49 | enum class Direction : u32 { | ||
| 50 | None, | ||
| 51 | Left, | ||
| 52 | Up, | ||
| 53 | Right, | ||
| 54 | Down, | ||
| 55 | }; | ||
| 56 | |||
| 57 | struct Attribute { | ||
| 58 | union { | ||
| 59 | u32_le raw{}; | ||
| 60 | |||
| 61 | BitField<0, 1, u32> is_new_touch; | ||
| 62 | BitField<1, 1, u32> is_double_tap; | ||
| 63 | }; | ||
| 64 | }; | ||
| 65 | static_assert(sizeof(Attribute) == 4, "Attribute is an invalid size"); | ||
| 66 | |||
| 67 | struct Points { | ||
| 32 | s32_le x; | 68 | s32_le x; |
| 33 | s32_le y; | 69 | s32_le y; |
| 34 | }; | 70 | }; |
| 71 | static_assert(sizeof(Points) == 8, "Points is an invalid size"); | ||
| 35 | 72 | ||
| 36 | struct GestureState { | 73 | struct GestureState { |
| 37 | s64_le sampling_number; | 74 | s64_le sampling_number; |
| 38 | s64_le sampling_number2; | 75 | s64_le sampling_number2; |
| 39 | 76 | ||
| 40 | s64_le detection_count; | 77 | s64_le detection_count; |
| 41 | s32_le type; | 78 | TouchType type; |
| 42 | s32_le dir; | 79 | Direction dir; |
| 43 | s32_le x; | 80 | s32_le x; |
| 44 | s32_le y; | 81 | s32_le y; |
| 45 | s32_le delta_x; | 82 | s32_le delta_x; |
| 46 | s32_le delta_y; | 83 | s32_le delta_y; |
| 47 | f32 vel_x; | 84 | f32 vel_x; |
| 48 | f32 vel_y; | 85 | f32 vel_y; |
| 49 | s32_le attributes; | 86 | Attribute attributes; |
| 50 | f32 scale; | 87 | u32 scale; |
| 51 | f32 rotation; | 88 | u32 rotation_angle; |
| 52 | s32_le location_count; | 89 | s32_le point_count; |
| 53 | std::array<Locations, 4> locations; | 90 | std::array<Points, 4> points; |
| 54 | }; | 91 | }; |
| 55 | static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size"); | 92 | static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size"); |
| 56 | 93 | ||
| @@ -58,6 +95,45 @@ private: | |||
| 58 | CommonHeader header; | 95 | CommonHeader header; |
| 59 | std::array<GestureState, 17> gesture_states; | 96 | std::array<GestureState, 17> gesture_states; |
| 60 | }; | 97 | }; |
| 98 | static_assert(sizeof(SharedMemory) == 0x708, "SharedMemory is an invalid size"); | ||
| 99 | |||
| 100 | struct Finger { | ||
| 101 | f32 x{}; | ||
| 102 | f32 y{}; | ||
| 103 | bool pressed{}; | ||
| 104 | }; | ||
| 105 | |||
| 106 | struct GestureProperties { | ||
| 107 | std::array<Points, MAX_POINTS> points{}; | ||
| 108 | std::size_t active_points{}; | ||
| 109 | Points mid_point{}; | ||
| 110 | s64_le detection_count{}; | ||
| 111 | u64_le delta_time{}; | ||
| 112 | float average_distance{}; | ||
| 113 | float angle{}; | ||
| 114 | }; | ||
| 115 | |||
| 116 | // Returns an unused finger id, if there is no fingers avaliable MAX_FINGERS will be returned | ||
| 117 | std::optional<size_t> GetUnusedFingerID() const; | ||
| 118 | |||
| 119 | /** If the touch is new it tries to assing a new finger id, if there is no fingers avaliable no | ||
| 120 | * changes will be made. Updates the coordinates if the finger id it's already set. If the touch | ||
| 121 | * ends delays the output by one frame to set the end_touch flag before finally freeing the | ||
| 122 | * finger id */ | ||
| 123 | size_t UpdateTouchInputEvent(const std::tuple<float, float, bool>& touch_input, | ||
| 124 | size_t finger_id); | ||
| 125 | |||
| 126 | // Returns the average distance, angle and middle point of the active fingers | ||
| 127 | GestureProperties GetGestureProperties(); | ||
| 128 | |||
| 61 | SharedMemory shared_memory{}; | 129 | SharedMemory shared_memory{}; |
| 130 | std::unique_ptr<Input::TouchDevice> touch_mouse_device; | ||
| 131 | std::unique_ptr<Input::TouchDevice> touch_udp_device; | ||
| 132 | std::unique_ptr<Input::TouchDevice> touch_btn_device; | ||
| 133 | std::array<size_t, MAX_FINGERS> mouse_finger_id; | ||
| 134 | std::array<size_t, MAX_FINGERS> keyboard_finger_id; | ||
| 135 | std::array<size_t, MAX_FINGERS> udp_finger_id; | ||
| 136 | std::array<Finger, MAX_POINTS> fingers; | ||
| 137 | GestureProperties last_gesture; | ||
| 62 | }; | 138 | }; |
| 63 | } // namespace Service::HID | 139 | } // namespace Service::HID |