Skip to content

Conversation

@mhduiy
Copy link
Contributor

@mhduiy mhduiy commented Jan 22, 2026

The code was modified to skip adding screensaver tasks when running under Wayland sessions (detected by XDG_SESSION_TYPE=wayland environment variable). This change is necessary because in treeland (Wayland compositor), screensaver management is handled by treeland itself, and the backend should not control screensaver functionality. The condition ensures that screensaver tasks are only added for non-Wayland sessions (like X11).

Influence:

  1. Test power save plan updates on Wayland sessions to verify screensaver tasks are not added
  2. Test on X11 sessions to ensure screensaver tasks are still added as before
  3. Verify that other power management tasks (lock, sleep, etc.) continue to work normally
  4. Test with different screenSaverStartDelay values to ensure conditional logic works correctly
  5. Verify environment variable detection works properly in different session types

fix: 在 Wayland 会话中跳过屏保任务

修改了代码,在 Wayland 会话(通过 XDG_SESSION_TYPE=wayland 环境变量检 测)下跳过添加屏保任务。这个变更是必要的,因为在 treeland(Wayland 合成
器)中,屏保管理由 treeland 自行处理,后端不应控制屏保功能。这个条件确保
屏保任务只被添加到非 Wayland 会话(如 X11)中。

Influence:

  1. 在 Wayland 会话中测试电源保存计划更新,验证屏保任务未被添加
  2. 在 X11 会话中测试,确保屏保任务仍像以前一样被添加
  3. 验证其他电源管理任务(锁定、休眠等)继续正常工作
  4. 使用不同的 screenSaverStartDelay 值测试,确保条件逻辑正常工作
  5. 验证环境变量检测在不同会话类型中正常工作

Summary by Sourcery

Bug Fixes:

  • Prevent backend from controlling screensaver behavior on Wayland sessions by avoiding creation of screensaver start tasks when XDG_SESSION_TYPE=wayland.

The code was modified to skip adding screensaver tasks when running
under Wayland sessions (detected by XDG_SESSION_TYPE=wayland environment
variable). This change is necessary because in treeland (Wayland
compositor), screensaver management is handled by treeland itself, and
the backend should not control screensaver functionality. The condition
ensures that screensaver tasks are only added for non-Wayland sessions
(like X11).

Influence:
1. Test power save plan updates on Wayland sessions to verify
screensaver tasks are not added
2. Test on X11 sessions to ensure screensaver tasks are still added
as before
3. Verify that other power management tasks (lock, sleep, etc.) continue
to work normally
4. Test with different screenSaverStartDelay values to ensure
conditional logic works correctly
5. Verify environment variable detection works properly in different
session types

fix: 在 Wayland 会话中跳过屏保任务

修改了代码,在 Wayland 会话(通过 XDG_SESSION_TYPE=wayland 环境变量检
测)下跳过添加屏保任务。这个变更是必要的,因为在 treeland(Wayland 合成
器)中,屏保管理由 treeland 自行处理,后端不应控制屏保功能。这个条件确保
屏保任务只被添加到非 Wayland 会话(如 X11)中。

Influence:
1. 在 Wayland 会话中测试电源保存计划更新,验证屏保任务未被添加
2. 在 X11 会话中测试,确保屏保任务仍像以前一样被添加
3. 验证其他电源管理任务(锁定、休眠等)继续正常工作
4. 使用不同的 screenSaverStartDelay 值测试,确保条件逻辑正常工作
5. 验证环境变量检测在不同会话类型中正常工作
@deepin-ci-robot
Copy link

