libghostty-vt 0.0.0
Loading...
Searching...
No Matches
System Interface

Plug in your own system functions, such as a PNG decoder. More...

Detailed Description

Plug in your own system functions, such as a PNG decoder.

The system interface is a set of process-global function pointers for things the library can't do by itself, such as decoding a PNG.

Setting one enables the feature that depends on it. For example, setting a PNG decoder enables PNG image support in the Kitty Graphics Protocol. Set them at startup with ghostty_sys_set() and a GhosttySysOption. It is not thread-safe, so call it before any other thread uses the library, never while one does. Passing NULL clears an implementation and disables the feature.

Example

bool decode_png(void* userdata,
const GhosttyAllocator* allocator,
const uint8_t* data,
size_t data_len,
int* count = (int*)userdata;
(*count)++;
printf(" decode_png called (size=%zu, call #%d)\n", data_len, *count);
/* Allocate RGBA pixel data through the provided allocator. */
const size_t pixel_len = 4; /* 1x1 RGBA */
uint8_t* pixels = ghostty_alloc(allocator, pixel_len);
if (!pixels) return false;
/* Fill with red (R=255, G=0, B=0, A=255). */
pixels[0] = 255;
pixels[1] = 0;
pixels[2] = 0;
pixels[3] = 255;
out->width = 1;
out->height = 1;
out->data = pixels;
out->data_len = pixel_len;
return true;
}

Full program: c-vt-kitty-graphics/src/main.c

See Decoding PNGs for how to write a PNG decoder.

Typedefs

typedef void(* GhosttySysLogFn) (void *userdata, GhosttySysLogLevel level, const uint8_t *scope, size_t scope_len, const uint8_t *message, size_t message_len)
typedef bool(* GhosttySysDecodePngFn) (void *userdata, const GhosttyAllocator *allocator, const uint8_t *data, size_t data_len, GhosttySysImage *out)
typedef bool(* GhosttySysRandomSecureFn) (void *userdata, uint8_t *buf, size_t len)

Enumerations

enum  GhosttySysLogLevel
enum  GhosttySysOption {
  GHOSTTY_SYS_OPT_INVALID = 0 , GHOSTTY_SYS_OPT_USERDATA = 1 , GHOSTTY_SYS_OPT_DECODE_PNG = 2 , GHOSTTY_SYS_OPT_LOG = 3 ,
  GHOSTTY_SYS_OPT_RANDOM_SECURE = 4 , GHOSTTY_SYS_OPT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE
}

Functions

GHOSTTY_API GhosttyResult ghostty_sys_set (GhosttySysOption option, const void *value)
GHOSTTY_API void ghostty_sys_log_stderr (void *userdata, GhosttySysLogLevel level, const uint8_t *scope, size_t scope_len, const uint8_t *message, size_t message_len)

Data Structures

struct  GhosttySysImage

Typedef Documentation

◆ GhosttySysDecodePngFn

typedef bool(* GhosttySysDecodePngFn) (void *userdata, const GhosttyAllocator *allocator, const uint8_t *data, size_t data_len, GhosttySysImage *out)

Callback type for PNG decoding.

The library calls this when it receives a PNG image and needs the raw pixels. The callback decodes the PNG bytes in data and describes the result in out. See the example in the System Interface overview for a complete callback.

On success

Allocate the pixel buffer with ghostty_alloc() and allocator, write the decoded pixels into it, set width, height, data and data_len in out, and return true. The library then owns the buffer and frees it with the same allocator. See GhosttySysImage for the expected pixel layout.

The allocator limits how much memory a single image may use, so ghostty_alloc() can return NULL for very large images. Treat that as a failure.

On failure

Free anything that was allocated and return false. The library does not read out in this case, and the image is rejected.

The output struct starts zeroed

The library sets every field of out to zero before it calls the callback, so any field the callback does not write reads as zero. This has two practical effects:

  • If the callback returns true but data is still NULL, the library treats the call as a failure.
  • Language bindings can store a pointer into out directly. Some runtimes, such as Go, require memory to be initialized before a pointer is written into it, and this guarantee satisfies that requirement.

Only out is zeroed. Memory returned by ghostty_alloc() is not.

The output struct never changes

GhosttySysImage is frozen. If a later version of libghostty-vt needs more fields, it adds a new callback typedef and a new option key, and keeps calling this callback with GhosttySysImage forever. A callback written against this header keeps working with every later version of the library.

data and allocator are only valid for the duration of the callback.

Use allocator only through ghostty_alloc() and ghostty_free(). Dereferencing its vtable is not supported.

Parameters
userdataThe userdata pointer set via GHOSTTY_SYS_OPT_USERDATA
allocatorThe allocator to use for the output pixel buffer
dataPointer to the raw PNG data
data_lenLength of the raw PNG data in bytes
[out]outThe decoded image. Zeroed by the library before the call, and filled in by the callback on success.
Returns
true if the image was decoded and out was filled in, false on failure

