Skip to content

Commit

Permalink
Fix for duplicate controller names under Ava when two controllers of …
Browse files Browse the repository at this point in the history
…the same type are attached (#29)
  • Loading branch information
amurgshere authored Oct 21, 2024
1 parent 9ae89d5 commit 0f3c7f9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
49 changes: 32 additions & 17 deletions src/Ryujinx.Input.SDL2/SDL2Gamepad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public GamepadStateSnapshot GetMappedStateSnapshot()
{
if (_buttonsUserMapping.Count == 0)
return rawState;


// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
foreach (ButtonMappingEntry entry in _buttonsUserMapping)
Expand Down Expand Up @@ -291,11 +291,28 @@ private static float ConvertRawStickValue(short value)
return value * ConvertRate;
}

private JoyconConfigControllerStick<GamepadInputId, Common.Configuration.Hid.Controller.StickInputId> 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)
return (0.0f, 0.0f);


(short stickX, short stickY) = GetStickXY(inputId);

Expand All @@ -304,24 +321,22 @@ private static float ConvertRawStickValue(short value)

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;
}
}
}

Expand Down
28 changes: 19 additions & 9 deletions src/Ryujinx/UI/ViewModels/Input/InputViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,28 @@ private static string GetShortGamepadId(string str)

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();
DeviceList.Clear();
Devices.Add((DeviceType.None, Disabled, LocaleManager.Instance[LocaleKeys.ControllerSettingsDeviceDisabled]));

int controllerNumber = 0;
foreach (string id in _mainWindow.InputManager.KeyboardDriver.GamepadsIds)
{
using IGamepad gamepad = _mainWindow.InputManager.KeyboardDriver.GetGamepad(id);
Expand All @@ -455,17 +471,11 @@ public void LoadDevices()

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);
}
Expand Down Expand Up @@ -679,7 +689,7 @@ public async void LoadProfile()

if (!File.Exists(path))
{
var index = ProfilesList.IndexOf(ProfileName);
int index = ProfilesList.IndexOf(ProfileName);
if (index != -1)
{
ProfilesList.RemoveAt(index);
Expand Down

0 comments on commit 0f3c7f9

Please sign in to comment.