blob: 21295a62a98f5fa1def451f8281c581869ea3ffe [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 Moolenaarb2a76ec2017-07-25 21:34:46 +020015#define TRUE 1
16#define FALSE 0
17
Bram Moolenaardb1085a2019-08-18 20:41:38 +020018// from stdint.h
Bram Moolenaar607985a2017-07-28 17:04:15 +020019typedef unsigned char uint8_t;
Bram Moolenaar7da34152021-11-24 19:30:55 +000020typedef unsigned short uint16_t;
Bram Moolenaar607985a2017-07-28 17:04:15 +020021typedef unsigned int uint32_t;
22
Bram Moolenaar6fc3b592020-05-17 22:27:55 +020023#define VTERM_VERSION_MAJOR 0
Bram Moolenaar7da34152021-11-24 19:30:55 +000024#define VTERM_VERSION_MINOR 2
Bram Moolenaar6fc3b592020-05-17 22:27:55 +020025
26#define VTERM_CHECK_VERSION \
27 vterm_check_version(VTERM_VERSION_MAJOR, VTERM_VERSION_MINOR)
28
Bram Moolenaar501e7772022-10-16 14:35:46 +010029/* Any cell can contain at most one basic printing character and 5 combining
30 * characters. This number could be changed but will be ABI-incompatible if
31 * you do */
32#define VTERM_MAX_CHARS_PER_CELL 6
33
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034typedef struct VTerm VTerm;
35typedef struct VTermState VTermState;
36typedef struct VTermScreen VTermScreen;
37
Bram Moolenaardb1085a2019-08-18 20:41:38 +020038// Specifies a screen point.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020039typedef struct {
40 int row;
41 int col;
42} VTermPos;
43
Bram Moolenaar591cec82020-05-22 22:06:06 +020044/* some small utility functions; we can just keep these static here */
Bram Moolenaare4f25e42017-07-07 11:54:15 +020045
46/*
47 * Order points by on-screen flow order:
48 * Return < 0 if "a" is before "b"
49 * Return 0 if "a" and "b" are equal
50 * Return > 0 if "a" is after "b".
51 */
52int vterm_pos_cmp(VTermPos a, VTermPos b);
53
54#if defined(DEFINE_INLINES) || USE_INLINE
55INLINE int vterm_pos_cmp(VTermPos a, VTermPos b)
56{
57 return (a.row == b.row) ? a.col - b.col : a.row - b.row;
58}
59#endif
60
Bram Moolenaardb1085a2019-08-18 20:41:38 +020061// Specifies a rectangular screen area.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020062typedef struct {
63 int start_row;
64 int end_row;
65 int start_col;
66 int end_col;
67} VTermRect;
68
Bram Moolenaar591cec82020-05-22 22:06:06 +020069/* true if the rect contains the point */
Bram Moolenaare4f25e42017-07-07 11:54:15 +020070int vterm_rect_contains(VTermRect r, VTermPos p);
71
72#if defined(DEFINE_INLINES) || USE_INLINE
73INLINE int vterm_rect_contains(VTermRect r, VTermPos p)
74{
75 return p.row >= r.start_row && p.row < r.end_row &&
76 p.col >= r.start_col && p.col < r.end_col;
77}
78#endif
79
Bram Moolenaar591cec82020-05-22 22:06:06 +020080/* move a rect */
Bram Moolenaardb1085a2019-08-18 20:41:38 +020081// Move "rect" "row_delta" down and "col_delta" right.
82// Does not check boundaries.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020083void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta);
84
85#if defined(DEFINE_INLINES) || USE_INLINE
86INLINE void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta)
87{
88 rect->start_row += row_delta; rect->end_row += row_delta;
89 rect->start_col += col_delta; rect->end_col += col_delta;
90}
91#endif
92
Bram Moolenaare5886cc2020-05-21 20:10:04 +020093/**
94 * Bit-field describing the value of VTermColor.type
95 */
96typedef enum {
97 /**
98 * If the lower bit of `type` is not set, the colour is 24-bit RGB.
99 */
100 VTERM_COLOR_RGB = 0x00,
101
102 /**
103 * The colour is an index into a palette of 256 colours.
104 */
105 VTERM_COLOR_INDEXED = 0x01,
106
107 /**
108 * Mask that can be used to extract the RGB/Indexed bit.
109 */
110 VTERM_COLOR_TYPE_MASK = 0x01,
111
112 /**
113 * If set, indicates that this colour should be the default foreground
114 * color, i.e. there was no SGR request for another colour. When
115 * rendering this colour it is possible to ignore "idx" and just use a
116 * colour that is not in the palette.
117 */
118 VTERM_COLOR_DEFAULT_FG = 0x02,
119
120 /**
121 * If set, indicates that this colour should be the default background
122 * color, i.e. there was no SGR request for another colour. A common
123 * option when rendering this colour is to not render a background at
124 * all, for example by rendering the window transparently at this spot.
125 */
126 VTERM_COLOR_DEFAULT_BG = 0x04,
127
128 /**
129 * Mask that can be used to extract the default foreground/background bit.
130 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000131 VTERM_COLOR_DEFAULT_MASK = 0x06,
132
133 /**
134 * If set, indicates that the color is invalid.
135 */
136 VTERM_COLOR_INVALID = 0x08
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200137} VTermColorType;
138
139/**
140 * Returns true if the VTERM_COLOR_RGB `type` flag is set, indicating that the
141 * given VTermColor instance is an indexed colour.
142 */
143#define VTERM_COLOR_IS_INDEXED(col) \
144 (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_INDEXED)
145
146/**
147 * Returns true if the VTERM_COLOR_INDEXED `type` flag is set, indicating that
148 * the given VTermColor instance is an rgb colour.
149 */
150#define VTERM_COLOR_IS_RGB(col) \
151 (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_RGB)
152
153/**
154 * Returns true if the VTERM_COLOR_DEFAULT_FG `type` flag is set, indicating
155 * that the given VTermColor instance corresponds to the default foreground
156 * color.
157 */
158#define VTERM_COLOR_IS_DEFAULT_FG(col) \
159 (!!((col)->type & VTERM_COLOR_DEFAULT_FG))
160
161/**
162 * Returns true if the VTERM_COLOR_DEFAULT_BG `type` flag is set, indicating
163 * that the given VTermColor instance corresponds to the default background
164 * color.
165 */
166#define VTERM_COLOR_IS_DEFAULT_BG(col) \
167 (!!((col)->type & VTERM_COLOR_DEFAULT_BG))
Bram Moolenaar46359e12017-11-29 22:33:38 +0100168
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000169/**
170 * Returns true if the VTERM_COLOR_INVALID `type` flag is set, indicating
171 * that the given VTermColor instance is an invalid color.
172 */
173#define VTERM_COLOR_IS_INVALID(col) (!!((col)->type & VTERM_COLOR_INVALID))
174
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200175typedef struct {
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200176 /**
177 * Tag indicating which member is actually valid.
178 * Please use the `VTERM_COLOR_IS_*` test macros to check whether a
179 * particular type flag is set.
180 */
181 uint8_t type;
182
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200183 uint8_t red, green, blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200184
185 uint8_t index;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200186} VTermColor;
187
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200188/**
189 * Constructs a new VTermColor instance representing the given RGB values.
190 */
191void vterm_color_rgb(VTermColor *col, uint8_t red, uint8_t green, uint8_t blue);
192
193/**
194 * Construct a new VTermColor instance representing an indexed color with the
195 * given index.
196 */
197void vterm_color_indexed(VTermColor *col, uint8_t idx);
198
199/**
200 * Compares two colours. Returns true if the colors are equal, false otherwise.
201 */
202int vterm_color_is_equal(const VTermColor *a, const VTermColor *b);
203
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200204typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200205 /* VTERM_VALUETYPE_NONE = 0 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200206 VTERM_VALUETYPE_BOOL = 1,
207 VTERM_VALUETYPE_INT,
208 VTERM_VALUETYPE_STRING,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200209 VTERM_VALUETYPE_COLOR,
210
211 VTERM_N_VALUETYPES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200212} VTermValueType;
213
Bram Moolenaarbe593bf2020-05-19 21:20:04 +0200214typedef struct {
215 const char *str;
216 size_t len : 30;
217 unsigned int initial : 1;
218 unsigned int final : 1;
219} VTermStringFragment;
220
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200221typedef union {
222 int boolean;
223 int number;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +0200224 VTermStringFragment string;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200225 VTermColor color;
226} VTermValue;
227
228typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200229 /* VTERM_ATTR_NONE = 0 */
Bram Moolenaarb691de02018-04-24 18:39:14 +0200230 VTERM_ATTR_BOLD = 1, // bool: 1, 22
231 VTERM_ATTR_UNDERLINE, // number: 4, 21, 24
232 VTERM_ATTR_ITALIC, // bool: 3, 23
233 VTERM_ATTR_BLINK, // bool: 5, 25
234 VTERM_ATTR_REVERSE, // bool: 7, 27
Bram Moolenaard8637282020-05-20 18:41:41 +0200235 VTERM_ATTR_CONCEAL, // bool: 8, 28
Bram Moolenaarb691de02018-04-24 18:39:14 +0200236 VTERM_ATTR_STRIKE, // bool: 9, 29
237 VTERM_ATTR_FONT, // number: 10-19
238 VTERM_ATTR_FOREGROUND, // color: 30-39 90-97
239 VTERM_ATTR_BACKGROUND, // color: 40-49 100-107
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200240
241 VTERM_N_ATTRS
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200242} VTermAttr;
243
244typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200245 /* VTERM_PROP_NONE = 0 */
Bram Moolenaarb691de02018-04-24 18:39:14 +0200246 VTERM_PROP_CURSORVISIBLE = 1, // bool
247 VTERM_PROP_CURSORBLINK, // bool
248 VTERM_PROP_ALTSCREEN, // bool
249 VTERM_PROP_TITLE, // string
250 VTERM_PROP_ICONNAME, // string
251 VTERM_PROP_REVERSE, // bool
252 VTERM_PROP_CURSORSHAPE, // number
253 VTERM_PROP_MOUSE, // number
254 VTERM_PROP_CURSORCOLOR, // string
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200255
256 VTERM_N_PROPS
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200257} VTermProp;
258
259enum {
260 VTERM_PROP_CURSORSHAPE_BLOCK = 1,
261 VTERM_PROP_CURSORSHAPE_UNDERLINE,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200262 VTERM_PROP_CURSORSHAPE_BAR_LEFT,
263
264 VTERM_N_PROP_CURSORSHAPES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200265};
266
267enum {
268 VTERM_PROP_MOUSE_NONE = 0,
269 VTERM_PROP_MOUSE_CLICK,
270 VTERM_PROP_MOUSE_DRAG,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200271 VTERM_PROP_MOUSE_MOVE,
272
273 VTERM_N_PROP_MOUSES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200274};
275
Bram Moolenaar7da34152021-11-24 19:30:55 +0000276typedef enum {
277 VTERM_SELECTION_CLIPBOARD = (1<<0),
278 VTERM_SELECTION_PRIMARY = (1<<1),
279 VTERM_SELECTION_SECONDARY = (1<<2),
280 VTERM_SELECTION_SELECT = (1<<3),
281 VTERM_SELECTION_CUT0 = (1<<4), /* also CUT1 .. CUT7 by bitshifting */
282} VTermSelectionMask;
283
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200284typedef struct {
285 const uint32_t *chars;
286 int width;
Bram Moolenaar591cec82020-05-22 22:06:06 +0200287 unsigned int protected_cell:1; /* DECSCA-protected against DECSEL/DECSED */
288 unsigned int dwl:1; /* DECDWL or DECDHL double-width line */
289 unsigned int dhl:2; /* DECDHL double-height line (1=top 2=bottom) */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200290} VTermGlyphInfo;
291
292typedef struct {
Bram Moolenaar88d68de2020-05-18 21:51:01 +0200293 unsigned int doublewidth:1; /* DECDWL or DECDHL line */
294 unsigned int doubleheight:2; /* DECDHL line (1=top 2=bottom) */
295 unsigned int continuation:1; /* Line is a flow continuation of the previous */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200296} VTermLineInfo;
297
Bram Moolenaar591cec82020-05-22 22:06:06 +0200298/* Copies of VTermState fields that the 'resize' callback might have reason to
299 * edit. 'resize' callback gets total control of these fields and may
300 * free-and-reallocate them if required. They will be copied back from the
301 * struct after the callback has returned.
302 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200303typedef struct {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200304 VTermPos pos; /* current cursor position */
Bram Moolenaar501e7772022-10-16 14:35:46 +0100305 VTermLineInfo *lineinfos[2]; /* [1] may be NULL */
Bram Moolenaar591cec82020-05-22 22:06:06 +0200306} VTermStateFields;
307
308typedef struct {
309 /* libvterm relies on this memory to be zeroed out before it is returned
310 * by the allocator. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200311 void *(*malloc)(size_t size, void *allocdata);
312 void (*free)(void *ptr, void *allocdata);
313} VTermAllocatorFunctions;
314
Bram Moolenaar501e7772022-10-16 14:35:46 +0100315/* A convenient shortcut for default cases */
Bram Moolenaar6fc3b592020-05-17 22:27:55 +0200316void vterm_check_version(int major, int minor);
317
Bram Moolenaar501e7772022-10-16 14:35:46 +0100318struct VTermBuilder {
319 int ver; /* currently unused but reserved for some sort of ABI version flag */
320
321 int rows, cols;
322
323 const VTermAllocatorFunctions *allocator;
324 void *allocdata;
325
326 /* Override default sizes for various structures */
327 size_t outbuffer_len; /* default: 4096 */
328 size_t tmpbuffer_len; /* default: 4096 */
329};
330
331VTerm *vterm_build(const struct VTermBuilder *builder);
332
333/* A convenient shortcut for default cases */
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200334// Allocate and initialize a new terminal with default allocators.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200335VTerm *vterm_new(int rows, int cols);
336
Bram Moolenaar501e7772022-10-16 14:35:46 +0100337/* These shortcuts are generally discouraged in favour of just using vterm_build() */
338
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200339// Allocate and initialize a new terminal with specified allocators.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200340VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata);
341
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200342// Free and cleanup a terminal and all its data.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200343void vterm_free(VTerm* vt);
344
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200345// Get the current size of the terminal and store in "rowsp" and "colsp".
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200346void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200347
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200348void vterm_set_size(VTerm *vt, int rows, int cols);
349
350int vterm_get_utf8(const VTerm *vt);
351void vterm_set_utf8(VTerm *vt, int is_utf8);
352
353size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len);
354
Bram Moolenaar94d729c2020-05-17 21:50:16 +0200355/* Setting output callback will override the buffer logic */
356typedef void VTermOutputCallback(const char *s, size_t len, void *user);
357void vterm_output_set_callback(VTerm *vt, VTermOutputCallback *func, void *user);
358
359/* These buffer functions only work if output callback is NOT set
360 * These are deprecated and will be removed in a later version */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200361size_t vterm_output_get_buffer_size(const VTerm *vt);
362size_t vterm_output_get_buffer_current(const VTerm *vt);
363size_t vterm_output_get_buffer_remaining(const VTerm *vt);
364
Bram Moolenaar94d729c2020-05-17 21:50:16 +0200365/* This too */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200366size_t vterm_output_read(VTerm *vt, char *buffer, size_t len);
367
Bram Moolenaar6a0299d2019-10-10 21:14:03 +0200368int vterm_is_modify_other_keys(VTerm *vt);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200369void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod);
370void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod);
371
372void vterm_keyboard_start_paste(VTerm *vt);
373void vterm_keyboard_end_paste(VTerm *vt);
374
375void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod);
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200376// "button" is 1 for left, 2 for middle, 3 for right.
377// Button 4 is scroll wheel down, button 5 is scroll wheel up.
Bram Moolenaarb2a76ec2017-07-25 21:34:46 +0200378void vterm_mouse_button(VTerm *vt, int button, int pressed, VTermModifier mod);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200379
Bram Moolenaarb691de02018-04-24 18:39:14 +0200380// ------------
381// Parser layer
382// ------------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200383
Bram Moolenaar591cec82020-05-22 22:06:06 +0200384/* Flag to indicate non-final subparameters in a single CSI parameter.
385 * Consider
386 * CSI 1;2:3:4;5a
387 * 1 4 and 5 are final.
388 * 2 and 3 are non-final and will have this bit set
389 *
390 * Don't confuse this with the final byte of the CSI escape; 'a' in this case.
391 */
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200392#define CSI_ARG_FLAG_MORE (1U<<31)
393#define CSI_ARG_MASK (~(1U<<31))
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200394
395#define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE)
396#define CSI_ARG(a) ((a) & CSI_ARG_MASK)
397
Bram Moolenaar591cec82020-05-22 22:06:06 +0200398/* Can't use -1 to indicate a missing argument; use this instead */
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200399#define CSI_ARG_MISSING ((1<<30)-1)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200400
401#define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING)
402#define CSI_ARG_OR(a,def) (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a))
403#define CSI_ARG_COUNT(a) (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a))
404
405typedef struct {
406 int (*text)(const char *bytes, size_t len, void *user);
407 int (*control)(unsigned char control, void *user);
408 int (*escape)(const char *bytes, size_t len, void *user);
409 int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +0200410 int (*osc)(int command, VTermStringFragment frag, void *user);
411 int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user);
Bram Moolenaar7da34152021-11-24 19:30:55 +0000412 int (*apc)(VTermStringFragment frag, void *user);
413 int (*pm)(VTermStringFragment frag, void *user);
414 int (*sos)(VTermStringFragment frag, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200415 int (*resize)(int rows, int cols, void *user);
416} VTermParserCallbacks;
417
418void vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user);
419void *vterm_parser_get_cbdata(VTerm *vt);
420
Bram Moolenaarb691de02018-04-24 18:39:14 +0200421// -----------
422// State layer
423// -----------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200424
425typedef struct {
426 int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user);
427 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
428 int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user);
429 int (*moverect)(VTermRect dest, VTermRect src, void *user);
430 int (*erase)(VTermRect rect, int selective, void *user);
431 int (*initpen)(void *user);
432 int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user);
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200433 // Callback for setting various properties. Must return 1 if the property
434 // was accepted, 0 otherwise.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200435 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
436 int (*bell)(void *user);
Bram Moolenaard098b822020-05-18 21:12:59 +0200437 int (*resize)(int rows, int cols, VTermStateFields *fields, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200438 int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user);
439} VTermStateCallbacks;
440
Bram Moolenaarc48369c2018-03-11 19:30:45 +0100441typedef struct {
442 VTermPos pos;
443 int buttons;
444#define MOUSE_BUTTON_LEFT 0x01
445#define MOUSE_BUTTON_MIDDLE 0x02
446#define MOUSE_BUTTON_RIGHT 0x04
447 int flags;
448#define MOUSE_WANT_CLICK 0x01
449#define MOUSE_WANT_DRAG 0x02
450#define MOUSE_WANT_MOVE 0x04
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200451 // useful to add protocol?
Bram Moolenaarc48369c2018-03-11 19:30:45 +0100452} VTermMouseState;
453
Bram Moolenaard8637282020-05-20 18:41:41 +0200454typedef struct {
455 int (*control)(unsigned char control, void *user);
456 int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
457 int (*osc)(int command, VTermStringFragment frag, void *user);
458 int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user);
Bram Moolenaar7da34152021-11-24 19:30:55 +0000459 int (*apc)(VTermStringFragment frag, void *user);
460 int (*pm)(VTermStringFragment frag, void *user);
461 int (*sos)(VTermStringFragment frag, void *user);
Bram Moolenaard8637282020-05-20 18:41:41 +0200462} VTermStateFallbacks;
463
Bram Moolenaar7da34152021-11-24 19:30:55 +0000464typedef struct {
465 int (*set)(VTermSelectionMask mask, VTermStringFragment frag, void *user);
466 int (*query)(VTermSelectionMask mask, void *user);
467} VTermSelectionCallbacks;
468
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200469VTermState *vterm_obtain_state(VTerm *vt);
470
471void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user);
472void *vterm_state_get_cbdata(VTermState *state);
473
Bram Moolenaard8637282020-05-20 18:41:41 +0200474void vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermStateFallbacks *fallbacks, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200475void *vterm_state_get_unrecognised_fbdata(VTermState *state);
476
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200477// Initialize the state.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200478void vterm_state_reset(VTermState *state, int hard);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200479
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200480void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos);
Bram Moolenaarc48369c2018-03-11 19:30:45 +0100481void vterm_state_get_mousestate(const VTermState *state, VTermMouseState *mousestate);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200482void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg);
483void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col);
484void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg);
485void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col);
486void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright);
487int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val);
488int vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val);
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200489void vterm_state_focus_in(VTermState *state);
490void vterm_state_focus_out(VTermState *state);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200491const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row);
492
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200493/**
494 * Makes sure that the given color `col` is indeed an RGB colour. After this
495 * function returns, VTERM_COLOR_IS_RGB(col) will return true, while all other
496 * flags stored in `col->type` will have been reset.
497 *
498 * @param state is the VTermState instance from which the colour palette should
499 * be extracted.
500 * @param col is a pointer at the VTermColor instance that should be converted
501 * to an RGB colour.
502 */
503void vterm_state_convert_color_to_rgb(const VTermState *state, VTermColor *col);
504
Bram Moolenaar7da34152021-11-24 19:30:55 +0000505void vterm_state_set_selection_callbacks(VTermState *state, const VTermSelectionCallbacks *callbacks, void *user,
506 char *buffer, size_t buflen);
507
508void vterm_state_send_selection(VTermState *state, VTermSelectionMask mask, VTermStringFragment frag);
509
Bram Moolenaarb691de02018-04-24 18:39:14 +0200510// ------------
511// Screen layer
512// ------------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200513
514typedef struct {
515 unsigned int bold : 1;
516 unsigned int underline : 2;
517 unsigned int italic : 1;
518 unsigned int blink : 1;
519 unsigned int reverse : 1;
Bram Moolenaard8637282020-05-20 18:41:41 +0200520 unsigned int conceal : 1;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200521 unsigned int strike : 1;
Bram Moolenaar591cec82020-05-22 22:06:06 +0200522 unsigned int font : 4; /* 0 to 9 */
523 unsigned int dwl : 1; /* On a DECDWL or DECDHL line */
524 unsigned int dhl : 2; /* On a DECDHL line (1=top 2=bottom) */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200525} VTermScreenCellAttrs;
526
Bram Moolenaar6fc3b592020-05-17 22:27:55 +0200527enum {
528 VTERM_UNDERLINE_OFF,
529 VTERM_UNDERLINE_SINGLE,
530 VTERM_UNDERLINE_DOUBLE,
531 VTERM_UNDERLINE_CURLY,
532};
533
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200534typedef struct {
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200535 uint32_t chars[VTERM_MAX_CHARS_PER_CELL];
536 char width;
537 VTermScreenCellAttrs attrs;
538 VTermColor fg, bg;
539} VTermScreenCell;
540
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200541// All fields are optional, NULL when not used.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200542typedef struct {
543 int (*damage)(VTermRect rect, void *user);
544 int (*moverect)(VTermRect dest, VTermRect src, void *user);
545 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
546 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
547 int (*bell)(void *user);
548 int (*resize)(int rows, int cols, void *user);
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200549 // A line was pushed off the top of the window.
550 // "cells[cols]" contains the cells of that line.
551 // Return value is unused.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200552 int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user);
553 int (*sb_popline)(int cols, VTermScreenCell *cells, void *user);
554} VTermScreenCallbacks;
555
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200556// Return the screen of the vterm.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200557VTermScreen *vterm_obtain_screen(VTerm *vt);
558
559/*
560 * Install screen callbacks. These are invoked when the screen contents is
561 * changed. "user" is passed into to the callback.
562 */
563void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user);
564void *vterm_screen_get_cbdata(VTermScreen *screen);
565
Bram Moolenaard8637282020-05-20 18:41:41 +0200566void vterm_screen_set_unrecognised_fallbacks(VTermScreen *screen, const VTermStateFallbacks *fallbacks, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200567void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen);
568
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200569// Enable support for using the alternate screen if "altscreen" is non-zero.
570// Before that switching to the alternate screen won't work.
571// Calling with "altscreen" zero has no effect.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200572void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen);
573
574typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200575 VTERM_DAMAGE_CELL, /* every cell */
576 VTERM_DAMAGE_ROW, /* entire rows */
577 VTERM_DAMAGE_SCREEN, /* entire screen */
578 VTERM_DAMAGE_SCROLL, /* entire screen + scrollrect */
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200579
580 VTERM_N_DAMAGES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200581} VTermDamageSize;
582
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200583// Invoke the relevant callbacks to update the screen.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200584void vterm_screen_flush_damage(VTermScreen *screen);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200585
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200586void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size);
587
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200588/*
589 * Reset the screen. Also invokes vterm_state_reset().
590 * Must be called before the terminal can be used.
591 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200592void vterm_screen_reset(VTermScreen *screen, int hard);
593
Bram Moolenaar591cec82020-05-22 22:06:06 +0200594/* Neither of these functions NUL-terminate the buffer */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200595size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect);
596size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect);
597
598typedef enum {
599 VTERM_ATTR_BOLD_MASK = 1 << 0,
600 VTERM_ATTR_UNDERLINE_MASK = 1 << 1,
601 VTERM_ATTR_ITALIC_MASK = 1 << 2,
602 VTERM_ATTR_BLINK_MASK = 1 << 3,
603 VTERM_ATTR_REVERSE_MASK = 1 << 4,
604 VTERM_ATTR_STRIKE_MASK = 1 << 5,
605 VTERM_ATTR_FONT_MASK = 1 << 6,
606 VTERM_ATTR_FOREGROUND_MASK = 1 << 7,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200607 VTERM_ATTR_BACKGROUND_MASK = 1 << 8,
Bram Moolenaard8637282020-05-20 18:41:41 +0200608 VTERM_ATTR_CONCEAL_MASK = 1 << 9,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200609
Bram Moolenaard8637282020-05-20 18:41:41 +0200610 VTERM_ALL_ATTRS_MASK = (1 << 10) - 1
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200611} VTermAttrMask;
612
613int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs);
614
615int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell);
616
617int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos);
618
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200619/**
620 * Same as vterm_state_convert_color_to_rgb(), but takes a `screen` instead of a `state`
621 * instance.
622 */
623void vterm_screen_convert_color_to_rgb(const VTermScreen *screen, VTermColor *col);
624
Bram Moolenaarb691de02018-04-24 18:39:14 +0200625// ---------
626// Utilities
627// ---------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200628
629VTermValueType vterm_get_attr_type(VTermAttr attr);
630VTermValueType vterm_get_prop_type(VTermProp prop);
631
632void vterm_scroll_rect(VTermRect rect,
633 int downward,
634 int rightward,
635 int (*moverect)(VTermRect src, VTermRect dest, void *user),
636 int (*eraserect)(VTermRect rect, int selective, void *user),
637 void *user);
638
639void vterm_copy_cells(VTermRect dest,
640 VTermRect src,
641 void (*copycell)(VTermPos dest, VTermPos src, void *user),
642 void *user);
643
644#ifdef __cplusplus
645}
646#endif
647
648#endif