2023-06-28 19:03:27 +02:00
|
|
|
using Ryujinx.Common.Configuration;
|
|
|
|
using Ryujinx.Common.Logging;
|
2023-05-02 03:29:47 +02:00
|
|
|
using Ryujinx.Input;
|
2021-07-06 22:08:44 +02:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.Numerics;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using static SDL2.SDL;
|
|
|
|
|
|
|
|
namespace Ryujinx.Headless.SDL2
|
|
|
|
{
|
|
|
|
class SDL2MouseDriver : IGamepadDriver
|
|
|
|
{
|
2023-01-15 01:05:44 +01:00
|
|
|
private const int CursorHideIdleTime = 5; // seconds
|
2023-01-09 04:55:37 +01:00
|
|
|
|
2021-07-06 22:08:44 +02:00
|
|
|
private bool _isDisposed;
|
2023-06-28 19:03:27 +02:00
|
|
|
private readonly HideCursorMode _hideCursorMode;
|
2023-01-09 04:55:37 +01:00
|
|
|
private bool _isHidden;
|
|
|
|
private long _lastCursorMoveTime;
|
2021-07-06 22:08:44 +02:00
|
|
|
|
|
|
|
public bool[] PressedButtons { get; }
|
|
|
|
|
|
|
|
public Vector2 CurrentPosition { get; private set; }
|
|
|
|
public Vector2 Scroll { get; private set; }
|
2023-06-28 19:03:27 +02:00
|
|
|
public Size ClientSize;
|
2021-07-06 22:08:44 +02:00
|
|
|
|
2023-05-02 03:29:47 +02:00
|
|
|
public SDL2MouseDriver(HideCursorMode hideCursorMode)
|
2021-07-06 22:08:44 +02:00
|
|
|
{
|
|
|
|
PressedButtons = new bool[(int)MouseButton.Count];
|
2023-05-02 03:29:47 +02:00
|
|
|
_hideCursorMode = hideCursorMode;
|
2023-01-09 04:55:37 +01:00
|
|
|
|
2023-05-02 03:29:47 +02:00
|
|
|
if (_hideCursorMode == HideCursorMode.Always)
|
2023-01-09 04:55:37 +01:00
|
|
|
{
|
2023-06-28 19:03:27 +02:00
|
|
|
if (SDL_ShowCursor(SDL_DISABLE) != SDL_DISABLE)
|
|
|
|
{
|
|
|
|
Logger.Error?.PrintMsg(LogClass.Application, "Failed to disable the cursor.");
|
|
|
|
}
|
|
|
|
|
2023-01-09 04:55:37 +01:00
|
|
|
_isHidden = true;
|
|
|
|
}
|
2021-07-06 22:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private static MouseButton DriverButtonToMouseButton(uint rawButton)
|
|
|
|
{
|
|
|
|
Debug.Assert(rawButton > 0 && rawButton <= (int)MouseButton.Count);
|
|
|
|
|
|
|
|
return (MouseButton)(rawButton - 1);
|
|
|
|
}
|
|
|
|
|
2023-01-09 04:55:37 +01:00
|
|
|
public void UpdatePosition()
|
2021-07-06 22:08:44 +02:00
|
|
|
{
|
2023-06-28 19:03:27 +02:00
|
|
|
_ = SDL_GetMouseState(out int posX, out int posY);
|
2023-01-09 04:55:37 +01:00
|
|
|
Vector2 position = new(posX, posY);
|
|
|
|
|
|
|
|
if (CurrentPosition != position)
|
2021-07-06 22:08:44 +02:00
|
|
|
{
|
2023-01-09 04:55:37 +01:00
|
|
|
CurrentPosition = position;
|
|
|
|
_lastCursorMoveTime = Stopwatch.GetTimestamp();
|
|
|
|
}
|
2021-07-06 22:08:44 +02:00
|
|
|
|
2023-01-09 04:55:37 +01:00
|
|
|
CheckIdle();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CheckIdle()
|
|
|
|
{
|
2023-05-02 03:29:47 +02:00
|
|
|
if (_hideCursorMode != HideCursorMode.OnIdle)
|
2023-01-09 04:55:37 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long cursorMoveDelta = Stopwatch.GetTimestamp() - _lastCursorMoveTime;
|
2021-07-06 22:08:44 +02:00
|
|
|
|
2023-01-09 04:55:37 +01:00
|
|
|
if (cursorMoveDelta >= CursorHideIdleTime * Stopwatch.Frequency)
|
|
|
|
{
|
|
|
|
if (!_isHidden)
|
|
|
|
{
|
2023-06-28 19:03:27 +02:00
|
|
|
if (SDL_ShowCursor(SDL_DISABLE) != SDL_DISABLE)
|
|
|
|
{
|
|
|
|
Logger.Error?.PrintMsg(LogClass.Application, "Failed to disable the cursor.");
|
|
|
|
}
|
|
|
|
|
2023-01-09 04:55:37 +01:00
|
|
|
_isHidden = true;
|
2021-07-06 22:08:44 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-09 04:55:37 +01:00
|
|
|
else
|
2021-07-06 22:08:44 +02:00
|
|
|
{
|
2023-01-09 04:55:37 +01:00
|
|
|
if (_isHidden)
|
|
|
|
{
|
2023-06-28 19:03:27 +02:00
|
|
|
if (SDL_ShowCursor(SDL_ENABLE) != SDL_ENABLE)
|
|
|
|
{
|
|
|
|
Logger.Error?.PrintMsg(LogClass.Application, "Failed to enable the cursor.");
|
|
|
|
}
|
|
|
|
|
2023-01-09 04:55:37 +01:00
|
|
|
_isHidden = false;
|
|
|
|
}
|
2021-07-06 22:08:44 +02:00
|
|
|
}
|
2023-01-09 04:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Update(SDL_Event evnt)
|
|
|
|
{
|
|
|
|
switch (evnt.type)
|
2021-07-06 22:08:44 +02:00
|
|
|
{
|
2023-01-09 04:55:37 +01:00
|
|
|
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;
|
2021-07-06 22:08:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetClientSize(int width, int height)
|
|
|
|
{
|
2023-06-28 19:03:27 +02:00
|
|
|
ClientSize = new Size(width, height);
|
2021-07-06 22:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsButtonPressed(MouseButton button)
|
|
|
|
{
|
|
|
|
return PressedButtons[(int)button];
|
|
|
|
}
|
|
|
|
|
|
|
|
public Size GetClientSize()
|
|
|
|
{
|
2023-06-28 19:03:27 +02:00
|
|
|
return ClientSize;
|
2021-07-06 22:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public string DriverName => "SDL2";
|
|
|
|
|
|
|
|
public event Action<string> OnGamepadConnected
|
|
|
|
{
|
|
|
|
add { }
|
|
|
|
remove { }
|
|
|
|
}
|
|
|
|
|
|
|
|
public event Action<string> OnGamepadDisconnected
|
|
|
|
{
|
|
|
|
add { }
|
|
|
|
remove { }
|
|
|
|
}
|
|
|
|
|
|
|
|
public ReadOnlySpan<string> GamepadsIds => new[] { "0" };
|
|
|
|
|
|
|
|
public IGamepad GetGamepad(string id)
|
|
|
|
{
|
|
|
|
return new SDL2Mouse(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (_isDisposed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isDisposed = true;
|
|
|
|
}
|
|
|
|
}
|
2023-06-28 19:03:27 +02:00
|
|
|
}
|