Keep every captured key owned until its own release

Binding capture tracked one owned key, so a second capture could overwrite an
unreleased earlier choice and leak its repeats. Ownership is now a bitset over
scancodes, released per key or on focus loss, with successive-capture, release
order, cancel and focus-loss regressions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-02 16:15:54 -04:00
co-authored by Claude Opus 5.5
parent 14e676426f
commit 582d4baa20
5 changed files with 110 additions and 10 deletions
@@ -72,3 +72,8 @@ is suppressed while it is held). The adapter runs an idle toolkit frame when not
is drawn and bounds the toolkit input queue at 512 events (dropped with every is drawn and bounds the toolkit input queue at 512 events (dropped with every
toolkit key/button released). The Controls host logic lives in toolkit key/button released). The Controls host logic lives in
`apps/evo-client/controls_flow.hpp` and is rehearsed with the real adapter. `apps/evo-client/controls_flow.hpp` and is rehearsed with the real adapter.
Follow-up: ownership is a set (one bit per SDL scancode), not a single key. Each
chosen key is released only by its own key-up or by focus loss; a later capture,
or cancelling it, never transfers or drops earlier ownership; text stays
suppressed while any owned key is held.
+3 -2
View File
@@ -85,7 +85,8 @@ the keys in force. `client-panels` checks the panel geometry and that a slot cli
requests capture without a world click. requests capture without a world click.
Capture and toolkit (M9.8d review): arming binding capture clears the toolkit's held Capture and toolkit (M9.8d review): arming binding capture clears the toolkit's held
keys/modifiers and its queued keyboard/text input; the chosen key is consumed until keys/modifiers and its queued keyboard/text input; every chosen key is consumed
released (repeats, text, release), and focus loss frees it. With no overlay or a until its own release (repeats, text, release), across successive captures; focus
loss frees them all. With no overlay or a
paused window the adapter still runs idle toolkit frames; more than 512 queued paused window the adapter still runs idle toolkit frames; more than 512 queued
toolkit events are dropped with all toolkit keys/buttons released. toolkit events are dropped with all toolkit keys/buttons released.
+19
View File
@@ -3831,3 +3831,22 @@ repository format check.
Limits: physical keyboards, non-US layouts and IME remain unqualified; the Apply Limits: physical keyboards, non-US layouts and IME remain unqualified; the Apply
rehearsal is automated with synthetic events, not a manual session. rehearsal is automated with synthetic events, not a manual session.
## 2026-10-02 — M9.8d follow-up (.cache/reviews/m98d-followup-review.md)
Owned capture keys are now a bounded set (bitset over SDL scancodes) instead of one
optional key, so a second capture cannot overwrite an unreleased earlier choice.
Lifecycle regressions in `capture_balances_toolkit`: W then K chosen with K released
first (W repeat/text reach neither toolkit nor world until W's release, then a
fresh W moves and reaches the toolkit); reverse release order (K stays owned after
J's release; text flows again only when nothing is owned); cancel during a second
capture keeps the first key owned; focus loss frees both; positive controls after
each. Negative check: restoring single-key ownership fails the W repeat check.
Executed: Windows MSVC client 9/9 and client-network 10/10 (final, after
formatting). Linux: clang-19 client-probe with clang-tidy (keep-going, no owned
diagnostics) CTest 13/13; Xvfb lifecycle and panels; Clang ASan/UBSan CTest 13/13
plus Xvfb lifecycle/panels; GCC 14 client-network CTest 10/10; clang-tidy app
main/panels; format check. Synthetic event sequences only: physical OS repeat with
several held keys, non-US layouts and IME remain unqualified.
+12 -7
View File
@@ -2,6 +2,7 @@
#include "render/sdl_renderer.hpp" #include "render/sdl_renderer.hpp"
#define SDL_MAIN_HANDLED #define SDL_MAIN_HANDLED
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <bitset>
#include <cmath> #include <cmath>
#include <imgui.h> #include <imgui.h>
#include <imgui_impl_sdl3.h> #include <imgui_impl_sdl3.h>
@@ -126,9 +127,10 @@ struct Application::Impl {
bool backlogged{false}; bool backlogged{false};
bool key_capture{false}; // Binding capture armed. bool key_capture{false}; // Binding capture armed.
std::optional<KeyCode> captured_key; // Taken once by the host. std::optional<KeyCode> captured_key; // Taken once by the host.
// The chosen physical key stays owned by capture until its release (or focus loss): // Every chosen physical key stays owned by capture until its own release (or focus
// its repeats, text and release reach neither the toolkit nor the world. // loss), across successive captures: its repeats and release reach neither the
std::optional<SDL_Scancode> owned_key; // toolkit nor the world, and text is suppressed while any is held.
std::bitset<SDL_SCANCODE_COUNT> owned_keys;
// Capture sources: an external modal/launcher, the legacy Tab toggle and the // Capture sources: an external modal/launcher, the legacy Tab toggle and the
// toolkit's own keyboard focus. World actions run only when none applies. // toolkit's own keyboard focus. World actions run only when none applies.
bool external_capture{false}, tab_capture{false}, toolkit_capture{false}; bool external_capture{false}, tab_capture{false}, toolkit_capture{false};
@@ -304,16 +306,19 @@ std::expected<void, Error> Application::poll_events() {
: event.type == SDL_EVENT_TEXT_INPUT ? event.text.windowID == id : event.type == SDL_EVENT_TEXT_INPUT ? event.text.windowID == id
: event.type == SDL_EVENT_TEXT_EDITING ? event.edit.windowID == id : event.type == SDL_EVENT_TEXT_EDITING ? event.edit.windowID == id
: false; : false;
const auto code = key_event ? static_cast<std::size_t>(event.key.scancode) : 0;
const bool known = key_event && code < p.owned_keys.size();
const bool owned = const bool owned =
keyboard && p.owned_key && (!key_event || event.key.scancode == *p.owned_key); keyboard && (key_event ? known && p.owned_keys.test(code) : p.owned_keys.any());
if (keyboard && (p.key_capture || owned)) { if (keyboard && (p.key_capture || owned)) {
if (p.key_capture && event.type == SDL_EVENT_KEY_DOWN && !event.key.repeat) { if (p.key_capture && event.type == SDL_EVENT_KEY_DOWN && !event.key.repeat) {
p.captured_key = static_cast<KeyCode>(event.key.scancode); p.captured_key = static_cast<KeyCode>(event.key.scancode);
p.owned_key = event.key.scancode; if (known)
p.owned_keys.set(code);
p.key_capture = false; p.key_capture = false;
p.update_capture(); // Drains the rest of this batch before world input. p.update_capture(); // Drains the rest of this batch before world input.
} else if (owned && event.type == SDL_EVENT_KEY_UP) { } else if (owned && event.type == SDL_EVENT_KEY_UP) {
p.owned_key.reset(); p.owned_keys.reset(code); // Only that key; others stay owned.
} }
continue; continue;
} }
@@ -355,7 +360,7 @@ std::expected<void, Error> Application::poll_events() {
if (!p.state.accepts_input()) { if (!p.state.accepts_input()) {
p.press.reset(); // A release may never arrive for this window. p.press.reset(); // A release may never arrive for this window.
p.ui_press = false; p.ui_press = false;
p.owned_key.reset(); // The toolkit clears its own keys on focus loss. p.owned_keys.reset(); // The toolkit clears its own keys on focus loss.
if (p.key_capture) { // Focus loss cancels binding capture without a key. if (p.key_capture) { // Focus loss cancels binding capture without a key.
p.key_capture = false; p.key_capture = false;
p.update_capture(); p.update_capture();
+70
View File
@@ -667,6 +667,76 @@ void capture_balances_toolkit() {
// World input still works with fresh presses. // World input still works with fresh presses.
push(SDL_SCANCODE_W, true); push(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && move_y() == 1000); CHECK(app->poll_events() && move_y() == 1000);
push(SDL_SCANCODE_W, false);
CHECK(app->poll_events() && move_y() == 0);
// Successive captures (follow-up review): every chosen key stays owned until its
// own release; a second capture never overwrites the first.
const auto choose = [&](SDL_Scancode scancode) {
app->begin_key_capture();
push(scancode, true);
CHECK(app->poll_events() && app->take_captured_key() == static_cast<KeyCode>(scancode));
};
const auto quiet = [&](SDL_Scancode repeated) { // Repeat + text: nothing reaches anyone.
push(repeated, true, true);
text("x");
CHECK(app->poll_events() && queued_keyboard_events() == 0 && move_y() == 0);
CHECK(app->present(frame) == Step::rendered);
};
const auto fresh = [&](SDL_Scancode scancode, ImGuiKey toolkit_key) { // Positive control.
push(scancode, true);
CHECK(app->poll_events() && queued_keyboard_events() > 0);
CHECK(app->present(frame) == Step::rendered && ImGui::IsKeyDown(toolkit_key));
push(scancode, false);
frame_once();
CHECK(!ImGui::IsKeyDown(toolkit_key));
};
// a) W (a world key) then K chosen while W is held; K released first.
app->set_ui_capture(true);
choose(SDL_SCANCODE_W);
choose(SDL_SCANCODE_K);
app->set_ui_capture(false);
push(SDL_SCANCODE_K, false);
CHECK(app->poll_events() && queued_keyboard_events() == 0);
quiet(SDL_SCANCODE_W);
CHECK(!ImGui::IsKeyDown(ImGuiKey_W) && !ImGui::IsKeyDown(ImGuiKey_K));
push(SDL_SCANCODE_W, false);
CHECK(app->poll_events() && queued_keyboard_events() == 0);
push(SDL_SCANCODE_W, true); // Fresh press: world and toolkit both see it.
CHECK(app->poll_events() && move_y() == 1000);
CHECK(app->present(frame) == Step::rendered && ImGui::IsKeyDown(ImGuiKey_W));
push(SDL_SCANCODE_W, false);
frame_once();
fresh(SDL_SCANCODE_K, ImGuiKey_K);
// b) Reverse release order: J then K chosen; J released first, K still owned.
choose(SDL_SCANCODE_J);
choose(SDL_SCANCODE_K);
push(SDL_SCANCODE_J, false);
CHECK(app->poll_events() && queued_keyboard_events() == 0);
quiet(SDL_SCANCODE_K);
push(SDL_SCANCODE_K, false);
CHECK(app->poll_events() && queued_keyboard_events() == 0);
text("x"); // Nothing owned any more: text flows again.
CHECK(app->poll_events() && queued_keyboard_events() > 0);
frame_once();
fresh(SDL_SCANCODE_J, ImGuiKey_J);
fresh(SDL_SCANCODE_K, ImGuiKey_K);
// c) Cancel during the second capture keeps the first key owned.
choose(SDL_SCANCODE_J);
app->begin_key_capture();
app->cancel_key_capture();
quiet(SDL_SCANCODE_J);
push(SDL_SCANCODE_J, false);
CHECK(app->poll_events() && queued_keyboard_events() == 0);
fresh(SDL_SCANCODE_J, ImGuiKey_J);
// d) Focus loss frees every owned key.
choose(SDL_SCANCODE_J);
choose(SDL_SCANCODE_K);
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
CHECK(app->poll_events() && app->poll_events());
frame_once();
fresh(SDL_SCANCODE_J, ImGuiKey_J);
fresh(SDL_SCANCODE_K, ImGuiKey_K);
// While paused (hidden) nothing is drawn, yet the toolkit queue stays bounded. // While paused (hidden) nothing is drawn, yet the toolkit queue stays bounded.
send(SDL_EVENT_WINDOW_HIDDEN, id); send(SDL_EVENT_WINDOW_HIDDEN, id);
for (int round = 0; round < 20; ++round) { for (int round = 0; round < 20; ++round) {