Skip to content

Preserve X11 key-repeat identity across focus changes - #18

Open
TheRenegadeDev wants to merge 6 commits into
storytold:migration/egui-037-20261024from
TheRenegadeDev:fix/2572-x11-ordered-key-state
Open

TheRenegadeDev wants to merge 6 commits into
storytold:migration/egui-037-20261024from
TheRenegadeDev:fix/2572-x11-ordered-key-state

Conversation

@TheRenegadeDev

@TheRenegadeDev TheRenegadeDev commented Oct 27, 2026 •

Copy link
Copy Markdown

Summary

Preserve server-ordered X11 focus key state so a held key is not reported as a fresh press after delayed dispatch. Retain native child/XEmbed input attribution and clear repeat provenance on release.

FocusIn -> event-ordered KeymapNotify -> held-repeat identity
release -> clear that key's provenance -> later press stays fresh

Supports GridCraft #2572, through egui #7 and GridCraft #2675. Targets migration/egui-037-20261024 from pin a517b384. This is fork-scoped integration for the pinned GridCraft dependency graph; no separate upstream proposal is part of this PR.

Evidence

Current head c76a01e8eec14023b6f389fc4732dc1d7a370865 adds the requested committed regressions and formatting to 58385004.

  • Committed sequence coverage: all 16 policy tests pass, including the three new cases in focus_keymap.rs: press/focus out/focus in/repeat/release/fresh press; release outside then return; and two held keys released independently within one bitmap byte or across bytes. N/Y, return to the same or another window, and either key-release order are covered. These now live in the repository rather than only in local probes.
  • Negative controls: scratch-only mutations that lose repeat identity or clear a whole bitmap byte fail the corresponding newly committed tests. The checked-in production policy was not mutated.
  • Current local execution: the exact checked-in X11 policy module and its tests run through a Rust test wrapper on Windows. cargo test --offline --locked --lib passes 17 Windows tests; cargo clippy --offline --locked --all-targets -- -D warnings passes. The existing ignored local lock was reused unchanged. No dependencies or tools were installed or downloaded. Windows library tests do not exercise the X11 dispatcher.
  • Formatting follow-up: the reported event closure/let-else reflow and ffi/focus_keymap ordering are fixed. The policy module passes the available stable rustfmt check; the corrected event hunk matches its formatting output. Nightly is not installed locally, so no local nightly check was run. FredApps now reports cargo +nightly fmt --check passes on this head; this is reviewer-run evidence, not a hosted fmt job. Unrelated base formatting that differs under stable's unsupported nightly options was left alone.
  • Production preservation: the policy before #[cfg(test)] is byte-identical to 58385004. Other production edits are whitespace, one empty-return semicolon and module-declaration ordering. No API, dependency, workflow, changelog or consumer-pin changes. Existing consumers still pin 58385004.
  • Historical Linux evidence: GridCraft Ubuntu CI compiled exact predecessor 58385004 with X11 enabled and passed all ten app gate stages plus the unchanged-lockfile check. This consumer result covers the predecessor only, not current-head hosted execution or the full standalone platform/toolchain matrix.
  • Current-head approval and reviewer-run Linux validation: FredApps approved c76a01e8, confirming both requested changes are fixed. He reports nightly formatting passes on this head, warning-free cargo clippy --all-targets, and 223 Linux library tests passed after merging this head with the current migration branch. These are reviewer-reported results; the local merge SHA/tree was not supplied, and they are not our local Windows runs or hosted CI. CI run 44227516408 is still action_required with zero jobs executed, requiring repository-admin action. No hosted result or live X11/XIM run is established.
  • Focus-loss limit: xinput2_unfocused keeps the base's query-keymap synthetic releases before Focused(false); this patch clears provenance at loss. A delayed query after physical release can already see the key up. The policy tests do not establish a complete per-key callback/release-delivery guarantee.

Merge Danger

Door: two-way; code can be reverted.

Blast Radius: X11 keyboard focus and child-window attribution.

Event-ordered state is bounded to tracked windows. Committed regressions cover stale authority, unrelated releases, focus transitions and independent held keys. Current source review found no new behavior in the test/formatting follow-up; it is not an additional independent audit.

  • Tested on all platforms changed — reviewer-run Linux library/Clippy and nightly formatting results are recorded above; hosted CI remains approval-gated, and live X11/XIM/XEmbed/physical keyboard operation remains unverified.
  • Added a changelog entry.
  • Documented the event-order contract.

@FredApps FredApps left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ReviewerA — source-read COMMENT (not an approval)

I read the head of the hunk: the X11 event processor gets a FocusKeymap module import, and per the title key-repeat identity is preserved across focus changes: when focus moves between windows (or out and back), a held key's repeat events keep their identity, so the app doesn't see a fresh press (or lose the release) at the change. This is the XInput2 path, which is easy to get wrong, because a focus-in event carries the current key state and the raw events differ from the device events. In this fork the patch should stay small; say whether it is meant for the fork only or is going upstream (and link the upstream issue). A held key whose release arrives after focus is lost must be released in the app (as a synthetic release), not stuck. I did not read the new module. Add tests with synthetic event sequences: press, focus out, focus in, repeat, release; press, focus out, release outside; and a focus change with two keys held. I can't test X11 here. Nothing was run.

I ran nothing and have no exact-head CI result for this PR, so this is not an approval. Where a diff was long I read its code hunks rather than every generated or documentation line.

