Skip to content

feat: add key state notification applet for Wayland#1614

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
18202781743:key-notify
Jun 1, 2026
Merged

feat: add key state notification applet for Wayland#1614
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
18202781743:key-notify

Conversation

@18202781743
Copy link
Copy Markdown
Contributor

  1. Create a new dde-key-notify applet to monitor Caps Lock and Num Lock
    states on Wayland
  2. Implement a Wayland client extension to listen for keyboard modifier
    state changes via the treeland-keyboard-state-notify-protocol
  3. Integrate with DConfig to control Caps Lock toggle OSD display
  4. Send OSD notifications through DBus (org.deepin.dde.shell) when lock
    states change
  5. Add build system integration and installation rules for the new
    applet

Log: Added keyboard state notification support for Wayland to display
OSD when Caps Lock and Num Lock states change

Influence:

  1. Test on Wayland session: toggle Caps Lock and verify OSD appears/
    disappears correctly
  2. Test on X11 session: verify no OSD is shown (feature is Wayland-only)
  3. Test Num Lock toggle and verify OSD notification
  4. Verify Caps Lock toggle OSD can be disabled via DConfig
    (org.deepin.dde.daemon/org.deepin.dde.daemon.keyboard/capslockToggle)
  5. Verify package installation completes successfully
  6. Test multiple rapid toggles to ensure no crashes or missed
    notifications

feat: 添加 Wayland 下的键盘状态通知小程序

  1. 创建新的 dde-key-notify 小程序,用于监控 Wayland 下的大写锁定和数字锁
    定键状态
  2. 实现 Wayland 客户端扩展,通过 treeland-keyboard-state-notify 协议监听
    键盘修饰键状态变化
  3. 集成 DConfig 以控制大写锁定键 OSD 显示
  4. 当锁定状态变化时通过 DBus(org.deepin.dde.shell)发送 OSD 通知
  5. 添加构建系统集成和安装规则

Log: 添加 Wayland 键盘状态通知功能,大写锁定和数字锁定状态变化时显示 OSD

Influence:

  1. 在 Wayland 会话中测试:切换大写锁定键,验证 OSD 正确显示/消失
  2. 在 X11 会话中测试:验证不显示 OSD(该功能仅支持 Wayland)
  3. 测试数字锁定键切换,验证 OSD 通知
  4. 通过 DConfig(org.deepin.dde.daemon/org.deepin.dde.daemon.keyboard/
    capslockToggle)验证大写锁定 OSD 可被禁用
  5. 验证软件包安装完成
  6. 测试快速多次切换,确保无崩溃或丢失通知

PMS: TASK-390457

Copy link
Copy Markdown

@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.

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

Please try again later or upgrade to continue using Sourcery

Comment thread applets/dde-key-notify/keynotifyapplet.cpp
Comment thread applets/dde-key-notify/CMakeLists.txt
yixinshark
yixinshark previously approved these changes Jun 1, 2026
1. Create a new dde-key-notify applet to monitor Caps Lock and Num Lock
states on Wayland
2. Implement a Wayland client extension to listen for keyboard modifier
state changes via the treeland-keyboard-state-notify-protocol
3. Integrate with DConfig to control Caps Lock toggle OSD display
4. Send OSD notifications through DBus (org.deepin.dde.shell) when lock
states change
5. Add build system integration and installation rules for the new
applet

Log: Added keyboard state notification support for Wayland to display
OSD when Caps Lock and Num Lock states change

Influence:
1. Test on Wayland session: toggle Caps Lock and verify OSD appears/
disappears correctly
2. Test on X11 session: verify no OSD is shown (feature is Wayland-only)
3. Test Num Lock toggle and verify OSD notification
4. Verify Caps Lock toggle OSD can be disabled via DConfig
(org.deepin.dde.daemon/org.deepin.dde.daemon.keyboard/capslockToggle)
5. Verify package installation completes successfully
6. Test multiple rapid toggles to ensure no crashes or missed
notifications

feat: 添加 Wayland 下的键盘状态通知小程序

1. 创建新的 dde-key-notify 小程序,用于监控 Wayland 下的大写锁定和数字锁
定键状态
2. 实现 Wayland 客户端扩展,通过 treeland-keyboard-state-notify 协议监听
键盘修饰键状态变化
3. 集成 DConfig 以控制大写锁定键 OSD 显示
4. 当锁定状态变化时通过 DBus(org.deepin.dde.shell)发送 OSD 通知
5. 添加构建系统集成和安装规则

