refactor: enhance AutoAssignController to utilize ViewModel and improve controller refresh logic

This commit is contained in:
uncavo-hdmi
2025-02-02 16:35:30 +01:00
parent ab4bb0a885
commit 3ff9d1e128
5 changed files with 59 additions and 138 deletions

View File

@@ -4,6 +4,7 @@ using Ryujinx.Common.Configuration.Hid.Controller.Motion;
using ConfigGamepadInputId = Ryujinx.Common.Configuration.Hid.Controller.GamepadInputId;
using ConfigStickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId;
using Ryujinx.Common.Configuration.Hid.Keyboard;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Services.Hid;
using System;
using System.Collections.Generic;
@@ -40,7 +41,6 @@ namespace Ryujinx.Input.HLE
private List<InputConfig> _inputConfig;
private bool _enableKeyboard;
private bool _enableMouse;
private bool _enableAutoAssign;
private Switch _device;
public NpadManager(IGamepadDriver keyboardDriver, IGamepadDriver gamepadDriver, IGamepadDriver mouseDriver)
@@ -53,7 +53,7 @@ namespace Ryujinx.Input.HLE
_mouseDriver = mouseDriver;
_inputConfig = new List<InputConfig>();
_gamepadDriver.OnGamepadConnected += HandleOnGamepadConnected;
//_gamepadDriver.OnGamepadConnected += HandleOnGamepadConnected;
_gamepadDriver.OnGamepadDisconnected += HandleOnGamepadDisconnected;
}
@@ -89,7 +89,7 @@ namespace Ryujinx.Input.HLE
}
}
ReloadConfiguration(_inputConfig, _enableKeyboard, _enableMouse);
//ReloadConfiguration(_inputConfig, _enableKeyboard, _enableMouse);
}
}
@@ -130,7 +130,7 @@ namespace Ryujinx.Input.HLE
NpadController[] oldControllers = _controllers.ToArray();
List<InputConfig> validInputs = new();
foreach (InputConfig inputConfigEntry in inputConfig)
{
NpadController controller;
@@ -160,129 +160,23 @@ namespace Ryujinx.Input.HLE
validInputs.Add(inputConfigEntry);
}
}
for (int i = 0; i < oldControllers.Length; i++)
{
// Disconnect any controllers that weren't reused by the new configuration.
oldControllers[i]?.Dispose();
oldControllers[i] = null;
}
_inputConfig = inputConfig;
_enableKeyboard = enableKeyboard;
_enableMouse = enableMouse;
_device.Hid.RefreshInputConfig(validInputs);
}
}
private InputConfig CreateConfigFromController(IGamepad controller)
{
if (controller == null) return null;
string id = controller.Id.Split(" ")[0];
bool isNintendoStyle = controller.Name.Contains("Nintendo");
ControllerType controllerType;
if (isNintendoStyle && !controller.Name.Contains("(L/R)"))
{
if (controller.Name.Contains("(L)"))
{
controllerType = ControllerType.JoyconLeft;
}
else if (controller.Name.Contains("(R)"))
{
controllerType = ControllerType.JoyconRight;
}
else
{
controllerType = ControllerType.ProController;
}
}
else
{
// if it's not a nintendo controller, we assume it's a pro controller or a joycon pair
controllerType = ControllerType.ProController;
}
InputConfig config = new StandardControllerInputConfig
{
Version = InputConfig.CurrentVersion,
Backend = InputBackendType.GamepadSDL2,
Id = id,
ControllerType = controllerType,
DeadzoneLeft = 0.1f,
DeadzoneRight = 0.1f,
RangeLeft = 1.0f,
RangeRight = 1.0f,
TriggerThreshold = 0.5f,
LeftJoycon = new LeftJoyconCommonConfig<ConfigGamepadInputId>
{
DpadUp = (controllerType == ControllerType.JoyconLeft) ? ConfigGamepadInputId.Y : ConfigGamepadInputId.DpadUp,
DpadDown = (controllerType == ControllerType.JoyconLeft) ? ConfigGamepadInputId.A : ConfigGamepadInputId.DpadDown,
DpadLeft = (controllerType == ControllerType.JoyconLeft) ? ConfigGamepadInputId.B : ConfigGamepadInputId.DpadLeft,
DpadRight = (controllerType == ControllerType.JoyconLeft) ? ConfigGamepadInputId.X : ConfigGamepadInputId.DpadRight,
ButtonMinus = (controllerType == ControllerType.JoyconLeft) ? ConfigGamepadInputId.Plus : ConfigGamepadInputId.Minus,
ButtonL = ConfigGamepadInputId.LeftShoulder,
ButtonZl = ConfigGamepadInputId.LeftTrigger,
ButtonSl = (controllerType == ControllerType.JoyconLeft) ? ConfigGamepadInputId.LeftShoulder : ConfigGamepadInputId.Unbound,
ButtonSr = (controllerType == ControllerType.JoyconLeft) ? ConfigGamepadInputId.RightShoulder : ConfigGamepadInputId.Unbound,
},
LeftJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
{
Joystick = ConfigStickInputId.Left,
StickButton = ConfigGamepadInputId.LeftStick,
InvertStickX = false,
InvertStickY = false,
Rotate90CW = (controllerType == ControllerType.JoyconLeft),
},
RightJoycon = new RightJoyconCommonConfig<ConfigGamepadInputId>
{
ButtonA = ConfigGamepadInputId.B,
ButtonB = (controllerType == ControllerType.JoyconRight) ? ConfigGamepadInputId.Y : ConfigGamepadInputId.A,
ButtonX = (controllerType == ControllerType.JoyconRight) ? ConfigGamepadInputId.A : ConfigGamepadInputId.Y,
ButtonY = ConfigGamepadInputId.X,
ButtonPlus = ConfigGamepadInputId.Plus,
ButtonR = ConfigGamepadInputId.RightShoulder,
ButtonZr = ConfigGamepadInputId.RightTrigger,
ButtonSl = (controllerType == ControllerType.JoyconRight) ? ConfigGamepadInputId.LeftShoulder : ConfigGamepadInputId.Unbound,
ButtonSr = (controllerType == ControllerType.JoyconRight) ? ConfigGamepadInputId.RightShoulder : ConfigGamepadInputId.Unbound,
},
RightJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
{
Joystick = (controllerType == ControllerType.JoyconRight) ? ConfigStickInputId.Left : ConfigStickInputId.Right,
StickButton = ConfigGamepadInputId.RightStick,
InvertStickX = (controllerType == ControllerType.JoyconRight),
InvertStickY = (controllerType == ControllerType.JoyconRight),
Rotate90CW = (controllerType == ControllerType.JoyconRight),
},
Motion = new StandardMotionConfigController
{
MotionBackend = MotionInputBackendType.GamepadDriver,
EnableMotion = true,
Sensitivity = 100,
GyroDeadzone = 1,
},
Rumble = new RumbleConfigController
{
StrongRumble = 1f,
WeakRumble = 1f,
EnableRumble = false,
},
Led = new LedConfigController
{
EnableLed = false,
TurnOffLed = false,
UseRainbow = false,
LedColor = 0,
},
};
return config;
}
public void UnblockInputUpdates()
{
lock (_lock)