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