Hid.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Core.Logging;
  3. using Ryujinx.Core.OsHle;
  4. using Ryujinx.Core.OsHle.Handles;
  5. using System;
  6. namespace Ryujinx.Core.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 =
  109. HidControllerType.ControllerType_Handheld |
  110. HidControllerType.ControllerType_JoyconPair;
  111. bool IsHalf = false;
  112. HidControllerColorDesc SingleColorDesc =
  113. HidControllerColorDesc.ColorDesc_ColorsNonexistent;
  114. JoyConColor SingleColorBody = JoyConColor.Black;
  115. JoyConColor SingleColorButtons = JoyConColor.Black;
  116. HidControllerColorDesc SplitColorDesc = 0;
  117. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x0, (int)Type);
  118. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x4, IsHalf ? 1 : 0);
  119. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x8, (int)SingleColorDesc);
  120. Memory.WriteInt32Unchecked(BaseControllerOffset + 0xc, (int)SingleColorBody);
  121. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x10, (int)SingleColorButtons);
  122. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x14, (int)SplitColorDesc);
  123. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x18, (int)LeftColorBody);
  124. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x1c, (int)LeftColorButtons);
  125. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x20, (int)RightColorBody);
  126. Memory.WriteInt32Unchecked(BaseControllerOffset + 0x24, (int)RightColorButtons);
  127. }
  128. public void SetJoyconButton(
  129. HidControllerId ControllerId,
  130. HidControllerLayouts ControllerLayout,
  131. HidControllerButtons Buttons,
  132. HidJoystickPosition LeftStick,
  133. HidJoystickPosition RightStick)
  134. {
  135. lock (ShMemLock)
  136. {
  137. foreach ((AMemory Memory, long Position) in ShMemPositions)
  138. {
  139. long ControllerOffset = Position + HidControllersOffset;
  140. ControllerOffset += (int)ControllerId * HidControllerSize;
  141. ControllerOffset += HidControllerHeaderSize;
  142. ControllerOffset += (int)ControllerLayout * HidControllerLayoutsSize;
  143. long LastEntry = Memory.ReadInt64Unchecked(ControllerOffset + 0x10);
  144. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  145. long Timestamp = GetTimestamp();
  146. Memory.WriteInt64Unchecked(ControllerOffset + 0x0, Timestamp);
  147. Memory.WriteInt64Unchecked(ControllerOffset + 0x8, HidEntryCount);
  148. Memory.WriteInt64Unchecked(ControllerOffset + 0x10, CurrEntry);
  149. Memory.WriteInt64Unchecked(ControllerOffset + 0x18, HidEntryCount - 1);
  150. ControllerOffset += HidControllersLayoutHeaderSize;
  151. long LastEntryOffset = ControllerOffset + LastEntry * HidControllersInputEntrySize;
  152. ControllerOffset += CurrEntry * HidControllersInputEntrySize;
  153. long SampleCounter = Memory.ReadInt64Unchecked(LastEntryOffset) + 1;
  154. Memory.WriteInt64Unchecked(ControllerOffset + 0x0, SampleCounter);
  155. Memory.WriteInt64Unchecked(ControllerOffset + 0x8, SampleCounter);
  156. Memory.WriteInt64Unchecked(ControllerOffset + 0x10, (uint)Buttons);
  157. Memory.WriteInt32Unchecked(ControllerOffset + 0x18, LeftStick.DX);
  158. Memory.WriteInt32Unchecked(ControllerOffset + 0x1c, LeftStick.DY);
  159. Memory.WriteInt32Unchecked(ControllerOffset + 0x20, RightStick.DX);
  160. Memory.WriteInt32Unchecked(ControllerOffset + 0x24, RightStick.DY);
  161. Memory.WriteInt64Unchecked(ControllerOffset + 0x28,
  162. (uint)HidControllerConnState.Controller_State_Connected |
  163. (uint)HidControllerConnState.Controller_State_Wired);
  164. }
  165. }
  166. }
  167. public void SetTouchPoints(params HidTouchPoint[] Points)
  168. {
  169. lock (ShMemLock)
  170. {
  171. foreach ((AMemory Memory, long Position) in ShMemPositions)
  172. {
  173. long TouchScreenOffset = Position + HidTouchScreenOffset;
  174. long LastEntry = Memory.ReadInt64Unchecked(TouchScreenOffset + 0x10);
  175. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  176. long Timestamp = GetTimestamp();
  177. Memory.WriteInt64Unchecked(TouchScreenOffset + 0x0, Timestamp);
  178. Memory.WriteInt64Unchecked(TouchScreenOffset + 0x8, HidEntryCount);
  179. Memory.WriteInt64Unchecked(TouchScreenOffset + 0x10, CurrEntry);
  180. Memory.WriteInt64Unchecked(TouchScreenOffset + 0x18, HidEntryCount - 1);
  181. Memory.WriteInt64Unchecked(TouchScreenOffset + 0x20, Timestamp);
  182. long TouchEntryOffset = TouchScreenOffset + HidTouchHeaderSize;
  183. long LastEntryOffset = TouchEntryOffset + LastEntry * HidTouchEntrySize;
  184. long SampleCounter = Memory.ReadInt64Unchecked(LastEntryOffset) + 1;
  185. TouchEntryOffset += CurrEntry * HidTouchEntrySize;
  186. Memory.WriteInt64Unchecked(TouchEntryOffset + 0x0, SampleCounter);
  187. Memory.WriteInt64Unchecked(TouchEntryOffset + 0x8, Points.Length);
  188. TouchEntryOffset += HidTouchEntryHeaderSize;
  189. const int Padding = 0;
  190. int Index = 0;
  191. foreach (HidTouchPoint Point in Points)
  192. {
  193. Memory.WriteInt64Unchecked(TouchEntryOffset + 0x0, Timestamp);
  194. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x8, Padding);
  195. Memory.WriteInt32Unchecked(TouchEntryOffset + 0xc, Index++);
  196. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x10, Point.X);
  197. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x14, Point.Y);
  198. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x18, Point.DiameterX);
  199. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x1c, Point.DiameterY);
  200. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x20, Point.Angle);
  201. Memory.WriteInt32Unchecked(TouchEntryOffset + 0x24, Padding);
  202. TouchEntryOffset += HidTouchEntryTouchSize;
  203. }
  204. }
  205. }
  206. }
  207. private static long GetTimestamp()
  208. {
  209. return (long)((ulong)Environment.TickCount * 19_200);
  210. }
  211. }
  212. }