Hid.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. namespace Ryujinx.HLE.HOS.Services.Hid
  7. {
  8. public class Hid
  9. {
  10. private readonly Switch _device;
  11. private readonly ulong _hidMemoryAddress;
  12. internal ref HidSharedMemory SharedMemory => ref _device.Memory.GetRef<HidSharedMemory>(_hidMemoryAddress);
  13. internal const int SharedMemEntryCount = 17;
  14. public DebugPadDevice DebugPad;
  15. public TouchDevice Touchscreen;
  16. public MouseDevice Mouse;
  17. public KeyboardDevice Keyboard;
  18. public NpadDevices Npads;
  19. static Hid()
  20. {
  21. if (Unsafe.SizeOf<ShMemDebugPad>() != 0x400)
  22. {
  23. throw new InvalidStructLayoutException<ShMemDebugPad>(0x400);
  24. }
  25. if (Unsafe.SizeOf<ShMemTouchScreen>() != 0x3000)
  26. {
  27. throw new InvalidStructLayoutException<ShMemTouchScreen>(0x3000);
  28. }
  29. if (Unsafe.SizeOf<ShMemKeyboard>() != 0x400)
  30. {
  31. throw new InvalidStructLayoutException<ShMemKeyboard>(0x400);
  32. }
  33. if (Unsafe.SizeOf<ShMemMouse>() != 0x400)
  34. {
  35. throw new InvalidStructLayoutException<ShMemMouse>(0x400);
  36. }
  37. if (Unsafe.SizeOf<ShMemNpad>() != 0x5000)
  38. {
  39. throw new InvalidStructLayoutException<ShMemNpad>(0x5000);
  40. }
  41. if (Unsafe.SizeOf<HidSharedMemory>() != Horizon.HidSize)
  42. {
  43. throw new InvalidStructLayoutException<HidSharedMemory>(Horizon.HidSize);
  44. }
  45. }
  46. public Hid(in Switch device, ulong sharedHidMemoryAddress)
  47. {
  48. _device = device;
  49. _hidMemoryAddress = sharedHidMemoryAddress;
  50. device.Memory.ZeroFill(sharedHidMemoryAddress, Horizon.HidSize);
  51. }
  52. public void InitDevices()
  53. {
  54. DebugPad = new DebugPadDevice(_device, true);
  55. Touchscreen = new TouchDevice(_device, true);
  56. Mouse = new MouseDevice(_device, false);
  57. Keyboard = new KeyboardDevice(_device, false);
  58. Npads = new NpadDevices(_device, true);
  59. }
  60. internal void RefreshInputConfig(List<InputConfig> inputConfig)
  61. {
  62. ControllerConfig[] npadConfig = new ControllerConfig[inputConfig.Count];
  63. for (int i = 0; i < npadConfig.Length; ++i)
  64. {
  65. npadConfig[i].Player = (PlayerIndex)inputConfig[i].PlayerIndex;
  66. npadConfig[i].Type = (ControllerType)inputConfig[i].ControllerType;
  67. }
  68. _device.Hid.Npads.Configure(npadConfig);
  69. }
  70. internal void RefreshInputConfigEvent(object _, ReactiveEventArgs<List<InputConfig>> args)
  71. {
  72. RefreshInputConfig(args.NewValue);
  73. }
  74. public ControllerKeys UpdateStickButtons(JoystickPosition leftStick, JoystickPosition rightStick)
  75. {
  76. const int stickButtonThreshold = short.MaxValue / 2;
  77. ControllerKeys result = 0;
  78. result |= (leftStick.Dx < -stickButtonThreshold) ? ControllerKeys.LStickLeft : result;
  79. result |= (leftStick.Dx > stickButtonThreshold) ? ControllerKeys.LStickRight : result;
  80. result |= (leftStick.Dy < -stickButtonThreshold) ? ControllerKeys.LStickDown : result;
  81. result |= (leftStick.Dy > stickButtonThreshold) ? ControllerKeys.LStickUp : result;
  82. result |= (rightStick.Dx < -stickButtonThreshold) ? ControllerKeys.RStickLeft : result;
  83. result |= (rightStick.Dx > stickButtonThreshold) ? ControllerKeys.RStickRight : result;
  84. result |= (rightStick.Dy < -stickButtonThreshold) ? ControllerKeys.RStickDown : result;
  85. result |= (rightStick.Dy > stickButtonThreshold) ? ControllerKeys.RStickUp : result;
  86. return result;
  87. }
  88. internal static ulong GetTimestampTicks()
  89. {
  90. return (ulong)PerformanceCounter.ElapsedMilliseconds * 19200;
  91. }
  92. }
  93. }