Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 1 | #ifndef __VTERM_INTERNAL_H__ |
| 2 | #define __VTERM_INTERNAL_H__ |
| 3 | |
| 4 | #include "vterm.h" |
| 5 | |
| 6 | #include <stdarg.h> |
| 7 | |
| 8 | #if defined(__GNUC__) |
| 9 | # define INTERNAL __attribute__((visibility("internal"))) |
| 10 | # define UNUSED __attribute__((unused)) |
| 11 | #else |
| 12 | # define INTERNAL |
| 13 | # define UNUSED |
| 14 | #endif |
| 15 | |
| 16 | #ifdef DEBUG |
| 17 | # define DEBUG_LOG(s) fprintf(stderr, s) |
| 18 | # define DEBUG_LOG1(s, a) fprintf(stderr, s, a) |
| 19 | # define DEBUG_LOG2(s, a, b) fprintf(stderr, s, a, b) |
| 20 | # define DEBUG_LOG3(s, a, b, c) fprintf(stderr, s, a, b, c) |
| 21 | #else |
| 22 | # define DEBUG_LOG(s) |
| 23 | # define DEBUG_LOG1(s, a) |
| 24 | # define DEBUG_LOG2(s, a, b) |
| 25 | # define DEBUG_LOG3(s, a, b, c) |
| 26 | #endif |
| 27 | |
| 28 | #define ESC_S "\x1b" |
| 29 | |
| 30 | typedef struct VTermEncoding VTermEncoding; |
| 31 | |
| 32 | typedef struct { |
| 33 | VTermEncoding *enc; |
| 34 | |
| 35 | /* This size should be increased if required by other stateful encodings */ |
| 36 | char data[4*sizeof(uint32_t)]; |
| 37 | } VTermEncodingInstance; |
| 38 | |
| 39 | struct VTermPen |
| 40 | { |
| 41 | VTermColor fg; |
| 42 | VTermColor bg; |
| 43 | unsigned int bold:1; |
| 44 | unsigned int underline:2; |
| 45 | unsigned int italic:1; |
| 46 | unsigned int blink:1; |
| 47 | unsigned int reverse:1; |
| 48 | unsigned int strike:1; |
| 49 | unsigned int font:4; /* To store 0-9 */ |
| 50 | }; |
| 51 | |
| 52 | int vterm_color_equal(VTermColor a, VTermColor b); |
| 53 | |
| 54 | #if defined(DEFINE_INLINES) || USE_INLINE |
| 55 | INLINE int vterm_color_equal(VTermColor a, VTermColor b) |
| 56 | { |
| 57 | return a.red == b.red && a.green == b.green && a.blue == b.blue; |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | struct VTermState |
| 62 | { |
| 63 | VTerm *vt; |
| 64 | |
| 65 | const VTermStateCallbacks *callbacks; |
| 66 | void *cbdata; |
| 67 | |
| 68 | const VTermParserCallbacks *fallbacks; |
| 69 | void *fbdata; |
| 70 | |
| 71 | int rows; |
| 72 | int cols; |
| 73 | |
| 74 | /* Current cursor position */ |
| 75 | VTermPos pos; |
| 76 | |
| 77 | int at_phantom; /* True if we're on the "81st" phantom column to defer a wraparound */ |
| 78 | |
| 79 | int scrollregion_top; |
| 80 | int scrollregion_bottom; /* -1 means unbounded */ |
| 81 | #define SCROLLREGION_BOTTOM(state) ((state)->scrollregion_bottom > -1 ? (state)->scrollregion_bottom : (state)->rows) |
| 82 | int scrollregion_left; |
| 83 | #define SCROLLREGION_LEFT(state) ((state)->mode.leftrightmargin ? (state)->scrollregion_left : 0) |
| 84 | int scrollregion_right; /* -1 means unbounded */ |
| 85 | #define SCROLLREGION_RIGHT(state) ((state)->mode.leftrightmargin && (state)->scrollregion_right > -1 ? (state)->scrollregion_right : (state)->cols) |
| 86 | |
| 87 | /* Bitvector of tab stops */ |
| 88 | unsigned char *tabstops; |
| 89 | |
| 90 | VTermLineInfo *lineinfo; |
| 91 | #define ROWWIDTH(state,row) ((state)->lineinfo[(row)].doublewidth ? ((state)->cols / 2) : (state)->cols) |
| 92 | #define THISROWWIDTH(state) ROWWIDTH(state, (state)->pos.row) |
| 93 | |
| 94 | /* Mouse state */ |
| 95 | int mouse_col, mouse_row; |
| 96 | int mouse_buttons; |
| 97 | int mouse_flags; |
| 98 | #define MOUSE_WANT_CLICK 0x01 |
| 99 | #define MOUSE_WANT_DRAG 0x02 |
| 100 | #define MOUSE_WANT_MOVE 0x04 |
| 101 | |
| 102 | enum { MOUSE_X10, MOUSE_UTF8, MOUSE_SGR, MOUSE_RXVT } mouse_protocol; |
| 103 | |
| 104 | /* Last glyph output, for Unicode recombining purposes */ |
| 105 | uint32_t *combine_chars; |
| 106 | size_t combine_chars_size; /* Number of ELEMENTS in the above */ |
| 107 | int combine_width; /* The width of the glyph above */ |
| 108 | VTermPos combine_pos; /* Position before movement */ |
| 109 | |
| 110 | struct { |
| 111 | unsigned int keypad:1; |
| 112 | unsigned int cursor:1; |
| 113 | unsigned int autowrap:1; |
| 114 | unsigned int insert:1; |
| 115 | unsigned int newline:1; |
| 116 | unsigned int cursor_visible:1; |
| 117 | unsigned int cursor_blink:1; |
| 118 | unsigned int cursor_shape:2; |
| 119 | unsigned int alt_screen:1; |
| 120 | unsigned int origin:1; |
| 121 | unsigned int screen:1; |
| 122 | unsigned int leftrightmargin:1; |
| 123 | unsigned int bracketpaste:1; |
| 124 | } mode; |
| 125 | |
| 126 | VTermEncodingInstance encoding[4], encoding_utf8; |
| 127 | int gl_set, gr_set, gsingle_set; |
| 128 | |
| 129 | struct VTermPen pen; |
| 130 | |
| 131 | VTermColor default_fg; |
| 132 | VTermColor default_bg; |
| 133 | VTermColor colors[16]; /* Store the 8 ANSI and the 8 ANSI high-brights only */ |
| 134 | |
| 135 | int fg_index; |
| 136 | int bg_index; |
| 137 | int bold_is_highbright; |
| 138 | |
| 139 | unsigned int protected_cell : 1; |
| 140 | |
| 141 | /* Saved state under DEC mode 1048/1049 */ |
| 142 | struct { |
| 143 | VTermPos pos; |
| 144 | struct VTermPen pen; |
| 145 | |
| 146 | struct { |
| 147 | int cursor_visible:1; |
| 148 | int cursor_blink:1; |
| 149 | unsigned int cursor_shape:2; |
| 150 | } mode; |
| 151 | } saved; |
| 152 | }; |
| 153 | |
| 154 | struct VTerm |
| 155 | { |
| 156 | VTermAllocatorFunctions *allocator; |
| 157 | void *allocdata; |
| 158 | |
| 159 | int rows; |
| 160 | int cols; |
| 161 | |
| 162 | struct { |
| 163 | unsigned int utf8:1; |
| 164 | unsigned int ctrl8bit:1; |
| 165 | } mode; |
| 166 | |
| 167 | enum VTermParserState { |
| 168 | NORMAL, |
| 169 | CSI, |
| 170 | OSC, |
| 171 | DCS, |
| 172 | ESC, |
| 173 | ESC_IN_OSC, |
| 174 | ESC_IN_DCS |
| 175 | } parser_state; |
| 176 | const VTermParserCallbacks *parser_callbacks; |
| 177 | void *cbdata; |
| 178 | |
| 179 | /* len == malloc()ed size; cur == number of valid bytes */ |
| 180 | char *strbuffer; |
| 181 | size_t strbuffer_len; |
| 182 | size_t strbuffer_cur; |
| 183 | |
| 184 | char *outbuffer; |
| 185 | size_t outbuffer_len; |
| 186 | size_t outbuffer_cur; |
| 187 | |
| 188 | VTermState *state; |
| 189 | VTermScreen *screen; |
| 190 | }; |
| 191 | |
| 192 | struct VTermEncoding { |
| 193 | void (*init) (VTermEncoding *enc, void *data); |
| 194 | void (*decode)(VTermEncoding *enc, void *data, |
| 195 | uint32_t cp[], int *cpi, int cplen, |
| 196 | const char bytes[], size_t *pos, size_t len); |
| 197 | }; |
| 198 | |
| 199 | typedef enum { |
| 200 | ENC_UTF8, |
| 201 | ENC_SINGLE_94 |
| 202 | } VTermEncodingType; |
| 203 | |
| 204 | void *vterm_allocator_malloc(VTerm *vt, size_t size); |
| 205 | void vterm_allocator_free(VTerm *vt, void *ptr); |
| 206 | |
| 207 | void vterm_push_output_bytes(VTerm *vt, const char *bytes, size_t len); |
| 208 | void vterm_push_output_vsprintf(VTerm *vt, const char *format, va_list args); |
| 209 | void vterm_push_output_sprintf(VTerm *vt, const char *format, ...); |
| 210 | void vterm_push_output_sprintf_ctrl(VTerm *vt, unsigned char ctrl, const char *fmt, ...); |
| 211 | void vterm_push_output_sprintf_dcs(VTerm *vt, const char *fmt, ...); |
| 212 | |
| 213 | void vterm_state_free(VTermState *state); |
| 214 | |
| 215 | void vterm_state_newpen(VTermState *state); |
| 216 | void vterm_state_resetpen(VTermState *state); |
| 217 | void vterm_state_setpen(VTermState *state, const long args[], int argcount); |
| 218 | int vterm_state_getpen(VTermState *state, long args[], int argcount); |
| 219 | void vterm_state_savepen(VTermState *state, int save); |
| 220 | |
| 221 | enum { |
| 222 | C1_SS3 = 0x8f, |
| 223 | C1_DCS = 0x90, |
| 224 | C1_CSI = 0x9b, |
| 225 | C1_ST = 0x9c |
| 226 | }; |
| 227 | |
| 228 | void vterm_state_push_output_sprintf_CSI(VTermState *vts, const char *format, ...); |
| 229 | |
| 230 | void vterm_screen_free(VTermScreen *screen); |
| 231 | |
| 232 | VTermEncoding *vterm_lookup_encoding(VTermEncodingType type, char designation); |
| 233 | |
| 234 | int vterm_unicode_width(uint32_t codepoint); |
| 235 | int vterm_unicode_is_combining(uint32_t codepoint); |
| 236 | |
| 237 | #endif |