From cf4154505bf2803201c3335197c7fe138420bb54 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Wed, 5 Apr 2023 22:55:24 -0400 Subject: [PATCH] Avoid BlockLiteral based APIs for simplicity --- src/iOS/Avalonia.iOS/DispatcherImpl.cs | 23 +++++++++-------------- src/iOS/Avalonia.iOS/Interop.cs | 24 ++++++++++++++++-------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/iOS/Avalonia.iOS/DispatcherImpl.cs b/src/iOS/Avalonia.iOS/DispatcherImpl.cs index 0c11aa473b..8b3c747b5a 100644 --- a/src/iOS/Avalonia.iOS/DispatcherImpl.cs +++ b/src/iOS/Avalonia.iOS/DispatcherImpl.cs @@ -34,21 +34,16 @@ internal class DispatcherImpl : IDispatcherImplWithExplicitBackgroundProcessing // This is needed to wakeup the loop if we are called from inside of BeforeWait hook }; - var observerBlock = new BlockLiteral(); - observerBlock.SetupBlock((Interop.CFRunLoopObserverCallback)ObserverCallback, null); - var observer = Interop.CFRunLoopObserverCreateWithHandler(IntPtr.Zero, + var observer = Interop.CFRunLoopObserverCreate(IntPtr.Zero, Interop.CFOptionFlags.kCFRunLoopAfterWaiting | Interop.CFOptionFlags.kCFRunLoopBeforeSources | Interop.CFOptionFlags.kCFRunLoopBeforeWaiting, - true, - 0, - ref observerBlock); - Interop.CFRunLoopAddObserver(CFRunLoop.Main.Handle, observer, Interop.kCFRunLoopCommonModes); - - var timerBlock = new BlockLiteral(); - timerBlock.SetupBlock((Interop.CFRunLoopTimerCallback)TimerCallback, null); - _timer = Interop.CFRunLoopTimerCreateWithHandler(IntPtr.Zero, + true, 0, ObserverCallback, IntPtr.Zero); + Interop.CFRunLoopAddObserver(Interop.CFRunLoopGetMain(), observer, Interop.kCFRunLoopCommonModes); + + _timer = Interop.CFRunLoopTimerCreate(IntPtr.Zero, Interop.CFAbsoluteTimeGetCurrent() + DistantFutureInterval, - DistantFutureInterval, 0, 0, ref timerBlock); + DistantFutureInterval, 0, 0, TimerCallback, IntPtr.Zero); + Interop.CFRunLoopAddTimer(Interop.CFRunLoopGetMain(), _timer, Interop.kCFRunLoopCommonModes); } public event Action? Signaled; @@ -115,7 +110,7 @@ internal class DispatcherImpl : IDispatcherImplWithExplicitBackgroundProcessing } [MonoPInvokeCallback(typeof(Interop.CFRunLoopObserverCallback))] - private static void ObserverCallback(IntPtr observer, Interop.CFOptionFlags activity) + private static void ObserverCallback(IntPtr observer, Interop.CFOptionFlags activity, IntPtr info) { if (activity == Interop.CFOptionFlags.kCFRunLoopBeforeWaiting) { @@ -133,7 +128,7 @@ internal class DispatcherImpl : IDispatcherImplWithExplicitBackgroundProcessing } [MonoPInvokeCallback(typeof(Interop.CFRunLoopTimerCallback))] - private static void TimerCallback(IntPtr timer) + private static void TimerCallback(IntPtr timer, IntPtr info) { Instance.Timer?.Invoke(); } diff --git a/src/iOS/Avalonia.iOS/Interop.cs b/src/iOS/Avalonia.iOS/Interop.cs index 2758faa2f1..c0b3506936 100644 --- a/src/iOS/Avalonia.iOS/Interop.cs +++ b/src/iOS/Avalonia.iOS/Interop.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using CoreFoundation; using Foundation; @@ -13,7 +14,7 @@ internal class Interop internal static NativeHandle kCFRunLoopCommonModes = CFString.CreateNative("kCFRunLoopCommonModes"); [Flags] - public enum CFOptionFlags : ulong + internal enum CFOptionFlags : ulong { kCFRunLoopBeforeSources = (1UL << 2), kCFRunLoopAfterWaiting = (1UL << 6), @@ -21,27 +22,34 @@ internal class Interop } [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate void CFRunLoopObserverCallback(IntPtr observer, CFOptionFlags activity); - + internal delegate void CFRunLoopObserverCallback(IntPtr observer, CFOptionFlags activity, IntPtr info); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate void CFRunLoopTimerCallback(IntPtr timer); + internal delegate void CFRunLoopTimerCallback(IntPtr timer, IntPtr info); + + [DllImport(CoreFoundationLibrary)] + internal static extern IntPtr CFRunLoopGetMain(); [DllImport(CoreFoundationLibrary)] - internal static extern IntPtr CFRunLoopObserverCreateWithHandler(IntPtr allocator, CFOptionFlags activities, bool repeats, int index, ref BlockLiteral block); + internal static extern IntPtr CFRunLoopObserverCreate(IntPtr allocator, CFOptionFlags activities, + bool repeats, int index, CFRunLoopObserverCallback callout, IntPtr context); [DllImport(CoreFoundationLibrary)] internal static extern IntPtr CFRunLoopAddObserver(IntPtr loop, IntPtr observer, IntPtr mode); [DllImport(CoreFoundationLibrary)] - internal static extern IntPtr CFRunLoopTimerCreateWithHandler(IntPtr allocator, double firstDate, double interval, - CFOptionFlags flags, int order, ref BlockLiteral block); + internal static extern IntPtr CFRunLoopTimerCreate(IntPtr allocator, double firstDate, double interval, + CFOptionFlags flags, int order, CFRunLoopTimerCallback callout, IntPtr context); [DllImport(CoreFoundationLibrary)] internal static extern void CFRunLoopTimerSetTolerance(IntPtr timer, double tolerance); [DllImport(CoreFoundationLibrary)] internal static extern void CFRunLoopTimerSetNextFireDate(IntPtr timer, double fireDate); - + + [DllImport(CoreFoundationLibrary)] + internal static extern void CFRunLoopAddTimer(IntPtr loop, IntPtr timer, IntPtr mode); + [DllImport(CoreFoundationLibrary)] internal static extern double CFAbsoluteTimeGetCurrent(); }