2021-06-24 02:09:08 +02:00
|
|
|
using Ryujinx.Common.Configuration.Hid;
|
2021-04-14 12:28:43 +02:00
|
|
|
using Ryujinx.Common.Configuration.Hid.Controller;
|
2021-07-24 01:01:36 +02:00
|
|
|
using Ryujinx.Common.Configuration.Hid.Controller.Motion;
|
2021-04-14 12:28:43 +02:00
|
|
|
using Ryujinx.Common.Configuration.Hid.Keyboard;
|
|
|
|
using Ryujinx.HLE.HOS.Services.Hid;
|
|
|
|
using System;
|
2021-08-05 00:39:40 +02:00
|
|
|
using System.Collections.Concurrent;
|
2021-04-14 12:28:43 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using CemuHookClient = Ryujinx.Input.Motion.CemuHook.Client;
|
2021-05-16 17:12:14 +02:00
|
|
|
using Switch = Ryujinx.HLE.Switch;
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.Input.HLE
|
|
|
|
{
|
|
|
|
public class NpadManager : IDisposable
|
|
|
|
{
|
|
|
|
private CemuHookClient _cemuHookClient;
|
|
|
|
|
|
|
|
private object _lock = new object();
|
|
|
|
|
|
|
|
private bool _blockInputUpdates;
|
|
|
|
|
|
|
|
private const int MaxControllers = 9;
|
|
|
|
|
|
|
|
private NpadController[] _controllers;
|
|
|
|
|
|
|
|
private readonly IGamepadDriver _keyboardDriver;
|
|
|
|
private readonly IGamepadDriver _gamepadDriver;
|
2021-06-24 02:09:08 +02:00
|
|
|
private readonly IGamepadDriver _mouseDriver;
|
2021-04-14 12:28:43 +02:00
|
|
|
private bool _isDisposed;
|
|
|
|
|
|
|
|
private List<InputConfig> _inputConfig;
|
2021-05-16 17:12:14 +02:00
|
|
|
private bool _enableKeyboard;
|
2021-06-24 02:09:08 +02:00
|
|
|
private bool _enableMouse;
|
2021-05-16 17:12:14 +02:00
|
|
|
private Switch _device;
|
2021-04-14 12:28:43 +02:00
|
|
|
|
2021-06-24 02:09:08 +02:00
|
|
|
public NpadManager(IGamepadDriver keyboardDriver, IGamepadDriver gamepadDriver, IGamepadDriver mouseDriver)
|
2021-04-14 12:28:43 +02:00
|
|
|
{
|
|
|
|
_controllers = new NpadController[MaxControllers];
|
2021-05-16 17:12:14 +02:00
|
|
|
_cemuHookClient = new CemuHookClient(this);
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
|
|
_keyboardDriver = keyboardDriver;
|
|
|
|
_gamepadDriver = gamepadDriver;
|
2021-06-24 02:09:08 +02:00
|
|
|
_mouseDriver = mouseDriver;
|
2021-05-16 17:12:14 +02:00
|
|
|
_inputConfig = new List<InputConfig>();
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
|
|
_gamepadDriver.OnGamepadConnected += HandleOnGamepadConnected;
|
|
|
|
_gamepadDriver.OnGamepadDisconnected += HandleOnGamepadDisconnected;
|
|
|
|
}
|
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
private void RefreshInputConfigForHLE()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
_device.Hid.RefreshInputConfig(_inputConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:28:43 +02:00
|
|
|
private void HandleOnGamepadDisconnected(string obj)
|
|
|
|
{
|
|
|
|
// Force input reload
|
2021-06-24 02:09:08 +02:00
|
|
|
ReloadConfiguration(_inputConfig, _enableKeyboard, _enableMouse);
|
2021-04-14 12:28:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void HandleOnGamepadConnected(string id)
|
|
|
|
{
|
|
|
|
// Force input reload
|
2021-06-24 02:09:08 +02:00
|
|
|
ReloadConfiguration(_inputConfig, _enableKeyboard, _enableMouse);
|
2021-04-14 12:28:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return controller.GamepadDriver != null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 02:09:08 +02:00
|
|
|
public void ReloadConfiguration(List<InputConfig> inputConfig, bool enableKeyboard, bool enableMouse)
|
2021-04-14 12:28:43 +02:00
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _controllers.Length; i++)
|
|
|
|
{
|
|
|
|
_controllers[i]?.Dispose();
|
|
|
|
_controllers[i] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (InputConfig inputConfigEntry in inputConfig)
|
|
|
|
{
|
|
|
|
NpadController controller = new NpadController(_cemuHookClient);
|
|
|
|
|
|
|
|
bool isValid = DriverConfigurationUpdate(ref controller, inputConfigEntry);
|
|
|
|
|
|
|
|
if (!isValid)
|
|
|
|
{
|
|
|
|
controller.Dispose();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_controllers[(int)inputConfigEntry.PlayerIndex] = controller;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 02:09:08 +02:00
|
|
|
_inputConfig = inputConfig;
|
2021-05-16 17:12:14 +02:00
|
|
|
_enableKeyboard = enableKeyboard;
|
2021-06-24 02:09:08 +02:00
|
|
|
_enableMouse = enableMouse;
|
2021-04-14 12:28:43 +02:00
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
_device.Hid.RefreshInputConfig(inputConfig);
|
2021-04-14 12:28:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnblockInputUpdates()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
_blockInputUpdates = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void BlockInputUpdates()
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
_blockInputUpdates = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 02:09:08 +02:00
|
|
|
public void Initialize(Switch device, List<InputConfig> inputConfig, bool enableKeyboard, bool enableMouse)
|
2021-05-16 17:12:14 +02:00
|
|
|
{
|
|
|
|
_device = device;
|
|
|
|
_device.Configuration.RefreshInputConfig = RefreshInputConfigForHLE;
|
|
|
|
|
2021-06-24 02:09:08 +02:00
|
|
|
ReloadConfiguration(inputConfig, enableKeyboard, enableMouse);
|
2021-05-16 17:12:14 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 02:09:08 +02:00
|
|
|
public void Update(float aspectRatio = 0)
|
2021-04-14 12:28:43 +02:00
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
List<GamepadInput> hleInputStates = new List<GamepadInput>();
|
|
|
|
List<SixAxisInput> hleMotionStates = new List<SixAxisInput>(NpadDevices.MaxControllers);
|
|
|
|
|
2021-04-21 01:47:49 +02:00
|
|
|
KeyboardInput? hleKeyboardInput = null;
|
|
|
|
|
2021-04-14 12:28:43 +02:00
|
|
|
foreach (InputConfig inputConfig in _inputConfig)
|
|
|
|
{
|
|
|
|
GamepadInput inputState = default;
|
2021-07-24 01:01:36 +02:00
|
|
|
(SixAxisInput, SixAxisInput) motionState = default;
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
|
|
NpadController controller = _controllers[(int)inputConfig.PlayerIndex];
|
2021-08-05 00:39:40 +02:00
|
|
|
Ryujinx.HLE.HOS.Services.Hid.PlayerIndex playerIndex = (Ryujinx.HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex;
|
2021-04-14 12:28:43 +02:00
|
|
|
|
2021-07-24 01:01:36 +02:00
|
|
|
bool isJoyconPair = false;
|
|
|
|
|
2021-04-14 12:28:43 +02:00
|
|
|
// Do we allow input updates and is a controller connected?
|
|
|
|
if (!_blockInputUpdates && controller != null)
|
|
|
|
{
|
|
|
|
DriverConfigurationUpdate(ref controller, inputConfig);
|
|
|
|
|
|
|
|
controller.UpdateUserConfiguration(inputConfig);
|
|
|
|
controller.Update();
|
2021-08-05 00:39:40 +02:00
|
|
|
controller.UpdateRumble(_device.Hid.Npads.GetRumbleQueue(playerIndex));
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
|
|
inputState = controller.GetHLEInputState();
|
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
inputState.Buttons |= _device.Hid.UpdateStickButtons(inputState.LStick, inputState.RStick);
|
2021-04-14 12:28:43 +02:00
|
|
|
|
2021-07-24 01:01:36 +02:00
|
|
|
isJoyconPair = inputConfig.ControllerType == Common.Configuration.Hid.ControllerType.JoyconPair;
|
|
|
|
|
|
|
|
var altMotionState = isJoyconPair ? controller.GetHLEMotionState(true) : default;
|
|
|
|
|
|
|
|
motionState = (controller.GetHLEMotionState(), altMotionState);
|
2021-04-21 01:47:49 +02:00
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
if (_enableKeyboard)
|
2021-04-21 01:47:49 +02:00
|
|
|
{
|
|
|
|
hleKeyboardInput = controller.GetHLEKeyboardInput();
|
|
|
|
}
|
2021-04-14 12:28:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Ensure that orientation isn't null
|
2021-07-24 01:01:36 +02:00
|
|
|
motionState.Item1.Orientation = new float[9];
|
2021-04-14 12:28:43 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 00:39:40 +02:00
|
|
|
inputState.PlayerId = playerIndex;
|
|
|
|
motionState.Item1.PlayerId = playerIndex;
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
|
|
hleInputStates.Add(inputState);
|
2021-07-24 01:01:36 +02:00
|
|
|
hleMotionStates.Add(motionState.Item1);
|
|
|
|
|
|
|
|
if (isJoyconPair && !motionState.Item2.Equals(default))
|
|
|
|
{
|
2021-08-05 00:39:40 +02:00
|
|
|
motionState.Item2.PlayerId = playerIndex;
|
2021-07-24 01:01:36 +02:00
|
|
|
|
|
|
|
hleMotionStates.Add(motionState.Item2);
|
|
|
|
}
|
2021-04-14 12:28:43 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
_device.Hid.Npads.Update(hleInputStates);
|
|
|
|
_device.Hid.Npads.UpdateSixAxis(hleMotionStates);
|
2021-04-21 01:47:49 +02:00
|
|
|
|
|
|
|
if (hleKeyboardInput.HasValue)
|
|
|
|
{
|
2021-05-16 17:12:14 +02:00
|
|
|
_device.Hid.Keyboard.Update(hleKeyboardInput.Value);
|
2021-04-21 01:47:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 02:09:08 +02:00
|
|
|
if (_enableMouse)
|
|
|
|
{
|
|
|
|
var mouse = _mouseDriver.GetGamepad("0") as IMouse;
|
|
|
|
|
|
|
|
var 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var 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);
|
|
|
|
}
|
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
_device.TamperMachine.UpdateInput(hleInputStates);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal InputConfig GetPlayerInputConfigByIndex(int index)
|
|
|
|
{
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
return _inputConfig.Find(x => x.PlayerIndex == (Ryujinx.Common.Configuration.Hid.PlayerIndex)index);
|
2021-04-14 12:28:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|