![]() |
libghostty
|
Represents the state required to render a visible screen (a viewport) of a terminal instance. This is stateful and optimized for repeated updates from a single terminal instance and only updating dirty regions of the screen.
The key design principle of this API is that it only needs read/write access to the terminal instance during the update call. This allows the render state to minimally impact terminal IO performance and also allows the renderer to be safely multi-threaded (as long as a lock is held during the update call to ensure exclusive access to the terminal instance).
The basic usage of this API is:
Dirty tracking is a key feature of the render state that allows renderers to efficiently determine what parts of the screen have changed and only redraw changed regions.
The render state API keeps track of dirty state at two independent layers: a global dirty state that indicates whether the entire frame is clean, partially dirty, or fully dirty, and a per-row dirty state that allows tracking which rows in a partially dirty frame have changed.
The user of the render state API is expected to unset both of these. The update call does not unset dirty state, it only updates it.
An extremely important detail: setting one dirty state doesn't unset the other. For example, setting the global dirty state to false does not reset the row-level dirty flags. So, the caller of the render state API must be careful to manage both layers of dirty state correctly.
Typedefs | |
| typedef struct GhosttyRenderState * | GhosttyRenderState |
| typedef struct GhosttyRenderStateRowIterator * | GhosttyRenderStateRowIterator |
| typedef struct GhosttyRenderStateRowCells * | GhosttyRenderStateRowCells |
Data Structures | |
| struct | GhosttyRenderStateColors |
| typedef struct GhosttyRenderState* GhosttyRenderState |
| typedef struct GhosttyRenderStateRowCells* GhosttyRenderStateRowCells |
| typedef struct GhosttyRenderStateRowIterator* GhosttyRenderStateRowIterator |
Visual style of the cursor.
Queryable data kinds for ghostty_render_state_get().
| Enumerator | |
|---|---|
| GHOSTTY_RENDER_STATE_DATA_INVALID | Invalid / sentinel value. |
| GHOSTTY_RENDER_STATE_DATA_COLS | Viewport width in cells (uint16_t). |
| GHOSTTY_RENDER_STATE_DATA_ROWS | Viewport height in cells (uint16_t). |
| GHOSTTY_RENDER_STATE_DATA_DIRTY | Current dirty state (GhosttyRenderStateDirty). |
| GHOSTTY_RENDER_STATE_DATA_ROW_ITERATOR | Populate a pre-allocated GhosttyRenderStateRowIterator with row data from the render state (GhosttyRenderStateRowIterator). Row data is only valid as long as the underlying render state is not updated. It is unsafe to use row data after updating the render state. |
| GHOSTTY_RENDER_STATE_DATA_COLOR_BACKGROUND | Default/current background color (GhosttyColorRgb). |
| GHOSTTY_RENDER_STATE_DATA_COLOR_FOREGROUND | Default/current foreground color (GhosttyColorRgb). |
| GHOSTTY_RENDER_STATE_DATA_COLOR_CURSOR | Cursor color when explicitly set by terminal state (GhosttyColorRgb). Returns GHOSTTY_INVALID_VALUE if no explicit cursor color is set; use COLOR_CURSOR_HAS_VALUE to check first. |
| GHOSTTY_RENDER_STATE_DATA_COLOR_CURSOR_HAS_VALUE | Whether an explicit cursor color is set (bool). |
| GHOSTTY_RENDER_STATE_DATA_COLOR_PALETTE | The active 256-color palette (GhosttyColorRgb[256]). |
| GHOSTTY_RENDER_STATE_DATA_CURSOR_VISUAL_STYLE | The visual style of the cursor (GhosttyRenderStateCursorVisualStyle). |
| GHOSTTY_RENDER_STATE_DATA_CURSOR_VISIBLE | Whether the cursor is visible based on terminal modes (bool). |
| GHOSTTY_RENDER_STATE_DATA_CURSOR_BLINKING | Whether the cursor should blink based on terminal modes (bool). |
| GHOSTTY_RENDER_STATE_DATA_CURSOR_PASSWORD_INPUT | Whether the cursor is at a password input field (bool). |
| GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_HAS_VALUE | Whether the cursor is visible within the viewport (bool). If false, the cursor viewport position values are undefined. |
| GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_X | Cursor viewport x position in cells (uint16_t). Only valid when CURSOR_VIEWPORT_HAS_VALUE is true. |
| GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_Y | Cursor viewport y position in cells (uint16_t). Only valid when CURSOR_VIEWPORT_HAS_VALUE is true. |
| GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_WIDE_TAIL | Whether the cursor is on the tail of a wide character (bool). Only valid when CURSOR_VIEWPORT_HAS_VALUE is true. |
Dirty state of a render state after update.
Settable options for ghostty_render_state_set().
| Enumerator | |
|---|---|
| GHOSTTY_RENDER_STATE_OPTION_DIRTY | Set dirty state (GhosttyRenderStateDirty). |
Queryable data kinds for ghostty_render_state_row_cells_get().
| Enumerator | |
|---|---|
| GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_INVALID | Invalid / sentinel value. |
| GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_RAW | The raw cell value (GhosttyCell). |
| GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_STYLE | The style for the current cell (GhosttyStyle). |
| GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_GRAPHEMES_LEN | The total number of grapheme codepoints including the base codepoint (uint32_t). Returns 0 if the cell has no text. |
| GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_GRAPHEMES_BUF | Write grapheme codepoints into a caller-provided buffer (uint32_t*). The buffer must be at least graphemes_len elements. The base codepoint is written first, followed by any extra codepoints. |
Queryable data kinds for ghostty_render_state_row_get().
| Enumerator | |
|---|---|
| GHOSTTY_RENDER_STATE_ROW_DATA_INVALID | Invalid / sentinel value. |
| GHOSTTY_RENDER_STATE_ROW_DATA_DIRTY | Whether the current row is dirty (bool). |
| GHOSTTY_RENDER_STATE_ROW_DATA_RAW | The raw row value (GhosttyRow). |
| GHOSTTY_RENDER_STATE_ROW_DATA_CELLS | Populate a pre-allocated GhosttyRenderStateRowCells with cell data for the current row (GhosttyRenderStateRowCells). Cell data is only valid as long as the underlying render state is not updated. It is unsafe to use cell data after updating the render state. |
Settable options for ghostty_render_state_row_set().
| Enumerator | |
|---|---|
| GHOSTTY_RENDER_STATE_ROW_OPTION_DIRTY | Set dirty state for the current row (bool). |
| GhosttyResult ghostty_render_state_colors_get | ( | GhosttyRenderState | state, |
| GhosttyRenderStateColors * | out_colors ) |
Get the current color information from a render state.
This writes as many fields as fit in the caller-provided sized struct. out_colors->size must be set by the caller (typically via GHOSTTY_INIT_SIZED(GhosttyRenderStateColors)).
| state | The render state handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| [out] | out_colors | Sized output struct to receive render-state colors |
| void ghostty_render_state_free | ( | GhosttyRenderState | state | ) |
Free a render state instance.
Releases all resources associated with the render state. After this call, the render state handle becomes invalid.
| state | The render state handle to free (may be NULL) |
| GhosttyResult ghostty_render_state_get | ( | GhosttyRenderState | state, |
| GhosttyRenderStateData | data, | ||
| void * | out ) |
Get a value from a render state.
The out pointer must point to a value of the type corresponding to the requested data kind (see GhosttyRenderStateData).
| state | The render state handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| data | The data kind to query | |
| [out] | out | Pointer to receive the queried value |
| GhosttyResult ghostty_render_state_new | ( | const GhosttyAllocator * | allocator, |
| GhosttyRenderState * | state ) |
Create a new render state instance.
| Memory Management | Pointer to allocator, or NULL to use the default allocator |
| state | Pointer to store the created render state handle |
| void ghostty_render_state_row_cells_free | ( | GhosttyRenderStateRowCells | cells | ) |
Free a row cells instance.
| cells | The row cells handle to free (may be NULL) |
| GhosttyResult ghostty_render_state_row_cells_get | ( | GhosttyRenderStateRowCells | cells, |
| GhosttyRenderStateRowCellsData | data, | ||
| void * | out ) |
Get a value from the current cell in a render-state row cells iterator.
The out pointer must point to a value of the type corresponding to the requested data kind (see GhosttyRenderStateRowCellsData). Call ghostty_render_state_row_cells_next() or ghostty_render_state_row_cells_select() at least once before calling this function.
| cells | The row cells handle to query (NULL returns GHOSTTY_INVALID_VALUE) | |
| data | The data kind to query | |
| [out] | out | Pointer to receive the queried value |
| GhosttyResult ghostty_render_state_row_cells_new | ( | const GhosttyAllocator * | allocator, |
| GhosttyRenderStateRowCells * | out_cells ) |
Create a new row cells instance.
All fields except the allocator are left undefined until populated via ghostty_render_state_row_get() with GHOSTTY_RENDER_STATE_ROW_DATA_CELLS.
You can reuse this value repeatedly with ghostty_render_state_row_get() to avoid allocating a new cells container for every row.
| Memory Management | Pointer to allocator, or NULL to use the default allocator | |
| [out] | out_cells | On success, receives the created row cells handle |
| bool ghostty_render_state_row_cells_next | ( | GhosttyRenderStateRowCells | cells | ) |
Move a render-state row cells iterator to the next cell.
Returns true if the iterator moved successfully and cell data is available to read at the new position.
| cells | The row cells handle to advance (may be NULL) |
| GhosttyResult ghostty_render_state_row_cells_select | ( | GhosttyRenderStateRowCells | cells, |
| uint16_t | x ) |
Move a render-state row cells iterator to a specific column.
Positions the iterator at the given x (column) index so that subsequent reads return data for that cell.
| cells | The row cells handle to reposition (NULL returns GHOSTTY_INVALID_VALUE) |
| x | The zero-based column index to select |
| GhosttyResult ghostty_render_state_row_get | ( | GhosttyRenderStateRowIterator | iterator, |
| GhosttyRenderStateRowData | data, | ||
| void * | out ) |
Get a value from the current row in a render-state row iterator.
The out pointer must point to a value of the type corresponding to the requested data kind (see GhosttyRenderStateRowData). Call ghostty_render_state_row_iterator_next() at least once before calling this function.
| iterator | The iterator handle to query (NULL returns GHOSTTY_INVALID_VALUE) | |
| data | The data kind to query | |
| [out] | out | Pointer to receive the queried value |
| void ghostty_render_state_row_iterator_free | ( | GhosttyRenderStateRowIterator | iterator | ) |
Free a render-state row iterator.
| iterator | The iterator handle to free (may be NULL) |
| GhosttyResult ghostty_render_state_row_iterator_new | ( | const GhosttyAllocator * | allocator, |
| GhosttyRenderStateRowIterator * | out_iterator ) |
Create a new row iterator instance.
All fields except the allocator are left undefined until populated via ghostty_render_state_get() with GHOSTTY_RENDER_STATE_DATA_ROW_ITERATOR.
| Memory Management | Pointer to allocator, or NULL to use the default allocator | |
| [out] | out_iterator | On success, receives the created iterator handle |
| bool ghostty_render_state_row_iterator_next | ( | GhosttyRenderStateRowIterator | iterator | ) |
Move a render-state row iterator to the next row.
Returns true if the iterator moved successfully and row data is available to read at the new position.
| iterator | The iterator handle to advance (may be NULL) |
| GhosttyResult ghostty_render_state_row_set | ( | GhosttyRenderStateRowIterator | iterator, |
| GhosttyRenderStateRowOption | option, | ||
| const void * | value ) |
Set an option on the current row in a render-state row iterator.
The value pointer must point to a value of the type corresponding to the requested option kind (see GhosttyRenderStateRowOption). Call ghostty_render_state_row_iterator_next() at least once before calling this function.
| iterator | The iterator handle to update (NULL returns GHOSTTY_INVALID_VALUE) | |
| option | The option to set | |
| [in] | value | Pointer to the value to set (NULL returns GHOSTTY_INVALID_VALUE) |
| GhosttyResult ghostty_render_state_set | ( | GhosttyRenderState | state, |
| GhosttyRenderStateOption | option, | ||
| const void * | value ) |
Set an option on a render state.
The value pointer must point to a value of the type corresponding to the requested option kind (see GhosttyRenderStateOption).
| state | The render state handle (NULL returns GHOSTTY_INVALID_VALUE) | |
| option | The option to set | |
| [in] | value | Pointer to the value to set (NULL returns GHOSTTY_INVALID_VALUE) |
| GhosttyResult ghostty_render_state_update | ( | GhosttyRenderState | state, |
| GhosttyTerminal | terminal ) |
Update a render state instance from a terminal.
This consumes terminal/screen dirty state in the same way as the internal render state update path.
| state | The render state handle (NULL returns GHOSTTY_INVALID_VALUE) |
| Terminal | The terminal handle to read from (NULL returns GHOSTTY_INVALID_VALUE) |