Skip to content

fix(waylib/inputmethod): restore IME keyboard grab after popup grab ends - #1226

Open
LFRon wants to merge 1 commit into
linuxdeepin:masterfrom
LFRon:fix/IME
Open

fix(waylib/inputmethod): restore IME keyboard grab after popup grab ends#1226
LFRon wants to merge 1 commit into
linuxdeepin:masterfrom
LFRon:fix/IME

Conversation

@LFRon

@LFRon LFRon commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

该问题修复的是由于设置了QT_IM_MODULES, XMODIFIERS等环境变量后, 在treeland运行一段时间后一定会出现的几个问题:

  1. Xwayland应用/非Qt的纯Wayland应用(例如Electron框架的)窗口中有的能正常用fcitx5, 有的压根弹不出fcitx5窗口
  2. DDE文件管理器有极大概率弹不出fcitx5输入法窗口形同报废
  3. 能不能恢复fcitx5状态? 能, 你只要把fcitx5能工作的Wayland/Xwayland窗口跟有问题的窗口都窗口化, 先在输入法正常的窗口调出输入法, 再在不正常的窗口输入框上按一下就可以, 但这会带来刚才输入法正常的窗口会持续置顶除非你按另外第三方(同时不属于两者的窗口)或者按dde-shell强制取消聚焦

该PR同时修复了上述问题

Summary by Sourcery

Restore the input method (IME) keyboard grab after temporary keyboard grabs end to ensure consistent input method availability across applications.

Bug Fixes:

  • Ensure IME keyboard grab is re-installed when another keyboard grab ends, preventing loss of fcitx5 input in certain XWayland and Wayland applications.
  • Clear stale active keyboard grab state when a new IME keyboard grab is created or destroyed to avoid inconsistent grab handling.

Summary by Sourcery

Restore reliable IME input across applications by coordinating keyboard filtering, popup grabs, focus transitions, and text-input state.

Bug Fixes:

  • Restore IME keyboard event handling after popup or other temporary keyboard grabs are replaced or end, preventing intermittent fcitx5 input failures across XWayland and Wayland applications.
  • Prevent stale text-input focus and keyboard-grab state from causing lost or misdirected IME activation and commits.

Enhancements:

  • Centralize seat keyboard filtering and improve popup keyboard-grab tracking and focus restoration.
  • Reconcile text-input focus and activation state across focus, enablement, destruction, and input-method lifecycle changes.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: LFRon

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

@deepin-ci-robot

Copy link
Copy Markdown

Hi @LFRon. Thanks for your PR.

I'm waiting for a linuxdeepin member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

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.

@sourcery-ai

sourcery-ai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR ensures that when a temporary keyboard grab ends, the IME’s own keyboard grab is correctly restored, preventing situations where fcitx5 fails to appear or becomes unusable after other grabs interfere.

Sequence diagram for restoring IME keyboard grab after other grab ends

sequenceDiagram
    participant Seat
    participant WInputMethodHelper
    participant IMEKeyboardGrab
    participant OtherKeyboardGrab

    Seat->>WInputMethodHelper: notify_keyboard_grab_begin
    WInputMethodHelper->>Seat: keyboard_start_grab(OtherKeyboardGrab)

    Seat->>WInputMethodHelper: notify_keyboard_grab_end
    WInputMethodHelper->>WInputMethodHelper: handleKeyboardGrabEnd()
    alt [activeKeyboardGrab and current grab != keyboardGrab]
        WInputMethodHelper->>Seat: keyboard_start_grab(keyboardGrab)
    end
Loading

File-Level Changes

Change Details Files
Restore IME keyboard grab when other keyboard grabs end and clean up active grab tracking correctly.
  • Hook seat keyboard grab end notification to WInputMethodHelper::handleKeyboardGrabEnd
  • Clear activeKeyboardGrab when starting a new input method keyboard grab if one was already active
  • Adjust the before-destroy handler for input method keyboard grabs so it clears tracking state before calling endGrab
  • Implement handleKeyboardGrabEnd to re-install the IME keyboard grab when a replacement grab finishes
waylib/src/server/protocols/winputmethodhelper.cpp
waylib/src/server/protocols/winputmethodhelper.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

