Hid.cs 11 KB

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