Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

encoder.h

297 lines10.8 KB

1/**
2 * @file encoder.h
3 *
4 * Key event encoding to terminal escape sequences.
5 */
6​
7#ifndef GHOSTTY_VT_KEY_ENCODER_H
8#define GHOSTTY_VT_KEY_ENCODER_H
9​
10#include <stddef.h>
11#include <stdint.h>
12#include <ghostty/vt/types.h>
13#include <ghostty/vt/allocator.h>
14#include <ghostty/vt/terminal.h>
15#include <ghostty/vt/key/event.h>
16​
17/**
18 * Opaque handle to a key encoder instance.
19 *
20 * This handle represents a key encoder that converts key events into terminal
21 * escape sequences.
22 *
23 * @ingroup key
24 */
25typedef struct GhosttyKeyEncoderImpl *GhosttyKeyEncoder;
26​
27/**
28 * Kitty keyboard protocol flags.
29 *
30 * Bitflags representing the various modes of the Kitty keyboard protocol.
31 * These can be combined using bitwise OR operations. Valid values all
32 * start with `GHOSTTY_KITTY_KEY_`.
33 *
34 * @ingroup key
35 */
36typedef uint8_t GhosttyKittyKeyFlags;
37​
38/** Kitty keyboard protocol disabled (all flags off). @ingroup key */
39#define GHOSTTY_KITTY_KEY_DISABLED 0
40​
41/** Disambiguate escape codes. @ingroup key */
42#define GHOSTTY_KITTY_KEY_DISAMBIGUATE (1 << 0)
43​
44/** Report key press and release events. @ingroup key */
45#define GHOSTTY_KITTY_KEY_REPORT_EVENTS (1 << 1)
46​
47/** Report alternate key codes. @ingroup key */
48#define GHOSTTY_KITTY_KEY_REPORT_ALTERNATES (1 << 2)
49​
50/** Report all key events including those normally handled by the terminal. @ingroup key */
51#define GHOSTTY_KITTY_KEY_REPORT_ALL (1 << 3)
52​
53/** Report associated text with key events. @ingroup key */
54#define GHOSTTY_KITTY_KEY_REPORT_ASSOCIATED (1 << 4)
55​
56/** All Kitty keyboard protocol flags enabled. @ingroup key */
57#define GHOSTTY_KITTY_KEY_ALL (GHOSTTY_KITTY_KEY_DISAMBIGUATE | GHOSTTY_KITTY_KEY_REPORT_EVENTS | GHOSTTY_KITTY_KEY_REPORT_ALTERNATES | GHOSTTY_KITTY_KEY_REPORT_ALL | GHOSTTY_KITTY_KEY_REPORT_ASSOCIATED)
58​
59/**
60 * macOS option key behavior.
61 *
62 * Determines whether the "option" key on macOS is treated as "alt" or not.
63 * See the Ghostty `macos-option-as-alt` configuration option for more details.
64 *
65 * @ingroup key
66 */
67typedef enum GHOSTTY_ENUM_TYPED {
68 /** Option key is not treated as alt */
69 GHOSTTY_OPTION_AS_ALT_FALSE = 0,
70 /** Option key is treated as alt */
71 GHOSTTY_OPTION_AS_ALT_TRUE = 1,
72 /** Only left option key is treated as alt */
73 GHOSTTY_OPTION_AS_ALT_LEFT = 2,
74 /** Only right option key is treated as alt */
75 GHOSTTY_OPTION_AS_ALT_RIGHT = 3,
76 GHOSTTY_OPTION_AS_ALT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
77} GhosttyOptionAsAlt;
78​
79/**
80 * Key encoder option identifiers.
81 *
82 * These values are used with ghostty_key_encoder_set() to configure
83 * the behavior of the key encoder.
84 *
85 * @ingroup key
86 */
87typedef enum GHOSTTY_ENUM_TYPED {
88 /**
89 * Not a real option. ghostty_key_encoder_set() returns
90 * GHOSTTY_INVALID_VALUE for it. Its value is 0, so a zeroed or
91 * uninitialized variable is never mistaken for a real option.
92 */
93 GHOSTTY_KEY_ENCODER_OPT_INVALID = 0,
94​
95 /** Terminal DEC mode 1: cursor key application mode (value: bool,
96 * default: false) */
97 GHOSTTY_KEY_ENCODER_OPT_CURSOR_KEY_APPLICATION = 1,
98​
99 /** Terminal DEC mode 66: keypad key application mode (value: bool,
100 * default: false) */
101 GHOSTTY_KEY_ENCODER_OPT_KEYPAD_KEY_APPLICATION = 2,
102​
103 /** Terminal DEC mode 1035: ignore keypad with numlock (value: bool,
104 * default: false) */
105 GHOSTTY_KEY_ENCODER_OPT_IGNORE_KEYPAD_WITH_NUMLOCK = 3,
106​
107 /** Terminal DEC mode 1036: alt sends escape prefix (value: bool,
108 * default: false) */
109 GHOSTTY_KEY_ENCODER_OPT_ALT_ESC_PREFIX = 4,
110​
111 /** xterm modifyOtherKeys mode 2 (value: bool, default: false) */
112 GHOSTTY_KEY_ENCODER_OPT_MODIFY_OTHER_KEYS_STATE_2 = 5,
113​
114 /** Kitty keyboard protocol flags (value: GhosttyKittyKeyFlags bitmask,
115 * default: GHOSTTY_KITTY_KEY_DISABLED) */
116 GHOSTTY_KEY_ENCODER_OPT_KITTY_FLAGS = 6,
117​
118 /** macOS option-as-alt setting (value: GhosttyOptionAsAlt,
119 * default: GHOSTTY_OPTION_AS_ALT_FALSE) */
120 GHOSTTY_KEY_ENCODER_OPT_MACOS_OPTION_AS_ALT = 7,
121​
122 /** Backarrow key mode (value: bool)
123 * See https://vt100.net/dec/ek-vt3xx-tp-002.pdf page 170
124 * If `false` (the default), `backspace` emits 0x7f
125 * If `true`, `backspace` emits 0x08
126 */
127 GHOSTTY_KEY_ENCODER_OPT_BACKARROW_KEY_MODE = 8,
128​
129 GHOSTTY_KEY_ENCODER_OPT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
130} GhosttyKeyEncoderOption;
131​
132/**
133 * Create a new key encoder instance.
134 *
135 * Creates a new key encoder with default options. The encoder can be configured
136 * using ghostty_key_encoder_set() and must be freed using
137 * ghostty_key_encoder_free() when no longer needed.
138 *
139 * @param allocator Pointer to the allocator to use for memory management, or NULL to use the default allocator
140 * @param encoder Pointer to store the created encoder handle (NULL returns
141 * GHOSTTY_INVALID_VALUE)
142 * @return GHOSTTY_SUCCESS on success, or an error code on failure
143 *
144 * @ingroup key
145 */
146GHOSTTY_API GhosttyResult ghostty_key_encoder_new(const GhosttyAllocator *allocator, GhosttyKeyEncoder *encoder);
147​
148/**
149 * Free a key encoder instance.
150 *
151 * Releases all resources associated with the key encoder. After this call,
152 * the encoder handle becomes invalid and must not be used.
153 *
154 * @param encoder The encoder handle to free (may be NULL)
155 *
156 * @ingroup key
157 */
158GHOSTTY_API void ghostty_key_encoder_free(GhosttyKeyEncoder encoder);
159​
160/**
161 * Set an option on the key encoder.
162 *
163 * Configures the behavior of the key encoder. Options control various aspects
164 * of encoding such as terminal modes (cursor key application mode, keypad mode),
165 * protocol selection (Kitty keyboard protocol flags), and platform-specific
166 * behaviors (macOS option-as-alt).
167 *
168 * If you are using a terminal instance, you can set the key encoding
169 * options based on the active terminal state (e.g. legacy vs Kitty mode
170 * and associated flags) with ghostty_key_encoder_set_from_terminal().
171 *
172 * Passing NULL for `value` resets the option to its default, the same
173 * value a new encoder starts with. Each option's default is listed on
174 * GhosttyKeyEncoderOption.
175 *
176 * @code{.c}
177 * bool enabled = true;
178 * ghostty_key_encoder_set(
179 * encoder, GHOSTTY_KEY_ENCODER_OPT_CURSOR_KEY_APPLICATION, &enabled);
180 *
181 * // Later, put the option back to its default (false).
182 * ghostty_key_encoder_set(
183 * encoder, GHOSTTY_KEY_ENCODER_OPT_CURSOR_KEY_APPLICATION, NULL);
184 * @endcode
185 *
186 * @param encoder The encoder handle (NULL returns GHOSTTY_INVALID_VALUE)
187 * @param option The option to set
188 * @param value Pointer to the value to set (type depends on the option), or
189 * NULL to reset the option to its default
190 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if `encoder` is
191 * NULL or the value is out of range (such as an unknown
192 * GhosttyOptionAsAlt), or GHOSTTY_UNSUPPORTED if `option` is not
193 * known to this version of the library
194 *
195 * @ingroup key
196 */
197GHOSTTY_API GhosttyResult ghostty_key_encoder_set(GhosttyKeyEncoder encoder, GhosttyKeyEncoderOption option, const void *value);
198​
199/**
200 * Set encoder options from a terminal's current state.
201 *
202 * Reads the terminal's current modes and flags and applies them to the
203 * encoder's options. This sets cursor key application mode, keypad mode,
204 * backarrow key mode, ignore-keypad-with-numlock, alt escape prefix,
205 * modifyOtherKeys state, and Kitty keyboard protocol flags from the terminal
206 * state.
207 *
208 * Note that the `macos_option_as_alt` option cannot be determined from
209 * terminal state and is reset to `GHOSTTY_OPTION_AS_ALT_FALSE` by this
210 * call. Use ghostty_key_encoder_set() to set it afterward if needed.
211 *
212 * @param encoder The encoder handle
213 * @param terminal The terminal handle
214 * @return GHOSTTY_SUCCESS on success, or GHOSTTY_INVALID_VALUE if `encoder`
215 * or `terminal` is NULL. On error the encoder is not changed.
216 *
217 * @ingroup key
218 */
219GHOSTTY_API GhosttyResult ghostty_key_encoder_set_from_terminal(GhosttyKeyEncoder encoder, GhosttyTerminal terminal);
220​
221/**
222 * Encode a key event into a terminal escape sequence.
223 *
224 * Converts a key event into the appropriate terminal escape sequence based on
225 * the encoder's current options. The sequence is written to the provided buffer.
226 *
227 * Not all key events produce output. For example, unmodified modifier keys
228 * typically don't generate escape sequences. For those events this returns
229 * GHOSTTY_SUCCESS with @p out_written set to 0.
230 *
231 * If @p buf is too small, this returns GHOSTTY_OUT_OF_SPACE and sets
232 * @p out_written to the size needed. The buffer may then hold part of the
233 * sequence, which you should discard. To get the size without encoding
234 * anything, pass NULL for @p buf and 0 for @p buf_len. An event with no
235 * output still returns GHOSTTY_SUCCESS in that case, because it needs no
236 * space.
237 *
238 * @param encoder The encoder handle (NULL returns GHOSTTY_INVALID_VALUE)
239 * @param event The key event to encode (NULL returns GHOSTTY_INVALID_VALUE)
240 * @param buf Buffer to write the sequence into. May be NULL if @p buf_len
241 * is 0.
242 * @param buf_len Size of @p buf in bytes
243 * @param[out] out_written The number of bytes written, or the size needed
244 * if the result is GHOSTTY_OUT_OF_SPACE. NULL returns GHOSTTY_INVALID_VALUE.
245 * @return GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_SPACE if buffer too small, or other error code
246 *
247 * ## Example: Calculate required buffer size
248 *
249 * @code{.c}
250 * // Query the required size with a NULL buffer
251 * size_t required = 0;
252 * GhosttyResult result = ghostty_key_encoder_encode(encoder, event, NULL, 0, &required);
253 * if (result == GHOSTTY_SUCCESS) {
254 * // This event produces no output, so there is nothing to send
255 * return;
256 * }
257 * assert(result == GHOSTTY_OUT_OF_SPACE);
258 *
259 * // Allocate buffer of required size
260 * uint8_t *buf = malloc(required);
261 *
262 * // Encode with properly sized buffer
263 * size_t written = 0;
264 * result = ghostty_key_encoder_encode(encoder, event, buf, required, &written);
265 * assert(result == GHOSTTY_SUCCESS);
266 *
267 * // Use the encoded sequence...
268 *
269 * free(buf);
270 * @endcode
271 *
272 * ## Example: Direct encoding with static buffer
273 *
274 * @code{.c}
275 * // Most escape sequences are short, so a static buffer often suffices
276 * uint8_t buf[128];
277 * size_t written = 0;
278 * GhosttyResult result = ghostty_key_encoder_encode(encoder, event, buf, sizeof(buf), &written);
279 *
280 * if (result == GHOSTTY_SUCCESS) {
281 * // Write the encoded sequence to the terminal
282 * write(pty_fd, buf, written);
283 * } else if (result == GHOSTTY_OUT_OF_SPACE) {
284 * // Buffer too small, written contains required size
285 * uint8_t *dynamic_buf = malloc(written);
286 * result = ghostty_key_encoder_encode(encoder, event, dynamic_buf, written, &written);
287 * assert(result == GHOSTTY_SUCCESS);
288 * write(pty_fd, dynamic_buf, written);
289 * free(dynamic_buf);
290 * }
291 * @endcode
292 *
293 * @ingroup key
294 */
295GHOSTTY_API GhosttyResult ghostty_key_encoder_encode(GhosttyKeyEncoder encoder, GhosttyKeyEvent event, uint8_t *buf, size_t buf_len, size_t *out_written);
296​
297#endif /* GHOSTTY_VT_KEY_ENCODER_H */