public static int GetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle, bool useLowLevel = false)
        {
            if (physicalMonitorHandle is null)
            {
                throw new ArgumentNullException(nameof(physicalMonitorHandle));
            }

            if (physicalMonitorHandle.IsClosed)
            {
                Debug.WriteLine("Failed to get brightness. The physical monitor handle has been closed.");
                return(-1);
            }

            if (!useLowLevel)
            {
                if (!GetMonitorBrightness(
                        physicalMonitorHandle,
                        out uint minimumBrightness,
                        out uint currentBrightness,
                        out uint maximumBrightness))
                {
                    Debug.WriteLine($"Failed to get brightness. {Error.CreateMessage()}");
                    return(-1);
                }
                return((int)currentBrightness);
            }
            else
            {
                if (!GetVCPFeatureAndVCPFeatureReply(
                        physicalMonitorHandle,
                        LuminanceCode,
                        out LPMC_VCP_CODE_TYPE _,
                        out uint currentValue,
                        out uint maximumValue))
                {
                    Debug.WriteLine($"Failed to get brightness (Low level). {Error.CreateMessage()}");
                    return(-1);
                }
                return((int)currentValue);
            }
        }
        public static bool SetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle, int brightness, bool useLowLevel = false)
        {
            if (physicalMonitorHandle is null)
            {
                throw new ArgumentNullException(nameof(physicalMonitorHandle));
            }
            if ((brightness < 0) || (100 < brightness))
            {
                throw new ArgumentOutOfRangeException(nameof(brightness), $"{nameof(brightness)} must be in the range of 0 to 100.");
            }

            if (physicalMonitorHandle.IsClosed)
            {
                Debug.WriteLine("Failed to set brightness. The physical monitor handle has been closed.");
                return(false);
            }

            if (!useLowLevel)
            {
                if (!SetMonitorBrightness(
                        physicalMonitorHandle,
                        (uint)brightness))
                {
                    Debug.WriteLine($"Failed to set brightness. {Error.CreateMessage()}");
                    return(false);
                }
            }
            else
            {
                if (!SetVCPFeature(
                        physicalMonitorHandle,
                        LuminanceCode,
                        (uint)brightness))
                {
                    Debug.WriteLine($"Failed to set brightness (Low level). {Error.CreateMessage()}");
                    return(false);
                }
            }
            return(true);
        }
 private static extern bool SetVCPFeature(
     SafePhysicalMonitorHandle hMonitor,
     byte bVCPCode,
     uint dwNewValue);
 private static extern bool GetVCPFeatureAndVCPFeatureReply(
     SafePhysicalMonitorHandle hMonitor,
     byte bVCPCode,
     out LPMC_VCP_CODE_TYPE pvct,
     out uint pdwCurrentValue,
     out uint pdwMaximumValue);
 private static extern bool GetCapabilitiesStringLength(
     SafePhysicalMonitorHandle hMonitor,
     out uint pdwCapabilitiesStringLengthInCharacters);
 private static extern bool SetMonitorBrightness(
     SafePhysicalMonitorHandle hMonitor,
     uint dwNewBrightness);
 private static extern bool GetMonitorBrightness(
     SafePhysicalMonitorHandle hMonitor,
     out uint pdwMinimumBrightness,
     out uint pdwCurrentBrightness,
     out uint pdwMaximumBrightness);
 private static extern bool GetMonitorCapabilities(
     SafePhysicalMonitorHandle hMonitor,
     out MC_CAPS pdwMonitorCapabilities,
     out MC_SUPPORTED_COLOR_TEMPERATURE pdwSupportedColorTemperatures);
        public static IEnumerable <PhysicalItem> EnumeratePhysicalMonitors(IntPtr monitorHandle, bool verbose = false)
        {
            if (!GetNumberOfPhysicalMonitorsFromHMONITOR(
                    monitorHandle,
                    out uint count))
            {
                Debug.WriteLine($"Failed to get the number of physical monitors. {Error.CreateMessage()}");
                yield break;
            }
            if (count == 0)
            {
                yield break;
            }

            var physicalMonitors = new PHYSICAL_MONITOR[count];

            try
            {
                if (!GetPhysicalMonitorsFromHMONITOR(
                        monitorHandle,
                        count,
                        physicalMonitors))
                {
                    Debug.WriteLine($"Failed to get an array of physical monitors. {Error.CreateMessage()}");
                    yield break;
                }

                int monitorIndex = 0;

                foreach (var physicalMonitor in physicalMonitors)
                {
                    var handle = new SafePhysicalMonitorHandle(physicalMonitor.hPhysicalMonitor);

                    bool isHighLevelSupported = GetMonitorCapabilities(
                        handle,
                        out MC_CAPS caps,
                        out MC_SUPPORTED_COLOR_TEMPERATURE _) &&
                                                caps.HasFlag(MC_CAPS.MC_CAPS_BRIGHTNESS);

                    bool   isLowLevelSupported = false;
                    string capabilities        = null;

                    if (!isHighLevelSupported || verbose)
                    {
                        if (GetCapabilitiesStringLength(
                                handle,
                                out uint capabilitiesStringLength))
                        {
                            var capabilitiesString = new StringBuilder((int)capabilitiesStringLength);

                            if (CapabilitiesRequestAndCapabilitiesReply(
                                    handle,
                                    capabilitiesString,
                                    capabilitiesStringLength))
                            {
                                capabilities        = capabilitiesString.ToString();
                                isLowLevelSupported = IsLowLevelSupported(capabilities);
                            }
                        }
                    }

                    //Debug.WriteLine($"Description: {physicalMonitor.szPhysicalMonitorDescription}");
                    //Debug.WriteLine($"Handle: {physicalMonitor.hPhysicalMonitor}");
                    //Debug.WriteLine($"IsHighLevelSupported: {isHighLevelSupported}");
                    //Debug.WriteLine($"IsLowLevelSupported: {isLowLevelSupported}");
                    //Debug.WriteLine($"Capabilities: {capabilities}");

                    yield return(new PhysicalItem(
                                     description: physicalMonitor.szPhysicalMonitorDescription,
                                     monitorIndex: monitorIndex,
                                     handle: handle,
                                     isHighLevelSupported: isHighLevelSupported,
                                     isLowLevelSupported: isLowLevelSupported,
                                     capabilities: verbose ? capabilities : null));

                    monitorIndex++;
                }
            }
            finally
            {
                // The physical monitor handles should be destroyed at a later stage.
            }
        }
