use int game controller ids

This commit is contained in:
Emmanuel Hansen 2023-07-08 07:29:56 +00:00
parent e9c7564a03
commit 6262bd1730
4 changed files with 40 additions and 51 deletions

View File

@ -417,29 +417,27 @@ namespace LibRyujinx
} }
[UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_inputSetButtonPressed")] [UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_inputSetButtonPressed")]
public static void JniSetButtonPressed(JEnvRef jEnv, JObjectLocalRef jObj, JInt button, JStringLocalRef id) public static void JniSetButtonPressed(JEnvRef jEnv, JObjectLocalRef jObj, JInt button, JInt id)
{ {
SetButtonPressed((Ryujinx.Input.GamepadButtonInputId)(int)button, GetString(jEnv, id)); SetButtonPressed((Ryujinx.Input.GamepadButtonInputId)(int)button, id);
} }
[UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_inputSetButtonReleased")] [UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_inputSetButtonReleased")]
public static void JniSetButtonReleased(JEnvRef jEnv, JObjectLocalRef jObj, JInt button, JStringLocalRef id) public static void JniSetButtonReleased(JEnvRef jEnv, JObjectLocalRef jObj, JInt button, JInt id)
{ {
SetButtonReleased((Ryujinx.Input.GamepadButtonInputId)(int)button, GetString(jEnv, id)); SetButtonReleased((Ryujinx.Input.GamepadButtonInputId)(int)button, id);
} }
[UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_inputSetStickAxis")] [UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_inputSetStickAxis")]
public static void JniSetStickAxis(JEnvRef jEnv, JObjectLocalRef jObj, JInt stick, JFloat x, JFloat y, JStringLocalRef id) public static void JniSetStickAxis(JEnvRef jEnv, JObjectLocalRef jObj, JInt stick, JFloat x, JFloat y, JInt id)
{ {
SetStickAxis((Ryujinx.Input.StickInputId)(int)stick, new System.Numerics.Vector2(x, y), GetString(jEnv, id)); SetStickAxis((Ryujinx.Input.StickInputId)(int)stick, new System.Numerics.Vector2(x, y), id);
} }
[UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_inputConnectGamepad")] [UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_inputConnectGamepad")]
public static JStringLocalRef JniConnectGamepad(JEnvRef jEnv, JObjectLocalRef jObj, JInt index) public static JInt JniConnectGamepad(JEnvRef jEnv, JObjectLocalRef jObj, JInt index)
{ {
var id = ConnectGamepad(index); return ConnectGamepad(index);
return (id ?? "").AsSpan().WithSafeFixed(jEnv, CreateString);
} }
private static Stream OpenFile(int descriptor) private static Stream OpenFile(int descriptor)

View File

