517 lines
20 KiB
C#
517 lines
20 KiB
C#
using Ryujinx.Common.Configuration.Hid;
|
|
using Ryujinx.Common.Configuration.Hid.Controller;
|
|
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.HLE.HOS.Services.Hid;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading;
|
|
using CemuHookClient = Ryujinx.Input.Motion.CemuHook.Client;
|
|
using ControllerType = Ryujinx.Common.Configuration.Hid.ControllerType;
|
|
using PlayerIndex = Ryujinx.HLE.HOS.Services.Hid.PlayerIndex;
|
|
using Switch = Ryujinx.HLE.Switch;
|
|
|
|
|
|
namespace Ryujinx.Input.HLE
|
|
{
|
|
public class NpadManager : IDisposable
|
|
{
|
|
private readonly CemuHookClient _cemuHookClient;
|
|
|
|
private readonly Lock _lock = new();
|
|
|
|
private bool _blockInputUpdates;
|
|
|
|
private const int MaxControllers = 9;
|
|
|
|
private readonly NpadController[] _controllers;
|
|
|
|
private readonly IGamepadDriver _keyboardDriver;
|
|
private readonly IGamepadDriver _gamepadDriver;
|
|
private readonly IGamepadDriver _mouseDriver;
|
|
private bool _isDisposed;
|
|
|
|
private List<InputConfig> _inputConfig;
|
|
private bool _enableKeyboard;
|
|
private bool _enableMouse;
|
|
private bool _enableAutoAssign;
|
|
private Switch _device;
|
|
|
|
public NpadManager(IGamepadDriver keyboardDriver, IGamepadDriver gamepadDriver, IGamepadDriver mouseDriver)
|
|
{
|
|
_controllers = new NpadController[MaxControllers];
|
|
_cemuHookClient = new CemuHookClient(this);
|
|
|
|
_keyboardDriver = keyboardDriver;
|
|
_gamepadDriver = gamepadDriver;
|
|
_mouseDriver = mouseDriver;
|
|
_inputConfig = new List<InputConfig>();
|
|
|
|
_gamepadDriver.OnGamepadConnected += HandleOnGamepadConnected;
|
|
_gamepadDriver.OnGamepadDisconnected += HandleOnGamepadDisconnected;
|
|
}
|
|
|
|
private void RefreshInputConfigForHLE()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
List<InputConfig> validInputs = new();
|
|
foreach (InputConfig inputConfigEntry in _inputConfig)
|
|
{
|
|
if (_controllers[(int)inputConfigEntry.PlayerIndex] != null)
|
|
{
|
|
validInputs.Add(inputConfigEntry);
|
|
}
|
|
}
|
|
|
|
_device.Hid.RefreshInputConfig(validInputs);
|
|
}
|
|
}
|
|
|
|
private void HandleOnGamepadDisconnected(string obj)
|
|
{
|
|
// Force input reload
|
|
lock (_lock)
|
|
{
|
|
// Forcibly disconnect any controllers with this ID.
|
|
for (int i = 0; i < _controllers.Length; i++)
|
|
{
|
|
if (_controllers[i]?.Id == obj)
|
|
{
|
|
_controllers[i]?.Dispose();
|
|
_controllers[i] = null;
|
|
}
|
|
}
|
|
|
|
ReloadConfiguration(_inputConfig, _enableKeyboard, _enableMouse, _enableAutoAssign);
|
|
}
|
|
}
|
|
|
|
private void HandleOnGamepadConnected(string id)
|
|
{
|
|
// Force input reload
|
|
ReloadConfiguration(_inputConfig, _enableKeyboard, _enableMouse, _enableAutoAssign);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private bool DriverConfigurationUpdate(ref NpadController controller, InputConfig config)
|
|
{
|
|
IGamepadDriver targetDriver = _gamepadDriver;
|
|
|
|
if (config is StandardControllerInputConfig)
|
|
{
|
|
targetDriver = _gamepadDriver;
|
|
}
|
|
else if (config is StandardKeyboardInputConfig)
|
|
{
|
|
targetDriver = _keyboardDriver;
|
|
}
|
|
|
|
Debug.Assert(targetDriver != null, "Unknown input configuration!");
|
|
|
|
if (controller.GamepadDriver != targetDriver || controller.Id != config.Id)
|
|
{
|
|
return controller.UpdateDriverConfiguration(targetDriver, config);
|
|
}
|
|
|
|
return controller.GamepadDriver != null;
|
|
}
|
|
|
|
public void ReloadConfiguration(List<InputConfig> inputConfig, bool enableKeyboard, bool enableMouse, bool enableAutoAssign)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
NpadController[] oldControllers = _controllers.ToArray();
|
|
|
|
List<InputConfig> validInputs = new();
|
|
|
|
// if auto assign is disabled, we want to keep the old logic with profiles.
|
|
if (!enableAutoAssign)
|
|
{
|
|
foreach (InputConfig inputConfigEntry in inputConfig)
|
|
{
|
|
NpadController controller;
|
|
int index = (int)inputConfigEntry.PlayerIndex;
|
|
|
|
if (oldControllers[index] != null)
|
|
{
|
|
// Try reuse the existing controller.
|
|
controller = oldControllers[index];
|
|
oldControllers[index] = null;
|
|
}
|
|
else
|
|
{
|
|
controller = new(_cemuHookClient);
|
|
}
|
|
|
|
bool isValid = DriverConfigurationUpdate(ref controller, inputConfigEntry);
|
|
|
|
if (!isValid)
|
|
{
|
|
_controllers[index] = null;
|
|
controller.Dispose();
|
|
}
|
|
else
|
|
{
|
|
_controllers[index] = controller;
|
|
validInputs.Add(inputConfigEntry);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
List<IGamepad> controllers = _gamepadDriver.GetGamepads().ToList();
|
|
|
|
foreach (IGamepad activeController in controllers)
|
|
{
|
|
NpadController controller;
|
|
int index = controllers.FindIndex(x => x == activeController);
|
|
|
|
// Also if old controller exists, try to reuse it (and create their config too).
|
|
InputConfig config = CreateConfigFromController(activeController);
|
|
|
|
config.PlayerIndex = (Common.Configuration.Hid.PlayerIndex)index;
|
|
|
|
if (oldControllers[index] != null)
|
|
{
|
|
// Try reuse the existing controller.
|
|
controller = oldControllers[index];
|
|
oldControllers[index] = null;
|
|
}
|
|
else
|
|
{
|
|
controller = new(_cemuHookClient);
|
|
}
|
|
|
|
// TODO: call function to get config from controller here
|
|
|
|
bool isValid = DriverConfigurationUpdate(ref controller, config);
|
|
|
|
if (!isValid)
|
|
{
|
|
_controllers[index] = null;
|
|
controller.Dispose();
|
|
}
|
|
else
|
|
{
|
|
_controllers[index] = controller;
|
|
validInputs.Add(config);
|
|
}
|
|
}
|
|
}
|
|
|
|
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 = (enableAutoAssign) ? validInputs : inputConfig;
|
|
_enableKeyboard = enableKeyboard;
|
|
_enableMouse = enableMouse;
|
|
_enableAutoAssign = enableAutoAssign;
|
|
|
|
_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)
|
|
{
|
|
foreach (InputConfig inputConfig in _inputConfig)
|
|
{
|
|
_controllers[(int)inputConfig.PlayerIndex]?.GamepadDriver?.Clear();
|
|
}
|
|
|
|
_blockInputUpdates = false;
|
|
}
|
|
}
|
|
|
|
public void BlockInputUpdates()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
_blockInputUpdates = true;
|
|
}
|
|
}
|
|
|
|
public void Initialize(Switch device, List<InputConfig> inputConfig, bool enableKeyboard, bool enableMouse, bool enableAutoAssign)
|
|
{
|
|
_device = device;
|
|
_device.Configuration.RefreshInputConfig = RefreshInputConfigForHLE;
|
|
|
|
ReloadConfiguration(inputConfig, enableKeyboard, enableMouse, enableAutoAssign);
|
|
}
|
|
|
|
public void Update(float aspectRatio = 1)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
List<GamepadInput> hleInputStates = new();
|
|
List<SixAxisInput> hleMotionStates = new(NpadDevices.MaxControllers);
|
|
|
|
KeyboardInput? hleKeyboardInput = null;
|
|
|
|
foreach (InputConfig inputConfig in _inputConfig)
|
|
{
|
|
GamepadInput inputState = default;
|
|
(SixAxisInput, SixAxisInput) motionState = default;
|
|
|
|
NpadController controller = _controllers[(int)inputConfig.PlayerIndex];
|
|
PlayerIndex playerIndex = (PlayerIndex)inputConfig.PlayerIndex;
|
|
|
|
bool isJoyconPair = false;
|
|
|
|
// Do we allow input updates and is a controller connected?
|
|
if (!_blockInputUpdates && controller != null)
|
|
{
|
|
DriverConfigurationUpdate(ref controller, inputConfig);
|
|
|
|
controller.UpdateUserConfiguration(inputConfig);
|
|
controller.Update();
|
|
controller.UpdateRumble(_device.Hid.Npads.GetRumbleQueue(playerIndex));
|
|
|
|
inputState = controller.GetHLEInputState();
|
|
|
|
inputState.Buttons |= _device.Hid.UpdateStickButtons(inputState.LStick, inputState.RStick);
|
|
|
|
isJoyconPair = inputConfig.ControllerType == ControllerType.JoyconPair;
|
|
|
|
SixAxisInput altMotionState = isJoyconPair ? controller.GetHLEMotionState(true) : default;
|
|
|
|
motionState = (controller.GetHLEMotionState(), altMotionState);
|
|
}
|
|
else
|
|
{
|
|
// Ensure that orientation isn't null
|
|
motionState.Item1.Orientation = new float[9];
|
|
}
|
|
|
|
inputState.PlayerId = playerIndex;
|
|
motionState.Item1.PlayerId = playerIndex;
|
|
|
|
hleInputStates.Add(inputState);
|
|
hleMotionStates.Add(motionState.Item1);
|
|
|
|
if (isJoyconPair && !motionState.Item2.Equals(default))
|
|
{
|
|
motionState.Item2.PlayerId = playerIndex;
|
|
|
|
hleMotionStates.Add(motionState.Item2);
|
|
}
|
|
}
|
|
|
|
if (!_blockInputUpdates && _enableKeyboard)
|
|
{
|
|
hleKeyboardInput = NpadController.GetHLEKeyboardInput(_keyboardDriver);
|
|
}
|
|
|
|
_device.Hid.Npads.Update(hleInputStates);
|
|
_device.Hid.Npads.UpdateSixAxis(hleMotionStates);
|
|
|
|
if (hleKeyboardInput.HasValue)
|
|
{
|
|
_device.Hid.Keyboard.Update(hleKeyboardInput.Value);
|
|
}
|
|
|
|
if (_enableMouse)
|
|
{
|
|
IMouse mouse = _mouseDriver.GetGamepad("0") as IMouse;
|
|
|
|
MouseStateSnapshot mouseInput = IMouse.GetMouseStateSnapshot(mouse);
|
|
|
|
uint buttons = 0;
|
|
|
|
if (mouseInput.IsPressed(MouseButton.Button1))
|
|
{
|
|
buttons |= 1 << 0;
|
|
}
|
|
|
|
if (mouseInput.IsPressed(MouseButton.Button2))
|
|
{
|
|
buttons |= 1 << 1;
|
|
}
|
|
|
|
if (mouseInput.IsPressed(MouseButton.Button3))
|
|
{
|
|
buttons |= 1 << 2;
|
|
}
|
|
|
|
if (mouseInput.IsPressed(MouseButton.Button4))
|
|
{
|
|
buttons |= 1 << 3;
|
|
}
|
|
|
|
if (mouseInput.IsPressed(MouseButton.Button5))
|
|
{
|
|
buttons |= 1 << 4;
|
|
}
|
|
|
|
Vector2 position = IMouse.GetScreenPosition(mouseInput.Position, mouse.ClientSize, aspectRatio);
|
|
|
|
_device.Hid.Mouse.Update((int)position.X, (int)position.Y, buttons, (int)mouseInput.Scroll.X, (int)mouseInput.Scroll.Y, true);
|
|
}
|
|
else
|
|
{
|
|
_device.Hid.Mouse.Update(0, 0);
|
|
}
|
|
|
|
_device.TamperMachine.UpdateInput(hleInputStates);
|
|
}
|
|
}
|
|
|
|
internal InputConfig GetPlayerInputConfigByIndex(int index)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _inputConfig.FirstOrDefault(x => x.PlayerIndex == (Common.Configuration.Hid.PlayerIndex)index);
|
|
}
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (!_isDisposed)
|
|
{
|
|
_cemuHookClient.Dispose();
|
|
|
|
_gamepadDriver.OnGamepadConnected -= HandleOnGamepadConnected;
|
|
_gamepadDriver.OnGamepadDisconnected -= HandleOnGamepadDisconnected;
|
|
|
|
for (int i = 0; i < _controllers.Length; i++)
|
|
{
|
|
_controllers[i]?.Dispose();
|
|
}
|
|
|
|
_isDisposed = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
GC.SuppressFinalize(this);
|
|
Dispose(true);
|
|
}
|
|
}
|
|
}
|