hid.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 "core/hle/service/service.h"
  6. #include "core/settings.h"
  7. namespace Service::HID {
  8. // Begin enums and output structs
  9. constexpr u32 HID_NUM_ENTRIES = 17;
  10. constexpr u32 HID_NUM_LAYOUTS = 7;
  11. constexpr s32 HID_JOYSTICK_MAX = 0x8000;
  12. constexpr s32 HID_JOYSTICK_MIN = -0x8000;
  13. constexpr u32 JOYCON_BODY_NEON_RED = 0xFF3C28;
  14. constexpr u32 JOYCON_BUTTONS_NEON_RED = 0x1E0A0A;
  15. constexpr u32 JOYCON_BODY_NEON_BLUE = 0x0AB9E6;
  16. constexpr u32 JOYCON_BUTTONS_NEON_BLUE = 0x001E1E;
  17. enum ControllerType : u32 {
  18. ControllerType_ProController = 1 << 0,
  19. ControllerType_Handheld = 1 << 1,
  20. ControllerType_JoyconPair = 1 << 2,
  21. ControllerType_JoyconLeft = 1 << 3,
  22. ControllerType_JoyconRight = 1 << 4,
  23. };
  24. enum ControllerLayoutType : u32 {
  25. Layout_ProController = 0, // Pro Controller or HID gamepad
  26. Layout_Handheld = 1, // Two Joy-Con docked to rails
  27. Layout_Single = 2, // Horizontal single Joy-Con or pair of Joy-Con, adjusted for orientation
  28. Layout_Left = 3, // Only raw left Joy-Con state, no orientation adjustment
  29. Layout_Right = 4, // Only raw right Joy-Con state, no orientation adjustment
  30. Layout_DefaultDigital = 5, // Same as next, but sticks have 8-direction values only
  31. Layout_Default = 6, // Safe default, single Joy-Con have buttons/sticks rotated for orientation
  32. };
  33. enum ControllerColorDescription {
  34. ColorDesc_ColorsNonexistent = 1 << 1,
  35. };
  36. enum ControllerConnectionState {
  37. ConnectionState_Connected = 1 << 0,
  38. ConnectionState_Wired = 1 << 1,
  39. };
  40. enum ControllerJoystick {
  41. Joystick_Left = 0,
  42. Joystick_Right = 1,
  43. };
  44. enum ControllerID {
  45. Controller_Player1 = 0,
  46. Controller_Player2 = 1,
  47. Controller_Player3 = 2,
  48. Controller_Player4 = 3,
  49. Controller_Player5 = 4,
  50. Controller_Player6 = 5,
  51. Controller_Player7 = 6,
  52. Controller_Player8 = 7,
  53. Controller_Handheld = 8,
  54. Controller_Unknown = 9,
  55. };
  56. // End enums and output structs
  57. // Begin UnkInput3
  58. struct UnkInput3Header {
  59. u64 timestamp_ticks;
  60. u64 num_entries;
  61. u64 latest_entry;
  62. u64 max_entry_index;
  63. };
  64. static_assert(sizeof(UnkInput3Header) == 0x20, "HID UnkInput3 header structure has incorrect size");
  65. struct UnkInput3Entry {
  66. u64 timestamp;
  67. u64 timestamp_2;
  68. u64 unk_8;
  69. u64 unk_10;
  70. u64 unk_18;
  71. };
  72. static_assert(sizeof(UnkInput3Entry) == 0x28, "HID UnkInput3 entry structure has incorrect size");
  73. struct UnkInput3 {
  74. UnkInput3Header header;
  75. std::array<UnkInput3Entry, 17> entries;
  76. std::array<u8, 0x138> padding;
  77. };
  78. static_assert(sizeof(UnkInput3) == 0x400, "HID UnkInput3 structure has incorrect size");
  79. // End UnkInput3
  80. // Begin TouchScreen
  81. struct TouchScreenHeader {
  82. u64 timestamp_ticks;
  83. u64 num_entries;
  84. u64 latest_entry;
  85. u64 max_entry_index;
  86. u64 timestamp;
  87. };
  88. static_assert(sizeof(TouchScreenHeader) == 0x28,
  89. "HID touch screen header structure has incorrect size");
  90. struct TouchScreenEntryHeader {
  91. u64 timestamp;
  92. u64 num_touches;
  93. };
  94. static_assert(sizeof(TouchScreenEntryHeader) == 0x10,
  95. "HID touch screen entry header structure has incorrect size");
  96. struct TouchScreenEntryTouch {
  97. u64 timestamp;
  98. u32 padding;
  99. u32 touch_index;
  100. u32 x;
  101. u32 y;
  102. u32 diameter_x;
  103. u32 diameter_y;
  104. u32 angle;
  105. u32 padding_2;
  106. };
  107. static_assert(sizeof(TouchScreenEntryTouch) == 0x28,
  108. "HID touch screen touch structure has incorrect size");
  109. struct TouchScreenEntry {
  110. TouchScreenEntryHeader header;
  111. std::array<TouchScreenEntryTouch, 16> touches;
  112. u64 unk;
  113. };
  114. static_assert(sizeof(TouchScreenEntry) == 0x298,
  115. "HID touch screen entry structure has incorrect size");
  116. struct TouchScreen {
  117. TouchScreenHeader header;
  118. std::array<TouchScreenEntry, 17> entries;
  119. std::array<u8, 0x3c0> padding;
  120. };
  121. static_assert(sizeof(TouchScreen) == 0x3000, "HID touch screen structure has incorrect size");
  122. // End TouchScreen
  123. // Begin Mouse
  124. struct MouseHeader {
  125. u64 timestamp_ticks;
  126. u64 num_entries;
  127. u64 latest_entry;
  128. u64 max_entry_index;
  129. };
  130. static_assert(sizeof(MouseHeader) == 0x20, "HID mouse header structure has incorrect size");
  131. struct MouseButtonState {
  132. union {
  133. u64 hex{};
  134. // Buttons
  135. BitField<0, 1, u64> left;
  136. BitField<1, 1, u64> right;
  137. BitField<2, 1, u64> middle;
  138. BitField<3, 1, u64> forward;
  139. BitField<4, 1, u64> back;
  140. };
  141. };
  142. struct MouseEntry {
  143. u64 timestamp;
  144. u64 timestamp_2;
  145. u32 x;
  146. u32 y;
  147. u32 velocity_x;
  148. u32 velocity_y;
  149. u32 scroll_velocity_x;
  150. u32 scroll_velocity_y;
  151. MouseButtonState buttons;
  152. };
  153. static_assert(sizeof(MouseEntry) == 0x30, "HID mouse entry structure has incorrect size");
  154. struct Mouse {
  155. MouseHeader header;
  156. std::array<MouseEntry, 17> entries;
  157. std::array<u8, 0xB0> padding;
  158. };
  159. static_assert(sizeof(Mouse) == 0x400, "HID mouse structure has incorrect size");
  160. // End Mouse
  161. // Begin Keyboard
  162. struct KeyboardHeader {
  163. u64 timestamp_ticks;
  164. u64 num_entries;
  165. u64 latest_entry;
  166. u64 max_entry_index;
  167. };
  168. static_assert(sizeof(KeyboardHeader) == 0x20, "HID keyboard header structure has incorrect size");
  169. struct KeyboardModifierKeyState {
  170. union {
  171. u64 hex{};
  172. // Buttons
  173. BitField<0, 1, u64> lctrl;
  174. BitField<1, 1, u64> lshift;
  175. BitField<2, 1, u64> lalt;
  176. BitField<3, 1, u64> lmeta;
  177. BitField<4, 1, u64> rctrl;
  178. BitField<5, 1, u64> rshift;
  179. BitField<6, 1, u64> ralt;
  180. BitField<7, 1, u64> rmeta;
  181. BitField<8, 1, u64> capslock;
  182. BitField<9, 1, u64> scrolllock;
  183. BitField<10, 1, u64> numlock;
  184. };
  185. };
  186. struct KeyboardEntry {
  187. u64 timestamp;
  188. u64 timestamp_2;
  189. KeyboardModifierKeyState modifier;
  190. u32 keys[8];
  191. };
  192. static_assert(sizeof(KeyboardEntry) == 0x38, "HID keyboard entry structure has incorrect size");
  193. struct Keyboard {
  194. KeyboardHeader header;
  195. std::array<KeyboardEntry, 17> entries;
  196. std::array<u8, 0x28> padding;
  197. };
  198. static_assert(sizeof(Keyboard) == 0x400, "HID keyboard structure has incorrect size");
  199. // End Keyboard
  200. // Begin UnkInput1
  201. struct UnkInput1Header {
  202. u64 timestamp_ticks;
  203. u64 num_entries;
  204. u64 latest_entry;
  205. u64 max_entry_index;
  206. };
  207. static_assert(sizeof(UnkInput1Header) == 0x20, "HID UnkInput1 header structure has incorrect size");
  208. struct UnkInput1Entry {
  209. u64 timestamp;
  210. u64 timestamp_2;
  211. u64 unk_8;
  212. u64 unk_10;
  213. u64 unk_18;
  214. };
  215. static_assert(sizeof(UnkInput1Entry) == 0x28, "HID UnkInput1 entry structure has incorrect size");
  216. struct UnkInput1 {
  217. UnkInput1Header header;
  218. std::array<UnkInput1Entry, 17> entries;
  219. std::array<u8, 0x138> padding;
  220. };
  221. static_assert(sizeof(UnkInput1) == 0x400, "HID UnkInput1 structure has incorrect size");
  222. // End UnkInput1
  223. // Begin UnkInput2
  224. struct UnkInput2Header {
  225. u64 timestamp_ticks;
  226. u64 num_entries;
  227. u64 latest_entry;
  228. u64 max_entry_index;
  229. };
  230. static_assert(sizeof(UnkInput2Header) == 0x20, "HID UnkInput2 header structure has incorrect size");
  231. struct UnkInput2 {
  232. UnkInput2Header header;
  233. std::array<u8, 0x1E0> padding;
  234. };
  235. static_assert(sizeof(UnkInput2) == 0x200, "HID UnkInput2 structure has incorrect size");
  236. // End UnkInput2
  237. // Begin Controller
  238. struct ControllerMAC {
  239. u64 timestamp;
  240. std::array<u8, 0x8> mac;
  241. u64 unk;
  242. u64 timestamp_2;
  243. };
  244. static_assert(sizeof(ControllerMAC) == 0x20, "HID controller MAC structure has incorrect size");
  245. struct ControllerHeader {
  246. u32 type;
  247. u32 is_half;
  248. u32 single_colors_descriptor;
  249. u32 single_color_body;
  250. u32 single_color_buttons;
  251. u32 split_colors_descriptor;
  252. u32 left_color_body;
  253. u32 left_color_buttons;
  254. u32 right_color_body;
  255. u32 right_color_buttons;
  256. };
  257. static_assert(sizeof(ControllerHeader) == 0x28,
  258. "HID controller header structure has incorrect size");
  259. struct ControllerLayoutHeader {
  260. u64 timestamp_ticks;
  261. u64 num_entries;
  262. u64 latest_entry;
  263. u64 max_entry_index;
  264. };
  265. static_assert(sizeof(ControllerLayoutHeader) == 0x20,
  266. "HID controller layout header structure has incorrect size");
  267. struct ControllerPadState {
  268. union {
  269. u64 hex{};
  270. // Buttons
  271. BitField<0, 1, u64> a;
  272. BitField<1, 1, u64> b;
  273. BitField<2, 1, u64> x;
  274. BitField<3, 1, u64> y;
  275. BitField<4, 1, u64> lstick;
  276. BitField<5, 1, u64> rstick;
  277. BitField<6, 1, u64> l;
  278. BitField<7, 1, u64> r;
  279. BitField<8, 1, u64> zl;
  280. BitField<9, 1, u64> zr;
  281. BitField<10, 1, u64> plus;
  282. BitField<11, 1, u64> minus;
  283. // D-pad buttons
  284. BitField<12, 1, u64> dleft;
  285. BitField<13, 1, u64> dup;
  286. BitField<14, 1, u64> dright;
  287. BitField<15, 1, u64> ddown;
  288. // Left stick directions
  289. BitField<16, 1, u64> lstick_left;
  290. BitField<17, 1, u64> lstick_up;
  291. BitField<18, 1, u64> lstick_right;
  292. BitField<19, 1, u64> lstick_down;
  293. // Right stick directions
  294. BitField<20, 1, u64> rstick_left;
  295. BitField<21, 1, u64> rstick_up;
  296. BitField<22, 1, u64> rstick_right;
  297. BitField<23, 1, u64> rstick_down;
  298. BitField<24, 1, u64> sl;
  299. BitField<25, 1, u64> sr;
  300. };
  301. };
  302. struct ControllerInputEntry {
  303. u64 timestamp;
  304. u64 timestamp_2;
  305. ControllerPadState buttons;
  306. s32 joystick_left_x;
  307. s32 joystick_left_y;
  308. s32 joystick_right_x;
  309. s32 joystick_right_y;
  310. u64 connection_state;
  311. };
  312. static_assert(sizeof(ControllerInputEntry) == 0x30,
  313. "HID controller input entry structure has incorrect size");
  314. struct ControllerLayout {
  315. ControllerLayoutHeader header;
  316. std::array<ControllerInputEntry, 17> entries;
  317. };
  318. static_assert(sizeof(ControllerLayout) == 0x350,
  319. "HID controller layout structure has incorrect size");
  320. struct Controller {
  321. ControllerHeader header;
  322. std::array<ControllerLayout, 7> layouts;
  323. std::array<u8, 0x2a70> unk_1;
  324. ControllerMAC mac_left;
  325. ControllerMAC mac_right;
  326. std::array<u8, 0xdf8> unk_2;
  327. };
  328. static_assert(sizeof(Controller) == 0x5000, "HID controller structure has incorrect size");
  329. // End Controller
  330. struct SharedMemory {
  331. UnkInput3 unk_input_3;
  332. TouchScreen touchscreen;
  333. Mouse mouse;
  334. Keyboard keyboard;
  335. std::array<UnkInput1, 4> unk_input_1;
  336. std::array<UnkInput2, 3> unk_input_2;
  337. std::array<u8, 0x800> unk_section_8;
  338. std::array<u8, 0x4000> controller_serials;
  339. std::array<Controller, 10> controllers;
  340. std::array<u8, 0x4600> unk_section_9;
  341. };
  342. static_assert(sizeof(SharedMemory) == 0x40000, "HID Shared Memory structure has incorrect size");
  343. /// Reload input devices. Used when input configuration changed
  344. void ReloadInputDevices();
  345. /// Registers all HID services with the specified service manager.
  346. void InstallInterfaces(SM::ServiceManager& service_manager);
  347. } // namespace Service::HID