Support other switch controller types (#487)

* Make controllers modular, support changing controller type

* return readable events

* signal hid events

* fix style
This commit is contained in:
emmauss
2018-11-20 02:01:36 +02:00
committed by Ac_K
parent 0c36835f6d
commit dc02ac08ca
24 changed files with 574 additions and 409 deletions

View File

@@ -1,10 +1,11 @@
using LibHac;
using Ryujinx.Common.Logging;
using Ryujinx.HLE;
using Ryujinx.HLE.Input;
using Ryujinx.UI.Input;
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
@@ -13,8 +14,8 @@ namespace Ryujinx
{
public static class Config
{
public static JoyConKeyboard JoyConKeyboard { get; private set; }
public static JoyConController JoyConController { get; private set; }
public static NpadKeyboard NpadKeyboard { get; private set; }
public static NpadController NpadController { get; private set; }
public static void Read(Switch device)
{
@@ -73,9 +74,13 @@ namespace Ryujinx
? IntegrityCheckLevel.ErrorOnInvalid
: IntegrityCheckLevel.None;
JoyConKeyboard = new JoyConKeyboard(
HidControllerType ControllerType = Enum.Parse<HidControllerType>(parser.Value("Controller_Type"));
new JoyConKeyboardLeft
device.Hid.InitilizePrimaryController(ControllerType);
NpadKeyboard = new NpadKeyboard(
new NpadKeyboardLeft
{
StickUp = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Stick_Up")),
StickDown = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Stick_Down")),
@@ -91,7 +96,7 @@ namespace Ryujinx
ButtonZl = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Button_ZL"))
},
new JoyConKeyboardRight
new NpadKeyboardRight
{
StickUp = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Stick_Up")),
StickDown = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Stick_Down")),
@@ -107,14 +112,14 @@ namespace Ryujinx
ButtonZr = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Button_ZR"))
});
JoyConController = new JoyConController(
NpadController = new NpadController(
Convert.ToBoolean(parser.Value("GamePad_Enable")),
Convert.ToInt32 (parser.Value("GamePad_Index")),
(float)Convert.ToDouble (parser.Value("GamePad_Deadzone"), CultureInfo.InvariantCulture),
(float)Convert.ToDouble (parser.Value("GamePad_Trigger_Threshold"), CultureInfo.InvariantCulture),
new JoyConControllerLeft
new NpadControllerLeft
{
Stick = ToId(parser.Value("Controls_Left_JoyConController_Stick")),
StickButton = ToId(parser.Value("Controls_Left_JoyConController_Stick_Button")),
@@ -127,7 +132,7 @@ namespace Ryujinx
ButtonZl = ToId(parser.Value("Controls_Left_JoyConController_Button_ZL"))
},
new JoyConControllerRight
new NpadControllerRight
{
Stick = ToId(parser.Value("Controls_Right_JoyConController_Stick")),
StickButton = ToId(parser.Value("Controls_Right_JoyConController_Stick_Button")),

View File

@@ -46,6 +46,9 @@ GamePad_Trigger_Threshold = 0.5
#Whether or not to enable Controller support
GamePad_Enable = true
#The primary controller's type. Supported Values: ProController, Handheld, NpadPair, NpadLeft, NpadRight
Controller_Type = Handheld
#https://github.com/opentk/opentk/blob/develop/src/OpenTK/Input/Key.cs
Controls_Left_JoyConKeyboard_Stick_Up = 105
Controls_Left_JoyConKeyboard_Stick_Down = 101

View File

@@ -142,25 +142,24 @@ namespace Ryujinx
{
KeyboardState keyboard = _keyboard.Value;
currentButton = Config.JoyConKeyboard.GetButtons(keyboard);
currentButton = Config.NpadKeyboard.GetButtons(keyboard);
(leftJoystickDx, leftJoystickDy) = Config.JoyConKeyboard.GetLeftStick(keyboard);
(leftJoystickDx, leftJoystickDy) = Config.NpadKeyboard.GetLeftStick(keyboard);
(rightJoystickDx, rightJoystickDy) = Config.JoyConKeyboard.GetRightStick(keyboard);
(rightJoystickDx, rightJoystickDy) = Config.NpadKeyboard.GetRightStick(keyboard);
}
//Controller Input
currentButton |= Config.JoyConController.GetButtons();
currentButton |= Config.NpadController.GetButtons();
//Keyboard has priority stick-wise
if (leftJoystickDx == 0 && leftJoystickDy == 0)
{
(leftJoystickDx, leftJoystickDy) = Config.JoyConController.GetLeftStick();
(leftJoystickDx, leftJoystickDy) = Config.NpadController.GetLeftStick();
}
if (rightJoystickDx == 0 && rightJoystickDy == 0)
{
(rightJoystickDx, rightJoystickDy) = Config.JoyConController.GetRightStick();
(rightJoystickDx, rightJoystickDy) = Config.NpadController.GetRightStick();
}
leftJoystick = new HidJoystickPosition
@@ -234,19 +233,9 @@ namespace Ryujinx
_device.Hid.SetTouchPoints();
}
_device.Hid.SetJoyconButton(
HidControllerId.CONTROLLER_HANDHELD,
HidControllerLayouts.Handheld_Joined,
currentButton,
leftJoystick,
rightJoystick);
HidControllerBase controller = _device.Hid.PrimaryController;
_device.Hid.SetJoyconButton(
HidControllerId.CONTROLLER_HANDHELD,
HidControllerLayouts.Main,
currentButton,
leftJoystick,
rightJoystick);
controller.SendInput(currentButton, leftJoystick, rightJoystick);
}
private new void RenderFrame()

View File

@@ -32,7 +32,7 @@ namespace Ryujinx.UI.Input
RJoystick
}
public struct JoyConControllerLeft
public struct NpadControllerLeft
{
public ControllerInputId Stick;
public ControllerInputId StickButton;
@@ -45,7 +45,7 @@ namespace Ryujinx.UI.Input
public ControllerInputId ButtonZl;
}
public struct JoyConControllerRight
public struct NpadControllerRight
{
public ControllerInputId Stick;
public ControllerInputId StickButton;
@@ -58,23 +58,23 @@ namespace Ryujinx.UI.Input
public ControllerInputId ButtonZr;
}
public class JoyConController
public class NpadController
{
public bool Enabled { private set; get; }
public int Index { private set; get; }
public float Deadzone { private set; get; }
public float TriggerThreshold { private set; get; }
public JoyConControllerLeft Left { private set; get; }
public JoyConControllerRight Right { private set; get; }
public NpadControllerLeft Left { private set; get; }
public NpadControllerRight Right { private set; get; }
public JoyConController(
public NpadController(
bool enabled,
int index,
float deadzone,
float triggerThreshold,
JoyConControllerLeft left,
JoyConControllerRight right)
NpadControllerLeft left,
NpadControllerRight right)
{
Enabled = enabled;
Index = index;
@@ -101,23 +101,23 @@ namespace Ryujinx.UI.Input
HidControllerButtons buttons = 0;
if (IsPressed(gpState, Left.DPadUp)) buttons |= HidControllerButtons.KEY_DUP;
if (IsPressed(gpState, Left.DPadDown)) buttons |= HidControllerButtons.KEY_DDOWN;
if (IsPressed(gpState, Left.DPadLeft)) buttons |= HidControllerButtons.KEY_DLEFT;
if (IsPressed(gpState, Left.DPadRight)) buttons |= HidControllerButtons.KEY_DRIGHT;
if (IsPressed(gpState, Left.StickButton)) buttons |= HidControllerButtons.KEY_LSTICK;
if (IsPressed(gpState, Left.ButtonMinus)) buttons |= HidControllerButtons.KEY_MINUS;
if (IsPressed(gpState, Left.ButtonL)) buttons |= HidControllerButtons.KEY_L;
if (IsPressed(gpState, Left.ButtonZl)) buttons |= HidControllerButtons.KEY_ZL;
if (IsPressed(gpState, Left.DPadUp)) buttons |= HidControllerButtons.DpadUp;
if (IsPressed(gpState, Left.DPadDown)) buttons |= HidControllerButtons.DpadDown;
if (IsPressed(gpState, Left.DPadLeft)) buttons |= HidControllerButtons.DpadLeft;
if (IsPressed(gpState, Left.DPadRight)) buttons |= HidControllerButtons.DPadRight;
if (IsPressed(gpState, Left.StickButton)) buttons |= HidControllerButtons.StickLeft;
if (IsPressed(gpState, Left.ButtonMinus)) buttons |= HidControllerButtons.Minus;
if (IsPressed(gpState, Left.ButtonL)) buttons |= HidControllerButtons.L;
if (IsPressed(gpState, Left.ButtonZl)) buttons |= HidControllerButtons.Zl;
if (IsPressed(gpState, Right.ButtonA)) buttons |= HidControllerButtons.KEY_A;
if (IsPressed(gpState, Right.ButtonB)) buttons |= HidControllerButtons.KEY_B;
if (IsPressed(gpState, Right.ButtonX)) buttons |= HidControllerButtons.KEY_X;
if (IsPressed(gpState, Right.ButtonY)) buttons |= HidControllerButtons.KEY_Y;
if (IsPressed(gpState, Right.StickButton)) buttons |= HidControllerButtons.KEY_RSTICK;
if (IsPressed(gpState, Right.ButtonPlus)) buttons |= HidControllerButtons.KEY_PLUS;
if (IsPressed(gpState, Right.ButtonR)) buttons |= HidControllerButtons.KEY_R;
if (IsPressed(gpState, Right.ButtonZr)) buttons |= HidControllerButtons.KEY_ZR;
if (IsPressed(gpState, Right.ButtonA)) buttons |= HidControllerButtons.A;
if (IsPressed(gpState, Right.ButtonB)) buttons |= HidControllerButtons.B;
if (IsPressed(gpState, Right.ButtonX)) buttons |= HidControllerButtons.X;
if (IsPressed(gpState, Right.ButtonY)) buttons |= HidControllerButtons.Y;
if (IsPressed(gpState, Right.StickButton)) buttons |= HidControllerButtons.StickRight;
if (IsPressed(gpState, Right.ButtonPlus)) buttons |= HidControllerButtons.Plus;
if (IsPressed(gpState, Right.ButtonR)) buttons |= HidControllerButtons.R;
if (IsPressed(gpState, Right.ButtonZr)) buttons |= HidControllerButtons.Zr;
return buttons;
}

View File

@@ -3,7 +3,7 @@ using Ryujinx.HLE.Input;
namespace Ryujinx.UI.Input
{
public struct JoyConKeyboardLeft
public struct NpadKeyboardLeft
{
public int StickUp;
public int StickDown;
@@ -19,7 +19,7 @@ namespace Ryujinx.UI.Input
public int ButtonZl;
}
public struct JoyConKeyboardRight
public struct NpadKeyboardRight
{
public int StickUp;
public int StickDown;
@@ -35,14 +35,14 @@ namespace Ryujinx.UI.Input
public int ButtonZr;
}
public class JoyConKeyboard
public class NpadKeyboard
{
public JoyConKeyboardLeft Left;
public JoyConKeyboardRight Right;
public NpadKeyboardLeft Left;
public NpadKeyboardRight Right;
public JoyConKeyboard(
JoyConKeyboardLeft left,
JoyConKeyboardRight right)
public NpadKeyboard(
NpadKeyboardLeft left,
NpadKeyboardRight right)
{
Left = left;
Right = right;
@@ -52,23 +52,23 @@ namespace Ryujinx.UI.Input
{
HidControllerButtons buttons = 0;
if (keyboard[(Key)Left.StickButton]) buttons |= HidControllerButtons.KEY_LSTICK;
if (keyboard[(Key)Left.DPadUp]) buttons |= HidControllerButtons.KEY_DUP;
if (keyboard[(Key)Left.DPadDown]) buttons |= HidControllerButtons.KEY_DDOWN;
if (keyboard[(Key)Left.DPadLeft]) buttons |= HidControllerButtons.KEY_DLEFT;
if (keyboard[(Key)Left.DPadRight]) buttons |= HidControllerButtons.KEY_DRIGHT;
if (keyboard[(Key)Left.ButtonMinus]) buttons |= HidControllerButtons.KEY_MINUS;
if (keyboard[(Key)Left.ButtonL]) buttons |= HidControllerButtons.KEY_L;
if (keyboard[(Key)Left.ButtonZl]) buttons |= HidControllerButtons.KEY_ZL;
if (keyboard[(Key)Left.StickButton]) buttons |= HidControllerButtons.StickLeft;
if (keyboard[(Key)Left.DPadUp]) buttons |= HidControllerButtons.DpadUp;
if (keyboard[(Key)Left.DPadDown]) buttons |= HidControllerButtons.DpadDown;
if (keyboard[(Key)Left.DPadLeft]) buttons |= HidControllerButtons.DpadLeft;
if (keyboard[(Key)Left.DPadRight]) buttons |= HidControllerButtons.DPadRight;
if (keyboard[(Key)Left.ButtonMinus]) buttons |= HidControllerButtons.Minus;
if (keyboard[(Key)Left.ButtonL]) buttons |= HidControllerButtons.L;
if (keyboard[(Key)Left.ButtonZl]) buttons |= HidControllerButtons.Zl;
if (keyboard[(Key)Right.StickButton]) buttons |= HidControllerButtons.KEY_RSTICK;
if (keyboard[(Key)Right.ButtonA]) buttons |= HidControllerButtons.KEY_A;
if (keyboard[(Key)Right.ButtonB]) buttons |= HidControllerButtons.KEY_B;
if (keyboard[(Key)Right.ButtonX]) buttons |= HidControllerButtons.KEY_X;
if (keyboard[(Key)Right.ButtonY]) buttons |= HidControllerButtons.KEY_Y;
if (keyboard[(Key)Right.ButtonPlus]) buttons |= HidControllerButtons.KEY_PLUS;
if (keyboard[(Key)Right.ButtonR]) buttons |= HidControllerButtons.KEY_R;
if (keyboard[(Key)Right.ButtonZr]) buttons |= HidControllerButtons.KEY_ZR;
if (keyboard[(Key)Right.StickButton]) buttons |= HidControllerButtons.StickRight;
if (keyboard[(Key)Right.ButtonA]) buttons |= HidControllerButtons.A;
if (keyboard[(Key)Right.ButtonB]) buttons |= HidControllerButtons.B;
if (keyboard[(Key)Right.ButtonX]) buttons |= HidControllerButtons.X;
if (keyboard[(Key)Right.ButtonY]) buttons |= HidControllerButtons.Y;
if (keyboard[(Key)Right.ButtonPlus]) buttons |= HidControllerButtons.Plus;
if (keyboard[(Key)Right.ButtonR]) buttons |= HidControllerButtons.R;
if (keyboard[(Key)Right.ButtonZr]) buttons |= HidControllerButtons.Zr;
return buttons;
}