Skip to content

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

Open
LFRon wants to merge 4 commits into
linuxdeepin:masterfrom
LFRon:fix/IME
Open

LFRon wants to merge 4 commits 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 reliable input-method operation across popup grabs, focus transitions, compositor restarts, and Wayland/XWayland session handovers.

New Features:

  • Add automatic input-method connection recovery and environment handover for Wayland and XWayland sessions.
  • Retarget input-method candidate popups when text-input focus moves between surfaces.

Bug Fixes:

  • Restore application focus and keyboard handling after popup keyboard grabs end or are replaced.
  • Prevent XWayland focus protection from disrupting embedded child-window focus changes.
  • Preserve text-input activation across transient focus changes and correctly reconcile it after input-method reconnects.
  • Prevent stale keyboard repeat state and invalid keyboard devices from producing lingering input events.

Enhancements:

  • Centralize text-input eligibility, keyboard filtering, popup focus tracking, and input-method state reconciliation across seats.
  • Improve input-method popup lifecycle handling, focus restoration, and placement during client teardown and surface changes.

Deployment:

  • Improve session socket activation and environment publication so input-method startup is synchronized with an accepted compositor socket and XWayland credentials.

@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:
提交邮箱中包含我们的合作伙伴,但您似乎并非合作伙伴的成员或对接人,请联系相关对接人将您添加至组织之中,或由其重新发起 Pull Request。
The commit email domain belongs to one of our partners, but it seems you are not yet a member of the current organization, please contact the contact person to add you to the organization or let them submit the Pull Request.

2 out of 2 committers have signed the CLA.
✅ LFRon
✅ zccrs
You can retrigger this bot by commenting recheck in this Pull Request

@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
@zzxyb
zzxyb requested a review from glyvut September 9, 2026 01:40
@LFRon
LFRon force-pushed the fix/IME branch 5 times, most recently from 9d1ef55 to 6135a3f Compare September 10, 2026 11:32
@deepin-bot

deepin-bot Bot commented Sep 10, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 0.10.0
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1392

@LFRon
LFRon force-pushed the fix/IME branch 5 times, most recently from 7fcbd2b to fc61cfe Compare September 16, 2026 05:34
@LFRon
LFRon force-pushed the fix/IME branch 7 times, most recently from 5008491 to 2cb9f46 Compare September 23, 2026 09:57
@LFRon
LFRon force-pushed the fix/IME branch 3 times, most recently from 868fe68 to 1933ed1 Compare October 4, 2026 11:41
@LFRon
LFRon force-pushed the fix/IME branch 2 times, most recently from 4d243a9 to 27af1d1 Compare October 9, 2026 10:57
@deepin-bot

deepin-bot Bot commented Oct 10, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 0.11.0
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1467

LFRon added 2 commits October 10, 2026 17:32
What this fixes
---------------
In WPS Office (and other XEmbed-based X11 apps) the input method is dead in
the document editing area while it keeps working in the home/search frame.
The editor is not a plain Qt child widget: it is an XEmbed plug window on a
separate X client connection, reparented inside the Qt shell top-level. To
route text into it the shell legitimately issues XSetInputFocus onto that
subwindow, exactly what a real X11 window manager allows.

Root cause
----------
xwm_handle_focus_in() guards against cross-application focus stealing: when a
FocusIn arrives for a window xwm does not manage and whose pid does not match,
it snaps input focus back to the previously tracked top-level. XEmbed plug
windows are never xwm surfaces (xwm only tracks managed top-levels), so every
legitimate in-client focus move into the plug is misread as a steal and yanked
back to the shell. The embedded editor never keeps real X input focus, so
input-method activation gated on real X focus (XIM/XIC set-focus) never fires:
the input method looks dead even though plain X keys still reach the window.

The fix
-------
Before refocusing, check whether the window that just gained X input focus is
located inside the currently focused surface's X subtree. That is an
in-application focus move inside the client's own hierarchy -- what a bare X
server and KWin leave alone -- so accept it silently instead of grabbing focus
back. Genuine cross-application stealing still falls through to the existing
refocus path.

The check walks upwards from the newly focused window (XQueryTree parent
pointers) with a hard cap of 8 hops, stopping at the root window, so it needs
at most a handful of synchronous X queries in every case -- one for a plain
top-level, two for a framed window. The previous top-down search visited the
focused window's whole subtree, which is unbounded in width, on the compositor
main loop for every FocusIn that needed the check. A WLR_DEBUG line (child,
parent, queries, result) keeps the runtime cost of focus churn observable.

Scope: only windows inside the focused top-level's own subtree are exempted;
the same-pid (Steam-type) special case, override-redirect handling, the
pointer-detail filter and the focus-serial race guard are untouched.
Combined input-method fix for treeland sessions: the Wayland input-method
stack in waylib, candidate-window anchoring and popup-grab bookkeeping in
treeland, the session environment that X11-side input methods depend on, and
the recovery paths for input methods that lose their seat connection.

Wayland input-method stack (waylib):

