2020-08-23 16:54:11 -04:00
|
|
|
using Ryujinx.Common;
|
2020-04-02 20:10:02 -04:00
|
|
|
using Ryujinx.Common.Logging;
|
2020-05-02 22:00:53 -04:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2021-05-02 16:01:30 -04:00
|
|
|
using Ryujinx.HLE.HOS.Services.Hid.Types;
|
|
|
|
using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.Npad;
|
2021-12-29 08:49:10 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.CompilerServices;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Hid
|
|
|
|
{
|
|
|
|
public class NpadDevices : BaseDevice
|
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
private const int NoMatchNotifyFrequencyMs = 2000;
|
|
|
|
private int _activeCount;
|
|
|
|
private long _lastNotifyTimestamp;
|
|
|
|
|
|
|
|
public const int MaxControllers = 9; // Players 1-8 and Handheld
|
|
|
|
private ControllerType[] _configuredTypes;
|
|
|
|
private KEvent[] _styleSetUpdateEvents;
|
|
|
|
private bool[] _supportedPlayers;
|
2021-08-04 18:39:40 -04:00
|
|
|
private static HidVibrationValue _neutralVibrationValue = new HidVibrationValue
|
|
|
|
{
|
|
|
|
AmplitudeLow = 0f,
|
|
|
|
FrequencyLow = 160f,
|
|
|
|
AmplitudeHigh = 0f,
|
|
|
|
FrequencyHigh = 320f
|
|
|
|
};
|
2020-08-23 16:54:11 -04:00
|
|
|
|
|
|
|
internal NpadJoyHoldType JoyHold { get; set; }
|
2020-04-02 20:10:02 -04:00
|
|
|
internal bool SixAxisActive = false; // TODO: link to hidserver when implemented
|
2020-08-23 16:54:11 -04:00
|
|
|
internal ControllerType SupportedStyleSets { get; set; }
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2021-08-04 18:39:40 -04:00
|
|
|
public Dictionary<PlayerIndex, ConcurrentQueue<(HidVibrationValue, HidVibrationValue)>> RumbleQueues = new Dictionary<PlayerIndex, ConcurrentQueue<(HidVibrationValue, HidVibrationValue)>>();
|
|
|
|
public Dictionary<PlayerIndex, (HidVibrationValue, HidVibrationValue)> LastVibrationValues = new Dictionary<PlayerIndex, (HidVibrationValue, HidVibrationValue)>();
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
public NpadDevices(Switch device, bool active = true) : base(device, active)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
_configuredTypes = new ControllerType[MaxControllers];
|
|
|
|
|
|
|
|
SupportedStyleSets = ControllerType.Handheld | ControllerType.JoyconPair |
|
|
|
|
ControllerType.JoyconLeft | ControllerType.JoyconRight |
|
|
|
|
ControllerType.ProController;
|
|
|
|
|
|
|
|
_supportedPlayers = new bool[MaxControllers];
|
|
|
|
_supportedPlayers.AsSpan().Fill(true);
|
|
|
|
|
|
|
|
_styleSetUpdateEvents = new KEvent[MaxControllers];
|
|
|
|
for (int i = 0; i < _styleSetUpdateEvents.Length; ++i)
|
|
|
|
{
|
|
|
|
_styleSetUpdateEvents[i] = new KEvent(_device.System.KernelContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
_activeCount = 0;
|
|
|
|
|
|
|
|
JoyHold = NpadJoyHoldType.Vertical;
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
internal ref KEvent GetStyleSetUpdateEvent(PlayerIndex player)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
return ref _styleSetUpdateEvents[(int)player];
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
internal void ClearSupportedPlayers()
|
|
|
|
{
|
|
|
|
_supportedPlayers.AsSpan().Clear();
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
internal void SetSupportedPlayer(PlayerIndex player, bool supported = true)
|
|
|
|
{
|
|
|
|
_supportedPlayers[(int)player] = supported;
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
internal IEnumerable<PlayerIndex> GetSupportedPlayers()
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
for (int i = 0; i < _supportedPlayers.Length; ++i)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
if (_supportedPlayers[i])
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
yield return (PlayerIndex)i;
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
public bool Validate(int playerMin, int playerMax, ControllerType acceptedTypes, out int configuredCount, out PlayerIndex primaryIndex)
|
|
|
|
{
|
|
|
|
primaryIndex = PlayerIndex.Unknown;
|
|
|
|
configuredCount = 0;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
for (int i = 0; i < MaxControllers; ++i)
|
|
|
|
{
|
|
|
|
ControllerType npad = _configuredTypes[i];
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
if (npad == ControllerType.Handheld && _device.System.State.DockedMode)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
ControllerType currentType = (ControllerType)_device.Hid.SharedMemory.Npads[i].InternalState.StyleSet;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
if (currentType != ControllerType.None && (npad & acceptedTypes) != 0 && _supportedPlayers[i])
|
|
|
|
{
|
|
|
|
configuredCount++;
|
|
|
|
if (primaryIndex == PlayerIndex.Unknown)
|
|
|
|
{
|
|
|
|
primaryIndex = (PlayerIndex)i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
if (configuredCount < playerMin || configuredCount > playerMax || primaryIndex == PlayerIndex.Unknown)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
return false;
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
return true;
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
public void Configure(params ControllerConfig[] configs)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
_configuredTypes = new ControllerType[MaxControllers];
|
|
|
|
|
2020-04-02 20:10:02 -04:00
|
|
|
for (int i = 0; i < configs.Length; ++i)
|
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
PlayerIndex player = configs[i].Player;
|
2020-04-02 20:10:02 -04:00
|
|
|
ControllerType controllerType = configs[i].Type;
|
|
|
|
|
|
|
|
if (player > PlayerIndex.Handheld)
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException("Player must be Player1-8 or Handheld");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (controllerType == ControllerType.Handheld)
|
|
|
|
{
|
|
|
|
player = PlayerIndex.Handheld;
|
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
_configuredTypes[(int)player] = controllerType;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
Logger.Info?.Print(LogClass.Hid, $"Configured Controller {controllerType} to {player}");
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
public void Update(IList<GamepadInput> states)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
Remap();
|
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
Span<bool> updated = stackalloc bool[10];
|
2020-08-23 16:54:11 -04:00
|
|
|
|
|
|
|
// Update configured inputs
|
|
|
|
for (int i = 0; i < states.Count; ++i)
|
|
|
|
{
|
2021-05-02 16:01:30 -04:00
|
|
|
GamepadInput state = states[i];
|
|
|
|
|
|
|
|
updated[(int)state.PlayerId] = true;
|
|
|
|
|
|
|
|
UpdateInput(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < updated.Length; i++)
|
|
|
|
{
|
|
|
|
if (!updated[i])
|
|
|
|
{
|
|
|
|
UpdateDisconnectedInput((PlayerIndex)i);
|
|
|
|
}
|
2020-08-23 16:54:11 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
private void Remap()
|
|
|
|
{
|
|
|
|
// Remap/Init if necessary
|
|
|
|
for (int i = 0; i < MaxControllers; ++i)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
ControllerType config = _configuredTypes[i];
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
// Remove Handheld config when Docked
|
|
|
|
if (config == ControllerType.Handheld && _device.System.State.DockedMode)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
config = ControllerType.None;
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
// Auto-remap ProController and JoyconPair
|
|
|
|
if (config == ControllerType.JoyconPair && (SupportedStyleSets & ControllerType.JoyconPair) == 0 && (SupportedStyleSets & ControllerType.ProController) != 0)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
config = ControllerType.ProController;
|
|
|
|
}
|
|
|
|
else if (config == ControllerType.ProController && (SupportedStyleSets & ControllerType.ProController) == 0 && (SupportedStyleSets & ControllerType.JoyconPair) != 0)
|
|
|
|
{
|
|
|
|
config = ControllerType.JoyconPair;
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
// Check StyleSet and PlayerSet
|
|
|
|
if ((config & SupportedStyleSets) == 0 || !_supportedPlayers[i])
|
|
|
|
{
|
|
|
|
config = ControllerType.None;
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
SetupNpad((PlayerIndex)i, config);
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
if (_activeCount == 0 && PerformanceCounter.ElapsedMilliseconds > _lastNotifyTimestamp + NoMatchNotifyFrequencyMs)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
Logger.Warning?.Print(LogClass.Hid, $"No matching controllers found. Application requests '{SupportedStyleSets}' on '{string.Join(", ", GetSupportedPlayers())}'");
|
|
|
|
_lastNotifyTimestamp = PerformanceCounter.ElapsedMilliseconds;
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
private void SetupNpad(PlayerIndex player, ControllerType type)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2021-05-02 16:01:30 -04:00
|
|
|
ref NpadInternalState controller = ref _device.Hid.SharedMemory.Npads[(int)player].InternalState;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
ControllerType oldType = (ControllerType)controller.StyleSet;
|
2020-08-23 16:54:11 -04:00
|
|
|
|
|
|
|
if (oldType == type)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
return; // Already configured
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
controller = NpadInternalState.Create(); // Reset it
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
if (type == ControllerType.None)
|
|
|
|
{
|
|
|
|
_styleSetUpdateEvents[(int)player].ReadableEvent.Signal(); // Signal disconnect
|
|
|
|
_activeCount--;
|
|
|
|
|
|
|
|
Logger.Info?.Print(LogClass.Hid, $"Disconnected Controller {oldType} from {player}");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-02 20:10:02 -04:00
|
|
|
// TODO: Allow customizing colors at config
|
2021-05-02 16:01:30 -04:00
|
|
|
controller.JoyAssignmentMode = NpadJoyAssignmentMode.Dual;
|
|
|
|
controller.FullKeyColor.FullKeyBody = (uint)NpadColor.BodyGray;
|
|
|
|
controller.FullKeyColor.FullKeyButtons = (uint)NpadColor.ButtonGray;
|
|
|
|
controller.JoyColor.LeftBody = (uint)NpadColor.BodyNeonBlue;
|
|
|
|
controller.JoyColor.LeftButtons = (uint)NpadColor.ButtonGray;
|
|
|
|
controller.JoyColor.RightBody = (uint)NpadColor.BodyNeonRed;
|
|
|
|
controller.JoyColor.RightButtons = (uint)NpadColor.ButtonGray;
|
|
|
|
|
|
|
|
controller.SystemProperties = NpadSystemProperties.IsPoweredJoyDual |
|
|
|
|
NpadSystemProperties.IsPoweredJoyLeft |
|
|
|
|
NpadSystemProperties.IsPoweredJoyRight;
|
|
|
|
|
|
|
|
controller.BatteryLevelJoyDual = NpadBatteryLevel.Percent100;
|
|
|
|
controller.BatteryLevelJoyLeft = NpadBatteryLevel.Percent100;
|
|
|
|
controller.BatteryLevelJoyRight = NpadBatteryLevel.Percent100;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ControllerType.ProController:
|
2021-06-23 16:52:55 -04:00
|
|
|
controller.StyleSet = NpadStyleTag.FullKey;
|
|
|
|
controller.DeviceType = DeviceType.FullKey;
|
|
|
|
controller.SystemProperties |= NpadSystemProperties.IsAbxyButtonOriented |
|
|
|
|
NpadSystemProperties.IsPlusAvailable |
|
|
|
|
NpadSystemProperties.IsMinusAvailable;
|
|
|
|
controller.AppletFooterUiType = AppletFooterUiType.SwitchProController;
|
2020-04-02 20:10:02 -04:00
|
|
|
break;
|
|
|
|
case ControllerType.Handheld:
|
2021-06-23 16:52:55 -04:00
|
|
|
controller.StyleSet = NpadStyleTag.Handheld;
|
|
|
|
controller.DeviceType = DeviceType.HandheldLeft |
|
|
|
|
DeviceType.HandheldRight;
|
|
|
|
controller.SystemProperties |= NpadSystemProperties.IsAbxyButtonOriented |
|
|
|
|
NpadSystemProperties.IsPlusAvailable |
|
|
|
|
NpadSystemProperties.IsMinusAvailable;
|
|
|
|
controller.AppletFooterUiType = AppletFooterUiType.HandheldJoyConLeftJoyConRight;
|
2020-04-02 20:10:02 -04:00
|
|
|
break;
|
|
|
|
case ControllerType.JoyconPair:
|
2021-06-23 16:52:55 -04:00
|
|
|
controller.StyleSet = NpadStyleTag.JoyDual;
|
|
|
|
controller.DeviceType = DeviceType.JoyLeft |
|
|
|
|
DeviceType.JoyRight;
|
|
|
|
controller.SystemProperties |= NpadSystemProperties.IsAbxyButtonOriented |
|
|
|
|
NpadSystemProperties.IsPlusAvailable |
|
|
|
|
NpadSystemProperties.IsMinusAvailable;
|
|
|
|
controller.AppletFooterUiType = _device.System.State.DockedMode ? AppletFooterUiType.JoyDual : AppletFooterUiType.HandheldJoyConLeftJoyConRight;
|
2020-04-02 20:10:02 -04:00
|
|
|
break;
|
|
|
|
case ControllerType.JoyconLeft:
|
2021-06-23 16:52:55 -04:00
|
|
|
controller.StyleSet = NpadStyleTag.JoyLeft;
|
|
|
|
controller.JoyAssignmentMode = NpadJoyAssignmentMode.Single;
|
|
|
|
controller.DeviceType = DeviceType.JoyLeft;
|
|
|
|
controller.SystemProperties |= NpadSystemProperties.IsSlSrButtonOriented |
|
|
|
|
NpadSystemProperties.IsMinusAvailable;
|
|
|
|
controller.AppletFooterUiType = _device.System.State.DockedMode ? AppletFooterUiType.JoyDualLeftOnly : AppletFooterUiType.HandheldJoyConLeftOnly;
|
2020-04-02 20:10:02 -04:00
|
|
|
break;
|
|
|
|
case ControllerType.JoyconRight:
|
2021-06-23 16:52:55 -04:00
|
|
|
controller.StyleSet = NpadStyleTag.JoyRight;
|
|
|
|
controller.JoyAssignmentMode = NpadJoyAssignmentMode.Single;
|
|
|
|
controller.DeviceType = DeviceType.JoyRight;
|
|
|
|
controller.SystemProperties |= NpadSystemProperties.IsSlSrButtonOriented |
|
|
|
|
NpadSystemProperties.IsPlusAvailable;
|
|
|
|
controller.AppletFooterUiType = _device.System.State.DockedMode ? AppletFooterUiType.JoyDualRightOnly : AppletFooterUiType.HandheldJoyConRightOnly;
|
2020-04-02 20:10:02 -04:00
|
|
|
break;
|
|
|
|
case ControllerType.Pokeball:
|
2021-06-23 16:52:55 -04:00
|
|
|
controller.StyleSet = NpadStyleTag.Palma;
|
|
|
|
controller.DeviceType = DeviceType.Palma;
|
|
|
|
controller.AppletFooterUiType = AppletFooterUiType.None;
|
2020-04-02 20:10:02 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_styleSetUpdateEvents[(int)player].ReadableEvent.Signal();
|
2020-08-23 16:54:11 -04:00
|
|
|
_activeCount++;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
Logger.Info?.Print(LogClass.Hid, $"Connected Controller {type} to {player}");
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
private ref RingLifo<NpadCommonState> GetCommonStateLifo(ref NpadInternalState npad)
|
|
|
|
{
|
|
|
|
switch (npad.StyleSet)
|
|
|
|
{
|
|
|
|
case NpadStyleTag.FullKey:
|
|
|
|
return ref npad.FullKey;
|
|
|
|
case NpadStyleTag.Handheld:
|
|
|
|
return ref npad.Handheld;
|
|
|
|
case NpadStyleTag.JoyDual:
|
|
|
|
return ref npad.JoyDual;
|
|
|
|
case NpadStyleTag.JoyLeft:
|
|
|
|
return ref npad.JoyLeft;
|
|
|
|
case NpadStyleTag.JoyRight:
|
|
|
|
return ref npad.JoyRight;
|
|
|
|
case NpadStyleTag.Palma:
|
|
|
|
return ref npad.Palma;
|
|
|
|
default:
|
|
|
|
return ref npad.SystemExt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateUnusedInputIfNotEqual(ref RingLifo<NpadCommonState> currentlyUsed, ref RingLifo<NpadCommonState> possiblyUnused)
|
|
|
|
{
|
2021-11-01 18:38:13 -04:00
|
|
|
if (!Unsafe.AreSame(ref currentlyUsed, ref possiblyUnused))
|
2021-05-02 16:01:30 -04:00
|
|
|
{
|
|
|
|
NpadCommonState newState = new NpadCommonState();
|
|
|
|
|
|
|
|
WriteNewInputEntry(ref possiblyUnused, ref newState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteNewInputEntry(ref RingLifo<NpadCommonState> lifo, ref NpadCommonState state)
|
|
|
|
{
|
|
|
|
ref NpadCommonState previousEntry = ref lifo.GetCurrentEntryRef();
|
|
|
|
|
|
|
|
state.SamplingNumber = previousEntry.SamplingNumber + 1;
|
|
|
|
|
|
|
|
lifo.Write(ref state);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateUnusedSixInputIfNotEqual(ref RingLifo<SixAxisSensorState> currentlyUsed, ref RingLifo<SixAxisSensorState> possiblyUnused)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2021-11-01 18:38:13 -04:00
|
|
|
if (!Unsafe.AreSame(ref currentlyUsed, ref possiblyUnused))
|
2021-05-02 16:01:30 -04:00
|
|
|
{
|
|
|
|
SixAxisSensorState newState = new SixAxisSensorState();
|
|
|
|
|
|
|
|
WriteNewSixInputEntry(ref possiblyUnused, ref newState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteNewSixInputEntry(ref RingLifo<SixAxisSensorState> lifo, ref SixAxisSensorState state)
|
|
|
|
{
|
|
|
|
ref SixAxisSensorState previousEntry = ref lifo.GetCurrentEntryRef();
|
|
|
|
|
|
|
|
state.SamplingNumber = previousEntry.SamplingNumber + 1;
|
|
|
|
|
|
|
|
lifo.Write(ref state);
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-08-23 16:54:11 -04:00
|
|
|
private void UpdateInput(GamepadInput state)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2020-08-23 16:54:11 -04:00
|
|
|
if (state.PlayerId == PlayerIndex.Unknown)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
ref NpadInternalState currentNpad = ref _device.Hid.SharedMemory.Npads[(int)state.PlayerId].InternalState;
|
2020-08-23 16:54:11 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
if (currentNpad.StyleSet == NpadStyleTag.None)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
ref RingLifo<NpadCommonState> lifo = ref GetCommonStateLifo(ref currentNpad);
|
|
|
|
|
|
|
|
NpadCommonState newState = new NpadCommonState
|
|
|
|
{
|
|
|
|
Buttons = (NpadButton)state.Buttons,
|
|
|
|
AnalogStickL = new AnalogStickState
|
|
|
|
{
|
|
|
|
X = state.LStick.Dx,
|
|
|
|
Y = state.LStick.Dy,
|
|
|
|
},
|
|
|
|
AnalogStickR = new AnalogStickState
|
|
|
|
{
|
|
|
|
X = state.RStick.Dx,
|
|
|
|
Y = state.RStick.Dy,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
newState.Attributes = NpadAttribute.IsConnected;
|
|
|
|
|
|
|
|
switch (currentNpad.StyleSet)
|
|
|
|
{
|
|
|
|
case NpadStyleTag.Handheld:
|
|
|
|
case NpadStyleTag.FullKey:
|
|
|
|
newState.Attributes |= NpadAttribute.IsWired;
|
|
|
|
break;
|
|
|
|
case NpadStyleTag.JoyDual:
|
|
|
|
newState.Attributes |= NpadAttribute.IsLeftConnected |
|
|
|
|
NpadAttribute.IsRightConnected;
|
|
|
|
break;
|
|
|
|
case NpadStyleTag.JoyLeft:
|
|
|
|
newState.Attributes |= NpadAttribute.IsLeftConnected;
|
|
|
|
break;
|
|
|
|
case NpadStyleTag.JoyRight:
|
|
|
|
newState.Attributes |= NpadAttribute.IsRightConnected;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
WriteNewInputEntry(ref lifo, ref newState);
|
2020-04-02 20:10:02 -04:00
|
|
|
|
|
|
|
// Mirror data to Default layout just in case
|
2021-05-02 16:01:30 -04:00
|
|
|
if (!currentNpad.StyleSet.HasFlag(NpadStyleTag.SystemExt))
|
|
|
|
{
|
|
|
|
WriteNewInputEntry(ref currentNpad.SystemExt, ref newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateUnusedInputIfNotEqual(ref lifo, ref currentNpad.FullKey);
|
|
|
|
UpdateUnusedInputIfNotEqual(ref lifo, ref currentNpad.Handheld);
|
|
|
|
UpdateUnusedInputIfNotEqual(ref lifo, ref currentNpad.JoyDual);
|
|
|
|
UpdateUnusedInputIfNotEqual(ref lifo, ref currentNpad.JoyLeft);
|
|
|
|
UpdateUnusedInputIfNotEqual(ref lifo, ref currentNpad.JoyRight);
|
|
|
|
UpdateUnusedInputIfNotEqual(ref lifo, ref currentNpad.Palma);
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
private void UpdateDisconnectedInput(PlayerIndex index)
|
2020-09-29 17:32:42 -04:00
|
|
|
{
|
2021-05-02 16:01:30 -04:00
|
|
|
ref NpadInternalState currentNpad = ref _device.Hid.SharedMemory.Npads[(int)index].InternalState;
|
|
|
|
|
|
|
|
NpadCommonState newState = new NpadCommonState();
|
|
|
|
|
|
|
|
WriteNewInputEntry(ref currentNpad.FullKey, ref newState);
|
|
|
|
WriteNewInputEntry(ref currentNpad.Handheld, ref newState);
|
|
|
|
WriteNewInputEntry(ref currentNpad.JoyDual, ref newState);
|
|
|
|
WriteNewInputEntry(ref currentNpad.JoyLeft, ref newState);
|
|
|
|
WriteNewInputEntry(ref currentNpad.JoyRight, ref newState);
|
|
|
|
WriteNewInputEntry(ref currentNpad.Palma, ref newState);
|
|
|
|
}
|
2020-09-29 17:32:42 -04:00
|
|
|
|
|
|
|
public void UpdateSixAxis(IList<SixAxisInput> states)
|
|
|
|
{
|
2021-05-02 16:01:30 -04:00
|
|
|
Span<bool> updated = stackalloc bool[10];
|
|
|
|
|
2020-09-29 17:32:42 -04:00
|
|
|
for (int i = 0; i < states.Count; ++i)
|
|
|
|
{
|
2021-05-02 16:01:30 -04:00
|
|
|
updated[(int)states[i].PlayerId] = true;
|
|
|
|
|
2020-09-29 17:32:42 -04:00
|
|
|
if (SetSixAxisState(states[i]))
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
|
2020-10-28 15:52:07 -04:00
|
|
|
if (i >= states.Count)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-29 17:32:42 -04:00
|
|
|
SetSixAxisState(states[i], true);
|
|
|
|
}
|
|
|
|
}
|
2021-05-02 16:01:30 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < updated.Length; i++)
|
|
|
|
{
|
|
|
|
if (!updated[i])
|
|
|
|
{
|
|
|
|
UpdateDisconnectedInputSixAxis((PlayerIndex)i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ref RingLifo<SixAxisSensorState> GetSixAxisSensorLifo(ref NpadInternalState npad, bool isRightPair)
|
|
|
|
{
|
|
|
|
switch (npad.StyleSet)
|
|
|
|
{
|
|
|
|
case NpadStyleTag.FullKey:
|
|
|
|
return ref npad.FullKeySixAxisSensor;
|
|
|
|
case NpadStyleTag.Handheld:
|
|
|
|
return ref npad.HandheldSixAxisSensor;
|
|
|
|
case NpadStyleTag.JoyDual:
|
|
|
|
if (isRightPair)
|
|
|
|
{
|
|
|
|
return ref npad.JoyDualRightSixAxisSensor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ref npad.JoyDualSixAxisSensor;
|
|
|
|
}
|
|
|
|
case NpadStyleTag.JoyLeft:
|
|
|
|
return ref npad.JoyLeftSixAxisSensor;
|
|
|
|
case NpadStyleTag.JoyRight:
|
|
|
|
return ref npad.JoyRightSixAxisSensor;
|
|
|
|
default:
|
|
|
|
throw new NotImplementedException($"{npad.StyleSet}");
|
|
|
|
}
|
2020-09-29 17:32:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool SetSixAxisState(SixAxisInput state, bool isRightPair = false)
|
|
|
|
{
|
|
|
|
if (state.PlayerId == PlayerIndex.Unknown)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
ref NpadInternalState currentNpad = ref _device.Hid.SharedMemory.Npads[(int)state.PlayerId].InternalState;
|
2020-09-29 17:32:42 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
if (currentNpad.StyleSet == NpadStyleTag.None)
|
2020-09-29 17:32:42 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
HidVector accel = new HidVector()
|
|
|
|
{
|
|
|
|
X = state.Accelerometer.X,
|
|
|
|
Y = state.Accelerometer.Y,
|
|
|
|
Z = state.Accelerometer.Z
|
|
|
|
};
|
|
|
|
|
|
|
|
HidVector gyro = new HidVector()
|
|
|
|
{
|
|
|
|
X = state.Gyroscope.X,
|
|
|
|
Y = state.Gyroscope.Y,
|
|
|
|
Z = state.Gyroscope.Z
|
|
|
|
};
|
|
|
|
|
|
|
|
HidVector rotation = new HidVector()
|
|
|
|
{
|
|
|
|
X = state.Rotation.X,
|
|
|
|
Y = state.Rotation.Y,
|
|
|
|
Z = state.Rotation.Z
|
|
|
|
};
|
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
SixAxisSensorState newState = new SixAxisSensorState
|
|
|
|
{
|
|
|
|
Acceleration = accel,
|
|
|
|
AngularVelocity = gyro,
|
|
|
|
Angle = rotation,
|
|
|
|
Attributes = SixAxisSensorAttribute.IsConnected
|
|
|
|
};
|
2020-09-29 17:32:42 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
state.Orientation.AsSpan().CopyTo(newState.Direction.ToSpan());
|
2020-09-29 17:32:42 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
ref RingLifo<SixAxisSensorState> lifo = ref GetSixAxisSensorLifo(ref currentNpad, isRightPair);
|
2020-09-29 17:32:42 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
WriteNewSixInputEntry(ref lifo, ref newState);
|
2020-09-29 17:32:42 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
bool needUpdateRight = currentNpad.StyleSet == NpadStyleTag.JoyDual && !isRightPair;
|
|
|
|
|
|
|
|
if (!isRightPair)
|
2020-09-29 17:32:42 -04:00
|
|
|
{
|
2021-05-02 16:01:30 -04:00
|
|
|
UpdateUnusedSixInputIfNotEqual(ref lifo, ref currentNpad.FullKeySixAxisSensor);
|
|
|
|
UpdateUnusedSixInputIfNotEqual(ref lifo, ref currentNpad.HandheldSixAxisSensor);
|
|
|
|
UpdateUnusedSixInputIfNotEqual(ref lifo, ref currentNpad.JoyDualSixAxisSensor);
|
|
|
|
UpdateUnusedSixInputIfNotEqual(ref lifo, ref currentNpad.JoyLeftSixAxisSensor);
|
|
|
|
UpdateUnusedSixInputIfNotEqual(ref lifo, ref currentNpad.JoyRightSixAxisSensor);
|
2020-09-29 17:32:42 -04:00
|
|
|
}
|
|
|
|
|
2021-07-23 19:01:36 -04:00
|
|
|
if (!needUpdateRight && !isRightPair)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
2021-05-02 16:01:30 -04:00
|
|
|
SixAxisSensorState emptyState = new SixAxisSensorState();
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
emptyState.Attributes = SixAxisSensorAttribute.IsConnected;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
WriteNewSixInputEntry(ref currentNpad.JoyDualRightSixAxisSensor, ref emptyState);
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
return needUpdateRight;
|
|
|
|
}
|
2020-09-29 17:32:42 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
private void UpdateDisconnectedInputSixAxis(PlayerIndex index)
|
|
|
|
{
|
|
|
|
ref NpadInternalState currentNpad = ref _device.Hid.SharedMemory.Npads[(int)index].InternalState;
|
2020-09-29 17:32:42 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
SixAxisSensorState newState = new SixAxisSensorState();
|
2020-09-29 17:32:42 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
newState.Attributes = SixAxisSensorAttribute.IsConnected;
|
2020-09-29 17:32:42 -04:00
|
|
|
|
2021-05-02 16:01:30 -04:00
|
|
|
WriteNewSixInputEntry(ref currentNpad.FullKeySixAxisSensor, ref newState);
|
|
|
|
WriteNewSixInputEntry(ref currentNpad.HandheldSixAxisSensor, ref newState);
|
|
|
|
WriteNewSixInputEntry(ref currentNpad.JoyDualSixAxisSensor, ref newState);
|
|
|
|
WriteNewSixInputEntry(ref currentNpad.JoyDualRightSixAxisSensor, ref newState);
|
|
|
|
WriteNewSixInputEntry(ref currentNpad.JoyLeftSixAxisSensor, ref newState);
|
|
|
|
WriteNewSixInputEntry(ref currentNpad.JoyRightSixAxisSensor, ref newState);
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
2021-08-04 18:39:40 -04:00
|
|
|
|
|
|
|
public void UpdateRumbleQueue(PlayerIndex index, Dictionary<byte, HidVibrationValue> dualVibrationValues)
|
|
|
|
{
|
|
|
|
if (RumbleQueues.TryGetValue(index, out ConcurrentQueue<(HidVibrationValue, HidVibrationValue)> currentQueue))
|
|
|
|
{
|
|
|
|
if (!dualVibrationValues.TryGetValue(0, out HidVibrationValue leftVibrationValue))
|
|
|
|
{
|
|
|
|
leftVibrationValue = _neutralVibrationValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dualVibrationValues.TryGetValue(1, out HidVibrationValue rightVibrationValue))
|
|
|
|
{
|
|
|
|
rightVibrationValue = _neutralVibrationValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!LastVibrationValues.TryGetValue(index, out (HidVibrationValue, HidVibrationValue) dualVibrationValue) || !leftVibrationValue.Equals(dualVibrationValue.Item1) || !rightVibrationValue.Equals(dualVibrationValue.Item2))
|
|
|
|
{
|
|
|
|
currentQueue.Enqueue((leftVibrationValue, rightVibrationValue));
|
|
|
|
|
|
|
|
LastVibrationValues[index] = (leftVibrationValue, rightVibrationValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public HidVibrationValue GetLastVibrationValue(PlayerIndex index, byte position)
|
|
|
|
{
|
|
|
|
if (!LastVibrationValues.TryGetValue(index, out (HidVibrationValue, HidVibrationValue) dualVibrationValue))
|
|
|
|
{
|
|
|
|
return _neutralVibrationValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (position == 0) ? dualVibrationValue.Item1 : dualVibrationValue.Item2;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ConcurrentQueue<(HidVibrationValue, HidVibrationValue)> GetRumbleQueue(PlayerIndex index)
|
|
|
|
{
|
|
|
|
if (!RumbleQueues.TryGetValue(index, out ConcurrentQueue<(HidVibrationValue, HidVibrationValue)> rumbleQueue))
|
|
|
|
{
|
|
|
|
rumbleQueue = new ConcurrentQueue<(HidVibrationValue, HidVibrationValue)>();
|
|
|
|
_device.Hid.Npads.RumbleQueues[index] = rumbleQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rumbleQueue;
|
|
|
|
}
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
}
|