Add M9.8d rebindable engineering controls

SDL-free action/HID-key bindings with conflict validation, a staging editor and
a versioned all-or-nothing controls file (ADR-0085). The adapter maps keys
through the bindings, releases and drains on rebinding, and captures the chosen
key ahead of the toolkit and world. The client adds a Controls panel, key hints,
startup loading with a bounded rejection notice, and --controls/--show-controls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-02 11:30:17 -04:00
co-authored by Claude Opus 5.5
parent bb8c2fc359
commit 5cf8709912
23 changed files with 1292 additions and 45 deletions
+101
View File
@@ -436,6 +436,106 @@ void toolkit_capture() {
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.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);
}
} // namespace
int main() try {
client_controls_tests();
@@ -443,6 +543,7 @@ int main() try {
lifecycle();
native_input();
toolkit_capture();
rebinding();
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s: %s\n", e.what(), SDL_GetError());