Browse Source

Avoid BlockLiteral based APIs for simplicity

pull/10923/head
Max Katz 4 years ago
parent
commit
cf4154505b
  1. 23
      src/iOS/Avalonia.iOS/DispatcherImpl.cs
  2. 24
      src/iOS/Avalonia.iOS/Interop.cs

23
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();
}

24
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();
}

Loading…
Cancel
Save