Skip to content

feat(power): migrate power services - #153

Open
mhduiy wants to merge 1 commit into
masterfrom
task/x11power
Open

feat(power): migrate power services#153
mhduiy wants to merge 1 commit into
masterfrom
task/x11power

Conversation

@mhduiy

@mhduiy mhduiy commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

This PR contains the functional Power migration only.

  1. Unify session Power scheduling across X11 and Wayland backends.
  2. Add the system Power1 plugin with battery and power-mode APIs.
  3. Preserve legacy idle, screen, brightness, short-idle, and scheduled-shutdown behavior.
  4. Harden X11 process/display handling and DPMS state transitions.
  5. Move Power D-Bus, DConfig, Polkit policy, and activation assets into dde-services.
  6. Require deepin-power-control for system hardware control.

The deepin-security-loader caller authorization integration is intentionally split into the stacked follow-up PR so its security design can be revised independently.

Log: Migrate Power1 ownership while preserving behavior across both display backends.
PMS: TASK-394241
Influence: X11 and Wayland share compatible Power APIs.

Summary by Sourcery

Migrate session and system power management into unified Power1 services while retaining existing power behavior across display backends.

New Features:

  • Expose a system Power1 D-Bus API with dynamic battery objects, power-mode controls, low-power configuration, and short-idle support.
  • Apply unified power scheduling and screen, brightness, suspend, lid, and shutdown behavior across X11 and Wayland sessions.

Bug Fixes:

  • Harden battery, lid, idle, DPMS, sleep, wake, inhibitor, and scheduled-shutdown state handling while preserving legacy behavior.
  • Improve power-mode persistence, brightness restoration, and migration of legacy power configuration.

Enhancements:

  • Add X11 idle and DPMS backends and integrate ambient-brightness-aware power-saving behavior.
  • Serialize system hardware operations through deepin-power-control and refresh battery and mains state from sysfs and udev.

Build:

  • Enable the system Power plugin, add X11 dependencies, and install its D-Bus, DConfig, and related service assets.

Deployment:

  • Move Power1 service, D-Bus policy, Polkit policy, activation, and configuration assets into dde-services.

Chores:

  • Remove the legacy DPMS state-file integration and obsolete system Power service activation path.

@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 @mhduiy, your pull request is larger than the review limit of 150000 diff characters

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 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

@sourcery-ai

sourcery-ai Bot commented Aug 18, 2026

Copy link
Copy Markdown

Reviewer's Guide

Unifies session and system power services for X11 and Wayland under dde-services, adding a system Power1 plugin with richer battery/power-mode APIs, short-idle and ambient-brightness integration, hardened DPMS and sleep/shutdown handling, and updated DConfig/DBus/Polkit wiring.

File-Level Changes

Change Details Files
Refactor session PowerManager to support unified X11/Wayland behavior, short-idle, ambient brightness, stronger DConfig persistence, and safer sleep/shutdown flows.
  • Introduce persist/resetConfig helpers and config reader to guard DConfig writes and avoid feedback loops.
  • Add short-idle delays, application/service-based short-idle eligibility checks, and kernel/screen idle state wiring via SessionDBusProxy.
  • Reuse AmbientBrightness1 sensor state, expose ambientLightAdjustBrightness, and integrate brightness/DPMS changes with Wayland and X11 screen controllers.
  • Tighten sleep/wakeup handling (audio suspend, screensaver state capture, multi-session checks, systemd inhibitors) and rework scheduled-shutdown persistence and timers.
  • Register DBus adaptors, object maps, and cleanly unregister objects on destruction.
src/plugin-qt/power/session/powermanager.cpp
src/plugin-qt/power/session/powermanager.h
src/plugin-qt/power/session/lidswitchhandler.cpp
src/plugin-qt/power/session/sleepinhibitor.cpp
Enhance session-side power scheduling (PowerSavePlan, LowPowerManager) to coordinate brightness drops, short-idle transitions, warn-level config, and X11 idle/DPMS integration.
  • Extend PowerSavePlan to include short-idle tasks, per-backend idle handling, DPMS/brightness coordination, and sleep via front-end on X11.
  • Add persistence and synchronization of power-saving brightness across sessions using DConfig and Power1 properties.
  • Implement LowPowerManager as a DBus adaptor with warn-level configuration properties, validation/reset, and DBus PropertiesChanged emission.
  • Adjust idle handling to ignore inactive sessions or fullscreen apps, introduce delays for idle-off when screen is black, and integrate new prepare-suspend states.
