-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonitorManager.cs
47 lines (42 loc) · 1.63 KB
/
MonitorManager.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
class MonitorManager
{
public static IEnumerable<DdcMonitorItem> EnumerateMonitors()
{
var deviceItems = DeviceContext.EnumerateMonitorDevices().ToList();
if (!(deviceItems?.Any() == true))
yield break;
// Obtained by DDC/CI
foreach (var handleItem in DeviceContext.GetMonitorHandles())
{
foreach (var physicalItem in MonitorApi.EnumeratePhysicalMonitors(handleItem.MonitorHandle))
{
int index = -1;
if (physicalItem.IsSupported)
{
index = deviceItems.FindIndex(x =>
(x.DisplayIndex == handleItem.DisplayIndex) &&
(x.MonitorIndex == physicalItem.MonitorIndex) &&
string.Equals(x.Description, physicalItem.Description, StringComparison.OrdinalIgnoreCase));
}
if (index < 0)
{
physicalItem.Handle.Dispose();
continue;
}
var deviceItem = deviceItems[index];
yield return new DdcMonitorItem(
deviceInstanceId: deviceItem.DeviceInstanceId,
description: deviceItem.Description,
displayIndex: deviceItem.DisplayIndex,
monitorIndex: deviceItem.MonitorIndex,
handle: physicalItem.Handle);
deviceItems.RemoveAt(index);
if (deviceItems.Count == 0)
yield break;
}
}
}
}