示例#1
0
        private static IEnumerable <MonitorName> GetAllMonitorsFriendlyNames()
        {
            uint pathCount;
            uint modeCount;
            int  error = GetDisplayConfigBufferSizes(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS, out pathCount, out modeCount);

            if ((uint)error > 0U)
            {
                throw new Win32Exception(error);
            }
            DISPLAYCONFIG_PATH_INFO[] displayPaths = new DISPLAYCONFIG_PATH_INFO[(int)pathCount];
            var displayModes = new DISPLAYCONFIG_MODE_INFO[(int)modeCount];

            error = QueryDisplayConfig(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS, ref pathCount, displayPaths, ref modeCount, displayModes, IntPtr.Zero);
            if ((uint)error > 0U)
            {
                throw new Win32Exception(error);
            }
            for (int i = 0; (long)i < (long)modeCount; ++i)
            {
                if (displayModes[i].infoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
                {
                    var sourceMode = displayModes[i + 1];
                    yield return(new MonitorName()
                    {
                        friendlyName = MonitorFriendlyName(displayModes[i].adapterId, displayModes[i].id),
                        sourceName = MonitorSourceName(sourceMode.adapterId, sourceMode.id)
                    });
                }
            }
        }
        public static IEnumerable <string> GetAllMonitorsFriendlyNames()
        {
            uint pathCount, modeCount;
            var  error = GetDisplayConfigBufferSizes(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS, out pathCount, out modeCount);

            if (error != ERROR_SUCCESS)
            {
                throw new Win32Exception(error);
            }

            var displayPaths = new DISPLAYCONFIG_PATH_INFO[pathCount];
            var displayModes = new DISPLAYCONFIG_MODE_INFO[modeCount];

            error = QueryDisplayConfig(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS,
                                       ref pathCount, displayPaths, ref modeCount, displayModes, IntPtr.Zero);
            if (error != ERROR_SUCCESS)
            {
                throw new Win32Exception(error);
            }

            for (var i = 0; i < modeCount; i++)
            {
                if (displayModes[i].infoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
                {
                    yield return(MonitorFriendlyName(displayModes[i].adapterId, displayModes[i].id));
                }
            }
        }
        private static List <QueryMonitorInfo> GetQueryDisplayMonitorInfo()
        {
            List <QueryMonitorInfo> monitors = new List <QueryMonitorInfo>();
            int error = GetDisplayConfigBufferSizes(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS,
                                                    out uint pathCount, out uint modeCount);

            if (error != ERROR_SUCCESS)
            {
                throw new Win32Exception(error);
            }

            DISPLAYCONFIG_PATH_INFO[] displayPaths = new DISPLAYCONFIG_PATH_INFO[pathCount];
            DISPLAYCONFIG_MODE_INFO[] displayModes = new DISPLAYCONFIG_MODE_INFO[modeCount];
            error = QueryDisplayConfig(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS,
                                       ref pathCount, displayPaths, ref modeCount, displayModes, IntPtr.Zero);
            if (error != ERROR_SUCCESS)
            {
                throw new Win32Exception(error);
            }

            Logger.Instance.LogMessage(TracingLevel.INFO, $"QueryDisplay returned {pathCount} paths and {modeCount} modes");
            QueryMonitorInfo currMonitor = new QueryMonitorInfo();

            for (int idx = 0; idx < modeCount; idx++)
            {
                if (idx % 2 == 0)
                {
                    currMonitor = new QueryMonitorInfo
                    {
                        AdapterId = displayModes[idx].adapterId,
                        Id        = displayModes[idx].id
                    };
                }
                else
                {
                    if (currMonitor.AdapterId.HighPart != displayModes[idx].adapterId.HighPart || currMonitor.AdapterId.LowPart != displayModes[idx].adapterId.LowPart)
                    {
                        throw new Exception("Inconsistent Adapter Id");
                    }
                }
                if (displayModes[idx].infoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
                {
                    currMonitor.UserFriendlyName = MonitorFriendlyName(displayModes[idx].adapterId, displayModes[idx].id);
                    currMonitor.DevicePath       = MonitorDevicePath(displayModes[idx].adapterId, displayModes[idx].id);
                }
                else if (displayModes[idx].infoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE)
                {
                    currMonitor.DisplayName = GDI(displayModes[idx].adapterId, displayModes[idx].id);
                }

                if (idx % 2 != 0)
                {
                    monitors.Add(currMonitor);
                }
            }
            Logger.Instance.LogMessage(TracingLevel.INFO, $"QueryDisplay returned {monitors.Count} monitors");
            return(monitors);
        }
示例#4
0
        public static IEnumerable <DisplayItem> EnumerateDisplayConfigs()
        {
            if (GetDisplayConfigBufferSizes(
                    QDC_ONLY_ACTIVE_PATHS,
                    out uint pathCount,
                    out uint modeCount) != ERROR_SUCCESS)
            {
                yield break;
            }

            var displayPaths = new DISPLAYCONFIG_PATH_INFO[pathCount];
            var displayModes = new DISPLAYCONFIG_MODE_INFO[modeCount];

            if (QueryDisplayConfig(
                    QDC_ONLY_ACTIVE_PATHS,
                    ref pathCount,
                    displayPaths,
                    ref modeCount,
                    displayModes,
                    IntPtr.Zero) != ERROR_SUCCESS)
            {
                yield break;
            }

            foreach (var displayPath in displayPaths)
            {
                var displayMode = displayModes
                                  .Where(x => x.infoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
                                  .FirstOrDefault(x => x.id == displayPath.targetInfo.id);
                if (displayMode.Equals(default(DISPLAYCONFIG_MODE_INFO)))
                {
                    continue;
                }

                var deviceName = new DISPLAYCONFIG_TARGET_DEVICE_NAME();
                deviceName.header.size      = (uint)Marshal.SizeOf <DISPLAYCONFIG_TARGET_DEVICE_NAME>();
                deviceName.header.adapterId = displayMode.adapterId;
                deviceName.header.id        = displayMode.id;
                deviceName.header.type      = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;

                if (DisplayConfigGetDeviceInfo(ref deviceName) != ERROR_SUCCESS)
                {
                    continue;
                }

                var deviceInstanceId = DeviceConversion.ConvertToDeviceInstanceId(deviceName.monitorDevicePath);

                yield return(new DisplayItem(
                                 deviceInstanceId: deviceInstanceId,
                                 displayName: deviceName.monitorFriendlyDeviceName,
                                 isInternal: (deviceName.outputTechnology == DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY.DISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL),
                                 connectionDescription: GetConnectionDescription(deviceName.outputTechnology),
                                 isAvailable: displayPath.targetInfo.targetAvailable));
            }
        }
        //Query single monitor
        private static bool QuerySingleMonitorDisplayConfig(int screenNumber, out DISPLAYCONFIG_PATH_INFO pathInfoTarget, out DISPLAYCONFIG_MODE_INFO modeInfoTarget)
        {
            try
            {
                //Check screen number
                if (screenNumber < 0)
                {
                    screenNumber = 0;
                }

                //Query all monitors
                QueryAllMonitorDisplayConfig(out uint displayPathCount, out uint displayModeCount, out DISPLAYCONFIG_PATH_INFO[] displayPaths, out DISPLAYCONFIG_MODE_INFO[] displayModes);

                //Check all monitors
                int monitorIndex  = 0;
                int pathInfoIndex = 0;
                pathInfoTarget = new DISPLAYCONFIG_PATH_INFO();
                modeInfoTarget = new DISPLAYCONFIG_MODE_INFO();
                foreach (DISPLAYCONFIG_PATH_INFO pathInfo in displayPaths)
                {
                    try
                    {
                        //Validate monitor
                        if (pathInfo.targetInfo.targetAvailable && pathInfo.sourceInfo.modeInfoIdx >= 0 && pathInfo.sourceInfo.modeInfoIdx < displayModeCount)
                        {
                            //Check the monitor id
                            if (screenNumber == monitorIndex)
                            {
                                pathInfoTarget = displayPaths[pathInfoIndex];
                                modeInfoTarget = displayModes[pathInfoIndex];
                                break;
                            }
                            monitorIndex++;
                        }
                    }
                    catch { }
                    pathInfoIndex++;
                }
                return(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to query single monitor: " + ex.Message);
                pathInfoTarget = new DISPLAYCONFIG_PATH_INFO();
                modeInfoTarget = new DISPLAYCONFIG_MODE_INFO();
                return(false);
            }
        }
        //Query all monitors
        private static bool QueryAllMonitorDisplayConfig(out uint displayPathCount, out uint displayModeCount, out DISPLAYCONFIG_PATH_INFO[] displayPaths, out DISPLAYCONFIG_MODE_INFO[] displayModes)
        {
            try
            {
                int error = GetDisplayConfigBufferSizes(QUERY_DEVICE_CONFIG_FLAGS.QDC_ALL_PATHS, out displayPathCount, out displayModeCount);
                if (error != 0)
                {
                    Debug.WriteLine("Failed GetDisplayConfigBufferSizes: " + error);
                    displayPathCount = 0;
                    displayModeCount = 0;
                    displayPaths     = null;
                    displayModes     = null;
                    return(false);
                }

                displayPaths = new DISPLAYCONFIG_PATH_INFO[displayPathCount];
                displayModes = new DISPLAYCONFIG_MODE_INFO[displayModeCount];
                error        = QueryDisplayConfig(QUERY_DEVICE_CONFIG_FLAGS.QDC_ALL_PATHS, ref displayPathCount, displayPaths, ref displayModeCount, displayModes, DISPLAYCONFIG_FLAGS.SDC_TOPOLOGY_NONE);
                if (error != 0)
                {
                    Debug.WriteLine("Failed QueryDisplayConfig: " + error);
                    displayPathCount = 0;
                    displayModeCount = 0;
                    displayPaths     = null;
                    displayModes     = null;
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to query all monitor: " + ex.Message);
                displayPathCount = 0;
                displayModeCount = 0;
                displayPaths     = null;
                displayModes     = null;
                return(false);
            }
        }
        public static string[] GetAllMonitorsFriendlyNames()
        {
            if (CoreHelper.RunningOnVista || CoreHelper.RunningOnXP)
            {
                return(GetDefaultScreenNames());
            }

            uint pathCount, modeCount;
            var  error = NativeMethods.GetDisplayConfigBufferSizes(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS, out pathCount,
                                                                   out modeCount);

            if (error != ERROR_SUCCESS)
            {
                return(GetDefaultScreenNames());
            }

            var displayPaths = new DISPLAYCONFIG_PATH_INFO[pathCount];
            var displayModes = new DISPLAYCONFIG_MODE_INFO[modeCount];

            error = NativeMethods.QueryDisplayConfig(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS,
                                                     ref pathCount, displayPaths, ref modeCount, displayModes, IntPtr.Zero);
            if (error != ERROR_SUCCESS)
            {
                return(GetDefaultScreenNames());
            }
            //throw new Win32Exception(error);

            var screenNames = new List <string>();

            for (var i = 0; i < modeCount; i++)
            {
                if (displayModes[i].infoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
                {
                    screenNames.Add(MonitorFriendlyName(displayModes[i].adapterId, displayModes[i].id));
                }
            }

            return(screenNames.ToArray());
        }
示例#8
0
        public static IEnumerable<string> GetAllMonitorsFriendlyNames()
        {
            uint pathCount, modeCount;
            var error = GetDisplayConfigBufferSizes(QUERY_DEVICE_CONFIG_FLAGS.QDC_DATABASE_CURRENT, out pathCount, out modeCount);
            if (error != ERROR_SUCCESS)
                throw new Win32Exception(error);

            var displayPaths = new DISPLAYCONFIG_PATH_INFO[pathCount];
            var displayModes = new DISPLAYCONFIG_MODE_INFO[modeCount];

            error = QueryDisplayConfig(QUERY_DEVICE_CONFIG_FLAGS.QDC_DATABASE_CURRENT,
                ref pathCount, displayPaths, ref modeCount, displayModes, IntPtr.Zero);

            if (error != ERROR_SUCCESS)
                throw new Win32Exception(error);

            for (var i = 0; i < modeCount; i++)
                if (displayModes[i].infoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
                    yield return MonitorFriendlyName(displayModes[i].adapterId, displayModes[i].id);
        }
示例#9
0
        public static ShaderHdrMetadata GetHdrMetadataForMonitor(string deviceName)
        {
            var err = BetterWin32Errors.Win32Error.ERROR_SUCCESS;
            // TODO: also query monitor HDR white point brightness (currently hardcoded to 800.0f)
            var hdrMetadata = new ShaderHdrMetadata {
                EnableHdrProcessing = false, MonSdrDispNits = 80.0f, ExposureLevel = 1.0f, MonHdrDispNits = 800.0f
            };
            var  monAdvColorInfo          = DISPLAYCONFIG_GET_ADVANCED_COLOR_INFO.CreateGet();
            var  monSdrWhiteLevel         = DISPLAYCONFIG_SDR_WHITE_LEVEL.CreateGet();
            uint numPathArrayElements     = 0;
            uint numModeInfoArrayElements = 0;

            err = GdiInterop.GetDisplayConfigBufferSizes(QDC_CONSTANT.QDC_ONLY_ACTIVE_PATHS,
                                                         ref numPathArrayElements, ref numModeInfoArrayElements);
            if (err != BetterWin32Errors.Win32Error.ERROR_SUCCESS)
            {
                throw new System.ComponentModel.Win32Exception((int)err);
            }

            var displayPathInfoArray = new DISPLAYCONFIG_PATH_INFO[numPathArrayElements];
            var displayModeInfoArray = new DISPLAYCONFIG_MODE_INFO[numModeInfoArrayElements];

            err = GdiInterop.QueryDisplayConfig(QDC_CONSTANT.QDC_ONLY_ACTIVE_PATHS,
                                                ref numPathArrayElements, displayPathInfoArray,
                                                ref numModeInfoArrayElements, displayModeInfoArray, IntPtr.Zero);
            if (err != BetterWin32Errors.Win32Error.ERROR_SUCCESS)
            {
                throw new System.ComponentModel.Win32Exception((int)err);
            }

            for (uint pathIdx = 0; pathIdx < numPathArrayElements; pathIdx++)
            {
                DISPLAYCONFIG_SOURCE_DEVICE_NAME srcName = DISPLAYCONFIG_SOURCE_DEVICE_NAME.CreateGet();
                srcName.header.adapterId.HighPart = displayPathInfoArray[pathIdx].sourceInfo.adapterId.HighPart;
                srcName.header.adapterId.LowPart  = displayPathInfoArray[pathIdx].sourceInfo.adapterId.LowPart;
                srcName.header.id = displayPathInfoArray[pathIdx].sourceInfo.id;

                err = GdiInterop.DisplayConfigGetDeviceInfo(ref srcName);
                if (err != BetterWin32Errors.Win32Error.ERROR_SUCCESS)
                {
                    throw new System.ComponentModel.Win32Exception((int)err);
                }

                if (srcName.DeviceName == deviceName)
                {
                    // If matches, proceed to query color information
                    monAdvColorInfo.header.adapterId.HighPart = displayPathInfoArray[pathIdx].targetInfo.adapterId.HighPart;
                    monAdvColorInfo.header.adapterId.LowPart  = displayPathInfoArray[pathIdx].targetInfo.adapterId.LowPart;
                    monAdvColorInfo.header.id = displayPathInfoArray[pathIdx].targetInfo.id;

                    monSdrWhiteLevel.header.adapterId.HighPart = displayPathInfoArray[pathIdx].targetInfo.adapterId.HighPart;
                    monSdrWhiteLevel.header.adapterId.LowPart  = displayPathInfoArray[pathIdx].targetInfo.adapterId.LowPart;
                    monSdrWhiteLevel.header.id = displayPathInfoArray[pathIdx].targetInfo.id;

                    err = GdiInterop.DisplayConfigGetDeviceInfo(ref monAdvColorInfo);
                    if (err != BetterWin32Errors.Win32Error.ERROR_SUCCESS)
                    {
                        throw new System.ComponentModel.Win32Exception((int)err);
                    }

                    hdrMetadata.EnableHdrProcessing = (monAdvColorInfo.AdvancedColorStatus & AdvancedColorStatus.AdvancedColorEnabled) == AdvancedColorStatus.AdvancedColorEnabled;
                    if (hdrMetadata.EnableHdrProcessing)
                    {
                        err = GdiInterop.DisplayConfigGetDeviceInfo(ref monSdrWhiteLevel);
                        if (err != BetterWin32Errors.Win32Error.ERROR_SUCCESS)
                        {
                            throw new System.ComponentModel.Win32Exception((int)err);
                        }

                        hdrMetadata.MonSdrDispNits = monSdrWhiteLevel.SDRWhiteLevelInNits;
                    }

                    break;
                }
            }

            return(hdrMetadata);
        }