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