Skip to content

Commit

Permalink
Add GamepadEvents definitions
Browse files Browse the repository at this point in the history
Use ulPlatformSetFontLoader
Update default config values
  • Loading branch information
SupinePandora43 committed Jul 14, 2023
1 parent 994bc08 commit 680ccf1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 20 deletions.
31 changes: 31 additions & 0 deletions src/UltralightNet/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UltralightNet.LowStuff;
using UltralightNet.Gamepad;

namespace UltralightNet;

Expand Down Expand Up @@ -38,6 +39,24 @@ public static Handle<Renderer> ulCreateRenderer(in ULConfig config)

[DllImport(LibUltralight)]
public static extern void ulLogMemoryUsage(Handle<Renderer> renderer);

[LibraryImport(LibUltralight)]
public static extern bool ulStartRemoteInspectorServer(Handle<Renderer> renderer, ReadOnlySpan<byte> address, ushort port);

[LibraryImport(LibUltralight)]
public static extern bool ulStartRemoteInspectorServer(Handle<Renderer> renderer, [MarshalAs(UnmanagedType.LPStr)]ReadOnlySpan<char> address, ushort port);

[DllImport(LibUltralight)]
public static extern void ulSetGamepadDetails(Handle<Renderer> renderer, uint index, ULString id, uint axisCount, uint buttonCount);

[DllImport(LibUltralight)]
public static extern void ulFireGamepadEvent(Handle<Renderer> renderer, GamepadEvent gamepadEvent);

[DllImport(LibUltralight)]
public static extern void ulFireGamepadAxisEvent(Handle<Renderer> renderer, GamepadAxisEvent gamepadAxisEvent);

[DllImport(LibUltralight)]
public static extern void ulFireGamepadButtonEvent(Handle<Renderer> renderer, GamepadButtonEvent gamepadButtonEvent);
}

public unsafe class Renderer : INativeContainer<Renderer>, INativeContainerInterface<Renderer>, IEquatable<Renderer>
Expand Down Expand Up @@ -102,6 +121,18 @@ public Session DefaultSession
public void PurgeMemory() { Methods.ulPurgeMemory(Handle); GC.KeepAlive(this); }
public void LogMemoryUsage() { Methods.ulLogMemoryUsage(Handle); GC.KeepAlive(this); }

public void StartRemoteInspectorServer(ReadOnlySpan<byte> address, ushort port){
throw new NotImplementedException();
bool result = Methods.ulStartRemoteInspectorServer(Handle, address, port);

Check warning on line 126 in src/UltralightNet/Renderer.cs

View workflow job for this annotation

GitHub Actions / Test (macos)

Unreachable code detected
GC.KeepAlive(this);
if(!result) throw new System.Net.WebException("Failed to start remote inspector server.");
}

public void SetGamepadDetails(uint index, ReadOnlySpan<char> id, uint axisCount, uint buttonCount) => throw new NotImplementedException();
public void FireGamepadEvent(GamepadEvent gamepadEvent) => throw new NotImplementedException();
public void FireGamepadAxisEvent(GamepadAxisEvent gamepadAxisEvent) => throw new NotImplementedException();
public void FireGamepadButtonEvent(GamepadButtonEvent gamepadbuttonEvent) => throw new NotImplementedException();

