SDL2MouseDriver.cs 4.9 KB

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