SDL2Gamepad.cs 16 KB

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