Skip to content

Commit

Permalink
Merge pull request #589 from emoacht/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
emoacht authored May 3, 2024
2 parents b9f3ae8 + a4e824a commit 71f0782
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Source/Installer/Product.wxs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" Name="Monitorian" Manufacturer="emoacht" Version="4.6.11"
<Product Id="*" Name="Monitorian" Manufacturer="emoacht" Version="4.6.12"
Language="1033" Codepage="1252" UpgradeCode="{81A4D148-75D3-462E-938D-8C208FB48E3C}">
<Package Id="*" InstallerVersion="500" Compressed="yes"
InstallScope="perMachine" InstallPrivileges="elevated"
Expand Down
13 changes: 13 additions & 0 deletions Source/Monitorian.Core/Models/Monitor/DisplayConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down
18 changes: 18 additions & 0 deletions Source/Monitorian.Core/Models/Monitor/DisplayMonitorProvider.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -147,6 +150,21 @@ public static async Task<DisplayItem[]> 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();
}
}
Expand Down
27 changes: 20 additions & 7 deletions Source/Monitorian.Core/Models/Monitor/MonitorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,31 @@ private static HashSet<string> GetOptionIds(string option)
#endregion

private static HashSet<string> _foundIds;
private static bool _isDisplayMonitorAvailable = true; // Default

private static async Task<DisplayMonitorProvider.DisplayItem[]> GetDisplayMonitorsAsync()
{
if (OsVersion.Is10Build17134OrGreater && _isDisplayMonitorAvailable)
{
try
{
return await DisplayMonitorProvider.GetDisplayMonitorsAsync();
}
catch (FileNotFoundException)
{
_isDisplayMonitorAvailable = false;
}
}
return null;
}

public static async Task<IEnumerable<IMonitor>> EnumerateMonitorsAsync(TimeSpan timeout, CancellationToken cancellationToken = default)
{
var deviceItems = DeviceContext.EnumerateMonitorDevices().ToArray();
_foundIds = new HashSet<string>(deviceItems.Select(x => x.DeviceInstanceId));

IDisplayItem[] displayItems = OsVersion.Is10Build17134OrGreater
? await DisplayMonitorProvider.GetDisplayMonitorsAsync()
: DisplayConfig.EnumerateDisplayConfigs().ToArray();
IDisplayItem[] displayItems = await GetDisplayMonitorsAsync();
displayItems ??= DisplayConfig.EnumerateDisplayConfigs().ToArray();

IEnumerable<BasicItem> EnumerateBasicItems()
{
Expand Down Expand Up @@ -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()),
Expand Down
4 changes: 2 additions & 2 deletions Source/Monitorian.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.6.11.0")]
[assembly: AssemblyFileVersion("4.6.11.0")]
[assembly: AssemblyVersion("4.6.12.0")]
[assembly: AssemblyFileVersion("4.6.12.0")]
[assembly: NeutralResourcesLanguage("en-US")]

// For unit test
Expand Down
2 changes: 1 addition & 1 deletion Source/Monitorian.Core/ViewModels/MonitorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
4 changes: 2 additions & 2 deletions Source/Monitorian/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.6.11.0")]
[assembly: AssemblyFileVersion("4.6.11.0")]
[assembly: AssemblyVersion("4.6.12.0")]
[assembly: AssemblyFileVersion("4.6.12.0")]
[assembly: Guid("a4cc5362-9b08-465b-ad64-5cfabc72a4c7")]
[assembly: NeutralResourcesLanguage("en-US")]
4 changes: 2 additions & 2 deletions Source/ScreenFrame/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.6.5.0")]
[assembly: AssemblyFileVersion("4.6.5.0")]
[assembly: AssemblyVersion("4.6.12.0")]
[assembly: AssemblyFileVersion("4.6.12.0")]
[assembly: NeutralResourcesLanguage("en-US")]
16 changes: 16 additions & 0 deletions Source/ScreenFrame/VisualTreeHelperAddition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 71f0782

Please sign in to comment.