Open History to see the last commit
1/**
2 * @file render.h
3 *
4 * Render state for creating high performance renderers.
5 */
6
7#ifndef GHOSTTY_VT_RENDER_H
8#define GHOSTTY_VT_RENDER_H
9
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13#include <ghostty/vt/allocator.h>
14#include <ghostty/vt/color.h>
15#include <ghostty/vt/terminal.h>
16#include <ghostty/vt/types.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/** @addtogroup render
23 *
24 * A render state is a copy of everything you need to draw one frame: the
25 * rows, the cells, the cursor, and the colors. It only needs the terminal
26 * during an update, so everything after that can happen while the terminal
27 * keeps working.
28 *
29 * Use this when you draw the terminal yourself.
30 *
31 * ## Example
32 *
33 * @snippet c-vt-render/src/main.c render-state-update
34 *
35 * Full program: @ref c-vt-render/src/main.c
36 *
37 * See @ref guide-rendering for how to draw a frame, track what changed,
38 * respect render holds, and use overscan and row identity. See
39 * @ref guide-images for drawing images.
40 *
41 * @{
42 */
43
44/**
45 * Dirty state of a render state after update.
46 *
47 * @ingroup render
48 */
49typedef enum GHOSTTY_ENUM_TYPED {
50 /** Not dirty at all; rendering can be skipped. */
51 GHOSTTY_RENDER_STATE_DIRTY_FALSE = 0,
52
53 /** Some rows changed; renderer can redraw incrementally. */
54 GHOSTTY_RENDER_STATE_DIRTY_PARTIAL = 1,
55
56 /** Global state changed; renderer should redraw everything. */
57 GHOSTTY_RENDER_STATE_DIRTY_FULL = 2,
58 GHOSTTY_RENDER_STATE_DIRTY_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
59} GhosttyRenderStateDirty;
60
61/**
62 * A number of rows above and below the viewport.
63 *
64 * This is used both to request overscan with
65 * GHOSTTY_RENDER_STATE_OPT_OVERSCAN and to report how many rows an
66 * update captured with GHOSTTY_RENDER_STATE_DATA_OVERSCAN. See
67 * @ref guide-rendering-overscan for how the extra rows are used.
68 *
69 * @ingroup render
70 */
71typedef struct {
72 /** Rows above the top of the viewport. */
73 uint16_t above;
74
75 /** Rows below the bottom of the viewport. */
76 uint16_t below;
77} GhosttyRenderStateOverscan;
78
79/**
80 * The identity of a row across render state updates.
81 *
82 * Treat this value as opaque. Two ids are the same when both words are
83 * equal. No other comparison or interpretation is meaningful, and the
84 * contents may change between library versions. A zero-initialized id is
85 * never valid, so it can be used to mean "no row".
86 *
87 * @code{.c}
88 * bool same = a.bits[0] == b.bits[0] && a.bits[1] == b.bits[1];
89 * @endcode
90 *
91 * See @ref guide-rendering-row-identity for how to use ids.
92 *
93 * @ingroup render
94 */
95typedef struct {
96 /** Opaque id data. Compare both words for equality. */
97 uint64_t bits[2];
98} GhosttyRenderStateRowId;
99
100/**
101 * Queryable data kinds for ghostty_render_state_get().
102 *
103 * @ingroup render
104 */
105typedef enum GHOSTTY_ENUM_TYPED {
106 /** Invalid / sentinel value. */
107 GHOSTTY_RENDER_STATE_DATA_INVALID = 0,
108
109 /** Viewport width in cells (uint16_t). */
110 GHOSTTY_RENDER_STATE_DATA_COLS = 1,
111
112 /** Viewport height in cells (uint16_t). This does not include
113 * overscan rows. */
114 GHOSTTY_RENDER_STATE_DATA_ROWS = 2,
115
116 /** Current dirty state (GhosttyRenderStateDirty). */
117 GHOSTTY_RENDER_STATE_DATA_DIRTY = 3,
118
119 /** Populate a pre-allocated GhosttyRenderStateRowIterator with row data
120 * from the render state (GhosttyRenderStateRowIterator). Row data is
121 * only valid as long as the underlying render state is not updated.
122 * It is unsafe to use row data after updating the render state.
123 *
124 * The iterator visits every row the last update captured, from top
125 * to bottom. This is exactly the viewport unless overscan was
126 * requested with GHOSTTY_RENDER_STATE_OPT_OVERSCAN.
127 * */
128 GHOSTTY_RENDER_STATE_DATA_ROW_ITERATOR = 4,
129
130 /** Default/current background color (GhosttyColorRgb). */
131 GHOSTTY_RENDER_STATE_DATA_COLOR_BACKGROUND = 5,
132
133 /** Default/current foreground color (GhosttyColorRgb). */
134 GHOSTTY_RENDER_STATE_DATA_COLOR_FOREGROUND = 6,
135
136 /** Cursor color when explicitly set by terminal state (GhosttyColorRgb).
137 * Returns GHOSTTY_NO_VALUE if no explicit cursor color is set. */
138 GHOSTTY_RENDER_STATE_DATA_COLOR_CURSOR = 7,
139
140 /** Whether an explicit cursor color is set (bool). */
141 GHOSTTY_RENDER_STATE_DATA_COLOR_CURSOR_HAS_VALUE = 8,
142
143 /** The active 256-color palette (GhosttyColorRgb[256]). */
144 GHOSTTY_RENDER_STATE_DATA_COLOR_PALETTE = 9,
145
146 /** The visual style of the cursor (GhosttyCursorStyle). */
147 GHOSTTY_RENDER_STATE_DATA_CURSOR_VISUAL_STYLE = 10,
148
149 /** Whether the cursor is visible based on terminal modes (bool). */
150 GHOSTTY_RENDER_STATE_DATA_CURSOR_VISIBLE = 11,
151
152 /** Whether the cursor should blink based on terminal modes (bool). */
153 GHOSTTY_RENDER_STATE_DATA_CURSOR_BLINKING = 12,
154
155 /** Whether the cursor is at a password input field (bool). */
156 GHOSTTY_RENDER_STATE_DATA_CURSOR_PASSWORD_INPUT = 13,
157
158 /** Whether the cursor is visible within the viewport (bool).
159 * If false, querying the cursor viewport position values returns
160 * GHOSTTY_NO_VALUE. */
161 GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_HAS_VALUE = 14,
162
163 /** Cursor viewport x position in cells (uint16_t).
164 * Returns GHOSTTY_NO_VALUE when CURSOR_VIEWPORT_HAS_VALUE is false. */
165 GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_X = 15,
166
167 /** Cursor viewport y position in cells (uint16_t).
168 * Returns GHOSTTY_NO_VALUE when CURSOR_VIEWPORT_HAS_VALUE is false. */
169 GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_Y = 16,
170
171 /** Whether the cursor is on the tail of a wide character (bool).
172 * Returns GHOSTTY_NO_VALUE when CURSOR_VIEWPORT_HAS_VALUE is false. */
173 GHOSTTY_RENDER_STATE_DATA_CURSOR_VIEWPORT_WIDE_TAIL = 17,
174
175 /** All cursor state in one sized struct (GhosttyRenderStateCursor).
176 * Initialize the output with GHOSTTY_INIT_SIZED before querying. */
177 GHOSTTY_RENDER_STATE_DATA_CURSOR = 18,
178
179 /** All render-state colors in one sized struct (GhosttyRenderStateColors).
180 * Initialize the output with GHOSTTY_INIT_SIZED before querying. */
181 GHOSTTY_RENDER_STATE_DATA_COLORS = 19,
182
183 /** How many overscan rows the last update captured on each side
184 * (GhosttyRenderStateOverscan). This is never more than the request.
185 * It is less when those rows don't exist: `above` is smaller near the
186 * top of the scrollback, and `below` is zero while the viewport is
187 * scrolled to the bottom. */
188 GHOSTTY_RENDER_STATE_DATA_OVERSCAN = 20,
189
190 /** The overscan request most recently set with
191 * GHOSTTY_RENDER_STATE_OPT_OVERSCAN (GhosttyRenderStateOverscan).
192 * The next update uses this request. Both sides are zero if it was
193 * never set. */
194 GHOSTTY_RENDER_STATE_DATA_OVERSCAN_REQUEST = 21,
195
196 /** Populate an existing GhosttyRenderStateImageIterator with the image
197 * placements found by the last update. See @ref guide-images-drawing. */
198 GHOSTTY_RENDER_STATE_DATA_IMAGE_ITERATOR = 22,
199
200 /** A value (uint64_t) that changes whenever an update changes the
201 * placements that GHOSTTY_RENDER_STATE_DATA_IMAGE_ITERATOR returns. If
202 * it matches the value from the last time you read the iterator, the
203 * placements are the same and you don't need to read them again. See
204 * @ref guide-images-changes for an example and for exactly when the
205 * value changes.
206 *
207 * The value belongs to the whole list. To know when the pixels of a
208 * single image changed, use
209 * GHOSTTY_RENDER_STATE_IMAGE_DATA_IMAGE_GENERATION.
210 *
211 * Only compare the value for equality. It is always 0 when Kitty
212 * graphics are disabled. */
213 GHOSTTY_RENDER_STATE_DATA_IMAGE_PLACEMENTS_GENERATION = 23,
214 GHOSTTY_RENDER_STATE_DATA_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
215} GhosttyRenderStateData;
216
217/**
218 * Settable options for ghostty_render_state_set().
219 *
220 * @ingroup render
221 */
222typedef enum GHOSTTY_ENUM_TYPED {
223 /**
224 * Not a real option. ghostty_render_state_set() returns
225 * GHOSTTY_INVALID_VALUE for it. Its value is 0, so a zeroed or
226 * uninitialized variable is never mistaken for a real option.
227 */
228 GHOSTTY_RENDER_STATE_OPT_INVALID = 0,
229
230 /** Set dirty state (GhosttyRenderStateDirty). Each update sets this
231 * value itself, so there is no default to restore and a NULL value
232 * returns GHOSTTY_INVALID_VALUE. */
233 GHOSTTY_RENDER_STATE_OPT_DIRTY = 1,
234
235 /** Request overscan rows above and below the viewport
236 * (GhosttyRenderStateOverscan). The request takes effect on the next
237 * update and stays in effect until it is changed. Both sides are zero
238 * by default, which captures only the viewport. A NULL value restores
239 * that default. The rows of the last update can still be read after
240 * changing the request. Expect a full redraw on the update after a
241 * change. See @ref guide-rendering-overscan. */
242 GHOSTTY_RENDER_STATE_OPT_OVERSCAN = 2,
243 GHOSTTY_RENDER_STATE_OPT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
244} GhosttyRenderStateOption;
245
246/**
247 * The image protocols that a terminal supports.
248 *
249 * The render state uses this to say which protocol each image placement
250 * came from (see GHOSTTY_RENDER_STATE_IMAGE_DATA_PROTOCOL). Use it to
251 * decide where to read the image's pixels. Unlike the other image types
252 * here, the name has no RenderState prefix, because the protocol isn't
253 * specific to the render state.
254 *
255 * New protocols may be added in later versions. Skip placements with a
256 * protocol you don't support.
257 *
258 * @ingroup render
259 */
260typedef enum GHOSTTY_ENUM_TYPED {
261 /** The Kitty graphics protocol. Read the image with
262 * ghostty_kitty_graphics_image(). */
263 GHOSTTY_IMAGE_PROTOCOL_KITTY = 0,
264 GHOSTTY_IMAGE_PROTOCOL_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
265} GhosttyImageProtocol;
266
267/**
268 * The kinds of Kitty graphics placements in a render state's image list.
269 *
270 * Read with GHOSTTY_RENDER_STATE_IMAGE_DATA_KITTY_KIND. Most renderers
271 * can draw every kind the same way. The kind is useful when you need to
272 * tell them apart, for example for debugging.
273 *
274 * @ingroup render
275 */
276typedef enum GHOSTTY_ENUM_TYPED {
277 /** Part of a virtual placement: one run of neighboring placeholder
278 * cells in a single row. It is always one row tall. */
279 GHOSTTY_RENDER_STATE_KITTY_PLACEMENT_KIND_VIRTUAL = 0,
280
281 /** A placement that a program positioned relative to another
282 * placement, its parent. Kitty graphics lets a program attach one
283 * image to another so that they move together. The parent is either
284 * a virtual or a fixed placement, possibly through a chain of other
285 * relative placements.
286 *
287 * With a virtual parent, the position depends on the placeholder
288 * cells. It is measured from the top-left corner of the parent's
289 * cells on screen. That is the leftmost column and the topmost row
290 * that any of its cells use, which matches Kitty. If none of those
291 * cells are in the captured rows (the viewport plus any overscan), the
292 * relative placement is not included.
293 *
294 * With a fixed parent, the position is measured from the parent's
295 * cell. The relative placement is included at its full size when any
296 * part of it is in the captured rows, even if the parent isn't, so it
297 * may need to be clipped when drawn.
298 *
299 * See @ref guide-images-relative. */
300 GHOSTTY_RENDER_STATE_KITTY_PLACEMENT_KIND_RELATIVE = 1,
301
302 /** A placement at a cell on the screen. It scrolls with the text,
303 * like a line of output would. Its z-index is the one the program
304 * chose, so it can be in any layer.
305 *
306 * It is included at its full size when any part of it is in the
307 * captured rows, so it may need to be clipped when drawn. See
308 * @ref guide-images-clipping. */
309 GHOSTTY_RENDER_STATE_KITTY_PLACEMENT_KIND_FIXED = 2,
310 GHOSTTY_RENDER_STATE_KITTY_PLACEMENT_KIND_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
311} GhosttyRenderStateKittyPlacementKind;
312
313/**
314 * Data you can read about the current placement of an image iterator
315 * with ghostty_render_state_image_get().
316 *
317 * The type in parentheses is the type that the `out` pointer must point
318 * to. All pixel values use the cell size set with ghostty_terminal_resize().
319 *
320 * Some values only apply to one protocol, and have the protocol in their
321 * name. For example, GHOSTTY_RENDER_STATE_IMAGE_DATA_KITTY_KIND only
322 * applies to Kitty graphics placements. Reading one of these for a
323 * placement from another protocol returns GHOSTTY_NO_VALUE.
324 *
325 * @ingroup render
326 */
327typedef enum GHOSTTY_ENUM_TYPED {
328 /** Invalid value. Querying it returns GHOSTTY_INVALID_VALUE. */
329 GHOSTTY_RENDER_STATE_IMAGE_DATA_INVALID = 0,
330
331 /** The protocol the image came from (GhosttyImageProtocol). */
332 GHOSTTY_RENDER_STATE_IMAGE_DATA_PROTOCOL = 1,
333
334 /** The ID of the image to draw (uint32_t). IDs are only unique within
335 * one protocol, so look the image up in the storage of its protocol.
336 * For Kitty graphics, use ghostty_kitty_graphics_image(). */
337 GHOSTTY_RENDER_STATE_IMAGE_DATA_IMAGE_ID = 2,
338
339 /** The generation of the image's contents (uint64_t). It changes
340 * whenever the image's pixels change, including when an animation
341 * moves to a new frame. Use it as part of your texture cache key,
342 * with the protocol and image ID: if it matches the texture you have,
343 * you don't need to read the pixels again. For Kitty graphics, this
344 * is the same value as GHOSTTY_KITTY_GRAPHICS_IMAGE_DATA_GENERATION.
345 *
346 * To know when the list of placements as a whole changed, use
347 * GHOSTTY_RENDER_STATE_DATA_IMAGE_PLACEMENTS_GENERATION. */
348 GHOSTTY_RENDER_STATE_IMAGE_DATA_IMAGE_GENERATION = 3,
349
350 /** The layer to draw the image in (GhosttyImageLayer). */
351 GHOSTTY_RENDER_STATE_IMAGE_DATA_LAYER = 4,
352
353 /** The z-index (int32_t), which orders images within a layer. Images
354 * with a lower z-index are drawn first. The value comes from the
355 * image's protocol. For Kitty graphics, it is the z-index the program
356 * asked for, and virtual placements are always -1. */
357 GHOSTTY_RENDER_STATE_IMAGE_DATA_Z = 5,
358
359 /** The column of the top-left cell (int32_t), where 0 is the leftmost
360 * column of the viewport. Fixed and relative placements are reported
361 * at their full size, so they can start to the left of the viewport,
362 * which gives a negative value. Virtual placements never do. See
363 * @ref guide-images-clipping. */
364 GHOSTTY_RENDER_STATE_IMAGE_DATA_VIEWPORT_COL = 6,
365
366 /** The row of the top-left cell (int32_t), where 0 is the top row of
367 * the viewport. A placement in an overscan row above the viewport has
368 * a negative row, the same as GHOSTTY_RENDER_STATE_ROW_DATA_VIEWPORT_Y.
369 * Fixed and relative placements are reported at their full size, so
370 * they can also start above the first overscan row. Virtual
371 * placements are always inside the captured rows. See
372 * @ref guide-images-clipping. */
373 GHOSTTY_RENDER_STATE_IMAGE_DATA_VIEWPORT_ROW = 7,
374
375 /** The number of columns of cells the placement covers (uint32_t). */
376 GHOSTTY_RENDER_STATE_IMAGE_DATA_GRID_COLS = 8,
377
378 /** The number of rows of cells the placement covers (uint32_t). */
379 GHOSTTY_RENDER_STATE_IMAGE_DATA_GRID_ROWS = 9,
380
381 /** How far the image starts from the left edge of the top-left cell,
382 * in pixels (uint32_t). For a virtual placement, an image that is
383 * narrower than its cells is centered, so this can be larger than a
384 * cell is wide. For fixed and relative placements, this is the offset
385 * the program asked for, limited to less than one cell. */
386 GHOSTTY_RENDER_STATE_IMAGE_DATA_CELL_OFFSET_X = 10,
387
388 /** How far the image starts from the top edge of the top-left cell,
389 * in pixels (uint32_t). For a virtual placement, an image that is
390 * shorter than its cells is centered, so this can be larger than a
391 * cell is tall. For fixed and relative placements, this is the offset
392 * the program asked for, limited to less than one cell. */
393 GHOSTTY_RENDER_STATE_IMAGE_DATA_CELL_OFFSET_Y = 11,
394
395 /** The width to draw the image at, in pixels (uint32_t). */
396 GHOSTTY_RENDER_STATE_IMAGE_DATA_PIXEL_WIDTH = 12,
397
398 /** The height to draw the image at, in pixels (uint32_t). */
399 GHOSTTY_RENDER_STATE_IMAGE_DATA_PIXEL_HEIGHT = 13,
400
401 /** The left edge of the part of the image to draw, in image pixels
402 * (uint32_t). */
403 GHOSTTY_RENDER_STATE_IMAGE_DATA_SOURCE_X = 14,
404
405 /** The top edge of the part of the image to draw, in image pixels
406 * (uint32_t). */
407 GHOSTTY_RENDER_STATE_IMAGE_DATA_SOURCE_Y = 15,
408
409 /** The width of the part of the image to draw, in image pixels
410 * (uint32_t). Scale it to PIXEL_WIDTH when drawing. */
411 GHOSTTY_RENDER_STATE_IMAGE_DATA_SOURCE_WIDTH = 16,
412
413 /** The height of the part of the image to draw, in image pixels
414 * (uint32_t). Scale it to PIXEL_HEIGHT when drawing. */
415 GHOSTTY_RENDER_STATE_IMAGE_DATA_SOURCE_HEIGHT = 17,
416
417 /** Kitty graphics only: the placement ID (uint32_t), or 0 if there
418 * isn't one.
419 *
420 * For a virtual placement, this is the placement ID that the
421 * placeholder cells ask for. Cells that don't ask for one show the
422 * image's virtual placement, whichever it is, and report 0. For a
423 * fixed or relative placement, this is the ID the program gave it, or
424 * 0 if it didn't give one. */
425 GHOSTTY_RENDER_STATE_IMAGE_DATA_KITTY_PLACEMENT_ID = 18,
426
427 /** Kitty graphics only: the kind of placement
428 * (GhosttyRenderStateKittyPlacementKind). */
429 GHOSTTY_RENDER_STATE_IMAGE_DATA_KITTY_KIND = 19,
430 GHOSTTY_RENDER_STATE_IMAGE_DATA_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
431} GhosttyRenderStateImageData;
432
433/**
434 * Queryable data kinds for ghostty_render_state_row_get().
435 *
436 * @ingroup render
437 */
438typedef enum GHOSTTY_ENUM_TYPED {
439 /** Invalid / sentinel value. */
440 GHOSTTY_RENDER_STATE_ROW_DATA_INVALID = 0,
441
442 /** Whether the current row is dirty (bool). */
443 GHOSTTY_RENDER_STATE_ROW_DATA_DIRTY = 1,
444
445 /** The raw row value (GhosttyRow). */
446 GHOSTTY_RENDER_STATE_ROW_DATA_RAW = 2,
447
448 /** Populate a pre-allocated GhosttyRenderStateRowCells with cell data for
449 * the current row (GhosttyRenderStateRowCells). Cell data is only
450 * valid as long as the underlying render state is not updated.
451 * It is unsafe to use cell data after updating the render state. */
452 GHOSTTY_RENDER_STATE_ROW_DATA_CELLS = 3,
453
454 /** Row-local selected cell range (GhosttyRenderStateRowSelection). */
455 GHOSTTY_RENDER_STATE_ROW_DATA_SELECTION = 4,
456
457 /** A borrowed view of the raw cell values for the current row
458 * (GhosttyCellsView). One value per column, identical to querying
459 * GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_RAW for each cell. The view
460 * is only valid as long as the underlying render state is not
461 * updated; it is unsafe to use after updating the render state.
462 *
463 * This is the bulk alternative to iterating cells one at a time.
464 * It lets callers with expensive call boundaries (e.g. WebAssembly
465 * embedders) read an entire row with a single call.
466 *
467 * Bit positions aren't protected by ABI, so callers should parse them
468 * out of the manifest from `ghostty_type_json`. Callers with access
469 * to the C header or without high FFI costs should use `ghostty_cell_get`.
470 */
471 GHOSTTY_RENDER_STATE_ROW_DATA_CELLS_RAW = 5,
472
473 /** The row's position relative to the top of the viewport (int32_t).
474 * Viewport rows are 0 through rows - 1. Overscan rows above the
475 * viewport are negative, and overscan rows below it start at rows.
476 * Without overscan, this equals the y reported by
477 * ghostty_render_state_row_iterator_next_dirty(). */
478 GHOSTTY_RENDER_STATE_ROW_DATA_VIEWPORT_Y = 6,
479
480 /** The row's identity across updates (GhosttyRenderStateRowId). This
481 * works with or without overscan. See
482 * @ref guide-rendering-row-identity. */
483 GHOSTTY_RENDER_STATE_ROW_DATA_ID = 7,
484 GHOSTTY_RENDER_STATE_ROW_DATA_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
485} GhosttyRenderStateRowData;
486
487/**
488 * Settable options for ghostty_render_state_row_set().
489 *
490 * @ingroup render
491 */
492typedef enum GHOSTTY_ENUM_TYPED {
493 /**
494 * Not a real option. ghostty_render_state_row_set() returns
495 * GHOSTTY_INVALID_VALUE for it. Its value is 0, so a zeroed or
496 * uninitialized variable is never mistaken for a real option.
497 */
498 GHOSTTY_RENDER_STATE_ROW_OPT_INVALID = 0,
499
500 /** Set dirty state for the current row (bool). Each update sets this
501 * value itself, so there is no default to restore and a NULL value
502 * returns GHOSTTY_INVALID_VALUE. */
503 GHOSTTY_RENDER_STATE_ROW_OPT_DIRTY = 1,
504 GHOSTTY_RENDER_STATE_ROW_OPT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
505} GhosttyRenderStateRowOption;
506
507/**
508 * Row-local selection range.
509 *
510 * This is a sized struct. Initialize it with GHOSTTY_INIT_SIZED() before
511 * querying GHOSTTY_RENDER_STATE_ROW_DATA_SELECTION. See @ref sized-structs.
512 *
513 * Querying GHOSTTY_RENDER_STATE_ROW_DATA_SELECTION returns GHOSTTY_NO_VALUE
514 * if the current row does not intersect the current selection.
515 *
516 * @ingroup render
517 */
518typedef struct {
519 /** Size of this struct in bytes. Must be set to sizeof(GhosttyRenderStateRowSelection). */
520 size_t size;
521
522 /** Start column of the row-local selection range, inclusive. */
523 uint16_t start_x;
524
525 /** End column of the row-local selection range, inclusive. */
526 uint16_t end_x;
527} GhosttyRenderStateRowSelection;
528
529/**
530 * Render-state cursor information.
531 *
532 * This is a sized struct. Initialize it with GHOSTTY_INIT_SIZED() before
533 * querying GHOSTTY_RENDER_STATE_DATA_CURSOR. See @ref sized-structs.
534 *
535 * When viewport_has_value is false, viewport_x, viewport_y, and wide_tail
536 * contain undefined data and must not be read.
537 *
538 * @ingroup render
539 */
540typedef struct {
541 /** Size of this struct in bytes. Must be set to sizeof(GhosttyRenderStateCursor). */
542 size_t size;
543
544 /** Whether the cursor is visible within the viewport. */
545 bool viewport_has_value;
546
547 /** Cursor viewport x position in cells. */
548 uint16_t viewport_x;
549
550 /** Cursor viewport y position in cells. */
551 uint16_t viewport_y;
552
553 /** Whether the cursor is on the tail of a wide character. */
554 bool wide_tail;
555
556 /** Whether the cursor is visible based on terminal modes. */
557 bool visible;
558
559 /** Whether the cursor should blink based on terminal modes. */
560 bool blinking;
561
562 /** Whether the cursor is at a password input field. */
563 bool password_input;
564
565 /** The visual style of the cursor. */
566 GhosttyCursorStyle visual_style;
567} GhosttyRenderStateCursor;
568
569/**
570 * Render-state color information.
571 *
572 * This is a sized struct. Initialize it with GHOSTTY_INIT_SIZED() before
573 * querying GHOSTTY_RENDER_STATE_DATA_COLORS. See @ref sized-structs.
574 *
575 * Example:
576 * @code
577 * GhosttyRenderStateColors colors = GHOSTTY_INIT_SIZED(GhosttyRenderStateColors);
578 * GhosttyResult result = ghostty_render_state_get(
579 * state, GHOSTTY_RENDER_STATE_DATA_COLORS, &colors);
580 * @endcode
581 *
582 * @ingroup render
583 */
584typedef struct {
585 /** Size of this struct in bytes. Must be set to sizeof(GhosttyRenderStateColors). */
586 size_t size;
587
588 /** The default/current background color for the render state. */
589 GhosttyColorRgb background;
590
591 /** The default/current foreground color for the render state. */
592 GhosttyColorRgb foreground;
593
594 /** The cursor color when explicitly set by terminal state. */
595 GhosttyColorRgb cursor;
596
597 /**
598 * True when cursor contains a valid explicit cursor color value.
599 * If this is false, the cursor color should be ignored; it will
600 * contain undefined data.
601 * */
602 bool cursor_has_value;
603
604 /** The active 256-color palette for this render state. */
605 GhosttyColorRgb palette[256];
606} GhosttyRenderStateColors;
607
608/** @name Lifecycle
609 * @{
610 */
611
612/**
613 * Create a new render state instance.
614 *
615 * @param allocator Pointer to allocator, or NULL to use the default allocator
616 * @param state Pointer to store the created render state handle (NULL returns
617 * GHOSTTY_INVALID_VALUE)
618 * @return GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_MEMORY on allocation
619 * failure
620 *
621 * @ingroup render
622 */
623GHOSTTY_API GhosttyResult ghostty_render_state_new(const GhosttyAllocator* allocator,
624 GhosttyRenderState* state);
625
626/**
627 * Free a render state instance.
628 *
629 * Releases all resources associated with the render state. After this call,
630 * the render state handle becomes invalid.
631 *
632 * @param state The render state handle to free (may be NULL)
633 *
634 * @ingroup render
635 */
636GHOSTTY_API void ghostty_render_state_free(GhosttyRenderState state);
637
638/** @} */
639
640/** @name Updating
641 * @{
642 */
643
644/**
645 * Update a render state instance from a terminal.
646 *
647 * This consumes terminal/screen dirty state in the same way as the internal
648 * render state update path.
649 *
650 * This is a convenience function that performs a full update in one call,
651 * equivalent to ghostty_render_state_begin_update immediately followed by
652 * ghostty_render_state_end_update. Callers that hold a lock over the
653 * terminal state should prefer calling the two phases directly so that the
654 * lock is only held for the begin phase.
655 *
656 * @param state The render state handle (NULL returns GHOSTTY_INVALID_VALUE)
657 * @param terminal The terminal handle to read from (NULL returns GHOSTTY_INVALID_VALUE)
658 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if `state` or
659 * `terminal` is NULL, GHOSTTY_OUT_OF_MEMORY if updating the state requires
660 * allocation and that allocation fails
661 *
662 * @ingroup render
663 */
664GHOSTTY_API GhosttyResult ghostty_render_state_update(GhosttyRenderState state,
665 GhosttyTerminal terminal);
666
667/**
668 * Begin an update of a render state instance from a terminal.
669 *
670 * Every begin must be completed with a ghostty_render_state_end_update call
671 * before the render state is read.
672 *
673 * This two-phase structure exists for callers that synchronize access to the
674 * terminal state (e.g. with a lock shared with an IO thread): only this
675 * function requires terminal access, so a caller can hold its lock for this
676 * call only and then call ghostty_render_state_end_update after releasing
677 * it. The end phase exclusively reads and writes memory owned by the render
678 * state, so it is safe to call while the terminal is being modified.
679 *
680 * Work that doesn't require terminal access may be deferred to the end phase
681 * to keep this call (and therefore lock hold time) as short as possible.
682 * You must treat the render state as incomplete until
683 * ghostty_render_state_end_update is called.
684 *
685 * This consumes terminal/screen dirty state in the same way as the internal
686 * render state update path.
687 *
688 * @param state The render state handle (NULL returns GHOSTTY_INVALID_VALUE)
689 * @param terminal The terminal handle to read from (NULL returns GHOSTTY_INVALID_VALUE)
690 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if `state` or
691 * `terminal` is NULL, GHOSTTY_OUT_OF_MEMORY if updating the state requires
692 * allocation and that allocation fails
693 *
694 * @ingroup render
695 */
696GHOSTTY_API GhosttyResult ghostty_render_state_begin_update(GhosttyRenderState state,
697 GhosttyTerminal terminal);
698
699/**
700 * Complete a prior ghostty_render_state_begin_update call by performing any
701 * deferred work.
702 *
703 * This only reads and writes memory owned by the render state, so it is safe
704 * to call while the terminal is being modified (no terminal synchronization
705 * is required). Calling this without a prior begin is a safe no-op.
706 *
707 * @param state The render state handle (NULL returns GHOSTTY_INVALID_VALUE)
708 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if `state` is
709 * NULL
710 *
711 * @ingroup render
712 */
713GHOSTTY_API GhosttyResult ghostty_render_state_end_update(GhosttyRenderState state);
714
715/**
716 * Mark all dirty render-state data as consumed.
717 *
718 * This sets the global dirty state to GHOSTTY_RENDER_STATE_DIRTY_FALSE and
719 * clears every per-row dirty flag. It is idempotent and does not modify cell
720 * contents or dirty state owned by the terminal. Call this only after a
721 * complete frame has been rendered successfully; partial consumers should
722 * use ghostty_render_state_set() and ghostty_render_state_row_set() instead.
723 *
724 * @param state The render state handle (NULL returns GHOSTTY_INVALID_VALUE)
725 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if `state` is
726 * NULL
727 *
728 * @ingroup render
729 */
730GHOSTTY_API GhosttyResult ghostty_render_state_clean(GhosttyRenderState state);
731
732/** @} */
733
734/** @name Reading State
735 * @{
736 */
737
738/**
739 * Get a value from a render state.
740 *
741 * The `out` pointer must point to a value of the type corresponding to the
742 * requested data kind (see GhosttyRenderStateData).
743 *
744 * @param state The render state handle (NULL returns GHOSTTY_INVALID_VALUE)
745 * @param data The data kind to query
746 * @param[out] out Pointer to receive the queried value
747 * @return GHOSTTY_SUCCESS on success, GHOSTTY_NO_VALUE if `data` has no
748 * value right now, GHOSTTY_INVALID_VALUE if `state` or `out` is NULL
749 * or a sized output struct is smaller than its base layout, or
750 * GHOSTTY_UNSUPPORTED if `data` is not known to this version of the
751 * library
752 *
753 * @ingroup render
754 */
755GHOSTTY_API GhosttyResult ghostty_render_state_get(GhosttyRenderState state,
756 GhosttyRenderStateData data,
757 void* out);
758
759/**
760 * Get multiple data fields from a render state in a single call.
761 *
762 * Use this when each call into the library is expensive, for example from
763 * WebAssembly or from another language through its foreign function
764 * interface. It reads every key in one call. From C in the same process it
765 * costs the same as calling ghostty_render_state_get() once per key, so use
766 * whichever reads better.
767 *
768 * Each element in the keys array specifies a data kind, and the
769 * corresponding element in the values array receives the result.
770 *
771 * Processing stops at the first error; on success out_written
772 * is set to count, on error it is set to the index of the
773 * failing key (i.e. the number of values successfully written).
774 *
775 * @param state The render state handle (NULL returns GHOSTTY_INVALID_VALUE)
776 * @param count Number of key/value pairs
777 * @param keys Array of data kinds to query
778 * @param values Array of output pointers (types must match each key's
779 * documented output type)
780 * @param[out] out_written On return, receives the number of values
781 * successfully written (may be NULL)
782 * @return GHOSTTY_SUCCESS if every key succeeds. Otherwise the result of the
783 * first key that failed, for example GHOSTTY_UNSUPPORTED for a key
784 * that is not known to this version of the library.
785 *
786 * @ingroup render
787 */
788GHOSTTY_API GhosttyResult ghostty_render_state_get_multi(
789 GhosttyRenderState state,
790 size_t count,
791 const GhosttyRenderStateData* keys,
792 void** values,
793 size_t* out_written);
794
795/**
796 * Set an option on a render state.
797 *
798 * The `value` pointer must point to a value of the type corresponding to the
799 * requested option kind (see GhosttyRenderStateOption).
800 *
801 * Passing NULL for `value` restores the option's default. Options that
802 * have no default, such as GHOSTTY_RENDER_STATE_OPT_DIRTY, return
803 * GHOSTTY_INVALID_VALUE for NULL instead.
804 *
805 * @code{.c}
806 * GhosttyRenderStateOverscan overscan = { .above = 4, .below = 4 };
807 * ghostty_render_state_set(state, GHOSTTY_RENDER_STATE_OPT_OVERSCAN,
808 * &overscan);
809 *
810 * // Later, go back to capturing only the viewport.
811 * ghostty_render_state_set(state, GHOSTTY_RENDER_STATE_OPT_OVERSCAN, NULL);
812 * @endcode
813 *
814 * @param state The render state handle (NULL returns GHOSTTY_INVALID_VALUE)
815 * @param option The option to set
816 * @param[in] value Pointer to the value to set, or NULL to restore the
817 * option's default
818 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if `state` is
819 * NULL, `value` is out of range, or `value` is NULL for an option
820 * with no default, or GHOSTTY_UNSUPPORTED if `option` is not known
821 * to this version of the library
822 *
823 * @ingroup render
824 */
825GHOSTTY_API GhosttyResult ghostty_render_state_set(GhosttyRenderState state,
826 GhosttyRenderStateOption option,
827 const void* value);
828
829/** @} */
830
831/** @name Rows
832 * @{
833 */
834
835/**
836 * Create a new row iterator instance.
837 *
838 * All fields except the allocator are left undefined until populated
839 * via ghostty_render_state_get() with
840 * GHOSTTY_RENDER_STATE_DATA_ROW_ITERATOR.
841 *
842 * @param allocator Pointer to allocator, or NULL to use the default allocator
843 * @param[out] out_iterator On success, receives the created iterator handle
844 * @return GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_MEMORY on allocation
845 * failure, GHOSTTY_INVALID_VALUE if @p out_iterator is NULL
846 *
847 * @ingroup render
848 */
849GHOSTTY_API GhosttyResult ghostty_render_state_row_iterator_new(
850 const GhosttyAllocator* allocator,
851 GhosttyRenderStateRowIterator* out_iterator);
852
853/**
854 * Free a render-state row iterator.
855 *
856 * @param iterator The iterator handle to free (may be NULL)
857 *
858 * @ingroup render
859 */
860GHOSTTY_API void ghostty_render_state_row_iterator_free(GhosttyRenderStateRowIterator iterator);
861
862/**
863 * Move a render-state row iterator to the next row.
864 *
865 * Rows are visited in order from top to bottom with no gaps. Without
866 * overscan, the first row is the top row of the viewport. With overscan,
867 * the first row is the highest captured row above the viewport (see
868 * GHOSTTY_RENDER_STATE_OPT_OVERSCAN). Returns true if the iterator
869 * moved successfully and row data is available to read at the new
870 * position.
871 *
872 * @param iterator The iterator handle to advance (may be NULL)
873 * @return true if advanced to the next row, false if `iterator` is
874 * NULL or if the iterator has reached the end
875 *
876 * @ingroup render
877 */
878GHOSTTY_API bool ghostty_render_state_row_iterator_next(GhosttyRenderStateRowIterator iterator);
879
880/**
881 * Move a render-state row iterator to the next row requiring a redraw.
882 *
883 * If the global dirty state is GHOSTTY_RENDER_STATE_DIRTY_FALSE, this returns
884 * false. If it is GHOSTTY_RENDER_STATE_DIRTY_PARTIAL, clean rows are skipped.
885 * If it is GHOSTTY_RENDER_STATE_DIRTY_FULL, every remaining row is returned
886 * regardless of its per-row dirty flag. Rows are returned in ascending
887 * viewport order. This function does not clear any dirty state.
888 *
889 * @param iterator The iterator handle to advance (NULL returns false)
890 * @param[out] out_y Receives the row's position in the iterator when true
891 * is returned (NULL returns false). It is not modified
892 * when false is returned. Without overscan, this is the
893 * viewport y. With overscan, it counts from the highest
894 * captured row, so use
895 * GHOSTTY_RENDER_STATE_ROW_DATA_VIEWPORT_Y to place
896 * the row.
897 * @return true if advanced to a row requiring a redraw, false if an argument
898 * is NULL or the iterator has reached the end of the effective dirty
899 * rows
900 *
901 * @ingroup render
902 */
903GHOSTTY_API bool ghostty_render_state_row_iterator_next_dirty(
904 GhosttyRenderStateRowIterator iterator,
905 uint16_t* out_y);
906
907/**
908 * Get a value from the current row in a render-state row iterator.
909 *
910 * The `out` pointer must point to a value of the type corresponding to the
911 * requested data kind (see GhosttyRenderStateRowData).
912 * Call ghostty_render_state_row_iterator_next() or
913 * ghostty_render_state_row_iterator_next_dirty() at least once before
914 * calling this function.
915 *
916 * @param iterator The iterator handle to query (NULL returns GHOSTTY_INVALID_VALUE)
917 * @param data The data kind to query
918 * @param[out] out Pointer to receive the queried value
919 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if `iterator` or
920 * `out` is NULL or the iterator is not positioned on a row, or
921 * GHOSTTY_UNSUPPORTED if `data` is not known to this version of the
922 * library
923 *
924 * @ingroup render
925 */
926GHOSTTY_API GhosttyResult ghostty_render_state_row_get(
927 GhosttyRenderStateRowIterator iterator,
928 GhosttyRenderStateRowData data,
929 void* out);
930
931/**
932 * Get multiple data fields from the current row in a single call.
933 *
934 * Use this when each call into the library is expensive, for example from
935 * WebAssembly or from another language through its foreign function
936 * interface. It reads every key in one call. From C in the same process it
937 * costs the same as calling ghostty_render_state_row_get() once per key, so
938 * use whichever reads better.
939 *
940 * Each element in the keys array specifies a data kind, and the
941 * corresponding element in the values array receives the result.
942 *
943 * Processing stops at the first error; on success out_written
944 * is set to count, on error it is set to the index of the
945 * failing key (i.e. the number of values successfully written).
946 *
947 * @param iterator The iterator handle (NULL returns GHOSTTY_INVALID_VALUE)
948 * @param count Number of key/value pairs
949 * @param keys Array of data kinds to query
950 * @param values Array of output pointers (types must match each key's
951 * documented output type)
952 * @param[out] out_written On return, receives the number of values
953 * successfully written (may be NULL)
954 * @return GHOSTTY_SUCCESS if every key succeeds. Otherwise the result of the
955 * first key that failed, for example GHOSTTY_UNSUPPORTED for a key
956 * that is not known to this version of the library.
957 *
958 * @ingroup render
959 */
960GHOSTTY_API GhosttyResult ghostty_render_state_row_get_multi(
961 GhosttyRenderStateRowIterator iterator,
962 size_t count,
963 const GhosttyRenderStateRowData* keys,
964 void** values,
965 size_t* out_written);
966
967/**
968 * Set an option on the current row in a render-state row iterator.
969 *
970 * The `value` pointer must point to a value of the type corresponding to the
971 * requested option kind (see GhosttyRenderStateRowOption).
972 * Call ghostty_render_state_row_iterator_next() or
973 * ghostty_render_state_row_iterator_next_dirty() at least once before
974 * calling this function.
975 *
976 * @param iterator The iterator handle to update (NULL returns GHOSTTY_INVALID_VALUE)
977 * @param option The option to set
978 * @param[in] value Pointer to the value to set. Row options have no
979 * default to restore, so NULL returns GHOSTTY_INVALID_VALUE.
980 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if `iterator` or
981 * `value` is NULL or the iterator is not positioned on a row, or
982 * GHOSTTY_UNSUPPORTED if `option` is not known to this version of the
983 * library
984 *
985 * @ingroup render
986 */
987GHOSTTY_API GhosttyResult ghostty_render_state_row_set(
988 GhosttyRenderStateRowIterator iterator,
989 GhosttyRenderStateRowOption option,
990 const void* value);
991
992/** @} */
993
994/**
995 * Queryable data kinds for ghostty_render_state_row_cells_get().
996 *
997 * @ingroup render
998 */
999typedef enum GHOSTTY_ENUM_TYPED {
1000 /** Invalid / sentinel value. */
1001 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_INVALID = 0,
1002
1003 /** The raw cell value (GhosttyCell). */
1004 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_RAW = 1,
1005
1006 /** The style for the current cell (GhosttyStyle). */
1007 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_STYLE = 2,
1008
1009 /** The total number of grapheme codepoints including the base codepoint
1010 * (uint32_t). Returns 0 if the cell has no text. */
1011 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_GRAPHEMES_LEN = 3,
1012
1013 /**
1014 * Copy the cell's grapheme codepoints into a buffer you provide
1015 * (GhosttyCodepointBuffer).
1016 *
1017 * The base codepoint comes first, followed by any extra codepoints. Up
1018 * to cap codepoints are written, and len is set to the total number of
1019 * codepoints in the cell. If they do not all fit, this returns
1020 * GHOSTTY_OUT_OF_SPACE and the buffer holds the first cap of them. A
1021 * cell with no text returns GHOSTTY_SUCCESS with len set to 0.
1022 *
1023 * @code{.c}
1024 * uint32_t codepoints[16];
1025 * GhosttyCodepointBuffer graphemes = {.ptr = codepoints, .cap = 16};
1026 * GhosttyResult result = ghostty_render_state_row_cells_get(
1027 * cells, GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_GRAPHEMES_BUF, &graphemes);
1028 * if (result == GHOSTTY_OUT_OF_SPACE) {
1029 * // The cell has graphemes.len codepoints, but only the first 16
1030 * // were written. Retry with a larger buffer to get the rest.
1031 * }
1032 * @endcode
1033 */
1034 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_GRAPHEMES_BUF = 4,
1035
1036 /** The resolved background color of the cell (GhosttyColorRgb).
1037 * Flattens the three possible sources: content-tag bg_color_rgb,
1038 * content-tag bg_color_palette (looked up in the palette), or the
1039 * style's bg_color.
1040 *
1041 * Returns GHOSTTY_NO_VALUE if the cell has no background color of its
1042 * own. Draw it with your default background, such as the terminal's:
1043 *
1044 * @code{.c}
1045 * GhosttyColorRgb bg;
1046 * GhosttyResult result = ghostty_render_state_row_cells_get(
1047 * cells, GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_BG_COLOR, &bg);
1048 * if (result == GHOSTTY_NO_VALUE) bg = colors.background;
1049 * @endcode
1050 */
1051 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_BG_COLOR = 5,
1052
1053 /** The resolved foreground color of the cell (GhosttyColorRgb).
1054 * Resolves palette indices through the palette. Bold color handling
1055 * is not applied; you should handle bold styling separately.
1056 * Returns GHOSTTY_NO_VALUE if the cell has no foreground color of its
1057 * own. Draw it with your default foreground, such as the terminal's. */
1058 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_FG_COLOR = 6,
1059
1060 /** Whether the cell is contained within the current selection (bool).
1061 * This returns true when the cell's column is within the current row's
1062 * row-local selection range, and false otherwise. Rendering policy for
1063 * selected cells (colors, inversion, etc.) is left to you.
1064 *
1065 * Renderers that can draw cells in spans may be more efficient querying
1066 * GHOSTTY_RENDER_STATE_ROW_DATA_SELECTION once per row and applying that
1067 * range directly, avoiding one C API call per cell for selection state. */
1068 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_SELECTED = 7,
1069
1070 /** Whether the cell has any explicit styling (bool).
1071 * This is equivalent to querying the raw cell's
1072 * GHOSTTY_CELL_DATA_HAS_STYLING value, but avoids materializing the raw
1073 * GhosttyCell for renderers that only need to know whether fetching the
1074 * full style is necessary. */
1075 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_HAS_STYLING = 8,
1076
1077 /**
1078 * Encode the current cell's full grapheme cluster as UTF-8 into a
1079 * caller-provided buffer (GhosttyBuffer).
1080 *
1081 * The base codepoint is encoded first, followed by any extra grapheme
1082 * codepoints. Returns GHOSTTY_SUCCESS with len=0 when the cell has no text.
1083 *
1084 * If ptr is NULL or cap is too small for a non-empty cell, returns
1085 * GHOSTTY_OUT_OF_SPACE without writing any bytes and sets len to the required
1086 * buffer size in bytes.
1087 */
1088 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_GRAPHEMES_UTF8 = 9,
1089 GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
1090} GhosttyRenderStateRowCellsData;
1091
1092/** @name Cells
1093 * @{
1094 */
1095
1096/**
1097 * Create a new row cells instance.
1098 *
1099 * All fields except the allocator are left undefined until populated
1100 * via ghostty_render_state_row_get() with
1101 * GHOSTTY_RENDER_STATE_ROW_DATA_CELLS.
1102 *
1103 * You can reuse this value repeatedly with ghostty_render_state_row_get() to
1104 * avoid allocating a new cells container for every row.
1105 *
1106 * @param allocator Pointer to allocator, or NULL to use the default allocator
1107 * @param[out] out_cells On success, receives the created row cells handle
1108 * @return GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_MEMORY on allocation
1109 * failure, GHOSTTY_INVALID_VALUE if @p out_cells is NULL
1110 *
1111 * @ingroup render
1112 */
1113GHOSTTY_API GhosttyResult ghostty_render_state_row_cells_new(
1114 const GhosttyAllocator* allocator,
1115 GhosttyRenderStateRowCells* out_cells);
1116
1117/**
1118 * Move a render-state row cells iterator to the next cell.
1119 *
1120 * Returns true if the iterator moved successfully and cell data is
1121 * available to read at the new position.
1122 *
1123 * @param cells The row cells handle to advance (may be NULL)
1124 * @return true if advanced to the next cell, false if `cells` is
1125 * NULL or if the iterator has reached the end
1126 *
1127 * @ingroup render
1128 */
1129GHOSTTY_API bool ghostty_render_state_row_cells_next(GhosttyRenderStateRowCells cells);
1130
1131/**
1132 * Move a render-state row cells iterator to a specific column.
1133 *
1134 * Positions the iterator at the given x (column) index so that
1135 * subsequent reads return data for that cell.
1136 *
1137 * @param cells The row cells handle to reposition (NULL returns
1138 * GHOSTTY_INVALID_VALUE)
1139 * @param x The zero-based column index to select
1140 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if `cells`
1141 * is NULL or `x` is out of range
1142 *
1143 * @ingroup render
1144 */
1145GHOSTTY_API GhosttyResult ghostty_render_state_row_cells_select(
1146 GhosttyRenderStateRowCells cells, uint16_t x);
1147
1148/**
1149 * Get a value from the current cell in a render-state row cells iterator.
1150 *
1151 * The `out` pointer must point to a value of the type corresponding to the
1152 * requested data kind (see GhosttyRenderStateRowCellsData).
1153 * Call ghostty_render_state_row_cells_next() or
1154 * ghostty_render_state_row_cells_select() at least once before
1155 * calling this function.
1156 *
1157 * @param cells The row cells handle to query (NULL returns GHOSTTY_INVALID_VALUE)
1158 * @param data The data kind to query
1159 * @param[out] out Pointer to receive the queried value
1160 * @return GHOSTTY_SUCCESS on success, GHOSTTY_NO_VALUE if the cell has no
1161 * value for `data`, GHOSTTY_INVALID_VALUE if `cells` is NULL or the
1162 * iterator is not positioned on a cell, or GHOSTTY_UNSUPPORTED if
1163 * `data` is not known to this version of the library
1164 *
1165 * @ingroup render
1166 */
1167GHOSTTY_API GhosttyResult ghostty_render_state_row_cells_get(
1168 GhosttyRenderStateRowCells cells,
1169 GhosttyRenderStateRowCellsData data,
1170 void* out);
1171
1172/**
1173 * Get multiple data fields from the current cell in a single call.
1174 *
1175 * Use this when each call into the library is expensive, for example from
1176 * WebAssembly or from another language through its foreign function
1177 * interface. It reads every key in one call. From C in the same process it
1178 * costs the same as calling ghostty_render_state_row_cells_get() once per
1179 * key, so use whichever reads better.
1180 *
1181 * Each element in the keys array specifies a data kind, and the
1182 * corresponding element in the values array receives the result.
1183 *
1184 * Processing stops at the first error; on success out_written
1185 * is set to count, on error it is set to the index of the
1186 * failing key (i.e. the number of values successfully written).
1187 *
1188 * @param cells The row cells handle (NULL returns GHOSTTY_INVALID_VALUE)
1189 * @param count Number of key/value pairs
1190 * @param keys Array of data kinds to query
1191 * @param values Array of output pointers (types must match each key's
1192 * documented output type)
1193 * @param[out] out_written On return, receives the number of values
1194 * successfully written (may be NULL)
1195 * @return GHOSTTY_SUCCESS if every key succeeds. Otherwise the result of the
1196 * first key that failed, for example GHOSTTY_UNSUPPORTED for a key
1197 * that is not known to this version of the library.
1198 *
1199 * @ingroup render
1200 */
1201GHOSTTY_API GhosttyResult ghostty_render_state_row_cells_get_multi(
1202 GhosttyRenderStateRowCells cells,
1203 size_t count,
1204 const GhosttyRenderStateRowCellsData* keys,
1205 void** values,
1206 size_t* out_written);
1207
1208/**
1209 * Free a row cells instance.
1210 *
1211 * @param cells The row cells handle to free (may be NULL)
1212 *
1213 * @ingroup render
1214 */
1215GHOSTTY_API void ghostty_render_state_row_cells_free(GhosttyRenderStateRowCells cells);
1216
1217/** @} */
1218
1219/** @name Images
1220 * @{
1221 */
1222
1223/**
1224 * Create a new image iterator.
1225 *
1226 * The iterator starts out empty. Fill it from a render state with
1227 * ghostty_render_state_get() and GHOSTTY_RENDER_STATE_DATA_IMAGE_ITERATOR.
1228 * You can keep one iterator for the lifetime of your renderer and fill it
1229 * again after every update. See @ref guide-images-drawing for a complete
1230 * example.
1231 *
1232 * @param allocator Pointer to allocator, or NULL to use the default allocator
1233 * @param[out] out_iterator On success, receives the created iterator handle
1234 * @return GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_MEMORY on allocation
1235 * failure, GHOSTTY_INVALID_VALUE if @p out_iterator is NULL
1236 *
1237 * @ingroup render
1238 */
1239GHOSTTY_API GhosttyResult ghostty_render_state_image_iterator_new(
1240 const GhosttyAllocator* allocator,
1241 GhosttyRenderStateImageIterator* out_iterator);
1242
1243/**
1244 * Free an image iterator.
1245 *
1246 * @param iterator The iterator handle to free (may be NULL)
1247 *
1248 * @ingroup render
1249 */
1250GHOSTTY_API void ghostty_render_state_image_iterator_free(
1251 GhosttyRenderStateImageIterator iterator);
1252
1253/**
1254 * Move an image iterator to the next image placement.
1255 *
1256 * Call this before reading the first placement, and keep calling it
1257 * until it returns false.
1258 *
1259 * The order of placements is not the order to draw them in. Sort them
1260 * by layer (GHOSTTY_RENDER_STATE_IMAGE_DATA_LAYER) and then by z-index
1261 * (GHOSTTY_RENDER_STATE_IMAGE_DATA_Z) first.
1262 *
1263 * Only placements with something to draw are returned. For example,
1264 * placements of an image the terminal hasn't fully received yet are
1265 * skipped.
1266 *
1267 * The iterator reads from the render state that filled it. After the
1268 * next update of that render state, fill the iterator again before
1269 * using it.
1270 *
1271 * @param iterator The iterator handle (NULL returns false)
1272 * @return true if the iterator moved to a placement, false if there are
1273 * no more placements
1274 *
1275 * @ingroup render
1276 */
1277GHOSTTY_API bool ghostty_render_state_image_iterator_next(
1278 GhosttyRenderStateImageIterator iterator);
1279
1280/**
1281 * Read one value from the current placement of an image iterator.
1282 *
1283 * The `out` pointer must point to a value of the type listed for `data`
1284 * in GhosttyRenderStateImageData. To read several values at once, use
1285 * ghostty_render_state_image_get_multi().
1286 *
1287 * @param iterator The iterator handle, moved to a placement with
1288 * ghostty_render_state_image_iterator_next()
1289 * @param data The value to read
1290 * @param[out] out Pointer that receives the value
1291 * @return GHOSTTY_SUCCESS on success. GHOSTTY_NO_VALUE if `data` only applies
1292 * to another protocol. GHOSTTY_INVALID_VALUE if the iterator is NULL
1293 * or is not on a placement. GHOSTTY_UNSUPPORTED if `data` is not known
1294 * to this version of the library.
1295 *
1296 * @ingroup render
1297 */
1298GHOSTTY_API GhosttyResult ghostty_render_state_image_get(
1299 GhosttyRenderStateImageIterator iterator,
1300 GhosttyRenderStateImageData data,
1301 void* out);
1302
1303/**
1304 * Read several values from the current placement of an image iterator in
1305 * one call.
1306 *
1307 * Use this when each call into the library is expensive, for example from
1308 * WebAssembly or from another language through its foreign function
1309 * interface. It reads every key in one call. From C in the same process it
1310 * costs the same as calling ghostty_render_state_image_get() once per key,
1311 * so use whichever reads better.
1312 *
1313 * `values[i]` receives the value for `keys[i]`. The keys are read in
1314 * order, and reading stops at the first error.
1315 *
1316 * @param iterator The iterator handle, moved to a placement with
1317 * ghostty_render_state_image_iterator_next()
1318 * @param count The number of keys and values
1319 * @param keys The values to read
1320 * @param values Output pointers, one for each key
1321 * @param[out] out_written If not NULL, receives the number of values
1322 * written before any error
1323 * @return GHOSTTY_SUCCESS if every key succeeds. Otherwise the result of the
1324 * first key that failed, for example GHOSTTY_UNSUPPORTED for a key
1325 * that is not known to this version of the library.
1326 *
1327 * @ingroup render
1328 */
1329GHOSTTY_API GhosttyResult ghostty_render_state_image_get_multi(
1330 GhosttyRenderStateImageIterator iterator,
1331 size_t count,
1332 const GhosttyRenderStateImageData* keys,
1333 void** values,
1334 size_t* out_written);
1335
1336/** @} */
1337
1338/** @} */
1339
1340#ifdef __cplusplus
1341}
1342#endif
1343
1344#endif /* GHOSTTY_VT_RENDER_H */