HidBaseController.cs 2.8 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. Device = device;
  14. HidControllerType = controllerType;
  15. }
  16. public virtual void Connect(HidControllerId controllerId)
  17. {
  18. 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. }