|
Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
|
Bind a key to an action with ui::onKey. This example displays help on F1; F10 closes the application. The runner reads and decodes terminal input automatically.
Use "Ctrl+S" for a chord and {key::F10, key::ALT + key::Q} for alternatives. Callbacks accept PRESS by default. Ordinary VT input reports PRESS, including repeated bytes from a held key; RELEASE and distinct REPEAT require an adapter that supplies them. See routing and focus.
Selecting shortcut phases
<portals/ui.hpp> provides owning shortcut selections for the existing onKey registration path. Plain string/chord/alternative bindings admit PRESS only:
ui::press(source) selects exact PRESS. All helpers accept text, a typed chord, an owning Shortcut, or an initializer list of alternative typed chords. ui::keyActions(source, actions) selects any nonempty subset of PRESS, REPEAT and RELEASE without more helper names. Alternatives and duplicate actions invoke at most once per event. Invalid/empty action sets report INVALID_ACTION_SET, with the offending action index (zero for empty). Normalization errors and allocation failures retain their existing typed errors. Parser budgets stay configurable.
Helpers normalize before returning and retain immutable owning alternatives; temporary strings and lists may expire immediately. Copying a KeySelection shares immutable values; each registration owns its callback. Normalization runs once at setup, never in callbacks. Diagnostic source location identifies the onKey registration, independently of helper construction; parser/action offsets identify the bad input. Newest matching registration wins, with exact modifiers and the existing ASCII letter case policy. Callbacks may accept no argument or const KeyEvent&, using the existing checked result adapters.
ShortcutBehavior{.repeat = true} remains the legacy PRESS+REPEAT adapter for ordinary shortcut sources. Explicit selections have no overload accepting that adapter; their phase set is complete. The raw KeyEvent overload still admits PRESS and REPEAT. Text input represents PRESS only, so cannot trigger repeat-only or release-only selections.
Selections do not add backend capabilities: the VT decoder emits PRESS, including repeated bytes, and does not synthesize releases. A native adapter must supply real REPEAT/RELEASE values. Held-key state, focus-loss release and Kitty negotiation are outside this API.
Managed multi-stroke commands
ui::onKey("Ctrl+K+K", action) registers Ctrl+K followed by Ctrl+K; "Ctrl+K,K" requires an unmodified second K. Plus-separated keys share all semantic modifiers of their segment. Comma starts independent modifiers. The owning Shortcut::sequences collection supplements the existing single alternatives; both share the configured alternative budget. The stateless matchShortcut predicate continues to check single chords only.
Phase selectors apply to every stroke: ui::release("A,B") waits for two genuine RELEASE events. Intermediate PRESS events are ignored by its pending matcher and take the normal input path once. No RELEASE is synthesized. Mixed candidates are filtered only when an admitted continuation matches. An unmatched PRESS cancels pending input only if a pending candidate admits PRESS; unmatched REPEAT/RELEASE preserve it. A root prefix started by a passed-through event replaces the prior pending group.
The newest matching registration decides terminal versus continuation at each depth, preserving legacy and selected-source ordering. Suppressed terminal commands never execute on timeout or mismatch. Unmodified Escape PRESS cancels and is consumed while pending; this chord is forbidden after the first stroke. Proper-prefix alternatives within one registration are rejected with AMBIGUOUS_PREFIX; reserved Escape produces RESERVED_CANCEL with a byte offset.
The existing Scheduler owns one weak, scoped timeout. The positive ManagedOptions::keySequenceTimeout defaults to 1000 ms and restarts only on accepted nonterminal strokes. now >= deadline clears pending state before continuation, without a delayed command or replay. Focus changes and effective visibility/enabled/removal mutations invalidate pending input, even when state changes away and back. Tree invalidation conservatively includes other nodes; paint/layout alone do not cancel commands. Details and examples are in the declarative UI guide.
InputDecoder serves custom terminal adapters. It synchronously converts bytes to events; the caller supplies time, enables terminal modes, bounds its event queue and services timeouts. Applications using ui::runApp() do not need this loop.
Pulling events
InputDecoder::create(options) validates settings and returns an owning decoder or InputError::INVALID_OPTIONS. One executor owns its mutable state; different instances can run concurrently. Do not call one instance concurrently. A moved source must be reset before reuse. Returned events own their values and survive decoder reset, move and destruction.
next(bytes, now) borrows bytes for that call and returns at most one event plus an exact consumed prefix. The caller advances by consumed, handles the event, and resubmits the suffix with its original arrival timestamp. A result can have an event and consume zero bytes: timeout, pending issue, double ESC and malformed UTF-8 recovery need this. NEED_INPUT means the supplied view is exhausted; WORK_LIMIT means another pull is required even if no event was produced. There is no growing output queue. The caller must bound any queue it creates.
Empty next calls advance time. Drain events before waiting; after NEED_INPUT, wait for bytes or deadline(). finish(now) starts EOF and drains at most one event per call. Repeat until finished or failed; a finished result may still carry the last event. Further finish calls are empty and idempotent, while next returns CLOSED. No input is accepted after EOF. reset() abandons the old session, frees retained paste and clears the clock, byte offsets and probe window, retaining options. It is a caller-declared boundary, not a safe way to continue an unclosed paste's untrusted suffix.
Call errors reject the entire supplied view. Backward timestamps are rejected before state changes. Offset overflow is checked for the maximum consumable prefix before a call. Allocation failures propagate and leave the decoder failed; after an exception the caller must abandon that session, because a consumed count cannot be returned. No partially collected paste event escapes.
Events and the supported profile
Text events carry a single validated Unicode scalar, not a grapheme cluster. Combining marks, ZWJ and regional indicators can arrive as separate events. The text layer groups them after insertion. Alt is represented as an observed modifier on text, not an invented physical scan code. No normalization, replacement character insertion, display-width calculation or text filtering occurs in this decoder. UTF-8-encoded control scalars remain data. ESC followed by [, O, ], P, X, ^ or _ is reserved for the corresponding sequence/string introducer, so these prefixes do not produce Alt text. This is another unavoidable convention of the selected legacy profile.
Key values include modifiers and press/repeat/release for native adapters, but this VT profile emits only press. It cannot establish physical key state. Mouse events preserve button, motion/release/wheel and modifiers; their coordinates are checked zero-based cells without viewport clipping.
The exact supported keyboard table is:
| Encoding | Event |
|---|---|
| ASCII 0x20–0x7e, valid UTF-8 | Text scalar |
| ESC followed by one scalar before deadline | Text scalar with Alt |
| Tab (0x09), CR (0x0d), BS/DEL (0x08/0x7f) | Tab, Enter, Backspace |
| Remaining C0 except ESC | Ctrl plus scalar byte+0x40 (NUL is Ctrl+@; LF is Ctrl+J) |
| ESC followed by C0/DEL | Corresponding key plus Alt |
| Lone ESC; consecutive ESC | Escape after deadline/EOF; first Escape immediately |
| CSI/SS3 A B C D H F | Up, Down, Right, Left, Home, End |
| CSI/SS3 P Q R S | F1–F4 |
| CSI n~ with n=1/7, 2, 3, 4/8, 5, 6 | Home, Insert, Delete, End, PageUp, PageDown |
| CSI n~ with n=11,12,13,14,15,17,18,19,20,21,23,24 | F1–F12 |
| CSI Z | Shift+Tab |
Letter-final CSI/SS3 keys accept no parameters, 1, or 1;m. Tilde keys accept n or n;m. Modifier m is 1–8: subtract one, then bits 1/2/4 mean Shift/Alt/Control. Missing or empty numeric fields are rejected. This is a small chosen profile, not the entire xterm keyboard grammar.
With sgr_mouse=true, CSI <b;x;y M/m decodes SGR cell mouse reports. Coordinates must be 1..2147483648, then subtract one into signed 32-bit fields. Button bits 0..1 identify left/middle/right/no-button; bits 4/8/16 are modifiers, 32 is motion, 64 is wheel. Wheel directions 64..67 are up/down/left/right. The decoder rejects unsupported bits, wheel+motion, wheel release, motion release and no-button transitions other than motion. Extended buttons and pixel coordinates are outside this profile. Raw 8-bit CSI, X10/1005/1015, Kitty keyboard and OSC/DCS replies are not supported.
Time and ambiguity
All times are caller-supplied unsigned 64-bit monotonic milliseconds. Defaults are 30 ms for ESC/Alt completion, 1000 ms for an entire CSI/SS3/control string or ordinary UTF-8 scalar, and 30000 ms for paste from its complete begin delimiter. These are configurable policy choices, not measured terminal guarantees. CSI, SS3 and strings use the initial ESC timestamp; incoming bytes never extend a deadline. Addition saturates at UINT64_MAX, and now >= deadline expires before processing further bytes, including a newly formed saturated deadline.
An ESC followed by an incomplete Alt scalar emits Escape then a truncated issue at timeout or EOF. The scalar's subsequent bytes are independently decoded. Ordinary truncated UTF-8 emits an issue and returns to ground. Unclosed control sequences, discarded sequences, strings and paste fail on timeout: automatic recovery could expose a data suffix as keyboard commands. Only explicit reset recovers a failed decoder.
The invariant is event equality for identical byte/timestamp schedules, EOF and probe-window changes. Subdividing an arrival batch retains its timestamp. Introducing new time advances or delaying bytes can legitimately change ESC interpretation. Legacy Ctrl-I/Tab, Ctrl-M/Enter, Ctrl-H/Backspace, Ctrl-[/Escape and ESC+character/Alt+character cannot be distinguished from their encodings.
Probe candidates
expectReplies installs an explicit inclusive-start/exclusive-deadline window, generation and one allowed response of each selected kind. It is valid only at ground with no pending event. The parser sends no queries. Primary DA is CSI ?p[;p...]c; CPR is CSI row;column R with positive fields. Both use checked 32-bit parameters. The initial ESC and sequence completion must lie in the window. A matching candidate consumes that kind's expectation once. Generation only correlates with the caller's request; it cannot authenticate the sender.
CPR overlaps modified F3. Within an expected window CPR wins; otherwise the documented key table wins (for example CSI 1;2R is Shift+F3), and remaining unsupported sequences emit issues. Typed, duplicate, stale or forged bytes are not distinguishable from genuine terminal responses by this syntax. The diagnostic tool's probe limitations apply to interpreting observations, but its collector is not used by this library decoder.
Bounds and recovery
Defaults cap CSI/SS3 at 64 bytes including ESC/introducer/final, at 16 numeric parameters and 10 decimal digits per parameter. These settings can be lowered; fixed scratch sizes set their maximums. Arithmetic is checked independently of digit count. A paste payload defaults to 1 MiB; zero permits only empty paste. Each pull accepts at most 4096 bytes by default. Work is bounded by that budget plus fixed grammar scratch; no callback can add unbounded work inside a pull.
Retained logical data is bounded by the paste cap plus at most five tentative end-marker bytes and fixed 64-byte sequence scratch. std::string allocation capacity/allocator overhead and a returned owning event are additional; space complexity is O(paste cap), with no full-payload copy on delivery. Output events are the caller's storage obligation. String and discarded-paste bodies are scanned without retaining them. One pending issue is used only when an Alt tail must produce Escape and an issue separately. A byte budget is not a total-stream time bound; the caller must schedule time advances.
Malformed UTF-8 rejects the partial scalar; a non-continuation byte is left unconsumed and reprocessed once at ground. Invalid leading bytes are consumed. Malformed control grammar consumes its offending byte, emits an issue and returns to ground; a parameter after an intermediate enters bounded discard through the final byte. Sequence overflow emits one issue at the first excess byte and discards through the final, or through a consumed invalid grammar byte. Unsupported complete sequences emit an issue and are never replayed as keys.
Unknown OSC/DCS/SOS/PM/APC strings emit an issue at the introducer, then discard through ST (ESC \\); OSC also accepts BEL. Their contents never enter key, mouse or probe decoding. Timeout fails the stream. EOF emits a truncated issue for incomplete state and closes it. Issues contain kind and a half-open session byte range for the consumed rejected prefix, with no raw ANSI excerpt. Subsequent discard bytes are represented by consumed counts, not an ever-growing diagnostic.
Paste boundary
With bracketed_paste=true, CSI 200~ begins paste and exact CSI 201~ ends it. Only the end delimiter has meaning inside paste. Nested begin delimiters, key, mouse, DA/CPR, OSC and arbitrary invalid UTF-8/NUL remain raw bytes. Partial end-marker prefixes are matched across every read boundary.
The first confirmed payload byte over the cap drops the whole payload and emits one limit issue. The decoder then discards through the exact end delimiter; there is no partial paste event and no reinterpretation of its suffix as keys. The original paste deadline remains active while discarding. A prefix that might still be the end marker does not count as confirmed payload until mismatched.
Literal ESC[201~ in untrusted data is indistinguishable from a genuine end: the protocol has no length or authentication. The regression corpus explicitly demonstrates that a forged delimiter ends paste. This parser therefore does not promise protection from a hostile terminal stream. Downstream insertion policy must validate/repair text and treat paste controls as data; never forward raw paste to a terminal writer, shell or command dispatcher.
Ordered protocol reports and pointer context
InputOptions::terminal_reports defaults to false. When enabled, CSI 48 owns four required dimensions in InputSizeReport; zero pixels remain a valid unavailable-size report. Only this grammar ignores colon subparameters, including empty slots, within the existing parameter/digit/byte limits. DEC private DECRPM owns mode and state 0..4 in InputModeReport. These reports use trailing InputStep::protocol and InputStatus::PROTOCOL, independently of DA/CPR probe windows. EVENT still contains an event; event and protocol are mutually exclusive. Handle either result before resubmitting the suffix, including at the exact work-byte boundary.
InputDecoder::setPointerContext affects subsequently started CSI sequences; a partial CSI retains its start context. It preserves partial UTF-8 and paste. Context values and optional MouseInputEvent::precision own all dimensions and generation. A raw pixel context with unavailable dimensions suppresses unsafe mouse samples while allowing keys. A transport owner must establish a real stream boundary and implement bounded fallback; setting a decoder flag cannot establish terminal ordering. A rejected recognized CSI 48 marks InputIssue::invalidates_pointer_metrics; the runner invalidates stale dimensions and requests bounded fallback. The application runner orders context cancellation through CONTROL before later mouse input. Custom transports can publish proven TerminalRead::coordinate_mode metadata before a batch and handle reports through the default no-op protocol hooks; hooks request work without blocking I/O. See native policy.
Existing five-field mouse and three-field step aggregates retain their trailing defaults. Rebuild clients: the mouse payload, InputStep, TerminalRead and terminal virtual interfaces changed. Unchanged sizeof(InputEvent) is not binary compatibility; structured bindings over extended aggregates must be updated.
Primary references: xterm control sequences (Control Bytes, SGR mouse, Bracketed Paste, Device Attributes and DSR), Microsoft input sequences, and Unicode UTF-8 definition, section 3.9.