blob: d553fbd51a4419ec6a25f0b93af88f55b269415b [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 Moolenaare4f25e42017-07-07 11:54:15 +020029typedef struct VTerm VTerm;
30typedef struct VTermState VTermState;
31typedef struct VTermScreen VTermScreen;
32
Bram Moolenaardb1085a2019-08-18 20:41:38 +020033// Specifies a screen point.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034typedef struct {
35 int row;
36 int col;
37} VTermPos;
38
Bram Moolenaar591cec82020-05-22 22:06:06 +020039/* some small utility functions; we can just keep these static here */
Bram Moolenaare4f25e42017-07-07 11:54:15 +020040
41/*
42 * Order points by on-screen flow order:
43 * Return < 0 if "a" is before "b"
44 * Return 0 if "a" and "b" are equal
45 * Return > 0 if "a" is after "b".
46 */
47int vterm_pos_cmp(VTermPos a, VTermPos b);
48
49#if defined(DEFINE_INLINES) || USE_INLINE
50INLINE int vterm_pos_cmp(VTermPos a, VTermPos b)
51{
52 return (a.row == b.row) ? a.col - b.col : a.row - b.row;
53}
54#endif
55
Bram Moolenaardb1085a2019-08-18 20:41:38 +020056// Specifies a rectangular screen area.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020057typedef struct {
58 int start_row;
59 int end_row;
60 int start_col;
61 int end_col;
62} VTermRect;
63
Bram Moolenaar591cec82020-05-22 22:06:06 +020064/* true if the rect contains the point */
Bram Moolenaare4f25e42017-07-07 11:54:15 +020065int vterm_rect_contains(VTermRect r, VTermPos p);
66
67#if defined(DEFINE_INLINES) || USE_INLINE
68INLINE int vterm_rect_contains(VTermRect r, VTermPos p)
69{
70 return p.row >= r.start_row && p.row < r.end_row &&
71 p.col >= r.start_col && p.col < r.end_col;
72}
73#endif
74
Bram Moolenaar591cec82020-05-22 22:06:06 +020075/* move a rect */
Bram Moolenaardb1085a2019-08-18 20:41:38 +020076// Move "rect" "row_delta" down and "col_delta" right.
77// Does not check boundaries.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta);
79
80#if defined(DEFINE_INLINES) || USE_INLINE
81INLINE void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta)
82{
83 rect->start_row += row_delta; rect->end_row += row_delta;
84 rect->start_col += col_delta; rect->end_col += col_delta;
85}
86#endif
87
Bram Moolenaare5886cc2020-05-21 20:10:04 +020088/**
89 * Bit-field describing the value of VTermColor.type
90 */
91typedef enum {
92 /**
93 * If the lower bit of `type` is not set, the colour is 24-bit RGB.
94 */
95 VTERM_COLOR_RGB = 0x00,
96
97 /**
98 * The colour is an index into a palette of 256 colours.
99 */
100 VTERM_COLOR_INDEXED = 0x01,
101
102 /**
103 * Mask that can be used to extract the RGB/Indexed bit.
104 */
105 VTERM_COLOR_TYPE_MASK = 0x01,
106
107 /**
108 * If set, indicates that this colour should be the default foreground
109 * color, i.e. there was no SGR request for another colour. When
110 * rendering this colour it is possible to ignore "idx" and just use a
111 * colour that is not in the palette.
112 */
113 VTERM_COLOR_DEFAULT_FG = 0x02,
114
115 /**
116 * If set, indicates that this colour should be the default background
117 * color, i.e. there was no SGR request for another colour. A common
118 * option when rendering this colour is to not render a background at
119 * all, for example by rendering the window transparently at this spot.
120 */
121 VTERM_COLOR_DEFAULT_BG = 0x04,
122
123 /**
124 * Mask that can be used to extract the default foreground/background bit.
125 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000126 VTERM_COLOR_DEFAULT_MASK = 0x06,
127
128 /**
129 * If set, indicates that the color is invalid.
130 */
131 VTERM_COLOR_INVALID = 0x08
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200132} VTermColorType;
133
134/**
135 * Returns true if the VTERM_COLOR_RGB `type` flag is set, indicating that the
136 * given VTermColor instance is an indexed colour.
137 */
138#define VTERM_COLOR_IS_INDEXED(col) \
139 (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_INDEXED)
140
141/**
142 * Returns true if the VTERM_COLOR_INDEXED `type` flag is set, indicating that
143 * the given VTermColor instance is an rgb colour.
144 */
145#define VTERM_COLOR_IS_RGB(col) \
146 (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_RGB)
147
148/**
149 * Returns true if the VTERM_COLOR_DEFAULT_FG `type` flag is set, indicating
150 * that the given VTermColor instance corresponds to the default foreground
151 * color.
152 */
153#define VTERM_COLOR_IS_DEFAULT_FG(col) \
154 (!!((col)->type & VTERM_COLOR_DEFAULT_FG))
155
156/**
157 * Returns true if the VTERM_COLOR_DEFAULT_BG `type` flag is set, indicating
158 * that the given VTermColor instance corresponds to the default background
159 * color.
160 */
161#define VTERM_COLOR_IS_DEFAULT_BG(col) \
162 (!!((col)->type & VTERM_COLOR_DEFAULT_BG))
Bram Moolenaar46359e12017-11-29 22:33:38 +0100163
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000164/**
165 * Returns true if the VTERM_COLOR_INVALID `type` flag is set, indicating
166 * that the given VTermColor instance is an invalid color.
167 */
168#define VTERM_COLOR_IS_INVALID(col) (!!((col)->type & VTERM_COLOR_INVALID))
169
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200170typedef struct {
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200171 /**
172 * Tag indicating which member is actually valid.
173 * Please use the `VTERM_COLOR_IS_*` test macros to check whether a
174 * particular type flag is set.
175 */
176 uint8_t type;
177
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200178 uint8_t red, green, blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200179
180 uint8_t index;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200181} VTermColor;
182
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200183/**
184 * Constructs a new VTermColor instance representing the given RGB values.
185 */
186void vterm_color_rgb(VTermColor *col, uint8_t red, uint8_t green, uint8_t blue);
187
188/**
189 * Construct a new VTermColor instance representing an indexed color with the
190 * given index.
191 */
192void vterm_color_indexed(VTermColor *col, uint8_t idx);
193
194/**
195 * Compares two colours. Returns true if the colors are equal, false otherwise.
196 */
197int vterm_color_is_equal(const VTermColor *a, const VTermColor *b);
198
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200199typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200200 /* VTERM_VALUETYPE_NONE = 0 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200201 VTERM_VALUETYPE_BOOL = 1,
202 VTERM_VALUETYPE_INT,
203 VTERM_VALUETYPE_STRING,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200204 VTERM_VALUETYPE_COLOR,
205
206 VTERM_N_VALUETYPES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200207} VTermValueType;
208
Bram Moolenaarbe593bf2020-05-19 21:20:04 +0200209typedef struct {
210 const char *str;
211 size_t len : 30;
212 unsigned int initial : 1;
213 unsigned int final : 1;
214} VTermStringFragment;
215
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200216typedef union {
217 int boolean;
218 int number;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +0200219 VTermStringFragment string;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200220 VTermColor color;
221} VTermValue;
222
223typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200224 /* VTERM_ATTR_NONE = 0 */
Bram Moolenaarb691de02018-04-24 18:39:14 +0200225 VTERM_ATTR_BOLD = 1, // bool: 1, 22
226 VTERM_ATTR_UNDERLINE, // number: 4, 21, 24
227 VTERM_ATTR_ITALIC, // bool: 3, 23
228 VTERM_ATTR_BLINK, // bool: 5, 25
229 VTERM_ATTR_REVERSE, // bool: 7, 27
Bram Moolenaard8637282020-05-20 18:41:41 +0200230 VTERM_ATTR_CONCEAL, // bool: 8, 28
Bram Moolenaarb691de02018-04-24 18:39:14 +0200231 VTERM_ATTR_STRIKE, // bool: 9, 29
232 VTERM_ATTR_FONT, // number: 10-19
233 VTERM_ATTR_FOREGROUND, // color: 30-39 90-97
234 VTERM_ATTR_BACKGROUND, // color: 40-49 100-107
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200235
236 VTERM_N_ATTRS
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200237} VTermAttr;
238
239typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200240 /* VTERM_PROP_NONE = 0 */
Bram Moolenaarb691de02018-04-24 18:39:14 +0200241 VTERM_PROP_CURSORVISIBLE = 1, // bool
242 VTERM_PROP_CURSORBLINK, // bool
243 VTERM_PROP_ALTSCREEN, // bool
244 VTERM_PROP_TITLE, // string
245 VTERM_PROP_ICONNAME, // string
246 VTERM_PROP_REVERSE, // bool
247 VTERM_PROP_CURSORSHAPE, // number
248 VTERM_PROP_MOUSE, // number
249 VTERM_PROP_CURSORCOLOR, // string
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200250
251 VTERM_N_PROPS
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252} VTermProp;
253
254enum {
255 VTERM_PROP_CURSORSHAPE_BLOCK = 1,
256 VTERM_PROP_CURSORSHAPE_UNDERLINE,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200257 VTERM_PROP_CURSORSHAPE_BAR_LEFT,
258
259 VTERM_N_PROP_CURSORSHAPES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200260};
261
262enum {
263 VTERM_PROP_MOUSE_NONE = 0,
264 VTERM_PROP_MOUSE_CLICK,
265 VTERM_PROP_MOUSE_DRAG,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200266 VTERM_PROP_MOUSE_MOVE,
267
268 VTERM_N_PROP_MOUSES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200269};
270
Bram Moolenaar7da34152021-11-24 19:30:55 +0000271typedef enum {
272 VTERM_SELECTION_CLIPBOARD = (1<<0),
273 VTERM_SELECTION_PRIMARY = (1<<1),
274 VTERM_SELECTION_SECONDARY = (1<<2),
275 VTERM_SELECTION_SELECT = (1<<3),
276 VTERM_SELECTION_CUT0 = (1<<4), /* also CUT1 .. CUT7 by bitshifting */
277} VTermSelectionMask;
278
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200279typedef struct {
280 const uint32_t *chars;
281 int width;
Bram Moolenaar591cec82020-05-22 22:06:06 +0200282 unsigned int protected_cell:1; /* DECSCA-protected against DECSEL/DECSED */
283 unsigned int dwl:1; /* DECDWL or DECDHL double-width line */
284 unsigned int dhl:2; /* DECDHL double-height line (1=top 2=bottom) */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200285} VTermGlyphInfo;
286
287typedef struct {
Bram Moolenaar88d68de2020-05-18 21:51:01 +0200288 unsigned int doublewidth:1; /* DECDWL or DECDHL line */
289 unsigned int doubleheight:2; /* DECDHL line (1=top 2=bottom) */
290 unsigned int continuation:1; /* Line is a flow continuation of the previous */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200291} VTermLineInfo;
292
Bram Moolenaar591cec82020-05-22 22:06:06 +0200293/* Copies of VTermState fields that the 'resize' callback might have reason to
294 * edit. 'resize' callback gets total control of these fields and may
295 * free-and-reallocate them if required. They will be copied back from the
296 * struct after the callback has returned.
297 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200298typedef struct {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200299 VTermPos pos; /* current cursor position */
300} VTermStateFields;
301
302typedef struct {
303 /* libvterm relies on this memory to be zeroed out before it is returned
304 * by the allocator. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200305 void *(*malloc)(size_t size, void *allocdata);
306 void (*free)(void *ptr, void *allocdata);
307} VTermAllocatorFunctions;
308
Bram Moolenaar6fc3b592020-05-17 22:27:55 +0200309void vterm_check_version(int major, int minor);
310
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200311// Allocate and initialize a new terminal with default allocators.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200312VTerm *vterm_new(int rows, int cols);
313
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200314// Allocate and initialize a new terminal with specified allocators.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200315VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata);
316
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200317// Free and cleanup a terminal and all its data.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200318void vterm_free(VTerm* vt);
319
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200320// Get the current size of the terminal and store in "rowsp" and "colsp".
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200321void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200322
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200323void vterm_set_size(VTerm *vt, int rows, int cols);
324
325int vterm_get_utf8(const VTerm *vt);
326void vterm_set_utf8(VTerm *vt, int is_utf8);
327
328size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len);
329
Bram Moolenaar94d729c2020-05-17 21:50:16 +0200330/* Setting output callback will override the buffer logic */
331typedef void VTermOutputCallback(const char *s, size_t len, void *user);
332void vterm_output_set_callback(VTerm *vt, VTermOutputCallback *func, void *user);
333
334/* These buffer functions only work if output callback is NOT set
335 * These are deprecated and will be removed in a later version */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200336size_t vterm_output_get_buffer_size(const VTerm *vt);
337size_t vterm_output_get_buffer_current(const VTerm *vt);
338size_t vterm_output_get_buffer_remaining(const VTerm *vt);
339
Bram Moolenaar94d729c2020-05-17 21:50:16 +0200340/* This too */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200341size_t vterm_output_read(VTerm *vt, char *buffer, size_t len);
342
Bram Moolenaar6a0299d2019-10-10 21:14:03 +0200343int vterm_is_modify_other_keys(VTerm *vt);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200344void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod);
345void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod);
346
347void vterm_keyboard_start_paste(VTerm *vt);
348void vterm_keyboard_end_paste(VTerm *vt);
349
350void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod);
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200351// "button" is 1 for left, 2 for middle, 3 for right.
352// Button 4 is scroll wheel down, button 5 is scroll wheel up.
Bram Moolenaarb2a76ec2017-07-25 21:34:46 +0200353void vterm_mouse_button(VTerm *vt, int button, int pressed, VTermModifier mod);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200354
Bram Moolenaarb691de02018-04-24 18:39:14 +0200355// ------------
356// Parser layer
357// ------------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200358
Bram Moolenaar591cec82020-05-22 22:06:06 +0200359/* Flag to indicate non-final subparameters in a single CSI parameter.
360 * Consider
361 * CSI 1;2:3:4;5a
362 * 1 4 and 5 are final.
363 * 2 and 3 are non-final and will have this bit set
364 *
365 * Don't confuse this with the final byte of the CSI escape; 'a' in this case.
366 */
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200367#define CSI_ARG_FLAG_MORE (1U<<31)
368#define CSI_ARG_MASK (~(1U<<31))
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200369
370#define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE)
371#define CSI_ARG(a) ((a) & CSI_ARG_MASK)
372
Bram Moolenaar591cec82020-05-22 22:06:06 +0200373/* Can't use -1 to indicate a missing argument; use this instead */
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200374#define CSI_ARG_MISSING ((1<<30)-1)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200375
376#define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING)
377#define CSI_ARG_OR(a,def) (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a))
378#define CSI_ARG_COUNT(a) (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a))
379
380typedef struct {
381 int (*text)(const char *bytes, size_t len, void *user);
382 int (*control)(unsigned char control, void *user);
383 int (*escape)(const char *bytes, size_t len, void *user);
384 int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +0200385 int (*osc)(int command, VTermStringFragment frag, void *user);
386 int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user);
Bram Moolenaar7da34152021-11-24 19:30:55 +0000387 int (*apc)(VTermStringFragment frag, void *user);
388 int (*pm)(VTermStringFragment frag, void *user);
389 int (*sos)(VTermStringFragment frag, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200390 int (*resize)(int rows, int cols, void *user);
391} VTermParserCallbacks;
392
393void vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user);
394void *vterm_parser_get_cbdata(VTerm *vt);
395
Bram Moolenaarb691de02018-04-24 18:39:14 +0200396// -----------
397// State layer
398// -----------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200399
400typedef struct {
401 int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user);
402 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
403 int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user);
404 int (*moverect)(VTermRect dest, VTermRect src, void *user);
405 int (*erase)(VTermRect rect, int selective, void *user);
406 int (*initpen)(void *user);
407 int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user);
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200408 // Callback for setting various properties. Must return 1 if the property
409 // was accepted, 0 otherwise.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200410 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
411 int (*bell)(void *user);
Bram Moolenaard098b822020-05-18 21:12:59 +0200412 int (*resize)(int rows, int cols, VTermStateFields *fields, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200413 int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user);
414} VTermStateCallbacks;
415
Bram Moolenaarc48369c2018-03-11 19:30:45 +0100416typedef struct {
417 VTermPos pos;
418 int buttons;
419#define MOUSE_BUTTON_LEFT 0x01
420#define MOUSE_BUTTON_MIDDLE 0x02
421#define MOUSE_BUTTON_RIGHT 0x04
422 int flags;
423#define MOUSE_WANT_CLICK 0x01
424#define MOUSE_WANT_DRAG 0x02
425#define MOUSE_WANT_MOVE 0x04
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200426 // useful to add protocol?
Bram Moolenaarc48369c2018-03-11 19:30:45 +0100427} VTermMouseState;
428
Bram Moolenaard8637282020-05-20 18:41:41 +0200429typedef struct {
430 int (*control)(unsigned char control, void *user);
431 int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
432 int (*osc)(int command, VTermStringFragment frag, void *user);
433 int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user);
Bram Moolenaar7da34152021-11-24 19:30:55 +0000434 int (*apc)(VTermStringFragment frag, void *user);
435 int (*pm)(VTermStringFragment frag, void *user);
436 int (*sos)(VTermStringFragment frag, void *user);
Bram Moolenaard8637282020-05-20 18:41:41 +0200437} VTermStateFallbacks;
438
Bram Moolenaar7da34152021-11-24 19:30:55 +0000439typedef struct {
440 int (*set)(VTermSelectionMask mask, VTermStringFragment frag, void *user);
441 int (*query)(VTermSelectionMask mask, void *user);
442} VTermSelectionCallbacks;
443
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200444VTermState *vterm_obtain_state(VTerm *vt);
445
446void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user);
447void *vterm_state_get_cbdata(VTermState *state);
448
Bram Moolenaard8637282020-05-20 18:41:41 +0200449void vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermStateFallbacks *fallbacks, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200450void *vterm_state_get_unrecognised_fbdata(VTermState *state);
451
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200452// Initialize the state.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200453void vterm_state_reset(VTermState *state, int hard);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200454
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200455void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos);
Bram Moolenaarc48369c2018-03-11 19:30:45 +0100456void vterm_state_get_mousestate(const VTermState *state, VTermMouseState *mousestate);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200457void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg);
458void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col);
459void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg);
460void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col);
461void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright);
462int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val);
463int vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val);
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200464void vterm_state_focus_in(VTermState *state);
465void vterm_state_focus_out(VTermState *state);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200466const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row);
467
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200468/**
469 * Makes sure that the given color `col` is indeed an RGB colour. After this
470 * function returns, VTERM_COLOR_IS_RGB(col) will return true, while all other
471 * flags stored in `col->type` will have been reset.
472 *
473 * @param state is the VTermState instance from which the colour palette should
474 * be extracted.
475 * @param col is a pointer at the VTermColor instance that should be converted
476 * to an RGB colour.
477 */
478void vterm_state_convert_color_to_rgb(const VTermState *state, VTermColor *col);
479
Bram Moolenaar7da34152021-11-24 19:30:55 +0000480void vterm_state_set_selection_callbacks(VTermState *state, const VTermSelectionCallbacks *callbacks, void *user,
481 char *buffer, size_t buflen);
482
483void vterm_state_send_selection(VTermState *state, VTermSelectionMask mask, VTermStringFragment frag);
484
Bram Moolenaarb691de02018-04-24 18:39:14 +0200485// ------------
486// Screen layer
487// ------------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200488
489typedef struct {
490 unsigned int bold : 1;
491 unsigned int underline : 2;
492 unsigned int italic : 1;
493 unsigned int blink : 1;
494 unsigned int reverse : 1;
Bram Moolenaard8637282020-05-20 18:41:41 +0200495 unsigned int conceal : 1;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200496 unsigned int strike : 1;
Bram Moolenaar591cec82020-05-22 22:06:06 +0200497 unsigned int font : 4; /* 0 to 9 */
498 unsigned int dwl : 1; /* On a DECDWL or DECDHL line */
499 unsigned int dhl : 2; /* On a DECDHL line (1=top 2=bottom) */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200500} VTermScreenCellAttrs;
501
Bram Moolenaar6fc3b592020-05-17 22:27:55 +0200502enum {
503 VTERM_UNDERLINE_OFF,
504 VTERM_UNDERLINE_SINGLE,
505 VTERM_UNDERLINE_DOUBLE,
506 VTERM_UNDERLINE_CURLY,
507};
508
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200509typedef struct {
510#define VTERM_MAX_CHARS_PER_CELL 6
511 uint32_t chars[VTERM_MAX_CHARS_PER_CELL];
512 char width;
513 VTermScreenCellAttrs attrs;
514 VTermColor fg, bg;
515} VTermScreenCell;
516
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200517// All fields are optional, NULL when not used.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200518typedef struct {
519 int (*damage)(VTermRect rect, void *user);
520 int (*moverect)(VTermRect dest, VTermRect src, void *user);
521 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
522 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
523 int (*bell)(void *user);
524 int (*resize)(int rows, int cols, void *user);
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200525 // A line was pushed off the top of the window.
526 // "cells[cols]" contains the cells of that line.
527 // Return value is unused.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200528 int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user);
529 int (*sb_popline)(int cols, VTermScreenCell *cells, void *user);
530} VTermScreenCallbacks;
531
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200532// Return the screen of the vterm.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200533VTermScreen *vterm_obtain_screen(VTerm *vt);
534
535/*
536 * Install screen callbacks. These are invoked when the screen contents is
537 * changed. "user" is passed into to the callback.
538 */
539void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user);
540void *vterm_screen_get_cbdata(VTermScreen *screen);
541
Bram Moolenaard8637282020-05-20 18:41:41 +0200542void vterm_screen_set_unrecognised_fallbacks(VTermScreen *screen, const VTermStateFallbacks *fallbacks, void *user);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200543void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen);
544
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200545// Enable support for using the alternate screen if "altscreen" is non-zero.
546// Before that switching to the alternate screen won't work.
547// Calling with "altscreen" zero has no effect.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200548void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen);
549
550typedef enum {
Bram Moolenaar591cec82020-05-22 22:06:06 +0200551 VTERM_DAMAGE_CELL, /* every cell */
552 VTERM_DAMAGE_ROW, /* entire rows */
553 VTERM_DAMAGE_SCREEN, /* entire screen */
554 VTERM_DAMAGE_SCROLL, /* entire screen + scrollrect */
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200555
556 VTERM_N_DAMAGES
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200557} VTermDamageSize;
558
Bram Moolenaardb1085a2019-08-18 20:41:38 +0200559// Invoke the relevant callbacks to update the screen.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200560void vterm_screen_flush_damage(VTermScreen *screen);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200561
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200562void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size);
563
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200564/*
565 * Reset the screen. Also invokes vterm_state_reset().
566 * Must be called before the terminal can be used.
567 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200568void vterm_screen_reset(VTermScreen *screen, int hard);
569
Bram Moolenaar591cec82020-05-22 22:06:06 +0200570/* Neither of these functions NUL-terminate the buffer */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200571size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect);
572size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect);
573
574typedef enum {
575 VTERM_ATTR_BOLD_MASK = 1 << 0,
576 VTERM_ATTR_UNDERLINE_MASK = 1 << 1,
577 VTERM_ATTR_ITALIC_MASK = 1 << 2,
578 VTERM_ATTR_BLINK_MASK = 1 << 3,
579 VTERM_ATTR_REVERSE_MASK = 1 << 4,
580 VTERM_ATTR_STRIKE_MASK = 1 << 5,
581 VTERM_ATTR_FONT_MASK = 1 << 6,
582 VTERM_ATTR_FOREGROUND_MASK = 1 << 7,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200583 VTERM_ATTR_BACKGROUND_MASK = 1 << 8,
Bram Moolenaard8637282020-05-20 18:41:41 +0200584 VTERM_ATTR_CONCEAL_MASK = 1 << 9,
Bram Moolenaarb5b49a32018-03-25 16:20:37 +0200585
Bram Moolenaard8637282020-05-20 18:41:41 +0200586 VTERM_ALL_ATTRS_MASK = (1 << 10) - 1
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200587} VTermAttrMask;
588
589int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs);
590
591int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell);
592
593int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos);
594
Bram Moolenaare5886cc2020-05-21 20:10:04 +0200595/**
596 * Same as vterm_state_convert_color_to_rgb(), but takes a `screen` instead of a `state`
597 * instance.
598 */
599void vterm_screen_convert_color_to_rgb(const VTermScreen *screen, VTermColor *col);
600
Bram Moolenaarb691de02018-04-24 18:39:14 +0200601// ---------
602// Utilities
603// ---------
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200604
605VTermValueType vterm_get_attr_type(VTermAttr attr);
606VTermValueType vterm_get_prop_type(VTermProp prop);
607
608void vterm_scroll_rect(VTermRect rect,
609 int downward,
610 int rightward,
611 int (*moverect)(VTermRect src, VTermRect dest, void *user),
612 int (*eraserect)(VTermRect rect, int selective, void *user),
613 void *user);
614
615void vterm_copy_cells(VTermRect dest,
616 VTermRect src,
617 void (*copycell)(VTermPos dest, VTermPos src, void *user),
618 void *user);
619
620#ifdef __cplusplus
621}
622#endif
623
624#endif