Fix controller and logging

This commit is contained in:
Stossy11 2025-03-09 08:00:08 +11:00
parent 14ce5102fb
commit 0bfca4c488
8 changed files with 52 additions and 69 deletions

View file

@ -9,18 +9,8 @@ namespace Ryujinx.Input.SDL2
{
private readonly Dictionary<int, string> _gamepadsInstanceIdsMapping;
private readonly List<string> _gamepadsIds;
private readonly object _lock = new object();
public ReadOnlySpan<string> GamepadsIds
{
get
{
lock (_lock)
{
return _gamepadsIds.ToArray();
}
}
}
public ReadOnlySpan<string> GamepadsIds => _gamepadsIds.ToArray();
public string DriverName => "SDL2";
@ -45,39 +35,28 @@ namespace Ryujinx.Input.SDL2
}
}
private string GenerateGamepadId(int joystickIndex)
private static string GenerateGamepadId(int joystickIndex)
{
Guid guid = SDL_JoystickGetDeviceGUID(joystickIndex);
// Add a unique identifier to the start of the GUID in case of duplicates.
if (guid == Guid.Empty)
{
return null;
}
string id;
lock (_lock)
{
int guidIndex = 0;
id = guidIndex + "-" + guid;
while (_gamepadsIds.Contains(id))
{
id = (++guidIndex) + "-" + guid;
}
}
return id;
return joystickIndex + "-" + guid;
}
private int GetJoystickIndexByGamepadId(string id)
private static int GetJoystickIndexByGamepadId(string id)
{
lock (_lock)
string[] data = id.Split("-");
if (data.Length != 6 || !int.TryParse(data[0], out int joystickIndex))
{
return _gamepadsIds.IndexOf(id);
return -1;
}
return joystickIndex;
}
private void HandleJoyStickDisconnected(int joystickInstanceId)
@ -85,11 +64,7 @@ namespace Ryujinx.Input.SDL2
if (_gamepadsInstanceIdsMapping.TryGetValue(joystickInstanceId, out string id))
{
_gamepadsInstanceIdsMapping.Remove(joystickInstanceId);
lock (_lock)
{
_gamepadsIds.Remove(id);
}
_gamepadsIds.Remove(id);
OnGamepadDisconnected?.Invoke(id);
}
@ -99,13 +74,6 @@ namespace Ryujinx.Input.SDL2
{
if (SDL_IsGameController(joystickDeviceId) == SDL_bool.SDL_TRUE)
{
if (_gamepadsInstanceIdsMapping.ContainsKey(joystickInstanceId))
{
// Sometimes a JoyStick connected event fires after the app starts even though it was connected before
// so it is rejected to avoid doubling the entries.
return;
}
string id = GenerateGamepadId(joystickDeviceId);
if (id == null)
@ -113,12 +81,16 @@ namespace Ryujinx.Input.SDL2
return;
}
// Sometimes a JoyStick connected event fires after the app starts even though it was connected before
// so it is rejected to avoid doubling the entries.
if (_gamepadsIds.Contains(id))
{
return;
}
if (_gamepadsInstanceIdsMapping.TryAdd(joystickInstanceId, id))
{
lock (_lock)
{
_gamepadsIds.Add(id);
}
_gamepadsIds.Add(id);
OnGamepadConnected?.Invoke(id);
}
@ -131,17 +103,13 @@ namespace Ryujinx.Input.SDL2
{
SDL2Driver.Instance.OnJoyStickConnected -= HandleJoyStickConnected;
SDL2Driver.Instance.OnJoystickDisconnected -= HandleJoyStickDisconnected;
// Simulate a full disconnect when disposing
foreach (string id in _gamepadsIds)
{
OnGamepadDisconnected?.Invoke(id);
}
lock (_lock)
{
_gamepadsIds.Clear();
}
_gamepadsIds.Clear();
SDL2Driver.Instance.Dispose();
}
@ -162,6 +130,11 @@ namespace Ryujinx.Input.SDL2
return null;
}
if (id != GenerateGamepadId(joystickIndex))
{
return null;
}
IntPtr gamepadHandle = SDL_GameControllerOpen(joystickIndex);
if (gamepadHandle == IntPtr.Zero)