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 /* Flag to indicate non-final subparameters in a single CSI parameter. 48 * Consider 49 * CSI 1;2:3:4;5a 50 * 1 4 and 5 are final. 51 * 2 and 3 are non-final and will have this bit set 52 * 53 * Don't confuse this with the final byte of the CSI escape; 'a' in this case. 54 */ 55 #define CSI_ARG_FLAG_MORE (1<<31) 56 #define CSI_ARG_MASK (~(1<<31)) 57 58 #define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE) 59 #define CSI_ARG(a) ((a) & CSI_ARG_MASK) 60 61 /* Can't use -1 to indicate a missing argument; use this instead */ 62 #define CSI_ARG_MISSING ((1UL<<31)-1) 63 64 #define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING) 65 #define CSI_ARG_OR(a,def) (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a)) 66 #define CSI_ARG_COUNT(a) (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a)) 67 68 typedef struct { 69 int (*text)(const char *bytes, size_t len, void *user); 70 int (*control)(unsigned char control, void *user); 71 int (*escape)(const char *bytes, size_t len, void *user); 72 int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user); 73 int (*osc)(const char *command, size_t cmdlen, void *user); 74 int (*dcs)(const char *command, size_t cmdlen, void *user); 75 int (*resize)(int rows, int cols, void *user); 76 } VTermParserCallbacks; 77 78 typedef struct { 79 uint8_t red, green, blue; 80 } VTermColor; 81 82 typedef enum { 83 /* VTERM_VALUETYPE_NONE = 0 */ 84 VTERM_VALUETYPE_BOOL = 1, 85 VTERM_VALUETYPE_INT, 86 VTERM_VALUETYPE_STRING, 87 VTERM_VALUETYPE_COLOR, 88 } VTermValueType; 89 90 typedef union { 91 int boolean; 92 int number; 93 char *string; 94 VTermColor color; 95 } VTermValue; 96 97 typedef enum { 98 /* VTERM_ATTR_NONE = 0 */ 99 VTERM_ATTR_BOLD = 1, // bool: 1, 22 100 VTERM_ATTR_UNDERLINE, // number: 4, 21, 24 101 VTERM_ATTR_ITALIC, // bool: 3, 23 102 VTERM_ATTR_BLINK, // bool: 5, 25 103 VTERM_ATTR_REVERSE, // bool: 7, 27 104 VTERM_ATTR_STRIKE, // bool: 9, 29 105 VTERM_ATTR_FONT, // number: 10-19 106 VTERM_ATTR_FOREGROUND, // color: 30-39 90-97 107 VTERM_ATTR_BACKGROUND, // color: 40-49 100-107 108 } VTermAttr; 109 110 typedef enum { 111 /* VTERM_PROP_NONE = 0 */ 112 VTERM_PROP_CURSORVISIBLE = 1, // bool 113 VTERM_PROP_CURSORBLINK, // bool 114 VTERM_PROP_ALTSCREEN, // bool 115 VTERM_PROP_TITLE, // string 116 VTERM_PROP_ICONNAME, // string 117 VTERM_PROP_REVERSE, // bool 118 VTERM_PROP_CURSORSHAPE, // number 119 } VTermProp; 120 121 enum { 122 VTERM_PROP_CURSORSHAPE_BLOCK = 1, 123 VTERM_PROP_CURSORSHAPE_UNDERLINE, 124 }; 125 126 typedef void (*VTermMouseFunc)(int x, int y, int button, int pressed, int modifiers, void *data); 127 128 typedef struct { 129 int (*putglyph)(const uint32_t chars[], int width, VTermPos pos, void *user); 130 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user); 131 int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user); 132 int (*moverect)(VTermRect dest, VTermRect src, void *user); 133 int (*erase)(VTermRect rect, void *user); 134 int (*initpen)(void *user); 135 int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user); 136 int (*settermprop)(VTermProp prop, VTermValue *val, void *user); 137 int (*setmousefunc)(VTermMouseFunc func, void *data, void *user); 138 int (*bell)(void *user); 139 int (*resize)(int rows, int cols, void *user); 140 } VTermStateCallbacks; 141 142 typedef struct { 143 int (*damage)(VTermRect rect, void *user); 144 int (*moverect)(VTermRect dest, VTermRect src, void *user); 145 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user); 146 int (*settermprop)(VTermProp prop, VTermValue *val, void *user); 147 int (*setmousefunc)(VTermMouseFunc func, void *data, void *user); 148 int (*bell)(void *user); 149 int (*resize)(int rows, int cols, void *user); 150 } VTermScreenCallbacks; 151 152 typedef struct { 153 /* libvterm relies on this memory to be zeroed out before it is returned 154 * by the allocator. */ 155 void *(*malloc)(size_t size, void *allocdata); 156 void (*free)(void *ptr, void *allocdata); 157 } VTermAllocatorFunctions; 158 159 VTerm *vterm_new(int rows, int cols); 160 VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata); 161 void vterm_free(VTerm* vt); 162 163 void vterm_get_size(VTerm *vt, int *rowsp, int *colsp); 164 void vterm_set_size(VTerm *vt, int rows, int cols); 165 166 void vterm_set_parser_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user); 167 168 VTermState *vterm_obtain_state(VTerm *vt); 169 170 void vterm_state_reset(VTermState *state, int hard); 171 void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user); 172 void vterm_state_get_cursorpos(VTermState *state, VTermPos *cursorpos); 173 void vterm_state_set_default_colors(VTermState *state, VTermColor *default_fg, VTermColor *default_bg); 174 void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright); 175 int vterm_state_get_penattr(VTermState *state, VTermAttr attr, VTermValue *val); 176 177 VTermValueType vterm_get_attr_type(VTermAttr attr); 178 VTermValueType vterm_get_prop_type(VTermProp prop); 179 180 VTermScreen *vterm_obtain_screen(VTerm *vt); 181 182 void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen); 183 void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user); 184 185 typedef enum { 186 VTERM_DAMAGE_CELL, /* every cell */ 187 VTERM_DAMAGE_ROW, /* entire rows */ 188 VTERM_DAMAGE_SCREEN, /* entire screen */ 189 VTERM_DAMAGE_SCROLL, /* entire screen + scrollrect */ 190 } VTermDamageSize; 191 192 void vterm_screen_flush_damage(VTermScreen *screen); 193 void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size); 194 195 void vterm_screen_reset(VTermScreen *screen, int hard); 196 size_t vterm_screen_get_chars(VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect); 197 size_t vterm_screen_get_text(VTermScreen *screen, char *str, size_t len, const VTermRect rect); 198 199 typedef struct { 200 #define VTERM_MAX_CHARS_PER_CELL 6 201 uint32_t chars[VTERM_MAX_CHARS_PER_CELL]; 202 char width; 203 struct { 204 unsigned int bold : 1; 205 unsigned int underline : 2; 206 unsigned int italic : 1; 207 unsigned int blink : 1; 208 unsigned int reverse : 1; 209 unsigned int strike : 1; 210 unsigned int font : 4; /* 0 to 9 */ 211 } attrs; 212 VTermColor fg, bg; 213 } VTermScreenCell; 214 215 void vterm_screen_get_cell(VTermScreen *screen, VTermPos pos, VTermScreenCell *cell); 216 217 int vterm_screen_is_eol(VTermScreen *screen, VTermPos pos); 218 219 void vterm_input_push_char(VTerm *vt, VTermModifier state, uint32_t c); 220 void vterm_input_push_key(VTerm *vt, VTermModifier state, VTermKey key); 221 222 void vterm_parser_set_utf8(VTerm *vt, int is_utf8); 223 void vterm_push_bytes(VTerm *vt, const char *bytes, size_t len); 224 225 size_t vterm_output_bufferlen(VTerm *vt); /* deprecated */ 226 227 size_t vterm_output_get_buffer_size(VTerm *vt); 228 size_t vterm_output_get_buffer_current(VTerm *vt); 229 size_t vterm_output_get_buffer_remaining(VTerm *vt); 230 231 size_t vterm_output_bufferread(VTerm *vt, char *buffer, size_t len); 232 233 void vterm_scroll_rect(VTermRect rect, 234 int downward, 235 int rightward, 236 int (*moverect)(VTermRect src, VTermRect dest, void *user), 237 int (*eraserect)(VTermRect rect, void *user), 238 void *user); 239 240 void vterm_copy_cells(VTermRect dest, 241 VTermRect src, 242 void (*copycell)(VTermPos dest, VTermPos src, void *user), 243 void *user); 244 245 #endif 246