2023-12-04 13:17:13 +00:00
|
|
|
using Ryujinx.SDL2.Common;
|
2021-05-05 21:37:09 +00:00
|
|
|
using System;
|
2021-04-14 10:28:43 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using static SDL2.SDL;
|
|
|
|
|
|
|
|
namespace Ryujinx.Input.SDL2
|
|
|
|
{
|
|
|
|
public class SDL2GamepadDriver : IGamepadDriver
|
|
|
|
{
|
2023-06-26 01:55:25 +00:00
|
|
|
private readonly Dictionary<int, string> _gamepadsInstanceIdsMapping;
|
|
|
|
private readonly List<string> _gamepadsIds;
|
2021-04-14 10:28:43 +00:00
|
|
|
|
|
|
|
public ReadOnlySpan<string> GamepadsIds => _gamepadsIds.ToArray();
|
|
|
|
|
|
|
|
public string DriverName => "SDL2";
|
|
|
|
|
|
|
|
public event Action<string> OnGamepadConnected;
|
|
|
|
public event Action<string> OnGamepadDisconnected;
|
|
|
|
|
|
|
|
public SDL2GamepadDriver()
|
|
|
|
{
|
|
|
|
_gamepadsInstanceIdsMapping = new Dictionary<int, string>();
|
|
|
|
_gamepadsIds = new List<string>();
|
|
|
|
|
|
|
|
SDL2Driver.Instance.Initialize();
|
|
|
|
SDL2Driver.Instance.OnJoyStickConnected += HandleJoyStickConnected;
|
|
|
|
SDL2Driver.Instance.OnJoystickDisconnected += HandleJoyStickDisconnected;
|
|
|
|
|
|
|
|
// Add already connected gamepads
|
2023-03-11 20:05:48 +00:00
|
|
|
int numJoysticks = SDL_NumJoysticks();
|
|
|
|
|
|
|
|
for (int joystickIndex = 0; joystickIndex < numJoysticks; joystickIndex++)
|
2021-04-14 10:28:43 +00:00
|
|
|
{
|
|
|
|
HandleJoyStickConnected(joystickIndex, SDL_JoystickGetDeviceInstanceID(joystickIndex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 01:55:25 +00:00
|
|
|
private static string GenerateGamepadId(int joystickIndex)
|
2021-04-14 10:28:43 +00:00
|
|
|
{
|
|
|
|
Guid guid = SDL_JoystickGetDeviceGUID(joystickIndex);
|
|
|
|
|
|
|
|
if (guid == Guid.Empty)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-06-26 01:55:25 +00:00
|
|
|
return joystickIndex + "-" + guid;
|
2021-04-14 10:28:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 01:55:25 +00:00
|
|
|
private static int GetJoystickIndexByGamepadId(string id)
|
2021-04-14 10:28:43 +00:00
|
|
|
{
|
|
|
|
string[] data = id.Split("-");
|
|
|
|
|
|
|
|
if (data.Length != 6 || !int.TryParse(data[0], out int joystickIndex))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return joystickIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void HandleJoyStickDisconnected(int joystickInstanceId)
|
|
|
|
{
|
|
|
|
if (_gamepadsInstanceIdsMapping.TryGetValue(joystickInstanceId, out string id))
|
|
|
|
{
|
|
|
|
_gamepadsInstanceIdsMapping.Remove(joystickInstanceId);
|
|
|
|
_gamepadsIds.Remove(id);
|
|
|
|
|
|
|
|
OnGamepadDisconnected?.Invoke(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void HandleJoyStickConnected(int joystickDeviceId, int joystickInstanceId)
|
|
|
|
{
|
|
|
|
if (SDL_IsGameController(joystickDeviceId) == SDL_bool.SDL_TRUE)
|
|
|
|
{
|
|
|
|
string id = GenerateGamepadId(joystickDeviceId);
|
|
|
|
|
|
|
|
if (id == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-20 17:01:55 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:28:43 +00:00
|
|
|
if (_gamepadsInstanceIdsMapping.TryAdd(joystickInstanceId, id))
|
|
|
|
{
|
|
|
|
_gamepadsIds.Add(id);
|
|
|
|
|
|
|
|
OnGamepadConnected?.Invoke(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
SDL2Driver.Instance.OnJoyStickConnected -= HandleJoyStickConnected;
|
|
|
|
SDL2Driver.Instance.OnJoystickDisconnected -= HandleJoyStickDisconnected;
|
|
|
|
|
|
|
|
// Simulate a full disconnect when disposing
|
|
|
|
foreach (string id in _gamepadsIds)
|
|
|
|
{
|
|
|
|
OnGamepadDisconnected?.Invoke(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
_gamepadsIds.Clear();
|
|
|
|
|
|
|
|
SDL2Driver.Instance.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2023-06-26 01:55:25 +00:00
|
|
|
GC.SuppressFinalize(this);
|
2021-04-14 10:28:43 +00:00
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IGamepad GetGamepad(string id)
|
|
|
|
{
|
|
|
|
int joystickIndex = GetJoystickIndexByGamepadId(id);
|
|
|
|
|
|
|
|
if (joystickIndex == -1)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id != GenerateGamepadId(joystickIndex))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntPtr gamepadHandle = SDL_GameControllerOpen(joystickIndex);
|
|
|
|
|
|
|
|
if (gamepadHandle == IntPtr.Zero)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SDL2Gamepad(gamepadHandle, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|