libghostty: C APIs for Kitty Graphics inspection#12145
Merged
Conversation
Add a C API for iterating over Kitty graphics placements via the new GhosttyKittyGraphics opaque handle. The API follows the same pattern as the render state row iterator: allocate an iterator with ghostty_kitty_graphics_placement_iterator_new, populate it from a graphics handle via ghostty_kitty_graphics_get with the PLACEMENT_ITERATOR data kind, advance with ghostty_kitty_graphics_placement_next, and query per-placement fields with ghostty_kitty_graphics_placement_get.
Add a GhosttyKittyGraphicsImage opaque type and API for looking up images by ID and querying their properties. This complements the existing placement iterator by allowing direct image introspection. The new ghostty_kitty_graphics_image() function looks up an image by its ID from the storage, returning a borrowed opaque handle. Properties are queried via ghostty_kitty_image_get() using the new GhosttyKittyGraphicsImageData enum, which exposes id, number, width, height, format, compression, and a borrowed data pointer with length. Format and compression are exposed as their own C enum types (GhosttyKittyImageFormat and GhosttyKittyImageCompression) rather than raw integers.
Convert the Transmission.Format, Transmission.Medium, and Transmission.Compression types from plain Zig enums to lib.Enum so they get a C-compatible backing type when building with c_abi. This lets the C API layer reuse the types directly instead of maintaining separate mirror enums. Move Format.bpp() to a standalone Transmission.formatBpp() function since lib.Enum types cannot have decls. In the C API layer, rename kitty_gfx to kitty_storage and command to kitty_cmd for clarity, and simplify the format/compression getters to direct assignment now that the types are shared.
Expose Placement.rect() from the Zig kitty graphics storage as a new C API function ghostty_kitty_graphics_placement_rect(). It takes the terminal, image handle, and a positioned placement iterator, and writes the bounding grid rectangle into a GhosttySelection out param. Virtual placements return GHOSTTY_NO_VALUE. Move all opaque handle typedefs (GhosttyTerminal, GhosttyKittyGraphics, GhosttyRenderState, GhosttySgrParser, GhosttyFormatter, GhosttyOsc*) into types.h so they are available everywhere without circular includes and Doxygen renders them in the correct @InGroup sections.
…Size Expose Placement.pixelSize() and Placement.gridSize() as new C API functions ghostty_kitty_graphics_placement_pixel_size() and ghostty_kitty_graphics_placement_grid_size(). Both take the placement iterator, image handle, and terminal, returning their results via out params. Rename the internal Zig method from calculatedSize to pixelSize to pair naturally with gridSize — one returns pixels, the other grid cells. Updated all callers including the renderer.
The test transmits an image with f=24 (24-bit RGB) but was asserting that the format field equals .rgba (32-bit). Corrected the expectation to .rgb to match the transmitted pixel format.
…image_get Rename the public API function to follow the consistent ghostty_kitty_graphics_* naming convention used by the other kitty graphics API symbols.
The PlacementIterator, PlacementMap, and PlacementIteratorWrapper types in the C API were unconditionally referencing kitty_storage.ImageStorage, which transitively pulled in Image.transmit_time (std.time.Instant). On wasm32-freestanding, std.time.Instant requires posix.timespec which does not exist, causing a compilation error. Gate these types behind build_options.kitty_graphics, matching the existing pattern used for KittyGraphics and ImageHandle. When kitty graphics is disabled, they fall back to opaque/void types. Add early-return guards to placement_iterator_new and placement_iterator_free which directly operate on the wrapper struct.
Add the inverse of ghostty_terminal_grid_ref(), converting a grid reference back to coordinates in a requested coordinate system (active, viewport, screen, or history). This wraps the existing internal PageList.pointFromPin and is placed on the terminal API since it requires terminal-owned PageList state to resolve the top-left anchor for each coordinate system. Returns GHOSTTY_NO_VALUE when the ref falls outside the requested range, e.g. a scrollback ref cannot be expressed in active coordinates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a C API for inspecting Kitty graphics image storage, images, and placements from a terminal instance.
I think this is enough of the API surface area for a renderer to draw images. But I'll have to add it to Ghostling to be sure.
Example
API
Functions
ghostty_kitty_graphics_getghostty_kitty_graphics_imageghostty_kitty_graphics_image_getghostty_kitty_graphics_placement_iterator_newghostty_kitty_graphics_placement_iterator_freeghostty_kitty_graphics_placement_nextghostty_kitty_graphics_placement_getghostty_kitty_graphics_placement_rectghostty_kitty_graphics_placement_pixel_sizeghostty_kitty_graphics_placement_grid_sizeTypes
GhosttyKittyGraphicsGhosttyKittyGraphicsImageGhosttyKittyGraphicsPlacementIteratorGhosttyKittyGraphicsDataghostty_kitty_graphics_getdata kindsGhosttyKittyGraphicsImageDataghostty_kitty_image_getdata kindsGhosttyKittyGraphicsPlacementDataghostty_kitty_graphics_placement_getdata kindsGhosttyKittyImageFormatGhosttyKittyImageCompression