blob: a3aa17a8f648cdcfa438a86be1e38ac7413ae4d2 [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/*
2 * NOTE: This is a MODIFIED version of libvterm, see the README file.
3 */
4#ifndef __VTERM_H__
5#define __VTERM_H__
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
Bram Moolenaare4f25e42017-07-07 11:54:15 +020011#include <stdlib.h>
Bram Moolenaare4f25e42017-07-07 11:54:15 +020012
13#include "vterm_keycodes.h"
14
Bram Moolenaar6a12d262022-10-16 19:26:52 +010015// VIM: use TRUE and FALSE instead of true and false
Bram Moolenaarb2a76ec2017-07-25 21:34:46 +020016#define TRUE 1
17#define FALSE 0
18
Bram Moolenaar6a12d262022-10-16 19:26:52 +010019// VIM: from stdint.h
Bram Moolenaar607985a2017-07-28 17:04:15 +020020typedef unsigned char uint8_t;
Bram Moolenaar7da34152021-11-24 19:30:55 +000021typedef unsigned short uint16_t;
Bram Moolenaar607985a2017-07-28 17:04:15 +020022typedef unsigned int uint32_t;
23
Bram Moolenaar6fc3b592020-05-17 22:27:55 +020024#define VTERM_VERSION_MAJOR 0
Bram Moolenaar6a12d262022-10-16 19:26:52 +010025#define VTERM_VERSION_MINOR 3
zeertzjqb00df7a2023-08-08 11:03:00 +080026#define VTERM_VERSION_PATCH 3
Bram Moolenaar6fc3b592020-05-17 22:27:55 +020027
28#define VTERM_CHECK_VERSION \
29 vterm_check_version(VTERM_VERSION_MAJOR, VTERM_VERSION_MINOR)
30
Bram Moolenaar501e7772022-10-16 14:35:46 +010031/* Any cell can contain at most one basic printing character and 5 combining
32 * characters. This number could be changed but will be ABI-incompatible if
33 * you do */
34#define VTERM_MAX_CHARS_PER_CELL 6
35
Bram Moolenaare4f25e42017-07-07 11:54:15 +020036typedef struct VTerm VTerm;
37typedef struct VTermState VTermState;
38typedef struct VTermScreen VTermScreen;
39
Bram Moolenaardb1085a2019-08-18 20:41:38 +020040// Specifies a screen point.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020041typedef struct {
42 int row;
43 int col;
44} VTermPos;
45
Bram Moolenaar591cec82020-05-22 22:06:06 +020046/* some small utility functions; we can just keep these static here */
Bram Moolenaare4f25e42017-07-07 11:54:15 +020047
48/*
49 * Order points by on-screen flow order:
50 * Return < 0 if "a" is before "b"
51 * Return 0 if "a" and "b" are equal
52 * Return > 0 if "a" is after "b".
53 */
54int vterm_pos_cmp(VTermPos a, VTermPos b);
55
56#if defined(DEFINE_INLINES) || USE_INLINE
57INLINE int vterm_pos_cmp(VTermPos a, VTermPos b)
58{
59 return (a.row == b.row) ? a.col - b.col : a.row - b.row;
60}
61#endif
62
Bram Moolenaardb1085a2019-08-18 20:41:38 +020063// Specifies a rectangular screen area.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020064typedef struct {
65 int start_row;
66 int end_row;
67 int start_col;
68 int end_col;
69} VTermRect;
70
Bram Moolenaar591cec82020-05-22 22:06:06 +020071/* true if the rect contains the point */
Bram Moolenaare4f25e42017-07-07 11:54:15 +020072int vterm_rect_contains(VTermRect r, VTermPos p);
73
74#if defined(DEFINE_INLINES) || USE_INLINE
75INLINE int vterm_rect_contains(VTermRect r, VTermPos p)
76{
77 return p.row >= r.start_row && p.row < r.end_row &&
78 p.col >= r.start_col && p.col < r.end_col;
79}
80#endif
81
Bram Moolenaar591cec82020-05-22 22:06:06 +020082/* move a rect */
Bram Moolenaardb1085a2019-08-18 20:41:38 +020083// Move "rect" "row_delta" down and "col_delta" right.
84// Does not check boundaries.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020085void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta);
86
87#if defined(DEFINE_INLINES) || USE_INLINE
88INLINE void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta)
89{
90 rect->start_row += row_delta; rect->end_row += row_delta;
91 rect->start_col += col_delta; rect->end_col += col_delta;
92}
93#endif
94
Bram Moolenaare5886cc2020-05-21 20:10:04 +020095/**
96 * Bit-field describing the value of VTermColor.type
97 */
98typedef enum {
99 /**
100 * If the lower bit of `type` is not set, the colour is 24-bit RGB.
101 */
102 VTERM_COLOR_RGB = 0x00,
103
104 /**
105 * The colour is an index into a palette of 256 colours.
106 */
107 VTERM_COLOR_INDEXED = 0x01,
108
109 /**
110 * Mask that can be used to extract the RGB/Indexed bit.
111 */
112 VTERM_COLOR_TYPE_MASK = 0x01,
113
114 /**
115 * If set, indicates that this colour should be the default foreground
116 * color, i.e. there was no SGR request for another colour. When
117 * rendering this colour it is possible to ignore "idx" and just use a
118 * colour that is not in the palette.
119 */
120 VTERM_COLOR_DEFAULT_FG = 0x02,
121
122 /**
123 * If set, indicates that this colour should be the default background
124 * color, i.e. there was no SGR request for another colour. A common
125 * option when rendering this colour is to not render a background at
126 * all, for example by rendering the window transparently at this spot.
127 */
128 VTERM_COLOR_DEFAULT_BG = 0x04,
129
130 /**
131 * Mask that can be used to extract the default foreground/background bit.
132 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000133 VTERM_COLOR_DEFAULT_MASK = 0x06,
134
135 /**
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100136 * VIM: If set, indicates that the color is invalid.
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000137 */
138 VTERM_COLOR_INVALID = 0x08
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200139} VTermColorType;
140
141/**
142 * Returns true if the VTERM_COLOR_RGB `type` flag is set, indicating that the
143 * given VTermColor instance is an indexed colour.
144 */
145#define VTERM_COLOR_IS_INDEXED(col) \
146 (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_INDEXED)
147
148/**
149 * Returns true if the VTERM_COLOR_INDEXED `type` flag is set, indicating that
150 * the given VTermColor instance is an rgb colour.
151 */
152#define VTERM_COLOR_IS_RGB(col) \
153 (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_RGB)
154
155/**
156 * Returns true if the VTERM_COLOR_DEFAULT_FG `type` flag is set, indicating
157 * that the given VTermColor instance corresponds to the default foreground
158 * color.
159 */
160#define VTERM_COLOR_IS_DEFAULT_FG(col) \
161 (!!((col)->type & VTERM_COLOR_DEFAULT_FG))
162
163/**
164 * Returns true if the VTERM_COLOR_DEFAULT_BG `type` flag is set, indicating
165 * that the given VTermColor instance corresponds to the default background
166 * color.
167 */
168#define VTERM_COLOR_IS_DEFAULT_BG(col) \
169 (!!((col)->type & VTERM_COLOR_DEFAULT_BG))
Bram Moolenaar46359e12017-11-29 22:33:38 +0100170
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000171/**
172 * Returns true if the VTERM_COLOR_INVALID `type` flag is set, indicating
173 * that the given VTermColor instance is an invalid color.
174 */
175#define VTERM_COLOR_IS_INVALID(col) (!!((col)->type & VTERM_COLOR_INVALID))
176
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100177// VIM: this was a union, but that doesn't always work.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200178typedef struct {
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200179 /**
180 * Tag indicating which member is actually valid.
181 * Please use the `VTERM_COLOR_IS_*` test macros to check whether a
182 * particular type flag is set.
183 */
184 uint8_t type;
185
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200186 uint8_t red, green, blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200187
188 uint8_t index;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200189} VTermColor;
190
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200191/**
192 * Constructs a new VTermColor instance representing the given RGB values.
193 */
194void vterm_color_rgb(VTermColor *col, uint8_t red, uint8_t green, uint8_t blue);
195
196/**
197 * Construct a new VTermColor instance representing an indexed color with the
198 * given index.
199 */
200void vterm_color_indexed(VTermColor *col, uint8_t idx);
201
202/**
203 * Compares two colours. Returns true if the colors are equal, false otherwise.
204 */
205int vterm_color_is_equal(const VTermColor *a, const VTermColor *b);
206
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200207typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200208 /* VTERM_VALUETYPE_NONE = 0 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200209 VTERM_VALUETYPE_BOOL = 1,
210 VTERM_VALUETYPE_INT,
211 VTERM_VALUETYPE_STRING,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200212 VTERM_VALUETYPE_COLOR,
213
214 VTERM_N_VALUETYPES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200215} VTermValueType;
216
Bram Moolenaarbe593bf2020-05-19 21:20:04 +0200217typedef struct {
218 const char *str;
219 size_t len : 30;
220 unsigned int initial : 1;
221 unsigned int final : 1;
222} VTermStringFragment;
223
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200224typedef union {
225 int boolean;
226 int number;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +0200227 VTermStringFragment string;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200228 VTermColor color;
229} VTermValue;
230
231typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200232 /* VTERM_ATTR_NONE = 0 */
Bram Moolenaarb691de02018-04-24 18:39:14 +0200233 VTERM_ATTR_BOLD = 1, // bool: 1, 22
234 VTERM_ATTR_UNDERLINE, // number: 4, 21, 24
235 VTERM_ATTR_ITALIC, // bool: 3, 23
236 VTERM_ATTR_BLINK, // bool: 5, 25
237 VTERM_ATTR_REVERSE, // bool: 7, 27
Bram Moolenaard8637282020-05-20 18:41:41 +0200238 VTERM_ATTR_CONCEAL, // bool: 8, 28
Bram Moolenaarb691de02018-04-24 18:39:14 +0200239 VTERM_ATTR_STRIKE, // bool: 9, 29
240 VTERM_ATTR_FONT, // number: 10-19
241 VTERM_ATTR_FOREGROUND, // color: 30-39 90-97
242 VTERM_ATTR_BACKGROUND, // color: 40-49 100-107
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100243 VTERM_ATTR_SMALL, // bool: 73, 74, 75
244 VTERM_ATTR_BASELINE, // number: 73, 74, 75
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200245
246 VTERM_N_ATTRS
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247} VTermAttr;
248
249typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200250 /* VTERM_PROP_NONE = 0 */
Bram Moolenaarb691de02018-04-24 18:39:14 +0200251 VTERM_PROP_CURSORVISIBLE = 1, // bool
252 VTERM_PROP_CURSORBLINK, // bool
253 VTERM_PROP_ALTSCREEN, // bool
254 VTERM_PROP_TITLE, // string
255 VTERM_PROP_ICONNAME, // string
256 VTERM_PROP_REVERSE, // bool
257 VTERM_PROP_CURSORSHAPE, // number
258 VTERM_PROP_MOUSE, // number
zeertzjqb00df7a2023-08-08 11:03:00 +0800259 VTERM_PROP_FOCUSREPORT, // bool
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100260 VTERM_PROP_CURSORCOLOR, // VIM - string
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200261
262 VTERM_N_PROPS
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263} VTermProp;
264
265enum {
266 VTERM_PROP_CURSORSHAPE_BLOCK = 1,
267 VTERM_PROP_CURSORSHAPE_UNDERLINE,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200268 VTERM_PROP_CURSORSHAPE_BAR_LEFT,
269
270 VTERM_N_PROP_CURSORSHAPES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200271};
272
273enum {
274 VTERM_PROP_MOUSE_NONE = 0,
275 VTERM_PROP_MOUSE_CLICK,
276 VTERM_PROP_MOUSE_DRAG,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200277 VTERM_PROP_MOUSE_MOVE,
278
279 VTERM_N_PROP_MOUSES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200280};
281
Bram Moolenaar7da34152021-11-24 19:30:55 +0000282typedef enum {
283 VTERM_SELECTION_CLIPBOARD = (1<<0),
284 VTERM_SELECTION_PRIMARY = (1<<1),
285 VTERM_SELECTION_SECONDARY = (1<<2),
286 VTERM_SELECTION_SELECT = (1<<3),
287 VTERM_SELECTION_CUT0 = (1<<4), /* also CUT1 .. CUT7 by bitshifting */
288} VTermSelectionMask;
289
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200290typedef struct {
291 const uint32_t *chars;
292 int width;
Bram Moolenaar591cec82020-05-22 22:06:06 +0200293 unsigned int protected_cell:1; /* DECSCA-protected against DECSEL/DECSED */
294 unsigned int dwl:1; /* DECDWL or DECDHL double-width line */
295 unsigned int dhl:2; /* DECDHL double-height line (1=top 2=bottom) */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200296} VTermGlyphInfo;
297
298typedef struct {
Bram Moolenaar88d68de2020-05-18 21:51:01 +0200299 unsigned int doublewidth:1; /* DECDWL or DECDHL line */
300 unsigned int doubleheight:2; /* DECDHL line (1=top 2=bottom) */
301 unsigned int continuation:1; /* Line is a flow continuation of the previous */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200302} VTermLineInfo;
303
Bram Moolenaar591cec82020-05-22 22:06:06 +0200304/* Copies of VTermState fields that the 'resize' callback might have reason to
305 * edit. 'resize' callback gets total control of these fields and may
306 * free-and-reallocate them if required. They will be copied back from the
307 * struct after the callback has returned.
308 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200309typedef struct {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200310 VTermPos pos; /* current cursor position */
Bram Moolenaar501e7772022-10-16 14:35:46 +0100311 VTermLineInfo *lineinfos[2]; /* [1] may be NULL */
Bram Moolenaar591cec82020-05-22 22:06:06 +0200312} VTermStateFields;
313
314typedef struct {
315 /* libvterm relies on this memory to be zeroed out before it is returned
316 * by the allocator. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200317 void *(*malloc)(size_t size, void *allocdata);
318 void (*free)(void *ptr, void *allocdata);
319} VTermAllocatorFunctions;
320
Bram Moolenaar6fc3b592020-05-17 22:27:55 +0200321void vterm_check_version(int major, int minor);
322
Bram Moolenaar501e7772022-10-16 14:35:46 +0100323struct VTermBuilder {
324 int ver; /* currently unused but reserved for some sort of ABI version flag */
325
326 int rows, cols;
327
328 const VTermAllocatorFunctions *allocator;
329 void *allocdata;
330
331 /* Override default sizes for various structures */
332 size_t outbuffer_len; /* default: 4096 */
333 size_t tmpbuffer_len; /* default: 4096 */
334};
335
336VTerm *vterm_build(const struct VTermBuilder *builder);
337
338/* A convenient shortcut for default cases */
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200339// Allocate and initialize a new terminal with default allocators.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200340VTerm *vterm_new(int rows, int cols);
Bram Moolenaar501e7772022-10-16 14:35:46 +0100341/* These shortcuts are generally discouraged in favour of just using vterm_build() */
342
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200343// Allocate and initialize a new terminal with specified allocators.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200344VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata);
345
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200346// Free and cleanup a terminal and all its data.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200347void vterm_free(VTerm* vt);
348
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200349// Get the current size of the terminal and store in "rowsp" and "colsp".
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200350void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200351
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200352void vterm_set_size(VTerm *vt, int rows, int cols);
353
354int vterm_get_utf8(const VTerm *vt);
355void vterm_set_utf8(VTerm *vt, int is_utf8);
356
357size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len);
358
Bram Moolenaar94d729c2020-05-17 21:50:16 +0200359/* Setting output callback will override the buffer logic */
360typedef void VTermOutputCallback(const char *s, size_t len, void *user);
361void vterm_output_set_callback(VTerm *vt, VTermOutputCallback *func, void *user);
362
363/* These buffer functions only work if output callback is NOT set
364 * These are deprecated and will be removed in a later version */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200365size_t vterm_output_get_buffer_size(const VTerm *vt);
366size_t vterm_output_get_buffer_current(const VTerm *vt);
367size_t vterm_output_get_buffer_remaining(const VTerm *vt);
368
Bram Moolenaar94d729c2020-05-17 21:50:16 +0200369/* This too */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200370size_t vterm_output_read(VTerm *vt, char *buffer, size_t len);
371
Bram Moolenaar6a0299d2019-10-10 21:14:03 +0200372int vterm_is_modify_other_keys(VTerm *vt);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000373int vterm_is_kitty_keyboard(VTerm *vt);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200374void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod);
375void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod);
376
377void vterm_keyboard_start_paste(VTerm *vt);
378void vterm_keyboard_end_paste(VTerm *vt);
379
380void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod);
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200381// "button" is 1 for left, 2 for middle, 3 for right.
382// Button 4 is scroll wheel down, button 5 is scroll wheel up.
Bram Moolenaarb2a76ec2017-07-25 21:34:46 +0200383void vterm_mouse_button(VTerm *vt, int button, int pressed, VTermModifier mod);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200384
Bram Moolenaarb691de02018-04-24 18:39:14 +0200385// ------------
386// Parser layer
387// ------------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200388
Bram Moolenaar591cec82020-05-22 22:06:06 +0200389/* Flag to indicate non-final subparameters in a single CSI parameter.
390 * Consider
391 * CSI 1;2:3:4;5a
392 * 1 4 and 5 are final.
393 * 2 and 3 are non-final and will have this bit set
394 *
395 * Don't confuse this with the final byte of the CSI escape; 'a' in this case.
396 */
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200397#define CSI_ARG_FLAG_MORE (1U<<31)
398#define CSI_ARG_MASK (~(1U<<31))
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200399
400#define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE)
401#define CSI_ARG(a) ((a) & CSI_ARG_MASK)
402
Bram Moolenaar591cec82020-05-22 22:06:06 +0200403/* Can't use -1 to indicate a missing argument; use this instead */
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100404// VIM: changed 31 to 30 to avoid an overflow warning
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200405#define CSI_ARG_MISSING ((1<<30)-1)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200406
407#define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING)
408#define CSI_ARG_OR(a,def) (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a))
409#define CSI_ARG_COUNT(a) (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a))
410
411typedef struct {
412 int (*text)(const char *bytes, size_t len, void *user);
413 int (*control)(unsigned char control, void *user);
414 int (*escape)(const char *bytes, size_t len, void *user);
415 int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +0200416 int (*osc)(int command, VTermStringFragment frag, void *user);
417 int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user);
Bram Moolenaar7da34152021-11-24 19:30:55 +0000418 int (*apc)(VTermStringFragment frag, void *user);
419 int (*pm)(VTermStringFragment frag, void *user);
420 int (*sos)(VTermStringFragment frag, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200421 int (*resize)(int rows, int cols, void *user);
422} VTermParserCallbacks;
423
424void vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user);
425void *vterm_parser_get_cbdata(VTerm *vt);
426
zeertzjqb00df7a2023-08-08 11:03:00 +0800427/* Normally NUL, CAN, SUB and DEL are ignored. Setting this true causes them
428 * to be emitted by the 'control' callback
429 */
430void vterm_parser_set_emit_nul(VTerm *vt, int emit);
431
Bram Moolenaarb691de02018-04-24 18:39:14 +0200432// -----------
433// State layer
434// -----------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200435
436typedef struct {
437 int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user);
438 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
439 int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user);
440 int (*moverect)(VTermRect dest, VTermRect src, void *user);
441 int (*erase)(VTermRect rect, int selective, void *user);
442 int (*initpen)(void *user);
443 int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user);
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200444 // Callback for setting various properties. Must return 1 if the property
445 // was accepted, 0 otherwise.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200446 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
447 int (*bell)(void *user);
Bram Moolenaard098b822020-05-18 21:12:59 +0200448 int (*resize)(int rows, int cols, VTermStateFields *fields, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200449 int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user);
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100450 int (*sb_clear)(void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200451} VTermStateCallbacks;
452
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100453// VIM: added
Bram Moolenaarc48369c2018-03-11 19:30:45 +0100454typedef struct {
455 VTermPos pos;
456 int buttons;
457#define MOUSE_BUTTON_LEFT 0x01
458#define MOUSE_BUTTON_MIDDLE 0x02
459#define MOUSE_BUTTON_RIGHT 0x04
460 int flags;
461#define MOUSE_WANT_CLICK 0x01
462#define MOUSE_WANT_DRAG 0x02
463#define MOUSE_WANT_MOVE 0x04
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200464 // useful to add protocol?
Bram Moolenaarc48369c2018-03-11 19:30:45 +0100465} VTermMouseState;
466
Bram Moolenaard8637282020-05-20 18:41:41 +0200467typedef struct {
468 int (*control)(unsigned char control, void *user);
469 int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
470 int (*osc)(int command, VTermStringFragment frag, void *user);
471 int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user);
Bram Moolenaar7da34152021-11-24 19:30:55 +0000472 int (*apc)(VTermStringFragment frag, void *user);
473 int (*pm)(VTermStringFragment frag, void *user);
474 int (*sos)(VTermStringFragment frag, void *user);
Bram Moolenaard8637282020-05-20 18:41:41 +0200475} VTermStateFallbacks;
476
Bram Moolenaar7da34152021-11-24 19:30:55 +0000477typedef struct {
478 int (*set)(VTermSelectionMask mask, VTermStringFragment frag, void *user);
479 int (*query)(VTermSelectionMask mask, void *user);
480} VTermSelectionCallbacks;
481
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200482VTermState *vterm_obtain_state(VTerm *vt);
483
484void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user);
485void *vterm_state_get_cbdata(VTermState *state);
486
Bram Moolenaard8637282020-05-20 18:41:41 +0200487void vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermStateFallbacks *fallbacks, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200488void *vterm_state_get_unrecognised_fbdata(VTermState *state);
489
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200490// Initialize the state.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200491void vterm_state_reset(VTermState *state, int hard);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200492
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200493void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos);
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100494// VIM: added
Bram Moolenaarc48369c2018-03-11 19:30:45 +0100495void vterm_state_get_mousestate(const VTermState *state, VTermMouseState *mousestate);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200496void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg);
497void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col);
498void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg);
499void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col);
500void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright);
501int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val);
502int vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val);
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200503void vterm_state_focus_in(VTermState *state);
504void vterm_state_focus_out(VTermState *state);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200505const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row);
506
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200507/**
508 * Makes sure that the given color `col` is indeed an RGB colour. After this
509 * function returns, VTERM_COLOR_IS_RGB(col) will return true, while all other
510 * flags stored in `col->type` will have been reset.
511 *
512 * @param state is the VTermState instance from which the colour palette should
513 * be extracted.
514 * @param col is a pointer at the VTermColor instance that should be converted
515 * to an RGB colour.
516 */
517void vterm_state_convert_color_to_rgb(const VTermState *state, VTermColor *col);
518
Bram Moolenaar7da34152021-11-24 19:30:55 +0000519void vterm_state_set_selection_callbacks(VTermState *state, const VTermSelectionCallbacks *callbacks, void *user,
520 char *buffer, size_t buflen);
521
522void vterm_state_send_selection(VTermState *state, VTermSelectionMask mask, VTermStringFragment frag);
523
Bram Moolenaarb691de02018-04-24 18:39:14 +0200524// ------------
525// Screen layer
526// ------------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200527
528typedef struct {
529 unsigned int bold : 1;
530 unsigned int underline : 2;
531 unsigned int italic : 1;
532 unsigned int blink : 1;
533 unsigned int reverse : 1;
Bram Moolenaard8637282020-05-20 18:41:41 +0200534 unsigned int conceal : 1;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200535 unsigned int strike : 1;
Bram Moolenaar591cec82020-05-22 22:06:06 +0200536 unsigned int font : 4; /* 0 to 9 */
537 unsigned int dwl : 1; /* On a DECDWL or DECDHL line */
538 unsigned int dhl : 2; /* On a DECDHL line (1=top 2=bottom) */
Bram Moolenaare6a16e92022-10-17 14:51:36 +0100539 unsigned int small : 1;
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100540 unsigned int baseline : 2;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200541} VTermScreenCellAttrs;
542
Bram Moolenaar6fc3b592020-05-17 22:27:55 +0200543enum {
544 VTERM_UNDERLINE_OFF,
545 VTERM_UNDERLINE_SINGLE,
546 VTERM_UNDERLINE_DOUBLE,
547 VTERM_UNDERLINE_CURLY,
548};
549
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100550enum {
551 VTERM_BASELINE_NORMAL,
552 VTERM_BASELINE_RAISE,
553 VTERM_BASELINE_LOWER,
554};
555
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200556typedef struct {
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200557 uint32_t chars[VTERM_MAX_CHARS_PER_CELL];
558 char width;
559 VTermScreenCellAttrs attrs;
560 VTermColor fg, bg;
561} VTermScreenCell;
562
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200563// All fields are optional, NULL when not used.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200564typedef struct {
565 int (*damage)(VTermRect rect, void *user);
566 int (*moverect)(VTermRect dest, VTermRect src, void *user);
567 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
568 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
569 int (*bell)(void *user);
570 int (*resize)(int rows, int cols, void *user);
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200571 // A line was pushed off the top of the window.
572 // "cells[cols]" contains the cells of that line.
573 // Return value is unused.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200574 int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user);
575 int (*sb_popline)(int cols, VTermScreenCell *cells, void *user);
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100576 int (*sb_clear)(void* user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200577} VTermScreenCallbacks;
578
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200579// Return the screen of the vterm.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200580VTermScreen *vterm_obtain_screen(VTerm *vt);
581
582/*
583 * Install screen callbacks. These are invoked when the screen contents is
584 * changed. "user" is passed into to the callback.
585 */
586void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user);
587void *vterm_screen_get_cbdata(VTermScreen *screen);
588
Bram Moolenaard8637282020-05-20 18:41:41 +0200589void vterm_screen_set_unrecognised_fallbacks(VTermScreen *screen, const VTermStateFallbacks *fallbacks, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200590void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen);
591
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100592void vterm_screen_enable_reflow(VTermScreen *screen, int reflow);
593
594// Back-compat alias for the brief time it was in 0.3-RC1
595#define vterm_screen_set_reflow vterm_screen_enable_reflow
596
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200597// Enable support for using the alternate screen if "altscreen" is non-zero.
598// Before that switching to the alternate screen won't work.
599// Calling with "altscreen" zero has no effect.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200600void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen);
601
602typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200603 VTERM_DAMAGE_CELL, /* every cell */
604 VTERM_DAMAGE_ROW, /* entire rows */
605 VTERM_DAMAGE_SCREEN, /* entire screen */
606 VTERM_DAMAGE_SCROLL, /* entire screen + scrollrect */
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200607
608 VTERM_N_DAMAGES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200609} VTermDamageSize;
610
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200611// Invoke the relevant callbacks to update the screen.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200612void vterm_screen_flush_damage(VTermScreen *screen);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200613
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200614void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size);
615
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200616/*
617 * Reset the screen. Also invokes vterm_state_reset().
618 * Must be called before the terminal can be used.
619 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200620void vterm_screen_reset(VTermScreen *screen, int hard);
621
Bram Moolenaar591cec82020-05-22 22:06:06 +0200622/* Neither of these functions NUL-terminate the buffer */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200623size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect);
624size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect);
625
626typedef enum {
627 VTERM_ATTR_BOLD_MASK = 1 << 0,
628 VTERM_ATTR_UNDERLINE_MASK = 1 << 1,
629 VTERM_ATTR_ITALIC_MASK = 1 << 2,
630 VTERM_ATTR_BLINK_MASK = 1 << 3,
631 VTERM_ATTR_REVERSE_MASK = 1 << 4,
632 VTERM_ATTR_STRIKE_MASK = 1 << 5,
633 VTERM_ATTR_FONT_MASK = 1 << 6,
634 VTERM_ATTR_FOREGROUND_MASK = 1 << 7,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200635 VTERM_ATTR_BACKGROUND_MASK = 1 << 8,
Bram Moolenaard8637282020-05-20 18:41:41 +0200636 VTERM_ATTR_CONCEAL_MASK = 1 << 9,
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100637 VTERM_ATTR_SMALL_MASK = 1 << 10,
638 VTERM_ATTR_BASELINE_MASK = 1 << 11,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200639
Bram Moolenaar6a12d262022-10-16 19:26:52 +0100640 VTERM_ALL_ATTRS_MASK = (1 << 12) - 1
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200641} VTermAttrMask;
642
643int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs);
644
645int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell);
646
647int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos);
648
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200649/**
650 * Same as vterm_state_convert_color_to_rgb(), but takes a `screen` instead of a `state`
651 * instance.
652 */
653void vterm_screen_convert_color_to_rgb(const VTermScreen *screen, VTermColor *col);
654
zeertzjqb00df7a2023-08-08 11:03:00 +0800655/**
656 * Similar to vterm_state_set_default_colors(), but also resets colours in the
657 * screen buffer(s)
658 */
659void vterm_screen_set_default_colors(VTermScreen *screen, const VTermColor *default_fg, const VTermColor *default_bg);
660
Bram Moolenaarb691de02018-04-24 18:39:14 +0200661// ---------
662// Utilities
663// ---------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200664
665VTermValueType vterm_get_attr_type(VTermAttr attr);
666VTermValueType vterm_get_prop_type(VTermProp prop);
667
668void vterm_scroll_rect(VTermRect rect,
669 int downward,
670 int rightward,
671 int (*moverect)(VTermRect src, VTermRect dest, void *user),
672 int (*eraserect)(VTermRect rect, int selective, void *user),
673 void *user);
674
675void vterm_copy_cells(VTermRect dest,
676 VTermRect src,
677 void (*copycell)(VTermPos dest, VTermPos src, void *user),
678 void *user);
679
680#ifdef __cplusplus
681}
682#endif
683
684#endif