Hid.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using Ryujinx.HLE.HOS;
  2. using System;
  3. namespace Ryujinx.HLE.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 Switch Device;
  55. private long HidPosition;
  56. public Hid(Switch Device, long HidPosition)
  57. {
  58. this.Device = Device;
  59. this.HidPosition = HidPosition;
  60. Device.Memory.FillWithZeros(HidPosition, Horizon.HidSize);
  61. InitializeJoyconPair(
  62. JoyConColor.Body_Neon_Red,
  63. JoyConColor.Buttons_Neon_Red,
  64. JoyConColor.Body_Neon_Blue,
  65. JoyConColor.Buttons_Neon_Blue);
  66. }
  67. private void InitializeJoyconPair(
  68. JoyConColor LeftColorBody,
  69. JoyConColor LeftColorButtons,
  70. JoyConColor RightColorBody,
  71. JoyConColor RightColorButtons)
  72. {
  73. long BaseControllerOffset = HidPosition + HidControllersOffset + 8 * HidControllerSize;
  74. HidControllerType Type = HidControllerType.ControllerType_Handheld;
  75. bool IsHalf = false;
  76. HidControllerColorDesc SingleColorDesc =
  77. HidControllerColorDesc.ColorDesc_ColorsNonexistent;
  78. JoyConColor SingleColorBody = JoyConColor.Black;
  79. JoyConColor SingleColorButtons = JoyConColor.Black;
  80. HidControllerColorDesc SplitColorDesc = 0;
  81. Device.Memory.WriteInt32(BaseControllerOffset + 0x0, (int)Type);
  82. Device.Memory.WriteInt32(BaseControllerOffset + 0x4, IsHalf ? 1 : 0);
  83. Device.Memory.WriteInt32(BaseControllerOffset + 0x8, (int)SingleColorDesc);
  84. Device.Memory.WriteInt32(BaseControllerOffset + 0xc, (int)SingleColorBody);
  85. Device.Memory.WriteInt32(BaseControllerOffset + 0x10, (int)SingleColorButtons);
  86. Device.Memory.WriteInt32(BaseControllerOffset + 0x14, (int)SplitColorDesc);
  87. Device.Memory.WriteInt32(BaseControllerOffset + 0x18, (int)LeftColorBody);
  88. Device.Memory.WriteInt32(BaseControllerOffset + 0x1c, (int)LeftColorButtons);
  89. Device.Memory.WriteInt32(BaseControllerOffset + 0x20, (int)RightColorBody);
  90. Device.Memory.WriteInt32(BaseControllerOffset + 0x24, (int)RightColorButtons);
  91. }
  92. public void SetJoyconButton(
  93. HidControllerId ControllerId,
  94. HidControllerLayouts ControllerLayout,
  95. HidControllerButtons Buttons,
  96. HidJoystickPosition LeftStick,
  97. HidJoystickPosition RightStick)
  98. {
  99. long ControllerOffset = HidPosition + HidControllersOffset;
  100. ControllerOffset += (int)ControllerId * HidControllerSize;
  101. ControllerOffset += HidControllerHeaderSize;
  102. ControllerOffset += (int)ControllerLayout * HidControllerLayoutsSize;
  103. long LastEntry = Device.Memory.ReadInt64(ControllerOffset + 0x10);
  104. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  105. long Timestamp = GetTimestamp();
  106. Device.Memory.WriteInt64(ControllerOffset + 0x0, Timestamp);
  107. Device.Memory.WriteInt64(ControllerOffset + 0x8, HidEntryCount);
  108. Device.Memory.WriteInt64(ControllerOffset + 0x10, CurrEntry);
  109. Device.Memory.WriteInt64(ControllerOffset + 0x18, HidEntryCount - 1);
  110. ControllerOffset += HidControllersLayoutHeaderSize;
  111. long LastEntryOffset = ControllerOffset + LastEntry * HidControllersInputEntrySize;
  112. ControllerOffset += CurrEntry * HidControllersInputEntrySize;
  113. long SampleCounter = Device.Memory.ReadInt64(LastEntryOffset) + 1;
  114. Device.Memory.WriteInt64(ControllerOffset + 0x0, SampleCounter);
  115. Device.Memory.WriteInt64(ControllerOffset + 0x8, SampleCounter);
  116. Device.Memory.WriteInt64(ControllerOffset + 0x10, (uint)Buttons);
  117. Device.Memory.WriteInt32(ControllerOffset + 0x18, LeftStick.DX);
  118. Device.Memory.WriteInt32(ControllerOffset + 0x1c, LeftStick.DY);
  119. Device.Memory.WriteInt32(ControllerOffset + 0x20, RightStick.DX);
  120. Device.Memory.WriteInt32(ControllerOffset + 0x24, RightStick.DY);
  121. Device.Memory.WriteInt64(ControllerOffset + 0x28,
  122. (uint)HidControllerConnState.Controller_State_Connected |
  123. (uint)HidControllerConnState.Controller_State_Wired);
  124. }
  125. public void SetTouchPoints(params HidTouchPoint[] Points)
  126. {
  127. long TouchScreenOffset = HidPosition + HidTouchScreenOffset;
  128. long LastEntry = Device.Memory.ReadInt64(TouchScreenOffset + 0x10);
  129. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  130. long Timestamp = GetTimestamp();
  131. Device.Memory.WriteInt64(TouchScreenOffset + 0x0, Timestamp);
  132. Device.Memory.WriteInt64(TouchScreenOffset + 0x8, HidEntryCount);
  133. Device.Memory.WriteInt64(TouchScreenOffset + 0x10, CurrEntry);
  134. Device.Memory.WriteInt64(TouchScreenOffset + 0x18, HidEntryCount - 1);
  135. Device.Memory.WriteInt64(TouchScreenOffset + 0x20, Timestamp);
  136. long TouchEntryOffset = TouchScreenOffset + HidTouchHeaderSize;
  137. long LastEntryOffset = TouchEntryOffset + LastEntry * HidTouchEntrySize;
  138. long SampleCounter = Device.Memory.ReadInt64(LastEntryOffset) + 1;
  139. TouchEntryOffset += CurrEntry * HidTouchEntrySize;
  140. Device.Memory.WriteInt64(TouchEntryOffset + 0x0, SampleCounter);
  141. Device.Memory.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. Device.Memory.WriteInt64(TouchEntryOffset + 0x0, Timestamp);
  148. Device.Memory.WriteInt32(TouchEntryOffset + 0x8, Padding);
  149. Device.Memory.WriteInt32(TouchEntryOffset + 0xc, Index++);
  150. Device.Memory.WriteInt32(TouchEntryOffset + 0x10, Point.X);
  151. Device.Memory.WriteInt32(TouchEntryOffset + 0x14, Point.Y);
  152. Device.Memory.WriteInt32(TouchEntryOffset + 0x18, Point.DiameterX);
  153. Device.Memory.WriteInt32(TouchEntryOffset + 0x1c, Point.DiameterY);
  154. Device.Memory.WriteInt32(TouchEntryOffset + 0x20, Point.Angle);
  155. Device.Memory.WriteInt32(TouchEntryOffset + 0x24, Padding);
  156. TouchEntryOffset += HidTouchEntryTouchSize;
  157. }
  158. }
  159. private static long GetTimestamp()
  160. {
  161. return (long)((ulong)Environment.TickCount * 19_200);
  162. }
  163. }
  164. }