SDL2Gamepad.cs 16 KB

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