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 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 11 | #include <stdlib.h> |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 12 | |
| 13 | #include "vterm_keycodes.h" |
| 14 | |
Bram Moolenaar | b2a76ec | 2017-07-25 21:34:46 +0200 | [diff] [blame] | 15 | #define TRUE 1 |
| 16 | #define FALSE 0 |
| 17 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 18 | // from stdint.h |
Bram Moolenaar | 607985a | 2017-07-28 17:04:15 +0200 | [diff] [blame] | 19 | typedef unsigned char uint8_t; |
| 20 | typedef unsigned int uint32_t; |
| 21 | |
Bram Moolenaar | 6fc3b59 | 2020-05-17 22:27:55 +0200 | [diff] [blame] | 22 | #define VTERM_VERSION_MAJOR 0 |
| 23 | #define VTERM_VERSION_MINOR 1 |
| 24 | |
| 25 | #define VTERM_CHECK_VERSION \ |
| 26 | vterm_check_version(VTERM_VERSION_MAJOR, VTERM_VERSION_MINOR) |
| 27 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 28 | typedef struct VTerm VTerm; |
| 29 | typedef struct VTermState VTermState; |
| 30 | typedef struct VTermScreen VTermScreen; |
| 31 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 32 | // Specifies a screen point. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 33 | typedef struct { |
| 34 | int row; |
| 35 | int col; |
| 36 | } VTermPos; |
| 37 | |
| 38 | /* |
| 39 | * Some small utility functions; we can just keep these static here. |
| 40 | */ |
| 41 | |
| 42 | /* |
| 43 | * Order points by on-screen flow order: |
| 44 | * Return < 0 if "a" is before "b" |
| 45 | * Return 0 if "a" and "b" are equal |
| 46 | * Return > 0 if "a" is after "b". |
| 47 | */ |
| 48 | int vterm_pos_cmp(VTermPos a, VTermPos b); |
| 49 | |
| 50 | #if defined(DEFINE_INLINES) || USE_INLINE |
| 51 | INLINE int vterm_pos_cmp(VTermPos a, VTermPos b) |
| 52 | { |
| 53 | return (a.row == b.row) ? a.col - b.col : a.row - b.row; |
| 54 | } |
| 55 | #endif |
| 56 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 57 | // Specifies a rectangular screen area. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 58 | typedef struct { |
| 59 | int start_row; |
| 60 | int end_row; |
| 61 | int start_col; |
| 62 | int end_col; |
| 63 | } VTermRect; |
| 64 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 65 | // Return true if the rect "r" contains the point "p". |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 66 | int vterm_rect_contains(VTermRect r, VTermPos p); |
| 67 | |
| 68 | #if defined(DEFINE_INLINES) || USE_INLINE |
| 69 | INLINE int vterm_rect_contains(VTermRect r, VTermPos p) |
| 70 | { |
| 71 | return p.row >= r.start_row && p.row < r.end_row && |
| 72 | p.col >= r.start_col && p.col < r.end_col; |
| 73 | } |
| 74 | #endif |
| 75 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 76 | // Move "rect" "row_delta" down and "col_delta" right. |
| 77 | // Does not check boundaries. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 78 | void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta); |
| 79 | |
| 80 | #if defined(DEFINE_INLINES) || USE_INLINE |
| 81 | INLINE void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta) |
| 82 | { |
| 83 | rect->start_row += row_delta; rect->end_row += row_delta; |
| 84 | rect->start_col += col_delta; rect->end_col += col_delta; |
| 85 | } |
| 86 | #endif |
| 87 | |
Bram Moolenaar | e5886cc | 2020-05-21 20:10:04 +0200 | [diff] [blame] | 88 | /** |
| 89 | * Bit-field describing the value of VTermColor.type |
| 90 | */ |
| 91 | typedef enum { |
| 92 | /** |
| 93 | * If the lower bit of `type` is not set, the colour is 24-bit RGB. |
| 94 | */ |
| 95 | VTERM_COLOR_RGB = 0x00, |
| 96 | |
| 97 | /** |
| 98 | * The colour is an index into a palette of 256 colours. |
| 99 | */ |
| 100 | VTERM_COLOR_INDEXED = 0x01, |
| 101 | |
| 102 | /** |
| 103 | * Mask that can be used to extract the RGB/Indexed bit. |
| 104 | */ |
| 105 | VTERM_COLOR_TYPE_MASK = 0x01, |
| 106 | |
| 107 | /** |
| 108 | * If set, indicates that this colour should be the default foreground |
| 109 | * color, i.e. there was no SGR request for another colour. When |
| 110 | * rendering this colour it is possible to ignore "idx" and just use a |
| 111 | * colour that is not in the palette. |
| 112 | */ |
| 113 | VTERM_COLOR_DEFAULT_FG = 0x02, |
| 114 | |
| 115 | /** |
| 116 | * If set, indicates that this colour should be the default background |
| 117 | * color, i.e. there was no SGR request for another colour. A common |
| 118 | * option when rendering this colour is to not render a background at |
| 119 | * all, for example by rendering the window transparently at this spot. |
| 120 | */ |
| 121 | VTERM_COLOR_DEFAULT_BG = 0x04, |
| 122 | |
| 123 | /** |
| 124 | * Mask that can be used to extract the default foreground/background bit. |
| 125 | */ |
| 126 | VTERM_COLOR_DEFAULT_MASK = 0x06 |
| 127 | } VTermColorType; |
| 128 | |
| 129 | /** |
| 130 | * Returns true if the VTERM_COLOR_RGB `type` flag is set, indicating that the |
| 131 | * given VTermColor instance is an indexed colour. |
| 132 | */ |
| 133 | #define VTERM_COLOR_IS_INDEXED(col) \ |
| 134 | (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_INDEXED) |
| 135 | |
| 136 | /** |
| 137 | * Returns true if the VTERM_COLOR_INDEXED `type` flag is set, indicating that |
| 138 | * the given VTermColor instance is an rgb colour. |
| 139 | */ |
| 140 | #define VTERM_COLOR_IS_RGB(col) \ |
| 141 | (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_RGB) |
| 142 | |
| 143 | /** |
| 144 | * Returns true if the VTERM_COLOR_DEFAULT_FG `type` flag is set, indicating |
| 145 | * that the given VTermColor instance corresponds to the default foreground |
| 146 | * color. |
| 147 | */ |
| 148 | #define VTERM_COLOR_IS_DEFAULT_FG(col) \ |
| 149 | (!!((col)->type & VTERM_COLOR_DEFAULT_FG)) |
| 150 | |
| 151 | /** |
| 152 | * Returns true if the VTERM_COLOR_DEFAULT_BG `type` flag is set, indicating |
| 153 | * that the given VTermColor instance corresponds to the default background |
| 154 | * color. |
| 155 | */ |
| 156 | #define VTERM_COLOR_IS_DEFAULT_BG(col) \ |
| 157 | (!!((col)->type & VTERM_COLOR_DEFAULT_BG)) |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 158 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 159 | typedef struct { |
Bram Moolenaar | e5886cc | 2020-05-21 20:10:04 +0200 | [diff] [blame] | 160 | /** |
| 161 | * Tag indicating which member is actually valid. |
| 162 | * Please use the `VTERM_COLOR_IS_*` test macros to check whether a |
| 163 | * particular type flag is set. |
| 164 | */ |
| 165 | uint8_t type; |
| 166 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 167 | uint8_t red, green, blue; |
Bram Moolenaar | e5886cc | 2020-05-21 20:10:04 +0200 | [diff] [blame] | 168 | |
| 169 | uint8_t index; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 170 | } VTermColor; |
| 171 | |
Bram Moolenaar | e5886cc | 2020-05-21 20:10:04 +0200 | [diff] [blame] | 172 | /** |
| 173 | * Constructs a new VTermColor instance representing the given RGB values. |
| 174 | */ |
| 175 | void vterm_color_rgb(VTermColor *col, uint8_t red, uint8_t green, uint8_t blue); |
| 176 | |
| 177 | /** |
| 178 | * Construct a new VTermColor instance representing an indexed color with the |
| 179 | * given index. |
| 180 | */ |
| 181 | void vterm_color_indexed(VTermColor *col, uint8_t idx); |
| 182 | |
| 183 | /** |
| 184 | * Compares two colours. Returns true if the colors are equal, false otherwise. |
| 185 | */ |
| 186 | int vterm_color_is_equal(const VTermColor *a, const VTermColor *b); |
| 187 | |
| 188 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 189 | typedef enum { |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 190 | // VTERM_VALUETYPE_NONE = 0 |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 191 | VTERM_VALUETYPE_BOOL = 1, |
| 192 | VTERM_VALUETYPE_INT, |
| 193 | VTERM_VALUETYPE_STRING, |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 194 | VTERM_VALUETYPE_COLOR, |
| 195 | |
| 196 | VTERM_N_VALUETYPES |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 197 | } VTermValueType; |
| 198 | |
Bram Moolenaar | be593bf | 2020-05-19 21:20:04 +0200 | [diff] [blame] | 199 | typedef struct { |
| 200 | const char *str; |
| 201 | size_t len : 30; |
| 202 | unsigned int initial : 1; |
| 203 | unsigned int final : 1; |
| 204 | } VTermStringFragment; |
| 205 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 206 | typedef union { |
| 207 | int boolean; |
| 208 | int number; |
Bram Moolenaar | be593bf | 2020-05-19 21:20:04 +0200 | [diff] [blame] | 209 | VTermStringFragment string; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 210 | VTermColor color; |
| 211 | } VTermValue; |
| 212 | |
| 213 | typedef enum { |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 214 | // VTERM_ATTR_NONE = 0 |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 215 | VTERM_ATTR_BOLD = 1, // bool: 1, 22 |
| 216 | VTERM_ATTR_UNDERLINE, // number: 4, 21, 24 |
| 217 | VTERM_ATTR_ITALIC, // bool: 3, 23 |
| 218 | VTERM_ATTR_BLINK, // bool: 5, 25 |
| 219 | VTERM_ATTR_REVERSE, // bool: 7, 27 |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 220 | VTERM_ATTR_CONCEAL, // bool: 8, 28 |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 221 | VTERM_ATTR_STRIKE, // bool: 9, 29 |
| 222 | VTERM_ATTR_FONT, // number: 10-19 |
| 223 | VTERM_ATTR_FOREGROUND, // color: 30-39 90-97 |
| 224 | VTERM_ATTR_BACKGROUND, // color: 40-49 100-107 |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 225 | |
| 226 | VTERM_N_ATTRS |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 227 | } VTermAttr; |
| 228 | |
| 229 | typedef enum { |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 230 | // VTERM_PROP_NONE = 0 |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 231 | VTERM_PROP_CURSORVISIBLE = 1, // bool |
| 232 | VTERM_PROP_CURSORBLINK, // bool |
| 233 | VTERM_PROP_ALTSCREEN, // bool |
| 234 | VTERM_PROP_TITLE, // string |
| 235 | VTERM_PROP_ICONNAME, // string |
| 236 | VTERM_PROP_REVERSE, // bool |
| 237 | VTERM_PROP_CURSORSHAPE, // number |
| 238 | VTERM_PROP_MOUSE, // number |
| 239 | VTERM_PROP_CURSORCOLOR, // string |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 240 | |
| 241 | VTERM_N_PROPS |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 242 | } VTermProp; |
| 243 | |
| 244 | enum { |
| 245 | VTERM_PROP_CURSORSHAPE_BLOCK = 1, |
| 246 | VTERM_PROP_CURSORSHAPE_UNDERLINE, |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 247 | VTERM_PROP_CURSORSHAPE_BAR_LEFT, |
| 248 | |
| 249 | VTERM_N_PROP_CURSORSHAPES |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | enum { |
| 253 | VTERM_PROP_MOUSE_NONE = 0, |
| 254 | VTERM_PROP_MOUSE_CLICK, |
| 255 | VTERM_PROP_MOUSE_DRAG, |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 256 | VTERM_PROP_MOUSE_MOVE, |
| 257 | |
| 258 | VTERM_N_PROP_MOUSES |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | typedef struct { |
| 262 | const uint32_t *chars; |
| 263 | int width; |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 264 | unsigned int protected_cell:1; // DECSCA-protected against DECSEL/DECSED |
| 265 | unsigned int dwl:1; // DECDWL or DECDHL double-width line |
| 266 | unsigned int dhl:2; // DECDHL double-height line (1=top 2=bottom) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 267 | } VTermGlyphInfo; |
| 268 | |
| 269 | typedef struct { |
Bram Moolenaar | 88d68de | 2020-05-18 21:51:01 +0200 | [diff] [blame] | 270 | unsigned int doublewidth:1; /* DECDWL or DECDHL line */ |
| 271 | unsigned int doubleheight:2; /* DECDHL line (1=top 2=bottom) */ |
| 272 | unsigned int continuation:1; /* Line is a flow continuation of the previous */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 273 | } VTermLineInfo; |
| 274 | |
| 275 | typedef struct { |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 276 | // libvterm relies on the allocated memory to be zeroed out before it is |
| 277 | // returned by the allocator. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 278 | void *(*malloc)(size_t size, void *allocdata); |
| 279 | void (*free)(void *ptr, void *allocdata); |
| 280 | } VTermAllocatorFunctions; |
| 281 | |
Bram Moolenaar | 6fc3b59 | 2020-05-17 22:27:55 +0200 | [diff] [blame] | 282 | void vterm_check_version(int major, int minor); |
| 283 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 284 | // Allocate and initialize a new terminal with default allocators. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 285 | VTerm *vterm_new(int rows, int cols); |
| 286 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 287 | // Allocate and initialize a new terminal with specified allocators. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 288 | VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata); |
| 289 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 290 | // Free and cleanup a terminal and all its data. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 291 | void vterm_free(VTerm* vt); |
| 292 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 293 | // 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] | 294 | void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp); |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 295 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 296 | void vterm_set_size(VTerm *vt, int rows, int cols); |
| 297 | |
| 298 | int vterm_get_utf8(const VTerm *vt); |
| 299 | void vterm_set_utf8(VTerm *vt, int is_utf8); |
| 300 | |
| 301 | size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len); |
| 302 | |
Bram Moolenaar | 94d729c | 2020-05-17 21:50:16 +0200 | [diff] [blame] | 303 | /* Setting output callback will override the buffer logic */ |
| 304 | typedef void VTermOutputCallback(const char *s, size_t len, void *user); |
| 305 | void vterm_output_set_callback(VTerm *vt, VTermOutputCallback *func, void *user); |
| 306 | |
| 307 | /* These buffer functions only work if output callback is NOT set |
| 308 | * These are deprecated and will be removed in a later version */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 309 | size_t vterm_output_get_buffer_size(const VTerm *vt); |
| 310 | size_t vterm_output_get_buffer_current(const VTerm *vt); |
| 311 | size_t vterm_output_get_buffer_remaining(const VTerm *vt); |
| 312 | |
Bram Moolenaar | 94d729c | 2020-05-17 21:50:16 +0200 | [diff] [blame] | 313 | /* This too */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 314 | size_t vterm_output_read(VTerm *vt, char *buffer, size_t len); |
| 315 | |
Bram Moolenaar | 6a0299d | 2019-10-10 21:14:03 +0200 | [diff] [blame] | 316 | int vterm_is_modify_other_keys(VTerm *vt); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 317 | void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod); |
| 318 | void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod); |
| 319 | |
| 320 | void vterm_keyboard_start_paste(VTerm *vt); |
| 321 | void vterm_keyboard_end_paste(VTerm *vt); |
| 322 | |
| 323 | void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod); |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 324 | // "button" is 1 for left, 2 for middle, 3 for right. |
| 325 | // Button 4 is scroll wheel down, button 5 is scroll wheel up. |
Bram Moolenaar | b2a76ec | 2017-07-25 21:34:46 +0200 | [diff] [blame] | 326 | void vterm_mouse_button(VTerm *vt, int button, int pressed, VTermModifier mod); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 327 | |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 328 | // ------------ |
| 329 | // Parser layer |
| 330 | // ------------ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 331 | |
Bram Moolenaar | 707d226 | 2019-12-04 22:16:54 +0100 | [diff] [blame] | 332 | // Flag to indicate non-final subparameters in a single CSI parameter. |
| 333 | // Consider |
| 334 | // CSI 1;2:3:4;5a |
| 335 | // 1 4 and 5 are final. |
| 336 | // 2 and 3 are non-final and will have this bit set |
| 337 | // |
| 338 | // Don't confuse this with the final byte of the CSI escape; 'a' in this case. |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 339 | #define CSI_ARG_FLAG_MORE (1U<<31) |
| 340 | #define CSI_ARG_MASK (~(1U<<31)) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 341 | |
| 342 | #define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE) |
| 343 | #define CSI_ARG(a) ((a) & CSI_ARG_MASK) |
| 344 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 345 | // Can't use -1 to indicate a missing argument; use this instead |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 346 | #define CSI_ARG_MISSING ((1<<30)-1) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 347 | |
| 348 | #define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING) |
| 349 | #define CSI_ARG_OR(a,def) (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a)) |
| 350 | #define CSI_ARG_COUNT(a) (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a)) |
| 351 | |
| 352 | typedef struct { |
| 353 | int (*text)(const char *bytes, size_t len, void *user); |
| 354 | int (*control)(unsigned char control, void *user); |
| 355 | int (*escape)(const char *bytes, size_t len, void *user); |
| 356 | int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user); |
Bram Moolenaar | be593bf | 2020-05-19 21:20:04 +0200 | [diff] [blame] | 357 | int (*osc)(int command, VTermStringFragment frag, void *user); |
| 358 | int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 359 | int (*resize)(int rows, int cols, void *user); |
| 360 | } VTermParserCallbacks; |
| 361 | |
| 362 | void vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user); |
| 363 | void *vterm_parser_get_cbdata(VTerm *vt); |
| 364 | |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 365 | // ----------- |
| 366 | // State layer |
| 367 | // ----------- |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 368 | |
Bram Moolenaar | d098b82 | 2020-05-18 21:12:59 +0200 | [diff] [blame] | 369 | /* Copies of VTermState fields that the 'resize' callback might have reason to |
| 370 | * edit. 'resize' callback gets total control of these fields and may |
| 371 | * free-and-reallocate them if required. They will be copied back from the |
| 372 | * struct after the callback has returned. |
| 373 | */ |
| 374 | typedef struct { |
| 375 | VTermPos pos; /* current cursor position */ |
| 376 | } VTermStateFields; |
| 377 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 378 | typedef struct { |
| 379 | int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user); |
| 380 | int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user); |
| 381 | int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user); |
| 382 | int (*moverect)(VTermRect dest, VTermRect src, void *user); |
| 383 | int (*erase)(VTermRect rect, int selective, void *user); |
| 384 | int (*initpen)(void *user); |
| 385 | int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user); |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 386 | // Callback for setting various properties. Must return 1 if the property |
| 387 | // was accepted, 0 otherwise. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 388 | int (*settermprop)(VTermProp prop, VTermValue *val, void *user); |
| 389 | int (*bell)(void *user); |
Bram Moolenaar | d098b82 | 2020-05-18 21:12:59 +0200 | [diff] [blame] | 390 | int (*resize)(int rows, int cols, VTermStateFields *fields, void *user); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 391 | int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user); |
| 392 | } VTermStateCallbacks; |
| 393 | |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 394 | typedef struct { |
| 395 | VTermPos pos; |
| 396 | int buttons; |
| 397 | #define MOUSE_BUTTON_LEFT 0x01 |
| 398 | #define MOUSE_BUTTON_MIDDLE 0x02 |
| 399 | #define MOUSE_BUTTON_RIGHT 0x04 |
| 400 | int flags; |
| 401 | #define MOUSE_WANT_CLICK 0x01 |
| 402 | #define MOUSE_WANT_DRAG 0x02 |
| 403 | #define MOUSE_WANT_MOVE 0x04 |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 404 | // useful to add protocol? |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 405 | } VTermMouseState; |
| 406 | |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 407 | typedef struct { |
| 408 | int (*control)(unsigned char control, void *user); |
| 409 | int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user); |
| 410 | int (*osc)(int command, VTermStringFragment frag, void *user); |
| 411 | int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user); |
| 412 | } VTermStateFallbacks; |
| 413 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 414 | VTermState *vterm_obtain_state(VTerm *vt); |
| 415 | |
| 416 | void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user); |
| 417 | void *vterm_state_get_cbdata(VTermState *state); |
| 418 | |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 419 | void vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermStateFallbacks *fallbacks, void *user); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 420 | void *vterm_state_get_unrecognised_fbdata(VTermState *state); |
| 421 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 422 | // Initialize the state. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 423 | void vterm_state_reset(VTermState *state, int hard); |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 424 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 425 | void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos); |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 426 | void vterm_state_get_mousestate(const VTermState *state, VTermMouseState *mousestate); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 427 | void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg); |
| 428 | void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col); |
| 429 | void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg); |
| 430 | void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col); |
| 431 | void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright); |
| 432 | int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val); |
| 433 | int vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val); |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 434 | void vterm_state_focus_in(VTermState *state); |
| 435 | void vterm_state_focus_out(VTermState *state); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 436 | const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row); |
| 437 | |
Bram Moolenaar | e5886cc | 2020-05-21 20:10:04 +0200 | [diff] [blame] | 438 | /** |
| 439 | * Makes sure that the given color `col` is indeed an RGB colour. After this |
| 440 | * function returns, VTERM_COLOR_IS_RGB(col) will return true, while all other |
| 441 | * flags stored in `col->type` will have been reset. |
| 442 | * |
| 443 | * @param state is the VTermState instance from which the colour palette should |
| 444 | * be extracted. |
| 445 | * @param col is a pointer at the VTermColor instance that should be converted |
| 446 | * to an RGB colour. |
| 447 | */ |
| 448 | void vterm_state_convert_color_to_rgb(const VTermState *state, VTermColor *col); |
| 449 | |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 450 | // ------------ |
| 451 | // Screen layer |
| 452 | // ------------ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 453 | |
| 454 | typedef struct { |
| 455 | unsigned int bold : 1; |
| 456 | unsigned int underline : 2; |
| 457 | unsigned int italic : 1; |
| 458 | unsigned int blink : 1; |
| 459 | unsigned int reverse : 1; |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 460 | unsigned int conceal : 1; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 461 | unsigned int strike : 1; |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 462 | unsigned int font : 4; // 0 to 9 |
| 463 | unsigned int dwl : 1; // On a DECDWL or DECDHL line |
| 464 | unsigned int dhl : 2; // On a DECDHL line (1=top 2=bottom) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 465 | } VTermScreenCellAttrs; |
| 466 | |
Bram Moolenaar | 6fc3b59 | 2020-05-17 22:27:55 +0200 | [diff] [blame] | 467 | enum { |
| 468 | VTERM_UNDERLINE_OFF, |
| 469 | VTERM_UNDERLINE_SINGLE, |
| 470 | VTERM_UNDERLINE_DOUBLE, |
| 471 | VTERM_UNDERLINE_CURLY, |
| 472 | }; |
| 473 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 474 | typedef struct { |
| 475 | #define VTERM_MAX_CHARS_PER_CELL 6 |
| 476 | uint32_t chars[VTERM_MAX_CHARS_PER_CELL]; |
| 477 | char width; |
| 478 | VTermScreenCellAttrs attrs; |
| 479 | VTermColor fg, bg; |
| 480 | } VTermScreenCell; |
| 481 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 482 | // All fields are optional, NULL when not used. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 483 | typedef struct { |
| 484 | int (*damage)(VTermRect rect, void *user); |
| 485 | int (*moverect)(VTermRect dest, VTermRect src, void *user); |
| 486 | int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user); |
| 487 | int (*settermprop)(VTermProp prop, VTermValue *val, void *user); |
| 488 | int (*bell)(void *user); |
| 489 | int (*resize)(int rows, int cols, void *user); |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 490 | // A line was pushed off the top of the window. |
| 491 | // "cells[cols]" contains the cells of that line. |
| 492 | // Return value is unused. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 493 | int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user); |
| 494 | int (*sb_popline)(int cols, VTermScreenCell *cells, void *user); |
| 495 | } VTermScreenCallbacks; |
| 496 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 497 | // Return the screen of the vterm. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 498 | VTermScreen *vterm_obtain_screen(VTerm *vt); |
| 499 | |
| 500 | /* |
| 501 | * Install screen callbacks. These are invoked when the screen contents is |
| 502 | * changed. "user" is passed into to the callback. |
| 503 | */ |
| 504 | void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user); |
| 505 | void *vterm_screen_get_cbdata(VTermScreen *screen); |
| 506 | |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 507 | void vterm_screen_set_unrecognised_fallbacks(VTermScreen *screen, const VTermStateFallbacks *fallbacks, void *user); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 508 | void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen); |
| 509 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 510 | // Enable support for using the alternate screen if "altscreen" is non-zero. |
| 511 | // Before that switching to the alternate screen won't work. |
| 512 | // Calling with "altscreen" zero has no effect. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 513 | void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen); |
| 514 | |
| 515 | typedef enum { |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 516 | VTERM_DAMAGE_CELL, // every cell |
| 517 | VTERM_DAMAGE_ROW, // entire rows |
| 518 | VTERM_DAMAGE_SCREEN, // entire screen |
| 519 | VTERM_DAMAGE_SCROLL, // entire screen + scrollrect |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 520 | |
| 521 | VTERM_N_DAMAGES |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 522 | } VTermDamageSize; |
| 523 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 524 | // Invoke the relevant callbacks to update the screen. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 525 | void vterm_screen_flush_damage(VTermScreen *screen); |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 526 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 527 | void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size); |
| 528 | |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 529 | /* |
| 530 | * Reset the screen. Also invokes vterm_state_reset(). |
| 531 | * Must be called before the terminal can be used. |
| 532 | */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 533 | void vterm_screen_reset(VTermScreen *screen, int hard); |
| 534 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 535 | // Neither of these functions NUL-terminate the buffer |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 536 | size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect); |
| 537 | size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect); |
| 538 | |
| 539 | typedef enum { |
| 540 | VTERM_ATTR_BOLD_MASK = 1 << 0, |
| 541 | VTERM_ATTR_UNDERLINE_MASK = 1 << 1, |
| 542 | VTERM_ATTR_ITALIC_MASK = 1 << 2, |
| 543 | VTERM_ATTR_BLINK_MASK = 1 << 3, |
| 544 | VTERM_ATTR_REVERSE_MASK = 1 << 4, |
| 545 | VTERM_ATTR_STRIKE_MASK = 1 << 5, |
| 546 | VTERM_ATTR_FONT_MASK = 1 << 6, |
| 547 | VTERM_ATTR_FOREGROUND_MASK = 1 << 7, |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 548 | VTERM_ATTR_BACKGROUND_MASK = 1 << 8, |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 549 | VTERM_ATTR_CONCEAL_MASK = 1 << 9, |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 550 | |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 551 | VTERM_ALL_ATTRS_MASK = (1 << 10) - 1 |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 552 | } VTermAttrMask; |
| 553 | |
| 554 | int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs); |
| 555 | |
| 556 | int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell); |
| 557 | |
| 558 | int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos); |
| 559 | |
Bram Moolenaar | e5886cc | 2020-05-21 20:10:04 +0200 | [diff] [blame] | 560 | /** |
| 561 | * Same as vterm_state_convert_color_to_rgb(), but takes a `screen` instead of a `state` |
| 562 | * instance. |
| 563 | */ |
| 564 | void vterm_screen_convert_color_to_rgb(const VTermScreen *screen, VTermColor *col); |
| 565 | |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 566 | // --------- |
| 567 | // Utilities |
| 568 | // --------- |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 569 | |
| 570 | VTermValueType vterm_get_attr_type(VTermAttr attr); |
| 571 | VTermValueType vterm_get_prop_type(VTermProp prop); |
| 572 | |
| 573 | void vterm_scroll_rect(VTermRect rect, |
| 574 | int downward, |
| 575 | int rightward, |
| 576 | int (*moverect)(VTermRect src, VTermRect dest, void *user), |
| 577 | int (*eraserect)(VTermRect rect, int selective, void *user), |
| 578 | void *user); |
| 579 | |
| 580 | void vterm_copy_cells(VTermRect dest, |
| 581 | VTermRect src, |
| 582 | void (*copycell)(VTermPos dest, VTermPos src, void *user), |
| 583 | void *user); |
| 584 | |
| 585 | #ifdef __cplusplus |
| 586 | } |
| 587 | #endif |
| 588 | |
| 589 | #endif |