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
+186
View File
@@ -0,0 +1,186 @@
// M9.8d rebindable controls (ADR-0085): SDL-free binding contract, editor and file.
#include "evo/client/bindings.hpp"
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
using namespace evo::client;
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("client bindings line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
KeyCode key(std::string_view name) {
const auto k = key_from_name(name);
if (!k)
throw std::runtime_error("client bindings: unknown key name");
return *k;
}
bool refused(const auto& result, BindingError error) {
return !result && result.error().error == error;
}
void names_and_defaults() {
const auto d = Bindings::defaults();
CHECK(d.action(key("W")) == Action::move_up && d.action(key("A")) == Action::move_left);
CHECK(d.action(key("Up")) == Action::pan_up && d.action(key("Tab")) == Action::toggle_capture);
CHECK(!d.action(key("Q")) && !d.action(no_key));
CHECK(key_name(26) == "W" && key_name(82) == "Up" && key_name(0).empty() &&
key_name(224).empty());
CHECK(!bindable(escape_key) && !bindable(no_key) && !bindable(224) && !bindable(57));
CHECK(bindable(key("Keypad5")) && bindable(key("F12")));
CHECK(!key_from_name("w") && !key_from_name("") && !key_from_name("LeftShift"));
for (std::size_t k = 0; k < action_count; ++k) {
const auto a = static_cast<Action>(k);
CHECK(action_from_id(action_id(a)) == a && !action_name(a).empty());
}
CHECK(!action_from_id("jump"));
// Defaults are themselves valid and round-trip through make().
const auto again = Bindings::make(d.slots());
CHECK(again && *again == d);
}
void validation() {
auto slots = Bindings::defaults().slots();
slots[2].secondary = key("I");
CHECK(Bindings::make(slots));
auto conflict = slots;
conflict[0].secondary = key("W"); // Move up's key.
const auto c = Bindings::make(conflict);
CHECK(refused(c, BindingError::conflict) && c.error().other == Action::move_left &&
c.error().action == Action::move_up && c.error().key == key("W"));
CHECK(describe(c.error()) == "W is already bound to Move left.");
auto same = slots;
same[3].secondary = same[3].primary;
CHECK(refused(Bindings::make(same), BindingError::conflict));
auto missing = slots;
missing[8].primary = no_key;
const auto m = Bindings::make(missing);
CHECK(refused(m, BindingError::missing_primary) && m.error().action == Action::toggle_capture);
auto reserved = slots;
reserved[1].primary = escape_key;
CHECK(refused(Bindings::make(reserved), BindingError::unbindable_key));
auto modifier = slots;
modifier[1].secondary = 225; // Left Shift.
CHECK(refused(Bindings::make(modifier), BindingError::unbindable_key));
}
void editor() {
BindingEditor e{Bindings::defaults()};
// A conflicting assignment is refused and leaves the draft unchanged.
const auto before = e.draft();
const auto refused_assign = e.assign(Action::move_up, false, key("Left"));
CHECK(!refused_assign && refused_assign.error().other == Action::pan_left);
CHECK(e.draft() == before);
CHECK(!e.assign(Action::move_up, true, escape_key));
CHECK(!e.assign(Action::move_up, true, key("W"))); // Its own primary.
// Reassigning a slot to its current key is allowed (no-op).
CHECK(e.assign(Action::move_up, false, key("W")));
CHECK(e.assign(Action::move_up, false, key("I")) && e.assign(Action::move_up, true, key("W")));
auto applied = e.apply();
CHECK(applied && applied->action(key("I")) == Action::move_up &&
applied->action(key("W")) == Action::move_up);
e.clear_secondary(Action::move_up);
CHECK(e.draft()[2].secondary == no_key);
e.reset_defaults();
CHECK(e.apply() == Bindings::defaults());
}
void file_format() {
auto slots = Bindings::defaults().slots();
slots[8] = {key("F1"), key("Keypad0")};
const auto custom = Bindings::make(slots);
CHECK(custom);
const auto text = serialize(*custom);
CHECK(text.starts_with("evo-controls 1\n") &&
text.find("toggle_capture F1 Keypad0\n") != std::string::npos);
CHECK(parse_bindings(text) == *custom);
// Comments, blank lines, CRLF and tabs are tolerated; order is free.
std::string loose = "# mine\r\nevo-controls 1\r\n\r\n";
for (std::size_t k = action_count; k-- > 0;) {
const auto a = static_cast<Action>(k);
loose += " " + std::string{action_id(a)} + "\t" + std::string{key_name(slots[k].primary)};
if (slots[k].secondary != no_key)
loose += " " + std::string{key_name(slots[k].secondary)};
loose += " # note\r\n";
}
CHECK(parse_bindings(loose) == *custom);
// Malformed inputs are rejected as a whole.
const auto without = [&](std::string_view action) {
std::string out;
std::size_t at = 0;
while (at < text.size()) {
const auto end = text.find('\n', at);
const auto line = text.substr(at, end - at + 1);
if (!line.starts_with(std::string{action} + " "))
out += line;
at = end + 1;
}
return out;
};
CHECK(refused(parse_bindings(""), BindingError::malformed));
CHECK(refused(parse_bindings("move_up W\n"), BindingError::malformed));
CHECK(refused(parse_bindings("evo-controls 2\n"), BindingError::unsupported_version));
CHECK(refused(parse_bindings("evo-controls 1 extra\n"), BindingError::malformed));
CHECK(refused(parse_bindings(without("pan_down")), BindingError::missing_primary));
CHECK(refused(parse_bindings(text + "move_up W\n"), BindingError::malformed));
CHECK(refused(parse_bindings(text + "jump Space\n"), BindingError::malformed));
CHECK(refused(parse_bindings(without("move_up") + "move_up w\n"), BindingError::malformed));
CHECK(refused(parse_bindings(without("move_up") + "move_up W I O\n"), BindingError::malformed));
CHECK(refused(parse_bindings(without("move_up") + "move_up\n"), BindingError::malformed));
CHECK(refused(parse_bindings(without("move_up") + "move_up A\n"), BindingError::conflict));
CHECK(refused(parse_bindings(without("move_up") + "move_up Escape\n"),
BindingError::unbindable_key));
CHECK(refused(parse_bindings(text + std::string{"\0", 1}), BindingError::malformed));
CHECK(refused(parse_bindings(text + "# \xC3\xA9\n"), BindingError::malformed));
CHECK(
refused(parse_bindings(std::string(max_bindings_bytes + 1, ' ')), BindingError::too_large));
CHECK(parse_bindings(text + std::string(max_bindings_bytes - text.size(), '\n')));
}
void files() {
const auto dir = std::filesystem::temp_directory_path() / "evo-bindings-test";
std::filesystem::remove_all(dir);
std::filesystem::create_directories(dir);
const auto file = dir / "controls-v1.txt";
CHECK(refused(load_bindings(file), BindingError::unreadable));
CHECK(refused(load_bindings(dir), BindingError::unreadable));
auto slots = Bindings::defaults().slots();
slots[0].secondary = key("J");
const auto custom = Bindings::make(slots);
CHECK(custom && save_bindings(*custom, file));
CHECK(load_bindings(file) == *custom && !std::filesystem::exists(dir / "controls-v1.txt.tmp"));
// Saving again replaces the file.
CHECK(save_bindings(Bindings::defaults(), file) && load_bindings(file) == Bindings::defaults());
{
std::ofstream big{file, std::ios::binary | std::ios::trunc};
big << std::string(max_bindings_bytes + 1, '#');
}
CHECK(refused(load_bindings(file), BindingError::too_large));
{
std::ofstream bad{file, std::ios::binary | std::ios::trunc};
bad << "evo-controls 1\nmove_up W\n";
}
CHECK(refused(load_bindings(file), BindingError::missing_primary));
CHECK(refused(save_bindings(Bindings::defaults(), dir / "missing" / "controls-v1.txt"),
BindingError::write_failed));
std::filesystem::remove_all(dir);
}
} // namespace
int main() try {
names_and_defaults();
validation();
editor();
file_format();
files();
std::puts("client binding tests passed");
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}