Skip to content

fix: monitor X11 lock state changes through XKB - #128

Open
yixinshark wants to merge 1 commit into
linuxdeepin:masterfrom
yixinshark:fix/x11-lock-state-notifications
Open

fix: monitor X11 lock state changes through XKB#128
yixinshark wants to merge 1 commit into
linuxdeepin:masterfrom
yixinshark:fix/x11-lock-state-notifications

Conversation

@yixinshark

@yixinshark yixinshark commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • subscribe to XKB modifier lock state notifications independently of passive shortcut grabs
  • propagate Caps Lock and Num Lock changes through the key handler
  • deduplicate the exported D-Bus state change signals

Tests

  • cmake --build build --target plugin-dde-shortcut -j2
  • shortcut unit tests (28 assertions passed)
  • git diff --check

Pms: BUG-372751

Summary by Sourcery

Handle X11 keyboard lock state changes via XKB and ensure consistent, deduplicated propagation of Caps Lock and Num Lock state updates through the shortcut subsystem.

Bug Fixes:

  • Monitor X11 Caps Lock and Num Lock state changes using XKB and propagate them through the key handling pipeline.
  • Deduplicate Num Lock and Caps Lock D-Bus state change emissions to avoid redundant signals.

Enhancements:

  • Expose lock state changes from the X11 key handler to the keybinding manager via new signals and slots.
  • Track last known Num Lock and Caps Lock states in the keybinding manager to prevent unnecessary updates.

@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 @yixinshark, 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: yixinshark

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 5, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR wires XKB-based monitoring of X11 modifier lock state (Caps/Num Lock) into the existing shortcut backend, propagates lock changes through the key handler and keybinding manager, and centralizes/filters emission of the exported D-Bus state-change signals to avoid duplicates.

Sequence diagram for XKB lock state propagation

sequenceDiagram
    participant XServer
    participant X11KeyHandler
    participant KeybindingManager
    participant DbusClients

    XServer->>X11KeyHandler: XkbStateNotify
    X11KeyHandler->>X11KeyHandler: notifyLockStateChange(event)
    alt [Num Lock changed]
        X11KeyHandler-->>KeybindingManager: numLockStateChanged(on)
        KeybindingManager->>KeybindingManager: updateNumLockState(on)
        KeybindingManager-->>DbusClients: NumLockStateChanged(state)
    end
    alt [Caps Lock changed]
        X11KeyHandler-->>KeybindingManager: capsLockStateChanged(on)
        KeybindingManager->>KeybindingManager: updateCapsLockState(on)
        KeybindingManager-->>DbusClients: CapsLockStateChanged(state)
    end
Loading

File-Level Changes

Change Details Files
Subscribe to XKB lock state notifications and handle them in the X11 key handler
  • Include XKB proto headers and store XKB event base opcode in the X11 key handler
  • Add enableLockStateMonitoring() to query the XKB extension, subscribe to XkbStateNotify with XkbModifierLockMask, and flush the display
  • Invoke enableLockStateMonitoring() during X11KeyHandler construction after modifier mask refresh
  • In the XCB event loop, route XKB events (matching m_xkbEventBase) to a new notifyLockStateChange() handler
  • Implement notifyLockStateChange() to filter state notifications by XkbStateNotify and XkbModifierLockMask and emit num/caps lock signals based on lockedMods and existing masks
src/plugin-qt/shortcut/src/backend/x11/x11keyhandler.cpp
src/plugin-qt/shortcut/src/backend/x11/x11keyhandler.h
Propagate lock state changes through the abstraction layer and deduplicate D-Bus state change signals
  • Extend AbstractKeyHandler with numLockStateChanged(bool) and capsLockStateChanged(bool) signals
  • Connect the key handler’s lock state signals to new slots in KeybindingManager that update internal last-state fields and only emit D-Bus signals on real changes
  • Initialize m_lastNumLockState and m_lastCapsLockState in the KeybindingManager constructor using existing getters
  • Refactor SetNumLockState and SetCapsLockState to delegate to the new update* slots instead of emitting signals directly, avoiding duplicate emission
src/plugin-qt/shortcut/src/backend/abstractkeyhandler.h
src/plugin-qt/shortcut/src/core/keybindingmanager.cpp
src/plugin-qt/shortcut/src/core/keybindingmanager.h

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

Subscribe to XKB modifier lock state notifications independently of passive shortcut grabs.
Propagate Caps Lock and Num Lock changes through the key handler and deduplicate the exported D-Bus signals.
This also observes lock state changes made through XKB APIs without physical key press events.

独立于被动快捷键抓取订阅 XKB 修饰键锁定状态通知。
通过按键处理后端传递大小写锁定和数字锁定状态变化,并对导出的 D-Bus 信号去重。
同时支持检测通过 XKB API 直接修改且没有物理按键事件的锁定状态变化。

Log: monitor X11 lock state changes through XKB
Pms: BUG-372751
Change-Id: I923a352f91a63218d287bb013464538a65b27c56
@yixinshark
yixinshark force-pushed the fix/x11-lock-state-notifications branch from cc94572 to 0639b49 Compare August 5, 2026 08:03
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码实现了X11下锁定键状态的被动监听与去重机制,逻辑严密且无安全风险
逻辑、质量、性能均表现优异,无扣分项

■ 【详细分析】

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

X11KeyHandler::notifyLockStateChange中使用static_assert在编译期校验xkbStateNotify结构体大小,结合std::memcpy安全解析XCB事件;KeybindingManager::updateNumLockState与updateCapsLockState中的去重逻辑严谨,SetNumLockState中正确复用update方法统一了主动与被动路径,无空指针或内存越界风险

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

代码架构分层清晰,底层X11事件解析与上层状态管理解耦良好;enableLockStateMonitoring在XKB不可用时优雅降级并输出警告日志;新增的m_lastNumLockState等成员变量命名规范且在头文件中正确初始化

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

采用XKB事件订阅机制替代轮询,开销极低;状态去重判断仅为简单的整数比较,时间复杂度为O(1);事件解析使用底层的std::memcpy,无多余对象构造或系统调用

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

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码未引入命令注入、缓冲区溢出、越权等安全风险,事件内存拷贝由编译期断言严格保障边界安全

■ 【改进建议代码示例】

// 当前代码已非常完善,无需额外修改。以下为符合当前风格的扩展性建议示例(如未来需支持Wayland):
// 在 abstractkeyhandler.cpp 中可预留空实现,确保接口多态完整性
void AbstractKeyHandler::enableLockStateMonitoring()
{
    // Default no-op for non-X11 backends (e.g., Wayland)
}

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