• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __VTERM_H__
2 #define __VTERM_H__
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #include <stdint.h>
9 #include <stdlib.h>
10 
11 #include "vterm_input.h"
12 
13 typedef struct VTerm VTerm;
14 typedef struct VTermState VTermState;
15 typedef struct VTermScreen VTermScreen;
16 
17 typedef struct {
18   int row;
19   int col;
20 } VTermPos;
21 
22 /* some small utility functions; we can just keep these static here */
23 
24 /* order points by on-screen flow order */
vterm_pos_cmp(VTermPos a,VTermPos b)25 static inline int vterm_pos_cmp(VTermPos a, VTermPos b)
26 {
27   return (a.row == b.row) ? a.col - b.col : a.row - b.row;
28 }
29 
30 typedef struct {
31   int start_row;
32   int end_row;
33   int start_col;
34   int end_col;
35 } VTermRect;
36 
37 /* true if the rect contains the point */
vterm_rect_contains(VTermRect r,VTermPos p)38 static inline int vterm_rect_contains(VTermRect r, VTermPos p)
39 {
40   return p.row >= r.start_row && p.row < r.end_row &&
41          p.col >= r.start_col && p.col < r.end_col;
42 }
43 
44 /* move a rect */
vterm_rect_move(VTermRect * rect,int row_delta,int col_delta)45 static inline void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta)
46 {
47   rect->start_row += row_delta; rect->end_row += row_delta;
48   rect->start_col += col_delta; rect->end_col += col_delta;
49 }
50 
51 typedef struct {
52   uint8_t red, green, blue;
53 } VTermColor;
54 
55 typedef enum {
56   /* VTERM_VALUETYPE_NONE = 0 */
57   VTERM_VALUETYPE_BOOL = 1,
58   VTERM_VALUETYPE_INT,
59   VTERM_VALUETYPE_STRING,
60   VTERM_VALUETYPE_COLOR,
61 } VTermValueType;
62 
63 typedef union {
64   int boolean;
65   int number;
66   char *string;
67   VTermColor color;
68 } VTermValue;
69 
70 typedef enum {
71   /* VTERM_ATTR_NONE = 0 */
72   VTERM_ATTR_BOLD = 1,   // bool:   1, 22
73   VTERM_ATTR_UNDERLINE,  // number: 4, 21, 24
74   VTERM_ATTR_ITALIC,     // bool:   3, 23
75   VTERM_ATTR_BLINK,      // bool:   5, 25
76   VTERM_ATTR_REVERSE,    // bool:   7, 27
77   VTERM_ATTR_STRIKE,     // bool:   9, 29
78   VTERM_ATTR_FONT,       // number: 10-19
79   VTERM_ATTR_FOREGROUND, // color:  30-39 90-97
80   VTERM_ATTR_BACKGROUND, // color:  40-49 100-107
81 } VTermAttr;
82 
83 typedef enum {
84   /* VTERM_PROP_NONE = 0 */
85   VTERM_PROP_CURSORVISIBLE = 1, // bool
86   VTERM_PROP_CURSORBLINK,       // bool
87   VTERM_PROP_ALTSCREEN,         // bool
88   VTERM_PROP_TITLE,             // string
89   VTERM_PROP_ICONNAME,          // string
90   VTERM_PROP_REVERSE,           // bool
91   VTERM_PROP_CURSORSHAPE,       // number
92 } VTermProp;
93 
94 enum {
95   VTERM_PROP_CURSORSHAPE_BLOCK = 1,
96   VTERM_PROP_CURSORSHAPE_UNDERLINE,
97   VTERM_PROP_CURSORSHAPE_BAR_LEFT,
98 };
99 
100 typedef void (*VTermMouseFunc)(int x, int y, int button, int pressed, int modifiers, void *data);
101 
102 typedef struct {
103   const uint32_t *chars;
104   int             width;
105   unsigned int    protected_cell:1;  /* DECSCA-protected against DECSEL/DECSED */
106   unsigned int    dwl:1;             /* DECDWL or DECDHL double-width line */
107   unsigned int    dhl:2;             /* DECDHL double-height line (1=top 2=bottom) */
108 } VTermGlyphInfo;
109 
110 typedef struct {
111   unsigned int    doublewidth:1;     /* DECDWL or DECDHL line */
112   unsigned int    doubleheight:2;    /* DECDHL line (1=top 2=bottom) */
113 } VTermLineInfo;
114 
115 typedef struct {
116   /* libvterm relies on this memory to be zeroed out before it is returned
117    * by the allocator. */
118   void *(*malloc)(size_t size, void *allocdata);
119   void  (*free)(void *ptr, void *allocdata);
120 } VTermAllocatorFunctions;
121 
122 VTerm *vterm_new(int rows, int cols);
123 VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata);
124 void   vterm_free(VTerm* vt);
125 
126 void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp);
127 void vterm_set_size(VTerm *vt, int rows, int cols);
128 
129 void vterm_push_bytes(VTerm *vt, const char *bytes, size_t len);
130 
131 void vterm_input_push_char(VTerm *vt, VTermModifier state, uint32_t c);
132 void vterm_input_push_key(VTerm *vt, VTermModifier state, VTermKey key);
133 
134 size_t vterm_output_bufferlen(VTerm *vt); /* deprecated */
135 
136 size_t vterm_output_get_buffer_size(const VTerm *vt);
137 size_t vterm_output_get_buffer_current(const VTerm *vt);
138 size_t vterm_output_get_buffer_remaining(const VTerm *vt);
139 
140 size_t vterm_output_bufferread(VTerm *vt, char *buffer, size_t len);
141 
142 // ------------
143 // Parser layer
144 // ------------
145 
146 /* Flag to indicate non-final subparameters in a single CSI parameter.
147  * Consider
148  *   CSI 1;2:3:4;5a
149  * 1 4 and 5 are final.
150  * 2 and 3 are non-final and will have this bit set
151  *
152  * Don't confuse this with the final byte of the CSI escape; 'a' in this case.
153  */
154 #define CSI_ARG_FLAG_MORE (1<<31)
155 #define CSI_ARG_MASK      (~(1<<31))
156 
157 #define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE)
158 #define CSI_ARG(a)          ((a) & CSI_ARG_MASK)
159 
160 /* Can't use -1 to indicate a missing argument; use this instead */
161 #define CSI_ARG_MISSING ((1UL<<31)-1)
162 
163 #define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING)
164 #define CSI_ARG_OR(a,def)     (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a))
165 #define CSI_ARG_COUNT(a)      (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a))
166 
167 typedef struct {
168   int (*text)(const char *bytes, size_t len, void *user);
169   int (*control)(unsigned char control, void *user);
170   int (*escape)(const char *bytes, size_t len, void *user);
171   int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
172   int (*osc)(const char *command, size_t cmdlen, void *user);
173   int (*dcs)(const char *command, size_t cmdlen, void *user);
174   int (*resize)(int rows, int cols, void *user);
175 } VTermParserCallbacks;
176 
177 void vterm_set_parser_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user);
178 
179 void vterm_parser_set_utf8(VTerm *vt, int is_utf8);
180 
181 // -----------
182 // State layer
183 // -----------
184 
185 typedef struct {
186   int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user);
187   int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
188   int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user);
189   int (*moverect)(VTermRect dest, VTermRect src, void *user);
190   int (*erase)(VTermRect rect, int selective, void *user);
191   int (*initpen)(void *user);
192   int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user);
193   int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
194   int (*setmousefunc)(VTermMouseFunc func, void *data, void *user);
195   int (*bell)(void *user);
196   int (*resize)(int rows, int cols, VTermPos *delta, void *user);
197   int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user);
198 } VTermStateCallbacks;
199 
200 VTermState *vterm_obtain_state(VTerm *vt);
201 
202 void vterm_state_reset(VTermState *state, int hard);
203 void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user);
204 void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos);
205 void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg);
206 void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col);
207 void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg);
208 void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col);
209 void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright);
210 int  vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val);
211 int  vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val);
212 const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row);
213 
214 // ------------
215 // Screen layer
216 // ------------
217 
218 typedef struct {
219 #define VTERM_MAX_CHARS_PER_CELL 6
220   uint32_t chars[VTERM_MAX_CHARS_PER_CELL];
221   char     width;
222   struct {
223     unsigned int bold      : 1;
224     unsigned int underline : 2;
225     unsigned int italic    : 1;
226     unsigned int blink     : 1;
227     unsigned int reverse   : 1;
228     unsigned int strike    : 1;
229     unsigned int font      : 4; /* 0 to 9 */
230     unsigned int dwl       : 1; /* On a DECDWL or DECDHL line */
231     unsigned int dhl       : 2; /* On a DECDHL line (1=top 2=bottom) */
232   } attrs;
233   VTermColor fg, bg;
234 } VTermScreenCell;
235 
236 typedef struct {
237   int (*damage)(VTermRect rect, void *user);
238   int (*moverect)(VTermRect dest, VTermRect src, void *user);
239   int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
240   int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
241   int (*setmousefunc)(VTermMouseFunc func, void *data, void *user);
242   int (*bell)(void *user);
243   int (*resize)(int rows, int cols, void *user);
244   int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user);
245   int (*sb_popline)(int cols, VTermScreenCell *cells, void *user);
246 } VTermScreenCallbacks;
247 
248 VTermScreen *vterm_obtain_screen(VTerm *vt);
249 
250 void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen);
251 void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user);
252 
253 typedef enum {
254   VTERM_DAMAGE_CELL,    /* every cell */
255   VTERM_DAMAGE_ROW,     /* entire rows */
256   VTERM_DAMAGE_SCREEN,  /* entire screen */
257   VTERM_DAMAGE_SCROLL,  /* entire screen + scrollrect */
258 } VTermDamageSize;
259 
260 void vterm_screen_flush_damage(VTermScreen *screen);
261 void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size);
262 
263 void   vterm_screen_reset(VTermScreen *screen, int hard);
264 
265 /* Neither of these functions NUL-terminate the buffer */
266 size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect);
267 size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect);
268 
269 typedef enum {
270   VTERM_ATTR_BOLD_MASK       = 1 << 0,
271   VTERM_ATTR_UNDERLINE_MASK  = 1 << 1,
272   VTERM_ATTR_ITALIC_MASK     = 1 << 2,
273   VTERM_ATTR_BLINK_MASK      = 1 << 3,
274   VTERM_ATTR_REVERSE_MASK    = 1 << 4,
275   VTERM_ATTR_STRIKE_MASK     = 1 << 5,
276   VTERM_ATTR_FONT_MASK       = 1 << 6,
277   VTERM_ATTR_FOREGROUND_MASK = 1 << 7,
278   VTERM_ATTR_BACKGROUND_MASK = 1 << 8,
279 } VTermAttrMask;
280 
281 int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs);
282 
283 int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell);
284 
285 int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos);
286 
287 // ---------
288 // Utilities
289 // ---------
290 
291 VTermValueType vterm_get_attr_type(VTermAttr attr);
292 VTermValueType vterm_get_prop_type(VTermProp prop);
293 
294 void vterm_scroll_rect(VTermRect rect,
295                        int downward,
296                        int rightward,
297                        int (*moverect)(VTermRect src, VTermRect dest, void *user),
298                        int (*eraserect)(VTermRect rect, int selective, void *user),
299                        void *user);
300 
301 void vterm_copy_cells(VTermRect dest,
302                       VTermRect src,
303                       void (*copycell)(VTermPos dest, VTermPos src, void *user),
304                       void *user);
305 
306 #ifdef __cplusplus
307 }
308 #endif
309 
310 #endif
311