Open History to see the last commit
1/**
2 * @file sys.h
3 *
4 * System interface - runtime-swappable implementations for external dependencies.
5 */
6
7#ifndef GHOSTTY_VT_SYS_H
8#define GHOSTTY_VT_SYS_H
9
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13#include <ghostty/vt/types.h>
14#include <ghostty/vt/allocator.h>
15
16/** @addtogroup sys
17 *
18 * The system interface is a set of process-global function pointers for things
19 * the library can't do by itself, such as decoding a PNG.
20 *
21 * Setting one enables the feature that depends on it. For example, setting a
22 * PNG decoder enables PNG image support in the Kitty Graphics Protocol. Set
23 * them at startup with ghostty_sys_set() and a `GhosttySysOption`. It is not
24 * thread-safe, so call it before any other thread uses the library, never while
25 * one does. Passing NULL clears an implementation and disables the feature.
26 *
27 * ## Example
28 *
29 * @snippet c-vt-kitty-graphics/src/main.c kitty-graphics-decode-png
30 *
31 * Full program: @ref c-vt-kitty-graphics/src/main.c
32 *
33 * See @ref guide-images-png for how to write a PNG decoder.
34 *
35 * @{
36 */
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/**
43 * A decoded image, filled in by a decode callback such as
44 * GhosttySysDecodePngFn.
45 *
46 * Pixels are 8-bit RGBA: four bytes per pixel, stored row by row starting
47 * at the top-left corner, with no padding between rows. A complete image
48 * is therefore `width * height * 4` bytes long.
49 *
50 * The pixel buffer must be allocated with the allocator passed to the
51 * decode callback. When the callback returns true, the library takes
52 * ownership of the buffer and frees it with that same allocator.
53 */
54typedef struct {
55 /** Image width in pixels. */
56 uint32_t width;
57
58 /** Image height in pixels. */
59 uint32_t height;
60
61 /**
62 * The decoded RGBA pixels, allocated with the allocator passed to
63 * the decode callback.
64 */
65 uint8_t* data;
66
67 /**
68 * Length of `data` in bytes. This must be the exact size that was
69 * requested from the allocator, because the library uses it to free
70 * the buffer.
71 */
72 size_t data_len;
73} GhosttySysImage;
74
75/**
76 * Log severity levels for the log callback.
77 */
78typedef enum GHOSTTY_ENUM_TYPED {
79 GHOSTTY_SYS_LOG_LEVEL_ERROR = 0,
80 GHOSTTY_SYS_LOG_LEVEL_WARNING = 1,
81 GHOSTTY_SYS_LOG_LEVEL_INFO = 2,
82 GHOSTTY_SYS_LOG_LEVEL_DEBUG = 3,
83 GHOSTTY_SYS_LOG_LEVEL_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
84} GhosttySysLogLevel;
85
86/**
87 * Callback type for logging.
88 *
89 * When installed, internal library log messages are delivered through
90 * this callback instead of being discarded. You are responsible
91 * for formatting and routing log output.
92 *
93 * @p scope is the log scope name as UTF-8 bytes (e.g. "osc", "kitty").
94 * When the log is unscoped (default scope), @p scope_len is 0.
95 *
96 * All pointer arguments are only valid for the duration of the callback.
97 * The callback must be safe to call from any thread.
98 *
99 * @param userdata The userdata pointer set via GHOSTTY_SYS_OPT_USERDATA
100 * @param level The severity level of the log message
101 * @param scope Pointer to the scope name bytes
102 * @param scope_len Length of the scope name in bytes
103 * @param message Pointer to the log message bytes
104 * @param message_len Length of the log message in bytes
105 */
106typedef void (*GhosttySysLogFn)(
107 void* userdata,
108 GhosttySysLogLevel level,
109 const uint8_t* scope,
110 size_t scope_len,
111 const uint8_t* message,
112 size_t message_len);
113
114/**
115 * Callback type for PNG decoding.
116 *
117 * The library calls this when it receives a PNG image and needs the raw
118 * pixels. The callback decodes the PNG bytes in @p data and describes
119 * the result in @p out. See the example in the @ref sys overview for a
120 * complete callback.
121 *
122 * ### On success
123 *
124 * Allocate the pixel buffer with ghostty_alloc() and @p allocator, write
125 * the decoded pixels into it, set `width`, `height`, `data` and
126 * `data_len` in @p out, and return true. The library then owns the
127 * buffer and frees it with the same allocator. See GhosttySysImage for
128 * the expected pixel layout.
129 *
130 * The allocator limits how much memory a single image may use, so
131 * ghostty_alloc() can return NULL for very large images. Treat that as
132 * a failure.
133 *
134 * ### On failure
135 *
136 * Free anything that was allocated and return false. The library does
137 * not read @p out in this case, and the image is rejected.
138 *
139 * ### The output struct starts zeroed
140 *
141 * The library sets every field of @p out to zero before it calls the
142 * callback, so any field the callback does not write reads as zero.
143 * This has two practical effects:
144 *
145 * - If the callback returns true but `data` is still NULL, the library
146 * treats the call as a failure.
147 * - Language bindings can store a pointer into @p out directly. Some
148 * runtimes, such as Go, require memory to be initialized before a
149 * pointer is written into it, and this guarantee satisfies that
150 * requirement.
151 *
152 * Only @p out is zeroed. Memory returned by ghostty_alloc() is not.
153 *
154 * ### The output struct never changes
155 *
156 * GhosttySysImage is frozen. If a later version of libghostty-vt needs
157 * more fields, it adds a new callback typedef and a new option key, and
158 * keeps calling this callback with GhosttySysImage forever. A callback
159 * written against this header keeps working with every later version of
160 * the library.
161 *
162 * @p data and @p allocator are only valid for the duration of the
163 * callback.
164 *
165 * Use @p allocator only through ghostty_alloc() and ghostty_free().
166 * Dereferencing its vtable is not supported.
167 *
168 * @param userdata The userdata pointer set via GHOSTTY_SYS_OPT_USERDATA
169 * @param allocator The allocator to use for the output pixel buffer
170 * @param data Pointer to the raw PNG data
171 * @param data_len Length of the raw PNG data in bytes
172 * @param[out] out The decoded image. Zeroed by the library before the
173 * call, and filled in by the callback on success.
174 * @return true if the image was decoded and @p out was filled in,
175 * false on failure
176 */
177typedef bool (*GhosttySysDecodePngFn)(
178 void* userdata,
179 const GhosttyAllocator* allocator,
180 const uint8_t* data,
181 size_t data_len,
182 GhosttySysImage* out);
183
184/**
185 * Callback type for secure random bytes.
186 *
187 * Fills @p buf with @p len cryptographically secure random bytes. The
188 * library uses this for secrets, so it must be a real CSPRNG (getrandom,
189 * arc4random_buf, BCryptGenRandom, crypto.getRandomValues, ...); a
190 * predictable source is a security hole.
191 *
192 * @param userdata The userdata pointer set via GHOSTTY_SYS_OPT_USERDATA
193 * @param buf Buffer to fill
194 * @param len Number of bytes to fill
195 * @return true if the buffer was filled, false if no entropy is available
196 */
197typedef bool (*GhosttySysRandomSecureFn)(
198 void* userdata,
199 uint8_t* buf,
200 size_t len);
201
202/**
203 * System option identifiers for ghostty_sys_set().
204 */
205typedef enum GHOSTTY_ENUM_TYPED {
206 /**
207 * Not a real option. ghostty_sys_set() returns GHOSTTY_INVALID_VALUE for
208 * it. Its value is 0, so a zeroed or uninitialized variable is never
209 * mistaken for a real option.
210 */
211 GHOSTTY_SYS_OPT_INVALID = 0,
212
213 /**
214 * Set the userdata pointer passed to all sys callbacks.
215 *
216 * Input type: void* (or NULL)
217 */
218 GHOSTTY_SYS_OPT_USERDATA = 1,
219
220 /**
221 * Set the PNG decode function.
222 *
223 * When set, the terminal can accept PNG images via the Kitty
224 * Graphics Protocol. When cleared (NULL value), PNG decoding is
225 * unsupported and PNG image data will be rejected.
226 *
227 * Input type: GhosttySysDecodePngFn (function pointer, or NULL)
228 */
229 GHOSTTY_SYS_OPT_DECODE_PNG = 2,
230
231 /**
232 * Set the log callback.
233 *
234 * When set, internal library log messages are delivered to this
235 * callback. When cleared (NULL value), log messages are silently
236 * discarded.
237 *
238 * Use ghostty_sys_log_stderr as a convenience callback that
239 * writes formatted messages to stderr.
240 *
241 * Which log levels are emitted depends on the build mode of the
242 * library and is not configurable at runtime. Debug builds emit
243 * all levels (debug and above). Release builds emit info and
244 * above; debug-level messages are compiled out entirely and will
245 * never reach the callback.
246 *
247 * Input type: GhosttySysLogFn (function pointer, or NULL)
248 */
249 GHOSTTY_SYS_OPT_LOG = 3,
250
251 /**
252 * Override the secure random source.
253 *
254 * By default the library draws secure random bytes from the
255 * platform (getrandom or arc4random_buf on POSIX, CNG on Windows).
256 * Targets without one, such as wasm32-freestanding, have no default
257 * and operations that need entropy fail with GHOSTTY_IO_ERROR until
258 * this is set. When set,
259 * it is used instead of the platform source on every target. When
260 * cleared (NULL value), the platform default is restored.
261 *
262 * Input type: GhosttySysRandomSecureFn (function pointer, or NULL)
263 */
264 GHOSTTY_SYS_OPT_RANDOM_SECURE = 4,
265 GHOSTTY_SYS_OPT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
266} GhosttySysOption;
267
268/**
269 * Set a system-level option.
270 *
271 * Configures a process-global implementation function. These should be
272 * set once at startup before using any terminal functionality that
273 * depends on them.
274 *
275 * @param option The option to set
276 * @param value Pointer to the value (type depends on the option),
277 * or NULL to clear it
278 * @return GHOSTTY_SUCCESS on success, or GHOSTTY_UNSUPPORTED if the
279 * option is not known to this build (see GhosttyResult)
280 */
281GHOSTTY_API GhosttyResult ghostty_sys_set(GhosttySysOption option,
282 const void* value);
283
284/**
285 * Built-in log callback that writes to stderr.
286 *
287 * Formats each message as "[level](scope): message\n".
288 * Can be passed directly to ghostty_sys_set():
289 *
290 * @code
291 * ghostty_sys_set(GHOSTTY_SYS_OPT_LOG, &ghostty_sys_log_stderr);
292 * @endcode
293 */
294GHOSTTY_API void ghostty_sys_log_stderr(void* userdata,
295 GhosttySysLogLevel level,
296 const uint8_t* scope,
297 size_t scope_len,
298 const uint8_t* message,
299 size_t message_len);
300
301#ifdef __cplusplus
302}
303#endif
304
305/** @} */
306
307#endif /* GHOSTTY_VT_SYS_H */