Hid.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 HidControllerButtons UpdateStickButtons(
  34. HidJoystickPosition leftStick,
  35. HidJoystickPosition rightStick)
  36. {
  37. HidControllerButtons result = 0;
  38. if (rightStick.Dx < 0)
  39. {
  40. result |= HidControllerButtons.RStickLeft;
  41. }
  42. if (rightStick.Dx > 0)
  43. {
  44. result |= HidControllerButtons.RStickRight;
  45. }
  46. if (rightStick.Dy < 0)
  47. {
  48. result |= HidControllerButtons.RStickDown;
  49. }
  50. if (rightStick.Dy > 0)
  51. {
  52. result |= HidControllerButtons.RStickUp;
  53. }
  54. if (leftStick.Dx < 0)
  55. {
  56. result |= HidControllerButtons.LStickLeft;
  57. }
  58. if (leftStick.Dx > 0)
  59. {
  60. result |= HidControllerButtons.LStickRight;
  61. }
  62. if (leftStick.Dy < 0)
  63. {
  64. result |= HidControllerButtons.LStickDown;
  65. }
  66. if (leftStick.Dy > 0)
  67. {
  68. result |= HidControllerButtons.LStickUp;
  69. }
  70. return result;
  71. }
  72. public void SetTouchPoints(params HidTouchPoint[] points)
  73. {
  74. long touchScreenOffset = HidPosition + HidTouchScreenOffset;
  75. long lastEntry = _device.Memory.ReadInt64(touchScreenOffset + 0x10);
  76. long currEntry = (lastEntry + 1) % HidEntryCount;
  77. long timestamp = GetTimestamp();
  78. _device.Memory.WriteInt64(touchScreenOffset + 0x00, timestamp);
  79. _device.Memory.WriteInt64(touchScreenOffset + 0x08, HidEntryCount);
  80. _device.Memory.WriteInt64(touchScreenOffset + 0x10, currEntry);
  81. _device.Memory.WriteInt64(touchScreenOffset + 0x18, HidEntryCount - 1);
  82. _device.Memory.WriteInt64(touchScreenOffset + 0x20, timestamp);
  83. long touchEntryOffset = touchScreenOffset + HidTouchHeaderSize;
  84. long lastEntryOffset = touchEntryOffset + lastEntry * HidTouchEntrySize;
  85. long sampleCounter = _device.Memory.ReadInt64(lastEntryOffset) + 1;
  86. touchEntryOffset += currEntry * HidTouchEntrySize;
  87. _device.Memory.WriteInt64(touchEntryOffset + 0x00, sampleCounter);
  88. _device.Memory.WriteInt64(touchEntryOffset + 0x08, points.Length);
  89. touchEntryOffset += HidTouchEntryHeaderSize;
  90. const int padding = 0;
  91. int index = 0;
  92. foreach (HidTouchPoint point in points)
  93. {
  94. _device.Memory.WriteInt64(touchEntryOffset + 0x00, sampleCounter);
  95. _device.Memory.WriteInt32(touchEntryOffset + 0x08, padding);
  96. _device.Memory.WriteInt32(touchEntryOffset + 0x0c, index++);
  97. _device.Memory.WriteInt32(touchEntryOffset + 0x10, point.X);
  98. _device.Memory.WriteInt32(touchEntryOffset + 0x14, point.Y);
  99. _device.Memory.WriteInt32(touchEntryOffset + 0x18, point.DiameterX);
  100. _device.Memory.WriteInt32(touchEntryOffset + 0x1c, point.DiameterY);
  101. _device.Memory.WriteInt32(touchEntryOffset + 0x20, point.Angle);
  102. _device.Memory.WriteInt32(touchEntryOffset + 0x24, padding);
  103. touchEntryOffset += HidTouchEntryTouchSize;
  104. }
  105. }
  106. internal static long GetTimestamp()
  107. {
  108. return PerformanceCounter.ElapsedMilliseconds * 19200;
  109. }
  110. }
  111. }