コード例 #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;
 }
コード例 #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));
 }
コード例 #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;
 }
コード例 #4
0
        /// <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);
        }
コード例 #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);
                }
            }
コード例 #6
0
 private static extern bool SetVCPFeature(
     SafePhysicalMonitorHandle hMonitor,
     byte bVCPCode,
     uint dwNewValue);
コード例 #7
0
 private static extern bool GetVCPFeatureAndVCPFeatureReply(
     SafePhysicalMonitorHandle hMonitor,
     byte bVCPCode,
     out LPMC_VCP_CODE_TYPE pvct,
     out uint pdwCurrentValue,
     out uint pdwMaximumValue);
コード例 #8
0
 private static extern bool GetCapabilitiesStringLength(
     SafePhysicalMonitorHandle hMonitor,
     out uint pdwCapabilitiesStringLengthInCharacters);
コード例 #9
0
 private static extern bool SetMonitorBrightness(
     SafePhysicalMonitorHandle hMonitor,
     uint dwNewBrightness);
コード例 #10
0
 private static extern bool GetMonitorBrightness(
     SafePhysicalMonitorHandle hMonitor,
     out uint pdwMinimumBrightness,
     out uint pdwCurrentBrightness,
     out uint pdwMaximumBrightness);
コード例 #11
0
 private static extern bool GetMonitorCapabilities(
     SafePhysicalMonitorHandle hMonitor,
     out MC_CAPS pdwMonitorCapabilities,
     out MC_SUPPORTED_COLOR_TEMPERATURE pdwSupportedColorTemperatures);
コード例 #12
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 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);
            }
        }
コード例 #13
0
        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.
            }
        }
コード例 #14
0
 private static extern bool CapabilitiesRequestAndCapabilitiesReply(
     SafePhysicalMonitorHandle hMonitor,
     IntPtr pszASCIICapabilitiesString,
     uint dwCapabilitiesStringLengthInCharacters);