Hid.cs 12 KB

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