- Route input-method keys through a dedicated WSeat keyboard filter instead
  of a synthetic wlr_seat_keyboard_grab. The IM grab object and xdg-popup /
  drag grabs competed for one seat grab slot, and a displaced popup grab
  left text-input focus, IM activation and keyboard delivery out of sync
  (folder rename in dde-file-manager broke; XWayland windows stopped
  receiving keys after popup close). Physical key/modifier events now go to
  the active input-method keyboard endpoint only while the keyboard focus
  surface owns an eligible text input, excluding the IM's own virtual
  keyboards and active drags; popup and drag keep their own wlroots grabs
  untouched.

- Activation stays strictly text-input driven. It is held on the anchor only
  across the momentary null focus of a window switch, so a focus flickering
  through null does not tear down and re-create the fcitx5 grab and virtual
  keyboard. As soon as focus settles on a concrete surface without an
  eligible text input (terminals, XWayland windows, the compositor's own
  QML), the input method deactivates: those surfaces must keep their raw keys
  so the client-side input-method path (XIM / D-Bus frontends) stays in
  charge, and a held activation would leave the candidate window parented to
  an unrelated text input. A destroyed or client-disabled anchor still falls
  through to normal deactivation.

- Commits never follow the anchor: a commit is delivered only to the text
  input exactly matching current keyboard focus. The virtual-keyboard typing
  fallback stays as a defensive path for commits that arrive while a held
  activation has not been reconciled against the new keyboard focus yet
  (latin/symbol keysyms; CJK needs an xwayland-side text-input bridge, out
  of scope here); other ineligible commits are discarded with a warning
  instead of being typed at the wrong surface, and the synthetic key events
  convert the xkb keycode of the keysym lookup back to the evdev keycode
  wlr_keyboard_notify_key() expects.

