Hid.cs 4.0 KB

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