aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Headless.SDL2/SDL2MouseDriver.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Ryujinx.Headless.SDL2/SDL2MouseDriver.cs')
-rw-r--r--Ryujinx.Headless.SDL2/SDL2MouseDriver.cs87
1 files changed, 74 insertions, 13 deletions
diff --git a/Ryujinx.Headless.SDL2/SDL2MouseDriver.cs b/Ryujinx.Headless.SDL2/SDL2MouseDriver.cs
index 236d4769..bdf428cc 100644
--- a/Ryujinx.Headless.SDL2/SDL2MouseDriver.cs
+++ b/Ryujinx.Headless.SDL2/SDL2MouseDriver.cs
@@ -10,7 +10,12 @@ namespace Ryujinx.Headless.SDL2
{
class SDL2MouseDriver : IGamepadDriver
{
+ private const int CursorHideIdleTime = 8; // seconds
+
private bool _isDisposed;
+ private HideCursor _hideCursor;
+ private bool _isHidden;
+ private long _lastCursorMoveTime;
public bool[] PressedButtons { get; }
@@ -18,9 +23,16 @@ namespace Ryujinx.Headless.SDL2
public Vector2 Scroll { get; private set; }
public Size _clientSize;
- public SDL2MouseDriver()
+ public SDL2MouseDriver(HideCursor hideCursor)
{
PressedButtons = new bool[(int)MouseButton.Count];
+ _hideCursor = hideCursor;
+
+ if (_hideCursor == HideCursor.Always)
+ {
+ SDL_ShowCursor(SDL_DISABLE);
+ _isHidden = true;
+ }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -31,26 +43,75 @@ namespace Ryujinx.Headless.SDL2
return (MouseButton)(rawButton - 1);
}
- public void Update(SDL_Event evnt)
+ public void UpdatePosition()
{
- if (evnt.type == SDL_EventType.SDL_MOUSEBUTTONDOWN || evnt.type == SDL_EventType.SDL_MOUSEBUTTONUP)
+ SDL_GetMouseState(out int posX, out int posY);
+ Vector2 position = new(posX, posY);
+
+ if (CurrentPosition != position)
{
- uint rawButton = evnt.button.button;
+ CurrentPosition = position;
+ _lastCursorMoveTime = Stopwatch.GetTimestamp();
+ }
- if (rawButton > 0 && rawButton <= (int)MouseButton.Count)
- {
- PressedButtons[(int)DriverButtonToMouseButton(rawButton)] = evnt.type == SDL_EventType.SDL_MOUSEBUTTONDOWN;
+ CheckIdle();
+ }
+
+ private void CheckIdle()
+ {
+ if (_hideCursor != HideCursor.OnIdle)
+ {
+ return;
+ }
+
+ long cursorMoveDelta = Stopwatch.GetTimestamp() - _lastCursorMoveTime;
- CurrentPosition = new Vector2(evnt.button.x, evnt.button.y);
+ if (cursorMoveDelta >= CursorHideIdleTime * Stopwatch.Frequency)
+ {
+ if (!_isHidden)
+ {
+ SDL_ShowCursor(SDL_DISABLE);
+ _isHidden = true;
}
}
- else if (evnt.type == SDL_EventType.SDL_MOUSEMOTION)
+ else
{
- CurrentPosition = new Vector2(evnt.motion.x, evnt.motion.y);
+ if (_isHidden)
+ {
+ SDL_ShowCursor(SDL_ENABLE);
+ _isHidden = false;
+ }
}
- else if (evnt.type == SDL_EventType.SDL_MOUSEWHEEL)
+ }
+
+ public void Update(SDL_Event evnt)
+ {
+ switch (evnt.type)
{
- Scroll = new Vector2(evnt.wheel.x, evnt.wheel.y);
+ case SDL_EventType.SDL_MOUSEBUTTONDOWN:
+ case SDL_EventType.SDL_MOUSEBUTTONUP:
+ uint rawButton = evnt.button.button;
+
+ if (rawButton > 0 && rawButton <= (int)MouseButton.Count)
+ {
+ PressedButtons[(int)DriverButtonToMouseButton(rawButton)] = evnt.type == SDL_EventType.SDL_MOUSEBUTTONDOWN;
+
+ CurrentPosition = new Vector2(evnt.button.x, evnt.button.y);
+ }
+
+ break;
+
+ // NOTE: On Linux using Wayland mouse motion events won't be received at all.
+ case SDL_EventType.SDL_MOUSEMOTION:
+ CurrentPosition = new Vector2(evnt.motion.x, evnt.motion.y);
+ _lastCursorMoveTime = Stopwatch.GetTimestamp();
+
+ break;
+
+ case SDL_EventType.SDL_MOUSEWHEEL:
+ Scroll = new Vector2(evnt.wheel.x, evnt.wheel.y);
+
+ break;
}
}
@@ -100,4 +161,4 @@ namespace Ryujinx.Headless.SDL2
_isDisposed = true;
}
}
-}
+} \ No newline at end of file