Example #1
0
            public MonitorData()
            {
                DeviceItems = DeviceContext.EnumerateMonitorDevices().ToArray();

                PhysicalItems = DeviceContext.GetMonitorHandles().ToDictionary(
                    x => x,
                    x => MonitorConfiguration.EnumeratePhysicalMonitors(x.MonitorHandle).ToArray());

                InstalledItems = DeviceInstallation.EnumerateInstalledMonitors().ToArray();
                DesktopItems   = MSMonitor.EnumerateDesktopMonitors().ToArray();
            }
Example #2
0
            public async Task PopulateAsync()
            {
                var sw = new Stopwatch();

                var actions = new[]
                {
                    GetAction(nameof(DeviceItems), () =>
                              DeviceItems = DeviceContext.EnumerateMonitorDevices().ToArray()),

                    GetAction(nameof(PhysicalItems), () =>
                              PhysicalItems = DeviceContext.GetMonitorHandles().ToDictionary(
                                  x => x,
                                  x => MonitorConfiguration.EnumeratePhysicalMonitors(x.MonitorHandle, true).ToArray())),

                    GetAction(nameof(InstalledItems), () =>
                              InstalledItems = DeviceInstallation.EnumerateInstalledMonitors().ToArray()),

                    GetAction(nameof(DesktopItems), () =>
                              DesktopItems = MSMonitor.EnumerateDesktopMonitors().ToArray()),

                    GetAction(nameof(DisplayItems), async() =>
                    {
                        if (OsVersion.Is10Redstone4OrNewer)
                        {
                            DisplayItems = await DisplayInformation.GetDisplayMonitorsAsync();
                        }
                    })
                };

                ElapsedTime = new string[actions.Length];

                sw.Start();

                await Task.WhenAll(actions.Select((x, index) => Task.Run(() => x.Invoke(index))));

                sw.Stop();

                Action <int> GetAction(string name, Action action) =>
                new Action <int>((index) =>
                {
                    action.Invoke();
                    ElapsedTime[index] = $"{name} -> {sw.ElapsedMilliseconds}";
                });
            }
Example #3
0
        public static IEnumerable <IMonitor> EnumerateMonitors()
        {
            var deviceItems = DeviceContext.EnumerateMonitorDevices().ToList();

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

            // By DDC/CI
            foreach (var handleItem in DeviceContext.GetMonitorHandles())
            {
                foreach (var physicalItem in MonitorConfiguration.EnumeratePhysicalMonitors(handleItem.MonitorHandle))
                {
                    int index = -1;
                    if (physicalItem.IsBrightnessSupported)
                    {
                        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.FriendlyName,
                                     displayIndex: deviceItem.DisplayIndex,
                                     monitorIndex: deviceItem.MonitorIndex,
                                     handle: physicalItem.Handle,
                                     isLowLevel: physicalItem.IsLowLevel));

                    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.FriendlyName,
                                     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.FriendlyName,
                                 displayIndex: deviceItem.DisplayIndex,
                                 monitorIndex: deviceItem.MonitorIndex));
            }
        }
Example #4
0
        public static IEnumerable <IMonitor> EnumerateMonitors()
        {
            var deviceItems = DeviceContext.EnumerateMonitorDevices().ToList();

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

            // By DDC/CI
            foreach (var handleItem in DeviceContext.GetMonitorHandles())
            {
                foreach (var physicalItem in MonitorConfiguration.EnumeratePhysicalMonitors(handleItem.MonitorHandle))
                {
                    //Debug.WriteLine($"Display: {handleItem.DisplayIndex}, Monitor: {physicalItem.MonitorIndex}");

                    var index = deviceItems.FindIndex(x =>
                                                      (x.DisplayIndex == handleItem.DisplayIndex) &&
                                                      (x.MonitorIndex == physicalItem.MonitorIndex) &&
                                                      string.Equals(x.Description, physicalItem.Description, StringComparison.OrdinalIgnoreCase));
                    if (0 <= index)
                    {
                        yield return(new DdcMonitorItem(
                                         description: deviceItems[index].Description,
                                         deviceInstanceId: deviceItems[index].DeviceInstanceId,
                                         displayIndex: deviceItems[index].DisplayIndex,
                                         monitorIndex: deviceItems[index].MonitorIndex,
                                         handle: physicalItem.Handle));

                        deviceItems.RemoveAt(index);
                        if (deviceItems.Count == 0)
                        {
                            yield break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    physicalItem.Handle.Dispose();
                }
            }

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

            foreach (var desktopItem in MSMonitor.EnumerateDesktopMonitors())
            {
                foreach (var installedItem in installedItems)
                {
                    var index = deviceItems.FindIndex(x =>
                                                      string.Equals(x.DeviceInstanceId, desktopItem.DeviceInstanceId, StringComparison.OrdinalIgnoreCase) &&
                                                      string.Equals(x.DeviceInstanceId, installedItem.DeviceInstanceId, StringComparison.OrdinalIgnoreCase));
                    if (0 <= index)
                    {
                        yield return(new WmiMonitorItem(
                                         description: deviceItems[index].Description,
                                         deviceInstanceId: deviceItems[index].DeviceInstanceId,
                                         displayIndex: deviceItems[index].DisplayIndex,
                                         monitorIndex: deviceItems[index].MonitorIndex,
                                         brightnessLevels: desktopItem.BrightnessLevels,
                                         isRemovable: installedItem.IsRemovable));

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