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