Esempio n. 1
0
        private static IEnumerable <IMonitor> EnumerateMonitors(List <DeviceItemPlus> deviceItems)
        {
            if (!(deviceItems?.Any() == true))
            {
                yield break;
            }

            // By DDC/CI
            foreach (var handleItem in DeviceContext.GetMonitorHandles())
            {
                foreach (var physicalItem in MonitorConfiguration.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.AlternateDescription,
                                     displayIndex: deviceItem.DisplayIndex,
                                     monitorIndex: deviceItem.MonitorIndex,
                                     handle: physicalItem.Handle,
                                     useLowLevel: physicalItem.IsLowLevelSupported));

                    deviceItems.RemoveAt(index);
                    if (deviceItems.Count == 0)
                    {
                        yield break;
                    }
                }
            }

            // By WMI
            var installedItems = DeviceInstallation.EnumerateInstalledMonitors().ToArray();

            foreach (var desktopItem in MSMonitor.EnumerateDesktopMonitors())
            {
                foreach (var installedItem in installedItems)
                {
                    int index = -1;
                    if (desktopItem.BrightnessLevels.Any())
                    {
                        index = deviceItems.FindIndex(x =>
                                                      string.Equals(x.DeviceInstanceId, desktopItem.DeviceInstanceId, StringComparison.OrdinalIgnoreCase) &&
                                                      string.Equals(x.DeviceInstanceId, installedItem.DeviceInstanceId, StringComparison.OrdinalIgnoreCase));
                    }
                    if (index < 0)
                    {
                        continue;
                    }

                    var deviceItem = deviceItems[index];
                    yield return(new WmiMonitorItem(
                                     deviceInstanceId: deviceItem.DeviceInstanceId,
                                     description: deviceItem.AlternateDescription,
                                     displayIndex: deviceItem.DisplayIndex,
                                     monitorIndex: deviceItem.MonitorIndex,
                                     brightnessLevels: desktopItem.BrightnessLevels,
                                     isRemovable: installedItem.IsRemovable));

                    deviceItems.RemoveAt(index);
                    if (deviceItems.Count == 0)
                    {
                        yield break;
                    }
                }
            }

            // Rest
            foreach (var deviceItem in deviceItems)
            {
                yield return(new InaccessibleMonitorItem(
                                 deviceInstanceId: deviceItem.DeviceInstanceId,
                                 description: deviceItem.AlternateDescription,
                                 displayIndex: deviceItem.DisplayIndex,
                                 monitorIndex: deviceItem.MonitorIndex));
            }
        }