From f20c00583c15d91214dee7e97e4f680a48e891a5 Mon Sep 17 00:00:00 2001 From: ElektroKill Date: Wed, 7 Aug 2024 11:46:10 +0200 Subject: [PATCH] Disable WPF hardware acceleration when running dnSpy under Wine --- dnSpy/dnSpy/MainApp/App.xaml.cs | 1 + dnSpy/dnSpy/MainApp/WineFixes.cs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 dnSpy/dnSpy/MainApp/WineFixes.cs diff --git a/dnSpy/dnSpy/MainApp/App.xaml.cs b/dnSpy/dnSpy/MainApp/App.xaml.cs index bbfc4eff97..35d2fff675 100644 --- a/dnSpy/dnSpy/MainApp/App.xaml.cs +++ b/dnSpy/dnSpy/MainApp/App.xaml.cs @@ -106,6 +106,7 @@ public App(bool readSettings, Stopwatch startupStopwatch) { ResourceHelper.SetResourceManagerTokenCache(resourceManagerTokenCacheImpl); AppDirectories.SetSettingsFilename(args.SettingsFilename); + WineFixes.Initialize(); AddAppContextFixes(); InstallExceptionHandlers(); InitializeComponent(); diff --git a/dnSpy/dnSpy/MainApp/WineFixes.cs b/dnSpy/dnSpy/MainApp/WineFixes.cs new file mode 100644 index 0000000000..32de05f5bd --- /dev/null +++ b/dnSpy/dnSpy/MainApp/WineFixes.cs @@ -0,0 +1,32 @@ +using System; +using System.Runtime.InteropServices; +using System.Windows.Interop; +using System.Windows.Media; + +namespace dnSpy.MainApp { + /// + /// Applies fixes and compatibility workarounds for running on Wine. + /// + static class WineFixes { + public static void Initialize() { + if (!IsRunningOnWine()) + return; + + // Disable WPF hardware acceleration on Wine + RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly; + } + + static bool IsRunningOnWine() { + var ntdll = GetModuleHandle("ntdll.dll"); + if (ntdll == IntPtr.Zero) + return false; + return GetProcAddress(ntdll, "wine_get_version") != IntPtr.Zero; + } + + [DllImport("kernel32", SetLastError = true)] + static extern IntPtr GetModuleHandle(string lpModuleName); + + [DllImport("kernel32", SetLastError = true)] + static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); + } +}