Skip to content

feat(ambient-brightness): add ambient light sensor based auto brightn… - #118

Open
fly602 wants to merge 1 commit into
linuxdeepin:masterfrom
fly602:master
Open

feat(ambient-brightness): add ambient light sensor based auto brightn…#118
fly602 wants to merge 1 commit into
linuxdeepin:masterfrom
fly602:master

Conversation

@fly602

@fly602 fly602 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

…ess plugin

Add a new ambient-brightness plugin that reads lux from iio-sensor-proxy, applies filtering, hysteresis, and debouncing, and publishes recommended brightness via DBus (org.deepin.dde.AmbientBrightness1). Refactor the power plugin to remove the old DConfig-based ambient brightness toggle, add isAmbientBrightnessActive() guard to PowerSavePlan so brightness save-plan operations don't conflict with ambient auto brightness. Remove redundant auto-brightness disable logic from dde-shortcut-tool since Display1.ChangeBrightness already calls prepareManualBrightnessChange() which disables ambient brightness via DBus.

新增环境光自动亮度插件,从 iio-sensor-proxy 读取 lux 值,经过滤波、滞回
和防抖处理后通过 DBus 发布推荐亮度。重构 power 插件,删除旧的 DConfig
自动亮度开关,在 PowerSavePlan 中添加 isAmbientBrightnessActive 守卫避免 省电计划与自动亮度冲突。移除 dde-shortcut-tool 中冗余的自动亮度关闭逻辑
(Display1.ChangeBrightness 已通过 prepareManualBrightnessChange 处理)。

Log: add ambient brightness plugin and refactor power/shortcut for auto brightness

Summary by Sourcery

Introduce a new ambient brightness plugin that exposes automatic brightness recommendations over DBus and integrates it with existing power and shortcut components.

New Features:

  • Add a session-level ambient-brightness plugin that reads lux from iio-sensor-proxy, applies filtering and hysteresis, and publishes recommended brightness via org.deepin.dde.AmbientBrightness1 on DBus.

Enhancements:

  • Integrate ambient brightness state with the power manager so power save brightness adjustments are skipped while auto brightness is active.
  • Remove legacy DConfig-based ambient auto brightness toggles and redundant manual disable logic from power and shortcut components, relying on the new ambient brightness service instead.
  • Add build and test infrastructure for the ambient-brightness plugin, including CMake setup, unit tests and Python-based tooling for sensor simulation and scenario testing.

Build:

  • Extend top-level and plugin CMake configuration to build and install the new ambient-brightness module and its tests.

Documentation:

  • Add README documentation for the ambient-brightness plugin and its continuous policy, configuration, lifecycle, and testing tools.

Tests:

  • Add unit tests for the continuous ambient light policy, brightness curve mapping, lifecycle state handling, model behavior, and ramp application tooling.
  • Add shell/Python-based scenario test scripts and tools to simulate ambient light changes and validate auto brightness ramp behavior.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @fly602, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fly602

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Reviewer's Guide

Add a new ambient-brightness plugin that exposes org.deepin.dde.AmbientBrightness1 on D-Bus, implements continuous ambient light based brightness recommendation with hysteresis/debounce and lifecycle handling, wires it into the Qt plugin build, and refactors existing power and shortcut code to rely on the new DBus state instead of the old DConfig toggle, preventing conflicts with power save brightness adjustments.

Sequence diagram for ambient brightness recommendation and power-plan guard

