Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async mouse and keyboard input on windows #6235

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions osu.Framework/Platform/SDL3Window.cs
Expand Up @@ -266,14 +266,26 @@ protected void RunFrame()
Update?.Invoke();
}

/// <summary>
/// Drop event from SDL event queue.
/// </summary>
/// <remarks>Return value of <see cref="HandleEventFromFilter"/></remarks>
protected const int DROP_EVENT = 0;

/// <summary>
/// Keep event in SDL event queue (it'll get processed later in <see cref="pollSDLEvents"/>).
/// </summary>
/// <remarks>Return value of <see cref="HandleEventFromFilter"/></remarks>
protected const int KEEP_EVENT = 1;

/// <summary>
/// Handles <see cref="SDL_Event"/>s fired from the SDL event filter.
/// </summary>
/// <remarks>
/// As per SDL's recommendation, application events should always be handled via the event filter.
/// See: https://wiki.libsdl.org/SDL3/SDL_EventType#android_ios_and_winrt_events
/// </remarks>
protected virtual void HandleEventFromFilter(SDL_Event evt)
protected virtual int HandleEventFromFilter(SDL_Event evt)
{
switch (evt.type)
{
Expand All @@ -293,6 +305,8 @@ protected virtual void HandleEventFromFilter(SDL_Event evt)
LowOnMemory?.Invoke();
break;
}

return KEEP_EVENT;
}

protected void HandleEventFromWatch(SDL_Event evt)
Expand All @@ -313,9 +327,9 @@ private static int eventFilter(IntPtr userdata, SDL_Event* eventPtr)
{
var handle = new ObjectHandle<SDL3Window>(userdata);
if (handle.GetTarget(out SDL3Window window))
window.HandleEventFromFilter(*eventPtr);
return window.HandleEventFromFilter(*eventPtr);

return 1;
return KEEP_EVENT;
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand Down
19 changes: 17 additions & 2 deletions osu.Framework/Platform/Windows/WindowsWindow.cs
Expand Up @@ -91,16 +91,31 @@ private SDL_bool handleEventFromHook(MSG msg)
return SDL_bool.SDL_TRUE;
}

protected override void HandleEventFromFilter(SDL_Event evt)
protected override int HandleEventFromFilter(SDL_Event evt)
{
switch (evt.type)
{
// handle raw mouse and keyboard events in SDL_EventFilter for lower latency and resilience against SDL_PumpEvents() lag
case SDL_EventType.SDL_EVENT_MOUSE_MOTION:
case SDL_EventType.SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EventType.SDL_EVENT_MOUSE_BUTTON_UP:
case SDL_EventType.SDL_EVENT_MOUSE_WHEEL:
if (SDL3.SDL_GetRelativeMouseMode() != SDL_bool.SDL_TRUE) break;

HandleEvent(evt);
return DROP_EVENT;

case SDL_EventType.SDL_EVENT_KEY_DOWN:
case SDL_EventType.SDL_EVENT_KEY_UP:
HandleEvent(evt);
return DROP_EVENT;

case SDL_EventType.SDL_EVENT_WINDOW_FOCUS_LOST:
warpCursorFromFocusLoss();
break;
}

base.HandleEventFromFilter(evt);
return base.HandleEventFromFilter(evt);
}

/// <summary>
Expand Down