robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

mgleonard425feat(terminal): synchronized output (mode 2026) #38284b38173e493

master
10.5 KiB337 linesraw
1#pragma once
2#include <stdbool.h>
3#include <stddef.h>
4#include <stdint.h>
5
6#include "nvim/types_defs.h"
7
8typedef struct VTerm VTerm;
9typedef struct VTermState VTermState;
10typedef struct VTermScreen VTermScreen;
11
12typedef struct {
13  int row;
14  int col;
15} VTermPos;
16
17// some small utility functions; we can just keep these static here
18
19typedef struct {
20  int start_row;
21  int end_row;
22  int start_col;
23  int end_col;
24} VTermRect;
25
26// Tagged union storing either an RGB color or an index into a colour palette. In order to convert
27// indexed colours to RGB, you may use the vterm_state_convert_color_to_rgb() or
28// vterm_screen_convert_color_to_rgb() functions which lookup the RGB colour from the palette
29// maintained by a VTermState or VTermScreen instance.
30typedef union {
31  // Tag indicating which union member is actually valid. This variable coincides with the `type`
32  // member of the `rgb` and the `indexed` struct in memory. Please use the `VTERM_COLOR_IS_*` test
33  // macros to check whether a particular type flag is set.
34  uint8_t type;
35
36  // Valid if `VTERM_COLOR_IS_RGB(type)` is true. Holds the RGB colour values.
37  struct {
38    // Same as the top-level `type` member stored in VTermColor.
39    uint8_t type;
40
41    // The actual 8-bit red, green, blue colour values.
42    uint8_t red, green, blue;
43  } rgb;
44
45  // If `VTERM_COLOR_IS_INDEXED(type)` is true, this member holds the index into the colour palette.
46  struct {
47    // Same as the top-level `type` member stored in VTermColor.
48    uint8_t type;
49
50    // Index into the colour map.
51    uint8_t idx;
52  } indexed;
53} VTermColor;
54
55typedef struct {
56  unsigned bold      : 1;
57  unsigned underline : 2;
58  unsigned italic    : 1;
59  unsigned blink     : 1;
60  unsigned reverse   : 1;
61  unsigned conceal   : 1;
62  unsigned strike    : 1;
63  unsigned font      : 4;  // 0 to 9
64  unsigned dwl       : 1;  // On a DECDWL or DECDHL line
65  unsigned dhl       : 2;  // On a DECDHL line (1=top 2=bottom)
66  unsigned small     : 1;
67  unsigned baseline  : 2;
68  unsigned dim       : 1;
69  unsigned overline  : 1;
70} VTermScreenCellAttrs;
71
72typedef struct {
73  schar_T schar;
74  char width;
75  VTermScreenCellAttrs attrs;
76  VTermColor fg, bg;
77  int uri;
78} VTermScreenCell;
79
80typedef enum {
81  // VTERM_PROP_NONE = 0
82  VTERM_PROP_CURSORVISIBLE = 1,  // bool
83  VTERM_PROP_CURSORBLINK,       // bool
84  VTERM_PROP_ALTSCREEN,         // bool
85  VTERM_PROP_TITLE,             // string
86  VTERM_PROP_ICONNAME,          // string
87  VTERM_PROP_REVERSE,           // bool
88  VTERM_PROP_CURSORSHAPE,       // number
89  VTERM_PROP_MOUSE,             // number
90  VTERM_PROP_FOCUSREPORT,       // bool
91  VTERM_PROP_THEMEUPDATES,      // bool
92  VTERM_PROP_SYNCOUTPUT,        // bool
93
94  VTERM_N_PROPS,
95} VTermProp;
96
97typedef enum {
98  VTERM_TERMINATOR_BEL,  // \x07
99  VTERM_TERMINATOR_ST,  // \x1b\x5c
100} VTermTerminator;
101
102typedef struct {
103  const char *str;
104  size_t len : 30;
105  bool initial : 1;
106  bool final : 1;
107  VTermTerminator terminator;
108} VTermStringFragment;
109
110typedef union {
111  int boolean;
112  int number;
113  VTermStringFragment string;
114  VTermColor color;
115} VTermValue;
116
117typedef struct {
118  int (*damage)(VTermRect rect, void *user);
119  int (*moverect)(VTermRect dest, VTermRect src, void *user);
120  int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
121  int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
122  int (*bell)(void *user);
123  int (*resize)(int rows, int cols, void *user);
124  int (*theme)(bool *dark, void *user);
125  int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user);
126  int (*sb_popline)(int cols, VTermScreenCell *cells, void *user);
127  int (*sb_clear)(void *user);
128} VTermScreenCallbacks;
129
130typedef struct {
131  int (*control)(uint8_t control, void *user);
132  int (*csi)(const char *leader, const long args[], int argcount, const char *intermed,
133             char command, void *user);
134  int (*osc)(int command, VTermStringFragment frag, void *user);
135  int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user);
136  int (*apc)(VTermStringFragment frag, void *user);
137  int (*pm)(VTermStringFragment frag, void *user);
138  int (*sos)(VTermStringFragment frag, void *user);
139} VTermStateFallbacks;
140
141typedef enum {
142  VTERM_DAMAGE_CELL,    // every cell
143  VTERM_DAMAGE_ROW,     // entire rows
144  VTERM_DAMAGE_SCREEN,  // entire screen
145  VTERM_DAMAGE_SCROLL,  // entire screen + scrollrect
146
147  VTERM_N_DAMAGES,
148} VTermDamageSize;
149
150typedef enum {
151  VTERM_ATTR_BOLD_MASK       = 1 << 0,
152  VTERM_ATTR_UNDERLINE_MASK  = 1 << 1,
153  VTERM_ATTR_ITALIC_MASK     = 1 << 2,
154  VTERM_ATTR_BLINK_MASK      = 1 << 3,
155  VTERM_ATTR_REVERSE_MASK    = 1 << 4,
156  VTERM_ATTR_STRIKE_MASK     = 1 << 5,
157  VTERM_ATTR_FONT_MASK       = 1 << 6,
158  VTERM_ATTR_FOREGROUND_MASK = 1 << 7,
159  VTERM_ATTR_BACKGROUND_MASK = 1 << 8,
160  VTERM_ATTR_CONCEAL_MASK    = 1 << 9,
161  VTERM_ATTR_SMALL_MASK      = 1 << 10,
162  VTERM_ATTR_BASELINE_MASK   = 1 << 11,
163  VTERM_ATTR_URI_MASK        = 1 << 12,
164  VTERM_ATTR_DIM_MASK        = 1 << 13,
165  VTERM_ATTR_OVERLINE_MASK   = 1 << 14,
166
167  VTERM_ALL_ATTRS_MASK = (1 << 15) - 1,
168} VTermAttrMask;
169
170typedef enum {
171  // VTERM_VALUETYPE_NONE = 0
172  VTERM_VALUETYPE_BOOL = 1,
173  VTERM_VALUETYPE_INT,
174  VTERM_VALUETYPE_STRING,
175  VTERM_VALUETYPE_COLOR,
176
177  VTERM_N_VALUETYPES,
178} VTermValueType;
179
180typedef enum {
181  // VTERM_ATTR_NONE = 0
182  VTERM_ATTR_BOLD = 1,   // bool:   1, 22
183  VTERM_ATTR_UNDERLINE,  // number: 4, 21, 24
184  VTERM_ATTR_ITALIC,     // bool:   3, 23
185  VTERM_ATTR_BLINK,      // bool:   5, 25
186  VTERM_ATTR_REVERSE,    // bool:   7, 27
187  VTERM_ATTR_CONCEAL,    // bool:   8, 28
188  VTERM_ATTR_STRIKE,     // bool:   9, 29
189  VTERM_ATTR_FONT,       // number: 10-19
190  VTERM_ATTR_FOREGROUND,  // color:  30-39 90-97
191  VTERM_ATTR_BACKGROUND,  // color:  40-49 100-107
192  VTERM_ATTR_SMALL,      // bool:   73, 74, 75
193  VTERM_ATTR_BASELINE,   // number: 73, 74, 75
194  VTERM_ATTR_URI,        // number
195  VTERM_ATTR_DIM,        // bool:   2, 22
196  VTERM_ATTR_OVERLINE,   // bool:   53, 55
197
198  VTERM_N_ATTRS,
199} VTermAttr;
200
201enum {
202  VTERM_PROP_CURSORSHAPE_BLOCK = 1,
203  VTERM_PROP_CURSORSHAPE_UNDERLINE,
204  VTERM_PROP_CURSORSHAPE_BAR_LEFT,
205
206  VTERM_N_PROP_CURSORSHAPES,
207};
208
209enum {
210  VTERM_PROP_MOUSE_NONE = 0,
211  VTERM_PROP_MOUSE_CLICK,
212  VTERM_PROP_MOUSE_DRAG,
213  VTERM_PROP_MOUSE_MOVE,
214
215  VTERM_N_PROP_MOUSES,
216};
217
218typedef enum {
219  VTERM_SELECTION_CLIPBOARD = (1<<0),
220  VTERM_SELECTION_PRIMARY   = (1<<1),
221  VTERM_SELECTION_SECONDARY = (1<<2),
222  VTERM_SELECTION_SELECT    = (1<<3),
223  VTERM_SELECTION_CUT0      = (1<<4),  // also CUT1 .. CUT7 by bitshifting
224} VTermSelectionMask;
225
226typedef struct {
227  schar_T schar;
228  int width;
229  unsigned protected_cell:1;  // DECSCA-protected against DECSEL/DECSED
230  unsigned dwl:1;             // DECDWL or DECDHL double-width line
231  unsigned dhl:2;             // DECDHL double-height line (1=top 2=bottom)
232} VTermGlyphInfo;
233
234typedef struct {
235  unsigned doublewidth:1;     // DECDWL or DECDHL line
236  unsigned doubleheight:2;    // DECDHL line (1=top 2=bottom)
237  unsigned continuation:1;    // Line is a flow continuation of the previous
238} VTermLineInfo;
239
240// Copies of VTermState fields that the 'resize' callback might have reason to edit. 'resize'
241// callback gets total control of these fields and may free-and-reallocate them if required. They
242// will be copied back from the struct after the callback has returned.
243typedef struct {
244  VTermPos pos;                // current cursor position
245  VTermLineInfo *lineinfos[2];  // [1] may be NULL
246} VTermStateFields;
247
248typedef struct {
249  // libvterm relies on this memory to be zeroed out before it is returned by the allocator.
250  void *(*malloc)(size_t size, void *allocdata);
251  void (*free)(void *ptr, void *allocdata);
252} VTermAllocatorFunctions;
253
254// Setting output callback will override the buffer logic
255typedef void VTermOutputCallback(const char *s, size_t len, void *user);
256
257struct VTermBuilder {
258  int ver;  // currently unused but reserved for some sort of ABI version flag
259
260  int rows, cols;
261
262  const VTermAllocatorFunctions *allocator;
263  void *allocdata;
264
265  // Override default sizes for various structures
266  size_t outbuffer_len;  // default: 4096
267  size_t tmpbuffer_len;  // default: 4096
268};
269
270typedef struct {
271  int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user);
272  int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
273  int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user);
274  int (*moverect)(VTermRect dest, VTermRect src, void *user);
275  int (*erase)(VTermRect rect, int selective, void *user);
276  int (*initpen)(void *user);
277  int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user);
278  int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
279  int (*bell)(void *user);
280  int (*resize)(int rows, int cols, VTermStateFields *fields, void *user);
281  int (*theme)(bool *dark, void *user);
282  int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo,
283                     void *user);
284  int (*sb_clear)(void *user);
285} VTermStateCallbacks;
286
287typedef struct {
288  int (*set)(VTermSelectionMask mask, VTermStringFragment frag, void *user);
289  int (*query)(VTermSelectionMask mask, void *user);
290} VTermSelectionCallbacks;
291
292typedef struct {
293  int (*text)(const char *bytes, size_t len, void *user);
294  int (*control)(uint8_t control, void *user);
295  int (*escape)(const char *bytes, size_t len, void *user);
296  int (*csi)(const char *leader, const long args[], int argcount, const char *intermed,
297             char command, void *user);
298  int (*osc)(int command, VTermStringFragment frag, void *user);
299  int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user);
300  int (*apc)(VTermStringFragment frag, void *user);
301  int (*pm)(VTermStringFragment frag, void *user);
302  int (*sos)(VTermStringFragment frag, void *user);
303  int (*resize)(int rows, int cols, void *user);
304} VTermParserCallbacks;
305
306// State of the pen at some moment in time, also used in a cell
307typedef struct {
308  // After the bitfield
309  VTermColor fg, bg;
310
311  // Opaque ID that maps to a URI in a set
312  int uri;
313
314  unsigned bold      : 1;
315  unsigned underline : 2;
316  unsigned italic    : 1;
317  unsigned blink     : 1;
318  unsigned reverse   : 1;
319  unsigned conceal   : 1;
320  unsigned strike    : 1;
321  unsigned font      : 4;  // 0 to 9
322  unsigned small     : 1;
323  unsigned baseline  : 2;
324  unsigned dim       : 1;
325  unsigned overline  : 1;
326
327  // Extra state storage that isn't strictly pen-related
328  unsigned protected_cell : 1;
329  unsigned dwl            : 1;  // on a DECDWL or DECDHL line
330  unsigned dhl            : 2;  // on a DECDHL line (1=top 2=bottom)
331} ScreenPen;
332
333// Internal representation of a screen cell
334typedef struct {
335  schar_T schar;
336  ScreenPen pen;
337} ScreenCell;