sequenceDiagram
    participant SensorProxy as net_hadess_SensorProxy
    participant AmbientBrightnessService
    participant AmbientBrightnessModel
    participant ContinuousAmbientLightPolicy
    participant AmbientBrightness1 as org_deepin_dde_AmbientBrightness1
    participant SessionDBusProxy
    participant PowerManager
    participant PowerSavePlan

    SensorProxy->>AmbientBrightnessService: PropertiesChanged(LightLevel)
    AmbientBrightnessService->>AmbientBrightnessModel: submitSample(lux, timestamp)
    AmbientBrightnessModel->>ContinuousAmbientLightPolicy: update(SensorSample)
    ContinuousAmbientLightPolicy-->>AmbientBrightnessModel: Recommendation
    AmbientBrightnessModel-->>AmbientBrightnessService: recommendedBrightnessChanged(brightness)
    AmbientBrightnessService-->>AmbientBrightness1: PropertiesChanged(State=Active, RecommendedBrightness)

    PowerSavePlan->>PowerManager: isAmbientBrightnessActive()
    PowerManager->>SessionDBusProxy: isAmbientBrightnessActive()
    SessionDBusProxy->>AmbientBrightness1: get Property(State)
    AmbientBrightness1-->>SessionDBusProxy: State
    SessionDBusProxy-->>PowerManager: bool
    alt ambient brightness active
        PowerSavePlan-->>PowerSavePlan: [skip brightness change]
    else inactive
        PowerSavePlan-->>PowerSavePlan: applyBrightnessDrop()/resetBrightness()
    end
Loading

File-Level Changes

Change Details Files
Introduce a session-level ambient brightness service and continuous policy implementation driven by iio-sensor-proxy and DConfig.
  • Implement AmbientBrightnessService to manage sensor DBus interfaces, lifecycle (lid, sleep, login1 session), algorithm configuration, and org.deepin.dde.AmbientBrightness1 properties/signals.
  • Add AmbientBrightnessModel and an AmbientBrightnessPolicy abstraction, plus a ContinuousAmbientLightPolicy with ring buffer, weighted windows/raw modes, hysteresis, debounce and brightness recommendation logic.
  • Provide BrightnessCurve for log1p(lux)-space interpolation, a policy factory that reads DConfig JSON to build and configure the continuous policy, and tests for policy, curve, model and lifecycle.
  • Add debugging/testing scripts and Python/unittest harness for ambient-light-tool ramp behavior and scenario scripts.