[SuppressMessage("Usage", "CA1816: Call GC.SupressFinalize correctly")]
public override void Dispose()
{
Expand Down
3 changes: 1 addition & 2 deletions src/UltralightNet/Structs/ULFontLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
namespace UltralightNet;
Expand Down Expand Up @@ -101,7 +100,7 @@ readonly get
public delegate* unmanaged[Cdecl]<ULString*, int, bool, ULString*> __GetFallbackFontForCharacters;
public delegate* unmanaged[Cdecl]<ULString*, int, bool, ULFontFile> __Load;

public void Dispose() => throw new NotImplementedException();
public void Dispose() => ULPlatform.Free(in this);

#pragma warning disable CS8909
public readonly bool Equals(ULFontLoader other) => __GetFallbackFont == other.__GetFallbackFont && __GetFallbackFontForCharacters == other.__GetFallbackFontForCharacters && __Load == other.__Load;
Expand Down
34 changes: 34 additions & 0 deletions src/UltralightNet/Structs/ULGamepadEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace UltralightNet.Gamepad;

/// <summary>The various <see cref="GamepadEvent" /> types.</summary>
public enum GamepadEventType : int
{
/// <summary>This event type should be fired when a gamepad is connected.</summary>
/// <remarks>You will need to previously declare the gamepad, its index, and details about
/// its axis and button layout via <see cref="Renderer.SetGamepadDetails(uint, ReadOnlySpan{char}, uint, uint)" /> prior to calling
/// <see cref="Renderer.FireGamepadEvent(GamepadEvent)" />.
/// </remarks>
Connected,
/// <summary>This event type should be fired when a gamepad is disconnected.</summary>
Disconnected
}

/// <summary>Event representing a change in gamepad connection state</summary>
/// <param name="Type">The type of this GamepadEvent.</param>
/// <param name="Index">The index of the gamepad.</param>
/// <see cref="Renderer.FireGamepadEvent(GamepadEvent)" />
public record struct GamepadEvent(GamepadEventType Type, uint Index);

/// <summary>Event representing a change in gamepad axis state (eg, pressing a stick in a certain direction).</summary>
/// <param name="Index">The index of the gamepad.</param>
/// <param name="Axis">The index of the axis whose value has changed.</param>
/// <param name="Value">The new value of the axis.<br /><remarks>This value should be normalized to the range [-1.0, 1.0].</remarks></param>
/// <see cref="Renderer.FireGamepadAxisEvent(GamepadAxisEvent)" />
public record struct GamepadAxisEvent(uint Index, uint Axis, double Value);

/// <summary>Event representing a change in gamepad button state (eg, pressing a button on a gamepad).</summary>
/// <param name="Index">The index of the gamepad.</param>
/// <param name="Axis">The index of the button whose value has changed.</param>
/// <param name="Value">The new value of the button.<br /><remarks>This value should be normalized to the range [-1.0, 1.0], with any value greater than 0.0 to be considered "pressed".</remarks></param>
/// <see cref="Renderer.FireGamepadButtonEvent(GamepadButtonEvent)" />
public record struct GamepadButtonEvent(uint Index, uint Button, double Value);
6 changes: 3 additions & 3 deletions src/UltralightNet/ULConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ public unsafe struct _ULConfig : IDisposable

/// <summary>The max amount of time (in seconds) to allow <see cref="Renderer.Update()"/> to run per call.</summary>
/// <remarks>The library will attempt to throttle timers and/or reschedule work if this time budget is exceeded.</remarks>
public double MaxUpdateTime = 1.0 / 100.0;
public double MaxUpdateTime = 1.0 / 200.0;

/// <summary>The alignment (in bytes) of the BitmapSurface when using the CPU renderer.</summary>
/// <remarks>You can set this to '0' to perform no padding (row_bytes will always be width * 4) at a slight cost to performance.</remarks>
/// <remarks>You can set this to '0' to perform no padding (row_bytes will always be width * Bpp) at a slight cost to performance.</remarks>
public uint BitmapAlignment = 16;

/// <example>
Expand Down Expand Up @@ -271,7 +271,7 @@ public struct ULConfig : IEquatable<ULConfig>
public uint NumRendererThreads = 0;

/// <inheritdoc cref="_ULConfig.MaxUpdateTime" />
public double MaxUpdateTime = 1.0 / 100.0;
public double MaxUpdateTime = 1.0 / 200.0;

/// <inheritdoc cref="_ULConfig.BitmapAlignment" />
public uint BitmapAlignment = 16;
Expand Down
51 changes: 36 additions & 15 deletions src/UltralightNet/ULPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public static partial class Methods
[DllImport("Ultralight", EntryPoint = "ulPlatformSetFileSystem")]
public static extern void ulPlatformSetFileSystem(ULFileSystem file_system);

[DllImport("Ultralight", EntryPoint = "ulPlatformSetFontLoader")]
public static extern void ulPlatformSetFontLoader(ULFontLoader fontLoader);

[DllImport("Ultralight", EntryPoint = "ulPlatformSetGPUDriver")]
public static extern void ulPlatformSetGPUDriver(ULGPUDriver gpu_driver);

Expand Down Expand Up @@ -113,6 +116,11 @@ internal static void Free(in ULFileSystem filesystem)
if (filesystemHandles.Remove(filesystem, out List<GCHandle>? handles))
foreach (GCHandle handle in handles!) handle.Free();
}
internal static void Free(in ULFontLoader fontLoader)
{
if (fontloaderHandles.Remove(fontLoader, out List<GCHandle>? handles))
foreach (GCHandle handle in handles!) handle.Free();
}
internal static void Free(in ULGPUDriver gpudriver)
{
if (gpudriverHandles.Remove(gpudriver, out List<GCHandle>? handles))
Expand All @@ -135,21 +143,24 @@ public static void Free()
lock (loggerHandles)
lock (clipboardHandles)
lock (filesystemHandles)
lock (gpudriverHandles)
lock (surfaceHandles)
{
foreach (List<GCHandle> handles in loggerHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();
foreach (List<GCHandle> handles in clipboardHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();
foreach (List<GCHandle> handles in filesystemHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();
foreach (List<GCHandle> handles in gpudriverHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();
foreach (List<GCHandle> handles in surfaceHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();

loggerHandles.Clear();
clipboardHandles.Clear();
filesystemHandles.Clear();
gpudriverHandles.Clear();
surfaceHandles.Clear();
}
lock (fontloaderHandles)
lock (gpudriverHandles)
lock (surfaceHandles)
{
foreach (List<GCHandle> handles in loggerHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();
foreach (List<GCHandle> handles in clipboardHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();
foreach (List<GCHandle> handles in filesystemHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();
foreach (List<GCHandle> handles in fontloaderHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();
foreach (List<GCHandle> handles in gpudriverHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();
foreach (List<GCHandle> handles in surfaceHandles.Values) foreach (GCHandle handle in handles) if (handle.IsAllocated) handle.Free();

loggerHandles.Clear();
clipboardHandles.Clear();
filesystemHandles.Clear();
fontloaderHandles.Clear();
gpudriverHandles.Clear();
surfaceHandles.Clear();
}
}

public static bool SetDefaultLogger { get; set; } = true;
Expand All @@ -166,6 +177,7 @@ public static void Free()

private static ULLogger _logger;
internal static ULFileSystem _filesystem;
private static ULFontLoader _fontLoader;
private static ULGPUDriver _gpudriver;
private static ULClipboard _clipboard;

Expand All @@ -192,6 +204,15 @@ public static ULFileSystem FileSystem
Methods.ulPlatformSetFileSystem(value);
}
}
public static ULFontLoader FontLoader
{
get => _fontLoader;
set
{
_fontLoader = value;
Methods.ulPlatformSetFontLoader(value);
}
}
public static ULGPUDriver GPUDriver
{
get => _gpudriver;
Expand Down

0 comments on commit 680ccf1

Please sign in to comment.