Status: waiting_author.

@TheRenegadeDev

Copy link
Copy Markdown
Author

@FredApps — response to ReviewerA:

This is fork-scoped, targeting the pinned migration/egui-037-20261024 dependency graph for GridCraft rust-windowing#2572. I have not opened a separate upstream issue or PR.

One distinction: the held-key snapshot comes from Core FocusIn/KeymapNotify, captured before XIM filtering. XI2 retains focus/IME ownership; the raw-device path is unchanged. The new module is focus_keymap.rs at 58385004.

I reran its 13 committed tests and three supplementary probes calling that same production policy: 16 passed. The probes exercise press → focus out → focus in → repeat → release → fresh press; release outside followed by return; and two held keys with independent releases, both within one bitmap byte and across bytes. These additional probes are local verification artifacts, not newly committed tests or live X11 dispatch tests. I kept the reviewed commit and dependency pins unchanged because the checks found no repeat-provenance regression.

For synthetic releases, xinput2_unfocused retains the base's query-keymap behavior: emit synthetic Released events for keys in that snapshot, then Focused(false). The patch clears its repeat provenance at loss. I cannot claim a complete release-delivery guarantee: if loss dispatch is delayed until after physical release, that existing query may already report the key up. The release-outside probe proves that repeat provenance does not stick; it does not prove callback delivery to an X11 application.

Hosted Ubuntu CI compiles this exact winit SHA with X11 enabled and passes all ten GridCraft gates plus the unchanged-lockfile check. There is no standalone winit CI result or live X11/XIM/XEmbed run. I updated the description with the fork scope, current consumer build and these verification limits.

@FredApps FredApps left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-review at 58385004. It's the same head as my Nov 1 comment, so I compared your reply with the code and ran it locally.

Answered:

  • Scope: fork-only, for the pinned migration/egui-037-20261024 graph. The description now says so.
  • Where the snapshot comes from: Core FocusIn/KeymapNotify, taken before XIM filtering. The XI2 raw-device path is unchanged.
  • Synthetic release on focus loss: xinput2_unfocused keeps the base's query-keymap releases, and the patch clears the repeat provenance when focus is lost.

Still open:

  • The event-sequence tests I asked for aren't committed. focus_keymap.rs has 13 tests, but the three sequences from my review only exist as your local probes: press → focus out → focus in → repeat → release; press → focus out → release outside → return; and two held keys released separately, in one bitmap byte and across bytes. They call the same production policy, so please commit them to focus_keymap.rs. Then a later change to the policy can't quietly break them.
  • Nightly rustfmt (the repo's fmt job) fails in this PR's own code. In x11/event_processor.rs:1042 the closure chain and the let … else { return }; need reflowing. In x11/mod.rs:40, mod focus_keymap; needs to go after pub mod ffi;. cargo +nightly fmt fixes both.

What I ran on Linux, merged with current migration/egui-037-20261024 (merges cleanly): cargo clippy --all-targets shows no warnings, and cargo test --lib passes all 220 tests. There's no standalone winit CI and no live X11 run for this head.

Once those two points are in, I expect to approve.

Cover held repeats across focus, releases outside, and independent bitmap keys.
Apply the requested event-handler and module-order formatting.
@TheRenegadeDev

Copy link
Copy Markdown
Author

Follow-up to this review, committed in c76a01e8eec14023b6f389fc4732dc1d7a370865:

  • The three requested sequences are now in focus_keymap.rs: focus out/in with held N/Y, release outside then return, and independent releases within one bitmap byte/across bytes. The latter covers either release order; focus return covers the same or another window. All 16 committed policy tests pass through a wrapper including that exact module.
  • Applied the closure/let-else reflow and moved mod focus_keymap; after pub mod ffi;. The production policy is byte-identical to 58385004; the other production changes are formatting, an empty-return semicolon and module order.
  • Windows cargo test --offline --locked --lib: 17 passed. Strict all-target Clippy passes. Scratch mutations losing repeat identity or clearing another key's bitmap bit fail the new sequence tests.

Nightly is not installed here, so I could not execute cargo +nightly fmt and have not claimed the full fmt job passes. The policy module passes available stable rustfmt, and the corrected event hunk matches its output; the description records the nightly limitation as CONTRIBUTING.md allows. No toolchain was installed.

The new tests remain policy-level coverage. They do not prove live X11/XIM/XEmbed callbacks or complete focus-loss release delivery. Your 220-test Linux result and the linked consumer CI cover the predecessor, not this new head. Consumer pins are unchanged.

@FredApps FredApps left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-review at c76a01e8. Both open points from my last review are fixed:

  • The event sequences are committed tests now. These are in focus_keymap.rs:
    • press_focus_out_focus_in_repeat_release_then_fresh_press
    • press_focus_out_release_outside_then_return_has_no_stale_repeat
    • two_held_keys_keep_independent_repeat_and_release_state, which covers separate bytes and one byte, released in either order
  • Nightly rustfmt. cargo +nightly fmt --check passes on this head now.

What I ran on Linux, with this head merged with current migration/egui-037-20261024 (merges cleanly): cargo clippy --all-targets shows no warnings, and cargo test --lib passes all 223 tests. The CI run for this head (44227516408) is waiting for approval, and that needs a repo admin, so there's still no hosted result. I didn't do a live X11/XIM run either.

Approving.

This branch has not been deployed

No deployments
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