• 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 } VTermGlyphInfo;
107 
108 typedef struct {
109   /* libvterm relies on this memory to be zeroed out before it is returned
110    * by the allocator. */
111   void *(*malloc)(size_t size, void *allocdata);
112   void  (*free)(void *ptr, void *allocdata);
113 } VTermAllocatorFunctions;
114 
115 VTerm *vterm_new(int rows, int cols);
116 VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata);
117 void   vterm_free(VTerm* vt);
118 
119 void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp);
120 void vterm_set_size(VTerm *vt, int rows, int cols);
121 
122 void vterm_push_bytes(VTerm *vt, const char *bytes, size_t len);
123 
124 void vterm_input_push_char(VTerm *vt, VTermModifier state, uint32_t c);
125 void vterm_input_push_key(VTerm *vt, VTermModifier state, VTermKey key);
126 
127 size_t vterm_output_bufferlen(VTerm *vt); /* deprecated */
128 
129 size_t vterm_output_get_buffer_size(const VTerm *vt);
130 size_t vterm_output_get_buffer_current(const VTerm *vt);
131 size_t vterm_output_get_buffer_remaining(const VTerm *vt);
132 
133 size_t vterm_output_bufferread(VTerm *vt, char *buffer, size_t len);
134 
135 // ------------
136 // Parser layer
137 // ------------
138 
139 /* Flag to indicate non-final subparameters in a single CSI parameter.
140  * Consider
141  *   CSI 1;2:3:4;5a
142  * 1 4 and 5 are final.
143  * 2 and 3 are non-final and will have this bit set
144  *
145  * Don't confuse this with the final byte of the CSI escape; 'a' in this case.
146  */
147 #define CSI_ARG_FLAG_MORE (1<<31)
148 #define CSI_ARG_MASK      (~(1<<31))
149 
150 #define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE)
151 #define CSI_ARG(a)          ((a) & CSI_ARG_MASK)
152 
153 /* Can't use -1 to indicate a missing argument; use this instead */
154 #define CSI_ARG_MISSING ((1UL<<31)-1)
155 
156 #define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING)
157 #define CSI_ARG_OR(a,def)     (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a))
158 #define CSI_ARG_COUNT(a)      (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a))
159 
160 typedef struct {
161   int (*text)(const char *bytes, size_t len, void *user);
162   int (*control)(unsigned char control, void *user);
163   int (*escape)(const char *bytes, size_t len, void *user);
164   int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
165   int (*osc)(const char *command, size_t cmdlen, void *user);
166   int (*dcs)(const char *command, size_t cmdlen, void *user);
167   int (*resize)(int rows, int cols, void *user);
168 } VTermParserCallbacks;
169 
170 void vterm_set_parser_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user);
171 
172 void vterm_parser_set_utf8(VTerm *vt, int is_utf8);
173 
174 // -----------
175 // State layer
176 // -----------
177 
178 typedef struct {
179   int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user);
180   int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
181   int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user);
182   int (*moverect)(VTermRect dest, VTermRect src, void *user);
183   int (*erase)(VTermRect rect, int selective, void *user);
184   int (*initpen)(void *user);
185   int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user);
186   int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
187   int (*setmousefunc)(VTermMouseFunc func, void *data, void *user);
188   int (*bell)(void *user);
189   int (*resize)(int rows, int cols, VTermPos *delta, void *user);
190 } VTermStateCallbacks;
191 
192 VTermState *vterm_obtain_state(VTerm *vt);
193 
194 void vterm_state_reset(VTermState *state, int hard);
195 void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user);
196 void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos);
197 void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg);
198 void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col);
199 void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg);
200 void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright);
201 int  vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val);
202 int  vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val);
203 
204 // ------------
205 // Screen layer
206 // ------------
207 
208 typedef struct {
209 #define VTERM_MAX_CHARS_PER_CELL 6
210   uint32_t chars[VTERM_MAX_CHARS_PER_CELL];
211   char     width;
212   struct {
213     unsigned int bold      : 1;
214     unsigned int underline : 2;
215     unsigned int italic    : 1;
216     unsigned int blink     : 1;
217     unsigned int reverse   : 1;
218     unsigned int strike    : 1;
219     unsigned int font      : 4; /* 0 to 9 */
220   } attrs;
221   VTermColor fg, bg;
222 } VTermScreenCell;
223 
224 typedef struct {
225   int (*damage)(VTermRect rect, void *user);
226   int (*moverect)(VTermRect dest, VTermRect src, void *user);
227   int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
228   int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
229   int (*setmousefunc)(VTermMouseFunc func, void *data, void *user);
230   int (*bell)(void *user);
231   int (*resize)(int rows, int cols, void *user);
232   int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user);
233   int (*sb_popline)(int cols, VTermScreenCell *cells, void *user);
234 } VTermScreenCallbacks;
235 
236 VTermScreen *vterm_obtain_screen(VTerm *vt);
237 
238 void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen);
239 void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user);
240 
241 typedef enum {
242   VTERM_DAMAGE_CELL,    /* every cell */
243   VTERM_DAMAGE_ROW,     /* entire rows */
244   VTERM_DAMAGE_SCREEN,  /* entire screen */
245   VTERM_DAMAGE_SCROLL,  /* entire screen + scrollrect */
246 } VTermDamageSize;
247 
248 void vterm_screen_flush_damage(VTermScreen *screen);
249 void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size);
250 
251 void   vterm_screen_reset(VTermScreen *screen, int hard);
252 size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect);
253 size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect);
254 
255 typedef enum {
256   VTERM_ATTR_BOLD_MASK       = 1 << 0,
257   VTERM_ATTR_UNDERLINE_MASK  = 1 << 1,
258   VTERM_ATTR_ITALIC_MASK     = 1 << 2,
259   VTERM_ATTR_BLINK_MASK      = 1 << 3,
260   VTERM_ATTR_REVERSE_MASK    = 1 << 4,
261   VTERM_ATTR_STRIKE_MASK     = 1 << 5,
262   VTERM_ATTR_FONT_MASK       = 1 << 6,
263   VTERM_ATTR_FOREGROUND_MASK = 1 << 7,
264   VTERM_ATTR_BACKGROUND_MASK = 1 << 8,
265 } VTermAttrMask;
266 
267 int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs);
268 
269 int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell);
270 
271 int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos);
272 
273 // ---------
274 // Utilities
275 // ---------
276 
277 VTermValueType vterm_get_attr_type(VTermAttr attr);
278 VTermValueType vterm_get_prop_type(VTermProp prop);
279 
280 void vterm_scroll_rect(VTermRect rect,
281                        int downward,
282                        int rightward,
283                        int (*moverect)(VTermRect src, VTermRect dest, void *user),
284                        int (*eraserect)(VTermRect rect, int selective, void *user),
285                        void *user);
286 
287 void vterm_copy_cells(VTermRect dest,
288                       VTermRect src,
289                       void (*copycell)(VTermPos dest, VTermPos src, void *user),
290                       void *user);
291 
292 #ifdef __cplusplus
293 }
294 #endif
295 
296 #endif
297