From b4cac89c1fa93c55a5a14a6e9bd3a5a65ea9c6f7 Mon Sep 17 00:00:00 2001 From: TheToid Date: Wed, 9 Oct 2024 12:41:31 +1000 Subject: [PATCH] Fix some input controller issues (mapping sticks and duplicate controller names) (#31) Co-authored-by: reggie --- src/Ryujinx.Input.SDL2/SDL2Gamepad.cs | 58 ++++++++++++++----- .../UI/ViewModels/Input/InputViewModel.cs | 32 ++++++---- 2 files changed, 65 insertions(+), 25 deletions(-) diff --git a/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs b/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs index 187ca48d..ef27b7ca 100644 --- a/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs +++ b/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs @@ -313,6 +313,32 @@ namespace Ryujinx.Input.SDL2 return value * ConvertRate; } + private JoyconConfigControllerStick GetLogicalJoyStickConfig(StickInputId inputId) + { + switch (inputId) + { + case StickInputId.Left: + if (_configuration.RightJoyconStick.Joystick == Common.Configuration.Hid.Controller.StickInputId.Left) + { + return _configuration.RightJoyconStick; + } + else + { + return _configuration.LeftJoyconStick; + } + case StickInputId.Right: + if (_configuration.LeftJoyconStick.Joystick == Common.Configuration.Hid.Controller.StickInputId.Right) + { + return _configuration.LeftJoyconStick; + } + else + { + return _configuration.RightJoyconStick; + } + } + + return null; + } public (float, float) GetStick(StickInputId inputId) { if (inputId == StickInputId.Unbound) @@ -343,24 +369,26 @@ namespace Ryujinx.Input.SDL2 if (HasConfiguration) { - if ((inputId == StickInputId.Left && _configuration.LeftJoyconStick.InvertStickX) || - (inputId == StickInputId.Right && _configuration.RightJoyconStick.InvertStickX)) - { - resultX = -resultX; - } + var joyconStickConfig = GetLogicalJoyStickConfig(inputId); - if ((inputId == StickInputId.Left && _configuration.LeftJoyconStick.InvertStickY) || - (inputId == StickInputId.Right && _configuration.RightJoyconStick.InvertStickY)) + if (joyconStickConfig != null) { - resultY = -resultY; - } + if (joyconStickConfig.InvertStickX) + { + resultX = -resultX; + } - if ((inputId == StickInputId.Left && _configuration.LeftJoyconStick.Rotate90CW) || - (inputId == StickInputId.Right && _configuration.RightJoyconStick.Rotate90CW)) - { - float temp = resultX; - resultX = resultY; - resultY = -temp; + if (joyconStickConfig.InvertStickY) + { + resultY = -resultY; + } + + if (joyconStickConfig.Rotate90CW) + { + float temp = resultX; + resultX = resultY; + resultY = -temp; + } } } diff --git a/src/Ryujinx/UI/ViewModels/Input/InputViewModel.cs b/src/Ryujinx/UI/ViewModels/Input/InputViewModel.cs index 89cc6496..b5252c5b 100644 --- a/src/Ryujinx/UI/ViewModels/Input/InputViewModel.cs +++ b/src/Ryujinx/UI/ViewModels/Input/InputViewModel.cs @@ -45,7 +45,6 @@ namespace Ryujinx.Ava.UI.ViewModels.Input private PlayerIndex _playerId; private int _controller; - private int _controllerNumber; private string _controllerImage; private int _device; private object _configViewModel; @@ -439,6 +438,24 @@ namespace Ryujinx.Ava.UI.ViewModels.Input public void LoadDevices() { + string GetGamepadName(IGamepad gamepad, int controllerNumber) + { + return $"{GetShortGamepadName(gamepad.Name)} ({controllerNumber})"; + } + + string GetUniqueGamepadName(IGamepad gamepad, ref int controllerNumber) + { + string name = GetGamepadName(gamepad, controllerNumber); + + if (Devices.Any(controller => controller.Name == name)) + { + controllerNumber++; + name = GetGamepadName(gamepad, controllerNumber); + } + + return name; + } + lock (Devices) { Devices.Clear(); @@ -455,23 +472,18 @@ namespace Ryujinx.Ava.UI.ViewModels.Input } } + int controllerNumber = 0; foreach (string id in _mainWindow.InputManager.GamepadDriver.GamepadsIds) { using IGamepad gamepad = _mainWindow.InputManager.GamepadDriver.GetGamepad(id); if (gamepad != null) { - if (Devices.Any(controller => GetShortGamepadId(controller.Id) == GetShortGamepadId(gamepad.Id))) - { - _controllerNumber++; - } - - Devices.Add((DeviceType.Controller, id, $"{GetShortGamepadName(gamepad.Name)} ({_controllerNumber})")); + string name = GetUniqueGamepadName(gamepad, ref controllerNumber); + Devices.Add((DeviceType.Controller, id, name)); } } - _controllerNumber = 0; - DeviceList.AddRange(Devices.Select(x => x.Name)); Device = Math.Min(Device, DeviceList.Count); } @@ -685,7 +697,7 @@ namespace Ryujinx.Ava.UI.ViewModels.Input if (!File.Exists(path)) { - var index = ProfilesList.IndexOf(ProfileName); + int index = ProfilesList.IndexOf(ProfileName); if (index != -1) { ProfilesList.RemoveAt(index);