SDL2MouseDriver.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Ryujinx.Input;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Numerics;
  6. using System.Runtime.CompilerServices;
  7. using static SDL2.SDL;
  8. namespace Ryujinx.Headless.SDL2
  9. {
  10. class SDL2MouseDriver : IGamepadDriver
  11. {
  12. private const int CursorHideIdleTime = 5; // seconds
  13. private bool _isDisposed;
  14. private HideCursor _hideCursor;
  15. private bool _isHidden;
  16. private long _lastCursorMoveTime;
  17. public bool[] PressedButtons { get; }
  18. public Vector2 CurrentPosition { get; private set; }
  19. public Vector2 Scroll { get; private set; }
  20. public Size _clientSize;
  21. public SDL2MouseDriver(HideCursor hideCursor)
  22. {
  23. PressedButtons = new bool[(int)MouseButton.Count];
  24. _hideCursor = hideCursor;
  25. if (_hideCursor == HideCursor.Always)
  26. {
  27. SDL_ShowCursor(SDL_DISABLE);
  28. _isHidden = true;
  29. }
  30. }
  31. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  32. private static MouseButton DriverButtonToMouseButton(uint rawButton)
  33. {
  34. Debug.Assert(rawButton > 0 && rawButton <= (int)MouseButton.Count);
  35. return (MouseButton)(rawButton - 1);
  36. }
  37. public void UpdatePosition()
  38. {
  39. SDL_GetMouseState(out int posX, out int posY);
  40. Vector2 position = new(posX, posY);
  41. if (CurrentPosition != position)
  42. {
  43. CurrentPosition = position;
  44. _lastCursorMoveTime = Stopwatch.GetTimestamp();
  45. }
  46. CheckIdle();
  47. }
  48. private void CheckIdle()
  49. {
  50. if (_hideCursor != HideCursor.OnIdle)
  51. {
  52. return;
  53. }
  54. long cursorMoveDelta = Stopwatch.GetTimestamp() - _lastCursorMoveTime;
  55. if (cursorMoveDelta >= CursorHideIdleTime * Stopwatch.Frequency)
  56. {
  57. if (!_isHidden)
  58. {
  59. SDL_ShowCursor(SDL_DISABLE);
  60. _isHidden = true;
  61. }
  62. }
  63. else
  64. {
  65. if (_isHidden)
  66. {
  67. SDL_ShowCursor(SDL_ENABLE);
  68. _isHidden = false;
  69. }
  70. }
  71. }
  72. public void Update(SDL_Event evnt)
  73. {
  74. switch (evnt.type)
  75. {
  76. case SDL_EventType.SDL_MOUSEBUTTONDOWN:
  77. case SDL_EventType.SDL_MOUSEBUTTONUP:
  78. uint rawButton = evnt.button.button;
  79. if (rawButton > 0 && rawButton <= (int)MouseButton.Count)
  80. {
  81. PressedButtons[(int)DriverButtonToMouseButton(rawButton)] = evnt.type == SDL_EventType.SDL_MOUSEBUTTONDOWN;
  82. CurrentPosition = new Vector2(evnt.button.x, evnt.button.y);
  83. }
  84. break;
  85. // NOTE: On Linux using Wayland mouse motion events won't be received at all.
  86. case SDL_EventType.SDL_MOUSEMOTION:
  87. CurrentPosition = new Vector2(evnt.motion.x, evnt.motion.y);
  88. _lastCursorMoveTime = Stopwatch.GetTimestamp();
  89. break;
  90. case SDL_EventType.SDL_MOUSEWHEEL:
  91. Scroll = new Vector2(evnt.wheel.x, evnt.wheel.y);
  92. break;
  93. }
  94. }
  95. public void SetClientSize(int width, int height)
  96. {
  97. _clientSize = new Size(width, height);
  98. }
  99. public bool IsButtonPressed(MouseButton button)
  100. {
  101. return PressedButtons[(int)button];
  102. }
  103. public Size GetClientSize()
  104. {
  105. return _clientSize;
  106. }
  107. public string DriverName => "SDL2";
  108. public event Action<string> OnGamepadConnected
  109. {
  110. add { }
  111. remove { }
  112. }
  113. public event Action<string> OnGamepadDisconnected
  114. {
  115. add { }
  116. remove { }
  117. }
  118. public ReadOnlySpan<string> GamepadsIds => new[] { "0" };
  119. public IGamepad GetGamepad(string id)
  120. {
  121. return new SDL2Mouse(this);
  122. }
  123. public void Dispose()
  124. {
  125. if (_isDisposed)
  126. {
  127. return;
  128. }
  129. _isDisposed = true;
  130. }
  131. }
  132. }