HidBaseController.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using static Ryujinx.HLE.Input.Hid;
  2. namespace Ryujinx.HLE.Input
  3. {
  4. public abstract class HidControllerBase : IHidDevice
  5. {
  6. protected HidControllerType HidControllerType;
  7. protected Switch Device;
  8. protected HidControllerId ControllerId;
  9. public long Offset { get; private set; }
  10. public bool Connected { get; protected set; }
  11. public HidControllerBase(HidControllerType ControllerType, Switch Device)
  12. {
  13. this.Device = Device;
  14. HidControllerType = ControllerType;
  15. }
  16. public virtual void Connect(HidControllerId ControllerId)
  17. {
  18. this.ControllerId = ControllerId;
  19. Offset = Device.Hid.HidPosition + HidControllersOffset + (int)ControllerId * HidControllerSize;
  20. Device.Memory.FillWithZeros(Offset, 0x5000);
  21. Device.Memory.WriteInt32(Offset + 0x00, (int)HidControllerType);
  22. }
  23. public abstract void SendInput(
  24. HidControllerButtons Buttons,
  25. HidJoystickPosition LeftStick,
  26. HidJoystickPosition RightStick);
  27. protected long WriteInput(
  28. HidControllerButtons Buttons,
  29. HidJoystickPosition LeftStick,
  30. HidJoystickPosition RightStick,
  31. HidControllerLayouts ControllerLayout)
  32. {
  33. long ControllerOffset = Offset + HidControllerHeaderSize;
  34. ControllerOffset += (int)ControllerLayout * HidControllerLayoutsSize;
  35. long LastEntry = Device.Memory.ReadInt64(ControllerOffset + 0x10);
  36. long CurrEntry = (LastEntry + 1) % HidEntryCount;
  37. long Timestamp = GetTimestamp();
  38. Device.Memory.WriteInt64(ControllerOffset + 0x00, Timestamp);
  39. Device.Memory.WriteInt64(ControllerOffset + 0x08, HidEntryCount);
  40. Device.Memory.WriteInt64(ControllerOffset + 0x10, CurrEntry);
  41. Device.Memory.WriteInt64(ControllerOffset + 0x18, HidEntryCount - 1);
  42. ControllerOffset += HidControllersLayoutHeaderSize;
  43. long LastEntryOffset = ControllerOffset + LastEntry * HidControllersInputEntrySize;
  44. ControllerOffset += CurrEntry * HidControllersInputEntrySize;
  45. long SampleCounter = Device.Memory.ReadInt64(LastEntryOffset) + 1;
  46. Device.Memory.WriteInt64(ControllerOffset + 0x00, SampleCounter);
  47. Device.Memory.WriteInt64(ControllerOffset + 0x08, SampleCounter);
  48. Device.Memory.WriteInt64(ControllerOffset + 0x10, (uint)Buttons);
  49. Device.Memory.WriteInt32(ControllerOffset + 0x18, LeftStick.DX);
  50. Device.Memory.WriteInt32(ControllerOffset + 0x1c, LeftStick.DY);
  51. Device.Memory.WriteInt32(ControllerOffset + 0x20, RightStick.DX);
  52. Device.Memory.WriteInt32(ControllerOffset + 0x24, RightStick.DY);
  53. return ControllerOffset;
  54. }
  55. }
  56. }