gesture.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2018 yuzu emulator team
  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 "common/swap.h"
  9. #include "core/frontend/input.h"
  10. #include "core/hle/service/hid/controllers/controller_base.h"
  11. namespace Service::HID {
  12. class Controller_Gesture final : public ControllerBase {
  13. public:
  14. explicit Controller_Gesture(Core::System& system);
  15. ~Controller_Gesture() override;
  16. // Called when the controller is initialized
  17. void OnInit() override;
  18. // When the controller is released
  19. void OnRelease() override;
  20. // When the controller is requesting an update for the shared memory
  21. void OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, size_t size) override;
  22. // Called when input devices should be loaded
  23. void OnLoadInputDevices() override;
  24. private:
  25. static constexpr size_t MAX_FINGERS = 16;
  26. static constexpr size_t MAX_POINTS = 4;
  27. enum class TouchType : u32 {
  28. Idle, // Nothing touching the screen
  29. Complete, // Unknown. End of touch?
  30. Cancel, // Never triggered
  31. Touch, // Pressing without movement
  32. Press, // Never triggered
  33. Tap, // Fast press then release
  34. Pan, // All points moving together across the screen
  35. Swipe, // Fast press movement and release of a single point
  36. Pinch, // All points moving away/closer to the midpoint
  37. Rotate, // All points rotating from the midpoint
  38. };
  39. enum class Direction : u32 {
  40. None,
  41. Left,
  42. Up,
  43. Right,
  44. Down,
  45. };
  46. struct Attribute {
  47. union {
  48. u32_le raw{};
  49. BitField<0, 1, u32> is_new_touch;
  50. BitField<1, 1, u32> is_double_tap;
  51. };
  52. };
  53. static_assert(sizeof(Attribute) == 4, "Attribute is an invalid size");
  54. struct Points {
  55. s32_le x;
  56. s32_le y;
  57. };
  58. static_assert(sizeof(Points) == 8, "Points is an invalid size");
  59. struct GestureState {
  60. s64_le sampling_number;
  61. s64_le sampling_number2;
  62. s64_le detection_count;
  63. TouchType type;
  64. Direction dir;
  65. s32_le x;
  66. s32_le y;
  67. s32_le delta_x;
  68. s32_le delta_y;
  69. f32 vel_x;
  70. f32 vel_y;
  71. Attribute attributes;
  72. u32 scale;
  73. u32 rotation_angle;
  74. s32_le point_count;
  75. std::array<Points, 4> points;
  76. };
  77. static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size");
  78. struct SharedMemory {
  79. CommonHeader header;
  80. std::array<GestureState, 17> gesture_states;
  81. };
  82. static_assert(sizeof(SharedMemory) == 0x708, "SharedMemory is an invalid size");
  83. struct Finger {
  84. f32 x{};
  85. f32 y{};
  86. bool pressed{};
  87. };
  88. struct GestureProperties {
  89. std::array<Points, MAX_POINTS> points{};
  90. std::size_t active_points{};
  91. Points mid_point{};
  92. s64_le detection_count{};
  93. u64_le delta_time{};
  94. float average_distance{};
  95. float angle{};
  96. };
  97. // Returns an unused finger id, if there is no fingers avaliable MAX_FINGERS will be returned
  98. std::optional<size_t> GetUnusedFingerID() const;
  99. /** If the touch is new it tries to assing a new finger id, if there is no fingers avaliable no
  100. * changes will be made. Updates the coordinates if the finger id it's already set. If the touch
  101. * ends delays the output by one frame to set the end_touch flag before finally freeing the
  102. * finger id */
  103. size_t UpdateTouchInputEvent(const std::tuple<float, float, bool>& touch_input,
  104. size_t finger_id);
  105. // Returns the average distance, angle and middle point of the active fingers
  106. GestureProperties GetGestureProperties();
  107. SharedMemory shared_memory{};
  108. std::unique_ptr<Input::TouchDevice> touch_mouse_device;
  109. std::unique_ptr<Input::TouchDevice> touch_udp_device;
  110. std::unique_ptr<Input::TouchDevice> touch_btn_device;
  111. std::array<size_t, MAX_FINGERS> mouse_finger_id;
  112. std::array<size_t, MAX_FINGERS> keyboard_finger_id;
  113. std::array<size_t, MAX_FINGERS> udp_finger_id;
  114. std::array<Finger, MAX_POINTS> fingers;
  115. GestureProperties last_gesture;
  116. };
  117. } // namespace Service::HID