HidNpadController.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. namespace Ryujinx.HLE.Input
  2. {
  3. public class HidNpadController : HidControllerBase
  4. {
  5. private (NpadColor Left, NpadColor Right) _npadBodyColors;
  6. private (NpadColor Left, NpadColor Right) _npadButtonColors;
  7. private HidControllerLayouts _currentLayout;
  8. private bool _isHalf;
  9. public HidNpadController(
  10. HidControllerType controllerType,
  11. Switch device,
  12. (NpadColor, NpadColor) npadBodyColors,
  13. (NpadColor, NpadColor) npadButtonColors) : base(controllerType, device)
  14. {
  15. _npadBodyColors = npadBodyColors;
  16. _npadButtonColors = npadButtonColors;
  17. _currentLayout = HidControllerLayouts.HandheldJoined;
  18. switch (controllerType)
  19. {
  20. case HidControllerType.NpadLeft:
  21. _currentLayout = HidControllerLayouts.Left;
  22. break;
  23. case HidControllerType.NpadRight:
  24. _currentLayout = HidControllerLayouts.Right;
  25. break;
  26. case HidControllerType.NpadPair:
  27. _currentLayout = HidControllerLayouts.Joined;
  28. break;
  29. }
  30. }
  31. public override void Connect(HidControllerId controllerId)
  32. {
  33. if(HidControllerType != HidControllerType.NpadLeft && HidControllerType != HidControllerType.NpadRight)
  34. {
  35. _isHalf = false;
  36. }
  37. base.Connect(_currentLayout == HidControllerLayouts.HandheldJoined ? HidControllerId.ControllerHandheld : controllerId);
  38. HidControllerColorDesc singleColorDesc =
  39. HidControllerColorDesc.ColorDescColorsNonexistent;
  40. HidControllerColorDesc splitColorDesc = 0;
  41. NpadColor singleColorBody = NpadColor.Black;
  42. NpadColor singleColorButtons = NpadColor.Black;
  43. Device.Memory.WriteInt32(Offset + 0x04, _isHalf ? 1 : 0);
  44. if (_isHalf)
  45. {
  46. Device.Memory.WriteInt32(Offset + 0x08, (int)singleColorDesc);
  47. Device.Memory.WriteInt32(Offset + 0x0c, (int)singleColorBody);
  48. Device.Memory.WriteInt32(Offset + 0x10, (int)singleColorButtons);
  49. Device.Memory.WriteInt32(Offset + 0x14, (int)splitColorDesc);
  50. }
  51. else
  52. {
  53. Device.Memory.WriteInt32(Offset + 0x18, (int)_npadBodyColors.Left);
  54. Device.Memory.WriteInt32(Offset + 0x1c, (int)_npadButtonColors.Left);
  55. Device.Memory.WriteInt32(Offset + 0x20, (int)_npadBodyColors.Right);
  56. Device.Memory.WriteInt32(Offset + 0x24, (int)_npadButtonColors.Right);
  57. }
  58. Connected = true;
  59. }
  60. public override void SendInput
  61. (HidControllerButtons buttons,
  62. HidJoystickPosition leftStick,
  63. HidJoystickPosition rightStick)
  64. {
  65. long controllerOffset = WriteInput(buttons, leftStick, rightStick, _currentLayout);
  66. Device.Memory.WriteInt64(controllerOffset + 0x28,
  67. (Connected ? (uint)HidControllerConnState.ControllerStateConnected : 0) |
  68. (_currentLayout == HidControllerLayouts.HandheldJoined ? (uint)HidControllerConnState.ControllerStateWired : 0));
  69. controllerOffset = WriteInput(buttons, leftStick, rightStick, HidControllerLayouts.Main);
  70. Device.Memory.WriteInt64(controllerOffset + 0x28,
  71. (Connected ? (uint)HidControllerConnState.ControllerStateWired : 0) |
  72. (uint)HidControllerConnState.ControllerStateWired);
  73. }
  74. }
  75. }