Files
aevolution/tests/client_lifecycle.cpp
T
dingoandClaude Opus 5.5 5f587f80e1
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (checks) (push) Successful in 17s
Foundation / checks (ecs) (push) Successful in 12s
Foundation / checks (client) (push) Successful in 1m47s
Foundation / checks (tsan) (push) Successful in 5m44s
Foundation / checks (network) (push) Successful in 13m29s
Foundation / checks (postgres) (push) Successful in 44m22s
Foundation / server-image (push) Successful in 31s
Foundation / postgres-tools (push) Successful in 1m34s
Foundation / image (push) Successful in 2m37s
Add M10.9c-1 session fuzzing and in-process abuse tests
ADR-0097. A libFuzzer target for session protocol v4, grants, tickets and
snapshot assembly (canonical re-encoding, verifiers always refuse, a
completed snapshot is applied), with the code under test compiled in, and
a seeded smoke CTest of the same target on every platform. Four
quic-sessions abuse tests measure an honest session tick by tick under a
Hello flood (garbage, untrusted grants, silence), a datagram flood, a
slow peer and a connect/disconnect storm; it keeps every snapshot and
receipt while connections and retained state stay bounded.

evo-session-probe run --grants FILE [--grant N] logs in from an existing
bundle (a dev renewal's) without an issuer key. client_lifecycle uses
SDL_getenv (MSVC rejected std::getenv).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 22:57:45 -04:00

776 lines
35 KiB
C++

#include "evo/client/application.hpp"
#include "render/sdl_renderer.hpp"
#include <SDL3/SDL.h>
#include <array>
#include <cmath>
#include <cstdio>
#include <filesystem>
#include <imgui.h>
#include <imgui_internal.h>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
void client_controls_tests();
namespace {
using namespace evo::client;
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("client lifecycle line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
template <typename T, typename E> bool refused(const std::expected<T, E>& result, E error) {
return !result && result.error() == error;
}
evo::render::Frame fixture() {
const std::array<evo::render::Rectangle, 1> rectangles{{{0, 0, 32, 32, {255, 0, 0, 255}}}};
auto result = evo::render::Frame::create(rectangles);
CHECK(result);
return std::move(result.value());
}
SDL_Window* window() {
int count{};
SDL_Window** windows = SDL_GetWindows(&count);
SDL_Window* result = count == 1 ? windows[0] : nullptr;
SDL_free(static_cast<void*>(windows));
CHECK(result);
return result;
}
void send(SDL_EventType type, SDL_WindowID id) {
SDL_Event event{};
event.type = type;
if (type >= SDL_EVENT_RENDER_TARGETS_RESET && type <= SDL_EVENT_RENDER_DEVICE_LOST)
event.render.windowID = id;
else
event.window.windowID = id;
CHECK(SDL_PushEvent(&event));
}
void frame_contract() {
using namespace evo::render;
std::array<Rectangle, 1> r{{{1, 2, 3, 4, {5, 6, 7, 255}}}};
const auto frame = Frame::create(r);
CHECK(frame);
r[0].x = 100;
CHECK(frame->rectangles()[0].x == 1); // Owns copy; caller mutation cannot alter it.
r[0].x = std::numeric_limits<float>::quiet_NaN();
CHECK(refused(Frame::create(r), FrameError::invalid_geometry));
r[0].x = 0;
r[0].width = -1;
CHECK(refused(Frame::create(r), FrameError::invalid_geometry));
r[0].width = std::numeric_limits<float>::infinity();
CHECK(refused(Frame::create(r), FrameError::invalid_geometry));
std::vector<Rectangle> full(max_rectangles);
CHECK(Frame::create(full));
full.emplace_back();
CHECK(refused(Frame::create(full), FrameError::too_many_primitives));
}
void pixels(SDL_Window* native, const evo::render::Frame& frame, WindowState state) {
auto* renderer = SDL_GetRenderer(native);
CHECK(renderer);
// Exercise non-unit, anisotropic drawable scaling without requiring a HiDPI monitor.
state.logical_width = state.pixel_width / 2;
state.logical_height = state.pixel_height;
CHECK(detail::draw(renderer, frame, state));
const std::unique_ptr<SDL_Surface, decltype(&SDL_DestroySurface)> surface{
SDL_RenderReadPixels(renderer, nullptr), SDL_DestroySurface};
CHECK(surface);
Uint8 r{}, g{}, b{}, a{};
CHECK(SDL_ReadSurfacePixel(surface.get(), 48, 8, &r, &g, &b, &a));
CHECK(r == 255 && g == 0 && b == 0 && a == 255);
CHECK(SDL_ReadSurfacePixel(surface.get(), 80, 8, &r, &g, &b, &a));
CHECK(r == 0 && g == 0 && b == 0 && a == 255);
CHECK(SDL_ReadSurfacePixel(surface.get(), 8, 48, &r, &g, &b, &a));
CHECK(r == 0 && g == 0 && b == 0 && a == 255);
}
void lifecycle() {
const auto frame = fixture();
CHECK(refused(Application::open({0, 64, false, "software"}), Error::invalid_config));
CHECK(refused(Application::open({64, 64, false, "evo-invalid"}), Error::renderer));
CHECK(SDL_WasInit(0) == 0); // Failed partial initialization released SDL.
const char* video_driver = SDL_getenv("SDL_VIDEO_DRIVER"); // Portable; MSVC deprecates getenv.
const int rounds = video_driver != nullptr && std::string_view{video_driver} == "x11" ? 1 : 32;
for (int round = 0; round < rounds; ++round) {
auto result = Application::open({160, 120, false, "software"});
if (!result) {
throw std::runtime_error(
"client lifecycle open failed round=" + std::to_string(round) + " error=" +
std::to_string(static_cast<unsigned>(result.error())) + " SDL=" + SDL_GetError());
}
auto app = std::move(result.value());
CHECK(refused(Application::open({}), Error::already_open));
CHECK(app->step(frame) == Step::rendered);
auto* native = window();
pixels(native, frame, app->state());
const auto id = SDL_GetWindowID(native);
// Foreign close/reset must not affect the one owned window.
send(SDL_EVENT_WINDOW_CLOSE_REQUESTED, id + 1);
send(SDL_EVENT_RENDER_DEVICE_LOST, id + 1);
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
CHECK(app->step(frame) == Step::rendered);
CHECK(app->state().accepts_input());
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
CHECK(app->step(frame) == Step::rendered);
CHECK(!app->state().accepts_input());
send(SDL_EVENT_WINDOW_MINIMIZED, id);
CHECK(app->step(frame) == Step::paused);
send(SDL_EVENT_WINDOW_RESTORED, id);
CHECK(app->step(frame) == Step::rendered);
CHECK(SDL_SetWindowSize(native, 240, 180));
send(SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED, id);
send(SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED, id);
CHECK(app->step(frame) == Step::rendered);
int lw{}, lh{}, pw{}, ph{};
CHECK(SDL_GetWindowSize(native, &lw, &lh));
CHECK(SDL_GetWindowSizeInPixels(native, &pw, &ph));
const auto state = app->state();
CHECK(state.logical_width == lw && state.logical_height == lh);
CHECK(state.pixel_width == pw && state.pixel_height == ph);
CHECK(std::isfinite(state.content_scale) && state.content_scale > 0);
send(SDL_EVENT_RENDER_TARGETS_RESET, id);
CHECK(app->step(frame) == Step::rendered);
send(SDL_EVENT_RENDER_DEVICE_RESET, id);
CHECK(app->step(frame) == Step::rendered);
CHECK(SDL_HideWindow(native));
send(SDL_EVENT_WINDOW_HIDDEN, id);
CHECK(app->step(frame) == Step::paused);
CHECK(SDL_ShowWindow(native));
send(SDL_EVENT_WINDOW_SHOWN, id);
CHECK(app->step(frame) == Step::rendered);
if (round % 2 == 0) {
send(SDL_EVENT_RENDER_DEVICE_LOST, id);
CHECK(refused(app->step(frame), Error::device_lost));
CHECK(refused(app->step(frame), Error::device_lost));
CHECK(app->state().failed && !app->state().accepts_input());
} else {
send(SDL_EVENT_WINDOW_CLOSE_REQUESTED, id);
CHECK(app->step(frame) == Step::closed);
CHECK(app->step(frame) == Step::closed);
}
app.reset();
CHECK(SDL_WasInit(0) == 0);
}
auto hidden = Application::open({64, 64, true, "software"});
CHECK(hidden);
CHECK((*hidden)->step(frame) == Step::paused);
send(SDL_EVENT_QUIT, 0);
CHECK((*hidden)->step(frame) == Step::closed);
}
Intention required(std::expected<Intention, InputError> result) {
CHECK(result);
return result.value();
}
void native_input() {
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 key = [id](SDL_Scancode scancode, bool down, bool repeat = false) {
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); // US fixture.
event.key.down = down;
event.key.repeat = repeat;
CHECK(SDL_PushEvent(&event));
};
key(SDL_SCANCODE_W, true);
key(SDL_SCANCODE_RIGHT, true);
CHECK(app->poll_events());
const auto first = app->sample_intention(40);
CHECK(first && first->move_y == 1000 && first->sequence == 1 && first->tick == 40);
CHECK(app->take_navigation().x == 1);
key(SDL_SCANCODE_W, false);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(41)).move_y == 0);
key(SDL_SCANCODE_W, true);
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
key(SDL_SCANCODE_W, true, true);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(42)).move_y == 0 && app->take_navigation().x == 0);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events());
app->set_ui_capture(true);
key(SDL_SCANCODE_TAB, true); // Legacy shortcut cannot escape an external modal.
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && app->ui_captured());
CHECK(required(app->sample_intention(43)).move_y == 0);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events());
app->set_ui_capture(false);
CHECK(required(app->sample_intention(44)).move_y == 0);
CHECK(app->poll_events());
key(SDL_SCANCODE_TAB, true);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events());
CHECK(app->ui_captured() && required(app->sample_intention(45)).move_y == 0);
key(SDL_SCANCODE_TAB, true, true);
CHECK(app->poll_events());
CHECK(app->ui_captured());
key(SDL_SCANCODE_TAB, false);
key(SDL_SCANCODE_TAB, true);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events());
CHECK(!app->ui_captured() && required(app->sample_intention(46)).move_y == 1000);
// A saturated event batch cancels motion until the backlog drains.
for (int i = 0; i < 300; ++i)
key(SDL_SCANCODE_RIGHT, true);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(47)).move_y == 0 && app->take_navigation().x == 0);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(48)).move_y == 0 && app->take_navigation().x == 0);
SDL_Event wheel{};
wheel.type = SDL_EVENT_MOUSE_WHEEL;
wheel.wheel.windowID = id;
wheel.wheel.y = 2;
wheel.wheel.mouse_x = 123;
wheel.wheel.mouse_y = 45;
wheel.wheel.direction = SDL_MOUSEWHEEL_FLIPPED;
CHECK(SDL_PushEvent(&wheel));
CHECK(app->poll_events());
const auto navigation = app->take_navigation();
CHECK(navigation.wheel == -2 && navigation.pointer.x == 123 && navigation.pointer.y == 45);
app->set_ui_capture(true);
CHECK(SDL_PushEvent(&wheel));
CHECK(app->poll_events());
CHECK(app->take_navigation().wheel == 0);
app->set_ui_capture(false);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events());
key(SDL_SCANCODE_W, true);
send(SDL_EVENT_WINDOW_MINIMIZED, id);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(49)).move_y == 0);
send(SDL_EVENT_WINDOW_RESTORED, id);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(50)).move_y == 0);
app->set_ui_capture(true);
key(SDL_SCANCODE_W, true); // Queued while captured; releasing capture must not replay it.
app->set_ui_capture(false);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(51)).move_y == 0);
CHECK(app->present(frame) == Step::rendered);
}
// M9.8c: the toolkit sees input first. A click that operates a widget never selects
// through it; keyboard focus in a text field suppresses world movement; releasing that
// focus requires a fresh key press (nothing is synthesized from held keys).
void toolkit_capture() {
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);
int presses = 0;
bool text_focus = false;
std::array<char, 32> text{}, field{};
ImVec2 field_min{}, field_max{};
app->set_overlay([&] {
ImGui::SetNextWindowPos({0, 0}, ImGuiCond_Always);
ImGui::SetNextWindowSize({150, 150}, ImGuiCond_Always);
ImGui::Begin("panel", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
if (ImGui::Button("Press", {130, 40}))
++presses;
ImGui::InputText("field", field.data(), field.size());
field_min = ImGui::GetItemRectMin();
field_max = ImGui::GetItemRectMax();
if (text_focus) {
ImGui::SetKeyboardFocusHere();
ImGui::InputText("name", text.data(), text.size());
}
ImGui::End();
});
auto mouse = [id](SDL_EventType type, float x, float y) {
SDL_Event event{};
event.type = type;
if (type == SDL_EVENT_MOUSE_MOTION) {
event.motion.windowID = id;
event.motion.x = x;
event.motion.y = y;
} else {
event.button.windowID = id;
event.button.button = SDL_BUTTON_LEFT;
event.button.down = type == SDL_EVENT_MOUSE_BUTTON_DOWN;
event.button.clicks = 1;
event.button.x = x;
event.button.y = y;
}
CHECK(SDL_PushEvent(&event));
};
const auto click = [&](float x, float y) {
mouse(SDL_EVENT_MOUSE_MOTION, x, y);
CHECK(app->poll_events() && app->present(frame) == Step::rendered); // Toolkit hovers.
mouse(SDL_EVENT_MOUSE_BUTTON_DOWN, x, y);
CHECK(app->poll_events() && app->present(frame) == Step::rendered);
mouse(SDL_EVENT_MOUSE_BUTTON_UP, x, y);
CHECK(app->poll_events() && app->present(frame) == Step::rendered);
return app->take_navigation().click;
};
// Over the widget: the button activates once, the world sees no click.
// The panel exists before the user clicks (as every application panel does).
CHECK(app->present(frame) == Step::rendered && app->present(frame) == Step::rendered);
CHECK(!click(60, 30) && presses == 1 && app->ui_wants_pointer());
// Open world: a click is delivered once, then consumed.
const auto world = click(260, 200);
CHECK(world && world->x == 260 && world->y == 200 && presses == 1);
CHECK(!app->take_navigation().click);
// A drag is not a click.
mouse(SDL_EVENT_MOUSE_MOTION, 260, 200);
CHECK(app->poll_events() && app->present(frame) == Step::rendered);
mouse(SDL_EVENT_MOUSE_BUTTON_DOWN, 260, 200);
mouse(SDL_EVENT_MOUSE_BUTTON_UP, 290, 200);
CHECK(app->poll_events() && !app->take_navigation().click);
// Same batch, no settling frame: the pointer was last over the world, then moves
// onto the panel and clicks. Ownership is resolved per event, so the world sees
// nothing and the widget still activates on the toolkit's next frames.
mouse(SDL_EVENT_MOUSE_MOTION, 260, 200);
CHECK(app->poll_events() && app->present(frame) == Step::rendered && !app->ui_wants_pointer());
mouse(SDL_EVENT_MOUSE_MOTION, 60, 30);
mouse(SDL_EVENT_MOUSE_BUTTON_DOWN, 60, 30);
mouse(SDL_EVENT_MOUSE_BUTTON_UP, 60, 30);
CHECK(app->poll_events() && !app->take_navigation().click);
for (int i = 0; i < 5 && presses == 1; ++i)
CHECK(app->present(frame) == Step::rendered && app->poll_events());
CHECK(presses == 2 && !app->take_navigation().click);
// Same for the wheel: moving onto the panel and scrolling never zooms the world.
mouse(SDL_EVENT_MOUSE_MOTION, 260, 200);
CHECK(app->poll_events() && app->present(frame) == Step::rendered);
SDL_Event wheel{};
wheel.type = SDL_EVENT_MOUSE_WHEEL;
wheel.wheel.windowID = id;
wheel.wheel.y = 3;
wheel.wheel.mouse_x = 60;
wheel.wheel.mouse_y = 30;
mouse(SDL_EVENT_MOUSE_MOTION, 60, 30);
CHECK(SDL_PushEvent(&wheel));
CHECK(app->poll_events() && app->take_navigation().wheel == 0);
CHECK(app->present(frame) == Step::rendered);
// ...while the same wheel over the world still zooms.
wheel.wheel.mouse_x = 260;
wheel.wheel.mouse_y = 200;
mouse(SDL_EVENT_MOUSE_MOTION, 260, 200);
CHECK(SDL_PushEvent(&wheel));
CHECK(app->poll_events() && app->take_navigation().wheel == 3);
CHECK(app->present(frame) == Step::rendered);
auto key = [id](SDL_Scancode scancode, bool down, bool repeat = false) {
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); // US fixture.
event.key.down = down;
event.key.repeat = repeat;
CHECK(SDL_PushEvent(&event));
};
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && required(app->sample_intention(100)).move_y == 1000);
// A focused text field takes the keyboard: held movement stops, typing never moves.
text_focus = true;
// Focus requests take effect over the next toolkit frames; wait a bounded number.
for (int i = 0; i < 5 && !app->ui_captured(); ++i)
CHECK(app->present(frame) == Step::rendered && app->poll_events());
CHECK(app->ui_captured());
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && app->ui_captured());
CHECK(required(app->sample_intention(101)).move_y == 0);
// Focus leaves the field: capture releases, but only a fresh press moves again.
text_focus = false; // The field is no longer submitted, so it loses focus.
for (int i = 0; i < 5 && app->ui_captured(); ++i)
CHECK(app->present(frame) == Step::rendered && app->poll_events());
CHECK(!app->ui_captured());
CHECK(required(app->sample_intention(102)).move_y == 0);
CHECK(app->poll_events());
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && required(app->sample_intention(103)).move_y == 1000);
key(SDL_SCANCODE_W, false);
CHECK(app->poll_events() && required(app->sample_intention(104)).move_y == 0);
// Click-to-focus and typing in one batch: the field is not yet reported focused,
// yet keys after a press on the panel never move the world.
const float fx = (field_min.x + field_max.x) / 2, fy = (field_min.y + field_max.y) / 2;
mouse(SDL_EVENT_MOUSE_MOTION, fx, fy);
mouse(SDL_EVENT_MOUSE_BUTTON_DOWN, fx, fy);
mouse(SDL_EVENT_MOUSE_BUTTON_UP, fx, fy);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && !app->take_navigation().click);
CHECK(required(app->sample_intention(105)).move_y == 0);
for (int i = 0; i < 5 && !app->ui_captured(); ++i)
CHECK(app->present(frame) == Step::rendered && app->poll_events());
CHECK(app->ui_captured() && required(app->sample_intention(106)).move_y == 0);
// A world click unfocuses the field; capture releases and fresh presses move again.
mouse(SDL_EVENT_MOUSE_MOTION, 260, 200);
mouse(SDL_EVENT_MOUSE_BUTTON_DOWN, 260, 200);
mouse(SDL_EVENT_MOUSE_BUTTON_UP, 260, 200);
CHECK(app->poll_events());
for (int i = 0; i < 5 && app->ui_captured(); ++i)
CHECK(app->present(frame) == Step::rendered && app->poll_events());
CHECK(!app->ui_captured());
static_cast<void>(app->take_navigation());
CHECK(app->poll_events());
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && required(app->sample_intention(107)).move_y == 1000);
key(SDL_SCANCODE_W, false);
CHECK(app->poll_events());
// Host order (main loop): poll -> set_ui_capture(false) outside modals -> sample ->
// present. The idempotent external release must not cancel the Tab toggle.
const auto host_frame = [&](std::uint64_t tick) {
CHECK(app->poll_events());
app->set_ui_capture(false);
const auto intention = required(app->sample_intention(tick));
CHECK(app->present(frame) == Step::rendered);
return intention;
};
key(SDL_SCANCODE_TAB, true);
key(SDL_SCANCODE_TAB, false);
CHECK(host_frame(110).move_y == 0 && app->ui_captured());
for (std::uint64_t tick = 111; tick < 115; ++tick) {
key(SDL_SCANCODE_W, true);
CHECK(host_frame(tick).move_y == 0 && app->ui_captured());
}
key(SDL_SCANCODE_TAB, true); // Second deliberate toggle; W stays held physically.
CHECK(host_frame(115).move_y == 0 && !app->ui_captured());
key(SDL_SCANCODE_W, true, true); // A repeat is not a fresh press.
CHECK(host_frame(116).move_y == 0);
key(SDL_SCANCODE_W, true);
CHECK(host_frame(117).move_y == 1000);
// Screenshot hook writes the composed frame (engineering visual review).
const auto shot = std::filesystem::temp_directory_path() / "evo-toolkit-capture.bmp";
CHECK(app->request_screenshot(shot.string()) && app->present(frame) == Step::rendered);
CHECK(std::filesystem::file_size(shot) > std::uintmax_t{320} * 240);
std::filesystem::remove(shot);
CHECK(!app->request_screenshot(""));
}
// M9.8d: actions come from the binding map; rebinding releases held actions; binding
// capture consumes the key before the toolkit and world; focus loss cancels it.
void rebinding() {
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);
CHECK(app->poll_events() && app->bindings() == Bindings::defaults());
auto key = [id](SDL_Scancode scancode, bool down, bool repeat = false) {
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); // US fixture.
event.key.down = down;
event.key.repeat = repeat;
CHECK(SDL_PushEvent(&event));
};
std::uint64_t tick = 200;
const auto move_y = [&] { return required(app->sample_intention(++tick)).move_y; };
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && move_y() == 1000);
// Rebind while W is held: the action is released at once and nothing queued before
// the change (including a press of the new key) replays.
auto slots = Bindings::defaults().slots();
slots[static_cast<std::size_t>(Action::move_up)] = {SDL_SCANCODE_I, SDL_SCANCODE_KP_8};
slots[static_cast<std::size_t>(Action::toggle_capture)] = {SDL_SCANCODE_F1, no_key};
const auto custom = Bindings::make(slots);
CHECK(custom);
key(SDL_SCANCODE_I, true); // Queued before the change.
app->set_bindings(*custom);
CHECK(move_y() == 0 && app->bindings() == *custom);
CHECK(app->poll_events() && move_y() == 0); // Drained batch.
key(SDL_SCANCODE_W, true); // No longer bound.
CHECK(app->poll_events() && move_y() == 0);
key(SDL_SCANCODE_I, true);
CHECK(app->poll_events() && move_y() == 1000);
// Two keys for one action: releasing either releases the action (never stuck).
key(SDL_SCANCODE_KP_8, true);
key(SDL_SCANCODE_KP_8, false);
CHECK(app->poll_events() && move_y() == 0);
key(SDL_SCANCODE_KP_8, true);
CHECK(app->poll_events() && move_y() == 1000);
key(SDL_SCANCODE_KP_8, false);
key(SDL_SCANCODE_I, false);
CHECK(app->poll_events() && move_y() == 0);
// Tab is unbound now; F1 toggles capture.
key(SDL_SCANCODE_TAB, true);
CHECK(app->poll_events() && !app->ui_captured());
key(SDL_SCANCODE_F1, true);
key(SDL_SCANCODE_I, true);
CHECK(app->poll_events() && app->ui_captured() && move_y() == 0);
key(SDL_SCANCODE_F1, true);
CHECK(app->poll_events() && !app->ui_captured() && move_y() == 0);
key(SDL_SCANCODE_I, true);
CHECK(app->poll_events() && move_y() == 1000);
// Binding capture: world input is released and captured while armed.
app->begin_key_capture();
CHECK(app->key_capture_active() && app->ui_captured() && move_y() == 0);
key(SDL_SCANCODE_K, true, true); // A repeat is not a choice.
CHECK(app->poll_events() && app->key_capture_active());
SDL_Event text{};
text.type = SDL_EVENT_TEXT_INPUT;
text.text.windowID = id;
text.text.text = "k";
key(SDL_SCANCODE_K, true);
CHECK(SDL_PushEvent(&text));
key(SDL_SCANCODE_I, true); // Same batch after the choice: drained, no movement.
CHECK(app->poll_events() && !app->key_capture_active());
CHECK(app->take_captured_key() == KeyCode{SDL_SCANCODE_K} && !app->take_captured_key());
CHECK(!app->ui_captured() && move_y() == 0);
CHECK(app->present(frame) == Step::rendered);
CHECK(!ImGui::IsKeyDown(ImGuiKey_K)); // The toolkit never saw the chosen key.
key(SDL_SCANCODE_K, false);
key(SDL_SCANCODE_I, true);
CHECK(app->poll_events() && move_y() == 1000);
// Escape is captured too (the host treats it as cancel); bound keys do not act.
app->begin_key_capture();
key(SDL_SCANCODE_ESCAPE, true);
CHECK(app->poll_events() && app->take_captured_key() == escape_key && move_y() == 0);
// Focus loss cancels capture without a key; explicit cancel likewise.
app->begin_key_capture();
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
key(SDL_SCANCODE_J, true);
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();
app->cancel_key_capture();
key(SDL_SCANCODE_J, true);
CHECK(app->poll_events() && !app->key_capture_active() && !app->take_captured_key());
// Restoring defaults restores W after a fresh press.
app->set_bindings(Bindings::defaults());
CHECK(app->poll_events());
key(SDL_SCANCODE_W, true);
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);
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.
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();
frame_contract();
lifecycle();
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());
return 1;
} catch (...) {
return 1;
}