Exemplo n.º 1
0
        private static async Task <List <DeviceItemPlus> > GetMonitorDevicesAsync()
        {
            if (!OsVersion.Is10Redstone4OrNewer)
            {
                return(DeviceContext.EnumerateMonitorDevices().Select(x => new DeviceItemPlus(x)).ToList());
            }

            var displayItems = await DisplayInformation.GetDisplayMonitorsAsync();

            const string genericPattern = "^Generic (?:PnP|Non-PnP) Monitor$";

            return(DeviceContext.EnumerateMonitorDevices()
                   .Select(x =>
            {
                string alternateDescription = null;
                if (Regex.IsMatch(x.Description, genericPattern, RegexOptions.IgnoreCase))
                {
                    var displayItem = displayItems.FirstOrDefault(y => string.Equals(x.DeviceInstanceId, y.DeviceInstanceId, StringComparison.OrdinalIgnoreCase));
                    if (!string.IsNullOrWhiteSpace(displayItem?.DisplayName))
                    {
                        alternateDescription = displayItem.DisplayName;
                    }
                    else if (!string.IsNullOrEmpty(displayItem?.ConnectionDescription))
                    {
                        alternateDescription = $"{x.Description} ({displayItem.ConnectionDescription})";
                    }
                }
                return new DeviceItemPlus(x, alternateDescription);
            })
                   .ToList());
        }
Exemplo n.º 2
0
        private static async Task <List <DeviceItemPlus> > GetMonitorDevicesAsync()
        {
            var displayItems = OsVersion.Is10Redstone4OrNewer
                                ? await DisplayInformation.GetDisplayMonitorsAsync()
                                : Array.Empty <DisplayInformation.DisplayItem>();

            IEnumerable <DeviceItemPlus> Enumerate()
            {
                foreach (var deviceItem in DeviceContext.EnumerateMonitorDevices())
                {
                    var isDescriptionNullOrWhiteSpace = string.IsNullOrWhiteSpace(deviceItem.Description);
                    if (isDescriptionNullOrWhiteSpace ||
                        Regex.IsMatch(deviceItem.Description, "^Generic (?:PnP|Non-PnP) Monitor$", RegexOptions.IgnoreCase))
                    {
                        var displayItem = displayItems.FirstOrDefault(x => string.Equals(deviceItem.DeviceInstanceId, x.DeviceInstanceId, StringComparison.OrdinalIgnoreCase));
                        if (!string.IsNullOrWhiteSpace(displayItem?.DisplayName))
                        {
                            yield return(new DeviceItemPlus(deviceItem, displayItem.DisplayName));

                            continue;
                        }
                        if (!isDescriptionNullOrWhiteSpace &&
                            !string.IsNullOrWhiteSpace(displayItem?.ConnectionDescription))
                        {
                            yield return(new DeviceItemPlus(deviceItem, $"{deviceItem.Description} ({displayItem.ConnectionDescription})"));

                            continue;
                        }
                    }
                    yield return(new DeviceItemPlus(deviceItem));
                }
            }

            return(Enumerate().Where(x => !string.IsNullOrWhiteSpace(x.AlternateDescription)).ToList());
        }
Exemplo n.º 3
0
        public static bool CheckMonitorsChanged()
        {
            var newIds = new HashSet <string>(DeviceContext.EnumerateMonitorDevices().Select(x => x.DeviceInstanceId));
            var oldIds = _ids;

            _ids = newIds;
            return(oldIds?.SetEquals(newIds) is not true);
        }
Exemplo n.º 4
0
            public async Task PopulateAsync()
            {
                System = GetSystem();

                var sw = new Stopwatch();

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

                    GetTask(nameof(DisplayMonitorItems), async() =>
                    {
                        if (OsVersion.Is10Redstone4OrNewer)
                        {
                            DisplayMonitorItems = await DisplayMonitor.GetDisplayMonitorsAsync();
                        }
                    }),

                    GetTask(nameof(DisplayConfigItems), () =>
                            DisplayConfigItems = DisplayConfig.EnumerateDisplayConfigs().ToArray()),

                    GetTask(nameof(InstalledItems), () =>
                            InstalledItems = DeviceInformation.EnumerateInstalledMonitors().ToArray()),

                    GetTask(nameof(PhysicalItems), () =>
                            PhysicalItems = DeviceContext.GetMonitorHandles().ToDictionary(
                                x => x,
                                x => MonitorConfiguration.EnumeratePhysicalMonitors(x.MonitorHandle, true)
                                .Select(x => new PhysicalItemPlus(x))
                                .ToArray())),

                    GetTask(nameof(DesktopItems), () =>
                            DesktopItems = MSMonitor.EnumerateDesktopMonitors().ToArray())
                };

                sw.Start();

                ElapsedTime = await Task.WhenAll(tasks);

                sw.Stop();

                Task <string> GetTask(string name, Action action) =>
                Task.Run(() =>
                {
                    action.Invoke();
                    var elapsed = sw.Elapsed;
                    return($@"{name,-14} -> {elapsed.ToString($@"{(elapsed.Minutes > 0 ? @"m\:" : string.Empty)}s\.fff")}");
                });
            }
Exemplo n.º 5
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}";
                });
            }
Exemplo n.º 6
0
        private static async Task <List <DeviceItemPlus> > GetMonitorDevicesAsync()
        {
            IDisplayItem[] displayItems = OsVersion.Is10Redstone4OrNewer
                                ? await DisplayMonitor.GetDisplayMonitorsAsync()
                                : DisplayConfig.EnumerateDisplayConfigs().ToArray();

            var deviceItems = DeviceContext.EnumerateMonitorDevices().ToArray();

            _ids = new HashSet <string>(deviceItems.Select(x => x.DeviceInstanceId));

            IEnumerable <DeviceItemPlus> Enumerate()
            {
                foreach (var deviceItem in deviceItems)
                {
                    var displayItem = displayItems.FirstOrDefault(x => string.Equals(deviceItem.DeviceInstanceId, x.DeviceInstanceId, StringComparison.OrdinalIgnoreCase));
                    if (displayItem is null)
                    {
                        yield return(new DeviceItemPlus(deviceItem));
                    }
                    else if (!string.IsNullOrWhiteSpace(displayItem.DisplayName))
                    {
                        yield return(new DeviceItemPlus(deviceItem, displayItem.DisplayName, displayItem.IsInternal));
                    }
                    else if (Regex.IsMatch(deviceItem.Description, "^Generic (?:PnP|Non-PnP) Monitor$", RegexOptions.IgnoreCase) &&
                             !string.IsNullOrWhiteSpace(displayItem.ConnectionDescription))
                    {
                        yield return(new DeviceItemPlus(deviceItem, $"{deviceItem.Description} ({displayItem.ConnectionDescription})", displayItem.IsInternal));
                    }
                    else
                    {
                        yield return(new DeviceItemPlus(deviceItem, null, displayItem.IsInternal));
                    }
                }
            }

            return(Enumerate().Where(x => !string.IsNullOrWhiteSpace(x.AlternateDescription)).ToList());
        }