fix sdl3 motion
This commit is contained in:
@@ -4,7 +4,6 @@ using Ryujinx.SDL3.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using static SDL3.SDL;
|
||||
@@ -13,7 +12,7 @@ namespace Ryujinx.Input.SDl3
|
||||
{
|
||||
public class SDL3GamepadDriver : IGamepadDriver
|
||||
{
|
||||
private readonly Dictionary<uint, string> _gamepadsInstanceIdsMapping;
|
||||
private readonly Dictionary<uint, GamepadInfo> _gamepadsInstanceIdsMapping;
|
||||
private readonly List<string> _gamepadsIds;
|
||||
private readonly Lock _lock = new();
|
||||
|
||||
@@ -35,7 +34,7 @@ namespace Ryujinx.Input.SDl3
|
||||
|
||||
public SDL3GamepadDriver()
|
||||
{
|
||||
_gamepadsInstanceIdsMapping = new Dictionary<uint, string>();
|
||||
_gamepadsInstanceIdsMapping = new Dictionary<uint, GamepadInfo>();
|
||||
_gamepadsIds = new List<string>();
|
||||
|
||||
SDL3Driver.Instance.Initialize();
|
||||
@@ -43,20 +42,19 @@ namespace Ryujinx.Input.SDl3
|
||||
SDL3Driver.Instance.OnJoystickDisconnected += HandleJoyStickDisconnected;
|
||||
SDL3Driver.Instance.OnJoyBatteryUpdated += HandleJoyBatteryUpdated;
|
||||
|
||||
IntPtr joystickArray = SDL_GetJoysticks(out int count);
|
||||
|
||||
var joystickIDs = new int[count];
|
||||
Marshal.Copy(joystickArray, joystickIDs, 0, count);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
HandleJoyStickConnected((uint)joystickIDs[i]);
|
||||
}
|
||||
// IntPtr joystickArray = SDL_GetJoysticks(out int count);
|
||||
//
|
||||
// var joystickIDs = new int[count];
|
||||
// Marshal.Copy(joystickArray, joystickIDs, 0, count);
|
||||
//
|
||||
// for (int i = 0; i < count; i++)
|
||||
// {
|
||||
// HandleJoyStickConnected((uint)joystickIDs[i]);
|
||||
// }
|
||||
}
|
||||
|
||||
private string GenerateGamepadId(uint joystickIndex)
|
||||
{
|
||||
|
||||
int bufferSize = 33;
|
||||
Span<byte> pszGUID = stackalloc byte[bufferSize];
|
||||
SDL_GUIDToString(SDL_GetJoystickGUIDForID(joystickIndex), pszGUID, bufferSize);
|
||||
@@ -82,23 +80,24 @@ namespace Ryujinx.Input.SDl3
|
||||
return id;
|
||||
}
|
||||
|
||||
private uint GetJoystickIndexByGamepadId(string id)
|
||||
private GamepadInfo GetJoystickIndexByGamepadId(string id)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return _gamepadsInstanceIdsMapping.FirstOrDefault(x=>x.Value == id).Key;
|
||||
return _gamepadsInstanceIdsMapping.FirstOrDefault(x => x.Value.driverId == id).Value;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleJoyStickDisconnected(uint joystickInstanceId)
|
||||
{
|
||||
bool joyConPairDisconnected = false;
|
||||
if (!_gamepadsInstanceIdsMapping.Remove(joystickInstanceId, out string id))
|
||||
if (!_gamepadsInstanceIdsMapping.Remove(joystickInstanceId, out GamepadInfo gamepadInfo))
|
||||
return;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
_gamepadsIds.Remove(id);
|
||||
_gamepadsIds.Remove(gamepadInfo.driverId);
|
||||
SDL_CloseGamepad(gamepadInfo.gamepadHandle);
|
||||
if (!SDL3JoyConPair.IsCombinable(_gamepadsInstanceIdsMapping))
|
||||
{
|
||||
_gamepadsIds.Remove(SDL3JoyConPair.Id);
|
||||
@@ -106,62 +105,58 @@ namespace Ryujinx.Input.SDl3
|
||||
}
|
||||
}
|
||||
|
||||
OnGamepadDisconnected?.Invoke(id);
|
||||
OnGamepadDisconnected?.Invoke(gamepadInfo.driverId);
|
||||
if (joyConPairDisconnected)
|
||||
{
|
||||
OnGamepadDisconnected?.Invoke(SDL3JoyConPair.Id);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleJoyStickConnected(uint joystickInstanceId)
|
||||
private void HandleJoyStickConnected(uint gamepadInstanceId)
|
||||
{
|
||||
bool joyConPairConnected = false;
|
||||
|
||||
|
||||
if (SDL_IsGamepad(joystickInstanceId))
|
||||
if (_gamepadsInstanceIdsMapping.ContainsKey(gamepadInstanceId))
|
||||
{
|
||||
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(gamepadInstanceId);
|
||||
if (id == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_gamepadsInstanceIdsMapping.TryAdd(gamepadInstanceId, new GamepadInfo(id, SDL_OpenGamepad(gamepadInstanceId))))
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
// 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;
|
||||
if (gamepadInstanceId <= _gamepadsIds.FindLastIndex(_ => true))
|
||||
{
|
||||
// _gamepadsIds.Insert(joystickDeviceId, id);
|
||||
}
|
||||
else
|
||||
_gamepadsIds.Add(id);
|
||||
|
||||
if (SDL3JoyConPair.IsCombinable(_gamepadsInstanceIdsMapping))
|
||||
{
|
||||
_gamepadsIds.Remove(SDL3JoyConPair.Id);
|
||||
_gamepadsIds.Add(SDL3JoyConPair.Id);
|
||||
joyConPairConnected = true;
|
||||
}
|
||||
}
|
||||
|
||||
string id = GenerateGamepadId(joystickInstanceId);
|
||||
if (id == null)
|
||||
OnGamepadConnected?.Invoke(id);
|
||||
if (joyConPairConnected)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_gamepadsInstanceIdsMapping.TryAdd(joystickInstanceId, id))
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (joystickInstanceId <= _gamepadsIds.FindLastIndex(_ => true))
|
||||
{
|
||||
// _gamepadsIds.Insert(joystickDeviceId, id);
|
||||
}
|
||||
else
|
||||
_gamepadsIds.Add(id);
|
||||
|
||||
if (SDL3JoyConPair.IsCombinable(_gamepadsInstanceIdsMapping))
|
||||
{
|
||||
_gamepadsIds.Remove(SDL3JoyConPair.Id);
|
||||
_gamepadsIds.Add(SDL3JoyConPair.Id);
|
||||
joyConPairConnected = true;
|
||||
}
|
||||
}
|
||||
|
||||
OnGamepadConnected?.Invoke(id);
|
||||
if (joyConPairConnected)
|
||||
{
|
||||
OnGamepadConnected?.Invoke(SDL3JoyConPair.Id);
|
||||
}
|
||||
OnGamepadConnected?.Invoke(SDL3JoyConPair.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleJoyBatteryUpdated(uint joystickDeviceId, SDL_JoyBatteryEvent joyBatteryEvent)
|
||||
private void HandleJoyBatteryUpdated(uint joystickDeviceId, SDL_JoyBatteryEvent joyBatteryEvent)
|
||||
{
|
||||
Logger.Info?.Print(LogClass.Hid,
|
||||
$"{SDL_GetGamepadNameForID(joystickDeviceId)}, Battery percent: {joyBatteryEvent.percent}");
|
||||
@@ -205,27 +200,18 @@ namespace Ryujinx.Input.SDl3
|
||||
}
|
||||
}
|
||||
|
||||
var instanceId = GetJoystickIndexByGamepadId(id);
|
||||
|
||||
if (instanceId == nint.Zero)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
nint gamepadHandle = SDL_OpenGamepad(instanceId);
|
||||
|
||||
if (gamepadHandle == nint.Zero)
|
||||
var gamepadInfo = GetJoystickIndexByGamepadId(id);
|
||||
if (gamepadInfo == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Console.WriteLine(SDL_GetGamepadName(gamepadHandle));
|
||||
if (SDL_GetGamepadName(gamepadHandle).StartsWith(SDL3JoyCon.Prefix))
|
||||
if (SDL3JoyCon.IsJoyCon(gamepadInfo.gamepadHandle))
|
||||
{
|
||||
return new SDL3JoyCon(gamepadHandle, id);
|
||||
return new SDL3JoyCon(gamepadInfo);
|
||||
}
|
||||
|
||||
return new SDL3Gamepad(gamepadHandle, id);
|
||||
return new SDL3Gamepad(gamepadInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user