Log: 添加 Wayland 键盘状态通知功能,大写锁定和数字锁定状态变化时显示 OSD

Influence:
1. 在 Wayland 会话中测试:切换大写锁定键,验证 OSD 正确显示/消失
2. 在 X11 会话中测试:验证不显示 OSD(该功能仅支持 Wayland)
3. 测试数字锁定键切换,验证 OSD 通知
4. 通过 DConfig(org.deepin.dde.daemon/org.deepin.dde.daemon.keyboard/
capslockToggle)验证大写锁定 OSD 可被禁用
5. 验证软件包安装完成
6. 测试快速多次切换,确保无崩溃或丢失通知

PMS: TASK-390457
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

你好!我是CodeGeeX。我已仔细审查了你提交的Git Diff。本次变更为DDE Shell新增了一个键盘状态(CapsLock/NumLock)Wayland通知插件,整体结构清晰,逻辑基本合理。但在代码细节、健壮性、安全性和构建配置方面,有一些值得注意和改进的地方。

以下是具体的审查意见:

1. 语法与逻辑

  • KeyNotifyApplet::load() 的返回值逻辑问题
    keynotifyapplet.cpp 中:

    bool KeyNotifyApplet::load()
    {
        if (!Dtk::Gui::DGuiApplicationHelper::testAttribute(Dtk::Gui::DGuiApplicationHelper::IsWaylandPlatform)) {
            return DApplet::load(); // 非Wayland平台直接返回
        }
        // ... Wayland下的初始化逻辑 ...
        return DApplet::load();
    }

    问题:在Wayland平台分支中,如果 m_keyNotify 初始化失败(例如 new 抛出异常,虽然Qt中极少发生,或者后续逻辑异常),依然会执行并返回 DApplet::load()。建议在Wayland分支中加入对 m_keyNotify 的有效性检查,如果初始化失败应提前返回 false
    改进建议

    m_keyNotify = new TreelandKeyNotify(this);
    if (!m_keyNotify) {
        return false; // 或者根据框架设计决定是否继续
    }
    // ...
    return DApplet::load();
  • TreelandKeyNotify 构造函数中的 setParent 问题
    treelandkeynotify.cpp 中:

    TreelandKeyNotify::TreelandKeyNotify(QObject *parent)
        : QWaylandClientExtensionTemplate<TreelandKeyNotify>(treeland_keyboard_state_notify_manager_v1_interface.version)
    {
        setParent(parent);
        // ...
    }

    问题QWaylandClientExtensionTemplate 继承自 QObject。在C++中,应该通过基类初始化列表将 parent 传递给 QObject 的构造函数,而不是在构造函数体内调用 setParent。这可以确保在对象构造伊始就拥有正确的父对象,避免某些依赖父对象的信号/槽在构造期间失效。
    改进建议

    TreelandKeyNotify::TreelandKeyNotify(QObject *parent)
        : QWaylandClientExtensionTemplate<TreelandKeyboardStateNotifyManager>(treeland_keyboard_state_notify_manager_v1_interface.version, parent)
    {
        connect(this, &TreelandKeyNotify::activeChanged, this, &TreelandKeyNotify::updateWatcher);
    }
  • m_config 的生命周期与空指针风险
    initConfig() 中:

    m_config = Dtk::Core::DConfig::create(QStringLiteral("org.deepin.dde.daemon"), QStringLiteral("org.deepin.dde.daemon.keyboard"), QString(), this);
    connect(m_config, &Dtk::Core::DConfig::valueChanged, this, &KeyNotifyApplet::updateCapsLockToggle);

    问题DConfig::create 在某些极端情况下(如配置文件缺失或后端服务异常)可能返回 nullptr。如果 m_config 为空,后续的 connect 会触发崩溃,updateCapsLockToggle 中的 m_config->value() 也会崩溃。
    改进建议:增加空指针判断。

    void KeyNotifyApplet::initConfig()
    {
        m_config = Dtk::Core::DConfig::create(QStringLiteral("org.deepin.dde.daemon"), QStringLiteral("org.deepin.dde.daemon.keyboard"), QString(), this);
        if (!m_config) {
            qWarning() << "Failed to create DConfig for keyboard";
            return;
        }
        connect(m_config, &Dtk::Core::DConfig::valueChanged, this, &KeyNotifyApplet::updateCapsLockToggle);
    }

    同时,在 updateCapsLockToggleload 中使用 m_config 前也应检查其有效性,或者使用 QPointer 来管理 m_config