src/plugin-qt/power/session/powersaveplan.cpp
src/plugin-qt/power/session/powersaveplan.h
src/plugin-qt/power/session/lowpowermanager.cpp
src/plugin-qt/power/session/lowpowermanager.h
src/plugin-qt/power/session/idle/idlewatcher_wl.cpp
src/plugin-qt/power/session/idle/idlewatcher.h
Add X11 implementations for idle watching and screen control, and wire them into session power for non-Wayland sessions.
  • Implement X11IdleWatcher using org.freedesktop.ScreenSaver DBus signals and XScreenSaver to report idle time and simulate activity.
  • Implement X11ScreenController using X11 DPMS to set/get output power modes and emit modeChanged.
  • Switch PowerManager factory methods to create X11-specific idle watcher and screen controller when not using Wayland.
  • Remove legacy dpms-state file writes and move DPMS/idle-state control into DBus/system hooks.
src/plugin-qt/power/session/idle/idlewatcher_x11.cpp
src/plugin-qt/power/session/screen/screencontroller_x11.cpp
src/plugin-qt/power/session/idle/idlewatcher.h
src/plugin-qt/power/session/screen/screencontroller.h
src/plugin-qt/power/session/powermanager.cpp
Rework system-level SystemPowerManager to expose richer Power1 APIs, migrate legacy config into DConfig, manage multiple BatteryDevice objects, and queue deepin-power-control operations.
  • Add BatteryDevice class and DBus interface, aggregate multiple batteries for percentage/time/capacity, and emit BatteryAdded/Removed/DisplayUpdate signals.
  • Replace simple battery and lid handling with BatteryManager that discovers sysfs power_supply devices, tracks mains vs battery state, and uses udev for events.
  • Introduce config migration from legacy JSON into DConfig, add config reader and guarded persistence, and extend properties for short idle, brightness data, and power-mode mapping.
  • Queue deepin-power-control invocations via a QProcess with a command queue and mapping from logical modes (balance/powersave/performance/lowBattery) to DSPC configs.
  • Implement new DBus methods SetMode, SetTlpMode, SetShortIdleState with validation, and synchronize session/system power-saving behavior and auto-mode selection.
src/plugin-qt/power/system/powermanager.cpp
src/plugin-qt/power/system/powermanager.h
src/plugin-qt/power/system/batterymanager.cpp
src/plugin-qt/power/system/batterymanager.h
src/plugin-qt/power/system/batterydevice.cpp
src/plugin-qt/power/system/batterydevice.h
src/plugin-qt/power/system/systemdbusproxy.cpp
src/plugin-qt/power/system/systemdbusproxy.h
Extend SessionDBusProxy and powerconstants to support new power, display, ambient-brightness, idle, and display-manager interactions.
  • Add properties and methods for PowerSavingModeBrightnessData, short idle state, idle/screen state, battery/mains refresh, and brightness retrieval/refresh.
  • Hook org.freedesktop.DBus.PropertiesChanged for display brightness and ambient brightness, emitting BrightnessChanged and ambientBrightnessPropertiesChanged.
  • Add helpers to call login1 ListInhibitors/ListSessions for inhibitor checks and multi-session awareness.
  • Define new constants for idle services/paths, short-idle-related DConfig keys, screen-saver/allow/delay config keys, power mapping config, and display-manager service information.
src/plugin-qt/power/session/sessiondbusproxy.cpp
src/plugin-qt/power/session/sessiondbusproxy.h
src/plugin-qt/power/powerconstants.h
Enable building and installing the system power plugin and updated session plugin assets, including DBus and Polkit policies, and X11 dependencies.
  • Add plugin-qt/power/system subdirectory back into the build and link, and install its plugin JSON and DBus system service files.
  • Install DBus system.d config and Polkit action for org.deepin.dde.Power1, plus DConfig schema org.deepin.dde.daemon.power.json under dsg configs.
  • Switch install paths to use CMAKE_INSTALL_DATADIR instead of FULL_DATADIR for service-manager JSON and DBus service files.
  • Add X11 pkg-config dependencies and link/include paths for the session plugin, and remove XDG_SESSION_TYPE=wayland gate so session plugin loads under X11 as well.
src/plugin-qt/power/CMakeLists.txt
src/plugin-qt/power/session/CMakeLists.txt
src/plugin-qt/power/system/CMakeLists.txt
src/plugin-qt/power/system/plugin-power-system.cpp
src/plugin-qt/power/session/plugin-power-session.cpp
src/plugin-qt/power/system/misc/dbus/org.deepin.dde.Power1.conf
src/plugin-qt/power/misc/org.deepin.dde.daemon.power.json
src/plugin-qt/power/system/misc/org.deepin.dde.power.policy

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

@mhduiy
mhduiy force-pushed the task/x11power branch 6 times, most recently from ebe043c to 8b1a80e Compare August 18, 2026 04:21
@deepin-bot

