mirror of
https://git.743378673.xyz/MeloNX/MeloNX.git
synced 2025-06-29 11:56:24 +02:00
Fix controller and logging
This commit is contained in:
parent
14ce5102fb
commit
0bfca4c488
8 changed files with 52 additions and 69 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue