SDL2GamepadDriver.cs 3.9 KB

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