SDL2GamepadDriver.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using Ryujinx.SDL2.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using static SDL2.SDL;
  6. namespace Ryujinx.Input.SDL2
  7. {
  8. public class SDL2GamepadDriver : IGamepadDriver
  9. {
  10. private readonly Dictionary<int, string> _gamepadsInstanceIdsMapping;
  11. private readonly List<string> _gamepadsIds;
  12. private readonly Lock _lock = new();
  13. public ReadOnlySpan<string> GamepadsIds
  14. {
  15. get
  16. {
  17. lock (_lock)
  18. {
  19. return _gamepadsIds.ToArray();
  20. }
  21. }
  22. }
  23. public string DriverName => "SDL2";
  24. public event Action<string> OnGamepadConnected;
  25. public event Action<string> OnGamepadDisconnected;
  26. public SDL2GamepadDriver()
  27. {
  28. _gamepadsInstanceIdsMapping = new Dictionary<int, string>();
  29. _gamepadsIds = [];
  30. SDL2Driver.Instance.Initialize();
  31. SDL2Driver.Instance.OnJoyStickConnected += HandleJoyStickConnected;
  32. SDL2Driver.Instance.OnJoystickDisconnected += HandleJoyStickDisconnected;
  33. // Add already connected gamepads
  34. int numJoysticks = SDL_NumJoysticks();
  35. for (int joystickIndex = 0; joystickIndex < numJoysticks; joystickIndex++)
  36. {
  37. HandleJoyStickConnected(joystickIndex, SDL_JoystickGetDeviceInstanceID(joystickIndex));
  38. }
  39. }
  40. private string GenerateGamepadId(int joystickIndex)
  41. {
  42. Guid guid = SDL_JoystickGetDeviceGUID(joystickIndex);
  43. // Add a unique identifier to the start of the GUID in case of duplicates.
  44. if (guid == Guid.Empty)
  45. {
  46. return null;
  47. }
  48. string id;
  49. lock (_lock)
  50. {
  51. int guidIndex = 0;
  52. id = guidIndex + "-" + guid;
  53. while (_gamepadsIds.Contains(id))
  54. {
  55. id = (++guidIndex) + "-" + guid;
  56. }
  57. }
  58. return id;
  59. }
  60. private int GetJoystickIndexByGamepadId(string id)
  61. {
  62. lock (_lock)
  63. {
  64. return _gamepadsIds.IndexOf(id);
  65. }
  66. }
  67. private void HandleJoyStickDisconnected(int joystickInstanceId)
  68. {
  69. if (!_gamepadsInstanceIdsMapping.Remove(joystickInstanceId, out string id))
  70. return;
  71. lock (_lock)
  72. {
  73. _gamepadsIds.Remove(id);
  74. }
  75. OnGamepadDisconnected?.Invoke(id);
  76. }
  77. private void HandleJoyStickConnected(int joystickDeviceId, int joystickInstanceId)
  78. {
  79. if (SDL_IsGameController(joystickDeviceId) == SDL_bool.SDL_TRUE)
  80. {
  81. if (_gamepadsInstanceIdsMapping.ContainsKey(joystickInstanceId))
  82. {
  83. // Sometimes a JoyStick connected event fires after the app starts even though it was connected before
  84. // so it is rejected to avoid doubling the entries.
  85. return;
  86. }
  87. string id = GenerateGamepadId(joystickDeviceId);
  88. if (id == null)
  89. {
  90. return;
  91. }
  92. if (_gamepadsInstanceIdsMapping.TryAdd(joystickInstanceId, id))
  93. {
  94. lock (_lock)
  95. {
  96. if (joystickDeviceId <= _gamepadsIds.FindLastIndex(_ => true))
  97. _gamepadsIds.Insert(joystickDeviceId, id);
  98. else
  99. _gamepadsIds.Add(id);
  100. }
  101. OnGamepadConnected?.Invoke(id);
  102. }
  103. }
  104. }
  105. protected virtual void Dispose(bool disposing)
  106. {
  107. if (disposing)
  108. {
  109. SDL2Driver.Instance.OnJoyStickConnected -= HandleJoyStickConnected;
  110. SDL2Driver.Instance.OnJoystickDisconnected -= HandleJoyStickDisconnected;
  111. // Simulate a full disconnect when disposing
  112. foreach (string id in _gamepadsIds)
  113. {
  114. OnGamepadDisconnected?.Invoke(id);
  115. }
  116. lock (_lock)
  117. {
  118. _gamepadsIds.Clear();
  119. }
  120. SDL2Driver.Instance.Dispose();
  121. }
  122. }
  123. public void Dispose()
  124. {
  125. GC.SuppressFinalize(this);
  126. Dispose(true);
  127. }
  128. public IGamepad GetGamepad(string id)
  129. {
  130. int joystickIndex = GetJoystickIndexByGamepadId(id);
  131. if (joystickIndex == -1)
  132. {
  133. return null;
  134. }
  135. nint gamepadHandle = SDL_GameControllerOpen(joystickIndex);
  136. if (gamepadHandle == nint.Zero)
  137. {
  138. return null;
  139. }
  140. return new SDL2Gamepad(gamepadHandle, id);
  141. }
  142. public IEnumerable<IGamepad> GetGamepads()
  143. {
  144. lock (_gamepadsIds)
  145. {
  146. foreach (string gamepadId in _gamepadsIds)
  147. {
  148. yield return GetGamepad(gamepadId);
  149. }
  150. }
  151. }
  152. }
  153. }