mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-28 22:18:25 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
1 changed files with 784842 additions and 0 deletions
108
Brutal.Sdl/Manual/Libraries/Sdl.Events.cs
Normal file
108
Brutal.Sdl/Manual/Libraries/Sdl.Events.cs
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
namespace Brutal.SdlApi;
|
||||
|
||||
public delegate bool SdlEventWatch(ref SdlEvent ev);
|
||||
|
||||
public static unsafe partial class Sdl
|
||||
{
|
||||
public static unsafe partial class Events
|
||||
{
|
||||
// Registered event watches, keyed by the managed delegate the caller passed. The GCHandle
|
||||
// roots the delegate for as long as SDL holds the native function pointer, and its IntPtr is
|
||||
// the userdata SDL threads back to the trampoline so it can dispatch to the right delegate.
|
||||
// SDL_RemoveEventWatch matches on (function pointer, userdata), so the same handle value must
|
||||
// be handed back on removal, hence the map. Guarded by a lock because SDL permits adding and
|
||||
// removing watches from any thread.
|
||||
private static readonly Dictionary<SdlEventWatch, GCHandle> Watches = new();
|
||||
private static readonly Lock WatchLock = new();
|
||||
|
||||
public static bool PollEvent(out SdlEvent ev)
|
||||
{
|
||||
ev = default;
|
||||
|
||||
bool dequeued;
|
||||
fixed (SdlEvent* p = &ev)
|
||||
dequeued = PInvoke.PollEvent(p) != 0;
|
||||
|
||||
Sdl.ExceptionAggregator.CheckExceptions();
|
||||
return dequeued;
|
||||
}
|
||||
|
||||
public static void AddEventWatch(SdlEventWatch callback)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(callback);
|
||||
|
||||
lock (WatchLock)
|
||||
{
|
||||
if (Watches.ContainsKey(callback))
|
||||
return;
|
||||
|
||||
var handle = GCHandle.Alloc(callback);
|
||||
if (PInvoke.AddEventWatch(&EventWatchTrampoline, (void*)GCHandle.ToIntPtr(handle)) == 0)
|
||||
{
|
||||
handle.Free();
|
||||
throw SdlException.FromError("SDL_AddEventWatch failed");
|
||||
}
|
||||
|
||||
Watches[callback] = handle;
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveEventWatch(SdlEventWatch callback)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(callback);
|
||||
|
||||
lock (WatchLock)
|
||||
{
|
||||
if (!Watches.Remove(callback, out var handle))
|
||||
return;
|
||||
|
||||
PInvoke.RemoveEventWatch(&EventWatchTrampoline, (void*)GCHandle.ToIntPtr(handle));
|
||||
handle.Free();
|
||||
}
|
||||
}
|
||||
|
||||
// Called from Sdl.Init.Quit so the managed map does not outlive SDL's own watch list.
|
||||
internal static void ClearWatches()
|
||||
{
|
||||
lock (WatchLock)
|
||||
{
|
||||
foreach (var (_, handle) in Watches)
|
||||
{
|
||||
// Remove the native registration first (still valid pre-SDL_Quit), then release
|
||||
// the managed root. Both are matched on (function pointer, userdata).
|
||||
PInvoke.RemoveEventWatch(&EventWatchTrampoline, (void*)GCHandle.ToIntPtr(handle));
|
||||
handle.Free();
|
||||
}
|
||||
|
||||
Watches.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
// The single native entry point for every registered watch. SDL passes the GCHandle IntPtr
|
||||
// back as userdata; we resolve it to the managed delegate and invoke it inside a try/catch so
|
||||
// no exception ever escapes into native code. Failures are parked in the aggregator and
|
||||
// rethrown from the next PollEvent.
|
||||
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
private static byte EventWatchTrampoline(void* userdata, SdlEvent* ev)
|
||||
{
|
||||
try
|
||||
{
|
||||
var handle = GCHandle.FromIntPtr((nint)userdata);
|
||||
if (handle.Target is SdlEventWatch filter)
|
||||
return filter(ref *ev) ? (byte)1 : (byte)0;
|
||||
|
||||
Sdl.ExceptionAggregator.Push(
|
||||
"An SDL event watch fired with a dead or foreign GCHandle (its target was not an "
|
||||
+ "SdlEventWatch); the callback was skipped."
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Sdl.ExceptionAggregator.Push("An exception occurred in an SDL event watch callback.", ex);
|
||||
}
|
||||
|
||||
// Event watches ignore the return value; permit by default.
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue