SDL2GamepadDriver.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using static SDL2.SDL;
  4. namespace Ryujinx.Input.SDL2
  5. {
  6. public class SDL2GamepadDriver : IGamepadDriver
  7. {
  8. private Dictionary<int, string> _gamepadsInstanceIdsMapping;
  9. private List<string> _gamepadsIds;
  10. public ReadOnlySpan<string> GamepadsIds => _gamepadsIds.ToArray();
  11. public string DriverName => "SDL2";
  12. public event Action<string> OnGamepadConnected;
  13. public event Action<string> OnGamepadDisconnected;
  14. public SDL2GamepadDriver()
  15. {
  16. _gamepadsInstanceIdsMapping = new Dictionary<int, string>();
  17. _gamepadsIds = new List<string>();
  18. SDL2Driver.Instance.Initialize();
  19. SDL2Driver.Instance.OnJoyStickConnected += HandleJoyStickConnected;
  20. SDL2Driver.Instance.OnJoystickDisconnected += HandleJoyStickDisconnected;
  21. // Add already connected gamepads
  22. for (int joystickIndex = 0; joystickIndex < SDL_NumJoysticks(); joystickIndex++)
  23. {
  24. HandleJoyStickConnected(joystickIndex, SDL_JoystickGetDeviceInstanceID(joystickIndex));
  25. }
  26. }
  27. private string GenerateGamepadId(int joystickIndex)
  28. {
  29. Guid guid = SDL_JoystickGetDeviceGUID(joystickIndex);
  30. if (guid == Guid.Empty)
  31. {
  32. return null;
  33. }
  34. return joystickIndex + "-" + guid.ToString();
  35. }
  36. private int GetJoystickIndexByGamepadId(string id)
  37. {
  38. string[] data = id.Split("-");
  39. if (data.Length != 6 || !int.TryParse(data[0], out int joystickIndex))
  40. {
  41. return -1;
  42. }
  43. return joystickIndex;
  44. }
  45. private void HandleJoyStickDisconnected(int joystickInstanceId)
  46. {
  47. if (_gamepadsInstanceIdsMapping.TryGetValue(joystickInstanceId, out string id))
  48. {
  49. _gamepadsInstanceIdsMapping.Remove(joystickInstanceId);
  50. _gamepadsIds.Remove(id);
  51. OnGamepadDisconnected?.Invoke(id);
  52. }
  53. }
  54. private void HandleJoyStickConnected(int joystickDeviceId, int joystickInstanceId)
  55. {
  56. if (SDL_IsGameController(joystickDeviceId) == SDL_bool.SDL_TRUE)
  57. {
  58. string id = GenerateGamepadId(joystickDeviceId);
  59. if (id == null)
  60. {
  61. return;
  62. }
  63. if (_gamepadsInstanceIdsMapping.TryAdd(joystickInstanceId, id))
  64. {
  65. _gamepadsIds.Add(id);
  66. OnGamepadConnected?.Invoke(id);
  67. }
  68. }
  69. }
  70. protected virtual void Dispose(bool disposing)
  71. {
  72. if (disposing)
  73. {
  74. SDL2Driver.Instance.OnJoyStickConnected -= HandleJoyStickConnected;
  75. SDL2Driver.Instance.OnJoystickDisconnected -= HandleJoyStickDisconnected;
  76. // Simulate a full disconnect when disposing
  77. foreach (string id in _gamepadsIds)
  78. {
  79. OnGamepadDisconnected?.Invoke(id);
  80. }
  81. _gamepadsIds.Clear();
  82. SDL2Driver.Instance.Dispose();
  83. }
  84. }
  85. public void Dispose()
  86. {
  87. Dispose(true);
  88. }
  89. public IGamepad GetGamepad(string id)
  90. {
  91. int joystickIndex = GetJoystickIndexByGamepadId(id);
  92. if (joystickIndex == -1)
  93. {
  94. return null;
  95. }
  96. if (id != GenerateGamepadId(joystickIndex))
  97. {
  98. return null;
  99. }
  100. IntPtr gamepadHandle = SDL_GameControllerOpen(joystickIndex);
  101. if (gamepadHandle == IntPtr.Zero)
  102. {
  103. return null;
  104. }
  105. return new SDL2Gamepad(gamepadHandle, id);
  106. }
  107. }
  108. }