Hid.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Core.OsHle.Handles;
  3. using System;
  4. namespace Ryujinx.Core.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 object ShMemLock;
  56. private (AMemory, long)[] ShMemPositions;
  57. public Hid()
  58. {
  59. ShMemLock = new object();
  60. ShMemPositions = new (AMemory, long)[0];
  61. }
  62. internal void ShMemMap(object sender, EventArgs e)
  63. {
  64. HSharedMem SharedMem = (HSharedMem)sender;
  65. lock (ShMemLock)
  66. {
  67. ShMemPositions = SharedMem.GetVirtualPositions();
  68. (AMemory Memory, long Position) ShMem = ShMemPositions[ShMemPositions.Length - 1];
  69. Logging.Info($"HID shared memory successfully mapped to 0x{ShMem.Position:x16}!");
  70. Init(ShMem.Memory, ShMem.Position);
  71. }
  72. }
  73. internal void ShMemUnmap(object sender, EventArgs e)
  74. {
  75. HSharedMem SharedMem = (HSharedMem)sender;
  76. lock (ShMemLock)
  77. {
  78. ShMemPositions = SharedMem.GetVirtualPositions();
  79. }
  80. }
  81. private void Init(AMemory Memory, long Position)
  82. {
  83. InitializeJoyconPair(
  84. Memory,
  85. Position,
  86. JoyConColor.Body_Neon_Red,
  87. JoyConColor.Buttons_Neon_Red,
  88. JoyConColor.Body_Neon_Blue,
  89. JoyConColor.Buttons_Neon_Blue);
  90. }
  91. private void InitializeJoyconPair(
  92. AMemory Memory,
  93. long Position,
  94. JoyConColor LeftColorBody,
  95. JoyConColor LeftColorButtons,
  96. JoyConColor RightColorBody,
  97. JoyConColor RightColorButtons)
  98. {
  99. long BaseControllerOffset = Position + HidControllersOffset + 8 * HidControllerSize;
  100. HidControllerType Type =
  101. HidControllerType.ControllerType_Handheld |
  102. HidControllerType.ControllerType_JoyconPair;
  103. bool IsHalf = false;
  104. HidControllerColorDesc SingleColorDesc =
  105. HidControllerColorDesc.ColorDesc_ColorsNonexistent;
  106. JoyConColor SingleColorBody = JoyConColor.Black;
  107. JoyConColor SingleColorButtons = JoyConColor.Black;
  108. HidControllerColorDesc SplitColorDesc = 0;
  109. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x0, (int)Type);
  110. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x4, IsHalf ? 1 : 0);
  111. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x8, (int)SingleColorDesc);
  112. Memory.WriteInt32Unchecked(BaseControllerOffset + 0xc, (int)SingleColorBody);
  113. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x10, (int)SingleColorButtons);
  114. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x14, (int)SplitColorDesc);
  115. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x18, (int)LeftColorBody);
  116. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x1c, (int)LeftColorButtons);
  117. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x20, (int)RightColorBody);
  118. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x24, (int)RightColorButtons);
  119. }
  120. public void SetJoyconButton(
  121. HidControllerId ControllerId,
  122. HidControllerLayouts ControllerLayout,
  123. HidControllerButtons Buttons,
  124. HidJoystickPosition LeftStick,
  125. HidJoystickPosition RightStick)
  126. {
  127. lock (ShMemLock)
  128. {
  129. foreach ((AMemory Memory, long Position) in ShMemPositions)
  130. {
  131. long ControllerOffset = Position + HidControllersOffset;
  132. ControllerOffset += (int)ControllerId * HidControllerSize;
  133. ControllerOffset += HidControllerHeaderSize;
  134. ControllerOffset += (int)ControllerLayout * HidControllerLayoutsSize;
  135. long LastEntry = Memory.ReadInt64Unchecked(ControllerOffset + 0x10);
  136. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  137. long Timestamp = GetTimestamp();
  138. Memory.WriteInt64Unchecked(ControllerOffset + 0x0, Timestamp);
  139. Memory.WriteInt64Unchecked(ControllerOffset + 0x8, HidEntryCount);
  140. Memory.WriteInt64Unchecked(ControllerOffset + 0x10, CurrEntry);
  141. Memory.WriteInt64Unchecked(ControllerOffset + 0x18, HidEntryCount - 1);
  142. ControllerOffset += HidControllersLayoutHeaderSize;
  143. ControllerOffset += CurrEntry * HidControllersInputEntrySize;
  144. Memory.WriteInt64Unchecked(ControllerOffset + 0x0, Timestamp);
  145. Memory.WriteInt64Unchecked(ControllerOffset + 0x8, Timestamp);
  146. Memory.WriteInt64Unchecked(ControllerOffset + 0x10, (uint)Buttons);
  147. Memory.WriteInt32Unchecked(ControllerOffset + 0x18, LeftStick.DX);
  148. Memory.WriteInt32Unchecked(ControllerOffset + 0x1c, LeftStick.DY);
  149. Memory.WriteInt64Unchecked(ControllerOffset + 0x20, RightStick.DX);
  150. Memory.WriteInt64Unchecked(ControllerOffset + 0x24, RightStick.DY);
  151. Memory.WriteInt64Unchecked(ControllerOffset + 0x28,
  152. (uint)HidControllerConnState.Controller_State_Connected |
  153. (uint)HidControllerConnState.Controller_State_Wired);
  154. }
  155. }
  156. }
  157. public void SetTouchPoints(params HidTouchPoint[] Points)
  158. {
  159. lock (ShMemLock)
  160. {
  161. foreach ((AMemory Memory, long Position) in ShMemPositions)
  162. {
  163. long TouchScreenOffset = Position + HidTouchScreenOffset;
  164. long LastEntry = Memory.ReadInt64Unchecked(TouchScreenOffset + 0x10);
  165. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  166. long Timestamp = GetTimestamp();
  167. Memory.WriteInt64Unchecked(TouchScreenOffset + 0x0, Timestamp);
  168. Memory.WriteInt64Unchecked(TouchScreenOffset + 0x8, HidEntryCount);
  169. Memory.WriteInt64Unchecked(TouchScreenOffset + 0x10, CurrEntry);
  170. Memory.WriteInt64Unchecked(TouchScreenOffset + 0x18, HidEntryCount - 1);
  171. Memory.WriteInt64Unchecked(TouchScreenOffset + 0x20, Timestamp);
  172. long TouchEntryOffset = TouchScreenOffset + HidTouchHeaderSize;
  173. long LastEntryOffset = TouchEntryOffset + LastEntry * HidTouchEntrySize;
  174. long SampleCounter = Memory.ReadInt64Unchecked(LastEntryOffset) + 1;
  175. TouchEntryOffset += CurrEntry * HidTouchEntrySize;
  176. Memory.WriteInt64Unchecked(TouchEntryOffset + 0x0, SampleCounter);
  177. Memory.WriteInt64Unchecked(TouchEntryOffset + 0x8, Points.Length);
  178. TouchEntryOffset += HidTouchEntryHeaderSize;
  179. const int Padding = 0;
  180. int Index = 0;
  181. foreach (HidTouchPoint Point in Points)
  182. {
  183. Memory.WriteInt64Unchecked(TouchEntryOffset + 0x0, Timestamp);
  184. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x8, Padding);
  185. Memory.WriteInt32Unchecked(TouchEntryOffset + 0xc, Index++);
  186. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x10, Point.X);
  187. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x14, Point.Y);
  188. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x18, Point.DiameterX);
  189. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x1c, Point.DiameterY);
  190. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x20, Point.Angle);
  191. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x24, Padding);
  192. TouchEntryOffset += HidTouchEntryTouchSize;
  193. }
  194. }
  195. }
  196. }
  197. private static long GetTimestamp()
  198. {
  199. return (long)((ulong)Environment.TickCount * 19_200);
  200. }
  201. }
  202. }