2021-04-14 12:28:43 +02:00
|
|
|
|
using Ryujinx.Common.Configuration.Hid;
|
|
|
|
|
using Ryujinx.Common.Configuration.Hid.Controller;
|
|
|
|
|
using Ryujinx.Common.Configuration.Hid.Keyboard;
|
|
|
|
|
using Ryujinx.HLE.HOS.Services.Hid;
|
|
|
|
|
using System;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
private bool _isDisposed;
|
|
|
|
|
|
|
|
|
|
private List<InputConfig> _inputConfig;
|
2021-05-16 17:12:14 +02:00
|
|
|
|
private bool _enableKeyboard;
|
|
|
|
|
private Switch _device;
|
2021-04-14 12:28:43 +02:00
|
|
|
|
|
|
|
|
|
public NpadManager(IGamepadDriver keyboardDriver, IGamepadDriver gamepadDriver)
|
|
|
|
|
{
|
|
|
|
|
_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-05-16 17:12:14 +02:00
|
|
|
|
_inputConfig = new List<InputConfig>();
|
|
|
|
|
_enableKeyboard = false;
|
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-05-16 17:12:14 +02:00
|
|
|
|
ReloadConfiguration(_inputConfig, _enableKeyboard);
|
2021-04-14 12:28:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleOnGamepadConnected(string id)
|
|
|
|
|
{
|
|
|
|
|
// Force input reload
|
2021-05-16 17:12:14 +02:00
|
|
|
|
ReloadConfiguration(_inputConfig, _enableKeyboard);
|
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-05-16 17:12:14 +02:00
|
|
|
|
public void ReloadConfiguration(List<InputConfig> inputConfig, bool enableKeyboard)
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_inputConfig = inputConfig;
|
2021-05-16 17:12:14 +02:00
|
|
|
|
_enableKeyboard = enableKeyboard;
|
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-05-16 17:12:14 +02:00
|
|
|
|
public void Initialize(Switch device, List<InputConfig> inputConfig, bool enableKeyboard)
|
|
|
|
|
{
|
|
|
|
|
_device = device;
|
|
|
|
|
_device.Configuration.RefreshInputConfig = RefreshInputConfigForHLE;
|
|
|
|
|
|
|
|
|
|
ReloadConfiguration(inputConfig, enableKeyboard);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update()
|
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;
|
|
|
|
|
SixAxisInput motionState = default;
|
|
|
|
|
|
|
|
|
|
NpadController controller = _controllers[(int)inputConfig.PlayerIndex];
|
|
|
|
|
|
|
|
|
|
// Do we allow input updates and is a controller connected?
|
|
|
|
|
if (!_blockInputUpdates && controller != null)
|
|
|
|
|
{
|
|
|
|
|
DriverConfigurationUpdate(ref controller, inputConfig);
|
|
|
|
|
|
|
|
|
|
controller.UpdateUserConfiguration(inputConfig);
|
|
|
|
|
controller.Update();
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
motionState = controller.GetHLEMotionState();
|
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
|
|
|
|
|
motionState.Orientation = new float[9];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inputState.PlayerId = (Ryujinx.HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex;
|
|
|
|
|
motionState.PlayerId = (Ryujinx.HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex;
|
|
|
|
|
|
|
|
|
|
hleInputStates.Add(inputState);
|
|
|
|
|
hleMotionStates.Add(motionState);
|
|
|
|
|
}
|
|
|
|
|
|
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-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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|