[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
Copy link

sourcery-ai bot commented Jan 22, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds an environment-based guard so that screensaver tasks are only scheduled on non-Wayland sessions, leaving other power management tasks unchanged.

Sequence diagram for powerSavePlan.Update screensaver scheduling on Wayland vs non-Wayland

sequenceDiagram
    participant Caller
    participant powerSavePlan
    participant OS_Env
    participant TaskScheduler

    Caller->>powerSavePlan: Update(screenSaverStartDelay, lockDelay, ...)
    activate powerSavePlan

    powerSavePlan->>powerSavePlan: canAddToTasks(screenSaverStart, screenSaverStartDelay, tasks)
    alt screenSaverStartDelay <= 0 or cannot add
        powerSavePlan-->>Caller: return without adding screensaver task
    else screenSaverStartDelay > 0 and can add
        powerSavePlan->>OS_Env: Getenv(XDG_SESSION_TYPE)
        OS_Env-->>powerSavePlan: sessionType
        alt sessionType == wayland
            powerSavePlan->>TaskScheduler: do not append screenSaverStart task
        else sessionType != wayland
            powerSavePlan->>TaskScheduler: append metaTask screenSaverStart with delay
            TaskScheduler-->>powerSavePlan: tasks updated
        end
        powerSavePlan-->>Caller: return with updated tasks
    end
    deactivate powerSavePlan
Loading

Updated class diagram for powerSavePlan screensaver task scheduling

classDiagram
    class powerSavePlan {
        +Update(screenSaverStartDelay int, lockDelay int, otherDelay int)
        +startScreensaver()
    }

    class metaTask {
        +string name
        +int delay
        +function fn
    }

    class OSEnvironment {
        +Getenv(key string) string
    }

    powerSavePlan "1" --> "*" metaTask : schedules
    powerSavePlan --> OSEnvironment : reads XDG_SESSION_TYPE
Loading

File-Level Changes

Change Details Files
Gate scheduling of screensaver-start task based on XDG_SESSION_TYPE to avoid managing screensaver under Wayland compositors.
  • Wrap existing screensaver task append logic with an XDG_SESSION_TYPE != "wayland" condition
  • Preserve existing checks for positive screenSaverStartDelay and canAddToTasks before considering the new condition
  • Leave lock and other power management task scheduling behavior unchanged
session/power1/power_save_plan.go

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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Calling os.Getenv("XDG_SESSION_TYPE") inside Update on every invocation may be unnecessarily repetitive; consider reading and caching the session type once (or passing it in) and reusing that value when building tasks.
  • The "wayland" string is currently hard-coded; consider centralizing this session-type check in a helper (e.g., isWaylandSession()) to avoid magic strings and make it easier to adapt if additional Wayland variants or detection logic are needed.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Calling `os.Getenv("XDG_SESSION_TYPE")` inside `Update` on every invocation may be unnecessarily repetitive; consider reading and caching the session type once (or passing it in) and reusing that value when building tasks.
- The `"wayland"` string is currently hard-coded; consider centralizing this session-type check in a helper (e.g., `isWaylandSession()`) to avoid magic strings and make it easier to adapt if additional Wayland variants or detection logic are needed.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码的修改意图是:在 Wayland 会话环境下(具体是针对 treeland 窗口管理器),由窗口管理器自行管理屏保逻辑,因此后端服务不再添加屏保启动的任务。

以下是对该代码修改的详细审查意见,包括语法逻辑、代码质量、性能和安全方面的建议:

1. 语法逻辑

  • 现状:代码逻辑符合 Go 语言语法,通过 os.Getenv 获取环境变量并进行字符串比较来决定是否添加任务。
  • 潜在问题:依赖环境变量 XDG_SESSION_TYPE 来判断是否为 Wayland 是一种通用做法,但注释中明确提到了 "treeland"。如果未来系统在 Wayland 下运行了其他非 treeland 的合成器(如 Mutter, KWin 等),这段逻辑会导致屏保在这些环境下失效,除非它们也能自行处理屏保。
  • 改进建议
    • 如果该逻辑适用于 treeland,建议判断条件更加精确,或者增加更明确的注释,说明为什么 Wayland 下不需要后端控制(是因为所有 Wayland 合成器都接管了,还是仅 treeland 接管了)。
    • 如果条件允许,可以考虑检测当前运行的桌面环境或窗口管理器,而不仅仅是依赖 XDG_SESSION_TYPE

2. 代码质量

  • 现状:增加了中文注释解释了跳过的原因,有助于理解。
  • 改进建议
    • 硬编码字符串"wayland""screenSaverStart" 是硬编码的字符串。建议将这些定义为常量(const),例如 const SessionTypeWayland = "wayland",以避免拼写错误并提高可维护性。
    • 注释风格:虽然中文注释在内部团队中很常见,但如果是开源项目或国际化团队,建议使用英文注释,或者确保代码规范允许使用中文注释。
    • 魔法值os.Getenv("XDG_SESSION_TYPE") 的调用分散在代码中。如果其他地方也有类似判断,建议封装成一个辅助函数,例如 isWaylandSession()isTreelandSession()

3. 代码性能

  • 现状os.Getenv 是一个轻量级调用,性能开销极小。
  • 改进建议
    • 如果 Update 函数会被高频调用(例如每秒多次),虽然 os.Getenv 很快,但重复读取环境变量略显冗余。不过考虑到电源管理计划通常由系统事件触发(如电源插拔、设置更改),调用频率通常不高,当前实现是可以接受的。

4. 代码安全

  • 现状:读取环境变量通常是安全的,不存在注入风险。
  • 改进建议
    • 无明显安全隐患。

总结与重构建议

为了提高代码的健壮性和可读性,建议对代码进行如下微调:

// 定义常量
const (
    TaskNameScreenSaverStart = "screenSaverStart"
    EnvSessionType           = "XDG_SESSION_TYPE"
    SessionTypeWayland       = "wayland"
)

// 可选:增加辅助函数提高可读性
func isWaylandSession() bool {
    return os.Getenv(EnvSessionType) == SessionTypeWayland
}

// 在 Update 函数中使用
if screenSaverStartDelay > 0 && canAddToTasks(TaskNameScreenSaverStart, screenSaverStartDelay, tasks) {
    // treeland 下屏保由 treeland 自行管理,后端不控制。
    // 注意:如果未来有其他 Wayland 合成器不支持自行管理,此处逻辑可能需要调整。
    if !isWaylandSession() {
        tasks = append(tasks, metaTask{
            name:  TaskNameScreenSaverStart,
            delay: screenSaverStartDelay,
            fn:    psp.startScreensaver,
        })
    }
}

核心建议
目前的修改解决了特定环境下的功能冲突,但从长远看,建议确认是否所有 Wayland 环境都不需要后端控制屏保。如果只是 treeland 的特性,建议将判断条件改为更具体的检测,或者明确注释此处的假设,以免在非 treeland 的 Wayland 环境下出现功能缺失。

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Jan 23, 2026

TAG Bot

New tag: 6.1.72
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1001

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