- Text-input enablement follows the protocols again: a compositor-driven
  enter/leave is a notification, not a disablement. For text-input-v1 the
  client activate record is persistent (cleared only by the client's own
  deactivate or the surface's destruction, enter/leave kept paired), so
  focus returning to an activated surface re-arms it deterministically. For
  text-input-v2 the leave no longer emits disabled(); revocation flows only
  through the client disable request or enabled-surface destruction, and
  every new enter is preceded by the obsolete leave.

- Adopt upstream's empty-preedit clearing (text-input v1/v2/v3): a commit
  that does not carry a preedit means "clear it", and leave sends an empty
  preedit while the client is still enabled, so composing text can no longer
  stay rendered after the composing ended. The enabled-surface destroy
  connection is tracked separately so clearing it never drops the
  focused-surface listener installed by sendEnter(). Because compositor
  leave no longer reports disabled(), the client's disable is authoritative
  and reports disabled() unconditionally, so the helper never keeps a stale
  enabled text input.

- Attach the IM keyboard endpoint to the keyboard group device when it is
  created (fcitx5 recreates its virtual keyboard right before grab
  requests), take keyboard-focus enter payloads from the group, restore the
  group keyboard when a virtual keyboard dies, guard Qt repeat handling
  against invalid keyboards/disabled rates, and re-run instead of dropping
  re-entrant focus reconciliations.

- Input-method helper lifecycle: handleActiveIMDestroyed() sends leave to
  every focused text input (each protocol's sendLeave() carries the empty
  preedit) so clients drop composing state, and handleNewIMV2() re-announces
  the keyboard focus before reconciling, so a text input left by a
  disconnected input method is re-armed deterministically even though the
  seat focus did not change.

- Popup focus tracking (seatsurfacemanager) validates exact
  wlr_xdg_popup_grab membership, tracks replacement and end events
  separately, restores the concrete pre-popup focus only while the tracked
  popup still owns focus, and dismisses the concrete popup instead of
  ending an arbitrary seat grab.

- Categorized transition logs (input-method / text-input / popup-focus)
  carry object and state identifiers only, never key codes, surrounding
  text, preedit or committed user input.

Input-method popup anchoring (waylib + treeland):

- Candidate windows (zwp_input_popup_surface_v2) are anchored to the text
  input that currently owns the input-method focus, not to the surface that
  happened to be focused when the input method created its panel, so the
  candidate window follows one application switching text focus between its
  own windows.

  * waylib announces the settled focus surface through a new
    textInputFocusSurfaceChanged signal, emitted once the focus
    reconciliation settled (idempotent, nothing emitted during teardown).

  * A popup surface that arrives before any text input is eligible is parked
    in a pending list and attached on the next reconciliation. The input
    method reuses its panel surface until the panel hides, so dropping the
    popup kept the candidate window invisible for that whole period.

  * treeland resolves the placement parent of an input popup dynamically
    (the current text-input focus surface looked up in the root surface
    container, falling back to the popup's own recorded parent), re-parents
    the popup to the placement parent's output and re-arranges it whenever
    the text-input focus surface changes.

  * Input-popup add/remove handling is null-guarded on both sides: a popup
    whose anchoring surface is already gone is skipped symmetrically instead
    of dereferencing a wrapper that was never created.

Input-method session recovery (waylib + treeland + treeland-sd):

- fcitx5 opens its Wayland and X11 frontends exactly once, at its own
  startup, and never reopens them. A start that raced the session socket, or
  an instance replaced by `fcitx5 -r` (fcitx5 is started more than once at
  login), used to leave the session without any input method until fcitx5
  was restarted by hand: the replacement's request is rejected by the
  compositor's one-input-method-per-seat rule while the previous instance
  still holds the seat, and the rejected client never retries.

- WInputMethodHelper now announces when the seat's input method is accepted
  or destroyed; treeland relays it as org.deepin.Compositor1
  InputMethodChanged. treeland-sd reacts to the destroyed edge by handing a
  fresh Wayland socket (ReopenWaylandConnectionSocket) to the still-running
  fcitx5, which makes it re-request the input method. The handover is
  coalesced and delayed by a settle window, so the events of one ownership
  transition (owner change, socket activation, input method destroyed)
  produce a single handover after the replaced instance had time to close
  its connection; a connected edge cancels a pending handover and a working
  connection is never replaced needlessly.

- Xwayland is started with `-terminate` and restarted in place by wlroots
  when its last X11 client exits, which kills every X11 connection opened
  before the restart while fcitx5 keeps running. After publishing the
  refreshed DISPLAY/XAUTHORITY, and whenever fcitx5 (re)appears, treeland-sd
  asks it to open an X11 connection; the request is idempotent ("already
  exists" is ignored), so the XCBUI candidate window and XIM recover without
  restarting fcitx5.

XWayland session environment (src/systemd-socket.cpp,
misc/systemd/.../treeland-xwayland.service.in):

- Publish DISPLAY and XAUTHORITY through systemd1.SetEnvironment in addition
  to UpdateActivationEnvironment. Transient session units (How apps and
  fcitx5 are started via StartTransientUnit) inherit the *manager*
  environment, not the activation one, so X-side input methods were blind to
  the XWayland display in treeland sessions; dde-session performs this same
  call on KWin, which is why it only worked there. UnsetEnvironment and
  ExecStop now clear XAUTHORITY alongside DISPLAY.

- Gate unit readiness on the compositor's ActivateWayland business reply: a
  successful D-Bus call returning false means the compositor is registered
  but not yet ready for this session socket (login handover). Retried in a
  bounded ~20s window, then degraded to the historic publish-anyway path so
  the session can never hang; this stops advertising WAYLAND_DISPLAY and
  autostarting input methods against a socket nobody serves, where they
  stayed permanently broken.

Stacking robustness (src/surface/surfacewrapper.cpp):

- SurfaceWrapper::stackAfter() falls back to the sub-chain head (validated
  as a sibling by the entry guard) when its deepest stacked member lives in
  a different QQuickItem container, as X11 transient children may:
  QQuickItem::stackAfter() silently rejects non-siblings and aborted the
  raise with the stacking bookkeeping left out of sync.

Together with the vendored wlroots XEmbed focus exemption committed
separately, the input method now works in native Wayland clients (folder
rename and others, including across popups and window switches, with the
candidate window following the focused window), in XWayland clients (WPS
Office document editing included), and survives a compositor restart, a
late or replaced fcitx5 start, and an in-place Xwayland restart without
fcitx5 having to be restarted by hand.

Log: Input methods keep working across boot-time instance races, late
starts, compositor restarts and Xwayland restarts; the transition and
recovery paths are observable in the treeland-sd, waylib.protocols.inputmethod
and waylib.protocols.textinput logs.

Influence:
1. Native Wayland clients: text input keeps its key routing, commits and
   candidate window across focus changes, popups and window switches.
2. XWayland clients: WPS Office-style embedded editors keep IME; the X11
   candidate window and XIM recover after an in-place Xwayland restart.
3. Boot with duplicate fcitx5 starts: the session recovers its input method
   automatically within a few hundred milliseconds instead of staying dead
   until fcitx5 is restarted by hand.
4. Compositors without the new InputMethodChanged signal still work;
   treeland-sd logs a warning and keeps the activation-time handover.
deepin-wm and others added 2 commits October 10, 2026 22:54
handleNewKGV2() bound the new zwp_input_method_keyboard_grab_v2 endpoint to
the seat's keyboard-group device unconditionally, even though the keyboard
that actually delivers the filtered keys is the seat's current keyboard.

The first key routed through filterKey() then made setKeyboard() rebind the
endpoint, and wlroots' wlr_input_method_keyboard_grab_v2_set_keyboard()
re-sent repeat-info and modifiers for the new keyboard. Together with the
explicit virtual-keyboard modifiers request that is three modifiers events
where the protocol sequence has two, which is what made
test_input_method_unstable_v2 fail on the keyboard-grab block.

Keep the keyboard-group fallback for the case it was written for - the seat's
current keyboard being the input method's own virtual keyboard, which
setKeyboard() deliberately skips to avoid a keymap echo loop - and bind to the
seat keyboard otherwise.
fix(waylib/inputmethod): bind IME keyboard endpoint to the seat keyboard
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