gesture.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include "common/bit_field.h"
  7. #include "common/common_types.h"
  8. #include "core/frontend/input.h"
  9. #include "core/hle/service/hid/controllers/controller_base.h"
  10. namespace Service::HID {
  11. class Controller_Gesture final : public ControllerBase {
  12. public:
  13. explicit Controller_Gesture(Core::System& system_);
  14. ~Controller_Gesture() override;
  15. // Called when the controller is initialized
  16. void OnInit() override;
  17. // When the controller is released
  18. void OnRelease() override;
  19. // When the controller is requesting an update for the shared memory
  20. void OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, size_t size) override;
  21. // Called when input devices should be loaded
  22. void OnLoadInputDevices() override;
  23. private:
  24. static constexpr size_t MAX_FINGERS = 16;
  25. static constexpr size_t MAX_POINTS = 4;
  26. enum class TouchType : u32 {
  27. Idle, // Nothing touching the screen
  28. Complete, // Set at the end of a touch event
  29. Cancel, // Set when the number of fingers change
  30. Touch, // A finger just touched the screen
  31. Press, // Set if last type is touch and the finger hasn't moved
  32. Tap, // Fast press then release
  33. Pan, // All points moving together across the screen
  34. Swipe, // Fast press movement and release of a single point
  35. Pinch, // All points moving away/closer to the midpoint
  36. Rotate, // All points rotating from the midpoint
  37. };
  38. enum class Direction : u32 {
  39. None,
  40. Left,
  41. Up,
  42. Right,
  43. Down,
  44. };
  45. struct Attribute {
  46. union {
  47. u32_le raw{};
  48. BitField<4, 1, u32> is_new_touch;
  49. BitField<8, 1, u32> is_double_tap;
  50. };
  51. };
  52. static_assert(sizeof(Attribute) == 4, "Attribute is an invalid size");
  53. template <typename T>
  54. struct Point {
  55. T x{};
  56. T y{};
  57. friend Point operator+(const Point& lhs, const Point& rhs) {
  58. return {
  59. .x = lhs.x + rhs.x,
  60. .y = lhs.y + rhs.y,
  61. };
  62. }
  63. friend Point operator-(const Point& lhs, const Point& rhs) {
  64. return {
  65. .x = lhs.x - rhs.x,
  66. .y = lhs.y - rhs.y,
  67. };
  68. }
  69. friend bool operator==(const Point&, const Point&) = default;
  70. };
  71. static_assert(sizeof(Point<s32_le>) == 8, "Point is an invalid size");
  72. struct GestureState {
  73. s64_le sampling_number;
  74. s64_le sampling_number2;
  75. s64_le detection_count;
  76. TouchType type;
  77. Direction direction;
  78. Point<s32_le> pos;
  79. Point<s32_le> delta;
  80. f32 vel_x;
  81. f32 vel_y;
  82. Attribute attributes;
  83. f32 scale;
  84. f32 rotation_angle;
  85. s32_le point_count;
  86. std::array<Point<s32_le>, 4> points;
  87. };
  88. static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size");
  89. struct SharedMemory {
  90. CommonHeader header;
  91. std::array<GestureState, 17> gesture_states;
  92. };
  93. static_assert(sizeof(SharedMemory) == 0x708, "SharedMemory is an invalid size");
  94. struct Finger {
  95. Point<f32> pos{};
  96. bool pressed{};
  97. };
  98. struct GestureProperties {
  99. std::array<Point<s32_le>, MAX_POINTS> points{};
  100. std::size_t active_points{};
  101. Point<s32_le> mid_point{};
  102. s64_le detection_count{};
  103. u64_le delta_time{};
  104. f32 average_distance{};
  105. f32 angle{};
  106. };
  107. // Reads input from all available input engines
  108. void ReadTouchInput();
  109. // Returns true if gesture state needs to be updated
  110. bool ShouldUpdateGesture(const GestureProperties& gesture, f32 time_difference);
  111. // Updates the shared memory to the next state
  112. void UpdateGestureSharedMemory(u8* data, std::size_t size, GestureProperties& gesture,
  113. f32 time_difference);
  114. // Initializes new gesture
  115. void NewGesture(GestureProperties& gesture, TouchType& type, Attribute& attributes);
  116. // Updates existing gesture state
  117. void UpdateExistingGesture(GestureProperties& gesture, TouchType& type, f32 time_difference);
  118. // Terminates exiting gesture
  119. void EndGesture(GestureProperties& gesture, GestureProperties& last_gesture_props,
  120. TouchType& type, Attribute& attributes, f32 time_difference);
  121. // Set current event to a tap event
  122. void SetTapEvent(GestureProperties& gesture, GestureProperties& last_gesture_props,
  123. TouchType& type, Attribute& attributes);
  124. // Calculates and set the extra parameters related to a pan event
  125. void UpdatePanEvent(GestureProperties& gesture, GestureProperties& last_gesture_props,
  126. TouchType& type, f32 time_difference);
  127. // Terminates the pan event
  128. void EndPanEvent(GestureProperties& gesture, GestureProperties& last_gesture_props,
  129. TouchType& type, f32 time_difference);
  130. // Set current event to a swipe event
  131. void SetSwipeEvent(GestureProperties& gesture, GestureProperties& last_gesture_props,
  132. TouchType& type);
  133. // Returns an unused finger id, if there is no fingers available std::nullopt is returned.
  134. std::optional<size_t> GetUnusedFingerID() const;
  135. /**
  136. * If the touch is new it tries to assign a new finger id, if there is no fingers available no
  137. * changes will be made. Updates the coordinates if the finger id it's already set. If the touch
  138. * ends delays the output by one frame to set the end_touch flag before finally freeing the
  139. * finger id
  140. */
  141. size_t UpdateTouchInputEvent(const std::tuple<float, float, bool>& touch_input,
  142. size_t finger_id);
  143. // Returns the average distance, angle and middle point of the active fingers
  144. GestureProperties GetGestureProperties();
  145. SharedMemory shared_memory{};
  146. std::unique_ptr<Input::TouchDevice> touch_mouse_device;
  147. std::unique_ptr<Input::TouchDevice> touch_udp_device;
  148. std::unique_ptr<Input::TouchDevice> touch_btn_device;
  149. std::array<size_t, MAX_FINGERS> mouse_finger_id;
  150. std::array<size_t, MAX_FINGERS> keyboard_finger_id;
  151. std::array<size_t, MAX_FINGERS> udp_finger_id;
  152. std::array<Finger, MAX_POINTS> fingers;
  153. GestureProperties last_gesture{};
  154. s64_le last_update_timestamp{};
  155. s64_le last_tap_timestamp{};
  156. f32 last_pan_time_difference{};
  157. bool force_update{false};
  158. bool enable_press_and_tap{false};
  159. };
  160. } // namespace Service::HID