src/plugin-qt/ambient-brightness/ambientbrightnessservice.cpp
src/plugin-qt/ambient-brightness/ambientbrightnessservice.h
src/plugin-qt/ambient-brightness/ambientbrightnessmodel.cpp
src/plugin-qt/ambient-brightness/ambientbrightnessmodel.h
src/plugin-qt/ambient-brightness/ambientbrightnesspolicy.h
src/plugin-qt/ambient-brightness/ambientbrightnesspolicyfactory.cpp
src/plugin-qt/ambient-brightness/ambientbrightnesspolicyfactory.h
src/plugin-qt/ambient-brightness/continuous/continuousambientlightpolicy.cpp
src/plugin-qt/ambient-brightness/continuous/continuousambientlightpolicy.h
src/plugin-qt/ambient-brightness/brightnesscurve.cpp
src/plugin-qt/ambient-brightness/brightnesscurve.h
src/plugin-qt/ambient-brightness/ambientlightlifecyclestate.h
src/plugin-qt/ambient-brightness/ambientbrightnesslogging.cpp
src/plugin-qt/ambient-brightness/ambientbrightnesslogging.h
src/plugin-qt/ambient-brightness/plugin.cpp
src/plugin-qt/ambient-brightness/CMakeLists.txt
src/plugin-qt/ambient-brightness/tests/*
src/plugin-qt/ambient-brightness/scripts/*
src/plugin-qt/ambient-brightness/configs/org.deepin.dde.daemon.ambient-brightness.json
src/plugin-qt/ambient-brightness/misc/ambient-brightness.service
src/plugin-qt/ambient-brightness/misc/plugin-ambient-brightness.json
Integrate the ambient-brightness plugin into the Qt plugin build and enable CTest-based testing.
  • Include CTest at top-level and in the ambient-brightness CMake to enable BUILD_TESTING.
  • Add ambient-brightness subdirectory to src/plugin-qt/CMakeLists.txt and define a MODULE library linked against Qt Core/DBus and Dtk Core/DConfig.
  • Install the plugin module, service-manager JSON, DBus service file, and DConfig metadata into appropriate install dirs.
  • Add CMake test targets for continuous policy, brightness curve, model, lifecycle, and Python-based ambient-light-tool ramp tests.
CMakeLists.txt
src/plugin-qt/CMakeLists.txt
src/plugin-qt/ambient-brightness/CMakeLists.txt
src/plugin-qt/ambient-brightness/tests/CMakeLists.txt
Expose ambient brightness active state to the power session via DBus and guard power save brightness operations when auto brightness is active.
  • Extend SessionDBusProxy to create an org.deepin.dde.AmbientBrightness1 DDBusInterface and add isAmbientBrightnessActive() reading its State property.
  • Add PowerManager::isAmbientBrightnessActive() delegating to the proxy, and use it in PowerSavePlan::resetBrightness/applyBrightnessDrop to skip brightness changes and clear m_oldBrightness when ambient auto brightness is active.
  • Initialize login1 session and power lifecycle hooks inside AmbientBrightnessService to coordinate enabling/disabling sensor claims with power events.
src/plugin-qt/power/session/sessiondbusproxy.cpp
src/plugin-qt/power/session/sessiondbusproxy.h
src/plugin-qt/power/session/powermanager.cpp
src/plugin-qt/power/session/powermanager.h
src/plugin-qt/power/session/powersaveplan.cpp
Remove legacy DConfig-based ambient brightness toggle and redundant shortcut-side disabling logic now that Display1/ambient-brightness service coordinate auto brightness state.
  • Delete AmbientLightAdjustBrightness Q_PROPERTY, setter/getter, backing field and DConfig init wiring from PowerManager and PowerDConfig.
  • Remove the KEY_AMBIENT_LIGHT_ADJUST_BRIGHTNESS constant from dde-shortcut-tool constants.
  • Simplify DisplayController::changeBrightness by removing DConfig access and ambientLightAdjustBrightness disabling, relying on Display1.ChangeBrightness/prepareManualBrightnessChange to handle DBus-based ambient brightness disable.
  • Drop DConfig include and namespace use from dde-shortcut-tool displaycontroller.cpp.
src/plugin-qt/power/session/powermanager.cpp
src/plugin-qt/power/session/powermanager.h
src/plugin-qt/power/powerconstants.h
src/plugin-qt/shortcut/tools/dde-shortcut-tool/constant.h
src/plugin-qt/shortcut/tools/dde-shortcut-tool/displaycontroller.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@fly602
fly602 force-pushed the master branch 3 times, most recently from f833acb to 8ae717f Compare July 30, 2026 13:04
1. Add an ambient brightness service backed by iio-sensor-proxy
2. Convert lux samples into stable brightness recommendations with filtering, hysteresis, and debounce
3. Coordinate sensor lifecycle with lid, sleep, session, service, and configuration state

Influence:
1. Publish automatic brightness state and recommendations through org.deepin.dde.AmbientBrightness1
2. Prevent power-saving and manual brightness paths from conflicting with ambient brightness
3. Verify sensor lifecycle and brightness policy with unit tests

fix: 支持环境光自动亮度调节

1. 新增基于iio-sensor-proxy的环境光亮度服务
2. 通过滤波、滞回和防抖将lux样本转换为稳定的亮度推荐值
3. 根据合盖、休眠、会话、传感器服务及配置状态管理光感生命周期

Influence:
1. 通过org.deepin.dde.AmbientBrightness1发布自动亮度状态及推荐值
2. 避免省电及手动亮度调节路径与环境光自动亮度冲突
3. 通过单元测试验证光感生命周期及亮度策略

PMS: BUG-372191
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码实现了环境光自动亮度推荐功能的独立化拆分,架构清晰且无安全漏洞
逻辑正确、分层合理、包含完整单元测试,因部分边界处理可进一步优化扣5分

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓
    具体分析内容:新增的 ContinuousAmbientLightPolicy::updateevaluate 方法逻辑严密,对样本的有效性、时间戳单调性进行了校验;环形缓冲区 AmbientLightRingBuffer 的扩容和裁剪逻辑正确;BrightnessCurve::map 使用 log1p 空间插值并处理了边界情况;移除 PowerManagerDisplayController 中的旧逻辑干净利落,未引入编译错误。
    潜在问题:AmbientLightRingBuffer::prune 方法中 while (m_count > 1 && timeAt(1) <= minTimeMs) 保留了可能过期的前一个样本,虽然用于区间计算是正确的,但在极端高频采样下可能导致缓冲区未及时收缩;plugin.cpp 中使用全局变量 g_service 管理生命周期,若 DSMRegister 被并发调用可能存在竞态。
    建议:考虑在 prune 中增加对 timeAt(0) 的判断或在注释中明确保留最旧样本的设计意图;为 DSMRegister 添加互斥锁或明确其单线程调用约束。
  • 2.代码质量(优秀)✓
    具体分析内容:代码分层清晰(Service/Model/Policy/Factory),职责单一;命名规范符合 Qt 和 C++ 惯例;注释详尽,包含完整的 README.md 解释算法原理和状态机;包含覆盖核心逻辑的单元测试 tst_ambientbrightnesspolicy.cpp;DConfig 配置项有完善的 JSON 元数据。
    潜在问题:ambientbrightnessservice.cpp 中存在部分重复的 D-Bus 连接断开逻辑(如 connectSensor 失败时的清理代码与 disconnectSensor 高度相似)。
    建议:提取 D-Bus 连接清理的公共方法,减少代码重复。
  • 3.代码性能(高效)✓
    具体分析内容:环形缓冲区采用倍增扩容策略,适合高频传感器数据;曲线映射使用 std::upper_bound 进行二分查找,复杂度为 O(log N);定时器采用 single-shot 模式按需触发,避免无效轮询;加权计算仅遍历窗口内有效样本。
    潜在问题:calculateWeightedAmbientLux 在每次 evaluate 时都会遍历窗口,若窗口极大且采样极高,可能存在轻微开销。
    建议:当前性能完全满足需求,无需特别优化。
  • 4.代码安全(存在0个安全漏洞)✓
    漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
    总体风险描述:代码主要处理 D-Bus 通信和数值计算,未涉及命令执行、文件路径拼接或 SQL 操作。DConfig 的 JSON 解析使用 Qt 标准库,安全可靠。移除旧逻辑未引入新的攻击面。

  • 建议:保持当前的安全编码实践。

■ 【改进建议代码示例】

// src/plugin-qt/ambient-brightness/plugin.cpp
#include "ambientbrightnessservice.h"

#include <QDBusConnection>
#include <QMutex>
#include <QMutexLocker>

using dde::ambient_brightness::AmbientBrightnessService;

static AmbientBrightnessService *g_service = nullptr;
static QMutex g_serviceMutex;

extern "C" int DSMRegister(const char *, void *data)
{
    QMutexLocker locker(&g_serviceMutex);
    auto *connection = reinterpret_cast<QDBusConnection *>(data);
    if (!connection)
        return -1;
    
    // 防御重复注册:先释放可能存在的旧实例,避免覆盖全局指针时泄漏。
    delete g_service;
    g_service = nullptr;
    
    g_service = new AmbientBrightnessService(*connection);
    if (!g_service->initialize()) {
        delete g_service;
        g_service = nullptr;
        return -1;
    }
    return 0;
}

extern "C" int DSMUnRegister(const char *, void *)
{
    QMutexLocker locker(&g_serviceMutex);
    delete g_service;
    g_service = nullptr;
    return 0;
}

@deepin-bot

deepin-bot Bot commented Aug 3, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 1.0.37
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #124

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants