-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11930 from AvaloniaUI/cleanup-runtime-platform-in…
…terface Cleanup runtime platform classes #2
- Loading branch information
Showing
27 changed files
with
270 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using Avalonia.Compatibility; | ||
using Avalonia.Platform.Interop; | ||
|
||
namespace Avalonia.Compatibility | ||
{ | ||
internal class NativeLibraryEx | ||
{ | ||
#if NET6_0_OR_GREATER | ||
public static IntPtr Load(string dll, Assembly assembly) => NativeLibrary.Load(dll, assembly, null); | ||
public static IntPtr Load(string dll) => NativeLibrary.Load(dll); | ||
public static bool TryGetExport(IntPtr handle, string name, out IntPtr address) => | ||
NativeLibrary.TryGetExport(handle, name, out address); | ||
#else | ||
public static IntPtr Load(string dll, Assembly assembly) => Load(dll); | ||
public static IntPtr Load(string dll) | ||
{ | ||
var handle = DlOpen!(dll); | ||
if (handle != IntPtr.Zero) | ||
return handle; | ||
throw new InvalidOperationException("Unable to load " + dll, DlError!()); | ||
} | ||
|
||
public static bool TryGetExport(IntPtr handle, string name, out IntPtr address) | ||
{ | ||
try | ||
{ | ||
address = DlSym!(handle, name); | ||
return address != default; | ||
} | ||
catch (Exception) | ||
{ | ||
address = default; | ||
return false; | ||
} | ||
} | ||
|
||
static NativeLibraryEx() | ||
{ | ||
if (OperatingSystemEx.IsWindows()) | ||
{ | ||
Win32Imports.Init(); | ||
} | ||
else if (OperatingSystemEx.IsLinux() || OperatingSystemEx.IsMacOS()) | ||
{ | ||
var buffer = Marshal.AllocHGlobal(0x1000); | ||
uname(buffer); | ||
var unixName = Marshal.PtrToStringAnsi(buffer); | ||
Marshal.FreeHGlobal(buffer); | ||
if (unixName == "Darwin") | ||
OsXImports.Init(); | ||
else | ||
LinuxImports.Init(); | ||
} | ||
} | ||
|
||
private static Func<string, IntPtr>? DlOpen; | ||
private static Func<IntPtr, string, IntPtr>? DlSym; | ||
private static Func<Exception?>? DlError; | ||
|
||
[DllImport("libc")] | ||
static extern int uname(IntPtr buf); | ||
|
||
static class Win32Imports | ||
{ | ||
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] | ||
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName); | ||
|
||
[DllImport("kernel32", EntryPoint = "LoadLibraryW", SetLastError = true, CharSet = CharSet.Unicode)] | ||
private static extern IntPtr LoadLibrary(string lpszLib); | ||
|
||
public static void Init() | ||
{ | ||
DlOpen = LoadLibrary; | ||
DlSym = GetProcAddress; | ||
DlError = () => new Win32Exception(Marshal.GetLastWin32Error()); | ||
} | ||
} | ||
|
||
static class LinuxImports | ||
{ | ||
[DllImport("libdl.so.2")] | ||
private static extern IntPtr dlopen(string path, int flags); | ||
|
||
[DllImport("libdl.so.2")] | ||
private static extern IntPtr dlsym(IntPtr handle, string symbol); | ||
|
||
[DllImport("libdl.so.2")] | ||
private static extern IntPtr dlerror(); | ||
|
||
public static void Init() | ||
{ | ||
DlOpen = s => dlopen(s, 1); | ||
DlSym = dlsym; | ||
DlError = () => new InvalidOperationException(Marshal.PtrToStringAnsi(dlerror())); | ||
} | ||
} | ||
|
||
static class OsXImports | ||
{ | ||
[DllImport("/usr/lib/libSystem.dylib")] | ||
private static extern IntPtr dlopen(string path, int flags); | ||
|
||
[DllImport("/usr/lib/libSystem.dylib")] | ||
private static extern IntPtr dlsym(IntPtr handle, string symbol); | ||
|
||
[DllImport("/usr/lib/libSystem.dylib")] | ||
private static extern IntPtr dlerror(); | ||
|
||
public static void Init() | ||
{ | ||
DlOpen = s => dlopen(s, 1); | ||
DlSym = dlsym; | ||
DlError = () => new InvalidOperationException(Marshal.PtrToStringAnsi(dlerror())); | ||
} | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
src/Avalonia.Base/Platform/Interop/IDynamicLibraryLoader.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
using System; | ||
using System.Threading; | ||
using Avalonia.Compatibility; | ||
using Avalonia.Metadata; | ||
using Avalonia.Platform.Internal; | ||
|
||
namespace Avalonia.Platform | ||
{ | ||
[PrivateApi] | ||
public class StandardRuntimePlatform : IRuntimePlatform | ||
{ | ||
public IDisposable StartSystemTimer(TimeSpan interval, Action tick) | ||
{ | ||
return new Timer(_ => tick(), null, interval, interval); | ||
} | ||
|
||
public IUnmanagedBlob AllocBlob(int size) => new UnmanagedBlob(size); | ||
|
||
private static readonly RuntimePlatformInfo s_info = new() | ||
{ | ||
IsDesktop = OperatingSystemEx.IsWindows() || OperatingSystemEx.IsMacOS() || OperatingSystemEx.IsLinux(), | ||
IsMobile = OperatingSystemEx.IsAndroid() || OperatingSystemEx.IsIOS() | ||
}; | ||
|
||
|
||
|
||
public virtual RuntimePlatformInfo GetRuntimeInfo() => s_info; | ||
} | ||
} |
Oops, something went wrong.