Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * NOTE: This is a MODIFIED version of libvterm, see the README file. |
| 3 | */ |
| 4 | #ifndef __VTERM_H__ |
| 5 | #define __VTERM_H__ |
| 6 | |
| 7 | #ifdef __cplusplus |
| 8 | extern "C" { |
| 9 | #endif |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <stdbool.h> |
| 14 | |
| 15 | #include "vterm_keycodes.h" |
| 16 | |
| 17 | typedef struct VTerm VTerm; |
| 18 | typedef struct VTermState VTermState; |
| 19 | typedef struct VTermScreen VTermScreen; |
| 20 | |
| 21 | /* Specifies a screen point. */ |
| 22 | typedef struct { |
| 23 | int row; |
| 24 | int col; |
| 25 | } VTermPos; |
| 26 | |
| 27 | /* |
| 28 | * Some small utility functions; we can just keep these static here. |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * Order points by on-screen flow order: |
| 33 | * Return < 0 if "a" is before "b" |
| 34 | * Return 0 if "a" and "b" are equal |
| 35 | * Return > 0 if "a" is after "b". |
| 36 | */ |
| 37 | int vterm_pos_cmp(VTermPos a, VTermPos b); |
| 38 | |
| 39 | #if defined(DEFINE_INLINES) || USE_INLINE |
| 40 | INLINE int vterm_pos_cmp(VTermPos a, VTermPos b) |
| 41 | { |
| 42 | return (a.row == b.row) ? a.col - b.col : a.row - b.row; |
| 43 | } |
| 44 | #endif |
| 45 | |
| 46 | /* Specifies a rectangular screen area. */ |
| 47 | typedef struct { |
| 48 | int start_row; |
| 49 | int end_row; |
| 50 | int start_col; |
| 51 | int end_col; |
| 52 | } VTermRect; |
| 53 | |
| 54 | /* Return true if the rect "r" contains the point "p". */ |
| 55 | int vterm_rect_contains(VTermRect r, VTermPos p); |
| 56 | |
| 57 | #if defined(DEFINE_INLINES) || USE_INLINE |
| 58 | INLINE int vterm_rect_contains(VTermRect r, VTermPos p) |
| 59 | { |
| 60 | return p.row >= r.start_row && p.row < r.end_row && |
| 61 | p.col >= r.start_col && p.col < r.end_col; |
| 62 | } |
| 63 | #endif |
| 64 | |
| 65 | /* Move "rect" "row_delta" down and "col_delta" right. |
| 66 | * Does not check boundaries. */ |
| 67 | void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta); |
| 68 | |
| 69 | #if defined(DEFINE_INLINES) || USE_INLINE |
| 70 | INLINE void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta) |
| 71 | { |
| 72 | rect->start_row += row_delta; rect->end_row += row_delta; |
| 73 | rect->start_col += col_delta; rect->end_col += col_delta; |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | typedef struct { |
| 78 | uint8_t red, green, blue; |
| 79 | } VTermColor; |
| 80 | |
| 81 | typedef enum { |
| 82 | /* VTERM_VALUETYPE_NONE = 0 */ |
| 83 | VTERM_VALUETYPE_BOOL = 1, |
| 84 | VTERM_VALUETYPE_INT, |
| 85 | VTERM_VALUETYPE_STRING, |
| 86 | VTERM_VALUETYPE_COLOR |
| 87 | } VTermValueType; |
| 88 | |
| 89 | typedef union { |
| 90 | int boolean; |
| 91 | int number; |
| 92 | char *string; |
| 93 | VTermColor color; |
| 94 | } VTermValue; |
| 95 | |
| 96 | typedef enum { |
| 97 | /* VTERM_ATTR_NONE = 0 */ |
| 98 | VTERM_ATTR_BOLD = 1, /* bool: 1, 22 */ |
| 99 | VTERM_ATTR_UNDERLINE, /* number: 4, 21, 24 */ |
| 100 | VTERM_ATTR_ITALIC, /* bool: 3, 23 */ |
| 101 | VTERM_ATTR_BLINK, /* bool: 5, 25 */ |
| 102 | VTERM_ATTR_REVERSE, /* bool: 7, 27 */ |
| 103 | VTERM_ATTR_STRIKE, /* bool: 9, 29 */ |
| 104 | VTERM_ATTR_FONT, /* number: 10-19 */ |
| 105 | VTERM_ATTR_FOREGROUND, /* color: 30-39 90-97 */ |
| 106 | VTERM_ATTR_BACKGROUND /* color: 40-49 100-107 */ |
| 107 | } VTermAttr; |
| 108 | |
| 109 | typedef enum { |
| 110 | /* VTERM_PROP_NONE = 0 */ |
| 111 | VTERM_PROP_CURSORVISIBLE = 1, /* bool */ |
| 112 | VTERM_PROP_CURSORBLINK, /* bool */ |
| 113 | VTERM_PROP_ALTSCREEN, /* bool */ |
| 114 | VTERM_PROP_TITLE, /* string */ |
| 115 | VTERM_PROP_ICONNAME, /* string */ |
| 116 | VTERM_PROP_REVERSE, /* bool */ |
| 117 | VTERM_PROP_CURSORSHAPE, /* number */ |
| 118 | VTERM_PROP_MOUSE /* number */ |
| 119 | } VTermProp; |
| 120 | |
| 121 | enum { |
| 122 | VTERM_PROP_CURSORSHAPE_BLOCK = 1, |
| 123 | VTERM_PROP_CURSORSHAPE_UNDERLINE, |
| 124 | VTERM_PROP_CURSORSHAPE_BAR_LEFT |
| 125 | }; |
| 126 | |
| 127 | enum { |
| 128 | VTERM_PROP_MOUSE_NONE = 0, |
| 129 | VTERM_PROP_MOUSE_CLICK, |
| 130 | VTERM_PROP_MOUSE_DRAG, |
| 131 | VTERM_PROP_MOUSE_MOVE |
| 132 | }; |
| 133 | |
| 134 | typedef struct { |
| 135 | const uint32_t *chars; |
| 136 | int width; |
| 137 | unsigned int protected_cell:1; /* DECSCA-protected against DECSEL/DECSED */ |
| 138 | unsigned int dwl:1; /* DECDWL or DECDHL double-width line */ |
| 139 | unsigned int dhl:2; /* DECDHL double-height line (1=top 2=bottom) */ |
| 140 | } VTermGlyphInfo; |
| 141 | |
| 142 | typedef struct { |
| 143 | unsigned int doublewidth:1; /* DECDWL or DECDHL line */ |
| 144 | unsigned int doubleheight:2; /* DECDHL line (1=top 2=bottom) */ |
| 145 | } VTermLineInfo; |
| 146 | |
| 147 | typedef struct { |
| 148 | /* libvterm relies on the allocated memory to be zeroed out before it is |
| 149 | * returned by the allocator. */ |
| 150 | void *(*malloc)(size_t size, void *allocdata); |
| 151 | void (*free)(void *ptr, void *allocdata); |
| 152 | } VTermAllocatorFunctions; |
| 153 | |
| 154 | /* Allocate and initialize a new terminal with default allocators. */ |
| 155 | VTerm *vterm_new(int rows, int cols); |
| 156 | |
| 157 | /* Allocate and initialize a new terminal with specified allocators. */ |
| 158 | VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata); |
| 159 | |
| 160 | /* Free and cleanup a terminal and all its data. */ |
| 161 | void vterm_free(VTerm* vt); |
| 162 | |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 163 | /* Get the current size of the terminal and store in "rowsp" and "colsp". */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 164 | void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp); |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 165 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 166 | void vterm_set_size(VTerm *vt, int rows, int cols); |
| 167 | |
| 168 | int vterm_get_utf8(const VTerm *vt); |
| 169 | void vterm_set_utf8(VTerm *vt, int is_utf8); |
| 170 | |
| 171 | size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len); |
| 172 | |
| 173 | size_t vterm_output_get_buffer_size(const VTerm *vt); |
| 174 | size_t vterm_output_get_buffer_current(const VTerm *vt); |
| 175 | size_t vterm_output_get_buffer_remaining(const VTerm *vt); |
| 176 | |
| 177 | size_t vterm_output_read(VTerm *vt, char *buffer, size_t len); |
| 178 | |
| 179 | void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod); |
| 180 | void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod); |
| 181 | |
| 182 | void vterm_keyboard_start_paste(VTerm *vt); |
| 183 | void vterm_keyboard_end_paste(VTerm *vt); |
| 184 | |
| 185 | void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod); |
| 186 | void vterm_mouse_button(VTerm *vt, int button, bool pressed, VTermModifier mod); |
| 187 | |
| 188 | /* ------------ |
| 189 | * Parser layer |
| 190 | * ------------ */ |
| 191 | |
| 192 | /* Flag to indicate non-final subparameters in a single CSI parameter. |
| 193 | * Consider |
| 194 | * CSI 1;2:3:4;5a |
| 195 | * 1 4 and 5 are final. |
| 196 | * 2 and 3 are non-final and will have this bit set |
| 197 | * |
| 198 | * Don't confuse this with the final byte of the CSI escape; 'a' in this case. |
| 199 | */ |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 200 | #define CSI_ARG_FLAG_MORE (1<<30) |
| 201 | #define CSI_ARG_MASK (~(1<<30)) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 202 | |
| 203 | #define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE) |
| 204 | #define CSI_ARG(a) ((a) & CSI_ARG_MASK) |
| 205 | |
| 206 | /* Can't use -1 to indicate a missing argument; use this instead */ |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 207 | #define CSI_ARG_MISSING ((1<<30)-1) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 208 | |
| 209 | #define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING) |
| 210 | #define CSI_ARG_OR(a,def) (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a)) |
| 211 | #define CSI_ARG_COUNT(a) (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a)) |
| 212 | |
| 213 | typedef struct { |
| 214 | int (*text)(const char *bytes, size_t len, void *user); |
| 215 | int (*control)(unsigned char control, void *user); |
| 216 | int (*escape)(const char *bytes, size_t len, void *user); |
| 217 | int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user); |
| 218 | int (*osc)(const char *command, size_t cmdlen, void *user); |
| 219 | int (*dcs)(const char *command, size_t cmdlen, void *user); |
| 220 | int (*resize)(int rows, int cols, void *user); |
| 221 | } VTermParserCallbacks; |
| 222 | |
| 223 | void vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user); |
| 224 | void *vterm_parser_get_cbdata(VTerm *vt); |
| 225 | |
| 226 | /* ----------- |
| 227 | * State layer |
| 228 | * ----------- */ |
| 229 | |
| 230 | typedef struct { |
| 231 | int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user); |
| 232 | int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user); |
| 233 | int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user); |
| 234 | int (*moverect)(VTermRect dest, VTermRect src, void *user); |
| 235 | int (*erase)(VTermRect rect, int selective, void *user); |
| 236 | int (*initpen)(void *user); |
| 237 | int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user); |
| 238 | int (*settermprop)(VTermProp prop, VTermValue *val, void *user); |
| 239 | int (*bell)(void *user); |
| 240 | int (*resize)(int rows, int cols, VTermPos *delta, void *user); |
| 241 | int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user); |
| 242 | } VTermStateCallbacks; |
| 243 | |
| 244 | VTermState *vterm_obtain_state(VTerm *vt); |
| 245 | |
| 246 | void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user); |
| 247 | void *vterm_state_get_cbdata(VTermState *state); |
| 248 | |
| 249 | /* Only invokes control, csi, osc, dcs */ |
| 250 | void vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermParserCallbacks *fallbacks, void *user); |
| 251 | void *vterm_state_get_unrecognised_fbdata(VTermState *state); |
| 252 | |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 253 | /* Initialize the state. */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 254 | void vterm_state_reset(VTermState *state, int hard); |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 255 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 256 | void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos); |
| 257 | void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg); |
| 258 | void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col); |
| 259 | void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg); |
| 260 | void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col); |
| 261 | void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright); |
| 262 | int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val); |
| 263 | int vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val); |
| 264 | const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row); |
| 265 | |
| 266 | /* ------------ |
| 267 | * Screen layer |
| 268 | * ------------ */ |
| 269 | |
| 270 | typedef struct { |
| 271 | unsigned int bold : 1; |
| 272 | unsigned int underline : 2; |
| 273 | unsigned int italic : 1; |
| 274 | unsigned int blink : 1; |
| 275 | unsigned int reverse : 1; |
| 276 | unsigned int strike : 1; |
| 277 | unsigned int font : 4; /* 0 to 9 */ |
| 278 | unsigned int dwl : 1; /* On a DECDWL or DECDHL line */ |
| 279 | unsigned int dhl : 2; /* On a DECDHL line (1=top 2=bottom) */ |
| 280 | } VTermScreenCellAttrs; |
| 281 | |
| 282 | typedef struct { |
| 283 | #define VTERM_MAX_CHARS_PER_CELL 6 |
| 284 | uint32_t chars[VTERM_MAX_CHARS_PER_CELL]; |
| 285 | char width; |
| 286 | VTermScreenCellAttrs attrs; |
| 287 | VTermColor fg, bg; |
| 288 | } VTermScreenCell; |
| 289 | |
| 290 | /* All fields are optional, NULL when not used. */ |
| 291 | typedef struct { |
| 292 | int (*damage)(VTermRect rect, void *user); |
| 293 | int (*moverect)(VTermRect dest, VTermRect src, void *user); |
| 294 | int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user); |
| 295 | int (*settermprop)(VTermProp prop, VTermValue *val, void *user); |
| 296 | int (*bell)(void *user); |
| 297 | int (*resize)(int rows, int cols, void *user); |
| 298 | int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user); |
| 299 | int (*sb_popline)(int cols, VTermScreenCell *cells, void *user); |
| 300 | } VTermScreenCallbacks; |
| 301 | |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 302 | /* Return the screen of the vterm. */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 303 | VTermScreen *vterm_obtain_screen(VTerm *vt); |
| 304 | |
| 305 | /* |
| 306 | * Install screen callbacks. These are invoked when the screen contents is |
| 307 | * changed. "user" is passed into to the callback. |
| 308 | */ |
| 309 | void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user); |
| 310 | void *vterm_screen_get_cbdata(VTermScreen *screen); |
| 311 | |
| 312 | /* Only invokes control, csi, osc, dcs */ |
| 313 | void vterm_screen_set_unrecognised_fallbacks(VTermScreen *screen, const VTermParserCallbacks *fallbacks, void *user); |
| 314 | void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen); |
| 315 | |
| 316 | void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen); |
| 317 | |
| 318 | typedef enum { |
| 319 | VTERM_DAMAGE_CELL, /* every cell */ |
| 320 | VTERM_DAMAGE_ROW, /* entire rows */ |
| 321 | VTERM_DAMAGE_SCREEN, /* entire screen */ |
| 322 | VTERM_DAMAGE_SCROLL /* entire screen + scrollrect */ |
| 323 | } VTermDamageSize; |
| 324 | |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 325 | /* Invoke the relevant callbacks to update the screen. */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 326 | void vterm_screen_flush_damage(VTermScreen *screen); |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 327 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 328 | void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size); |
| 329 | |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 330 | /* |
| 331 | * Reset the screen. Also invokes vterm_state_reset(). |
| 332 | * Must be called before the terminal can be used. |
| 333 | */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 334 | void vterm_screen_reset(VTermScreen *screen, int hard); |
| 335 | |
| 336 | /* Neither of these functions NUL-terminate the buffer */ |
| 337 | size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect); |
| 338 | size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect); |
| 339 | |
| 340 | typedef enum { |
| 341 | VTERM_ATTR_BOLD_MASK = 1 << 0, |
| 342 | VTERM_ATTR_UNDERLINE_MASK = 1 << 1, |
| 343 | VTERM_ATTR_ITALIC_MASK = 1 << 2, |
| 344 | VTERM_ATTR_BLINK_MASK = 1 << 3, |
| 345 | VTERM_ATTR_REVERSE_MASK = 1 << 4, |
| 346 | VTERM_ATTR_STRIKE_MASK = 1 << 5, |
| 347 | VTERM_ATTR_FONT_MASK = 1 << 6, |
| 348 | VTERM_ATTR_FOREGROUND_MASK = 1 << 7, |
| 349 | VTERM_ATTR_BACKGROUND_MASK = 1 << 8 |
| 350 | } VTermAttrMask; |
| 351 | |
| 352 | int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs); |
| 353 | |
| 354 | int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell); |
| 355 | |
| 356 | int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos); |
| 357 | |
| 358 | /* --------- |
| 359 | * Utilities |
| 360 | * --------- */ |
| 361 | |
| 362 | VTermValueType vterm_get_attr_type(VTermAttr attr); |
| 363 | VTermValueType vterm_get_prop_type(VTermProp prop); |
| 364 | |
| 365 | void vterm_scroll_rect(VTermRect rect, |
| 366 | int downward, |
| 367 | int rightward, |
| 368 | int (*moverect)(VTermRect src, VTermRect dest, void *user), |
| 369 | int (*eraserect)(VTermRect rect, int selective, void *user), |
| 370 | void *user); |
| 371 | |
| 372 | void vterm_copy_cells(VTermRect dest, |
| 373 | VTermRect src, |
| 374 | void (*copycell)(VTermPos dest, VTermPos src, void *user), |
| 375 | void *user); |
| 376 | |
| 377 | #ifdef __cplusplus |
| 378 | } |
| 379 | #endif |
| 380 | |
| 381 | #endif |