Skip to content

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

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
fly602:master
Aug 17, 2026
Merged

feat(ambient-brightness): add ambient light sensor based auto brightn…#118
deepin-bot[bot] merged 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

@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 4 times, most recently from 8ae717f to e346e81 Compare July 31, 2026 06:59
@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

@fly602
fly602 force-pushed the master branch 3 times, most recently from 29e7d37 to 2f2ae80 Compare August 4, 2026 08:01
@deepin-bot

deepin-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 1.0.38
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #135

@deepin-bot

deepin-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 1.0.39
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #142

Comment thread src/plugin-qt/ambient-brightness/continuous/continuousambientlightpolicy.cpp Outdated
Comment thread src/plugin-qt/ambient-brightness/misc/ambient-brightness.service Outdated
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::evaluate 中的加权窗口计算、方向滞回判定、debounce 确认及推荐值死区过滤逻辑严密,边界条件处理得当。例如在初始化分支中正确设置了 m_stableLux 并直接返回推荐值,避免了未初始化状态下的误判。环形缓冲区 AmbientLightRingBuffer 的动态扩容与裁剪逻辑正确,保证了在样本频率波动时的数据完整性。D-Bus 服务的生命周期管理与状态机转换契合,无内存泄漏或空指针解引用风险。
建议:无

  • 2.代码质量(良好)✓

代码整体符合 DDE/Qt 开发规范,命名清晰(如 kSensorServicem_lifecycle),注释详尽,特别是提供了极其完善的 README 文档和设计说明。但存在两处明显的代码重复:第一,AmbientBrightnessModel 中的 submitSampletick 方法内,对推荐值的比较、赋值、日志打印及信号发射的逻辑完全重复;第二,AmbientBrightnessService 中的 onAutomaticBrightnessEnabledChangedEnable 方法在处理关闭逻辑时(停止定时器、释放传感器、重置状态)存在高度重复的代码块。
潜在问题:重复代码增加了后续维护的成本,若修改推荐值发布逻辑容易遗漏同步修改。
建议:在 AmbientBrightnessModel 中提取私有方法 handleRecommendation 统一处理推荐值;在 AmbientBrightnessService 中提取 disableSensorAndResetState 方法封装停用逻辑。

  • 3.代码性能(高效)✓

算法性能表现良好。策略层中的时间加权窗口计算 calculateWeightedAmbientLux 和连续越界时间查找 nextBrighteningTransitionMs 均采用了对环形缓冲区的线性遍历,时间复杂度为 O(N)。由于缓冲区容量受限于慢窗口时长(默认 10000ms)与最小采样间隔(通常 >10ms),最大样本量被控制在千级别以内,在实际硬件传感器上报频率下不会造成性能瓶颈。定时器均采用单次触发模式,避免了无效的轮询开销。
建议:无

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码在安全方面表现良好。外部输入(DConfig 中的 JSON 曲线配置)通过 QJsonDocument::fromJson 进行了解析,并辅以严格的类型校验(isDoubleisObject)和数值范围校验(std::isfinite、非负、递增等),有效防止了 JSON 注入或畸形数据导致的异常。D-Bus 接口仅暴露了属性和一个 Enable 槽函数,未涉及命令执行或敏感文件路径操作,攻击面极小。

  • 建议:继续保持对 DConfig 外部输入的严格校验机制。

■ 【改进建议代码示例】

// 在 ambientbrightnessmodel.h 中添加私有方法声明
private:
    void handleRecommendation(const std::optional<Recommendation> &recommendation, const QString &logPrefix);

// 在 ambientbrightnessmodel.cpp 中实现并替换 submitSample 和 tick 中的重复逻辑
void AmbientBrightnessModel::handleRecommendation(const std::optional<Recommendation> &recommendation, const QString &logPrefix)
{
    if (!recommendation)
        return;

    // +1.0 偏移:qFuzzyCompare 对 0 附近的值不可靠(brightness 合法值为 0),
    // 偏移到 [1,2] 区间使其进入 qFuzzyCompare 的有效比较范围。
    if (!m_haveRecommendation
        || !qFuzzyCompare(m_recommendedBrightness + 1.0, recommendation->brightness + 1.0)) {
        m_recommendedBrightness = recommendation->brightness;
        m_haveRecommendation = true;
        qCDebug(logAmbientBrightness)
            << logPrefix << "stableLux=" << recommendation->stableLux
            << "brightness=" << m_recommendedBrightness;
        Q_EMIT recommendedBrightnessChanged(m_recommendedBrightness);
    }
    setState(QStringLiteral("Active"));
}

void AmbientBrightnessModel::submitSample(double lux,
                                          double monotonicTimestampMs,
                                          SensorSample::Source source)
{
    const auto recommendation = m_policy->update({ lux, monotonicTimestampMs, source });
    handleRecommendation(recommendation, QStringLiteral("recommendation changed:"));
}

void AmbientBrightnessModel::tick(double monotonicTimestampMs)
{
    const auto recommendation = m_policy->tick(monotonicTimestampMs);
    handleRecommendation(recommendation, QStringLiteral("timer recommendation changed:"));
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fly602, mhduiy

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

@fly602

fly602 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Aug 17, 2026

Copy link
Copy Markdown

This pr force merged! (status: blocked)

@deepin-bot
deepin-bot Bot merged commit 08a6429 into linuxdeepin:master Aug 17, 2026
9 checks passed
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.

3 participants