2020-09-29 23:32:42 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-08-23 22:54:11 +02:00
|
|
|
using Ryujinx.Common;
|
2020-04-03 02:10:02 +02:00
|
|
|
using Ryujinx.Common.Logging;
|
2020-08-23 22:54:11 +02:00
|
|
|
using Ryujinx.Common.Memory;
|
2020-05-03 04:00:53 +02:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2020-04-03 02:10:02 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Hid
|
|
|
|
{
|
|
|
|
public class NpadDevices : BaseDevice
|
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
private const BatteryCharge DefaultBatteryCharge = BatteryCharge.Percent100;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
internal NpadJoyHoldType JoyHold { get; set; }
|
2020-04-03 02:10:02 +02:00
|
|
|
internal bool SixAxisActive = false; // TODO: link to hidserver when implemented
|
2020-08-23 22:54:11 +02:00
|
|
|
internal ControllerType SupportedStyleSets { get; set; }
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
public NpadDevices(Switch device, bool active = true) : base(device, active)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02: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-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
internal ref KEvent GetStyleSetUpdateEvent(PlayerIndex player)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
return ref _styleSetUpdateEvents[(int)player];
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
internal void ClearSupportedPlayers()
|
|
|
|
{
|
|
|
|
_supportedPlayers.AsSpan().Clear();
|
|
|
|
}
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
internal void SetSupportedPlayer(PlayerIndex player, bool supported = true)
|
|
|
|
{
|
|
|
|
_supportedPlayers[(int)player] = supported;
|
|
|
|
}
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
internal IEnumerable<PlayerIndex> GetSupportedPlayers()
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
for (int i = 0; i < _supportedPlayers.Length; ++i)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
if (_supportedPlayers[i])
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
yield return (PlayerIndex)i;
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
public bool Validate(int playerMin, int playerMax, ControllerType acceptedTypes, out int configuredCount, out PlayerIndex primaryIndex)
|
|
|
|
{
|
|
|
|
primaryIndex = PlayerIndex.Unknown;
|
|
|
|
configuredCount = 0;
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
for (int i = 0; i < MaxControllers; ++i)
|
|
|
|
{
|
|
|
|
ControllerType npad = _configuredTypes[i];
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
if (npad == ControllerType.Handheld && _device.System.State.DockedMode)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
ControllerType currentType = _device.Hid.SharedMemory.Npads[i].Header.Type;
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
if (currentType != ControllerType.None && (npad & acceptedTypes) != 0 && _supportedPlayers[i])
|
|
|
|
{
|
|
|
|
configuredCount++;
|
|
|
|
if (primaryIndex == PlayerIndex.Unknown)
|
|
|
|
{
|
|
|
|
primaryIndex = (PlayerIndex)i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
if (configuredCount < playerMin || configuredCount > playerMax || primaryIndex == PlayerIndex.Unknown)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
return false;
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
return true;
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
public void Configure(params ControllerConfig[] configs)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
_configuredTypes = new ControllerType[MaxControllers];
|
|
|
|
|
2020-04-03 02:10:02 +02:00
|
|
|
for (int i = 0; i < configs.Length; ++i)
|
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
PlayerIndex player = configs[i].Player;
|
2020-04-03 02:10:02 +02: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 22:54:11 +02:00
|
|
|
_configuredTypes[(int)player] = controllerType;
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
Logger.Info?.Print(LogClass.Hid, $"Configured Controller {controllerType} to {player}");
|
|
|
|
}
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
public void Update(IList<GamepadInput> states)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
Remap();
|
|
|
|
|
|
|
|
UpdateAllEntries();
|
|
|
|
|
|
|
|
// Update configured inputs
|
|
|
|
for (int i = 0; i < states.Count; ++i)
|
|
|
|
{
|
|
|
|
UpdateInput(states[i]);
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
private void Remap()
|
|
|
|
{
|
|
|
|
// Remap/Init if necessary
|
|
|
|
for (int i = 0; i < MaxControllers; ++i)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
ControllerType config = _configuredTypes[i];
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
// Remove Handheld config when Docked
|
|
|
|
if (config == ControllerType.Handheld && _device.System.State.DockedMode)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
config = ControllerType.None;
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
// Auto-remap ProController and JoyconPair
|
|
|
|
if (config == ControllerType.JoyconPair && (SupportedStyleSets & ControllerType.JoyconPair) == 0 && (SupportedStyleSets & ControllerType.ProController) != 0)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
config = ControllerType.ProController;
|
|
|
|
}
|
|
|
|
else if (config == ControllerType.ProController && (SupportedStyleSets & ControllerType.ProController) == 0 && (SupportedStyleSets & ControllerType.JoyconPair) != 0)
|
|
|
|
{
|
|
|
|
config = ControllerType.JoyconPair;
|
|
|
|
}
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
// Check StyleSet and PlayerSet
|
|
|
|
if ((config & SupportedStyleSets) == 0 || !_supportedPlayers[i])
|
|
|
|
{
|
|
|
|
config = ControllerType.None;
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
SetupNpad((PlayerIndex)i, config);
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
if (_activeCount == 0 && PerformanceCounter.ElapsedMilliseconds > _lastNotifyTimestamp + NoMatchNotifyFrequencyMs)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
Logger.Warning?.Print(LogClass.Hid, $"No matching controllers found. Application requests '{SupportedStyleSets}' on '{string.Join(", ", GetSupportedPlayers())}'");
|
|
|
|
_lastNotifyTimestamp = PerformanceCounter.ElapsedMilliseconds;
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
private void SetupNpad(PlayerIndex player, ControllerType type)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
ref ShMemNpad controller = ref _device.Hid.SharedMemory.Npads[(int)player];
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
ControllerType oldType = controller.Header.Type;
|
|
|
|
|
|
|
|
if (oldType == type)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
return; // Already configured
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
controller = new ShMemNpad(); // Zero it
|
|
|
|
|
2020-08-23 22:54:11 +02: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-03 02:10:02 +02:00
|
|
|
// TODO: Allow customizing colors at config
|
|
|
|
NpadStateHeader defaultHeader = new NpadStateHeader
|
|
|
|
{
|
2020-05-03 04:00:53 +02:00
|
|
|
IsHalf = false,
|
|
|
|
SingleColorBody = NpadColor.BodyGray,
|
2020-04-03 02:10:02 +02:00
|
|
|
SingleColorButtons = NpadColor.ButtonGray,
|
2020-05-03 04:00:53 +02:00
|
|
|
LeftColorBody = NpadColor.BodyNeonBlue,
|
|
|
|
LeftColorButtons = NpadColor.ButtonGray,
|
|
|
|
RightColorBody = NpadColor.BodyNeonRed,
|
|
|
|
RightColorButtons = NpadColor.ButtonGray
|
2020-04-03 02:10:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
controller.SystemProperties = NpadSystemProperties.PowerInfo0Connected |
|
|
|
|
NpadSystemProperties.PowerInfo1Connected |
|
|
|
|
NpadSystemProperties.PowerInfo2Connected;
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
controller.BatteryState.ToSpan().Fill(DefaultBatteryCharge);
|
2020-04-03 02:10:02 +02:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ControllerType.ProController:
|
2020-05-03 04:00:53 +02:00
|
|
|
defaultHeader.Type = ControllerType.ProController;
|
|
|
|
controller.DeviceType = DeviceType.FullKey;
|
2020-04-03 02:10:02 +02:00
|
|
|
controller.SystemProperties |= NpadSystemProperties.AbxyButtonOriented |
|
|
|
|
NpadSystemProperties.PlusButtonCapability |
|
|
|
|
NpadSystemProperties.MinusButtonCapability;
|
|
|
|
break;
|
|
|
|
case ControllerType.Handheld:
|
2020-05-03 04:00:53 +02:00
|
|
|
defaultHeader.Type = ControllerType.Handheld;
|
|
|
|
controller.DeviceType = DeviceType.HandheldLeft |
|
2020-04-03 02:10:02 +02:00
|
|
|
DeviceType.HandheldRight;
|
|
|
|
controller.SystemProperties |= NpadSystemProperties.AbxyButtonOriented |
|
|
|
|
NpadSystemProperties.PlusButtonCapability |
|
|
|
|
NpadSystemProperties.MinusButtonCapability;
|
|
|
|
break;
|
|
|
|
case ControllerType.JoyconPair:
|
2020-05-03 04:00:53 +02:00
|
|
|
defaultHeader.Type = ControllerType.JoyconPair;
|
|
|
|
controller.DeviceType = DeviceType.JoyLeft |
|
2020-04-03 02:10:02 +02:00
|
|
|
DeviceType.JoyRight;
|
|
|
|
controller.SystemProperties |= NpadSystemProperties.AbxyButtonOriented |
|
|
|
|
NpadSystemProperties.PlusButtonCapability |
|
|
|
|
NpadSystemProperties.MinusButtonCapability;
|
|
|
|
break;
|
|
|
|
case ControllerType.JoyconLeft:
|
2020-05-03 04:00:53 +02:00
|
|
|
defaultHeader.Type = ControllerType.JoyconLeft;
|
|
|
|
defaultHeader.IsHalf = true;
|
|
|
|
controller.DeviceType = DeviceType.JoyLeft;
|
2020-04-03 02:10:02 +02:00
|
|
|
controller.SystemProperties |= NpadSystemProperties.SlSrButtonOriented |
|
|
|
|
NpadSystemProperties.MinusButtonCapability;
|
|
|
|
break;
|
|
|
|
case ControllerType.JoyconRight:
|
2020-05-03 04:00:53 +02:00
|
|
|
defaultHeader.Type = ControllerType.JoyconRight;
|
|
|
|
defaultHeader.IsHalf = true;
|
|
|
|
controller.DeviceType = DeviceType.JoyRight;
|
2020-04-03 02:10:02 +02:00
|
|
|
controller.SystemProperties |= NpadSystemProperties.SlSrButtonOriented |
|
|
|
|
NpadSystemProperties.PlusButtonCapability;
|
|
|
|
break;
|
|
|
|
case ControllerType.Pokeball:
|
2020-05-03 04:00:53 +02:00
|
|
|
defaultHeader.Type = ControllerType.Pokeball;
|
2020-04-03 02:10:02 +02:00
|
|
|
controller.DeviceType = DeviceType.Palma;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller.Header = defaultHeader;
|
|
|
|
|
|
|
|
_styleSetUpdateEvents[(int)player].ReadableEvent.Signal();
|
2020-08-23 22:54:11 +02:00
|
|
|
_activeCount++;
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
Logger.Info?.Print(LogClass.Hid, $"Connected Controller {type} to {player}");
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
private static NpadLayoutsIndex ControllerTypeToNpadLayout(ControllerType controllerType)
|
2020-04-03 02:10:02 +02:00
|
|
|
=> controllerType switch
|
|
|
|
{
|
|
|
|
ControllerType.ProController => NpadLayoutsIndex.ProController,
|
2020-05-03 04:00:53 +02:00
|
|
|
ControllerType.Handheld => NpadLayoutsIndex.Handheld,
|
|
|
|
ControllerType.JoyconPair => NpadLayoutsIndex.JoyDual,
|
|
|
|
ControllerType.JoyconLeft => NpadLayoutsIndex.JoyLeft,
|
|
|
|
ControllerType.JoyconRight => NpadLayoutsIndex.JoyRight,
|
|
|
|
ControllerType.Pokeball => NpadLayoutsIndex.Pokeball,
|
|
|
|
_ => NpadLayoutsIndex.SystemExternal
|
2020-04-03 02:10:02 +02:00
|
|
|
};
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
private void UpdateInput(GamepadInput state)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
2020-08-23 22:54:11 +02:00
|
|
|
if (state.PlayerId == PlayerIndex.Unknown)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
ref ShMemNpad currentNpad = ref _device.Hid.SharedMemory.Npads[(int)state.PlayerId];
|
|
|
|
|
|
|
|
if (currentNpad.Header.Type == ControllerType.None)
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
ref NpadLayout currentLayout = ref currentNpad.Layouts[(int)ControllerTypeToNpadLayout(currentNpad.Header.Type)];
|
2020-05-03 04:00:53 +02:00
|
|
|
ref NpadState currentEntry = ref currentLayout.Entries[(int)currentLayout.Header.LatestEntry];
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-08-23 22:54:11 +02:00
|
|
|
currentEntry.Buttons = state.Buttons;
|
|
|
|
currentEntry.LStickX = state.LStick.Dx;
|
|
|
|
currentEntry.LStickY = state.LStick.Dy;
|
|
|
|
currentEntry.RStickX = state.RStick.Dx;
|
|
|
|
currentEntry.RStickY = state.RStick.Dy;
|
2020-04-03 02:10:02 +02:00
|
|
|
|
|
|
|
// Mirror data to Default layout just in case
|
|
|
|
ref NpadLayout mainLayout = ref currentNpad.Layouts[(int)NpadLayoutsIndex.SystemExternal];
|
|
|
|
mainLayout.Entries[(int)mainLayout.Header.LatestEntry] = currentEntry;
|
|
|
|
}
|
|
|
|
|
2020-09-29 23:32:42 +02:00
|
|
|
private static SixAxixLayoutsIndex ControllerTypeToSixAxisLayout(ControllerType controllerType)
|
|
|
|
=> controllerType switch
|
|
|
|
{
|
|
|
|
ControllerType.ProController => SixAxixLayoutsIndex.ProController,
|
|
|
|
ControllerType.Handheld => SixAxixLayoutsIndex.Handheld,
|
|
|
|
ControllerType.JoyconPair => SixAxixLayoutsIndex.JoyDualLeft,
|
|
|
|
ControllerType.JoyconLeft => SixAxixLayoutsIndex.JoyLeft,
|
|
|
|
ControllerType.JoyconRight => SixAxixLayoutsIndex.JoyRight,
|
|
|
|
ControllerType.Pokeball => SixAxixLayoutsIndex.Pokeball,
|
|
|
|
_ => SixAxixLayoutsIndex.SystemExternal
|
|
|
|
};
|
|
|
|
|
|
|
|
public void UpdateSixAxis(IList<SixAxisInput> states)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < states.Count; ++i)
|
|
|
|
{
|
|
|
|
if (SetSixAxisState(states[i]))
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
|
2020-10-28 20:52:07 +01:00
|
|
|
if (i >= states.Count)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-29 23:32:42 +02:00
|
|
|
SetSixAxisState(states[i], true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool SetSixAxisState(SixAxisInput state, bool isRightPair = false)
|
|
|
|
{
|
|
|
|
if (state.PlayerId == PlayerIndex.Unknown)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref ShMemNpad currentNpad = ref _device.Hid.SharedMemory.Npads[(int)state.PlayerId];
|
|
|
|
|
|
|
|
if (currentNpad.Header.Type == ControllerType.None)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
ref NpadSixAxis currentLayout = ref currentNpad.Sixaxis[(int)ControllerTypeToSixAxisLayout(currentNpad.Header.Type) + (isRightPair ? 1 : 0)];
|
|
|
|
ref SixAxisState currentEntry = ref currentLayout.Entries[(int)currentLayout.Header.LatestEntry];
|
|
|
|
|
|
|
|
int previousEntryIndex = (int)(currentLayout.Header.LatestEntry == 0 ?
|
|
|
|
currentLayout.Header.MaxEntryIndex : currentLayout.Header.LatestEntry - 1);
|
|
|
|
|
|
|
|
ref SixAxisState previousEntry = ref currentLayout.Entries[previousEntryIndex];
|
|
|
|
|
|
|
|
currentEntry.Accelerometer = accel;
|
|
|
|
currentEntry.Gyroscope = gyro;
|
|
|
|
currentEntry.Rotations = rotation;
|
|
|
|
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 9; i++)
|
|
|
|
{
|
|
|
|
currentEntry.Orientation[i] = state.Orientation[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentNpad.Header.Type == ControllerType.JoyconPair && !isRightPair;
|
|
|
|
}
|
|
|
|
|
2020-05-03 04:00:53 +02:00
|
|
|
private void UpdateAllEntries()
|
2020-04-03 02:10:02 +02:00
|
|
|
{
|
|
|
|
ref Array10<ShMemNpad> controllers = ref _device.Hid.SharedMemory.Npads;
|
|
|
|
for (int i = 0; i < controllers.Length; ++i)
|
|
|
|
{
|
|
|
|
ref Array7<NpadLayout> layouts = ref controllers[i].Layouts;
|
|
|
|
for (int l = 0; l < layouts.Length; ++l)
|
|
|
|
{
|
|
|
|
ref NpadLayout currentLayout = ref layouts[l];
|
|
|
|
int currentIndex = UpdateEntriesHeader(ref currentLayout.Header, out int previousIndex);
|
|
|
|
|
|
|
|
ref NpadState currentEntry = ref currentLayout.Entries[currentIndex];
|
2020-05-03 04:00:53 +02:00
|
|
|
NpadState previousEntry = currentLayout.Entries[previousIndex];
|
2020-04-03 02:10:02 +02:00
|
|
|
|
2020-05-03 04:00:53 +02:00
|
|
|
currentEntry.SampleTimestamp = previousEntry.SampleTimestamp + 1;
|
2020-04-03 02:10:02 +02:00
|
|
|
currentEntry.SampleTimestamp2 = previousEntry.SampleTimestamp2 + 1;
|
|
|
|
|
|
|
|
if (controllers[i].Header.Type == ControllerType.None)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentEntry.ConnectionState = NpadConnectionState.ControllerStateConnected;
|
|
|
|
|
|
|
|
switch (controllers[i].Header.Type)
|
|
|
|
{
|
|
|
|
case ControllerType.Handheld:
|
|
|
|
case ControllerType.ProController:
|
|
|
|
currentEntry.ConnectionState |= NpadConnectionState.ControllerStateWired;
|
|
|
|
break;
|
|
|
|
case ControllerType.JoyconPair:
|
|
|
|
currentEntry.ConnectionState |= NpadConnectionState.JoyLeftConnected |
|
|
|
|
NpadConnectionState.JoyRightConnected;
|
|
|
|
break;
|
|
|
|
case ControllerType.JoyconLeft:
|
|
|
|
currentEntry.ConnectionState |= NpadConnectionState.JoyLeftConnected;
|
|
|
|
break;
|
|
|
|
case ControllerType.JoyconRight:
|
|
|
|
currentEntry.ConnectionState |= NpadConnectionState.JoyRightConnected;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 23:32:42 +02:00
|
|
|
|
|
|
|
ref Array6<NpadSixAxis> sixaxis = ref controllers[i].Sixaxis;
|
|
|
|
for (int l = 0; l < sixaxis.Length; ++l)
|
|
|
|
{
|
|
|
|
ref NpadSixAxis currentLayout = ref sixaxis[l];
|
|
|
|
int currentIndex = UpdateEntriesHeader(ref currentLayout.Header, out int previousIndex);
|
|
|
|
|
|
|
|
ref SixAxisState currentEntry = ref currentLayout.Entries[currentIndex];
|
|
|
|
SixAxisState previousEntry = currentLayout.Entries[previousIndex];
|
|
|
|
|
|
|
|
currentEntry.SampleTimestamp = previousEntry.SampleTimestamp + 1;
|
|
|
|
currentEntry.SampleTimestamp2 = previousEntry.SampleTimestamp2 + 1;
|
|
|
|
|
|
|
|
currentEntry._unknown2 = 1;
|
|
|
|
}
|
2020-04-03 02:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|