Definition at line 177 of file sys.h.

◆ GhosttySysLogFn

typedef void(* GhosttySysLogFn) (void *userdata, GhosttySysLogLevel level, const uint8_t *scope, size_t scope_len, const uint8_t *message, size_t message_len)

Callback type for logging.

When installed, internal library log messages are delivered through this callback instead of being discarded. You are responsible for formatting and routing log output.

scope is the log scope name as UTF-8 bytes (e.g. "osc", "kitty"). When the log is unscoped (default scope), scope_len is 0.

All pointer arguments are only valid for the duration of the callback. The callback must be safe to call from any thread.

Parameters
userdataThe userdata pointer set via GHOSTTY_SYS_OPT_USERDATA
levelThe severity level of the log message
scopePointer to the scope name bytes
scope_lenLength of the scope name in bytes
messagePointer to the log message bytes
message_lenLength of the log message in bytes

Definition at line 106 of file sys.h.

◆ GhosttySysRandomSecureFn

typedef bool(* GhosttySysRandomSecureFn) (void *userdata, uint8_t *buf, size_t len)

Callback type for secure random bytes.

Fills buf with len cryptographically secure random bytes. The library uses this for secrets, so it must be a real CSPRNG (getrandom, arc4random_buf, BCryptGenRandom, crypto.getRandomValues, ...); a predictable source is a security hole.

Parameters
userdataThe userdata pointer set via GHOSTTY_SYS_OPT_USERDATA
bufBuffer to fill
lenNumber of bytes to fill
Returns
true if the buffer was filled, false if no entropy is available

Definition at line 197 of file sys.h.

Enumeration Type Documentation

◆ GhosttySysLogLevel

Log severity levels for the log callback.

Definition at line 78 of file sys.h.

◆ GhosttySysOption

System option identifiers for ghostty_sys_set().

Enumerator
GHOSTTY_SYS_OPT_INVALID 

Not a real option. ghostty_sys_set() returns GHOSTTY_INVALID_VALUE for it. Its value is 0, so a zeroed or uninitialized variable is never mistaken for a real option.

GHOSTTY_SYS_OPT_USERDATA 

Set the userdata pointer passed to all sys callbacks.

Input type: void* (or NULL)

GHOSTTY_SYS_OPT_DECODE_PNG 

Set the PNG decode function.

When set, the terminal can accept PNG images via the Kitty Graphics Protocol. When cleared (NULL value), PNG decoding is unsupported and PNG image data will be rejected.

Input type: GhosttySysDecodePngFn (function pointer, or NULL)

GHOSTTY_SYS_OPT_LOG 

Set the log callback.

When set, internal library log messages are delivered to this callback. When cleared (NULL value), log messages are silently discarded.

Use ghostty_sys_log_stderr as a convenience callback that writes formatted messages to stderr.

Which log levels are emitted depends on the build mode of the library and is not configurable at runtime. Debug builds emit all levels (debug and above). Release builds emit info and above; debug-level messages are compiled out entirely and will never reach the callback.

Input type: GhosttySysLogFn (function pointer, or NULL)

GHOSTTY_SYS_OPT_RANDOM_SECURE 

Override the secure random source.

By default the library draws secure random bytes from the platform (getrandom or arc4random_buf on POSIX, CNG on Windows). Targets without one, such as wasm32-freestanding, have no default and operations that need entropy fail with GHOSTTY_IO_ERROR until this is set. When set, it is used instead of the platform source on every target. When cleared (NULL value), the platform default is restored.

Input type: GhosttySysRandomSecureFn (function pointer, or NULL)

Definition at line 205 of file sys.h.

Function Documentation

◆ ghostty_sys_log_stderr()

GHOSTTY_API void ghostty_sys_log_stderr ( void * userdata,
GhosttySysLogLevel level,
const uint8_t * scope,
size_t scope_len,
const uint8_t * message,
size_t message_len )

Built-in log callback that writes to stderr.

Formats each message as "[level](scope): message\n". Can be passed directly to ghostty_sys_set():

GHOSTTY_API GhosttyResult ghostty_sys_set(GhosttySysOption option, const void *value)
GHOSTTY_API void ghostty_sys_log_stderr(void *userdata, GhosttySysLogLevel level, const uint8_t *scope, size_t scope_len, const uint8_t *message, size_t message_len)
@ GHOSTTY_SYS_OPT_LOG
Definition sys.h:249

◆ ghostty_sys_set()

GHOSTTY_API GhosttyResult ghostty_sys_set ( GhosttySysOption option,
const void * value )

Set a system-level option.

Configures a process-global implementation function. These should be set once at startup before using any terminal functionality that depends on them.

Parameters
optionThe option to set
valuePointer to the value (type depends on the option), or NULL to clear it
Returns
GHOSTTY_SUCCESS on success, or GHOSTTY_UNSUPPORTED if the option is not known to this build (see GhosttyResult)
Examples
c-vt-kitty-graphics/src/main.c.