hid.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2015 Citra 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 "core/hle/kernel/kernel.h"
  7. #include "core/hle/service/service.h"
  8. #include "common/bit_field.h"
  9. namespace Kernel {
  10. class SharedMemory;
  11. class Event;
  12. }
  13. namespace Service {
  14. namespace HID {
  15. // Handle to shared memory region designated to HID_User service
  16. extern Kernel::SharedPtr<Kernel::SharedMemory> g_shared_mem;
  17. // Event handles
  18. extern Kernel::SharedPtr<Kernel::Event> g_event_pad_or_touch_1;
  19. extern Kernel::SharedPtr<Kernel::Event> g_event_pad_or_touch_2;
  20. extern Kernel::SharedPtr<Kernel::Event> g_event_accelerometer;
  21. extern Kernel::SharedPtr<Kernel::Event> g_event_gyroscope;
  22. extern Kernel::SharedPtr<Kernel::Event> g_event_debug_pad;
  23. /**
  24. * Structure of a Pad controller state.
  25. */
  26. struct PadState {
  27. union {
  28. u32 hex;
  29. BitField<0, 1, u32> a;
  30. BitField<1, 1, u32> b;
  31. BitField<2, 1, u32> select;
  32. BitField<3, 1, u32> start;
  33. BitField<4, 1, u32> right;
  34. BitField<5, 1, u32> left;
  35. BitField<6, 1, u32> up;
  36. BitField<7, 1, u32> down;
  37. BitField<8, 1, u32> r;
  38. BitField<9, 1, u32> l;
  39. BitField<10, 1, u32> x;
  40. BitField<11, 1, u32> y;
  41. BitField<14, 1, u32> zl;
  42. BitField<15, 1, u32> zr;
  43. BitField<20, 1, u32> touch;
  44. BitField<24, 1, u32> c_right;
  45. BitField<25, 1, u32> c_left;
  46. BitField<26, 1, u32> c_up;
  47. BitField<27, 1, u32> c_down;
  48. BitField<28, 1, u32> circle_right;
  49. BitField<29, 1, u32> circle_left;
  50. BitField<30, 1, u32> circle_up;
  51. BitField<31, 1, u32> circle_down;
  52. };
  53. };
  54. /**
  55. * Structure of a single entry of Pad state history within HID shared memory
  56. */
  57. struct PadDataEntry {
  58. PadState current_state;
  59. PadState delta_additions;
  60. PadState delta_removals;
  61. s16 circle_pad_x;
  62. s16 circle_pad_y;
  63. };
  64. /**
  65. * Structure of a single entry of touch state history within HID shared memory
  66. */
  67. struct TouchDataEntry {
  68. u16 x;
  69. u16 y;
  70. u32 data_valid;
  71. };
  72. /**
  73. * Structure of data stored in HID shared memory
  74. */
  75. struct SharedMem {
  76. // Offset 0x0 : "PAD" data, this is used for buttons and the circle pad
  77. struct {
  78. s64 index_reset_ticks;
  79. s64 index_reset_ticks_previous;
  80. u32 index; // Index of the last updated pad state history element
  81. INSERT_PADDING_BYTES(0x8);
  82. PadState current_state; // Same as entries[index].current_state
  83. u32 raw_circle_pad_data;
  84. INSERT_PADDING_BYTES(0x4);
  85. std::array<PadDataEntry, 8> entries; // Pad state history
  86. } pad;
  87. // Offset 0xA8 : Touchpad data, this is used for touchpad input
  88. struct {
  89. s64 index_reset_ticks;
  90. s64 index_reset_ticks_previous;
  91. u32 index; // Index of the last updated touch state history element
  92. INSERT_PADDING_BYTES(0xC);
  93. std::array<TouchDataEntry, 8> entries;
  94. } touch;
  95. };
  96. // Pre-defined PadStates for single button presses
  97. const PadState PAD_NONE = {{0}};
  98. const PadState PAD_A = {{1u << 0}};
  99. const PadState PAD_B = {{1u << 1}};
  100. const PadState PAD_SELECT = {{1u << 2}};
  101. const PadState PAD_START = {{1u << 3}};
  102. const PadState PAD_RIGHT = {{1u << 4}};
  103. const PadState PAD_LEFT = {{1u << 5}};
  104. const PadState PAD_UP = {{1u << 6}};
  105. const PadState PAD_DOWN = {{1u << 7}};
  106. const PadState PAD_R = {{1u << 8}};
  107. const PadState PAD_L = {{1u << 9}};
  108. const PadState PAD_X = {{1u << 10}};
  109. const PadState PAD_Y = {{1u << 11}};
  110. const PadState PAD_ZL = {{1u << 14}};
  111. const PadState PAD_ZR = {{1u << 15}};
  112. const PadState PAD_TOUCH = {{1u << 20}};
  113. const PadState PAD_C_RIGHT = {{1u << 24}};
  114. const PadState PAD_C_LEFT = {{1u << 25}};
  115. const PadState PAD_C_UP = {{1u << 26}};
  116. const PadState PAD_C_DOWN = {{1u << 27}};
  117. const PadState PAD_CIRCLE_RIGHT = {{1u << 28}};
  118. const PadState PAD_CIRCLE_LEFT = {{1u << 29}};
  119. const PadState PAD_CIRCLE_UP = {{1u << 30}};
  120. const PadState PAD_CIRCLE_DOWN = {{1u << 31}};
  121. /**
  122. * HID::GetIPCHandles service function
  123. * Inputs:
  124. * None
  125. * Outputs:
  126. * 1 : Result of function, 0 on success, otherwise error code
  127. * 2 : Unused
  128. * 3 : Handle to HID_User shared memory
  129. * 4 : Event signaled by HID_User
  130. * 5 : Event signaled by HID_User
  131. * 6 : Event signaled by HID_User
  132. * 7 : Gyroscope event
  133. * 8 : Event signaled by HID_User
  134. */
  135. void GetIPCHandles(Interface* self);
  136. /**
  137. * Sets a Pad state (button or button combo) as pressed
  138. * @param pad_state PadState data indicating which buttons have been pressed
  139. */
  140. void PadButtonPress(const PadState& pad_state);
  141. /**
  142. * Sets a Pad state (button or button combo) as released
  143. * @param pad_state PadState data indicating which buttons have been released
  144. */
  145. void PadButtonRelease(const PadState& pad_state);
  146. /**
  147. * Called after all Pad changes to be included in this update have been made, including both Pad
  148. * key changes and analog circle Pad changes.
  149. */
  150. void PadUpdateComplete();
  151. void HIDInit();
  152. void HIDShutdown();
  153. }
  154. }