SDL2Gamepad.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. using Ryujinx.Common.Configuration.Hid;
  2. using Ryujinx.Common.Configuration.Hid.Controller;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Numerics;
  6. using static SDL2.SDL;
  7. namespace Ryujinx.Input.SDL2
  8. {
  9. class SDL2Gamepad : IGamepad
  10. {
  11. private bool HasConfiguration => _configuration != null;
  12. private class ButtonMappingEntry
  13. {
  14. public readonly GamepadButtonInputId To;
  15. public readonly GamepadButtonInputId From;
  16. public ButtonMappingEntry(GamepadButtonInputId to, GamepadButtonInputId from)
  17. {
  18. To = to;
  19. From = from;
  20. }
  21. }
  22. private StandardControllerInputConfig _configuration;
  23. private static readonly SDL_GameControllerButton[] _buttonsDriverMapping = new SDL_GameControllerButton[(int)GamepadButtonInputId.Count]
  24. {
  25. // Unbound, ignored.
  26. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID,
  27. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A,
  28. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B,
  29. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X,
  30. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y,
  31. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK,
  32. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK,
  33. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
  34. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
  35. // NOTE: The left and right trigger are axis, we handle those differently
  36. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID,
  37. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID,
  38. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP,
  39. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN,
  40. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT,
  41. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
  42. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK,
  43. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START,
  44. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE,
  45. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_MISC1,
  46. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_PADDLE1,
  47. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_PADDLE2,
  48. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_PADDLE3,
  49. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_PADDLE4,
  50. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_TOUCHPAD,
  51. // Virtual buttons are invalid, ignored.
  52. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID,
  53. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID,
  54. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID,
  55. SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID,
  56. };
  57. private object _userMappingLock = new object();
  58. private List<ButtonMappingEntry> _buttonsUserMapping;
  59. private StickInputId[] _stickUserMapping = new StickInputId[(int)StickInputId.Count]
  60. {
  61. StickInputId.Unbound,
  62. StickInputId.Left,
  63. StickInputId.Right
  64. };
  65. public GamepadFeaturesFlag Features { get; }
  66. private IntPtr _gamepadHandle;
  67. private float _triggerThreshold;
  68. public SDL2Gamepad(IntPtr gamepadHandle, string driverId)
  69. {
  70. _gamepadHandle = gamepadHandle;
  71. _buttonsUserMapping = new List<ButtonMappingEntry>();
  72. Name = SDL_GameControllerName(_gamepadHandle);
  73. Id = driverId;
  74. Features = GetFeaturesFlag();
  75. _triggerThreshold = 0.0f;
  76. // Enable motion tracking
  77. if (Features.HasFlag(GamepadFeaturesFlag.Motion))
  78. {
  79. SDL_GameControllerSetSensorEnabled(_gamepadHandle, SDL_SensorType.SDL_SENSOR_ACCEL, SDL_bool.SDL_TRUE);
  80. SDL_GameControllerSetSensorEnabled(_gamepadHandle, SDL_SensorType.SDL_SENSOR_GYRO, SDL_bool.SDL_TRUE);
  81. }
  82. }
  83. private GamepadFeaturesFlag GetFeaturesFlag()
  84. {
  85. GamepadFeaturesFlag result = GamepadFeaturesFlag.None;
  86. if (SDL_GameControllerHasSensor(_gamepadHandle, SDL_SensorType.SDL_SENSOR_ACCEL) == SDL_bool.SDL_TRUE &&
  87. SDL_GameControllerHasSensor(_gamepadHandle, SDL_SensorType.SDL_SENSOR_GYRO) == SDL_bool.SDL_TRUE)
  88. {
  89. result |= GamepadFeaturesFlag.Motion;
  90. }
  91. int error = SDL_GameControllerRumble(_gamepadHandle, 0, 0, 100);
  92. if (error == 0)
  93. {
  94. result |= GamepadFeaturesFlag.Rumble;
  95. }
  96. return result;
  97. }
  98. public string Id { get; }
  99. public string Name { get; }
  100. public bool IsConnected => SDL_GameControllerGetAttached(_gamepadHandle) == SDL_bool.SDL_TRUE;
  101. protected virtual void Dispose(bool disposing)
  102. {
  103. if (disposing && _gamepadHandle != IntPtr.Zero)
  104. {
  105. SDL_GameControllerClose(_gamepadHandle);
  106. _gamepadHandle = IntPtr.Zero;
  107. }
  108. }
  109. public void Dispose()
  110. {
  111. Dispose(true);
  112. }
  113. public void SetTriggerThreshold(float triggerThreshold)
  114. {
  115. _triggerThreshold = triggerThreshold;
  116. }
  117. public void Rumble(float lowFrequency, float highFrequency, uint durationMs)
  118. {
  119. if (Features.HasFlag(GamepadFeaturesFlag.Rumble))
  120. {
  121. ushort lowFrequencyRaw = (ushort)(lowFrequency * ushort.MaxValue);
  122. ushort highFrequencyRaw = (ushort)(highFrequency * ushort.MaxValue);
  123. SDL_GameControllerRumble(_gamepadHandle, lowFrequencyRaw, highFrequencyRaw, durationMs);
  124. }
  125. }
  126. public Vector3 GetMotionData(MotionInputId inputId)
  127. {
  128. SDL_SensorType sensorType = SDL_SensorType.SDL_SENSOR_INVALID;
  129. if (inputId == MotionInputId.Accelerometer)
  130. {
  131. sensorType = SDL_SensorType.SDL_SENSOR_ACCEL;
  132. }
  133. else if (inputId == MotionInputId.Gyroscope)
  134. {
  135. sensorType = SDL_SensorType.SDL_SENSOR_GYRO;
  136. }
  137. if (Features.HasFlag(GamepadFeaturesFlag.Motion) && sensorType != SDL_SensorType.SDL_SENSOR_INVALID)
  138. {
  139. const int ElementCount = 3;
  140. unsafe
  141. {
  142. float* values = stackalloc float[ElementCount];
  143. int result = SDL_GameControllerGetSensorData(_gamepadHandle, sensorType, (IntPtr)values, ElementCount);
  144. if (result == 0)
  145. {
  146. Vector3 value = new Vector3(values[0], values[1], values[2]);
  147. if (inputId == MotionInputId.Gyroscope)
  148. {
  149. return RadToDegree(value);
  150. }
  151. else if (inputId == MotionInputId.Accelerometer)
  152. {
  153. return GsToMs2(value);
  154. }
  155. return value;
  156. }
  157. }
  158. }
  159. return Vector3.Zero;
  160. }
  161. private static Vector3 RadToDegree(Vector3 rad)
  162. {
  163. return rad * (180 / MathF.PI);
  164. }
  165. private static Vector3 GsToMs2(Vector3 gs)
  166. {
  167. return gs / SDL_STANDARD_GRAVITY;
  168. }
  169. public void SetConfiguration(InputConfig configuration)
  170. {
  171. lock (_userMappingLock)
  172. {
  173. _configuration = (StandardControllerInputConfig)configuration;
  174. _buttonsUserMapping.Clear();
  175. // First update sticks
  176. _stickUserMapping[(int)StickInputId.Left] = (StickInputId)_configuration.LeftJoyconStick.Joystick;
  177. _stickUserMapping[(int)StickInputId.Right] = (StickInputId)_configuration.RightJoyconStick.Joystick;
  178. // Then left joycon
  179. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.LeftStick, (GamepadButtonInputId)_configuration.LeftJoyconStick.StickButton));
  180. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadUp, (GamepadButtonInputId)_configuration.LeftJoycon.DpadUp));
  181. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadDown, (GamepadButtonInputId)_configuration.LeftJoycon.DpadDown));
  182. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadLeft, (GamepadButtonInputId)_configuration.LeftJoycon.DpadLeft));
  183. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.DpadRight, (GamepadButtonInputId)_configuration.LeftJoycon.DpadRight));
  184. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.Minus, (GamepadButtonInputId)_configuration.LeftJoycon.ButtonMinus));
  185. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.LeftShoulder, (GamepadButtonInputId)_configuration.LeftJoycon.ButtonL));
  186. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.LeftTrigger, (GamepadButtonInputId)_configuration.LeftJoycon.ButtonZl));
  187. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.SingleRightTrigger0, (GamepadButtonInputId)_configuration.LeftJoycon.ButtonSr));
  188. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.SingleLeftTrigger0, (GamepadButtonInputId)_configuration.LeftJoycon.ButtonSl));
  189. // Finally right joycon
  190. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.RightStick, (GamepadButtonInputId)_configuration.RightJoyconStick.StickButton));
  191. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.A, (GamepadButtonInputId)_configuration.RightJoycon.ButtonA));
  192. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.B, (GamepadButtonInputId)_configuration.RightJoycon.ButtonB));
  193. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.X, (GamepadButtonInputId)_configuration.RightJoycon.ButtonX));
  194. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.Y, (GamepadButtonInputId)_configuration.RightJoycon.ButtonY));
  195. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.Plus, (GamepadButtonInputId)_configuration.RightJoycon.ButtonPlus));
  196. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.RightShoulder, (GamepadButtonInputId)_configuration.RightJoycon.ButtonR));
  197. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.RightTrigger, (GamepadButtonInputId)_configuration.RightJoycon.ButtonZr));
  198. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.SingleRightTrigger1, (GamepadButtonInputId)_configuration.RightJoycon.ButtonSr));
  199. _buttonsUserMapping.Add(new ButtonMappingEntry(GamepadButtonInputId.SingleLeftTrigger1, (GamepadButtonInputId)_configuration.RightJoycon.ButtonSl));
  200. SetTriggerThreshold(_configuration.TriggerThreshold);
  201. }
  202. }
  203. public GamepadStateSnapshot GetStateSnapshot()
  204. {
  205. return IGamepad.GetStateSnapshot(this);
  206. }
  207. public GamepadStateSnapshot GetMappedStateSnapshot()
  208. {
  209. GamepadStateSnapshot rawState = GetStateSnapshot();
  210. GamepadStateSnapshot result = default;
  211. lock (_userMappingLock)
  212. {
  213. if (_buttonsUserMapping.Count == 0)
  214. {
  215. return rawState;
  216. }
  217. foreach (ButtonMappingEntry entry in _buttonsUserMapping)
  218. {
  219. if (entry.From == GamepadButtonInputId.Unbound || entry.To == GamepadButtonInputId.Unbound)
  220. {
  221. continue;
  222. }
  223. // Do not touch state of button already pressed
  224. if (!result.IsPressed(entry.To))
  225. {
  226. result.SetPressed(entry.To, rawState.IsPressed(entry.From));
  227. }
  228. }
  229. (float leftStickX, float leftStickY) = rawState.GetStick(_stickUserMapping[(int)StickInputId.Left]);
  230. (float rightStickX, float rightStickY) = rawState.GetStick(_stickUserMapping[(int)StickInputId.Right]);
  231. result.SetStick(StickInputId.Left, leftStickX, leftStickY);
  232. result.SetStick(StickInputId.Right, rightStickX, rightStickY);
  233. }
  234. return result;
  235. }
  236. private static float ConvertRawStickValue(short value)
  237. {
  238. const float ConvertRate = 1.0f / (short.MaxValue + 0.5f);
  239. return value * ConvertRate;
  240. }
  241. public (float, float) GetStick(StickInputId inputId)
  242. {
  243. if (inputId == StickInputId.Unbound)
  244. {
  245. return (0.0f, 0.0f);
  246. }
  247. short stickX;
  248. short stickY;
  249. if (inputId == StickInputId.Left)
  250. {
  251. stickX = SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTX);
  252. stickY = SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTY);
  253. }
  254. else if (inputId == StickInputId.Right)
  255. {
  256. stickX = SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTX);
  257. stickY = SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTY);
  258. }
  259. else
  260. {
  261. throw new NotSupportedException($"Unsupported stick {inputId}");
  262. }
  263. float resultX = ConvertRawStickValue(stickX);
  264. float resultY = -ConvertRawStickValue(stickY);
  265. if (HasConfiguration)
  266. {
  267. if ((inputId == StickInputId.Left && _configuration.LeftJoyconStick.InvertStickX) ||
  268. (inputId == StickInputId.Right && _configuration.RightJoyconStick.InvertStickX))
  269. {
  270. resultX = -resultX;
  271. }
  272. if ((inputId == StickInputId.Left && _configuration.LeftJoyconStick.InvertStickY) ||
  273. (inputId == StickInputId.Right && _configuration.RightJoyconStick.InvertStickY))
  274. {
  275. resultY = -resultY;
  276. }
  277. }
  278. return (resultX, resultY);
  279. }
  280. public bool IsPressed(GamepadButtonInputId inputId)
  281. {
  282. if (inputId == GamepadButtonInputId.LeftTrigger)
  283. {
  284. return ConvertRawStickValue(SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERLEFT)) > _triggerThreshold;
  285. }
  286. else if (inputId == GamepadButtonInputId.RightTrigger)
  287. {
  288. return ConvertRawStickValue(SDL_GameControllerGetAxis(_gamepadHandle, SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) > _triggerThreshold;
  289. }
  290. else if (_buttonsDriverMapping[(int)inputId] == SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID)
  291. {
  292. return false;
  293. }
  294. else
  295. {
  296. return SDL_GameControllerGetButton(_gamepadHandle, _buttonsDriverMapping[(int)inputId]) == 1;
  297. }
  298. }
  299. }
  300. }