deepin-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 1.0.40
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #154

@deepin-bot

deepin-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 1.0.41
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #155

@mhduiy
mhduiy force-pushed the task/x11power branch 3 times, most recently from 22fd43d to e022cb4 Compare August 18, 2026 11:06
@mhduiy

mhduiy commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

/test sync-to-gitee

@deepin-ci-robot

Copy link
Copy Markdown

@mhduiy: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test deepin-auto-translation
  • /test github-pr-review-ci

Use /test all to run the following jobs that were automatically triggered:

  • github-pr-review-ci
Details

In response to this:

/test sync-to-gitee

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

1. Unify session Power scheduling across X11 and Wayland backends.
2. Add the system Power1 plugin with battery and power-mode APIs.
3. Preserve legacy idle, screen, brightness, short-idle, and shutdown behavior.
4. Harden legacy configuration migration and X11 process validation.
5. Move Power D-Bus, DConfig, Polkit, and activation assets into dde-services.
6. Require deepin-power-control for system hardware control.

Log: Migrate Power1 ownership while preserving behavior across display backends.
Influence: X11 and Wayland share compatible Power APIs.

feat(power): 迁移电源服务

1. 统一 X11 与 Wayland 的会话电源调度。
2. 新增包含电池与电源模式接口的系统 Power1 插件。
3. 保持旧版空闲、屏幕、亮度、短空闲及关机行为。
4. 加固旧配置迁移和 X11 进程校验。
5. 将电源 D-Bus、DConfig、Polkit 与激活文件迁移至 dde-services。
6. 为系统硬件控制添加 deepin-power-control 依赖。

Log: 迁移 Power1 所有权并保持两种显示后端的兼容行为。
PMS: TASK-394241
Influence: X11 与 Wayland 共用兼容的电源接口。
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码实现了X11窗口检测和电源管理重构,逻辑严谨且无安全漏洞
代码结构清晰,考虑了PID复用等安全风险,性能优化合理,因少量非致命瑕疵扣5分

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

代码中X11相关实现如X11IdleWatcherX11ScreenController的资源管理正确,XOpenDisplayXCloseDisplay配对使用,XScreenSaverAllocInfo分配的内存通过XFree释放。LowPowerManager重构为QDBusAbstractAdaptor,属性和信号定义符合Qt D-Bus规范。SystemPowerManager::migrateLegacyConfig()的JSON解析逻辑严密,错误处理完善。
潜在问题:无
建议:无

  • 2.代码质量(优秀)✓

代码命名规范,注释详尽,特别是对设计决策的说明(如“0%电量容忍”、“DConfig写入防回环”)。宏定义如DEF_SETTER_PERSISTSET_CONFIG_VALUE有效减少了代码重复。新增的DConfig Schema配置项描述清晰,包含中英文翻译。
潜在问题:部分函数如canEnterShortIdle较长,但逻辑内聚,可接受。
建议:可考虑将canEnterShortIdle中的应用和服务检查逻辑拆分为独立的辅助函数,进一步提升可读性。

  • 3.代码性能(良好)✓

X11IdleWatcher::idleTimeMs()通过直接查询X ScreenSaver扩展获取空闲时间,避免了不必要的轮询。canEnterShortIdle()中的D-Bus调用设置了1秒超时,防止主线程长时间阻塞。BatteryManager使用udev事件驱动结合定时器轮询,平衡了实时性和性能。
潜在问题:无
建议:无

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

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码在安全方面表现优异。shouldPreventIdle()函数在读取/proc/<pid>/cmdline前后均检查文件所有者ownerId() == ::geteuid(),有效防止了PID复用攻击。setAudioSuspended使用QProcess::start传递参数列表而非拼接命令字符串,避免了命令注入风险。D-Bus配置遵循最小权限原则,doAction操作需通过polkit认证。

  • 建议:无

■ 【改进建议代码示例】

// 建议将 canEnterShortIdle 中的检查逻辑拆分,提升可读性
bool PowerManager::canEnterShortIdle() const
{
    if (m_useWayland)
        return true;

    if (!checkNoBlacklistedAppRunning())
        return false;

    if (!checkNoThirdPartyAppRunning())
        return false;

    if (!checkNoThirdPartyServiceRunning())
        return false;

    return true;
}

bool PowerManager::checkNoBlacklistedAppRunning() const
{
    // 原有应用黑名单检查逻辑
    // ...
    return true;
}

bool PowerManager::checkNoThirdPartyAppRunning() const
{
    // 原有第三方应用检查逻辑
    // ...
    return true;
}

bool PowerManager::checkNoThirdPartyServiceRunning() const
{
    // 原有第三方服务检查逻辑
    // ...
    return true;
}

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