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. this.Device = Device;
  13. this.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.CONTROLLER_HANDHELD : HidControllerId.CONTROLLER_PLAYER_1;
  20. if (ControllerType == HidControllerType.ProController)
  21. {
  22. PrimaryController = new HidProController(Device);
  23. }
  24. else
  25. {
  26. PrimaryController = new HidNpadController(ControllerType,
  27. Device,
  28. (NpadColor.Body_Neon_Red, NpadColor.Body_Neon_Red),
  29. (NpadColor.Buttons_Neon_Blue, NpadColor.Buttons_Neon_Blue));
  30. }
  31. PrimaryController.Connect(ControllerId);
  32. }
  33. private 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. }