Hid.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using Ryujinx.Common;
  2. using Ryujinx.Configuration.Hid;
  3. using Ryujinx.HLE.HOS;
  4. using System;
  5. namespace Ryujinx.HLE.Input
  6. {
  7. public partial class Hid
  8. {
  9. private Switch _device;
  10. private long _touchScreenOffset;
  11. private long _touchEntriesOffset;
  12. private long _keyboardOffset;
  13. private TouchHeader _currentTouchHeader;
  14. private KeyboardHeader _currentKeyboardHeader;
  15. private KeyboardEntry _currentKeyboardEntry;
  16. public BaseController PrimaryController { get; private set; }
  17. internal long HidPosition;
  18. public Hid(Switch device, long hidPosition)
  19. {
  20. _device = device;
  21. HidPosition = hidPosition;
  22. device.Memory.FillWithZeros(hidPosition, Horizon.HidSize);
  23. _currentTouchHeader = new TouchHeader()
  24. {
  25. CurrentEntryIndex = -1,
  26. };
  27. _currentKeyboardHeader = new KeyboardHeader()
  28. {
  29. CurrentEntryIndex = -1,
  30. };
  31. _currentKeyboardEntry = new KeyboardEntry()
  32. {
  33. SamplesTimestamp = -1,
  34. SamplesTimestamp2 = -1
  35. };
  36. _touchScreenOffset = HidPosition + HidTouchScreenOffset;
  37. _touchEntriesOffset = _touchScreenOffset + HidTouchHeaderSize;
  38. _keyboardOffset = HidPosition + HidKeyboardOffset;
  39. }
  40. private static ControllerStatus ConvertControllerTypeToState(ControllerType controllerType)
  41. {
  42. switch (controllerType)
  43. {
  44. case ControllerType.Handheld: return ControllerStatus.Handheld;
  45. case ControllerType.NpadLeft: return ControllerStatus.NpadLeft;
  46. case ControllerType.NpadRight: return ControllerStatus.NpadRight;
  47. case ControllerType.NpadPair: return ControllerStatus.NpadPair;
  48. case ControllerType.ProController: return ControllerStatus.ProController;
  49. default: throw new NotImplementedException();
  50. }
  51. }
  52. public void InitializePrimaryController(ControllerType controllerType)
  53. {
  54. ControllerId controllerId = controllerType == ControllerType.Handheld ?
  55. ControllerId.ControllerHandheld : ControllerId.ControllerPlayer1;
  56. if (controllerType == ControllerType.ProController)
  57. {
  58. PrimaryController = new ProController(_device, NpadColor.Black, NpadColor.Black);
  59. }
  60. else
  61. {
  62. PrimaryController = new NpadController(ConvertControllerTypeToState(controllerType),
  63. _device,
  64. (NpadColor.BodyNeonRed, NpadColor.BodyNeonRed),
  65. (NpadColor.ButtonsNeonBlue, NpadColor.ButtonsNeonBlue));
  66. }
  67. PrimaryController.Connect(controllerId);
  68. }
  69. public ControllerButtons UpdateStickButtons(
  70. JoystickPosition leftStick,
  71. JoystickPosition rightStick)
  72. {
  73. ControllerButtons result = 0;
  74. if (rightStick.Dx < 0)
  75. {
  76. result |= ControllerButtons.RStickLeft;
  77. }
  78. if (rightStick.Dx > 0)
  79. {
  80. result |= ControllerButtons.RStickRight;
  81. }
  82. if (rightStick.Dy < 0)
  83. {
  84. result |= ControllerButtons.RStickDown;
  85. }
  86. if (rightStick.Dy > 0)
  87. {
  88. result |= ControllerButtons.RStickUp;
  89. }
  90. if (leftStick.Dx < 0)
  91. {
  92. result |= ControllerButtons.LStickLeft;
  93. }
  94. if (leftStick.Dx > 0)
  95. {
  96. result |= ControllerButtons.LStickRight;
  97. }
  98. if (leftStick.Dy < 0)
  99. {
  100. result |= ControllerButtons.LStickDown;
  101. }
  102. if (leftStick.Dy > 0)
  103. {
  104. result |= ControllerButtons.LStickUp;
  105. }
  106. return result;
  107. }
  108. public void SetTouchPoints(params TouchPoint[] points)
  109. {
  110. long timestamp = GetTimestamp();
  111. long sampleCounter = _currentTouchHeader.SamplesTimestamp + 1;
  112. var newTouchHeader = new TouchHeader
  113. {
  114. CurrentEntryIndex = (_currentTouchHeader.CurrentEntryIndex + 1) % HidEntryCount,
  115. EntryCount = HidEntryCount,
  116. MaxEntries = HidEntryCount - 1,
  117. SamplesTimestamp = sampleCounter,
  118. Timestamp = timestamp,
  119. };
  120. long currentTouchEntryOffset = _touchEntriesOffset + newTouchHeader.CurrentEntryIndex * HidTouchEntrySize;
  121. TouchEntry touchEntry = new TouchEntry()
  122. {
  123. SamplesTimestamp = sampleCounter,
  124. TouchCount = points.Length
  125. };
  126. _device.Memory.WriteStruct(currentTouchEntryOffset, touchEntry);
  127. currentTouchEntryOffset += HidTouchEntryHeaderSize;
  128. for (int i = 0; i < points.Length; i++)
  129. {
  130. TouchData touch = new TouchData()
  131. {
  132. Angle = points[i].Angle,
  133. DiameterX = points[i].DiameterX,
  134. DiameterY = points[i].DiameterY,
  135. Index = i,
  136. SampleTimestamp = sampleCounter,
  137. X = points[i].X,
  138. Y = points[i].Y
  139. };
  140. _device.Memory.WriteStruct(currentTouchEntryOffset, touch);
  141. currentTouchEntryOffset += HidTouchEntryTouchSize;
  142. }
  143. _device.Memory.WriteStruct(_touchScreenOffset, newTouchHeader);
  144. _currentTouchHeader = newTouchHeader;
  145. }
  146. public unsafe void WriteKeyboard(Keyboard keyboard)
  147. {
  148. long timestamp = GetTimestamp();
  149. var newKeyboardHeader = new KeyboardHeader()
  150. {
  151. CurrentEntryIndex = (_currentKeyboardHeader.CurrentEntryIndex + 1) % HidEntryCount,
  152. EntryCount = HidEntryCount,
  153. MaxEntries = HidEntryCount - 1,
  154. Timestamp = timestamp,
  155. };
  156. _device.Memory.WriteStruct(_keyboardOffset, newKeyboardHeader);
  157. long keyboardEntryOffset = _keyboardOffset + HidKeyboardHeaderSize;
  158. keyboardEntryOffset += newKeyboardHeader.CurrentEntryIndex * HidKeyboardEntrySize;
  159. var newkeyboardEntry = new KeyboardEntry()
  160. {
  161. SamplesTimestamp = _currentKeyboardEntry.SamplesTimestamp + 1,
  162. SamplesTimestamp2 = _currentKeyboardEntry.SamplesTimestamp2 + 1,
  163. Keys = keyboard.Keys,
  164. Modifier = keyboard.Modifier,
  165. };
  166. _device.Memory.WriteStruct(keyboardEntryOffset, newkeyboardEntry);
  167. _currentKeyboardEntry = newkeyboardEntry;
  168. _currentKeyboardHeader = newKeyboardHeader;
  169. }
  170. internal static long GetTimestamp()
  171. {
  172. return PerformanceCounter.ElapsedMilliseconds * 19200;
  173. }
  174. }
  175. }