Hid.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Ryujinx.Common;
  2. using Ryujinx.HLE.Exceptions;
  3. using Ryujinx.Common.Configuration.Hid;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using Ryujinx.Common.Memory;
  7. using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory;
  8. using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.Common;
  9. using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.Mouse;
  10. using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.Keyboard;
  11. using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.DebugPad;
  12. using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.TouchScreen;
  13. using Ryujinx.HLE.HOS.Services.Hid.Types.SharedMemory.Npad;
  14. namespace Ryujinx.HLE.HOS.Services.Hid
  15. {
  16. public class Hid
  17. {
  18. private readonly Switch _device;
  19. private readonly ulong _hidMemoryAddress;
  20. internal ref SharedMemory SharedMemory => ref _device.Memory.GetRef<SharedMemory>(_hidMemoryAddress);
  21. internal const int SharedMemEntryCount = 17;
  22. public DebugPadDevice DebugPad;
  23. public TouchDevice Touchscreen;
  24. public MouseDevice Mouse;
  25. public KeyboardDevice Keyboard;
  26. public NpadDevices Npads;
  27. private static void CheckTypeSizeOrThrow<T>(int expectedSize)
  28. {
  29. if (Unsafe.SizeOf<T>() != expectedSize)
  30. {
  31. throw new InvalidStructLayoutException<T>(expectedSize);
  32. }
  33. }
  34. static Hid()
  35. {
  36. CheckTypeSizeOrThrow<RingLifo<DebugPadState>>(0x2c8);
  37. CheckTypeSizeOrThrow<RingLifo<TouchScreenState>>(0x2C38);
  38. CheckTypeSizeOrThrow<RingLifo<MouseState>>(0x350);
  39. CheckTypeSizeOrThrow<RingLifo<KeyboardState>>(0x3D8);
  40. CheckTypeSizeOrThrow<Array10<NpadState>>(0x32000);
  41. CheckTypeSizeOrThrow<SharedMemory>(Horizon.HidSize);
  42. }
  43. public Hid(in Switch device, ulong sharedHidMemoryAddress)
  44. {
  45. _device = device;
  46. _hidMemoryAddress = sharedHidMemoryAddress;
  47. SharedMemory = SharedMemory.Create();
  48. }
  49. public void InitDevices()
  50. {
  51. DebugPad = new DebugPadDevice(_device, true);
  52. Touchscreen = new TouchDevice(_device, true);
  53. Mouse = new MouseDevice(_device, false);
  54. Keyboard = new KeyboardDevice(_device, false);
  55. Npads = new NpadDevices(_device, true);
  56. }
  57. internal void RefreshInputConfig(List<InputConfig> inputConfig)
  58. {
  59. ControllerConfig[] npadConfig = new ControllerConfig[inputConfig.Count];
  60. for (int i = 0; i < npadConfig.Length; ++i)
  61. {
  62. npadConfig[i].Player = (PlayerIndex)inputConfig[i].PlayerIndex;
  63. npadConfig[i].Type = (ControllerType)inputConfig[i].ControllerType;
  64. }
  65. _device.Hid.Npads.Configure(npadConfig);
  66. }
  67. internal void RefreshInputConfigEvent(object _, ReactiveEventArgs<List<InputConfig>> args)
  68. {
  69. RefreshInputConfig(args.NewValue);
  70. }
  71. public ControllerKeys UpdateStickButtons(JoystickPosition leftStick, JoystickPosition rightStick)
  72. {
  73. const int stickButtonThreshold = short.MaxValue / 2;
  74. ControllerKeys result = 0;
  75. result |= (leftStick.Dx < -stickButtonThreshold) ? ControllerKeys.LStickLeft : result;
  76. result |= (leftStick.Dx > stickButtonThreshold) ? ControllerKeys.LStickRight : result;
  77. result |= (leftStick.Dy < -stickButtonThreshold) ? ControllerKeys.LStickDown : result;
  78. result |= (leftStick.Dy > stickButtonThreshold) ? ControllerKeys.LStickUp : result;
  79. result |= (rightStick.Dx < -stickButtonThreshold) ? ControllerKeys.RStickLeft : result;
  80. result |= (rightStick.Dx > stickButtonThreshold) ? ControllerKeys.RStickRight : result;
  81. result |= (rightStick.Dy < -stickButtonThreshold) ? ControllerKeys.RStickDown : result;
  82. result |= (rightStick.Dy > stickButtonThreshold) ? ControllerKeys.RStickUp : result;
  83. return result;
  84. }
  85. internal static ulong GetTimestampTicks()
  86. {
  87. return (ulong)PerformanceCounter.ElapsedMilliseconds * 19200;
  88. }
  89. }
  90. }