mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 03:07:56 +01:00
Refactor DDC/CI brightness initialization logic
Move brightness setup from MonitorDiscoveryHelper to DdcCiController to avoid slow I2C operations during monitor discovery. Set default brightness to 50 and update after discovery. Remove unused brightness methods and type aliases. Update comments to clarify initialization responsibilities.
This commit is contained in:
@@ -418,6 +418,9 @@ namespace PowerDisplay.Common.Drivers.DDC
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize brightness (always supported for DDC/CI monitors)
|
||||
InitializeBrightness(monitor, candidate.Handle);
|
||||
|
||||
monitors.Add(monitor);
|
||||
newHandleMap[monitor.Id] = candidate.Handle;
|
||||
|
||||
@@ -452,6 +455,19 @@ namespace PowerDisplay.Common.Drivers.DDC
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize brightness value for a monitor using VCP 0x10.
|
||||
/// </summary>
|
||||
private static void InitializeBrightness(Monitor monitor, IntPtr handle)
|
||||
{
|
||||
if (GetVCPFeatureAndVCPFeatureReply(handle, VcpCodeBrightness, IntPtr.Zero, out uint current, out uint max))
|
||||
{
|
||||
var brightnessInfo = new VcpFeatureValue((int)current, 0, (int)max);
|
||||
monitor.CurrentBrightness = brightnessInfo.ToPercentage();
|
||||
Logger.LogDebug($"[{monitor.Id}] Brightness: {monitor.CurrentBrightness}%");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update monitor capability flags based on parsed VCP capabilities.
|
||||
/// </summary>
|
||||
|
||||
@@ -7,19 +7,10 @@ using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using ManagedCommon;
|
||||
using static PowerDisplay.Common.Drivers.NativeConstants;
|
||||
using static PowerDisplay.Common.Drivers.NativeDelegates;
|
||||
using static PowerDisplay.Common.Drivers.PInvoke;
|
||||
|
||||
// Type aliases for Windows API naming conventions compatibility
|
||||
using DISPLAY_DEVICE = PowerDisplay.Common.Drivers.DisplayDevice;
|
||||
using DISPLAYCONFIG_DEVICE_INFO_HEADER = PowerDisplay.Common.Drivers.DISPLAYCONFIG_DEVICE_INFO_HEADER;
|
||||
using DISPLAYCONFIG_MODE_INFO = PowerDisplay.Common.Drivers.DISPLAYCONFIG_MODE_INFO;
|
||||
using DISPLAYCONFIG_PATH_INFO = PowerDisplay.Common.Drivers.DISPLAYCONFIG_PATH_INFO;
|
||||
using DISPLAYCONFIG_TARGET_DEVICE_NAME = PowerDisplay.Common.Drivers.DISPLAYCONFIG_TARGET_DEVICE_NAME;
|
||||
using LUID = PowerDisplay.Common.Drivers.Luid;
|
||||
using MONITORINFOEX = PowerDisplay.Common.Drivers.MonitorInfoEx;
|
||||
using PHYSICAL_MONITOR = PowerDisplay.Common.Drivers.PhysicalMonitor;
|
||||
using RECT = PowerDisplay.Common.Drivers.Rect;
|
||||
|
||||
#pragma warning disable SA1649 // File name should match first type name - Multiple related types for DDC/CI
|
||||
#pragma warning disable SA1402 // File may only contain a single type - Related DDC/CI types grouped together
|
||||
|
||||
@@ -6,11 +6,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using ManagedCommon;
|
||||
using PowerDisplay.Common.Models;
|
||||
using PowerDisplay.Common.Utils;
|
||||
using static PowerDisplay.Common.Drivers.NativeConstants;
|
||||
using static PowerDisplay.Common.Drivers.PInvoke;
|
||||
|
||||
using MONITORINFOEX = PowerDisplay.Common.Drivers.MonitorInfoEx;
|
||||
using PHYSICAL_MONITOR = PowerDisplay.Common.Drivers.PhysicalMonitor;
|
||||
|
||||
namespace PowerDisplay.Common.Drivers.DDC
|
||||
@@ -100,6 +97,8 @@ namespace PowerDisplay.Common.Drivers.DDC
|
||||
/// <summary>
|
||||
/// Create Monitor object from physical monitor and display info.
|
||||
/// Uses MonitorDisplayInfo directly from QueryDisplayConfig for stable identification.
|
||||
/// Note: Brightness is not initialized here - MonitorManager handles brightness initialization
|
||||
/// after discovery to avoid slow I2C operations during the discovery phase.
|
||||
/// </summary>
|
||||
/// <param name="physicalMonitor">Physical monitor structure with handle and description</param>
|
||||
/// <param name="monitorInfo">Display info from QueryDisplayConfig (HardwareId, FriendlyName, MonitorNumber)</param>
|
||||
@@ -131,14 +130,11 @@ namespace PowerDisplay.Common.Drivers.DDC
|
||||
name = "External Display";
|
||||
}
|
||||
|
||||
// Get current brightness
|
||||
var brightnessInfo = GetCurrentBrightness(physicalMonitor.HPhysicalMonitor);
|
||||
|
||||
var monitor = new Monitor
|
||||
{
|
||||
Id = monitorId,
|
||||
Name = name.Trim(),
|
||||
CurrentBrightness = brightnessInfo.IsValid ? brightnessInfo.ToPercentage() : 50,
|
||||
CurrentBrightness = 50, // Default value, will be updated by MonitorManager after discovery
|
||||
MinBrightness = 0,
|
||||
MaxBrightness = 100,
|
||||
IsAvailable = true,
|
||||
@@ -165,23 +161,10 @@ namespace PowerDisplay.Common.Drivers.DDC
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get current brightness using VCP code 0x10
|
||||
/// </summary>
|
||||
private VcpFeatureValue GetCurrentBrightness(IntPtr handle)
|
||||
{
|
||||
if (GetVCPFeatureAndVCPFeatureReply(handle, VcpCodeBrightness, IntPtr.Zero, out uint current, out uint max))
|
||||
{
|
||||
return new VcpFeatureValue((int)current, 0, (int)max);
|
||||
}
|
||||
|
||||
return VcpFeatureValue.Invalid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract manufacturer from name
|
||||
/// </summary>
|
||||
private string ExtractManufacturer(string name)
|
||||
private static string ExtractManufacturer(string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
|
||||
@@ -79,7 +79,8 @@ namespace PowerDisplay.Helpers
|
||||
|
||||
/// <summary>
|
||||
/// Discover all monitors from all controllers.
|
||||
/// All initialization (brightness, capabilities, input source) is done during controller discovery.
|
||||
/// Each controller is responsible for fully initializing its monitors
|
||||
/// (including brightness, capabilities, input source, color temperature, etc.)
|
||||
/// </summary>
|
||||
public async Task<IReadOnlyList<Monitor>> DiscoverMonitorsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user