Example #10
0
        /// <summary>
        /// Gets raw brightnesses not represented in percentage.
        /// </summary>
        /// <param name="physicalMonitorHandle">Physical monitor handle</param>
        /// <param name="useLowLevel">Whether to use low level function</param>
        /// <returns>
        /// <para>success: True if successfully got</para>
        /// <para>minimum: Raw minimum brightness (not always 0)</para>
        /// <para>current: Raw current brightness (not always 0 to 100)</para>
        /// <para>maximum: Raw maximum brightness (not always 100)</para>
        /// </returns>
        /// <remarks>
        /// Raw minimum and maximum brightnesses will become meaningful when they are not standard
        /// values (0 and 100) and so raw current brightness needs to be converted to brightness
        /// in percentage using those values. They are used to convert brightness in percentage
        /// back to raw brightness when settings brightness as well.
        /// </remarks>
        public static (bool success, uint minimum, uint current, uint maximum) GetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle, bool useLowLevel = false)
        {
            if (physicalMonitorHandle is null)
            {
                throw new ArgumentNullException(nameof(physicalMonitorHandle));
            }

            if (physicalMonitorHandle.IsClosed)
            {
                Debug.WriteLine("Failed to get brightnesses. The physical monitor handle has been closed.");
                return(success : false, 0, 0, 0);
            }

            if (!useLowLevel)
            {
                if (!GetMonitorBrightness(
                        physicalMonitorHandle,
                        out uint minimumBrightness,
                        out uint currentBrightness,
                        out uint maximumBrightness))
                {
                    Debug.WriteLine($"Failed to get brightnesses. {Error.CreateMessage()}");
                    return(success : false, 0, 0, 0);
                }
                return(success : true,
                       minimum : minimumBrightness,
                       current : currentBrightness,
                       maximum : maximumBrightness);
            }
            else
            {
                if (!GetVCPFeatureAndVCPFeatureReply(
                        physicalMonitorHandle,
                        LuminanceCode,
                        out _,
                        out uint currentValue,
                        out uint maximumValue))
                {
                    Debug.WriteLine($"Failed to get brightnesses (Low level). {Error.CreateMessage()}");
                    return(success : false, 0, 0, 0);
                }
                return(success : true,
                       minimum : 0,
                       current : currentValue,
                       maximum : maximumValue);
            }
        }