Hid.cs 6.6 KB

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