Open History to see the last commit
1/**
2 * @file encoder.h
3 *
4 * Mouse event encoding to terminal escape sequences.
5 */
6
7#ifndef GHOSTTY_VT_MOUSE_ENCODER_H
8#define GHOSTTY_VT_MOUSE_ENCODER_H
9
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13#include <ghostty/vt/allocator.h>
14#include <ghostty/vt/mouse/event.h>
15#include <ghostty/vt/terminal.h>
16#include <ghostty/vt/types.h>
17
18/**
19 * Opaque handle to a mouse encoder instance.
20 *
21 * This handle represents a mouse encoder that converts normalized
22 * mouse events into terminal escape sequences.
23 *
24 * @ingroup mouse
25 */
26typedef struct GhosttyMouseEncoderImpl *GhosttyMouseEncoder;
27
28/**
29 * Mouse tracking mode.
30 *
31 * @ingroup mouse
32 */
33typedef enum GHOSTTY_ENUM_TYPED {
34 /** Mouse reporting disabled. */
35 GHOSTTY_MOUSE_TRACKING_NONE = 0,
36
37 /** X10 mouse mode. */
38 GHOSTTY_MOUSE_TRACKING_X10 = 1,
39
40 /** Normal mouse mode (button press/release only). */
41 GHOSTTY_MOUSE_TRACKING_NORMAL = 2,
42
43 /** Button-event tracking mode. */
44 GHOSTTY_MOUSE_TRACKING_BUTTON = 3,
45
46 /** Any-event tracking mode. */
47 GHOSTTY_MOUSE_TRACKING_ANY = 4,
48 GHOSTTY_MOUSE_TRACKING_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
49} GhosttyMouseTrackingMode;
50
51/**
52 * Mouse output format.
53 *
54 * @ingroup mouse
55 */
56typedef enum GHOSTTY_ENUM_TYPED {
57 GHOSTTY_MOUSE_FORMAT_X10 = 0,
58 GHOSTTY_MOUSE_FORMAT_UTF8 = 1,
59 GHOSTTY_MOUSE_FORMAT_SGR = 2,
60 GHOSTTY_MOUSE_FORMAT_URXVT = 3,
61 GHOSTTY_MOUSE_FORMAT_SGR_PIXELS = 4,
62 GHOSTTY_MOUSE_FORMAT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
63} GhosttyMouseFormat;
64
65/**
66 * Mouse encoder size and geometry context.
67 *
68 * This describes the rendered terminal geometry used to convert
69 * surface-space positions into encoded coordinates.
70 *
71 * This is a sized struct. Initialize it with GHOSTTY_INIT_SIZED(), then set
72 * the fields you need. See @ref sized-structs.
73 *
74 * @ingroup mouse
75 */
76typedef struct {
77 /** Size of this struct in bytes. Must be set to sizeof(GhosttyMouseEncoderSize). */
78 size_t size;
79
80 /** Full screen width in pixels. */
81 uint32_t screen_width;
82
83 /** Full screen height in pixels. */
84 uint32_t screen_height;
85
86 /** Cell width in pixels. Must be non-zero. */
87 uint32_t cell_width;
88
89 /** Cell height in pixels. Must be non-zero. */
90 uint32_t cell_height;
91
92 /** Top padding in pixels. */
93 uint32_t padding_top;
94
95 /** Bottom padding in pixels. */
96 uint32_t padding_bottom;
97
98 /** Right padding in pixels. */
99 uint32_t padding_right;
100
101 /** Left padding in pixels. */
102 uint32_t padding_left;
103} GhosttyMouseEncoderSize;
104
105/**
106 * Mouse encoder option identifiers.
107 *
108 * These values are used with ghostty_mouse_encoder_set() to configure
109 * the behavior of the mouse encoder.
110 *
111 * @ingroup mouse
112 */
113typedef enum GHOSTTY_ENUM_TYPED {
114 /**
115 * Not a real option. ghostty_mouse_encoder_set() returns
116 * GHOSTTY_INVALID_VALUE for it. Its value is 0, so a zeroed or
117 * uninitialized variable is never mistaken for a real option.
118 */
119 GHOSTTY_MOUSE_ENCODER_OPT_INVALID = 0,
120
121 /** Mouse tracking mode (value: GhosttyMouseTrackingMode,
122 * default: GHOSTTY_MOUSE_TRACKING_NONE). Changing it forgets the cell
123 * remembered by GHOSTTY_MOUSE_ENCODER_OPT_TRACK_LAST_CELL. */
124 GHOSTTY_MOUSE_ENCODER_OPT_EVENT = 1,
125
126 /** Mouse output format (value: GhosttyMouseFormat,
127 * default: GHOSTTY_MOUSE_FORMAT_X10). Changing it forgets the cell
128 * remembered by GHOSTTY_MOUSE_ENCODER_OPT_TRACK_LAST_CELL. */
129 GHOSTTY_MOUSE_ENCODER_OPT_FORMAT = 2,
130
131 /** Renderer size context (value: GhosttyMouseEncoderSize, default: a
132 * 1x1 pixel screen with 1x1 pixel cells and no padding). Changing it
133 * forgets the cell remembered by
134 * GHOSTTY_MOUSE_ENCODER_OPT_TRACK_LAST_CELL. Setting the size it
135 * already has does nothing, so it's fine to set it before every event. */
136 GHOSTTY_MOUSE_ENCODER_OPT_SIZE = 3,
137
138 /** Whether any mouse button is currently pressed (value: bool,
139 * default: false). */
140 GHOSTTY_MOUSE_ENCODER_OPT_ANY_BUTTON_PRESSED = 4,
141
142 /**
143 * Whether to skip motion events that stay in the same cell (value: bool,
144 * default: false).
145 *
146 * When this is on, the encoder remembers the cell of the last event it
147 * wrote, and writes nothing for a motion event in that same cell. Motion
148 * is reported only when the pointer moves to a different cell. This is
149 * what xterm and Ghostty do, and it's what most programs expect. Turn it
150 * on unless you have a reason not to.
151 *
152 * The SGR-Pixels format reports positions in pixels rather than cells,
153 * so this option has no effect on it. Every motion event is written.
154 *
155 * The encoder forgets the remembered cell when the tracking mode, the
156 * format or the size changes, so the first motion after a change is
157 * always written. Setting an option to the value it already has doesn't
158 * count as a change. Turning this option off, or calling
159 * ghostty_mouse_encoder_reset(), forgets it too.
160 *
161 * That means you can sync the encoder before every event safely:
162 *
163 * @code{.c}
164 * ghostty_mouse_encoder_set(encoder,
165 * GHOSTTY_MOUSE_ENCODER_OPT_TRACK_LAST_CELL, &(bool){true});
166 *
167 * // For each mouse event:
168 * ghostty_mouse_encoder_set_from_terminal(encoder, terminal);
169 * ghostty_mouse_encoder_set(encoder, GHOSTTY_MOUSE_ENCODER_OPT_SIZE, &size);
170 * ghostty_mouse_encoder_encode(encoder, event, buf, sizeof(buf), &written);
171 * @endcode
172 */
173 GHOSTTY_MOUSE_ENCODER_OPT_TRACK_LAST_CELL = 5,
174 GHOSTTY_MOUSE_ENCODER_OPT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
175} GhosttyMouseEncoderOption;
176
177/**
178 * Create a new mouse encoder instance.
179 *
180 * @param allocator Pointer to allocator, or NULL to use the default allocator
181 * @param encoder Pointer to store the created encoder handle (NULL returns
182 * GHOSTTY_INVALID_VALUE)
183 * @return GHOSTTY_SUCCESS on success, or an error code on failure
184 *
185 * @ingroup mouse
186 */
187GHOSTTY_API GhosttyResult ghostty_mouse_encoder_new(const GhosttyAllocator *allocator,
188 GhosttyMouseEncoder *encoder);
189
190/**
191 * Free a mouse encoder instance.
192 *
193 * @param encoder The encoder handle to free (may be NULL)
194 *
195 * @ingroup mouse
196 */
197GHOSTTY_API void ghostty_mouse_encoder_free(GhosttyMouseEncoder encoder);
198
199/**
200 * Set an option on the mouse encoder.
201 *
202 * Passing NULL for `value` resets the option to its default, the same
203 * value a new encoder starts with. Each option's default is listed on
204 * GhosttyMouseEncoderOption. A reset works like setting the default
205 * yourself. If the option already has its default, nothing changes.
206 *
207 * @code{.c}
208 * GhosttyMouseFormat format = GHOSTTY_MOUSE_FORMAT_SGR;
209 * GhosttyResult result = ghostty_mouse_encoder_set(
210 * encoder, GHOSTTY_MOUSE_ENCODER_OPT_FORMAT, &format);
211 * @endcode
212 *
213 * @param encoder The encoder handle (NULL returns GHOSTTY_INVALID_VALUE)
214 * @param option The option to set
215 * @param value Pointer to option value (type depends on option), or NULL to
216 * reset the option to its default
217 * @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if `encoder` is
218 * NULL or the value is not valid (an unknown tracking mode or
219 * format, or a size that is too small or has a zero cell
220 * dimension), or GHOSTTY_UNSUPPORTED if `option` is not known to
221 * this version of the library
222 *
223 * @ingroup mouse
224 */
225GHOSTTY_API GhosttyResult ghostty_mouse_encoder_set(GhosttyMouseEncoder encoder,
226 GhosttyMouseEncoderOption option,
227 const void *value);
228
229/**
230 * Set encoder options from a terminal's current state.
231 *
232 * This sets tracking mode and output format from terminal state.
233 * It does not modify size or any-button state.
234 *
235 * If the terminal's tracking mode or format differs from the encoder's,
236 * the encoder also forgets the cell remembered by
237 * GHOSTTY_MOUSE_ENCODER_OPT_TRACK_LAST_CELL, so the next motion event is
238 * written even if it's in that cell. If neither differs, the encoder keeps
239 * the remembered cell. You can call this before every event.
240 *
241 * @param encoder The encoder handle
242 * @param terminal The terminal handle
243 * @return GHOSTTY_SUCCESS on success, or GHOSTTY_INVALID_VALUE if `encoder`
244 * or `terminal` is NULL. On error the encoder is not changed.
245 *
246 * @ingroup mouse
247 */
248GHOSTTY_API GhosttyResult ghostty_mouse_encoder_set_from_terminal(GhosttyMouseEncoder encoder,
249 GhosttyTerminal terminal);
250
251/**
252 * Reset internal encoder state.
253 *
254 * This clears motion deduplication state (last tracked cell).
255 *
256 * @param encoder The encoder handle
257 * @return GHOSTTY_SUCCESS on success, or GHOSTTY_INVALID_VALUE if `encoder`
258 * is NULL.
259 *
260 * @ingroup mouse
261 */
262GHOSTTY_API GhosttyResult ghostty_mouse_encoder_reset(GhosttyMouseEncoder encoder);
263
264/**
265 * Encode a mouse event into a terminal escape sequence.
266 *
267 * Not all mouse events produce output. In such cases this returns
268 * GHOSTTY_SUCCESS with @p out_written set to 0.
269 *
270 * If @p buf is too small, this returns GHOSTTY_OUT_OF_SPACE and sets
271 * @p out_written to the size needed. The buffer may then hold part of the
272 * sequence, which you should discard. To get the size without encoding
273 * anything, pass NULL for @p buf and 0 for @p buf_len. An event with no
274 * output still returns GHOSTTY_SUCCESS in that case, because it needs no
275 * space.
276 *
277 * A 128-byte buffer is enough for typical events:
278 *
279 * @code{.c}
280 * uint8_t buf[128];
281 * size_t written = 0;
282 * if (ghostty_mouse_encoder_encode(encoder, event, buf, sizeof(buf),
283 * &written) == GHOSTTY_SUCCESS &&
284 * written > 0) {
285 * write(pty_fd, buf, written);
286 * }
287 * @endcode
288 *
289 * @param encoder The encoder handle (NULL returns GHOSTTY_INVALID_VALUE)
290 * @param event The mouse event to encode (NULL returns GHOSTTY_INVALID_VALUE)
291 * @param buf Buffer to write the sequence into. May be NULL if @p buf_len
292 * is 0.
293 * @param buf_len Size of @p buf in bytes
294 * @param[out] out_written The number of bytes written, or the size needed
295 * if the result is GHOSTTY_OUT_OF_SPACE. NULL returns GHOSTTY_INVALID_VALUE.
296 * @return GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_SPACE if buffer is too small,
297 * or another error code
298 *
299 * @ingroup mouse
300 */
301GHOSTTY_API GhosttyResult ghostty_mouse_encoder_encode(GhosttyMouseEncoder encoder,
302 GhosttyMouseEvent event,
303 uint8_t *buf,
304 size_t buf_len,
305 size_t *out_written);
306
307#endif /* GHOSTTY_VT_MOUSE_ENCODER_H */