@ -73,22 +73,22 @@ namespace LibRyujinx
_virtualTouchScreen?.ReleaseTouch(); _virtualTouchScreen?.ReleaseTouch();
} }
public static void SetButtonPressed(GamepadButtonInputId button, string id) public static void SetButtonPressed(GamepadButtonInputId button, int id)
{ {
_gamepadDriver?.SetButtonPressed(button, id); _gamepadDriver?.SetButtonPressed(button, id);
} }
public static void SetButtonReleased(GamepadButtonInputId button, string id) public static void SetButtonReleased(GamepadButtonInputId button, int id)
{ {
_gamepadDriver?.SetButtonReleased(button, id); _gamepadDriver?.SetButtonReleased(button, id);
} }
public static void SetStickAxis(StickInputId stick, Vector2 axes, string deviceId) public static void SetStickAxis(StickInputId stick, Vector2 axes, int deviceId)
{ {
_gamepadDriver?.SetStickAxis(stick, axes, deviceId); _gamepadDriver?.SetStickAxis(stick, axes, deviceId);
} }
public static string ConnectGamepad(int index) public static int ConnectGamepad(int index)
{ {
var gamepad = _gamepadDriver?.GetGamepad(index); var gamepad = _gamepadDriver?.GetGamepad(index);
if (gamepad != null) if (gamepad != null)
@ -103,7 +103,7 @@ namespace LibRyujinx
_npadManager?.ReloadConfiguration(_configs.Where(x => x != null).ToList(), false, false); _npadManager?.ReloadConfiguration(_configs.Where(x => x != null).ToList(), false, false);
return gamepad?.Id ?? string.Empty; return int.TryParse(gamepad?.Id, out var idInt) ? idInt : -1;
} }
private static InputConfig CreateDefaultInputConfig() private static InputConfig CreateDefaultInputConfig()
@ -223,32 +223,27 @@ namespace LibRyujinx
} }
[UnmanagedCallersOnly(EntryPoint = "input_set_button_pressed")] [UnmanagedCallersOnly(EntryPoint = "input_set_button_pressed")]
public static void SetButtonPressedNative(GamepadButtonInputId button, IntPtr idPtr) public static void SetButtonPressedNative(GamepadButtonInputId button, int id)
{ {
var id = Marshal.PtrToStringAnsi(idPtr);
SetButtonPressed(button, id); SetButtonPressed(button, id);
} }
[UnmanagedCallersOnly(EntryPoint = "input_set_button_released")] [UnmanagedCallersOnly(EntryPoint = "input_set_button_released")]
public static void SetButtonReleased(GamepadButtonInputId button, IntPtr idPtr) public static void SetButtonReleasedNative(GamepadButtonInputId button, int id)
{ {
var id = Marshal.PtrToStringAnsi(idPtr);
SetButtonReleased(button, id); SetButtonReleased(button, id);
} }
[UnmanagedCallersOnly(EntryPoint = "input_set_stick_axis")] [UnmanagedCallersOnly(EntryPoint = "input_set_stick_axis")]
public static void SetStickAxisNative(StickInputId stick, Vector2 axes, IntPtr idPtr) public static void SetStickAxisNative(StickInputId stick, Vector2 axes, int id)
{ {
var id = Marshal.PtrToStringAnsi(idPtr);
SetStickAxis(stick, axes, id); SetStickAxis(stick, axes, id);
} }
[UnmanagedCallersOnly(EntryPoint = "input_connect_gamepad")] [UnmanagedCallersOnly(EntryPoint = "input_connect_gamepad")]
public static IntPtr ConnectGamepadNative(int index) public static IntPtr ConnectGamepadNative(int index)
{ {
var id = ConnectGamepad(index); return ConnectGamepad(index);
return Marshal.StringToHGlobalAnsi(id);
} }
} }
@ -386,18 +381,18 @@ namespace LibRyujinx
{ {
private readonly int _controllerCount; private readonly int _controllerCount;
public ReadOnlySpan<string> GamepadsIds => _gamePads.Keys.ToArray(); public ReadOnlySpan<string> GamepadsIds => _gamePads.Keys.Select(x => x.ToString()).ToArray();
public string DriverName => "SDL2"; public string DriverName => "Virtual";
public event Action<string> OnGamepadConnected; public event Action<string> OnGamepadConnected;
public event Action<string> OnGamepadDisconnected; public event Action<string> OnGamepadDisconnected;
private Dictionary<string, VirtualGamepad> _gamePads; private Dictionary<int, VirtualGamepad> _gamePads;
public VirtualGamepadDriver(int controllerCount) public VirtualGamepadDriver(int controllerCount)
{ {
_gamePads = new Dictionary<string, VirtualGamepad>(); _gamePads = new Dictionary<int, VirtualGamepad>();
for (int joystickIndex = 0; joystickIndex < controllerCount; joystickIndex++) for (int joystickIndex = 0; joystickIndex < controllerCount; joystickIndex++)
{ {
HandleJoyStickConnected(joystickIndex); HandleJoyStickConnected(joystickIndex);
@ -406,16 +401,10 @@ namespace LibRyujinx
_controllerCount = controllerCount; _controllerCount = controllerCount;
} }
private string GenerateGamepadId(int joystickIndex)
{
return "VirtualGamePad-" + joystickIndex;
}
private void HandleJoyStickConnected(int joystickDeviceId) private void HandleJoyStickConnected(int joystickDeviceId)
{ {
string id = GenerateGamepadId(joystickDeviceId); _gamePads[joystickDeviceId] = new VirtualGamepad(this, joystickDeviceId);
_gamePads[id] = new VirtualGamepad(this, id); OnGamepadConnected?.Invoke(joystickDeviceId.ToString());
OnGamepadConnected?.Invoke(id);
} }
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
@ -440,16 +429,15 @@ namespace LibRyujinx
public IGamepad GetGamepad(string id) public IGamepad GetGamepad(string id)
{ {
return _gamePads[id]; return _gamePads[int.Parse(id)];
} }
public IGamepad GetGamepad(int index) public IGamepad GetGamepad(int index)
{ {
string id = GenerateGamepadId(index); return _gamePads[index];
return _gamePads[id];
} }
public void SetStickAxis(StickInputId stick, Vector2 axes, string deviceId) public void SetStickAxis(StickInputId stick, Vector2 axes, int deviceId)
{ {
if(_gamePads.TryGetValue(deviceId, out var gamePad)) if(_gamePads.TryGetValue(deviceId, out var gamePad))
{ {
@ -457,7 +445,7 @@ namespace LibRyujinx
} }
} }
public void SetButtonPressed(GamepadButtonInputId button, string deviceId) public void SetButtonPressed(GamepadButtonInputId button, int deviceId)
{ {
if (_gamePads.TryGetValue(deviceId, out var gamePad)) if (_gamePads.TryGetValue(deviceId, out var gamePad))
{ {
@ -465,7 +453,7 @@ namespace LibRyujinx
} }
} }
public void SetButtonReleased(GamepadButtonInputId button, string deviceId) public void SetButtonReleased(GamepadButtonInputId button, int deviceId)
{ {
if (_gamePads.TryGetValue(deviceId, out var gamePad)) if (_gamePads.TryGetValue(deviceId, out var gamePad))
{ {
@ -482,12 +470,13 @@ namespace LibRyujinx
private Vector2[] _stickInputs; private Vector2[] _stickInputs;
public VirtualGamepad(VirtualGamepadDriver driver, string id) public VirtualGamepad(VirtualGamepadDriver driver, int id)
{ {
_buttonInputs = new bool[(int)GamepadButtonInputId.Count]; _buttonInputs = new bool[(int)GamepadButtonInputId.Count];
_stickInputs = new Vector2[(int)StickInputId.Count]; _stickInputs = new Vector2[(int)StickInputId.Count];
_driver = driver; _driver = driver;
Id = id; Id = id.ToString();
IdInt = id;
} }
public void Dispose() { } public void Dispose() { }
@ -495,6 +484,8 @@ namespace LibRyujinx
public GamepadFeaturesFlag Features { get; } public GamepadFeaturesFlag Features { get; }
public string Id { get; } public string Id { get; }
internal readonly int IdInt;
public string Name => Id; public string Name => Id;
public bool IsConnected { get; } public bool IsConnected { get; }
public Vector2[] StickInputs { get => _stickInputs; set => _stickInputs = value; } public Vector2[] StickInputs { get => _stickInputs; set => _stickInputs = value; }

View File

@ -47,7 +47,7 @@ typealias GamePadConfig = RadialGamePadConfig
class GameController(var activity: Activity, var ryujinxNative: RyujinxNative = RyujinxNative()) { class GameController(var activity: Activity, var ryujinxNative: RyujinxNative = RyujinxNative()) {
var leftGamePad: GamePad var leftGamePad: GamePad
var rightGamePad: GamePad var rightGamePad: GamePad
var controllerId: String? = null var controllerId: Int = -1
init { init {
leftGamePad = GamePad(generateConfig(true), 16f, activity) leftGamePad = GamePad(generateConfig(true), 16f, activity)
@ -92,12 +92,12 @@ class GameController(var activity: Activity, var ryujinxNative: RyujinxNative =
} }
fun connect(){ fun connect(){
if(controllerId.isNullOrEmpty()) if(controllerId == -1)
controllerId = ryujinxNative.inputConnectGamepad(0) controllerId = ryujinxNative.inputConnectGamepad(0)
} }
private fun handleEvent(ev: Event) { private fun handleEvent(ev: Event) {
if(controllerId.isNullOrEmpty()) if(controllerId == -1)
controllerId = ryujinxNative.inputConnectGamepad(0) controllerId = ryujinxNative.inputConnectGamepad(0)
controllerId?.apply { controllerId?.apply {

View File

@ -43,9 +43,9 @@ class RyujinxNative {
external fun inputSetTouchPoint(x: Int, y: Int): Unit external fun inputSetTouchPoint(x: Int, y: Int): Unit
external fun inputReleaseTouchPoint(): Unit external fun inputReleaseTouchPoint(): Unit
external fun inputUpdate(): Unit external fun inputUpdate(): Unit
external fun inputSetButtonPressed(button: Int, id: String): Unit external fun inputSetButtonPressed(button: Int, id: Int): Unit
external fun inputSetButtonReleased(button: Int, id: String): Unit external fun inputSetButtonReleased(button: Int, id: Int): Unit
external fun inputConnectGamepad(index: Int): String external fun inputConnectGamepad(index: Int): Int
external fun inputSetStickAxis(stick: Int, x: Float, y: Float, id: String): Unit external fun inputSetStickAxis(stick: Int, x: Float, y: Float, id: Int): Unit
external fun graphicsSetSurface(surface: Long): String external fun graphicsSetSurface(surface: Long): String
} }