From 16cc52164b0f814a93a777d73e91e8690c4cf793 Mon Sep 17 00:00:00 2001 From: emoacht Date: Fri, 26 Apr 2024 08:43:18 +0900 Subject: [PATCH 1/4] Add alternatove methods for debug --- .../Models/Monitor/DisplayConfig.cs | 13 +++++++++++++ .../Models/Monitor/DisplayMonitorProvider.cs | 18 ++++++++++++++++++ Source/ScreenFrame/VisualTreeHelperAddition.cs | 16 ++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/Source/Monitorian.Core/Models/Monitor/DisplayConfig.cs b/Source/Monitorian.Core/Models/Monitor/DisplayConfig.cs index 2d71604d..4755c661 100644 --- a/Source/Monitorian.Core/Models/Monitor/DisplayConfig.cs +++ b/Source/Monitorian.Core/Models/Monitor/DisplayConfig.cs @@ -32,6 +32,10 @@ private static extern int QueryDisplayConfig( private static extern int DisplayConfigGetDeviceInfo( ref DISPLAYCONFIG_TARGET_DEVICE_NAME requestPacket); + [DllImport("User32.dll")] + private static extern int DisplayConfigGetDeviceInfo( + ref DISPLAYCONFIG_SOURCE_DEVICE_NAME requestPacket); + // All derived from wingdi.h [StructLayout(LayoutKind.Sequential)] private struct DISPLAYCONFIG_PATH_INFO @@ -80,6 +84,15 @@ private struct DISPLAYCONFIG_TARGET_DEVICE_NAME public string monitorDevicePath; } + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + private struct DISPLAYCONFIG_SOURCE_DEVICE_NAME + { + public DISPLAYCONFIG_DEVICE_INFO_HEADER header; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] + public string viewGdiDeviceName; + } + [StructLayout(LayoutKind.Sequential)] private struct DISPLAYCONFIG_PATH_SOURCE_INFO { diff --git a/Source/Monitorian.Core/Models/Monitor/DisplayMonitorProvider.cs b/Source/Monitorian.Core/Models/Monitor/DisplayMonitorProvider.cs index 2cbbad59..109e8fc1 100644 --- a/Source/Monitorian.Core/Models/Monitor/DisplayMonitorProvider.cs +++ b/Source/Monitorian.Core/Models/Monitor/DisplayMonitorProvider.cs @@ -1,8 +1,11 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; using System.Runtime.Serialization; using System.Threading.Tasks; using System.Windows; +using Windows.Devices.Display.Core; namespace Monitorian.Core.Models.Monitor; @@ -147,6 +150,21 @@ public static async Task GetDisplayMonitorsAsync() isInternal: (displayMonitor.ConnectionKind == Windows.Devices.Display.DisplayMonitorConnectionKind.Internal), connectionDescription: GetConnectionDescription(displayMonitor.ConnectionKind, displayMonitor.PhysicalConnector))); } + +#if DEBUG + using var manager = DisplayManager.Create(DisplayManagerOptions.None); + var state = manager.TryReadCurrentStateForAllTargets().State; + foreach (var target in state.Views + .SelectMany(x => x.Paths) + .Select(x => x.Target) + .Where(x => x.IsConnected)) + { + var displayMonitor = target.TryGetMonitor(); + var deviceInstanceId = DeviceConversion.ConvertToDeviceInstanceId(displayMonitor.DeviceId); + Debug.Assert(items.Any(x => x.DeviceInstanceId == deviceInstanceId)); + } +#endif + return items.ToArray(); } } diff --git a/Source/ScreenFrame/VisualTreeHelperAddition.cs b/Source/ScreenFrame/VisualTreeHelperAddition.cs index 20fe2688..d2da60e9 100644 --- a/Source/ScreenFrame/VisualTreeHelperAddition.cs +++ b/Source/ScreenFrame/VisualTreeHelperAddition.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; @@ -67,6 +68,11 @@ private enum MONITOR_DPI_TYPE MDT_Default = MDT_Effective_DPI } + [DllImport("Shcore.dll")] + private static extern int GetScaleFactorForMonitor( + IntPtr hMon, + out uint pScale); + #endregion #region DPI @@ -196,6 +202,16 @@ private static DpiScale GetDpi(IntPtr monitorHandle) if (result != S_OK) return SystemDpi; +#if DEBUG + result = GetScaleFactorForMonitor( + monitorHandle, + out uint factor); + if (result == S_OK) + { + Debug.Assert(factor == (dpiX / DefaultPixelsPerInch * 100)); + } +#endif + return new DpiScale(dpiX / DefaultPixelsPerInch, dpiY / DefaultPixelsPerInch); } From 3dfa483c6151b9d95fbe3c2fd8fbbb42c478f59a Mon Sep 17 00:00:00 2001 From: emoacht Date: Fri, 3 May 2024 13:11:26 +0900 Subject: [PATCH 2/4] Suppress FileNotFoundException --- .../Models/Monitor/MonitorManager.cs | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/Source/Monitorian.Core/Models/Monitor/MonitorManager.cs b/Source/Monitorian.Core/Models/Monitor/MonitorManager.cs index d9f52e8d..b621806e 100644 --- a/Source/Monitorian.Core/Models/Monitor/MonitorManager.cs +++ b/Source/Monitorian.Core/Models/Monitor/MonitorManager.cs @@ -72,15 +72,31 @@ private static HashSet GetOptionIds(string option) #endregion private static HashSet _foundIds; + private static bool _isDisplayMonitorAvailable = true; // Default + + private static async Task GetDisplayMonitorsAsync() + { + if (OsVersion.Is10Build17134OrGreater && _isDisplayMonitorAvailable) + { + try + { + return await DisplayMonitorProvider.GetDisplayMonitorsAsync(); + } + catch (FileNotFoundException) + { + _isDisplayMonitorAvailable = false; + } + } + return null; + } public static async Task> EnumerateMonitorsAsync(TimeSpan timeout, CancellationToken cancellationToken = default) { var deviceItems = DeviceContext.EnumerateMonitorDevices().ToArray(); _foundIds = new HashSet(deviceItems.Select(x => x.DeviceInstanceId)); - IDisplayItem[] displayItems = OsVersion.Is10Build17134OrGreater - ? await DisplayMonitorProvider.GetDisplayMonitorsAsync() - : DisplayConfig.EnumerateDisplayConfigs().ToArray(); + IDisplayItem[] displayItems = await GetDisplayMonitorsAsync(); + displayItems ??= DisplayConfig.EnumerateDisplayConfigs().ToArray(); IEnumerable EnumerateBasicItems() { @@ -341,10 +357,7 @@ public async Task PopulateAsync() DeviceItems = DeviceContext.EnumerateMonitorDevices().ToArray()), GetTask(nameof(DisplayMonitorItems), async () => - { - if (OsVersion.Is10Build17134OrGreater) - DisplayMonitorItems = await DisplayMonitorProvider.GetDisplayMonitorsAsync(); - }), + DisplayMonitorItems = await GetDisplayMonitorsAsync()), GetTask(nameof(DisplayConfigItems), () => DisplayConfigItems = DisplayConfig.EnumerateDisplayConfigs().ToArray()), From d00071830f904d9251ec67570fafe8362f323a02 Mon Sep 17 00:00:00 2001 From: emoacht Date: Fri, 3 May 2024 13:13:25 +0900 Subject: [PATCH 3/4] Change access to set contrast --- Source/Monitorian.Core/ViewModels/MonitorViewModel.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Monitorian.Core/ViewModels/MonitorViewModel.cs b/Source/Monitorian.Core/ViewModels/MonitorViewModel.cs index f0042e19..d0b78b17 100644 --- a/Source/Monitorian.Core/ViewModels/MonitorViewModel.cs +++ b/Source/Monitorian.Core/ViewModels/MonitorViewModel.cs @@ -351,7 +351,7 @@ public void DecrementContrast(int tickSize) SetContrast(contrast); } - private bool SetContrast(int contrast) + public bool SetContrast(int contrast) { contrast = Math.Min(100, Math.Max(0, contrast)); From a4e824aab227e0995ec3caa3f199bcc02779313e Mon Sep 17 00:00:00 2001 From: emoacht Date: Fri, 3 May 2024 13:26:53 +0900 Subject: [PATCH 4/4] Increment version to 4.6.12 --- Source/Installer/Product.wxs | 2 +- Source/Monitorian.Core/Properties/AssemblyInfo.cs | 4 ++-- Source/Monitorian/Properties/AssemblyInfo.cs | 4 ++-- Source/ScreenFrame/Properties/AssemblyInfo.cs | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Source/Installer/Product.wxs b/Source/Installer/Product.wxs index 76adbf92..547fc966 100644 --- a/Source/Installer/Product.wxs +++ b/Source/Installer/Product.wxs @@ -1,6 +1,6 @@  -