JoystickController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using OpenTK;
  2. using OpenTK.Input;
  3. using Ryujinx.Common.Configuration.Hid;
  4. using Ryujinx.HLE.HOS.Services.Hid;
  5. using System;
  6. using ControllerConfig = Ryujinx.Common.Configuration.Hid.ControllerConfig;
  7. namespace Ryujinx.Ui
  8. {
  9. public class JoystickController
  10. {
  11. private readonly ControllerConfig _config;
  12. public JoystickController(ControllerConfig config)
  13. {
  14. _config = config;
  15. }
  16. private bool IsEnabled()
  17. {
  18. return Joystick.GetState(_config.Index).IsConnected;
  19. }
  20. public ControllerKeys GetButtons()
  21. {
  22. // NOTE: This should be initialized AFTER GTK for compat reasons with OpenTK SDL2 backend and GTK on Linux.
  23. // BODY: Usage of Joystick.GetState must be defer to after GTK full initialization. Otherwise, GTK will segfault because SDL2 was already init *sighs*
  24. if (!IsEnabled())
  25. {
  26. return 0;
  27. }
  28. JoystickState joystickState = Joystick.GetState(_config.Index);
  29. ControllerKeys buttons = 0;
  30. if (IsActivated(joystickState, _config.LeftJoycon.DPadUp)) buttons |= ControllerKeys.DpadUp;
  31. if (IsActivated(joystickState, _config.LeftJoycon.DPadDown)) buttons |= ControllerKeys.DpadDown;
  32. if (IsActivated(joystickState, _config.LeftJoycon.DPadLeft)) buttons |= ControllerKeys.DpadLeft;
  33. if (IsActivated(joystickState, _config.LeftJoycon.DPadRight)) buttons |= ControllerKeys.DpadRight;
  34. if (IsActivated(joystickState, _config.LeftJoycon.StickButton)) buttons |= ControllerKeys.LStick;
  35. if (IsActivated(joystickState, _config.LeftJoycon.ButtonMinus)) buttons |= ControllerKeys.Minus;
  36. if (IsActivated(joystickState, _config.LeftJoycon.ButtonL)) buttons |= ControllerKeys.L;
  37. if (IsActivated(joystickState, _config.LeftJoycon.ButtonZl)) buttons |= ControllerKeys.Zl;
  38. if (IsActivated(joystickState, _config.LeftJoycon.ButtonSl)) buttons |= ControllerKeys.SlLeft;
  39. if (IsActivated(joystickState, _config.LeftJoycon.ButtonSr)) buttons |= ControllerKeys.SrLeft;
  40. if (IsActivated(joystickState, _config.RightJoycon.ButtonA)) buttons |= ControllerKeys.A;
  41. if (IsActivated(joystickState, _config.RightJoycon.ButtonB)) buttons |= ControllerKeys.B;
  42. if (IsActivated(joystickState, _config.RightJoycon.ButtonX)) buttons |= ControllerKeys.X;
  43. if (IsActivated(joystickState, _config.RightJoycon.ButtonY)) buttons |= ControllerKeys.Y;
  44. if (IsActivated(joystickState, _config.RightJoycon.StickButton)) buttons |= ControllerKeys.RStick;
  45. if (IsActivated(joystickState, _config.RightJoycon.ButtonPlus)) buttons |= ControllerKeys.Plus;
  46. if (IsActivated(joystickState, _config.RightJoycon.ButtonR)) buttons |= ControllerKeys.R;
  47. if (IsActivated(joystickState, _config.RightJoycon.ButtonZr)) buttons |= ControllerKeys.Zr;
  48. if (IsActivated(joystickState, _config.RightJoycon.ButtonSl)) buttons |= ControllerKeys.SlRight;
  49. if (IsActivated(joystickState, _config.RightJoycon.ButtonSr)) buttons |= ControllerKeys.SrRight;
  50. return buttons;
  51. }
  52. private bool IsActivated(JoystickState joystickState, ControllerInputId controllerInputId)
  53. {
  54. if (controllerInputId <= ControllerInputId.Button20)
  55. {
  56. return joystickState.IsButtonDown((int)controllerInputId);
  57. }
  58. else if (controllerInputId <= ControllerInputId.Axis5)
  59. {
  60. int axis = controllerInputId - ControllerInputId.Axis0;
  61. return joystickState.GetAxis(axis) > _config.TriggerThreshold;
  62. }
  63. else if (controllerInputId <= ControllerInputId.Hat2Right)
  64. {
  65. int hat = (controllerInputId - ControllerInputId.Hat0Up) / 4;
  66. int baseHatId = (int)ControllerInputId.Hat0Up + (hat * 4);
  67. JoystickHatState hatState = joystickState.GetHat((JoystickHat)hat);
  68. if (hatState.IsUp && ((int)controllerInputId % baseHatId == 0)) return true;
  69. if (hatState.IsDown && ((int)controllerInputId % baseHatId == 1)) return true;
  70. if (hatState.IsLeft && ((int)controllerInputId % baseHatId == 2)) return true;
  71. if (hatState.IsRight && ((int)controllerInputId % baseHatId == 3)) return true;
  72. }
  73. return false;
  74. }
  75. public (short, short) GetLeftStick()
  76. {
  77. if (!IsEnabled())
  78. {
  79. return (0, 0);
  80. }
  81. return GetStick(_config.LeftJoycon.StickX, _config.LeftJoycon.StickY, _config.DeadzoneLeft);
  82. }
  83. public (short, short) GetRightStick()
  84. {
  85. if (!IsEnabled())
  86. {
  87. return (0, 0);
  88. }
  89. return GetStick(_config.RightJoycon.StickX, _config.RightJoycon.StickY, _config.DeadzoneRight);
  90. }
  91. private (short, short) GetStick(ControllerInputId stickXInputId, ControllerInputId stickYInputId, float deadzone)
  92. {
  93. if (stickXInputId < ControllerInputId.Axis0 || stickXInputId > ControllerInputId.Axis5 ||
  94. stickYInputId < ControllerInputId.Axis0 || stickYInputId > ControllerInputId.Axis5)
  95. {
  96. return (0, 0);
  97. }
  98. JoystickState jsState = Joystick.GetState(_config.Index);
  99. int xAxis = stickXInputId - ControllerInputId.Axis0;
  100. int yAxis = stickYInputId - ControllerInputId.Axis0;
  101. float xValue = jsState.GetAxis(xAxis);
  102. float yValue = -jsState.GetAxis(yAxis); // Invert Y-axis
  103. return ApplyDeadzone(new Vector2(xValue, yValue), deadzone);
  104. }
  105. private (short, short) ApplyDeadzone(Vector2 axis, float deadzone)
  106. {
  107. return (ClampAxis(MathF.Abs(axis.X) > deadzone ? axis.X : 0f),
  108. ClampAxis(MathF.Abs(axis.Y) > deadzone ? axis.Y : 0f));
  109. }
  110. private static short ClampAxis(float value)
  111. {
  112. if (value <= -short.MaxValue)
  113. {
  114. return -short.MaxValue;
  115. }
  116. else
  117. {
  118. return (short)(value * short.MaxValue);
  119. }
  120. }
  121. }
  122. }