Event Types
ZREV batches contain these event record types:| Record | Description | Fields |
|---|---|---|
| Key | Keyboard input with key code and modifiers | keyCode, mods, text |
| Mouse | Mouse events (move, drag, down, up, wheel) | x, y, mouseKind, buttons, wheelX, wheelY, mods |
| Resize | Terminal size change | cols, rows |
| Tick | Animation timer | deltaNs |
Binary Format
Header Structure
Every ZREV buffer begins with a 24-byte header. All fields are little-endian.| Offset | Size | Type | Field | Description |
|---|---|---|---|---|
| 0 | 4 | u32 | magic | 0x5645525A (ASCII ZREV as LE u32) |
| 4 | 4 | u32 | version | Format version (currently 1) |
| 8 | 4 | u32 | header_size | Always 24 |
| 12 | 4 | u32 | total_size | Total byte length of entire buffer |
| 16 | 4 | u32 | record_count | Number of event records |
| 20 | 4 | u32 | reserved0 | Must be 0 |
Event Record Structure
After the header, event records are laid out contiguously. Each record is self-framed with a record header:| Offset | Size | Type | Field |
|---|---|---|---|
| 0 | 1 | u8 | kind (1=key, 2=text, 3=paste, 4=mouse, 5=resize, 6=tick) |
| 1 | 1 | u8 | flags (reserved, must be 0) |
| 2 | 2 | u16 | size (total bytes including this header) |
Key Events
Kind:1Total size: Variable (minimum 12 bytes)
| Offset | Size | Type | Field |
|---|---|---|---|
| 0 | 1 | u8 | kind = 1 |
| 1 | 1 | u8 | flags = 0 |
| 2 | 2 | u16 | size |
| 4 | 4 | u32 | keyCode (ZR_KEY_* constant) |
| 8 | 4 | u32 | mods (bitmask of ZR_MOD_*) |
| 12+ | variable | UTF-8 | text (optional, when printable) |
Key Codes
Special keys:| Key | Code |
|---|---|
ZR_KEY_ESCAPE | 1 |
ZR_KEY_ENTER | 2 |
ZR_KEY_TAB | 3 |
ZR_KEY_BACKSPACE | 4 |
ZR_KEY_INSERT | 10 |
ZR_KEY_DELETE | 11 |
ZR_KEY_HOME | 12 |
ZR_KEY_END | 13 |
ZR_KEY_PAGE_UP | 14 |
ZR_KEY_PAGE_DOWN | 15 |
| Key | Code |
|---|---|
ZR_KEY_UP | 20 |
ZR_KEY_DOWN | 21 |
ZR_KEY_LEFT | 22 |
ZR_KEY_RIGHT | 23 |
ZR_KEY_F1 (100) through ZR_KEY_F12 (111)
Printable keys: Use ASCII codepoints (32-126)
Modifier Bitmask
Mouse Events
Kind:4Total size: 28 bytes
| Offset | Size | Type | Field |
|---|---|---|---|
| 0 | 1 | u8 | kind = 4 |
| 1 | 1 | u8 | flags = 0 |
| 2 | 2 | u16 | size = 28 |
| 4 | 4 | i32 | x (column, 0-based) |
| 8 | 4 | i32 | y (row, 0-based) |
| 12 | 4 | u32 | mouseKind (1=move, 2=drag, 3=down, 4=up, 5=wheel) |
| 16 | 4 | u32 | mods (modifier bitmask) |
| 20 | 4 | u32 | buttons (button state bitmask) |
| 24 | 2 | i16 | wheelX (horizontal scroll delta) |
| 26 | 2 | i16 | wheelY (vertical scroll delta) |
Mouse Kind Values
| Kind | Value | Description |
|---|---|---|
move | 1 | Mouse moved without button pressed |
drag | 2 | Mouse moved with button pressed |
down | 3 | Button pressed |
up | 4 | Button released |
wheel | 5 | Scroll wheel moved |
Button Bitmask
Resize Events
Kind:5Total size: 12 bytes
| Offset | Size | Type | Field |
|---|---|---|---|
| 0 | 1 | u8 | kind = 5 |
| 1 | 1 | u8 | flags = 0 |
| 2 | 2 | u16 | size = 12 |
| 4 | 4 | u32 | cols (new column count) |
| 8 | 4 | u32 | rows (new row count) |
Tick Events
Kind:6Total size: 12 bytes
| Offset | Size | Type | Field |
|---|---|---|---|
| 0 | 1 | u8 | kind = 6 |
| 1 | 1 | u8 | flags = 0 |
| 2 | 2 | u16 | size = 12 |
| 4 | 8 | i64 | deltaNs (nanoseconds since last tick) |
Parsing
Rezi parses ZREV deterministically:- No reads past buffer end
- No unbounded allocations from untrusted sizes
- Explicit, structured errors (no exceptions into user code)
packages/core/src/events.ts
Event Caps
Event batches have configurable size limits:- Engine enforces cap when building ZREV batches
- Events exceeding cap are dropped (with warning)
- Prevents unbounded memory allocation
Validation Rules
The parser enforces these constraints:magicmust be0x5645525A(ZREV)versionmust be1total_sizemust be >=header_size(24)record_countmust be >= 0- Each record’s
kindmust be valid (1-6) - Each record’s
sizemust be >= 4 (minimum record header) - Sum of all record sizes must not exceed buffer bounds
Related Documentation
- Protocol Overview — Binary protocol principles
- Event System — Event routing and dispatch
- Safety Rules — Validation patterns
- Mouse Support — Mouse interaction patterns