Skip to content

Commit

Permalink
Don't use GLib helper functions introduced in 2.74 (#17392)
Browse files Browse the repository at this point in the history
  • Loading branch information
kekekeks authored Oct 31, 2024
1 parent c4afa52 commit 282f844
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/Avalonia.X11/Interop/Glib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,34 @@ private static extern ulong g_signal_connect_object(IntPtr instance, Utf8Buffer

[DllImport(GlibName)]
public static extern void g_main_loop_unref(IntPtr loop);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void GOnceSourceFunc(IntPtr userData);


[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int GSourceFunc(IntPtr userData);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void GDestroyNotify(IntPtr userData);

[DllImport(GlibName)]
private static extern int g_idle_add_once(GOnceSourceFunc cb, IntPtr userData);
private static extern int g_idle_add(GSourceFunc cb, IntPtr userData);

private static readonly GOnceSourceFunc s_onceSourceCb = (userData) =>
private static readonly GSourceFunc s_onceSourceCb = (userData) =>
{
var h = GCHandle.FromIntPtr(userData);
var cb = (Action)h.Target!;
h.Free();
cb();
return 0;
};

public static void g_idle_add_once(Action cb) =>
g_idle_add_once(s_onceSourceCb, GCHandle.ToIntPtr(GCHandle.Alloc(cb)));
g_idle_add(s_onceSourceCb, GCHandle.ToIntPtr(GCHandle.Alloc(cb)));

[DllImport(GlibName)]
private static extern uint g_timeout_add_once(uint interval, GOnceSourceFunc cb, IntPtr userData);
private static extern uint g_timeout_add(uint interval, GSourceFunc cb, IntPtr userData);

public static uint g_timeout_add_once(uint interval, Action cb) =>
g_timeout_add_once(interval, s_onceSourceCb, GCHandle.ToIntPtr(GCHandle.Alloc(cb)));
g_timeout_add(interval, s_onceSourceCb, GCHandle.ToIntPtr(GCHandle.Alloc(cb)));

private static readonly GDestroyNotify s_gcHandleDestroyNotify = handle => GCHandle.FromIntPtr(handle).Free();

Expand Down

0 comments on commit 282f844

Please sign in to comment.