Hid.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using Ryujinx.HLE.HOS;
  2. using System;
  3. namespace Ryujinx.HLE.Input
  4. {
  5. public class Hid
  6. {
  7. /*
  8. * Reference:
  9. * https://github.com/reswitched/libtransistor/blob/development/lib/hid.c
  10. * https://github.com/reswitched/libtransistor/blob/development/include/libtransistor/hid.h
  11. * https://github.com/switchbrew/libnx/blob/master/nx/source/services/hid.c
  12. * https://github.com/switchbrew/libnx/blob/master/nx/include/switch/services/hid.h
  13. */
  14. private const int HidHeaderSize = 0x400;
  15. private const int HidTouchScreenSize = 0x3000;
  16. private const int HidMouseSize = 0x400;
  17. private const int HidKeyboardSize = 0x400;
  18. private const int HidUnkSection1Size = 0x400;
  19. private const int HidUnkSection2Size = 0x400;
  20. private const int HidUnkSection3Size = 0x400;
  21. private const int HidUnkSection4Size = 0x400;
  22. private const int HidUnkSection5Size = 0x200;
  23. private const int HidUnkSection6Size = 0x200;
  24. private const int HidUnkSection7Size = 0x200;
  25. private const int HidUnkSection8Size = 0x800;
  26. private const int HidControllerSerialsSize = 0x4000;
  27. private const int HidControllersSize = 0x32000;
  28. private const int HidUnkSection9Size = 0x800;
  29. private const int HidTouchHeaderSize = 0x28;
  30. private const int HidTouchEntrySize = 0x298;
  31. private const int HidTouchEntryHeaderSize = 0x10;
  32. private const int HidTouchEntryTouchSize = 0x28;
  33. private const int HidControllerSize = 0x5000;
  34. private const int HidControllerHeaderSize = 0x28;
  35. private const int HidControllerLayoutsSize = 0x350;
  36. private const int HidControllersLayoutHeaderSize = 0x20;
  37. private const int HidControllersInputEntrySize = 0x30;
  38. private const int HidHeaderOffset = 0;
  39. private const int HidTouchScreenOffset = HidHeaderOffset + HidHeaderSize;
  40. private const int HidMouseOffset = HidTouchScreenOffset + HidTouchScreenSize;
  41. private const int HidKeyboardOffset = HidMouseOffset + HidMouseSize;
  42. private const int HidUnkSection1Offset = HidKeyboardOffset + HidKeyboardSize;
  43. private const int HidUnkSection2Offset = HidUnkSection1Offset + HidUnkSection1Size;
  44. private const int HidUnkSection3Offset = HidUnkSection2Offset + HidUnkSection2Size;
  45. private const int HidUnkSection4Offset = HidUnkSection3Offset + HidUnkSection3Size;
  46. private const int HidUnkSection5Offset = HidUnkSection4Offset + HidUnkSection4Size;
  47. private const int HidUnkSection6Offset = HidUnkSection5Offset + HidUnkSection5Size;
  48. private const int HidUnkSection7Offset = HidUnkSection6Offset + HidUnkSection6Size;
  49. private const int HidUnkSection8Offset = HidUnkSection7Offset + HidUnkSection7Size;
  50. private const int HidControllerSerialsOffset = HidUnkSection8Offset + HidUnkSection8Size;
  51. private const int HidControllersOffset = HidControllerSerialsOffset + HidControllerSerialsSize;
  52. private const int HidUnkSection9Offset = HidControllersOffset + HidControllersSize;
  53. private const int HidEntryCount = 17;
  54. private Switch Device;
  55. private long HidPosition;
  56. public Hid(Switch Device, long HidPosition)
  57. {
  58. this.Device = Device;
  59. this.HidPosition = HidPosition;
  60. Device.Memory.FillWithZeros(HidPosition, Horizon.HidSize);
  61. InitializeJoyconPair(
  62. JoyConColor.Body_Neon_Red,
  63. JoyConColor.Buttons_Neon_Red,
  64. JoyConColor.Body_Neon_Blue,
  65. JoyConColor.Buttons_Neon_Blue);
  66. }
  67. private void InitializeJoyconPair(
  68. JoyConColor LeftColorBody,
  69. JoyConColor LeftColorButtons,
  70. JoyConColor RightColorBody,
  71. JoyConColor RightColorButtons)
  72. {
  73. long BaseControllerOffset = HidPosition + HidControllersOffset + 8 * HidControllerSize;
  74. HidControllerType Type = HidControllerType.ControllerType_Handheld;
  75. bool IsHalf = false;
  76. HidControllerColorDesc SingleColorDesc =
  77. HidControllerColorDesc.ColorDesc_ColorsNonexistent;
  78. JoyConColor SingleColorBody = JoyConColor.Black;
  79. JoyConColor SingleColorButtons = JoyConColor.Black;
  80. HidControllerColorDesc SplitColorDesc = 0;
  81. Device.Memory.WriteInt32(BaseControllerOffset + 0x0, (int)Type);
  82. Device.Memory.WriteInt32(BaseControllerOffset + 0x4, IsHalf ? 1 : 0);
  83. Device.Memory.WriteInt32(BaseControllerOffset + 0x8, (int)SingleColorDesc);
  84. Device.Memory.WriteInt32(BaseControllerOffset + 0xc, (int)SingleColorBody);
  85. Device.Memory.WriteInt32(BaseControllerOffset + 0x10, (int)SingleColorButtons);
  86. Device.Memory.WriteInt32(BaseControllerOffset + 0x14, (int)SplitColorDesc);
  87. Device.Memory.WriteInt32(BaseControllerOffset + 0x18, (int)LeftColorBody);
  88. Device.Memory.WriteInt32(BaseControllerOffset + 0x1c, (int)LeftColorButtons);
  89. Device.Memory.WriteInt32(BaseControllerOffset + 0x20, (int)RightColorBody);
  90. Device.Memory.WriteInt32(BaseControllerOffset + 0x24, (int)RightColorButtons);
  91. }
  92. private HidControllerButtons UpdateStickButtons(
  93. HidJoystickPosition LeftStick,
  94. HidJoystickPosition RightStick)
  95. {
  96. HidControllerButtons Result = 0;
  97. if (RightStick.DX < 0)
  98. {
  99. Result |= HidControllerButtons.KEY_RSTICK_LEFT;
  100. }
  101. if (RightStick.DX > 0)
  102. {
  103. Result |= HidControllerButtons.KEY_RSTICK_RIGHT;
  104. }
  105. if (RightStick.DY < 0)
  106. {
  107. Result |= HidControllerButtons.KEY_RSTICK_DOWN;
  108. }
  109. if (RightStick.DY > 0)
  110. {
  111. Result |= HidControllerButtons.KEY_RSTICK_UP;
  112. }
  113. if (LeftStick.DX < 0)
  114. {
  115. Result |= HidControllerButtons.KEY_LSTICK_LEFT;
  116. }
  117. if (LeftStick.DX > 0)
  118. {
  119. Result |= HidControllerButtons.KEY_LSTICK_RIGHT;
  120. }
  121. if (LeftStick.DY < 0)
  122. {
  123. Result |= HidControllerButtons.KEY_LSTICK_DOWN;
  124. }
  125. if (LeftStick.DY > 0)
  126. {
  127. Result |= HidControllerButtons.KEY_LSTICK_UP;
  128. }
  129. return Result;
  130. }
  131. public void SetJoyconButton(
  132. HidControllerId ControllerId,
  133. HidControllerLayouts ControllerLayout,
  134. HidControllerButtons Buttons,
  135. HidJoystickPosition LeftStick,
  136. HidJoystickPosition RightStick)
  137. {
  138. Buttons |= UpdateStickButtons(LeftStick, RightStick);
  139. long ControllerOffset = HidPosition + HidControllersOffset;
  140. ControllerOffset += (int)ControllerId * HidControllerSize;
  141. ControllerOffset += HidControllerHeaderSize;
  142. ControllerOffset += (int)ControllerLayout * HidControllerLayoutsSize;
  143. long LastEntry = Device.Memory.ReadInt64(ControllerOffset + 0x10);
  144. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  145. long Timestamp = GetTimestamp();
  146. Device.Memory.WriteInt64(ControllerOffset + 0x0, Timestamp);
  147. Device.Memory.WriteInt64(ControllerOffset + 0x8, HidEntryCount);
  148. Device.Memory.WriteInt64(ControllerOffset + 0x10, CurrEntry);
  149. Device.Memory.WriteInt64(ControllerOffset + 0x18, HidEntryCount - 1);
  150. ControllerOffset += HidControllersLayoutHeaderSize;
  151. long LastEntryOffset = ControllerOffset + LastEntry * HidControllersInputEntrySize;
  152. ControllerOffset += CurrEntry * HidControllersInputEntrySize;
  153. long SampleCounter = Device.Memory.ReadInt64(LastEntryOffset) + 1;
  154. Device.Memory.WriteInt64(ControllerOffset + 0x0, SampleCounter);
  155. Device.Memory.WriteInt64(ControllerOffset + 0x8, SampleCounter);
  156. Device.Memory.WriteInt64(ControllerOffset + 0x10, (uint)Buttons);
  157. Device.Memory.WriteInt32(ControllerOffset + 0x18, LeftStick.DX);
  158. Device.Memory.WriteInt32(ControllerOffset + 0x1c, LeftStick.DY);
  159. Device.Memory.WriteInt32(ControllerOffset + 0x20, RightStick.DX);
  160. Device.Memory.WriteInt32(ControllerOffset + 0x24, RightStick.DY);
  161. Device.Memory.WriteInt64(ControllerOffset + 0x28,
  162. (uint)HidControllerConnState.Controller_State_Connected |
  163. (uint)HidControllerConnState.Controller_State_Wired);
  164. }
  165. public void SetTouchPoints(params HidTouchPoint[] Points)
  166. {
  167. long TouchScreenOffset = HidPosition + HidTouchScreenOffset;
  168. long LastEntry = Device.Memory.ReadInt64(TouchScreenOffset + 0x10);
  169. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  170. long Timestamp = GetTimestamp();
  171. Device.Memory.WriteInt64(TouchScreenOffset + 0x0, Timestamp);
  172. Device.Memory.WriteInt64(TouchScreenOffset + 0x8, HidEntryCount);
  173. Device.Memory.WriteInt64(TouchScreenOffset + 0x10, CurrEntry);
  174. Device.Memory.WriteInt64(TouchScreenOffset + 0x18, HidEntryCount - 1);
  175. Device.Memory.WriteInt64(TouchScreenOffset + 0x20, Timestamp);
  176. long TouchEntryOffset = TouchScreenOffset + HidTouchHeaderSize;
  177. long LastEntryOffset = TouchEntryOffset + LastEntry * HidTouchEntrySize;
  178. long SampleCounter = Device.Memory.ReadInt64(LastEntryOffset) + 1;
  179. TouchEntryOffset += CurrEntry * HidTouchEntrySize;
  180. Device.Memory.WriteInt64(TouchEntryOffset + 0x0, SampleCounter);
  181. Device.Memory.WriteInt64(TouchEntryOffset + 0x8, Points.Length);
  182. TouchEntryOffset += HidTouchEntryHeaderSize;
  183. const int Padding = 0;
  184. int Index = 0;
  185. foreach (HidTouchPoint Point in Points)
  186. {
  187. Device.Memory.WriteInt64(TouchEntryOffset + 0x0, Timestamp);
  188. Device.Memory.WriteInt32(TouchEntryOffset + 0x8, Padding);
  189. Device.Memory.WriteInt32(TouchEntryOffset + 0xc, Index++);
  190. Device.Memory.WriteInt32(TouchEntryOffset + 0x10, Point.X);
  191. Device.Memory.WriteInt32(TouchEntryOffset + 0x14, Point.Y);
  192. Device.Memory.WriteInt32(TouchEntryOffset + 0x18, Point.DiameterX);
  193. Device.Memory.WriteInt32(TouchEntryOffset + 0x1c, Point.DiameterY);
  194. Device.Memory.WriteInt32(TouchEntryOffset + 0x20, Point.Angle);
  195. Device.Memory.WriteInt32(TouchEntryOffset + 0x24, Padding);
  196. TouchEntryOffset += HidTouchEntryTouchSize;
  197. }
  198. }
  199. private static long GetTimestamp()
  200. {
  201. return (long)((ulong)Environment.TickCount * 19_200);
  202. }
  203. }
  204. }