Hid.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Ryujinx.Common;
  2. using Ryujinx.HLE.Exceptions;
  3. using System.Runtime.CompilerServices;
  4. namespace Ryujinx.HLE.HOS.Services.Hid
  5. {
  6. public class Hid
  7. {
  8. private readonly Switch _device;
  9. private readonly ulong _hidMemoryAddress;
  10. internal ref HidSharedMemory SharedMemory => ref _device.Memory.GetRef<HidSharedMemory>(_hidMemoryAddress);
  11. internal const int SharedMemEntryCount = 17;
  12. public DebugPadDevice DebugPad;
  13. public TouchDevice Touchscreen;
  14. public MouseDevice Mouse;
  15. public KeyboardDevice Keyboard;
  16. public NpadDevices Npads;
  17. static Hid()
  18. {
  19. if (Unsafe.SizeOf<ShMemDebugPad>() != 0x400)
  20. {
  21. throw new InvalidStructLayoutException<ShMemDebugPad>(0x400);
  22. }
  23. if (Unsafe.SizeOf<ShMemTouchScreen>() != 0x3000)
  24. {
  25. throw new InvalidStructLayoutException<ShMemTouchScreen>(0x3000);
  26. }
  27. if (Unsafe.SizeOf<ShMemKeyboard>() != 0x400)
  28. {
  29. throw new InvalidStructLayoutException<ShMemKeyboard>(0x400);
  30. }
  31. if (Unsafe.SizeOf<ShMemMouse>() != 0x400)
  32. {
  33. throw new InvalidStructLayoutException<ShMemMouse>(0x400);
  34. }
  35. if (Unsafe.SizeOf<ShMemNpad>() != 0x5000)
  36. {
  37. throw new InvalidStructLayoutException<ShMemNpad>(0x5000);
  38. }
  39. if (Unsafe.SizeOf<HidSharedMemory>() != Horizon.HidSize)
  40. {
  41. throw new InvalidStructLayoutException<HidSharedMemory>(Horizon.HidSize);
  42. }
  43. }
  44. public Hid(in Switch device, ulong sharedHidMemoryAddress)
  45. {
  46. _device = device;
  47. _hidMemoryAddress = sharedHidMemoryAddress;
  48. device.Memory.ZeroFill(sharedHidMemoryAddress, Horizon.HidSize);
  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 ControllerKeys UpdateStickButtons(JoystickPosition leftStick, JoystickPosition rightStick)
  59. {
  60. ControllerKeys result = 0;
  61. result |= (leftStick.Dx < 0) ? ControllerKeys.LStickLeft : result;
  62. result |= (leftStick.Dx > 0) ? ControllerKeys.LStickRight : result;
  63. result |= (leftStick.Dy < 0) ? ControllerKeys.LStickDown : result;
  64. result |= (leftStick.Dy > 0) ? ControllerKeys.LStickUp : result;
  65. result |= (rightStick.Dx < 0) ? ControllerKeys.RStickLeft : result;
  66. result |= (rightStick.Dx > 0) ? ControllerKeys.RStickRight : result;
  67. result |= (rightStick.Dy < 0) ? ControllerKeys.RStickDown : result;
  68. result |= (rightStick.Dy > 0) ? ControllerKeys.RStickUp : result;
  69. return result;
  70. }
  71. internal static ulong GetTimestampTicks()
  72. {
  73. return (ulong)PerformanceCounter.ElapsedMilliseconds * 19200;
  74. }
  75. }
  76. }