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 | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 88 | // The ansi_index is used for the lower 16 colors, which can be set to any |
| 89 | // color. |
| 90 | #define VTERM_ANSI_INDEX_DEFAULT 0 // color cleared |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 91 | #define VTERM_ANSI_INDEX_MIN 1 |
| 92 | #define VTERM_ANSI_INDEX_MAX 16 |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 93 | #define VTERM_ANSI_INDEX_NONE 255 // non-ANSI color, use red/green/blue |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 94 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 95 | typedef struct { |
| 96 | uint8_t red, green, blue; |
Bram Moolenaar | 46359e1 | 2017-11-29 22:33:38 +0100 | [diff] [blame] | 97 | uint8_t ansi_index; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 98 | } VTermColor; |
| 99 | |
| 100 | typedef enum { |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 101 | // VTERM_VALUETYPE_NONE = 0 |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 102 | VTERM_VALUETYPE_BOOL = 1, |
| 103 | VTERM_VALUETYPE_INT, |
| 104 | VTERM_VALUETYPE_STRING, |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 105 | VTERM_VALUETYPE_COLOR, |
| 106 | |
| 107 | VTERM_N_VALUETYPES |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 108 | } VTermValueType; |
| 109 | |
Bram Moolenaar | be593bf | 2020-05-19 21:20:04 +0200 | [diff] [blame] | 110 | typedef struct { |
| 111 | const char *str; |
| 112 | size_t len : 30; |
| 113 | unsigned int initial : 1; |
| 114 | unsigned int final : 1; |
| 115 | } VTermStringFragment; |
| 116 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 117 | typedef union { |
| 118 | int boolean; |
| 119 | int number; |
Bram Moolenaar | be593bf | 2020-05-19 21:20:04 +0200 | [diff] [blame] | 120 | VTermStringFragment string; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 121 | VTermColor color; |
| 122 | } VTermValue; |
| 123 | |
| 124 | typedef enum { |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 125 | // VTERM_ATTR_NONE = 0 |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 126 | VTERM_ATTR_BOLD = 1, // bool: 1, 22 |
| 127 | VTERM_ATTR_UNDERLINE, // number: 4, 21, 24 |
| 128 | VTERM_ATTR_ITALIC, // bool: 3, 23 |
| 129 | VTERM_ATTR_BLINK, // bool: 5, 25 |
| 130 | VTERM_ATTR_REVERSE, // bool: 7, 27 |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 131 | VTERM_ATTR_CONCEAL, // bool: 8, 28 |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 132 | VTERM_ATTR_STRIKE, // bool: 9, 29 |
| 133 | VTERM_ATTR_FONT, // number: 10-19 |
| 134 | VTERM_ATTR_FOREGROUND, // color: 30-39 90-97 |
| 135 | VTERM_ATTR_BACKGROUND, // color: 40-49 100-107 |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 136 | |
| 137 | VTERM_N_ATTRS |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 138 | } VTermAttr; |
| 139 | |
| 140 | typedef enum { |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 141 | // VTERM_PROP_NONE = 0 |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 142 | VTERM_PROP_CURSORVISIBLE = 1, // bool |
| 143 | VTERM_PROP_CURSORBLINK, // bool |
| 144 | VTERM_PROP_ALTSCREEN, // bool |
| 145 | VTERM_PROP_TITLE, // string |
| 146 | VTERM_PROP_ICONNAME, // string |
| 147 | VTERM_PROP_REVERSE, // bool |
| 148 | VTERM_PROP_CURSORSHAPE, // number |
| 149 | VTERM_PROP_MOUSE, // number |
| 150 | VTERM_PROP_CURSORCOLOR, // string |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 151 | |
| 152 | VTERM_N_PROPS |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 153 | } VTermProp; |
| 154 | |
| 155 | enum { |
| 156 | VTERM_PROP_CURSORSHAPE_BLOCK = 1, |
| 157 | VTERM_PROP_CURSORSHAPE_UNDERLINE, |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 158 | VTERM_PROP_CURSORSHAPE_BAR_LEFT, |
| 159 | |
| 160 | VTERM_N_PROP_CURSORSHAPES |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | enum { |
| 164 | VTERM_PROP_MOUSE_NONE = 0, |
| 165 | VTERM_PROP_MOUSE_CLICK, |
| 166 | VTERM_PROP_MOUSE_DRAG, |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 167 | VTERM_PROP_MOUSE_MOVE, |
| 168 | |
| 169 | VTERM_N_PROP_MOUSES |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | typedef struct { |
| 173 | const uint32_t *chars; |
| 174 | int width; |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 175 | unsigned int protected_cell:1; // DECSCA-protected against DECSEL/DECSED |
| 176 | unsigned int dwl:1; // DECDWL or DECDHL double-width line |
| 177 | unsigned int dhl:2; // DECDHL double-height line (1=top 2=bottom) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 178 | } VTermGlyphInfo; |
| 179 | |
| 180 | typedef struct { |
Bram Moolenaar | 88d68de | 2020-05-18 21:51:01 +0200 | [diff] [blame] | 181 | unsigned int doublewidth:1; /* DECDWL or DECDHL line */ |
| 182 | unsigned int doubleheight:2; /* DECDHL line (1=top 2=bottom) */ |
| 183 | unsigned int continuation:1; /* Line is a flow continuation of the previous */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 184 | } VTermLineInfo; |
| 185 | |
| 186 | typedef struct { |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 187 | // libvterm relies on the allocated memory to be zeroed out before it is |
| 188 | // returned by the allocator. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 189 | void *(*malloc)(size_t size, void *allocdata); |
| 190 | void (*free)(void *ptr, void *allocdata); |
| 191 | } VTermAllocatorFunctions; |
| 192 | |
Bram Moolenaar | 6fc3b59 | 2020-05-17 22:27:55 +0200 | [diff] [blame] | 193 | void vterm_check_version(int major, int minor); |
| 194 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 195 | // Allocate and initialize a new terminal with default allocators. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 196 | VTerm *vterm_new(int rows, int cols); |
| 197 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 198 | // Allocate and initialize a new terminal with specified allocators. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 199 | VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata); |
| 200 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 201 | // Free and cleanup a terminal and all its data. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 202 | void vterm_free(VTerm* vt); |
| 203 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 204 | // 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] | 205 | void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp); |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 206 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 207 | void vterm_set_size(VTerm *vt, int rows, int cols); |
| 208 | |
| 209 | int vterm_get_utf8(const VTerm *vt); |
| 210 | void vterm_set_utf8(VTerm *vt, int is_utf8); |
| 211 | |
| 212 | size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len); |
| 213 | |
Bram Moolenaar | 94d729c | 2020-05-17 21:50:16 +0200 | [diff] [blame] | 214 | /* Setting output callback will override the buffer logic */ |
| 215 | typedef void VTermOutputCallback(const char *s, size_t len, void *user); |
| 216 | void vterm_output_set_callback(VTerm *vt, VTermOutputCallback *func, void *user); |
| 217 | |
| 218 | /* These buffer functions only work if output callback is NOT set |
| 219 | * These are deprecated and will be removed in a later version */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 220 | size_t vterm_output_get_buffer_size(const VTerm *vt); |
| 221 | size_t vterm_output_get_buffer_current(const VTerm *vt); |
| 222 | size_t vterm_output_get_buffer_remaining(const VTerm *vt); |
| 223 | |
Bram Moolenaar | 94d729c | 2020-05-17 21:50:16 +0200 | [diff] [blame] | 224 | /* This too */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 225 | size_t vterm_output_read(VTerm *vt, char *buffer, size_t len); |
| 226 | |
Bram Moolenaar | 6a0299d | 2019-10-10 21:14:03 +0200 | [diff] [blame] | 227 | int vterm_is_modify_other_keys(VTerm *vt); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 228 | void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod); |
| 229 | void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod); |
| 230 | |
| 231 | void vterm_keyboard_start_paste(VTerm *vt); |
| 232 | void vterm_keyboard_end_paste(VTerm *vt); |
| 233 | |
| 234 | void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod); |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 235 | // "button" is 1 for left, 2 for middle, 3 for right. |
| 236 | // Button 4 is scroll wheel down, button 5 is scroll wheel up. |
Bram Moolenaar | b2a76ec | 2017-07-25 21:34:46 +0200 | [diff] [blame] | 237 | void vterm_mouse_button(VTerm *vt, int button, int pressed, VTermModifier mod); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 238 | |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 239 | // ------------ |
| 240 | // Parser layer |
| 241 | // ------------ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 242 | |
Bram Moolenaar | 707d226 | 2019-12-04 22:16:54 +0100 | [diff] [blame] | 243 | // Flag to indicate non-final subparameters in a single CSI parameter. |
| 244 | // Consider |
| 245 | // CSI 1;2:3:4;5a |
| 246 | // 1 4 and 5 are final. |
| 247 | // 2 and 3 are non-final and will have this bit set |
| 248 | // |
| 249 | // 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] | 250 | #define CSI_ARG_FLAG_MORE (1U<<31) |
| 251 | #define CSI_ARG_MASK (~(1U<<31)) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 252 | |
| 253 | #define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE) |
| 254 | #define CSI_ARG(a) ((a) & CSI_ARG_MASK) |
| 255 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 256 | // Can't use -1 to indicate a missing argument; use this instead |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 257 | #define CSI_ARG_MISSING ((1<<30)-1) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 258 | |
| 259 | #define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING) |
| 260 | #define CSI_ARG_OR(a,def) (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a)) |
| 261 | #define CSI_ARG_COUNT(a) (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a)) |
| 262 | |
| 263 | typedef struct { |
| 264 | int (*text)(const char *bytes, size_t len, void *user); |
| 265 | int (*control)(unsigned char control, void *user); |
| 266 | int (*escape)(const char *bytes, size_t len, void *user); |
| 267 | 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] | 268 | int (*osc)(int command, VTermStringFragment frag, void *user); |
| 269 | int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 270 | int (*resize)(int rows, int cols, void *user); |
| 271 | } VTermParserCallbacks; |
| 272 | |
| 273 | void vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user); |
| 274 | void *vterm_parser_get_cbdata(VTerm *vt); |
| 275 | |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 276 | // ----------- |
| 277 | // State layer |
| 278 | // ----------- |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 279 | |
Bram Moolenaar | d098b82 | 2020-05-18 21:12:59 +0200 | [diff] [blame] | 280 | /* Copies of VTermState fields that the 'resize' callback might have reason to |
| 281 | * edit. 'resize' callback gets total control of these fields and may |
| 282 | * free-and-reallocate them if required. They will be copied back from the |
| 283 | * struct after the callback has returned. |
| 284 | */ |
| 285 | typedef struct { |
| 286 | VTermPos pos; /* current cursor position */ |
| 287 | } VTermStateFields; |
| 288 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 289 | typedef struct { |
| 290 | int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user); |
| 291 | int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user); |
| 292 | int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user); |
| 293 | int (*moverect)(VTermRect dest, VTermRect src, void *user); |
| 294 | int (*erase)(VTermRect rect, int selective, void *user); |
| 295 | int (*initpen)(void *user); |
| 296 | int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user); |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 297 | // Callback for setting various properties. Must return 1 if the property |
| 298 | // was accepted, 0 otherwise. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 299 | int (*settermprop)(VTermProp prop, VTermValue *val, void *user); |
| 300 | int (*bell)(void *user); |
Bram Moolenaar | d098b82 | 2020-05-18 21:12:59 +0200 | [diff] [blame] | 301 | int (*resize)(int rows, int cols, VTermStateFields *fields, void *user); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 302 | int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user); |
| 303 | } VTermStateCallbacks; |
| 304 | |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 305 | typedef struct { |
| 306 | VTermPos pos; |
| 307 | int buttons; |
| 308 | #define MOUSE_BUTTON_LEFT 0x01 |
| 309 | #define MOUSE_BUTTON_MIDDLE 0x02 |
| 310 | #define MOUSE_BUTTON_RIGHT 0x04 |
| 311 | int flags; |
| 312 | #define MOUSE_WANT_CLICK 0x01 |
| 313 | #define MOUSE_WANT_DRAG 0x02 |
| 314 | #define MOUSE_WANT_MOVE 0x04 |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 315 | // useful to add protocol? |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 316 | } VTermMouseState; |
| 317 | |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 318 | typedef struct { |
| 319 | int (*control)(unsigned char control, void *user); |
| 320 | int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user); |
| 321 | int (*osc)(int command, VTermStringFragment frag, void *user); |
| 322 | int (*dcs)(const char *command, size_t commandlen, VTermStringFragment frag, void *user); |
| 323 | } VTermStateFallbacks; |
| 324 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 325 | VTermState *vterm_obtain_state(VTerm *vt); |
| 326 | |
| 327 | void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user); |
| 328 | void *vterm_state_get_cbdata(VTermState *state); |
| 329 | |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 330 | 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] | 331 | void *vterm_state_get_unrecognised_fbdata(VTermState *state); |
| 332 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 333 | // Initialize the state. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 334 | void vterm_state_reset(VTermState *state, int hard); |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 335 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 336 | void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos); |
Bram Moolenaar | c48369c | 2018-03-11 19:30:45 +0100 | [diff] [blame] | 337 | void vterm_state_get_mousestate(const VTermState *state, VTermMouseState *mousestate); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 338 | void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg); |
| 339 | void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col); |
| 340 | void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg); |
| 341 | void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col); |
| 342 | void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright); |
| 343 | int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val); |
| 344 | int vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val); |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 345 | void vterm_state_focus_in(VTermState *state); |
| 346 | void vterm_state_focus_out(VTermState *state); |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 347 | const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row); |
| 348 | |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 349 | // ------------ |
| 350 | // Screen layer |
| 351 | // ------------ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 352 | |
| 353 | typedef struct { |
| 354 | unsigned int bold : 1; |
| 355 | unsigned int underline : 2; |
| 356 | unsigned int italic : 1; |
| 357 | unsigned int blink : 1; |
| 358 | unsigned int reverse : 1; |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 359 | unsigned int conceal : 1; |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 360 | unsigned int strike : 1; |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 361 | unsigned int font : 4; // 0 to 9 |
| 362 | unsigned int dwl : 1; // On a DECDWL or DECDHL line |
| 363 | unsigned int dhl : 2; // On a DECDHL line (1=top 2=bottom) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 364 | } VTermScreenCellAttrs; |
| 365 | |
Bram Moolenaar | 6fc3b59 | 2020-05-17 22:27:55 +0200 | [diff] [blame] | 366 | enum { |
| 367 | VTERM_UNDERLINE_OFF, |
| 368 | VTERM_UNDERLINE_SINGLE, |
| 369 | VTERM_UNDERLINE_DOUBLE, |
| 370 | VTERM_UNDERLINE_CURLY, |
| 371 | }; |
| 372 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 373 | typedef struct { |
| 374 | #define VTERM_MAX_CHARS_PER_CELL 6 |
| 375 | uint32_t chars[VTERM_MAX_CHARS_PER_CELL]; |
| 376 | char width; |
| 377 | VTermScreenCellAttrs attrs; |
| 378 | VTermColor fg, bg; |
| 379 | } VTermScreenCell; |
| 380 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 381 | // All fields are optional, NULL when not used. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 382 | typedef struct { |
| 383 | int (*damage)(VTermRect rect, void *user); |
| 384 | int (*moverect)(VTermRect dest, VTermRect src, void *user); |
| 385 | int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user); |
| 386 | int (*settermprop)(VTermProp prop, VTermValue *val, void *user); |
| 387 | int (*bell)(void *user); |
| 388 | int (*resize)(int rows, int cols, void *user); |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 389 | // A line was pushed off the top of the window. |
| 390 | // "cells[cols]" contains the cells of that line. |
| 391 | // Return value is unused. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 392 | int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user); |
| 393 | int (*sb_popline)(int cols, VTermScreenCell *cells, void *user); |
| 394 | } VTermScreenCallbacks; |
| 395 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 396 | // Return the screen of the vterm. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 397 | VTermScreen *vterm_obtain_screen(VTerm *vt); |
| 398 | |
| 399 | /* |
| 400 | * Install screen callbacks. These are invoked when the screen contents is |
| 401 | * changed. "user" is passed into to the callback. |
| 402 | */ |
| 403 | void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user); |
| 404 | void *vterm_screen_get_cbdata(VTermScreen *screen); |
| 405 | |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 406 | 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] | 407 | void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen); |
| 408 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 409 | // Enable support for using the alternate screen if "altscreen" is non-zero. |
| 410 | // Before that switching to the alternate screen won't work. |
| 411 | // Calling with "altscreen" zero has no effect. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 412 | void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen); |
| 413 | |
| 414 | typedef enum { |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 415 | VTERM_DAMAGE_CELL, // every cell |
| 416 | VTERM_DAMAGE_ROW, // entire rows |
| 417 | VTERM_DAMAGE_SCREEN, // entire screen |
| 418 | VTERM_DAMAGE_SCROLL, // entire screen + scrollrect |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 419 | |
| 420 | VTERM_N_DAMAGES |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 421 | } VTermDamageSize; |
| 422 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 423 | // Invoke the relevant callbacks to update the screen. |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 424 | void vterm_screen_flush_damage(VTermScreen *screen); |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 425 | |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 426 | void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size); |
| 427 | |
Bram Moolenaar | 9cc5f75 | 2017-07-23 22:07:27 +0200 | [diff] [blame] | 428 | /* |
| 429 | * Reset the screen. Also invokes vterm_state_reset(). |
| 430 | * Must be called before the terminal can be used. |
| 431 | */ |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 432 | void vterm_screen_reset(VTermScreen *screen, int hard); |
| 433 | |
Bram Moolenaar | db1085a | 2019-08-18 20:41:38 +0200 | [diff] [blame] | 434 | // Neither of these functions NUL-terminate the buffer |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 435 | size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect); |
| 436 | size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect); |
| 437 | |
| 438 | typedef enum { |
| 439 | VTERM_ATTR_BOLD_MASK = 1 << 0, |
| 440 | VTERM_ATTR_UNDERLINE_MASK = 1 << 1, |
| 441 | VTERM_ATTR_ITALIC_MASK = 1 << 2, |
| 442 | VTERM_ATTR_BLINK_MASK = 1 << 3, |
| 443 | VTERM_ATTR_REVERSE_MASK = 1 << 4, |
| 444 | VTERM_ATTR_STRIKE_MASK = 1 << 5, |
| 445 | VTERM_ATTR_FONT_MASK = 1 << 6, |
| 446 | VTERM_ATTR_FOREGROUND_MASK = 1 << 7, |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 447 | VTERM_ATTR_BACKGROUND_MASK = 1 << 8, |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 448 | VTERM_ATTR_CONCEAL_MASK = 1 << 9, |
Bram Moolenaar | b5b49a3 | 2018-03-25 16:20:37 +0200 | [diff] [blame] | 449 | |
Bram Moolenaar | d863728 | 2020-05-20 18:41:41 +0200 | [diff] [blame] | 450 | VTERM_ALL_ATTRS_MASK = (1 << 10) - 1 |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 451 | } VTermAttrMask; |
| 452 | |
| 453 | int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs); |
| 454 | |
| 455 | int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell); |
| 456 | |
| 457 | int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos); |
| 458 | |
Bram Moolenaar | b691de0 | 2018-04-24 18:39:14 +0200 | [diff] [blame] | 459 | // --------- |
| 460 | // Utilities |
| 461 | // --------- |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 462 | |
| 463 | VTermValueType vterm_get_attr_type(VTermAttr attr); |
| 464 | VTermValueType vterm_get_prop_type(VTermProp prop); |
| 465 | |
| 466 | void vterm_scroll_rect(VTermRect rect, |
| 467 | int downward, |
| 468 | int rightward, |
| 469 | int (*moverect)(VTermRect src, VTermRect dest, void *user), |
| 470 | int (*eraserect)(VTermRect rect, int selective, void *user), |
| 471 | void *user); |
| 472 | |
| 473 | void vterm_copy_cells(VTermRect dest, |
| 474 | VTermRect src, |
| 475 | void (*copycell)(VTermPos dest, VTermPos src, void *user), |
| 476 | void *user); |
| 477 | |
| 478 | #ifdef __cplusplus |
| 479 | } |
| 480 | #endif |
| 481 | |
| 482 | #endif |