@LFRon
LFRon marked this pull request as ready for review July 30, 2026 12:00

@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 @LFRon, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@github-actions

github-actions Bot commented Jul 30, 2026

Copy link
Copy Markdown

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@LFRon
LFRon force-pushed the fix/IME branch 2 times, most recently from 6d84a13 to f4d41e9 Compare July 31, 2026 03:13
@deepin-bot

deepin-bot Bot commented Jul 31, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 0.8.17
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1229

@LFRon
LFRon force-pushed the fix/IME branch 2 times, most recently from c7ac3b3 to 60bf411 Compare July 31, 2026 07:37
@LFRon
LFRon marked this pull request as draft July 31, 2026 07:37
@LFRon
LFRon marked this pull request as ready for review July 31, 2026 08:34

@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 @LFRon, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@LFRon
LFRon force-pushed the fix/IME branch 3 times, most recently from 51bcb32 to c676a1f Compare August 3, 2026 05:06
@LFRon

LFRon commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

大佬们有没有好思路修复这个(

@LFRon
LFRon force-pushed the fix/IME branch 3 times, most recently from 49e9b4a to 502ecd5 Compare August 13, 2026 13:00
@deepin-bot

deepin-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 0.8.18
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1286

@LFRon
LFRon force-pushed the fix/IME branch 5 times, most recently from de1839c to c363b5c Compare August 21, 2026 09:24
@deepin-bot

deepin-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 0.9.0
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1312

With Wayland input-method support enabled, closing an xdg popup or switching focus can occasionally leave fcitx5 unusable. This is most visible when renaming a folder in dde-file-manager: the editor no longer activates the input method, XWayland clients can stop receiving keyboard input, and the terminal-based workaround can leave focus associated with the wrong window.

The previous helper implemented zwp_input_method_keyboard_grab_v2 by installing a synthetic wlr_seat_keyboard_grab. Xdg-popup and drag grabs share the same single seat grab slot, and wlroots replaces that slot without emitting a matching end event for the displaced grab. The protocol keyboard-grab object could therefore remain alive after the helper lost structural grab ownership, leaving text-input focus, input-method activation and keyboard delivery out of sync.

Focus reconciliation also handled leave and enter independently, while popup-grab end restored m_activatedSurface. A stale same-client leave could clear the new Qt text-input-v2 focus, and popup teardown could restore keyboard focus to an unrelated previously activated surface.

Route input-method keys independently from structural seat grabs:

- Add a WSeat keyboard filter and forward physical key and modifier events directly to the active input-method keyboard endpoint only when seat, Wayland client, focused surface and enabled text-input all match. Input-method-owned virtual keyboards bypass the filter to avoid feedback loops and preserve normal client delivery.

- Keep popup and drag grab ownership in wlroots. Validate popup focus against the exact wlr_xdg_popup_grab membership and pointer, track replacement and end events separately, restore the real popup parent or pre-popup focus only while a tracked popup still owns focus, and dismiss the concrete popup instead of ending an arbitrary seat grab.

- Derive the active text-input from current seat focus, send all obsolete leaves before any enter, and activate or deactivate the input method from that reconciled state. Focus loss disables IME routing immediately even if fcitx5's protocol keyboard endpoint is still alive.

- Harden text-input-v2 focus and enabled-surface destruction connections so callbacks from an old surface cannot clear newer focus state.

- Route keyboard filtering from the raw WSeat key path before Qt shortcut and QQuick delivery, so an eligible input-method grab cannot be bypassed by the compositor's focus window or by an early Qt shortcut handler.

- Preserve exclusive input-method key delivery, guard Qt repeat handling against invalid keyboards and disabled repeat rates, and clear stale repeat state whenever keyboard focus or the compositor focus window changes.

- Add categorized transition logs for input-method, text-input and popup-focus state. They contain object and state identifiers only, never key codes, surrounding text, preedit text or committed user input.

This removes the seat-grab race instead of trying to reinstall an IME grab after popup close, keeps XWayland keyboard delivery independent from stale IME state, prevents the raw Qt keyboard path from bypassing IME ownership, and matches the raw-wlroots and WWaylandResource lifecycle used by current master.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants