Skip to content

Commit

Permalink
Merge pull request #6389 from hwsmm/sdl3-bindings-update
Browse files Browse the repository at this point in the history
Update SDL3-CS
  • Loading branch information
smoogipoo authored Oct 17, 2024
2 parents db96651 + 10ad2e7 commit d56a920
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion osu.Framework.Android/osu.Framework.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ProjectReference Include="..\osu.Framework\osu.Framework.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ppy.SDL3-CS.Android" Version="2024.916.0" />
<PackageReference Include="ppy.SDL3-CS.Android" Version="2024.1015.0" />
<PackageReference Include="Xamarin.AndroidX.Window" Version="1.2.0.1" PrivateAssets="compile" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion osu.Framework.iOS/IOSWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override unsafe void RunMainLoop()
// frame rate with multi-threaded mode turned on, but it is going to give them worse input latency
// and higher power usage.

SDL_SetiOSEventPump(SDL_bool.SDL_FALSE);
SDL_SetiOSEventPump(false);
SDL_SetiOSAnimationCallback(SDLWindowHandle, 1, &runFrame, ObjectHandle.Handle);
}

Expand Down
7 changes: 3 additions & 4 deletions osu.Framework/Platform/SDL3/SDL3Clipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using System.Text;
using osu.Framework.Allocation;
using osu.Framework.Logging;
using SDL;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using static SDL.SDL3;
Expand Down Expand Up @@ -41,7 +40,7 @@ public SDL3Clipboard(IImageFormat imageFormat)
// SDL cannot differentiate between string.Empty and no text (eg. empty clipboard or an image)
// doesn't matter as text editors don't really allow copying empty strings.
// assume that empty text means no text.
public override string? GetText() => SDL_HasClipboardText() == SDL_bool.SDL_TRUE ? SDL_GetClipboardText() : null;
public override string? GetText() => SDL_HasClipboardText() ? SDL_GetClipboardText() : null;

public override void SetText(string text) => SDL_SetClipboardText(text);

Expand Down Expand Up @@ -86,7 +85,7 @@ public override bool SetImage(Image image)

private static unsafe bool tryGetData<T>(string mimeType, SpanDecoder<T> decoder, out T? data)
{
if (SDL_HasClipboardData(mimeType) == SDL_bool.SDL_FALSE)
if (!SDL_HasClipboardData(mimeType))
{
data = default;
return false;
Expand Down Expand Up @@ -128,7 +127,7 @@ private static unsafe bool trySetData(string mimeType, Func<ReadOnlyMemory<byte>
// TODO: support multiple mime types in a single callback
fixed (byte* ptr = Encoding.UTF8.GetBytes(mimeType + '\0'))
{
if (SDL_SetClipboardData(&dataCallback, &cleanupCallback, objectHandle.Handle, &ptr, 1) == SDL_bool.SDL_FALSE)
if (!SDL_SetClipboardData(&dataCallback, &cleanupCallback, objectHandle.Handle, &ptr, 1))
{
objectHandle.Dispose();
Logger.Log($"Failed to set clipboard data callback. SDL error: {SDL_GetError()}");
Expand Down
5 changes: 2 additions & 3 deletions osu.Framework/Platform/SDL3/SDL3DesktopWindow.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using SDL;
using static SDL.SDL3;

namespace osu.Framework.Platform.SDL3
Expand All @@ -18,8 +17,8 @@ protected override unsafe void UpdateWindowStateAndSize(WindowState state, Displ
// this reset is required even on changing from one fullscreen resolution to another.
// if it is not included, the GL context will not get the correct size.
// this is mentioned by multiple sources as an SDL issue, which seems to resolve by similar means (see https://discourse.libsdl.org/t/sdl-setwindowsize-does-not-work-in-fullscreen/20711/4).
SDL_SetWindowBordered(SDLWindowHandle, SDL_bool.SDL_TRUE);
SDL_SetWindowFullscreen(SDLWindowHandle, SDL_bool.SDL_FALSE);
SDL_SetWindowBordered(SDLWindowHandle, true);
SDL_SetWindowFullscreen(SDLWindowHandle, false);
SDL_RestoreWindow(SDLWindowHandle);

base.UpdateWindowStateAndSize(state, display, displayMode);
Expand Down
3 changes: 1 addition & 2 deletions osu.Framework/Platform/SDL3/SDL3MobileWindow.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using SDL;
using static SDL.SDL3;

namespace osu.Framework.Platform.SDL3
Expand All @@ -16,7 +15,7 @@ public SDL3MobileWindow(GraphicsSurfaceType surfaceType, string appName)
protected override unsafe void UpdateWindowStateAndSize(WindowState state, Display display, DisplayMode displayMode)
{
// This sets the status bar to hidden.
SDL_SetWindowFullscreen(SDLWindowHandle, SDL_bool.SDL_TRUE);
SDL_SetWindowFullscreen(SDLWindowHandle, true);

// Don't run base logic at all. Let's keep things simple.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SDL3ReadableKeyCombinationProvider : ReadableKeyCombinationProvider
private static SDL_Keycode getKeyFromScancode(SDL_Scancode scancode, SDL_Keymod modstate)
{
if (FrameworkEnvironment.UseSDL3)
return SDL_GetKeyFromScancode(scancode, modstate, SDL_FALSE); // third parameter is not useful unless SDL_HINT_KEYCODE_OPTIONS is set
return SDL_GetKeyFromScancode(scancode, modstate, false); // third parameter is not useful unless SDL_HINT_KEYCODE_OPTIONS is set

return (SDL_Keycode)global::SDL2.SDL.SDL_GetKeyFromScancode((global::SDL2.SDL.SDL_Scancode)scancode);
}
Expand Down
15 changes: 8 additions & 7 deletions osu.Framework/Platform/SDL3/SDL3Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected SDL3Window(GraphicsSurfaceType surfaceType, string appName)

SDL_SetHint(SDL_HINT_APP_NAME, appName);

if (SDL_Init(SDL_InitFlags.SDL_INIT_VIDEO | SDL_InitFlags.SDL_INIT_GAMEPAD) == SDL_bool.SDL_FALSE)
if (!SDL_Init(SDL_InitFlags.SDL_INIT_VIDEO | SDL_InitFlags.SDL_INIT_GAMEPAD))
{
throw new InvalidOperationException($"Failed to initialise SDL: {SDL_GetError()}");
}
Expand All @@ -182,10 +182,11 @@ protected SDL3Window(GraphicsSurfaceType surfaceType, string appName)
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static void logOutput(IntPtr _, SDL_LogCategory category, SDL_LogPriority priority, byte* messagePtr)
private static void logOutput(IntPtr _, int category, SDL_LogPriority priority, byte* messagePtr)
{
SDL_LogCategory categoryEnum = (SDL_LogCategory)category;
string? message = PtrToStringUTF8(messagePtr);
Logger.Log($@"SDL {category.ReadableName()} log [{priority.ReadableName()}]: {message}");
Logger.Log($@"SDL {categoryEnum.ReadableName()} log [{priority.ReadableName()}]: {message}");
}

public void SetupWindow(FrameworkConfigManager config)
Expand Down Expand Up @@ -320,23 +321,23 @@ protected void HandleEventFromWatch(SDL_Event evt)
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static SDL_bool eventFilter(IntPtr userdata, SDL_Event* eventPtr)
private static SDLBool eventFilter(IntPtr userdata, SDL_Event* eventPtr)
{
var handle = new ObjectHandle<SDL3Window>(userdata);
if (handle.GetTarget(out SDL3Window window))
window.HandleEventFromFilter(*eventPtr);

return SDL_bool.SDL_TRUE;
return true;
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static SDL_bool eventWatch(IntPtr userdata, SDL_Event* eventPtr)
private static SDLBool eventWatch(IntPtr userdata, SDL_Event* eventPtr)
{
var handle = new ObjectHandle<SDL3Window>(userdata);
if (handle.GetTarget(out SDL3Window window))
window.HandleEventFromWatch(*eventPtr);

return SDL_bool.SDL_TRUE;
return true;
}

private bool firstDraw = true;
Expand Down
14 changes: 7 additions & 7 deletions osu.Framework/Platform/SDL3/SDL3Window_Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public bool RelativeMouseMode
throw new InvalidOperationException($"Cannot set {nameof(RelativeMouseMode)} to true when the cursor is not hidden via {nameof(CursorState)}.");

relativeMouseMode = value;
ScheduleCommand(() => SDL_SetWindowRelativeMouseMode(SDLWindowHandle, value ? SDL_bool.SDL_TRUE : SDL_bool.SDL_FALSE));
ScheduleCommand(() => SDL_SetWindowRelativeMouseMode(SDLWindowHandle, value));
updateCursorConfinement();
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ private void updateCursorConfinement()
{
bool confined = CursorState.HasFlagFast(CursorState.Confined);

ScheduleCommand(() => SDL_SetWindowMouseGrab(SDLWindowHandle, confined ? SDL_bool.SDL_TRUE : SDL_bool.SDL_FALSE));
ScheduleCommand(() => SDL_SetWindowMouseGrab(SDLWindowHandle, confined));

// Don't use SDL_SetWindowMouseRect when relative mode is enabled, as relative mode already confines the OS cursor to the window.
// This is fine for our use case, as UserInputManager will clamp the mouse position.
Expand Down Expand Up @@ -336,7 +336,7 @@ private void addJoystick(SDL_JoystickID instanceID)
SDL_Joystick* joystick = SDL_OpenJoystick(instanceID);

SDL_Gamepad* controller = null;
if (SDL_IsGamepad(instanceID) == SDL_bool.SDL_TRUE)
if (SDL_IsGamepad(instanceID))
controller = SDL_OpenGamepad(instanceID);

controllers[instanceID] = new SDL3ControllerBindings(joystick, controller);
Expand Down Expand Up @@ -466,7 +466,7 @@ private void handleMouseButtonEvent(SDL_MouseButtonEvent evtButton)

private void handleMouseMotionEvent(SDL_MouseMotionEvent evtMotion)
{
if (SDL_GetWindowRelativeMouseMode(SDLWindowHandle) == SDL_bool.SDL_FALSE)
if (!SDL_GetWindowRelativeMouseMode(SDLWindowHandle))
MouseMove?.Invoke(new Vector2(evtMotion.x * Scale, evtMotion.y * Scale));
else
MouseMoveRelative?.Invoke(new Vector2(evtMotion.xrel * Scale, evtMotion.yrel * Scale));
Expand Down Expand Up @@ -517,15 +517,15 @@ private void handlePenMotionEvent(SDL_PenMotionEvent evtPenMotion)

private void handlePenTouchEvent(SDL_PenTouchEvent evtPenTouch)
{
if (evtPenTouch.eraser == SDL_bool.SDL_TRUE)
if (evtPenTouch.eraser)
return;

handlePenPressEvent(0, evtPenTouch.down == SDL_bool.SDL_TRUE);
handlePenPressEvent(0, evtPenTouch.down);
}

private void handlePenButtonEvent(SDL_PenButtonEvent evtPenButton)
{
handlePenPressEvent(evtPenButton.button, evtPenButton.down == SDL_bool.SDL_TRUE);
handlePenPressEvent(evtPenButton.button, evtPenButton.down);
}

private void handlePenPressEvent(byte penButton, bool pressed)
Expand Down
12 changes: 6 additions & 6 deletions osu.Framework/Platform/SDL3/SDL3Window_Windowing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public unsafe bool Resizable
return;

resizable = value;
ScheduleCommand(() => SDL_SetWindowResizable(SDLWindowHandle, value ? SDL_bool.SDL_TRUE : SDL_bool.SDL_FALSE));
ScheduleCommand(() => SDL_SetWindowResizable(SDLWindowHandle, value));
}
}

Expand Down Expand Up @@ -356,7 +356,7 @@ private static unsafe bool tryGetDisplayFromSDL(int displayIndex, SDL_DisplayID

SDL_Rect rect;

if (SDL_GetDisplayBounds(displayID, &rect) == SDL_bool.SDL_FALSE)
if (!SDL_GetDisplayBounds(displayID, &rect))
{
Logger.Log($"Failed to get display bounds for display at index ({displayIndex}). SDL Error: {SDL_GetError()}");
display = null;
Expand Down Expand Up @@ -644,7 +644,7 @@ protected virtual unsafe void UpdateWindowStateAndSize(WindowState state, Displa

SDL_RestoreWindow(SDLWindowHandle);
SDL_SetWindowSize(SDLWindowHandle, Size.Width, Size.Height);
SDL_SetWindowResizable(SDLWindowHandle, Resizable ? SDL_bool.SDL_TRUE : SDL_bool.SDL_FALSE);
SDL_SetWindowResizable(SDLWindowHandle, Resizable);

readWindowPositionFromConfig(state, display);
break;
Expand All @@ -657,7 +657,7 @@ protected virtual unsafe void UpdateWindowStateAndSize(WindowState state, Displa
ensureWindowOnDisplay(display);

SDL_SetWindowFullscreenMode(SDLWindowHandle, &closestMode);
SDL_SetWindowFullscreen(SDLWindowHandle, SDL_bool.SDL_TRUE);
SDL_SetWindowFullscreen(SDLWindowHandle, true);
break;

case WindowState.FullscreenBorderless:
Expand Down Expand Up @@ -874,14 +874,14 @@ private static unsafe SDL_DisplayMode getClosestDisplayMode(SDL_Window* windowHa

SDL_DisplayMode mode;

if (SDL_GetClosestFullscreenDisplayMode(displayID, size.Width, size.Height, requestedMode.RefreshRate, SDL_bool.SDL_TRUE, &mode) == SDL_bool.SDL_TRUE)
if (SDL_GetClosestFullscreenDisplayMode(displayID, size.Width, size.Height, requestedMode.RefreshRate, true, &mode))
return mode;

Logger.Log(
$"Unable to get preferred display mode (try #1/2). Target display: {display.Index}, mode: {size.Width}x{size.Height}@{requestedMode.RefreshRate}. SDL error: {SDL3Extensions.GetAndClearError()}");

// fallback to current display's native bounds
if (SDL_GetClosestFullscreenDisplayMode(displayID, display.Bounds.Width, display.Bounds.Height, 0f, SDL_bool.SDL_TRUE, &mode) == SDL_bool.SDL_TRUE)
if (SDL_GetClosestFullscreenDisplayMode(displayID, display.Bounds.Width, display.Bounds.Height, 0f, true, &mode))
return mode;

Logger.Log(
Expand Down
10 changes: 5 additions & 5 deletions osu.Framework/Platform/Windows/SDL3WindowsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ public override unsafe void Run()
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static unsafe SDL_bool messageHook(IntPtr userdata, MSG* msg)
private static unsafe SDLBool messageHook(IntPtr userdata, MSG* msg)
{
var handle = new ObjectHandle<SDL3WindowsWindow>(userdata);
if (handle.GetTarget(out SDL3WindowsWindow window))
return window.handleEventFromHook(*msg);

return SDL_bool.SDL_TRUE;
return true;
}

private SDL_bool handleEventFromHook(MSG msg)
private SDLBool handleEventFromHook(MSG msg)
{
switch (msg.message)
{
Expand All @@ -89,7 +89,7 @@ private SDL_bool handleEventFromHook(MSG msg)
break;
}

return SDL_bool.SDL_TRUE;
return true;
}

protected override void HandleEventFromFilter(SDL_Event evt)
Expand Down Expand Up @@ -259,7 +259,7 @@ protected set

protected override unsafe Size SetBorderless(Display display)
{
SDL_SetWindowBordered(SDLWindowHandle, SDL_bool.SDL_FALSE);
SDL_SetWindowBordered(SDLWindowHandle, false);

var newSize = display.Bounds.Size;

Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/osu.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<PackageReference Include="ppy.osuTK.NS20" Version="1.0.211" />
<PackageReference Include="StbiSharp" Version="1.1.0" />
<PackageReference Include="ppy.SDL2-CS" Version="1.0.741-alpha" />
<PackageReference Include="ppy.SDL3-CS" Version="2024.916.0" />
<PackageReference Include="ppy.SDL3-CS" Version="2024.1015.0" />
<PackageReference Include="ppy.osu.Framework.SourceGeneration" Version="2023.720.0" />

<!-- DO NOT use ProjectReference for native packaging project.
Expand Down

0 comments on commit d56a920

Please sign in to comment.