Exemple #1
0
 public PhysicalItem(
     string description,
     int monitorIndex,
     SafePhysicalMonitorHandle handle,
     bool isHighLevelSupported,
     bool isLowLevelSupported  = false,
     string capabilitiesString = null,
     string capabilitiesReport = null)
 {
     this.Description          = description;
     this.MonitorIndex         = monitorIndex;
     this.Handle               = handle;
     this.IsHighLevelSupported = isHighLevelSupported;
     this.IsLowLevelSupported  = isLowLevelSupported;
     this.CapabilitiesString   = capabilitiesString;
     this.CapabilitiesReport   = capabilitiesReport;
 }
Exemple #2
0
 public DdcMonitorItem(
     string deviceInstanceId,
     string description,
     byte displayIndex,
     byte monitorIndex,
     Rect monitorRect,
     SafePhysicalMonitorHandle handle,
     MonitorCapability capability) : base(
         deviceInstanceId: deviceInstanceId,
         description: description,
         displayIndex: displayIndex,
         monitorIndex: monitorIndex,
         monitorRect: monitorRect,
         isReachable: true)
 {
     this._handle     = handle ?? throw new ArgumentNullException(nameof(handle));
     this._capability = capability ?? throw new ArgumentNullException(nameof(capability));
 }
Exemple #3
0
 public DdcMonitorItem(
     string deviceInstanceId,
     string description,
     byte displayIndex,
     byte monitorIndex,
     Rect monitorRect,
     SafePhysicalMonitorHandle handle,
     bool useHighLevel = true) : base(
         deviceInstanceId: deviceInstanceId,
         description: description,
         displayIndex: displayIndex,
         monitorIndex: monitorIndex,
         monitorRect: monitorRect,
         isReachable: true)
 {
     this._handle       = handle ?? throw new ArgumentNullException(nameof(handle));
     this._useHighLevel = useHighLevel;
 }
        /// <summary>
        /// Sets raw brightness not represented in percentage.
        /// </summary>
        /// <param name="physicalMonitorHandle">Physical monitor handle</param>
        /// <param name="brightness">Raw brightness (not always 0 to 100)</param>
        /// <param name="useLowLevel">Whether to use low level function</param>
        /// <returns>True if successfully sets</returns>
        public static bool SetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle, uint brightness, bool useLowLevel = false)
        {
            if (physicalMonitorHandle is null)
            {
                throw new ArgumentNullException(nameof(physicalMonitorHandle));
            }

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

            if (!useLowLevel)
            {
                if (!SetMonitorBrightness(
                        physicalMonitorHandle,
                        brightness))
                {
                    Debug.WriteLine($"Failed to set brightness. {Error.GetMessage()}");
                    return(false);
                }
            }
            else
            {
                if (!SetVCPFeature(
                        physicalMonitorHandle,
                        LuminanceCode,
                        brightness))
                {
                    Debug.WriteLine($"Failed to set brightness (Low level). {Error.GetMessage()}");
                    return(false);
                }
            }
            return(true);
        }
Exemple #5
0
            static byte[] GetCapabilitiesData(SafePhysicalMonitorHandle physicalMonitorHandle, uint capabilitiesStringLength)
            {
                var dataPointer = IntPtr.Zero;

                try
                {
                    dataPointer = Marshal.AllocHGlobal((int)capabilitiesStringLength);

                    if (CapabilitiesRequestAndCapabilitiesReply(
                            physicalMonitorHandle,
                            dataPointer,
                            capabilitiesStringLength))
                    {
                        var data = new byte[capabilitiesStringLength];
                        Marshal.Copy(dataPointer, data, 0, data.Length);
                        return(data);
                    }
                    return(null);
                }
                finally
                {
                    Marshal.FreeHGlobal(dataPointer);
                }
            }
 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);
        /// <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 gets</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.GetMessage()}");
                    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.GetMessage()}");
                    return(success : false, 0, 0, 0);
                }
                return(success : true,
                       minimum : 0,
                       current : currentValue,
                       maximum : maximumValue);
            }
        }
        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.GetMessage()}");
                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.GetMessage()}");
                    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 _) &&
                                                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.
            }
        }
Exemple #14
0
 private static extern bool CapabilitiesRequestAndCapabilitiesReply(
     SafePhysicalMonitorHandle hMonitor,
     IntPtr pszASCIICapabilitiesString,
     uint dwCapabilitiesStringLengthInCharacters);