hid.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /**
  16. * Structure of a Pad controller state.
  17. */
  18. struct PadState {
  19. union {
  20. u32 hex;
  21. BitField<0, 1, u32> a;
  22. BitField<1, 1, u32> b;
  23. BitField<2, 1, u32> select;
  24. BitField<3, 1, u32> start;
  25. BitField<4, 1, u32> right;
  26. BitField<5, 1, u32> left;
  27. BitField<6, 1, u32> up;
  28. BitField<7, 1, u32> down;
  29. BitField<8, 1, u32> r;
  30. BitField<9, 1, u32> l;
  31. BitField<10, 1, u32> x;
  32. BitField<11, 1, u32> y;
  33. BitField<14, 1, u32> zl;
  34. BitField<15, 1, u32> zr;
  35. BitField<20, 1, u32> touch;
  36. BitField<24, 1, u32> c_right;
  37. BitField<25, 1, u32> c_left;
  38. BitField<26, 1, u32> c_up;
  39. BitField<27, 1, u32> c_down;
  40. BitField<28, 1, u32> circle_right;
  41. BitField<29, 1, u32> circle_left;
  42. BitField<30, 1, u32> circle_up;
  43. BitField<31, 1, u32> circle_down;
  44. };
  45. };
  46. /**
  47. * Structure of a single entry of Pad state history within HID shared memory
  48. */
  49. struct PadDataEntry {
  50. PadState current_state;
  51. PadState delta_additions;
  52. PadState delta_removals;
  53. s16 circle_pad_x;
  54. s16 circle_pad_y;
  55. };
  56. /**
  57. * Structure of a single entry of touch state history within HID shared memory
  58. */
  59. struct TouchDataEntry {
  60. u16 x; ///< Y-coordinate of a touchpad press on the lower screen
  61. u16 y; ///< X-coordinate of a touchpad press on the lower screen
  62. BitField<0, 7, u32> valid; ///< Set to 1 when this entry contains actual X/Y data, otherwise 0
  63. };
  64. /**
  65. * Structure of data stored in HID shared memory
  66. */
  67. struct SharedMem {
  68. /// Pad data, this is used for buttons and the circle pad
  69. struct {
  70. s64 index_reset_ticks; ///< CPU tick count for when HID module updated entry index 0
  71. s64 index_reset_ticks_previous; ///< Previous `index_reset_ticks`
  72. u32 index; ///< Index of the last updated pad state entry
  73. INSERT_PADDING_WORDS(0x2);
  74. PadState current_state; ///< Current state of the pad buttons
  75. // TODO(bunnei): Implement `raw_circle_pad_data` field
  76. u32 raw_circle_pad_data; ///< Raw (analog) circle pad data, before being converted
  77. INSERT_PADDING_WORDS(0x1);
  78. std::array<PadDataEntry, 8> entries; ///< Last 8 pad entries
  79. } pad;
  80. /// Touchpad data, this is used for touchpad input
  81. struct {
  82. s64 index_reset_ticks; ///< CPU tick count for when HID module updated entry index 0
  83. s64 index_reset_ticks_previous; ///< Previous `index_reset_ticks`
  84. u32 index; ///< Index of the last updated touch entry
  85. INSERT_PADDING_WORDS(0x1);
  86. // TODO(bunnei): Implement `raw_entry` field
  87. TouchDataEntry raw_entry; ///< Raw (analog) touch data, before being converted
  88. std::array<TouchDataEntry, 8> entries; ///< Last 8 touch entries, in pixel coordinates
  89. } touch;
  90. };
  91. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  92. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  93. // support for that.
  94. #ifndef _MSC_VER
  95. #define ASSERT_REG_POSITION(field_name, position) \
  96. static_assert(offsetof(SharedMem, field_name) == position * 4, \
  97. "Field "#field_name" has invalid position")
  98. ASSERT_REG_POSITION(pad.index_reset_ticks, 0x0);
  99. ASSERT_REG_POSITION(touch.index_reset_ticks, 0x2A);
  100. #undef ASSERT_REG_POSITION
  101. #endif // !defined(_MSC_VER)
  102. // Pre-defined PadStates for single button presses
  103. const PadState PAD_NONE = {{0}};
  104. const PadState PAD_A = {{1u << 0}};
  105. const PadState PAD_B = {{1u << 1}};
  106. const PadState PAD_SELECT = {{1u << 2}};
  107. const PadState PAD_START = {{1u << 3}};
  108. const PadState PAD_RIGHT = {{1u << 4}};
  109. const PadState PAD_LEFT = {{1u << 5}};
  110. const PadState PAD_UP = {{1u << 6}};
  111. const PadState PAD_DOWN = {{1u << 7}};
  112. const PadState PAD_R = {{1u << 8}};
  113. const PadState PAD_L = {{1u << 9}};
  114. const PadState PAD_X = {{1u << 10}};
  115. const PadState PAD_Y = {{1u << 11}};
  116. const PadState PAD_ZL = {{1u << 14}};
  117. const PadState PAD_ZR = {{1u << 15}};
  118. const PadState PAD_TOUCH = {{1u << 20}};
  119. const PadState PAD_C_RIGHT = {{1u << 24}};
  120. const PadState PAD_C_LEFT = {{1u << 25}};
  121. const PadState PAD_C_UP = {{1u << 26}};
  122. const PadState PAD_C_DOWN = {{1u << 27}};
  123. const PadState PAD_CIRCLE_RIGHT = {{1u << 28}};
  124. const PadState PAD_CIRCLE_LEFT = {{1u << 29}};
  125. const PadState PAD_CIRCLE_UP = {{1u << 30}};
  126. const PadState PAD_CIRCLE_DOWN = {{1u << 31}};
  127. /**
  128. * HID::GetIPCHandles service function
  129. * Inputs:
  130. * None
  131. * Outputs:
  132. * 1 : Result of function, 0 on success, otherwise error code
  133. * 2 : IPC Command Structure translate-header
  134. * 3 : Handle to HID shared memory
  135. * 4 : Event signaled by HID
  136. * 5 : Event signaled by HID
  137. * 6 : Event signaled by HID
  138. * 7 : Gyroscope event
  139. * 8 : Event signaled by HID
  140. */
  141. void GetIPCHandles(Interface* self);
  142. /**
  143. * HID::EnableAccelerometer service function
  144. * Inputs:
  145. * None
  146. * Outputs:
  147. * 1 : Result of function, 0 on success, otherwise error code
  148. */
  149. void EnableAccelerometer(Interface* self);
  150. /**
  151. * HID::DisableAccelerometer service function
  152. * Inputs:
  153. * None
  154. * Outputs:
  155. * 1 : Result of function, 0 on success, otherwise error code
  156. */
  157. void DisableAccelerometer(Interface* self);
  158. /**
  159. * HID::EnableGyroscopeLow service function
  160. * Inputs:
  161. * None
  162. * Outputs:
  163. * 1 : Result of function, 0 on success, otherwise error code
  164. */
  165. void EnableGyroscopeLow(Interface* self);
  166. /**
  167. * HID::DisableGyroscopeLow service function
  168. * Inputs:
  169. * None
  170. * Outputs:
  171. * 1 : Result of function, 0 on success, otherwise error code
  172. */
  173. void DisableGyroscopeLow(Interface* self);
  174. /**
  175. * HID::GetSoundVolume service function
  176. * Inputs:
  177. * None
  178. * Outputs:
  179. * 1 : Result of function, 0 on success, otherwise error code
  180. * 2 : u8 output value
  181. */
  182. void GetSoundVolume(Interface* self);
  183. /// Checks for user input updates
  184. void Update();
  185. /// Initialize HID service
  186. void Init();
  187. /// Shutdown HID service
  188. void Shutdown();
  189. }
  190. }