SDL2MouseDriver.cs 5.0 KB

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