#pragma once // M9.8d rebindable engineering controls (ADR-0085). Actions are separate from // physical keys; keys are USB HID keyboard usages (page 7), the numbering SDL // scancodes also use, so this contract stays SDL-free. Mouse selection/zoom are not // rebindable. The file stores key names only: never credentials, paths or grants. #include #include #include #include #include #include #include #include namespace evo::client { enum class Action : std::uint8_t { move_left, move_right, move_up, move_down, pan_left, pan_right, pan_up, pan_down, toggle_capture }; inline constexpr std::size_t action_count = 9; using KeyCode = std::uint16_t; // HID usage; 0 means unbound. inline constexpr KeyCode no_key = 0; inline constexpr KeyCode escape_key = 41; // Reserved: cancels binding capture. struct KeySlot { KeyCode primary{no_key}, secondary{no_key}; friend bool operator==(const KeySlot&, const KeySlot&) = default; }; using KeySlots = std::array; enum class BindingError : std::uint8_t { unbindable_key, // Not in the supported key table, or reserved (Escape). conflict, // One physical key bound to two actions or both slots. missing_primary, // Every action needs a primary key. malformed, // File syntax, unknown action/key name, duplicate action line. unsupported_version, too_large, unreadable, write_failed }; // Why a set of bindings was refused, for fixed-wording display. struct BindingProblem { BindingError error{BindingError::malformed}; std::optional action, other; KeyCode key{no_key}; }; [[nodiscard]] std::string_view action_id(Action action) noexcept; // "move_up" (file token) [[nodiscard]] std::string_view action_name(Action action) noexcept; // "Move up" (label) [[nodiscard]] std::optional action_from_id(std::string_view id) noexcept; // Display/file name of a supported key ("W", "Up", "F5"); empty when unsupported. [[nodiscard]] std::string_view key_name(KeyCode key) noexcept; [[nodiscard]] std::optional key_from_name(std::string_view name) noexcept; [[nodiscard]] bool bindable(KeyCode key) noexcept; [[nodiscard]] std::string describe(const BindingProblem& problem); // An always-valid binding set: every action has a bindable primary, at most one // bindable secondary, and no key appears twice. class Bindings { public: [[nodiscard]] static Bindings defaults() noexcept; [[nodiscard]] static std::expected make(const KeySlots& slots); [[nodiscard]] std::optional action(KeyCode key) const noexcept; [[nodiscard]] const KeySlot& slot(Action action) const noexcept { return slots_[static_cast(action)]; } [[nodiscard]] const KeySlots& slots() const noexcept { return slots_; } friend bool operator==(const Bindings&, const Bindings&) = default; private: explicit Bindings(const KeySlots& slots) noexcept : slots_{slots} {} KeySlots slots_; }; // Staged edits for a controls panel. Nothing applies until apply() validates the // whole set; an assignment that would conflict is refused and leaves the draft as is. class BindingEditor { public: explicit BindingEditor(const Bindings& current) noexcept : draft_{current.slots()} {} [[nodiscard]] std::expected assign(Action action, bool secondary, KeyCode key); void clear_secondary(Action action) noexcept; void reset_defaults() noexcept; [[nodiscard]] const KeySlots& draft() const noexcept { return draft_; } [[nodiscard]] std::expected apply() const; private: KeySlots draft_; }; // Versioned text: "evo-controls 1", then one "action key [key]" line per action. // All actions must appear exactly once; '#' starts a comment. Rejected as a whole // on any error (never partially applied). inline constexpr std::size_t max_bindings_bytes = 4096; [[nodiscard]] std::string serialize(const Bindings& bindings); [[nodiscard]] std::expected parse_bindings(std::string_view text); [[nodiscard]] std::expected load_bindings(const std::filesystem::path& file); // Writes a sibling temporary file, then replaces `file`. [[nodiscard]] std::expected save_bindings(const Bindings& bindings, const std::filesystem::path& file); } // namespace evo::client