1#ifndef __VTERM_H__
2#define __VTERM_H__
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8#include <stdint.h>
9#include <stdlib.h>
10#include <stdbool.h>
11
12#include "vterm_keycodes.h"
13
14typedef struct VTerm VTerm;
15typedef struct VTermState VTermState;
16typedef struct VTermScreen VTermScreen;
17
18typedef struct {
19 int row;
20 int col;
21} VTermPos;
22
23/* some small utility functions; we can just keep these static here */
24
25/* order points by on-screen flow order */
26static inline int vterm_pos_cmp(VTermPos a, VTermPos b)
27{
28 return (a.row == b.row) ? a.col - b.col : a.row - b.row;
29}
30
31typedef struct {
32 int start_row;
33 int end_row;
34 int start_col;
35 int end_col;
36} VTermRect;
37
38/* true if the rect contains the point */
39static inline int vterm_rect_contains(VTermRect r, VTermPos p)
40{
41 return p.row >= r.start_row && p.row < r.end_row &&
42 p.col >= r.start_col && p.col < r.end_col;
43}
44
45/* move a rect */
46static inline void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta)
47{
48 rect->start_row += row_delta; rect->end_row += row_delta;
49 rect->start_col += col_delta; rect->end_col += col_delta;
50}
51
52/**
53 * Bit-field describing the content of the tagged union `VTermColor`.
54 */
55typedef enum {
56 /**
57 * If the lower bit of `type` is not set, the colour is 24-bit RGB.
58 */
59 VTERM_COLOR_RGB = 0x00,
60
61 /**
62 * The colour is an index into a palette of 256 colours.
63 */
64 VTERM_COLOR_INDEXED = 0x01,
65
66 /**
67 * Mask that can be used to extract the RGB/Indexed bit.
68 */
69 VTERM_COLOR_TYPE_MASK = 0x01,
70
71 /**
72 * If set, indicates that this colour should be the default foreground
73 * color, i.e. there was no SGR request for another colour. When
74 * rendering this colour it is possible to ignore "idx" and just use a
75 * colour that is not in the palette.
76 */
77 VTERM_COLOR_DEFAULT_FG = 0x02,
78
79 /**
80 * If set, indicates that this colour should be the default background
81 * color, i.e. there was no SGR request for another colour. A common
82 * option when rendering this colour is to not render a background at
83 * all, for example by rendering the window transparently at this spot.
84 */
85 VTERM_COLOR_DEFAULT_BG = 0x04,
86
87 /**
88 * Mask that can be used to extract the default foreground/background bit.
89 */
90 VTERM_COLOR_DEFAULT_MASK = 0x06
91} VTermColorType;
92
93/**
94 * Returns true if the VTERM_COLOR_RGB `type` flag is set, indicating that the
95 * given VTermColor instance is an indexed colour.
96 */
97#define VTERM_COLOR_IS_INDEXED(col) \
98 (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_INDEXED)
99
100/**
101 * Returns true if the VTERM_COLOR_INDEXED `type` flag is set, indicating that
102 * the given VTermColor instance is an rgb colour.
103 */
104#define VTERM_COLOR_IS_RGB(col) \
105 (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_RGB)
106
107/**
108 * Returns true if the VTERM_COLOR_DEFAULT_FG `type` flag is set, indicating
109 * that the given VTermColor instance corresponds to the default foreground
110 * color.
111 */
112#define VTERM_COLOR_IS_DEFAULT_FG(col) \
113 (!!((col)->type & VTERM_COLOR_DEFAULT_FG))
114
115/**
116 * Returns true if the VTERM_COLOR_DEFAULT_BG `type` flag is set, indicating
117 * that the given VTermColor instance corresponds to the default background
118 * color.
119 */
120#define VTERM_COLOR_IS_DEFAULT_BG(col) \
121 (!!((col)->type & VTERM_COLOR_DEFAULT_BG))
122
123/**
124 * Tagged union storing either an RGB color or an index into a colour palette.
125 * In order to convert indexed colours to RGB, you may use the
126 * vterm_state_convert_color_to_rgb() or vterm_screen_convert_color_to_rgb()
127 * functions which lookup the RGB colour from the palette maintained by a
128 * VTermState or VTermScreen instance.
129 */
130typedef union {
131 /**
132 * Tag indicating which union member is actually valid. This variable
133 * coincides with the `type` member of the `rgb` and the `indexed` struct
134 * in memory. Please use the `VTERM_COLOR_IS_*` test macros to check whether
135 * a particular type flag is set.
136 */
137 uint8_t type;
138
139 /**
140 * Valid if `VTERM_COLOR_IS_RGB(type)` is true. Holds the RGB colour values.
141 */
142 struct {
143 /**
144 * Same as the top-level `type` member stored in VTermColor.
145 */
146 uint8_t type;
147
148 /**
149 * The actual 8-bit red, green, blue colour values.
150 */
151 uint8_t red, green, blue;
152 } rgb;
153
154 /**
155 * If `VTERM_COLOR_IS_INDEXED(type)` is true, this member holds the index into
156 * the colour palette.
157 */
158 struct {
159 /**
160 * Same as the top-level `type` member stored in VTermColor.
161 */
162 uint8_t type;
163
164 /**
165 * Index into the colour map.
166 */
167 uint8_t idx;
168 } indexed;
169} VTermColor;
170
171/**
172 * Constructs a new VTermColor instance representing the given RGB values.
173 */
174static inline void vterm_color_rgb(VTermColor *col, uint8_t red, uint8_t green,
175 uint8_t blue)
176{
177 col->type = VTERM_COLOR_RGB;
178 col->rgb.red = red;
179 col->rgb.green = green;
180 col->rgb.blue = blue;
181}
182
183/**
184 * Construct a new VTermColor instance representing an indexed color with the
185 * given index.
186 */
187static inline void vterm_color_indexed(VTermColor *col, uint8_t idx)
188{
189 col->type = VTERM_COLOR_INDEXED;
190 col->indexed.idx = idx;
191}
192
193/**
194 * Compares two colours. Returns true if the colors are equal, false otherwise.
195 */
196int vterm_color_is_equal(const VTermColor *a, const VTermColor *b);
197
198typedef enum {
199 /* VTERM_VALUETYPE_NONE = 0 */
200 VTERM_VALUETYPE_BOOL = 1,
201 VTERM_VALUETYPE_INT,
202 VTERM_VALUETYPE_STRING,
203 VTERM_VALUETYPE_COLOR,
204
205 VTERM_N_VALUETYPES
206} VTermValueType;
207
208typedef union {
209 int boolean;
210 int number;
211 char *string;
212 VTermColor color;
213} VTermValue;
214
215typedef enum {
216 /* VTERM_ATTR_NONE = 0 */
217 VTERM_ATTR_BOLD = 1, // bool: 1, 22
218 VTERM_ATTR_UNDERLINE, // number: 4, 21, 24
219 VTERM_ATTR_ITALIC, // bool: 3, 23
220 VTERM_ATTR_BLINK, // bool: 5, 25
221 VTERM_ATTR_REVERSE, // bool: 7, 27
222 VTERM_ATTR_STRIKE, // bool: 9, 29
223 VTERM_ATTR_FONT, // number: 10-19
224 VTERM_ATTR_FOREGROUND, // color: 30-39 90-97
225 VTERM_ATTR_BACKGROUND, // color: 40-49 100-107
226
227 VTERM_N_ATTRS
228} VTermAttr;
229
230typedef enum {
231 /* VTERM_PROP_NONE = 0 */
232 VTERM_PROP_CURSORVISIBLE = 1, // bool
233 VTERM_PROP_CURSORBLINK, // bool
234 VTERM_PROP_ALTSCREEN, // bool
235 VTERM_PROP_TITLE, // string
236 VTERM_PROP_ICONNAME, // string
237 VTERM_PROP_REVERSE, // bool
238 VTERM_PROP_CURSORSHAPE, // number
239 VTERM_PROP_MOUSE, // number
240
241 VTERM_N_PROPS
242} VTermProp;
243
244enum {
245 VTERM_PROP_CURSORSHAPE_BLOCK = 1,
246 VTERM_PROP_CURSORSHAPE_UNDERLINE,
247 VTERM_PROP_CURSORSHAPE_BAR_LEFT,
248
249 VTERM_N_PROP_CURSORSHAPES
250};
251
252enum {
253 VTERM_PROP_MOUSE_NONE = 0,
254 VTERM_PROP_MOUSE_CLICK,
255 VTERM_PROP_MOUSE_DRAG,
256 VTERM_PROP_MOUSE_MOVE,
257
258 VTERM_N_PROP_MOUSES
259};
260
261typedef struct {
262 const uint32_t *chars;
263 int width;
264 unsigned int protected_cell:1; /* DECSCA-protected against DECSEL/DECSED */
265 unsigned int dwl:1; /* DECDWL or DECDHL double-width line */
266 unsigned int dhl:2; /* DECDHL double-height line (1=top 2=bottom) */
267} VTermGlyphInfo;
268
269typedef struct {
270 unsigned int doublewidth:1; /* DECDWL or DECDHL line */
271 unsigned int doubleheight:2; /* DECDHL line (1=top 2=bottom) */
272} VTermLineInfo;
273
274typedef struct {
275 /* libvterm relies on this memory to be zeroed out before it is returned
276 * by the allocator. */
277 void *(*malloc)(size_t size, void *allocdata);
278 void (*free)(void *ptr, void *allocdata);
279} VTermAllocatorFunctions;
280
281VTerm *vterm_new(int rows, int cols);
282VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata);
283void vterm_free(VTerm* vt);
284
285void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp);
286void vterm_set_size(VTerm *vt, int rows, int cols);
287
288int vterm_get_utf8(const VTerm *vt);
289void vterm_set_utf8(VTerm *vt, int is_utf8);
290
291size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len);
292
293/* Setting output callback will override the buffer logic */
294typedef void VTermOutputCallback(const char *s, size_t len, void *user);
295void vterm_output_set_callback(VTerm *vt, VTermOutputCallback *func, void *user);
296
297/* These buffer functions only work if output callback is NOT set
298 * These are deprecated and will be removed in a later version */
299size_t vterm_output_get_buffer_size(const VTerm *vt);
300size_t vterm_output_get_buffer_current(const VTerm *vt);
301size_t vterm_output_get_buffer_remaining(const VTerm *vt);
302
303/* This too */
304size_t vterm_output_read(VTerm *vt, char *buffer, size_t len);
305
306void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod);
307void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod);
308
309void vterm_keyboard_start_paste(VTerm *vt);
310void vterm_keyboard_end_paste(VTerm *vt);
311
312void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod);
313void vterm_mouse_button(VTerm *vt, int button, bool pressed, VTermModifier mod);
314
315// ------------
316// Parser layer
317// ------------
318
319/* Flag to indicate non-final subparameters in a single CSI parameter.
320 * Consider
321 * CSI 1;2:3:4;5a
322 * 1 4 and 5 are final.
323 * 2 and 3 are non-final and will have this bit set
324 *
325 * Don't confuse this with the final byte of the CSI escape; 'a' in this case.
326 */
327#define CSI_ARG_FLAG_MORE (1U<<31)
328#define CSI_ARG_MASK (~(1U<<31))
329
330#define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE)
331#define CSI_ARG(a) ((a) & CSI_ARG_MASK)
332
333/* Can't use -1 to indicate a missing argument; use this instead */
334#define CSI_ARG_MISSING ((1UL<<31)-1)
335
336#define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING)
337#define CSI_ARG_OR(a,def) (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a))
338#define CSI_ARG_COUNT(a) (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a))
339
340typedef struct {
341 int (*text)(const char *bytes, size_t len, void *user);
342 int (*control)(unsigned char control, void *user);
343 int (*escape)(const char *bytes, size_t len, void *user);
344 int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
345 int (*osc)(const char *command, size_t cmdlen, void *user);
346 int (*dcs)(const char *command, size_t cmdlen, void *user);
347 int (*resize)(int rows, int cols, void *user);
348} VTermParserCallbacks;
349
350void vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user);
351void *vterm_parser_get_cbdata(VTerm *vt);
352
353// -----------
354// State layer
355// -----------
356
357typedef struct {
358 int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user);
359 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
360 int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user);
361 int (*moverect)(VTermRect dest, VTermRect src, void *user);
362 int (*erase)(VTermRect rect, int selective, void *user);
363 int (*initpen)(void *user);
364 int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user);
365 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
366 int (*bell)(void *user);
367 int (*resize)(int rows, int cols, VTermPos *delta, void *user);
368 int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user);
369} VTermStateCallbacks;
370
371VTermState *vterm_obtain_state(VTerm *vt);
372
373void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user);
374void *vterm_state_get_cbdata(VTermState *state);
375
376// Only invokes control, csi, osc, dcs
377void vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermParserCallbacks *fallbacks, void *user);
378void *vterm_state_get_unrecognised_fbdata(VTermState *state);
379
380void vterm_state_reset(VTermState *state, int hard);
381void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos);
382void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg);
383void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col);
384void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg);
385void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col);
386void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright);
387int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val);
388int vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val);
389void vterm_state_focus_in(VTermState *state);
390void vterm_state_focus_out(VTermState *state);
391const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row);
392
393/**
394 * Makes sure that the given color `col` is indeed an RGB colour. After this
395 * function returns, VTERM_COLOR_IS_RGB(col) will return true, while all other
396 * flags stored in `col->type` will have been reset.
397 *
398 * @param state is the VTermState instance from which the colour palette should
399 * be extracted.
400 * @param col is a pointer at the VTermColor instance that should be converted
401 * to an RGB colour.
402 */
403void vterm_state_convert_color_to_rgb(const VTermState *state, VTermColor *col);
404
405// ------------
406// Screen layer
407// ------------
408
409typedef struct {
410 unsigned int bold : 1;
411 unsigned int underline : 2;
412 unsigned int italic : 1;
413 unsigned int blink : 1;
414 unsigned int reverse : 1;
415 unsigned int strike : 1;
416 unsigned int font : 4; /* 0 to 9 */
417 unsigned int dwl : 1; /* On a DECDWL or DECDHL line */
418 unsigned int dhl : 2; /* On a DECDHL line (1=top 2=bottom) */
419} VTermScreenCellAttrs;
420
421enum {
422 VTERM_UNDERLINE_OFF,
423 VTERM_UNDERLINE_SINGLE,
424 VTERM_UNDERLINE_DOUBLE,
425 VTERM_UNDERLINE_CURLY,
426};
427
428typedef struct {
429#define VTERM_MAX_CHARS_PER_CELL 6
430 uint32_t chars[VTERM_MAX_CHARS_PER_CELL];
431 char width;
432 VTermScreenCellAttrs attrs;
433 VTermColor fg, bg;
434} VTermScreenCell;
435
436typedef struct {
437 int (*damage)(VTermRect rect, void *user);
438 int (*moverect)(VTermRect dest, VTermRect src, void *user);
439 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
440 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
441 int (*bell)(void *user);
442 int (*resize)(int rows, int cols, void *user);
443 int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user);
444 int (*sb_popline)(int cols, VTermScreenCell *cells, void *user);
445} VTermScreenCallbacks;
446
447VTermScreen *vterm_obtain_screen(VTerm *vt);
448
449void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user);
450void *vterm_screen_get_cbdata(VTermScreen *screen);
451
452// Only invokes control, csi, osc, dcs
453void vterm_screen_set_unrecognised_fallbacks(VTermScreen *screen, const VTermParserCallbacks *fallbacks, void *user);
454void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen);
455
456void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen);
457
458typedef enum {
459 VTERM_DAMAGE_CELL, /* every cell */
460 VTERM_DAMAGE_ROW, /* entire rows */
461 VTERM_DAMAGE_SCREEN, /* entire screen */
462 VTERM_DAMAGE_SCROLL, /* entire screen + scrollrect */
463
464 VTERM_N_DAMAGES
465} VTermDamageSize;
466
467void vterm_screen_flush_damage(VTermScreen *screen);
468void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size);
469
470void vterm_screen_reset(VTermScreen *screen, int hard);
471
472/* Neither of these functions NUL-terminate the buffer */
473size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect);
474size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect);
475
476typedef enum {
477 VTERM_ATTR_BOLD_MASK = 1 << 0,
478 VTERM_ATTR_UNDERLINE_MASK = 1 << 1,
479 VTERM_ATTR_ITALIC_MASK = 1 << 2,
480 VTERM_ATTR_BLINK_MASK = 1 << 3,
481 VTERM_ATTR_REVERSE_MASK = 1 << 4,
482 VTERM_ATTR_STRIKE_MASK = 1 << 5,
483 VTERM_ATTR_FONT_MASK = 1 << 6,
484 VTERM_ATTR_FOREGROUND_MASK = 1 << 7,
485 VTERM_ATTR_BACKGROUND_MASK = 1 << 8,
486
487 VTERM_ALL_ATTRS_MASK = (1 << 9) - 1
488} VTermAttrMask;
489
490int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs);
491
492int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell);
493
494int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos);
495
496/**
497 * Same as vterm_state_convert_color_to_rgb(), but takes a `screen` instead of a `state`
498 * instance.
499 */
500void vterm_screen_convert_color_to_rgb(const VTermScreen *screen, VTermColor *col);
501
502// ---------
503// Utilities
504// ---------
505
506VTermValueType vterm_get_attr_type(VTermAttr attr);
507VTermValueType vterm_get_prop_type(VTermProp prop);
508
509void vterm_scroll_rect(VTermRect rect,
510 int downward,
511 int rightward,
512 int (*moverect)(VTermRect src, VTermRect dest, void *user),
513 int (*eraserect)(VTermRect rect, int selective, void *user),
514 void *user);
515
516void vterm_copy_cells(VTermRect dest,
517 VTermRect src,
518 void (*copycell)(VTermPos dest, VTermPos src, void *user),
519 void *user);
520
521#ifdef __cplusplus
522}
523#endif
524
525#endif
526