2. 代码质量

  • 魔法数字 应替换为枚举
    treelandkeynotify.cpp 的 Lambda 中:

    if (modifier == TreelandKeyWatcher::modifier_caps_lock) { ... }
    else if (modifier == TreelandKeyWatcher::modifier_num_lock) { ... }

    这里 modifier_caps_lockmodifier_num_lock 看起来像是整型常量或自动生成的Wayland协议常量。虽然可以使用,但建议在 TreelandKeyWatcher 中定义具名枚举(或在已有的枚举中补充),以增强代码可读性和IDE的代码提示支持。

  • KeyNotifyApplet 头文件中的 Slot 修饰符
    keynotifyapplet.h 中:

    private Q_SLOTS:
        void showCapsLockOsd(bool locked);
        void showNumLockOsd(bool locked);
        void updateCapsLockToggle(const QString &key);

    建议showCapsLockOsdshowNumLockOsd 仅作为信号槽的接收端,且逻辑非常简单(仅转发字符串)。可以考虑将它们合并为一个通用的私有方法,或者直接在 connect 时使用 Lambda 表达式,减少类的接口体积,使代码更紧凑:

    connect(m_keyNotify, &TreelandKeyNotify::capsLockChanged, this, [this](bool locked) {
        sendOsd(locked ? QStringLiteral("CapsLockOn") : QStringLiteral("CapsLockOff"));
    });

3. 代码性能

  • Wayland Watcher 的重建逻辑
    updateWatcher() 中,当 isActive() 变为 false 时,会销毁 m_watcher;变为 true 时,又重新 createWatcher
    潜在风险:如果 Wayland compositor 因为某种原因频繁重启或断开/重连,会导致 m_watcher 频繁地 deleteLaternew,引发不必要的性能开销和内存抖动。
    建议:当前逻辑在一般场景下可以接受,但如果后续发现重连场景频繁,建议保留 m_watcher 对象,仅暂停其监听(如调用协议中的 disable 方法,如果协议支持),或者在重连时复用现有对象。

4. 代码安全

  • DBus 调用的输入校验
    sendOsd 方法通过 DBus 调用 org.deepin.dde.shell.osdShowOSD 方法:
    void KeyNotifyApplet::sendOsd(const QString &osdType)
    {
        DDBusSender().service("org.deepin.dde.shell").path("/org/deepin/dde/shell/osd").interface("org.deepin.dde.shell.osd").method("ShowOSD").arg(osdType).call();
    }
    风险:虽然目前 osdType 是由内部逻辑硬编码生成的(CapsLockOn 等),相对安全。但从防御性编程的角度,如果未来有其他地方调用 sendOsd,或者 Wayland 协议被恶意篡改发送异常字符串,可能会引发 OSD 端的异常解析或注入风险。
    建议:确保 osdType 不包含非法字符,或者使用白名单机制(如 QSet<QString>switch-case)对传入的 osdType 进行校验,只允许特定的已知字符串通过 DBus 发送。

5. 构建与打包

  • 版权年份问题
    SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
    问题:2026年是未来的年份,这通常是一个笔误。建议修改为当前年份(如2023或2024),或使用年份范围(如2023-2024)。

  • Debian 依赖版本号操作符 >> 的语义
    debian/control 中:

    - treeland-protocols (>= 0.5.7),
    + treeland-protocols (>> 0.5.7),

    问题:将 >= 改为 >>。在 Debian 包管理系统中,>> 表示严格大于(不包含 0.5.7 本身)。请确认你的意图:

    1. 如果该协议在 0.5.7 版本引入了破坏性变更,且你的代码必须依赖 0.5.8 及以上,使用 >> 是正确的。
    2. 如果你的代码在 0.5.7 上也能正常编译和运行,应该保留 >=,否则使用 0.5.7 版本的用户在升级此软件包时会遇到无法解决的依赖问题。请慎重评估此修改。

总结

代码整体质量不错,主要风险点在于 DConfig::create 可能返回空指针导致的潜在崩溃,以及 QObject 构造时未通过初始化列表传递 parent 的不规范写法。建议优先修复这两个逻辑/语法问题,并核实 Debian 依赖符号和版权年份。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

@18202781743
Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown

deepin-bot Bot commented Jun 1, 2026

This pr force merged! (status: blocked)

@deepin-bot deepin-bot Bot merged commit cb92112 into linuxdeepin:master Jun 1, 2026
9 of 12 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.

5 participants