Hid.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using ChocolArm64.Memory;
  2. using System.Diagnostics;
  3. namespace Ryujinx.Core.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 long SharedMemOffset;
  55. private Switch Ns;
  56. public Hid(Switch Ns)
  57. {
  58. this.Ns = Ns;
  59. }
  60. public void Init(long HidOffset)
  61. {
  62. SharedMemOffset = HidOffset;
  63. InitializeJoyconPair(
  64. JoyConColor.Body_Neon_Red,
  65. JoyConColor.Buttons_Neon_Red,
  66. JoyConColor.Body_Neon_Blue,
  67. JoyConColor.Buttons_Neon_Blue);
  68. }
  69. public void InitializeJoyconPair(
  70. JoyConColor LeftColorBody,
  71. JoyConColor LeftColorButtons,
  72. JoyConColor RightColorBody,
  73. JoyConColor RightColorButtons)
  74. {
  75. long BaseControllerOffset = HidControllersOffset + 8 * HidControllerSize;
  76. HidControllerType Type =
  77. HidControllerType.ControllerType_Handheld |
  78. HidControllerType.ControllerType_JoyconPair;
  79. bool IsHalf = false;
  80. HidControllerColorDesc SingleColorDesc =
  81. HidControllerColorDesc.ColorDesc_ColorsNonexistent;
  82. JoyConColor SingleColorBody = JoyConColor.Black;
  83. JoyConColor SingleColorButtons = JoyConColor.Black;
  84. HidControllerColorDesc SplitColorDesc = 0;
  85. WriteInt32(BaseControllerOffset + 0x0, (int)Type);
  86. WriteInt32(BaseControllerOffset + 0x4, IsHalf ? 1 : 0);
  87. WriteInt32(BaseControllerOffset + 0x8, (int)SingleColorDesc);
  88. WriteInt32(BaseControllerOffset + 0xc, (int)SingleColorBody);
  89. WriteInt32(BaseControllerOffset + 0x10, (int)SingleColorButtons);
  90. WriteInt32(BaseControllerOffset + 0x14, (int)SplitColorDesc);
  91. WriteInt32(BaseControllerOffset + 0x18, (int)LeftColorBody);
  92. WriteInt32(BaseControllerOffset + 0x1c, (int)LeftColorButtons);
  93. WriteInt32(BaseControllerOffset + 0x20, (int)RightColorBody);
  94. WriteInt32(BaseControllerOffset + 0x24, (int)RightColorButtons);
  95. }
  96. public void SetJoyconButton(
  97. HidControllerId ControllerId,
  98. HidControllerLayouts ControllerLayout,
  99. HidControllerButtons Buttons,
  100. HidJoystickPosition LeftStick,
  101. HidJoystickPosition RightStick)
  102. {
  103. long ControllerOffset = HidControllersOffset + (int)ControllerId * HidControllerSize;
  104. ControllerOffset += HidControllerHeaderSize;
  105. ControllerOffset += (int)ControllerLayout * HidControllerLayoutsSize;
  106. long LastEntry = ReadInt64(ControllerOffset + 0x10);
  107. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  108. long Timestamp = Stopwatch.GetTimestamp();
  109. WriteInt64(ControllerOffset + 0x0, Timestamp);
  110. WriteInt64(ControllerOffset + 0x8, HidEntryCount);
  111. WriteInt64(ControllerOffset + 0x10, CurrEntry);
  112. WriteInt64(ControllerOffset + 0x18, HidEntryCount - 1);
  113. ControllerOffset += HidControllersLayoutHeaderSize;
  114. ControllerOffset += CurrEntry * HidControllersInputEntrySize;
  115. WriteInt64(ControllerOffset + 0x0, Timestamp);
  116. WriteInt64(ControllerOffset + 0x8, Timestamp);
  117. WriteInt64(ControllerOffset + 0x10, (uint)Buttons);
  118. WriteInt32(ControllerOffset + 0x18, LeftStick.DX);
  119. WriteInt32(ControllerOffset + 0x1c, LeftStick.DY);
  120. WriteInt64(ControllerOffset + 0x20, RightStick.DX);
  121. WriteInt64(ControllerOffset + 0x24, RightStick.DY);
  122. WriteInt64(ControllerOffset + 0x28,
  123. (uint)HidControllerConnState.Controller_State_Connected |
  124. (uint)HidControllerConnState.Controller_State_Wired);
  125. }
  126. public void SetTouchPoints(params HidTouchPoint[] Points)
  127. {
  128. long LastEntry = ReadInt64(HidTouchScreenOffset + 0x10);
  129. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  130. long Timestamp = Stopwatch.GetTimestamp();
  131. WriteInt64(HidTouchScreenOffset + 0x0, Timestamp);
  132. WriteInt64(HidTouchScreenOffset + 0x8, HidEntryCount);
  133. WriteInt64(HidTouchScreenOffset + 0x10, CurrEntry);
  134. WriteInt64(HidTouchScreenOffset + 0x18, HidEntryCount - 1);
  135. WriteInt64(HidTouchScreenOffset + 0x20, Timestamp);
  136. long TouchEntryOffset = HidTouchScreenOffset + HidTouchHeaderSize;
  137. long LastEntryOffset = TouchEntryOffset + LastEntry * HidTouchEntrySize;
  138. long LastTimestamp = ReadInt64(LastEntryOffset);
  139. TouchEntryOffset += CurrEntry * HidTouchEntrySize;
  140. WriteInt64(TouchEntryOffset + 0x0, LastTimestamp + 1);
  141. WriteInt64(TouchEntryOffset + 0x8, Points.Length);
  142. TouchEntryOffset += HidTouchEntryHeaderSize;
  143. const int Padding = 0;
  144. int Index = 0;
  145. foreach (HidTouchPoint Point in Points)
  146. {
  147. WriteInt64(TouchEntryOffset + 0x0, Timestamp);
  148. WriteInt32(TouchEntryOffset + 0x8, Padding);
  149. WriteInt32(TouchEntryOffset + 0xc, Index++);
  150. WriteInt32(TouchEntryOffset + 0x10, Point.X);
  151. WriteInt32(TouchEntryOffset + 0x14, Point.Y);
  152. WriteInt32(TouchEntryOffset + 0x18, Point.DiameterX);
  153. WriteInt32(TouchEntryOffset + 0x1c, Point.DiameterY);
  154. WriteInt32(TouchEntryOffset + 0x20, Point.Angle);
  155. WriteInt32(TouchEntryOffset + 0x24, Padding);
  156. TouchEntryOffset += HidTouchEntryTouchSize;
  157. }
  158. }
  159. private unsafe long ReadInt64(long Position)
  160. {
  161. Position += SharedMemOffset;
  162. if ((ulong)Position + 8 > AMemoryMgr.AddrSize) return 0;
  163. return *((long*)((byte*)Ns.Ram + Position));
  164. }
  165. private unsafe void WriteInt32(long Position, int Value)
  166. {
  167. Position += SharedMemOffset;
  168. if ((ulong)Position + 4 > AMemoryMgr.AddrSize) return;
  169. *((int*)((byte*)Ns.Ram + Position)) = Value;
  170. }
  171. private unsafe void WriteInt64(long Position, long Value)
  172. {
  173. Position += SharedMemOffset;
  174. if ((ulong)Position + 8 > AMemoryMgr.AddrSize) return;
  175. *((long*)((byte*)Ns.Ram + Position)) = Value;
  176. }
  177. }
  178. }