Fix M9.8d review findings in binding capture
Arming capture drops the toolkit's held keys/modifiers and queued keyboard input; the chosen key stays owned until release or focus loss so its repeats, text and release reach neither toolkit nor world. Idle toolkit frames run when nothing is drawn and the toolkit queue is bounded. Controls host logic moves to ControlsFlow with an Apply -> save -> restart -> load rehearsal; key test events now carry realistic keycodes and modifiers with positive controls. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <imgui.h>
|
||||
#include <imgui_internal.h>
|
||||
#include <limits>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
@@ -167,6 +168,8 @@ void native_input() {
|
||||
event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP;
|
||||
event.key.windowID = id;
|
||||
event.key.scancode = scancode;
|
||||
event.key.key = SDL_GetKeyFromScancode(scancode, SDL_KMOD_NONE, false); // US fixture.
|
||||
event.key.down = down;
|
||||
event.key.repeat = repeat;
|
||||
CHECK(SDL_PushEvent(&event));
|
||||
};
|
||||
@@ -356,6 +359,8 @@ void toolkit_capture() {
|
||||
event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP;
|
||||
event.key.windowID = id;
|
||||
event.key.scancode = scancode;
|
||||
event.key.key = SDL_GetKeyFromScancode(scancode, SDL_KMOD_NONE, false); // US fixture.
|
||||
event.key.down = down;
|
||||
event.key.repeat = repeat;
|
||||
CHECK(SDL_PushEvent(&event));
|
||||
};
|
||||
@@ -452,6 +457,8 @@ void rebinding() {
|
||||
event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP;
|
||||
event.key.windowID = id;
|
||||
event.key.scancode = scancode;
|
||||
event.key.key = SDL_GetKeyFromScancode(scancode, SDL_KMOD_NONE, false); // US fixture.
|
||||
event.key.down = down;
|
||||
event.key.repeat = repeat;
|
||||
CHECK(SDL_PushEvent(&event));
|
||||
};
|
||||
@@ -536,6 +543,143 @@ void rebinding() {
|
||||
CHECK(app->poll_events() && move_y() == 1000);
|
||||
CHECK(app->present(frame) == Step::rendered);
|
||||
}
|
||||
// M9.8d review: realistic key events (keycode, scancode, modifiers). Keys the toolkit
|
||||
// saw pressed before binding capture are up there after choose, cancel or focus loss;
|
||||
// the chosen key is owned until released (no repeats, text or press reach the toolkit).
|
||||
int queued_keyboard_events() {
|
||||
const auto& queue = ImGui::GetCurrentContext()->InputEventsQueue;
|
||||
int n = 0;
|
||||
for (const auto& e : queue)
|
||||
n += e.Type == ImGuiInputEventType_Key || e.Type == ImGuiInputEventType_Text ? 1 : 0;
|
||||
return n;
|
||||
}
|
||||
void capture_balances_toolkit() {
|
||||
const auto frame = fixture();
|
||||
auto result = Application::open({320, 240, false, "software"});
|
||||
CHECK(result);
|
||||
auto app = std::move(result.value());
|
||||
CHECK(app->poll_events());
|
||||
const auto id = SDL_GetWindowID(window());
|
||||
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
|
||||
auto push = [id](SDL_Scancode scancode, bool down, bool repeat = false,
|
||||
SDL_Keymod mod = SDL_KMOD_NONE) {
|
||||
SDL_Event event{};
|
||||
event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP;
|
||||
event.key.windowID = id;
|
||||
event.key.scancode = scancode;
|
||||
event.key.key = SDL_GetKeyFromScancode(scancode, SDL_KMOD_NONE, false);
|
||||
event.key.mod = mod;
|
||||
event.key.down = down;
|
||||
event.key.repeat = repeat;
|
||||
CHECK(SDL_PushEvent(&event));
|
||||
};
|
||||
auto text = [id](const char* utf8) {
|
||||
SDL_Event event{};
|
||||
event.type = SDL_EVENT_TEXT_INPUT;
|
||||
event.text.windowID = id;
|
||||
event.text.text = utf8;
|
||||
CHECK(SDL_PushEvent(&event));
|
||||
};
|
||||
const auto frame_once = [&] {
|
||||
CHECK(app->poll_events() && app->present(frame) == Step::rendered);
|
||||
};
|
||||
const auto& io = ImGui::GetIO();
|
||||
// No overlay: the adapter still runs idle toolkit frames, so its key state follows.
|
||||
std::uint64_t tick = 300;
|
||||
const auto move_y = [&] { return required(app->sample_intention(++tick)).move_y; };
|
||||
CHECK(app->present(frame) == Step::rendered);
|
||||
// Positive controls: complete events press and release toolkit keys and modifiers.
|
||||
push(SDL_SCANCODE_Q, true);
|
||||
frame_once();
|
||||
CHECK(ImGui::IsKeyDown(ImGuiKey_Q));
|
||||
push(SDL_SCANCODE_Q, false);
|
||||
frame_once();
|
||||
CHECK(!ImGui::IsKeyDown(ImGuiKey_Q));
|
||||
push(SDL_SCANCODE_LSHIFT, true, false, SDL_KMOD_LSHIFT);
|
||||
frame_once();
|
||||
CHECK(ImGui::IsKeyDown(ImGuiKey_LeftShift) && io.KeyShift);
|
||||
push(SDL_SCANCODE_LSHIFT, false);
|
||||
frame_once();
|
||||
CHECK(!ImGui::IsKeyDown(ImGuiKey_LeftShift) && !io.KeyShift);
|
||||
|
||||
// Shift+Q and W held, capture armed (the host holds external capture for the
|
||||
// Controls panel), all three released while capture consumes keyboard input.
|
||||
const auto held_then_released = [&](const auto& finish) {
|
||||
app->set_ui_capture(true);
|
||||
push(SDL_SCANCODE_LSHIFT, true, false, SDL_KMOD_LSHIFT);
|
||||
push(SDL_SCANCODE_Q, true, false, SDL_KMOD_LSHIFT);
|
||||
push(SDL_SCANCODE_W, true, false, SDL_KMOD_LSHIFT);
|
||||
frame_once();
|
||||
CHECK(ImGui::IsKeyDown(ImGuiKey_Q) && ImGui::IsKeyDown(ImGuiKey_LeftShift) && io.KeyShift);
|
||||
app->begin_key_capture();
|
||||
push(SDL_SCANCODE_Q, false, false, SDL_KMOD_LSHIFT);
|
||||
push(SDL_SCANCODE_LSHIFT, false);
|
||||
push(SDL_SCANCODE_W, false);
|
||||
frame_once();
|
||||
finish();
|
||||
frame_once();
|
||||
CHECK(!ImGui::IsKeyDown(ImGuiKey_Q) && !ImGui::IsKeyDown(ImGuiKey_LeftShift) &&
|
||||
!ImGui::IsKeyDown(ImGuiKey_W) && !io.KeyShift);
|
||||
app->set_ui_capture(false);
|
||||
CHECK(app->poll_events() && move_y() == 0);
|
||||
};
|
||||
// 1. Choose J, then hold it across polls: repeats and text stay away from the toolkit.
|
||||
held_then_released([&] {
|
||||
push(SDL_SCANCODE_J, true);
|
||||
CHECK(app->poll_events() && app->take_captured_key() == KeyCode{SDL_SCANCODE_J});
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
push(SDL_SCANCODE_J, true, true);
|
||||
text("j");
|
||||
CHECK(app->poll_events() && queued_keyboard_events() == 0);
|
||||
CHECK(app->present(frame) == Step::rendered && !ImGui::IsKeyDown(ImGuiKey_J));
|
||||
}
|
||||
push(SDL_SCANCODE_J, false); // Release ends ownership; it is consumed too.
|
||||
CHECK(app->poll_events() && queued_keyboard_events() == 0);
|
||||
});
|
||||
// After release a fresh press reaches the toolkit normally (positive control).
|
||||
push(SDL_SCANCODE_J, true);
|
||||
CHECK(app->poll_events() && queued_keyboard_events() > 0);
|
||||
CHECK(app->present(frame) == Step::rendered && ImGui::IsKeyDown(ImGuiKey_J));
|
||||
push(SDL_SCANCODE_J, false);
|
||||
frame_once();
|
||||
CHECK(!ImGui::IsKeyDown(ImGuiKey_J));
|
||||
// 2. Explicit cancel.
|
||||
held_then_released([&] {
|
||||
app->cancel_key_capture();
|
||||
CHECK(!app->key_capture_active() && !app->take_captured_key());
|
||||
});
|
||||
// 3. Focus loss cancels capture; also drops an owned key held at that moment.
|
||||
held_then_released([&] {
|
||||
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
|
||||
CHECK(app->poll_events() && !app->key_capture_active() && !app->take_captured_key());
|
||||
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
|
||||
CHECK(app->poll_events());
|
||||
});
|
||||
app->begin_key_capture();
|
||||
push(SDL_SCANCODE_K, true);
|
||||
CHECK(app->poll_events() && app->take_captured_key() == KeyCode{SDL_SCANCODE_K});
|
||||
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
|
||||
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
|
||||
CHECK(app->poll_events());
|
||||
push(SDL_SCANCODE_K, true); // K's release never arrived; after focus loss it is free.
|
||||
CHECK(app->poll_events() && queued_keyboard_events() > 0);
|
||||
CHECK(app->present(frame) == Step::rendered && ImGui::IsKeyDown(ImGuiKey_K));
|
||||
// World input still works with fresh presses.
|
||||
push(SDL_SCANCODE_W, true);
|
||||
CHECK(app->poll_events() && move_y() == 1000);
|
||||
// While paused (hidden) nothing is drawn, yet the toolkit queue stays bounded.
|
||||
send(SDL_EVENT_WINDOW_HIDDEN, id);
|
||||
for (int round = 0; round < 20; ++round) {
|
||||
for (int i = 0; i < 100; ++i)
|
||||
push(SDL_SCANCODE_Q, i % 2 == 0);
|
||||
CHECK(app->poll_events() && app->present(frame) == Step::paused);
|
||||
CHECK(ImGui::GetCurrentContext()->InputEventsQueue.Size <= 512); // Adapter bound.
|
||||
}
|
||||
send(SDL_EVENT_WINDOW_SHOWN, id);
|
||||
for (int i = 0; i < 2; ++i)
|
||||
CHECK(app->poll_events() && app->present(frame) == Step::rendered);
|
||||
CHECK(!ImGui::IsKeyDown(ImGuiKey_Q)); // The flood ended with a release; never stuck.
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
client_controls_tests();
|
||||
@@ -544,6 +688,7 @@ int main() try {
|
||||
native_input();
|
||||
toolkit_capture();
|
||||
rebinding();
|
||||
capture_balances_toolkit();
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::fprintf(stderr, "%s: %s\n", e.what(), SDL_GetError());
|
||||
|
||||
Reference in New Issue
Block a user