-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayMonitorResolver.cs
More file actions
82 lines (68 loc) · 2.74 KB
/
DisplayMonitorResolver.cs
File metadata and controls
82 lines (68 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
namespace AmpUp;
internal sealed class DisplayMonitorInfo
{
public int Index { get; init; }
public string DeviceName { get; init; } = "";
public string FriendlyName { get; init; } = "";
public string DevicePath { get; init; } = "";
public bool IsPrimary { get; init; }
public string DisplayName => !string.IsNullOrWhiteSpace(FriendlyName) ? FriendlyName : DeviceName;
public string Label => IsPrimary ? $"{DisplayName} (Primary)" : DisplayName;
}
internal static class DisplayMonitorResolver
{
public static List<DisplayMonitorInfo> GetMonitors()
{
var screens = System.Windows.Forms.Screen.AllScreens;
var displayConfig = NativeMethods.GetMonitorDisplayConfigInfo();
var monitors = new List<DisplayMonitorInfo>(screens.Length);
for (int i = 0; i < screens.Length; i++)
{
displayConfig.TryGetValue(screens[i].DeviceName, out var info);
monitors.Add(new DisplayMonitorInfo
{
Index = i,
DeviceName = screens[i].DeviceName,
FriendlyName = info?.FriendlyName ?? "",
DevicePath = info?.DevicePath ?? "",
IsPrimary = screens[i].Primary,
});
}
return monitors;
}
public static int ResolveOsdMonitorIndex(OsdConfig osd)
{
var monitors = GetMonitors();
if (monitors.Count == 0)
return 0;
var match = FindByIdentity(monitors, osd.MonitorDevicePath, m => m.DevicePath)
?? FindByIdentity(monitors, osd.MonitorFriendlyName, m => m.FriendlyName)
?? FindByIdentity(monitors, osd.MonitorDeviceName, m => m.DeviceName);
if (match != null)
return match.Index;
if (osd.MonitorIndex >= 0 && osd.MonitorIndex < monitors.Count)
return osd.MonitorIndex;
return monitors.FirstOrDefault(m => m.IsPrimary)?.Index ?? 0;
}
public static void RememberOsdMonitor(OsdConfig osd, int monitorIndex)
{
var monitors = GetMonitors();
var monitor = monitors.FirstOrDefault(m => m.Index == monitorIndex);
if (monitor == null)
return;
osd.MonitorIndex = monitor.Index;
osd.MonitorDeviceName = monitor.DeviceName;
osd.MonitorFriendlyName = monitor.FriendlyName;
osd.MonitorDevicePath = monitor.DevicePath;
}
private static DisplayMonitorInfo? FindByIdentity(
List<DisplayMonitorInfo> monitors,
string? storedValue,
Func<DisplayMonitorInfo, string> selector)
{
if (string.IsNullOrWhiteSpace(storedValue))
return null;
return monitors.FirstOrDefault(m =>
string.Equals(selector(m), storedValue, StringComparison.OrdinalIgnoreCase));
}
}