Hid.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. public HidControllerBase PrimaryController { get; private set; }
  9. internal long HidPosition;
  10. public Hid(Switch device, long hidPosition)
  11. {
  12. _device = device;
  13. HidPosition = hidPosition;
  14. device.Memory.FillWithZeros(hidPosition, Horizon.HidSize);
  15. }
  16. public void InitilizePrimaryController(HidControllerType controllerType)
  17. {
  18. HidControllerId controllerId = controllerType == HidControllerType.Handheld ?
  19. HidControllerId.ControllerHandheld : HidControllerId.ControllerPlayer1;
  20. if (controllerType == HidControllerType.ProController)
  21. {
  22. PrimaryController = new HidProController(_device);
  23. }
  24. else
  25. {
  26. PrimaryController = new HidNpadController(controllerType,
  27. _device,
  28. (NpadColor.BodyNeonRed, NpadColor.BodyNeonRed),
  29. (NpadColor.ButtonsNeonBlue, NpadColor.ButtonsNeonBlue));
  30. }
  31. PrimaryController.Connect(controllerId);
  32. }
  33. public void InitilizeKeyboard()
  34. {
  35. _device.Memory.FillWithZeros(HidPosition + HidKeyboardOffset, HidKeyboardSize);
  36. }
  37. public HidControllerButtons UpdateStickButtons(
  38. HidJoystickPosition leftStick,
  39. HidJoystickPosition rightStick)
  40. {
  41. HidControllerButtons result = 0;
  42. if (rightStick.Dx < 0)
  43. {
  44. result |= HidControllerButtons.RStickLeft;
  45. }
  46. if (rightStick.Dx > 0)
  47. {
  48. result |= HidControllerButtons.RStickRight;
  49. }
  50. if (rightStick.Dy < 0)
  51. {
  52. result |= HidControllerButtons.RStickDown;
  53. }
  54. if (rightStick.Dy > 0)
  55. {
  56. result |= HidControllerButtons.RStickUp;
  57. }
  58. if (leftStick.Dx < 0)
  59. {
  60. result |= HidControllerButtons.LStickLeft;
  61. }
  62. if (leftStick.Dx > 0)
  63. {
  64. result |= HidControllerButtons.LStickRight;
  65. }
  66. if (leftStick.Dy < 0)
  67. {
  68. result |= HidControllerButtons.LStickDown;
  69. }
  70. if (leftStick.Dy > 0)
  71. {
  72. result |= HidControllerButtons.LStickUp;
  73. }
  74. return result;
  75. }
  76. public void SetTouchPoints(params HidTouchPoint[] points)
  77. {
  78. long touchScreenOffset = HidPosition + HidTouchScreenOffset;
  79. long lastEntry = _device.Memory.ReadInt64(touchScreenOffset + 0x10);
  80. long currEntry = (lastEntry + 1) % HidEntryCount;
  81. long timestamp = GetTimestamp();
  82. _device.Memory.WriteInt64(touchScreenOffset + 0x00, timestamp);
  83. _device.Memory.WriteInt64(touchScreenOffset + 0x08, HidEntryCount);
  84. _device.Memory.WriteInt64(touchScreenOffset + 0x10, currEntry);
  85. _device.Memory.WriteInt64(touchScreenOffset + 0x18, HidEntryCount - 1);
  86. _device.Memory.WriteInt64(touchScreenOffset + 0x20, timestamp);
  87. long touchEntryOffset = touchScreenOffset + HidTouchHeaderSize;
  88. long lastEntryOffset = touchEntryOffset + lastEntry * HidTouchEntrySize;
  89. long sampleCounter = _device.Memory.ReadInt64(lastEntryOffset) + 1;
  90. touchEntryOffset += currEntry * HidTouchEntrySize;
  91. _device.Memory.WriteInt64(touchEntryOffset + 0x00, sampleCounter);
  92. _device.Memory.WriteInt64(touchEntryOffset + 0x08, points.Length);
  93. touchEntryOffset += HidTouchEntryHeaderSize;
  94. const int padding = 0;
  95. int index = 0;
  96. foreach (HidTouchPoint point in points)
  97. {
  98. _device.Memory.WriteInt64(touchEntryOffset + 0x00, sampleCounter);
  99. _device.Memory.WriteInt32(touchEntryOffset + 0x08, padding);
  100. _device.Memory.WriteInt32(touchEntryOffset + 0x0c, index++);
  101. _device.Memory.WriteInt32(touchEntryOffset + 0x10, point.X);
  102. _device.Memory.WriteInt32(touchEntryOffset + 0x14, point.Y);
  103. _device.Memory.WriteInt32(touchEntryOffset + 0x18, point.DiameterX);
  104. _device.Memory.WriteInt32(touchEntryOffset + 0x1c, point.DiameterY);
  105. _device.Memory.WriteInt32(touchEntryOffset + 0x20, point.Angle);
  106. _device.Memory.WriteInt32(touchEntryOffset + 0x24, padding);
  107. touchEntryOffset += HidTouchEntryTouchSize;
  108. }
  109. }
  110. public void WriteKeyboard(HidKeyboard keyboard)
  111. {
  112. long keyboardOffset = HidPosition + HidKeyboardOffset;
  113. long lastEntry = _device.Memory.ReadInt64(keyboardOffset + 0x10);
  114. long currEntry = (lastEntry + 1) % HidEntryCount;
  115. long timestamp = GetTimestamp();
  116. _device.Memory.WriteInt64(keyboardOffset + 0x00, timestamp);
  117. _device.Memory.WriteInt64(keyboardOffset + 0x08, HidEntryCount);
  118. _device.Memory.WriteInt64(keyboardOffset + 0x10, currEntry);
  119. _device.Memory.WriteInt64(keyboardOffset + 0x18, HidEntryCount - 1);
  120. long keyboardEntryOffset = keyboardOffset + HidKeyboardHeaderSize;
  121. long lastEntryOffset = keyboardEntryOffset + lastEntry * HidKeyboardEntrySize;
  122. long sampleCounter = _device.Memory.ReadInt64(lastEntryOffset);
  123. keyboardEntryOffset += currEntry * HidKeyboardEntrySize;
  124. _device.Memory.WriteInt64(keyboardEntryOffset + 0x00, sampleCounter + 1);
  125. _device.Memory.WriteInt64(keyboardEntryOffset + 0x08, sampleCounter);
  126. _device.Memory.WriteInt64(keyboardEntryOffset + 0x10, keyboard.Modifier);
  127. for (int i = 0; i < keyboard.Keys.Length; i++)
  128. {
  129. _device.Memory.WriteInt32(keyboardEntryOffset + 0x18 + (i * 4), keyboard.Keys[i]);
  130. }
  131. }
  132. internal static long GetTimestamp()
  133. {
  134. return PerformanceCounter.ElapsedMilliseconds * 19200;
  135. }
  136. }
  137. }