2020-04-02 20:10:02 -04:00
|
|
|
using System;
|
|
|
|
using Ryujinx.Common.Logging;
|
2020-05-02 22:00:53 -04:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Hid
|
|
|
|
{
|
|
|
|
public class NpadDevices : BaseDevice
|
|
|
|
{
|
|
|
|
internal NpadJoyHoldType JoyHold = NpadJoyHoldType.Vertical;
|
|
|
|
internal bool SixAxisActive = false; // TODO: link to hidserver when implemented
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
private enum FilterState
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
|
|
|
Unconfigured = 0,
|
2020-05-02 22:00:53 -04:00
|
|
|
Configured = 1,
|
|
|
|
Accepted = 2
|
2020-04-02 20:10:02 -04:00
|
|
|
}
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
private struct NpadConfig
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
|
|
|
public ControllerType ConfiguredType;
|
|
|
|
public FilterState State;
|
|
|
|
}
|
|
|
|
|
|
|
|
private const int _maxControllers = 9; // Players1-8 and Handheld
|
|
|
|
private NpadConfig[] _configuredNpads;
|
|
|
|
|
|
|
|
private ControllerType _supportedStyleSets = ControllerType.ProController |
|
|
|
|
ControllerType.JoyconPair |
|
|
|
|
ControllerType.JoyconLeft |
|
|
|
|
ControllerType.JoyconRight |
|
|
|
|
ControllerType.Handheld;
|
|
|
|
|
|
|
|
public ControllerType SupportedStyleSets
|
|
|
|
{
|
2020-05-02 22:00:53 -04:00
|
|
|
get => _supportedStyleSets;
|
2020-04-02 20:10:02 -04:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_supportedStyleSets != value) // Deal with spamming
|
|
|
|
{
|
|
|
|
_supportedStyleSets = value;
|
|
|
|
MatchControllers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlayerIndex PrimaryController { get; set; } = PlayerIndex.Unknown;
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
private KEvent[] _styleSetUpdateEvents;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
private static readonly Array3<BatteryCharge> _fullBattery;
|
2020-04-02 20:10:02 -04:00
|
|
|
|
|
|
|
public NpadDevices(Switch device, bool active = true) : base(device, active)
|
|
|
|
{
|
|
|
|
_configuredNpads = new NpadConfig[_maxControllers];
|
|
|
|
|
|
|
|
_styleSetUpdateEvents = new KEvent[_maxControllers];
|
|
|
|
|
|
|
|
for (int i = 0; i < _styleSetUpdateEvents.Length; ++i)
|
|
|
|
{
|
|
|
|
_styleSetUpdateEvents[i] = new KEvent(_device.System);
|
|
|
|
}
|
|
|
|
|
|
|
|
_fullBattery[0] = _fullBattery[1] = _fullBattery[2] = BatteryCharge.Percent100;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddControllers(params ControllerConfig[] configs)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < configs.Length; ++i)
|
|
|
|
{
|
2020-05-02 22:00:53 -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;
|
|
|
|
}
|
|
|
|
|
|
|
|
_configuredNpads[(int)player] = new NpadConfig { ConfiguredType = controllerType, State = FilterState.Configured };
|
|
|
|
}
|
|
|
|
|
|
|
|
MatchControllers();
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
private void MatchControllers()
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
|
|
|
PrimaryController = PlayerIndex.Unknown;
|
|
|
|
|
|
|
|
for (int i = 0; i < _configuredNpads.Length; ++i)
|
|
|
|
{
|
|
|
|
ref NpadConfig config = ref _configuredNpads[i];
|
|
|
|
|
|
|
|
if (config.State == FilterState.Unconfigured)
|
|
|
|
{
|
|
|
|
continue; // Ignore unconfigured
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((config.ConfiguredType & _supportedStyleSets) == 0)
|
|
|
|
{
|
|
|
|
Logger.PrintWarning(LogClass.Hid, $"ControllerType {config.ConfiguredType} (connected to {(PlayerIndex)i}) not supported by game. Removing...");
|
|
|
|
|
|
|
|
config.State = FilterState.Configured;
|
|
|
|
_device.Hid.SharedMemory.Npads[i] = new ShMemNpad(); // Zero it
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
InitController((PlayerIndex)i, config.ConfiguredType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Couldn't find any matching configuration. Reassign to something that works.
|
|
|
|
if (PrimaryController == PlayerIndex.Unknown)
|
|
|
|
{
|
|
|
|
ControllerType[] npadsTypeList = (ControllerType[])Enum.GetValues(typeof(ControllerType));
|
|
|
|
|
|
|
|
// Skip None Type
|
|
|
|
for (int i = 1; i < npadsTypeList.Length; ++i)
|
|
|
|
{
|
|
|
|
ControllerType controllerType = npadsTypeList[i];
|
|
|
|
if ((controllerType & _supportedStyleSets) != 0)
|
|
|
|
{
|
|
|
|
Logger.PrintWarning(LogClass.Hid, $"No matching controllers found. Reassigning input as ControllerType {controllerType}...");
|
|
|
|
|
|
|
|
InitController(controllerType == ControllerType.Handheld ? PlayerIndex.Handheld : PlayerIndex.Player1, controllerType);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.PrintError(LogClass.Hid, "Couldn't find any appropriate controller.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal ref KEvent GetStyleSetUpdateEvent(PlayerIndex player)
|
|
|
|
{
|
|
|
|
return ref _styleSetUpdateEvents[(int)player];
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
private void InitController(PlayerIndex player, ControllerType type)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
|
|
|
if (type == ControllerType.Handheld)
|
|
|
|
{
|
|
|
|
player = PlayerIndex.Handheld;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref ShMemNpad controller = ref _device.Hid.SharedMemory.Npads[(int)player];
|
|
|
|
|
|
|
|
controller = new ShMemNpad(); // Zero it
|
|
|
|
|
|
|
|
// TODO: Allow customizing colors at config
|
|
|
|
NpadStateHeader defaultHeader = new NpadStateHeader
|
|
|
|
{
|
2020-05-02 22:00:53 -04:00
|
|
|
IsHalf = false,
|
|
|
|
SingleColorBody = NpadColor.BodyGray,
|
2020-04-02 20:10:02 -04:00
|
|
|
SingleColorButtons = NpadColor.ButtonGray,
|
2020-05-02 22:00:53 -04:00
|
|
|
LeftColorBody = NpadColor.BodyNeonBlue,
|
|
|
|
LeftColorButtons = NpadColor.ButtonGray,
|
|
|
|
RightColorBody = NpadColor.BodyNeonRed,
|
|
|
|
RightColorButtons = NpadColor.ButtonGray
|
2020-04-02 20:10:02 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
controller.SystemProperties = NpadSystemProperties.PowerInfo0Connected |
|
|
|
|
NpadSystemProperties.PowerInfo1Connected |
|
|
|
|
NpadSystemProperties.PowerInfo2Connected;
|
|
|
|
|
|
|
|
controller.BatteryState = _fullBattery;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ControllerType.ProController:
|
2020-05-02 22:00:53 -04:00
|
|
|
defaultHeader.Type = ControllerType.ProController;
|
|
|
|
controller.DeviceType = DeviceType.FullKey;
|
2020-04-02 20:10:02 -04:00
|
|
|
controller.SystemProperties |= NpadSystemProperties.AbxyButtonOriented |
|
|
|
|
NpadSystemProperties.PlusButtonCapability |
|
|
|
|
NpadSystemProperties.MinusButtonCapability;
|
|
|
|
break;
|
|
|
|
case ControllerType.Handheld:
|
2020-05-02 22:00:53 -04:00
|
|
|
defaultHeader.Type = ControllerType.Handheld;
|
|
|
|
controller.DeviceType = DeviceType.HandheldLeft |
|
2020-04-02 20:10:02 -04:00
|
|
|
DeviceType.HandheldRight;
|
|
|
|
controller.SystemProperties |= NpadSystemProperties.AbxyButtonOriented |
|
|
|
|
NpadSystemProperties.PlusButtonCapability |
|
|
|
|
NpadSystemProperties.MinusButtonCapability;
|
|
|
|
break;
|
|
|
|
case ControllerType.JoyconPair:
|
2020-05-02 22:00:53 -04:00
|
|
|
defaultHeader.Type = ControllerType.JoyconPair;
|
|
|
|
controller.DeviceType = DeviceType.JoyLeft |
|
2020-04-02 20:10:02 -04:00
|
|
|
DeviceType.JoyRight;
|
|
|
|
controller.SystemProperties |= NpadSystemProperties.AbxyButtonOriented |
|
|
|
|
NpadSystemProperties.PlusButtonCapability |
|
|
|
|
NpadSystemProperties.MinusButtonCapability;
|
|
|
|
break;
|
|
|
|
case ControllerType.JoyconLeft:
|
2020-05-02 22:00:53 -04:00
|
|
|
defaultHeader.Type = ControllerType.JoyconLeft;
|
|
|
|
defaultHeader.IsHalf = true;
|
|
|
|
controller.DeviceType = DeviceType.JoyLeft;
|
2020-04-02 20:10:02 -04:00
|
|
|
controller.SystemProperties |= NpadSystemProperties.SlSrButtonOriented |
|
|
|
|
NpadSystemProperties.MinusButtonCapability;
|
|
|
|
break;
|
|
|
|
case ControllerType.JoyconRight:
|
2020-05-02 22:00:53 -04:00
|
|
|
defaultHeader.Type = ControllerType.JoyconRight;
|
|
|
|
defaultHeader.IsHalf = true;
|
|
|
|
controller.DeviceType = DeviceType.JoyRight;
|
2020-04-02 20:10:02 -04:00
|
|
|
controller.SystemProperties |= NpadSystemProperties.SlSrButtonOriented |
|
|
|
|
NpadSystemProperties.PlusButtonCapability;
|
|
|
|
break;
|
|
|
|
case ControllerType.Pokeball:
|
2020-05-02 22:00:53 -04:00
|
|
|
defaultHeader.Type = ControllerType.Pokeball;
|
2020-04-02 20:10:02 -04:00
|
|
|
controller.DeviceType = DeviceType.Palma;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller.Header = defaultHeader;
|
|
|
|
|
|
|
|
if (PrimaryController == PlayerIndex.Unknown)
|
|
|
|
{
|
|
|
|
PrimaryController = player;
|
|
|
|
}
|
|
|
|
|
|
|
|
_configuredNpads[(int)player].State = FilterState.Accepted;
|
|
|
|
|
|
|
|
_styleSetUpdateEvents[(int)player].ReadableEvent.Signal();
|
|
|
|
|
|
|
|
Logger.PrintInfo(LogClass.Hid, $"Connected ControllerType {type} to PlayerIndex {player}");
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
private static NpadLayoutsIndex ControllerTypeToLayout(ControllerType controllerType)
|
2020-04-02 20:10:02 -04:00
|
|
|
=> controllerType switch
|
|
|
|
{
|
|
|
|
ControllerType.ProController => NpadLayoutsIndex.ProController,
|
2020-05-02 22:00:53 -04: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-02 20:10:02 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
public void SetGamepadsInput(params GamepadInput[] states)
|
|
|
|
{
|
|
|
|
UpdateAllEntries();
|
|
|
|
|
|
|
|
for (int i = 0; i < states.Length; ++i)
|
|
|
|
{
|
|
|
|
SetGamepadState(states[i].PlayerId, states[i].Buttons, states[i].LStick, states[i].RStick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
private void SetGamepadState(PlayerIndex player, ControllerKeys buttons,
|
|
|
|
JoystickPosition leftJoystick, JoystickPosition rightJoystick)
|
2020-04-02 20:10:02 -04:00
|
|
|
{
|
|
|
|
if (player == PlayerIndex.Auto)
|
|
|
|
{
|
|
|
|
player = PrimaryController;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (player == PlayerIndex.Unknown)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_configuredNpads[(int)player].State != FilterState.Accepted)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
ref ShMemNpad currentNpad = ref _device.Hid.SharedMemory.Npads[(int)player];
|
2020-04-02 20:10:02 -04:00
|
|
|
ref NpadLayout currentLayout = ref currentNpad.Layouts[(int)ControllerTypeToLayout(currentNpad.Header.Type)];
|
2020-05-02 22:00:53 -04:00
|
|
|
ref NpadState currentEntry = ref currentLayout.Entries[(int)currentLayout.Header.LatestEntry];
|
2020-04-02 20:10:02 -04:00
|
|
|
|
|
|
|
currentEntry.Buttons = buttons;
|
|
|
|
currentEntry.LStickX = leftJoystick.Dx;
|
|
|
|
currentEntry.LStickY = leftJoystick.Dy;
|
|
|
|
currentEntry.RStickX = rightJoystick.Dx;
|
|
|
|
currentEntry.RStickY = rightJoystick.Dy;
|
|
|
|
|
|
|
|
// 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-05-02 22:00:53 -04:00
|
|
|
private void UpdateAllEntries()
|
2020-04-02 20:10:02 -04: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-02 22:00:53 -04:00
|
|
|
NpadState previousEntry = currentLayout.Entries[previousIndex];
|
2020-04-02 20:10:02 -04:00
|
|
|
|
2020-05-02 22:00:53 -04:00
|
|
|
currentEntry.SampleTimestamp = previousEntry.SampleTimestamp + 1;
|
2020-04-02 20:10:02 -04: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|