From 6b4cf555e0ff7147b3d21a20f3d12bb8415dde67 Mon Sep 17 00:00:00 2001 From: lytico Date: Mon, 19 Jul 2021 19:24:17 +0200 Subject: [PATCH 1/4] GLibrary.cs: introduce IsSupported --- Source/Libs/Shared/GLibrary.cs | 142 ++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 63 deletions(-) diff --git a/Source/Libs/Shared/GLibrary.cs b/Source/Libs/Shared/GLibrary.cs index 62f810f55..58c5c2ffe 100644 --- a/Source/Libs/Shared/GLibrary.cs +++ b/Source/Libs/Shared/GLibrary.cs @@ -5,77 +5,93 @@ using System.Runtime.InteropServices; class GLibrary { - [DllImport("kernel32.dll", SetLastError = true)] - private static extern bool SetDllDirectory(string lpPathName); - private static Dictionary _libraries; - private static Dictionary _customlibraries; - private static Dictionary _libraryDefinitions; + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool SetDllDirectory(string lpPathName); - static GLibrary() - { - _customlibraries = new Dictionary(); - _libraries = new Dictionary(); - _libraryDefinitions = new Dictionary(); - _libraryDefinitions[Library.GLib] = new[] { "libglib-2.0-0.dll", "libglib-2.0.so.0", "libglib-2.0.0.dylib", "glib-2.dll" }; - _libraryDefinitions[Library.GObject] = new[] { "libgobject-2.0-0.dll", "libgobject-2.0.so.0", "libgobject-2.0.0.dylib", "gobject-2.dll" }; - _libraryDefinitions[Library.Cairo] = new[] { "libcairo-2.dll", "libcairo.so.2", "libcairo.2.dylib", "cairo.dll" }; - _libraryDefinitions[Library.Gio] = new[] { "libgio-2.0-0.dll", "libgio-2.0.so.0", "libgio-2.0.0.dylib", "gio-2.dll" }; - _libraryDefinitions[Library.Atk] = new[] { "libatk-1.0-0.dll", "libatk-1.0.so.0", "libatk-1.0.0.dylib", "atk-1.dll" }; - _libraryDefinitions[Library.Pango] = new[] { "libpango-1.0-0.dll", "libpango-1.0.so.0", "libpango-1.0.0.dylib", "pango-1.dll" }; - _libraryDefinitions[Library.Gdk] = new[] { "libgdk-3-0.dll", "libgdk-3.so.0", "libgdk-3.0.dylib", "gdk-3.dll" }; - _libraryDefinitions[Library.GdkPixbuf] = new[] { "libgdk_pixbuf-2.0-0.dll", "libgdk_pixbuf-2.0.so.0", "libgdk_pixbuf-2.0.dylib", "gdk_pixbuf-2.dll" }; - _libraryDefinitions[Library.Gtk] = new[] { "libgtk-3-0.dll", "libgtk-3.so.0", "libgtk-3.0.dylib", "gtk-3.dll" }; - _libraryDefinitions[Library.PangoCairo] = new[] { "libpangocairo-1.0-0.dll", "libpangocairo-1.0.so.0", "libpangocairo-1.0.0.dylib", "pangocairo-1.dll" }; - _libraryDefinitions[Library.GtkSource] = new[] { "libgtksourceview-4-0.dll", "libgtksourceview-4.so.0", "libgtksourceview-4.0.dylib", "gtksourceview-4.dll" }; - } + private static Dictionary _libraries; + private static HashSet _librariesNotFound; + private static Dictionary _customlibraries; + private static Dictionary _libraryDefinitions; - public static IntPtr Load(Library library) - { - var ret = IntPtr.Zero; - if (_libraries.TryGetValue(library, out ret)) - return ret; + static GLibrary() + { + _customlibraries = new Dictionary(); + _librariesNotFound = new HashSet(); + _libraries = new Dictionary(); + _libraryDefinitions = new Dictionary(); + _libraryDefinitions[Library.GLib] = new[] {"libglib-2.0-0.dll", "libglib-2.0.so.0", "libglib-2.0.0.dylib", "glib-2.dll"}; + _libraryDefinitions[Library.GObject] = new[] {"libgobject-2.0-0.dll", "libgobject-2.0.so.0", "libgobject-2.0.0.dylib", "gobject-2.dll"}; + _libraryDefinitions[Library.Cairo] = new[] {"libcairo-2.dll", "libcairo.so.2", "libcairo.2.dylib", "cairo.dll"}; + _libraryDefinitions[Library.Gio] = new[] {"libgio-2.0-0.dll", "libgio-2.0.so.0", "libgio-2.0.0.dylib", "gio-2.dll"}; + _libraryDefinitions[Library.Atk] = new[] {"libatk-1.0-0.dll", "libatk-1.0.so.0", "libatk-1.0.0.dylib", "atk-1.dll"}; + _libraryDefinitions[Library.Pango] = new[] {"libpango-1.0-0.dll", "libpango-1.0.so.0", "libpango-1.0.0.dylib", "pango-1.dll"}; + _libraryDefinitions[Library.Gdk] = new[] {"libgdk-3-0.dll", "libgdk-3.so.0", "libgdk-3.0.dylib", "gdk-3.dll"}; + _libraryDefinitions[Library.GdkPixbuf] = new[] {"libgdk_pixbuf-2.0-0.dll", "libgdk_pixbuf-2.0.so.0", "libgdk_pixbuf-2.0.dylib", "gdk_pixbuf-2.dll"}; + _libraryDefinitions[Library.Gtk] = new[] {"libgtk-3-0.dll", "libgtk-3.so.0", "libgtk-3.0.dylib", "gtk-3.dll"}; + _libraryDefinitions[Library.PangoCairo] = new[] {"libpangocairo-1.0-0.dll", "libpangocairo-1.0.so.0", "libpangocairo-1.0.0.dylib", "pangocairo-1.dll"}; + _libraryDefinitions[Library.GtkSource] = new[] {"libgtksourceview-4-0.dll", "libgtksourceview-4.so.0", "libgtksourceview-4.0.dylib", "gtksourceview-4.dll"}; + } - if (FuncLoader.IsWindows) - { - ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][0]); + public static IntPtr Load(Library library) + { + if (_libraries.TryGetValue(library, out var ret)) + return ret; - if (ret == IntPtr.Zero) - { - SetDllDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Gtk", "3.24.24")); - ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][0]); - } - } - else if (FuncLoader.IsOSX) - { - ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][2]); + if (TryGet(library, out ret)) return ret; - if (ret == IntPtr.Zero) - { - ret = FuncLoader.LoadLibrary("/usr/local/lib/" + _libraryDefinitions[library][2]); - } - } - else - ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][1]); + var err = library + ": " + string.Join(", ", _libraryDefinitions[library]); - if (ret == IntPtr.Zero) - { - for (int i = 0; i < _libraryDefinitions[library].Length; i++) - { - ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][i]); + throw new DllNotFoundException(err); - if (ret != IntPtr.Zero) - break; - } - } + } - if (ret == IntPtr.Zero) - { - var err = library + ": " + string.Join(", ", _libraryDefinitions[library]); - throw new DllNotFoundException(err); - } + public static bool IsSupported(Library library) => TryGet(library, out var __); + + static bool TryGet(Library library, out IntPtr ret) + { + ret = IntPtr.Zero; + + if (_libraries.TryGetValue(library, out ret)) { + return true; + } + + if (_librariesNotFound.Contains(library)) { + return false; + } + + if (FuncLoader.IsWindows) { + ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][0]); + + if (ret == IntPtr.Zero) { + SetDllDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Gtk", "3.24.24")); + ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][0]); + } + } else if (FuncLoader.IsOSX) { + ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][2]); + + if (ret == IntPtr.Zero) { + ret = FuncLoader.LoadLibrary("/usr/local/lib/" + _libraryDefinitions[library][2]); + } + } else + ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][1]); + + if (ret == IntPtr.Zero) { + for (var i = 0; i < _libraryDefinitions[library].Length; i++) { + ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][i]); + + if (ret != IntPtr.Zero) + break; + } + } + + if (ret != IntPtr.Zero) { + _libraries[library] = ret; + } else { + _librariesNotFound.Add(library); + } + + return ret != IntPtr.Zero; + } - _libraries[library] = ret; - return ret; - } } \ No newline at end of file From 882140fd9dfac19247e2858947850f84862fe46e Mon Sep 17 00:00:00 2001 From: lytico Date: Mon, 19 Jul 2021 20:13:14 +0200 Subject: [PATCH 2/4] introduce IsSupported in .Global --- Source/Libs/AtkSharp/Global.cs | 3 +++ Source/Libs/CairoSharp/Global.cs | 11 +++++++++++ Source/Libs/GLibSharp/Global.cs | 3 +++ Source/Libs/GdkSharp/Global.cs | 3 +++ Source/Libs/GioSharp/Global.cs | 11 +++++++++++ Source/Libs/GtkSharp/Global.cs | 5 +++-- Source/Libs/GtkSourceSharp/Global.cs | 11 +++++++++++ Source/Libs/PangoSharp/Global.cs | 3 +++ 8 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 Source/Libs/CairoSharp/Global.cs create mode 100644 Source/Libs/GioSharp/Global.cs create mode 100644 Source/Libs/GtkSourceSharp/Global.cs diff --git a/Source/Libs/AtkSharp/Global.cs b/Source/Libs/AtkSharp/Global.cs index 0c8a0eb50..7d6acc534 100644 --- a/Source/Libs/AtkSharp/Global.cs +++ b/Source/Libs/AtkSharp/Global.cs @@ -40,6 +40,9 @@ namespace Atk { GLib.Marshaller.Free (native_event_type); return id; } + + public static bool IsSupported => GLibrary.IsSupported(Library.Atk); + } } diff --git a/Source/Libs/CairoSharp/Global.cs b/Source/Libs/CairoSharp/Global.cs new file mode 100644 index 000000000..902aee5c7 --- /dev/null +++ b/Source/Libs/CairoSharp/Global.cs @@ -0,0 +1,11 @@ +namespace Cairo +{ + + public partial class Global + { + + public static bool IsSupported => GLibrary.IsSupported(Library.Cairo); + + } + +} \ No newline at end of file diff --git a/Source/Libs/GLibSharp/Global.cs b/Source/Libs/GLibSharp/Global.cs index 5d119d019..0695ed584 100644 --- a/Source/Libs/GLibSharp/Global.cs +++ b/Source/Libs/GLibSharp/Global.cs @@ -85,6 +85,9 @@ namespace GLib { { return Marshaller.PtrToStringGFree (g_format_size_for_display (size)); } + + public static bool IsSupported => GLibrary.IsSupported(Library.GLib); + } } diff --git a/Source/Libs/GdkSharp/Global.cs b/Source/Libs/GdkSharp/Global.cs index 719cae401..7cbc11194 100644 --- a/Source/Libs/GdkSharp/Global.cs +++ b/Source/Libs/GdkSharp/Global.cs @@ -160,6 +160,9 @@ namespace Gdk { result [i] = (VisualType) tmp [i]; return result; } + + public static bool IsSupported => GLibrary.IsSupported(Library.Gdk); + } } diff --git a/Source/Libs/GioSharp/Global.cs b/Source/Libs/GioSharp/Global.cs new file mode 100644 index 000000000..475b0fbbf --- /dev/null +++ b/Source/Libs/GioSharp/Global.cs @@ -0,0 +1,11 @@ +namespace Gio +{ + + public partial class Global + { + + public static bool IsSupported => GLibrary.IsSupported(Library.Gio); + + } + +} \ No newline at end of file diff --git a/Source/Libs/GtkSharp/Global.cs b/Source/Libs/GtkSharp/Global.cs index b5ae6c364..8936495fe 100644 --- a/Source/Libs/GtkSharp/Global.cs +++ b/Source/Libs/GtkSharp/Global.cs @@ -20,8 +20,6 @@ namespace Gtk { public partial class Global { - internal const string GtkNativeDll = "libgtk-3-0.dll"; - public static bool ShowUri (string uri) { return ShowUri (null, uri); @@ -31,6 +29,9 @@ namespace Gtk { { return ShowUri (screen, uri, Gtk.Global.CurrentEventTime); } + + public static bool IsSupported => GLibrary.IsSupported(Library.Gtk); + } } diff --git a/Source/Libs/GtkSourceSharp/Global.cs b/Source/Libs/GtkSourceSharp/Global.cs new file mode 100644 index 000000000..029f1db8f --- /dev/null +++ b/Source/Libs/GtkSourceSharp/Global.cs @@ -0,0 +1,11 @@ +namespace GtkSource +{ + + public partial class Global + { + + public static bool IsSupported => GLibrary.IsSupported(Library.GtkSource); + + } + +} \ No newline at end of file diff --git a/Source/Libs/PangoSharp/Global.cs b/Source/Libs/PangoSharp/Global.cs index 2d3d43bd3..26c2438dc 100644 --- a/Source/Libs/PangoSharp/Global.cs +++ b/Source/Libs/PangoSharp/Global.cs @@ -50,6 +50,9 @@ namespace Pango { attrs = new Pango.AttrList (attrs_handle); return result; } + + public static bool IsSupported => GLibrary.IsSupported(Library.Pango); + } } From e4b774da5f2a98341eff1d32e0a39e62c03e54c6 Mon Sep 17 00:00:00 2001 From: lytico Date: Tue, 20 Jul 2021 01:51:36 +0200 Subject: [PATCH 3/4] Webkit: merge changes & add WebKit.Global.IsSupported --- Source/Libs/Shared/GLibrary.cs | 1 - Source/Libs/WebkitGtkSharp/Global.cs | 11 +++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 Source/Libs/WebkitGtkSharp/Global.cs diff --git a/Source/Libs/Shared/GLibrary.cs b/Source/Libs/Shared/GLibrary.cs index bf44a57af..86a58c38f 100644 --- a/Source/Libs/Shared/GLibrary.cs +++ b/Source/Libs/Shared/GLibrary.cs @@ -32,7 +32,6 @@ class GLibrary _libraryDefinitions[Library.PangoCairo] = new[] {"libpangocairo-1.0-0.dll", "libpangocairo-1.0.so.0", "libpangocairo-1.0.0.dylib", "pangocairo-1.dll"}; _libraryDefinitions[Library.GtkSource] = new[] {"libgtksourceview-4-0.dll", "libgtksourceview-4.so.0", "libgtksourceview-4.0.dylib", "gtksourceview-4.dll"}; _libraryDefinitions[Library.Webkit] = new[] { "libwebkit2gtk-4.0.dll", "libwebkit2gtk-4.0.so.37", "libwebkit2gtk-4.0.dylib", "libwebkit2gtk-4.0.0.dll" }; - } public static IntPtr Load(Library library) diff --git a/Source/Libs/WebkitGtkSharp/Global.cs b/Source/Libs/WebkitGtkSharp/Global.cs new file mode 100644 index 000000000..7f70a401a --- /dev/null +++ b/Source/Libs/WebkitGtkSharp/Global.cs @@ -0,0 +1,11 @@ +namespace WebKit +{ + + public partial class Global + { + + public static bool IsSupported => GLibrary.IsSupported(Library.Webkit); + + } + +} \ No newline at end of file From 69531abb9c1f332ce0542a707944af4b96383dc6 Mon Sep 17 00:00:00 2001 From: lytico Date: Tue, 20 Jul 2021 01:52:18 +0200 Subject: [PATCH 4/4] Samples.csproj: WebviewSection: check for WebKit.Global.IsSupported --- Source/Samples/Sections/Widgets/WebviewSection.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Source/Samples/Sections/Widgets/WebviewSection.cs b/Source/Samples/Sections/Widgets/WebviewSection.cs index 7e1ede642..f63239813 100644 --- a/Source/Samples/Sections/Widgets/WebviewSection.cs +++ b/Source/Samples/Sections/Widgets/WebviewSection.cs @@ -19,6 +19,11 @@ namespace Samples public WebviewSection() { + if (!WebKit.Global.IsSupported) { + AddItem(($"{nameof(WebKit.WebView)}",new Label($"{typeof(WebView).Namespace} is not suported on your OS"))); + return; + } + AddItem(ShowHtml()); AddItem(ShowUri());