IGamepad.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Ryujinx.Common.Configuration.Hid;
  2. using Ryujinx.Common.Memory;
  3. using System;
  4. using System.Numerics;
  5. using System.Runtime.CompilerServices;
  6. namespace Ryujinx.Input
  7. {
  8. /// <summary>
  9. /// Represent an emulated gamepad.
  10. /// </summary>
  11. public interface IGamepad : IDisposable
  12. {
  13. /// <summary>
  14. /// Features supported by the gamepad.
  15. /// </summary>
  16. GamepadFeaturesFlag Features { get; }
  17. /// <summary>
  18. /// Unique Id of the gamepad.
  19. /// </summary>
  20. string Id { get; }
  21. /// <summary>
  22. /// The name of the gamepad.
  23. /// </summary>
  24. string Name { get; }
  25. /// <summary>
  26. /// True if the gamepad is connected.
  27. /// </summary>
  28. bool IsConnected { get; }
  29. /// <summary>
  30. /// Check if a given input button is pressed on the gamepad.
  31. /// </summary>
  32. /// <param name="inputId">The button id</param>
  33. /// <returns>True if the given button is pressed on the gamepad</returns>
  34. bool IsPressed(GamepadButtonInputId inputId);
  35. /// <summary>
  36. /// Get the values of a given input joystick on the gamepad.
  37. /// </summary>
  38. /// <param name="inputId">The stick id</param>
  39. /// <returns>The values of the given input joystick on the gamepad</returns>
  40. (float, float) GetStick(StickInputId inputId);
  41. /// <summary>
  42. /// Get the values of a given motion sensors on the gamepad.
  43. /// </summary>
  44. /// <param name="inputId">The motion id</param>
  45. /// <returns> The values of the given motion sensors on the gamepad.</returns>
  46. Vector3 GetMotionData(MotionInputId inputId);
  47. /// <summary>
  48. /// Configure the threshold of the triggers on the gamepad.
  49. /// </summary>
  50. /// <param name="triggerThreshold">The threshold value for the triggers on the gamepad</param>
  51. void SetTriggerThreshold(float triggerThreshold);
  52. /// <summary>
  53. /// Set the configuration of the gamepad.
  54. /// </summary>
  55. /// <remarks>This expect config to be in the format expected by the driver</remarks>
  56. /// <param name="configuration">The configuration of the gamepad</param>
  57. void SetConfiguration(InputConfig configuration);
  58. /// <summary>
  59. /// Starts a rumble effect on the gamepad.
  60. /// </summary>
  61. /// <param name="lowFrequency">The intensity of the low frequency from 0.0f to 1.0f</param>
  62. /// <param name="highFrequency">The intensity of the high frequency from 0.0f to 1.0f</param>
  63. /// <param name="durationMs">The duration of the rumble effect in milliseconds.</param>
  64. void Rumble(float lowFrequency, float highFrequency, uint durationMs);
  65. /// <summary>
  66. /// Get a snaphost of the state of the gamepad that is remapped with the informations from the <see cref="InputConfig"/> set via <see cref="SetConfiguration(InputConfig)"/>.
  67. /// </summary>
  68. /// <returns>A remapped snaphost of the state of the gamepad.</returns>
  69. GamepadStateSnapshot GetMappedStateSnapshot();
  70. /// <summary>
  71. /// Get a snaphost of the state of the gamepad.
  72. /// </summary>
  73. /// <returns>A snaphost of the state of the gamepad.</returns>
  74. GamepadStateSnapshot GetStateSnapshot();
  75. /// <summary>
  76. /// Get a snaphost of the state of a gamepad.
  77. /// </summary>
  78. /// <param name="gamepad">The gamepad to do a snapshot of</param>
  79. /// <returns>A snaphost of the state of the gamepad.</returns>
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. static GamepadStateSnapshot GetStateSnapshot(IGamepad gamepad)
  82. {
  83. // NOTE: Update Array size if JoystickInputId is changed.
  84. Array3<Array2<float>> joysticksState = default;
  85. for (StickInputId inputId = StickInputId.Left; inputId < StickInputId.Count; inputId++)
  86. {
  87. (float state0, float state1) = gamepad.GetStick(inputId);
  88. Array2<float> state = default;
  89. state[0] = state0;
  90. state[1] = state1;
  91. joysticksState[(int)inputId] = state;
  92. }
  93. // NOTE: Update Array size if GamepadInputId is changed.
  94. Array28<bool> buttonsState = default;
  95. for (GamepadButtonInputId inputId = GamepadButtonInputId.A; inputId < GamepadButtonInputId.Count; inputId++)
  96. {
  97. buttonsState[(int)inputId] = gamepad.IsPressed(inputId);
  98. }
  99. return new GamepadStateSnapshot(joysticksState, buttonsState);
  100. }
  101. }
  102. }