Example #1
0
        private uint _maximum = 100; // Raw maximum brightness (not always 100)

        public void ReadDeviceBrightness()
        {
            var(success, minimum, current, maximum) = MonitorApi.GetBrightness(_handle);

            if (!success || !(minimum < maximum) || !(minimum <= current) || !(current <= maximum))
            {
                Brightness = -1;
                return;
            }

            Brightness = (int)Math.Round((double)(current - minimum) / (maximum - minimum) * 100D,
                                         MidpointRounding.AwayFromZero);
            _minimum = minimum;
            _maximum = maximum;
        }
Example #2
0
        public static IEnumerable <DdcMonitorItem> EnumerateMonitors()
        {
            var deviceItems = DeviceContext.EnumerateMonitorDevices().ToList();

            if (deviceItems.Any() != true)
            {
                yield break;
            }

            // Obtained by DDC/CI
            foreach (var handleItem in DeviceContext.GetMonitorHandles())
            {
                foreach (var physicalItem in MonitorApi.EnumeratePhysicalMonitors(handleItem.MonitorHandle))
                {
                    var index = -1;
                    if (physicalItem.IsSupported)
                    {
                        index = deviceItems.FindIndex(x =>
                                                      x.DisplayIndex == handleItem.DisplayIndex &&
                                                      x.MonitorIndex == physicalItem.MonitorIndex &&
                                                      string.Equals(x.Description, physicalItem.Description, StringComparison.OrdinalIgnoreCase));
                    }

                    if (index < 0)
                    {
                        physicalItem.Handle.Dispose();
                        continue;
                    }

                    var deviceItem = deviceItems[index];
                    yield return(new DdcMonitorItem(
                                     deviceItem.DeviceInstanceId,
                                     deviceItem.Description,
                                     deviceItem.DisplayIndex,
                                     deviceItem.MonitorIndex,
                                     physicalItem.Handle));

                    deviceItems.RemoveAt(index);
                    if (deviceItems.Count == 0)
                    {
                        yield break;
                    }
                }
            }
        }
Example #3
0
        public bool SetDeviceBrightness(int brightness)
        {
            if ((brightness < 0) || (100 < brightness))
            {
                throw new ArgumentOutOfRangeException(nameof(brightness), brightness,
                                                      "The brightness must be within 0 to 100.");
            }

            var buffer = (uint)Math.Round(brightness / 100D * (_maximum - _minimum) + _minimum,
                                          MidpointRounding.AwayFromZero);

            if (!MonitorApi.SetBrightness(_handle, buffer))
            {
                return(false);
            }

            Brightness = brightness;
            return(true);
        }
Example #4
0
        public override bool IsInvalid => false; // The validity cannot be checked by the handle.

        protected override bool ReleaseHandle()
        {
            return(MonitorApi.DestroyPhysicalMonitor(handle));
        }