robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
b38173e493
master
1#pragma once 2 3#include <stdarg.h> 4 5#include "nvim/mbyte_defs.h" 6#include "nvim/vterm/vterm_defs.h" 7 8#ifdef DEBUG 9# define DEBUG_LOG (...) fprintf(stderr, __VA_ARGS__) 10#else 11# define DEBUG_LOG (...) 12#endif 13 14#define ESC_S "\x1b" 15 16#define INTERMED_MAX 16 17 18#define CSI_ARGS_MAX 32 19#define CSI_LEADER_MAX 16 20 21#define BUFIDX_PRIMARY 0 22#define BUFIDX_ALTSCREEN 1 23 24#define KEY_ENCODING_DISAMBIGUATE 0x1 25#define KEY_ENCODING_REPORT_EVENTS 0x2 26#define KEY_ENCODING_REPORT_ALTERNATE 0x4 27#define KEY_ENCODING_REPORT_ALL_KEYS 0x8 28#define KEY_ENCODING_REPORT_ASSOCIATED 0x10 29 30typedef struct VTermEncoding VTermEncoding ; 31typedef struct VTermKeyEncodingFlags VTermKeyEncodingFlags ; 32 33typedef struct { 34VTermEncoding * enc ; 35 36// This size should be increased if required by other stateful encodings 37char data [4 * sizeof (uint32_t )]; 38}VTermEncodingInstance ; 39 40struct VTermPen { 41VTermColor fg ; 42VTermColor bg ; 43int uri ; 44unsigned bold :1 ; 45unsigned underline :2 ; 46unsigned italic :1 ; 47unsigned blink :1 ; 48unsigned reverse :1 ; 49unsigned conceal :1 ; 50unsigned strike :1 ; 51unsigned font :4 ;// To store 0-9 52unsigned small :1 ; 53unsigned baseline :2 ; 54unsigned dim :1 ; 55unsigned overline :1 ; 56}; 57 58// https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement 59struct VTermKeyEncodingFlags { 60bool disambiguate :1 ; 61bool report_events :1 ; 62bool report_alternate :1 ; 63bool report_all_keys :1 ; 64bool report_associated :1 ; 65}; 66 67struct VTermKeyEncodingStack { 68VTermKeyEncodingFlags items [16 ]; 69uint8_t size ;///< Number of items in the stack. This is at least 1 and at 70///< most the length of the "items" array. 71}; 72 73struct VTermState { 74VTerm * vt ; 75 76const VTermStateCallbacks * callbacks ; 77void * cbdata ; 78 79const VTermStateFallbacks * fallbacks ; 80void * fbdata ; 81 82int rows ; 83int cols ; 84 85// Current cursor position 86VTermPos pos ; 87 88int at_phantom ;// True if we're on the "81st" phantom column to defer a wraparound 89 90int scrollregion_top ; 91int scrollregion_bottom ;// -1 means unbounded 92#define SCROLLREGION_BOTTOM (state ) ((state)->scrollregion_bottom > \ 93 -1 ? (state)->scrollregion_bottom : (state)->rows) 94int scrollregion_left ; 95#define SCROLLREGION_LEFT (state ) ((state)->mode.leftrightmargin ? (state)->scrollregion_left : 0) 96int scrollregion_right ;// -1 means unbounded 97#define SCROLLREGION_RIGHT (state ) ((state)->mode.leftrightmargin \ 98 && (state)->scrollregion_right > \ 99 -1 ? (state)->scrollregion_right : (state)->cols) 100 101// Bitvector of tab stops 102uint8_t * tabstops ; 103 104// Primary and Altscreen; lineinfos[1] is lazily allocated as needed 105VTermLineInfo * lineinfos [2 ]; 106 107// lineinfo will == lineinfos[0] or lineinfos[1], depending on altscreen 108VTermLineInfo * lineinfo ; 109#define ROWWIDTH (state , \ 110row ) ((state)->lineinfo[(row)].doublewidth ? ((state)->cols / 2) : (state)->cols) 111#define THISROWWIDTH (state ) ROWWIDTH(state, (state)->pos.row) 112 113// Mouse state 114int mouse_col ,mouse_row ; 115int mouse_buttons ; 116int mouse_flags ; 117#define MOUSE_WANT_CLICK 0x01 118#define MOUSE_WANT_DRAG 0x02 119#define MOUSE_WANT_MOVE 0x04 120 121enum {MOUSE_X10 ,MOUSE_UTF8 ,MOUSE_SGR ,MOUSE_RXVT , }mouse_protocol ; 122 123// Last glyph output, for Unicode recombining purposes 124char grapheme_buf [MAX_SCHAR_SIZE ]; 125size_t grapheme_len ; 126uint32_t grapheme_last ;// last added UTF-32 char 127GraphemeState grapheme_state ; 128int combine_width ;// The width of the glyph above 129VTermPos combine_pos ;// Position before movement 130 131struct { 132unsigned keypad :1 ; 133unsigned cursor :1 ; 134unsigned autowrap :1 ; 135unsigned insert :1 ; 136unsigned newline :1 ; 137unsigned cursor_visible :1 ; 138unsigned cursor_blink :1 ; 139unsigned cursor_shape :2 ; 140unsigned alt_screen :1 ; 141unsigned origin :1 ; 142unsigned screen :1 ; 143unsigned leftrightmargin :1 ; 144unsigned bracketpaste :1 ; 145unsigned report_focus :1 ; 146unsigned theme_updates :1 ; 147unsigned synchronized_output :1 ; 148 }mode ; 149 150VTermEncodingInstance encoding [4 ],encoding_utf8 ; 151int gl_set ,gr_set ,gsingle_set ; 152 153struct VTermPen pen ; 154 155VTermColor default_fg ; 156VTermColor default_bg ; 157VTermColor colors [16 ];// Store the 8 ANSI and the 8 ANSI high-brights only 158 159int bold_is_highbright ; 160 161unsigned protected_cell :1 ; 162 163// Saved state under DEC mode 1048/1049 164struct { 165VTermPos pos ; 166struct VTermPen pen ; 167 168struct { 169unsigned cursor_visible :1 ; 170unsigned cursor_blink :1 ; 171unsigned cursor_shape :2 ; 172 }mode ; 173 }saved ; 174 175// Temporary state for DECRQSS parsing 176union { 177char decrqss [4 ]; 178struct { 179uint16_t mask ; 180enum { 181SELECTION_INITIAL , 182SELECTION_SELECTED , 183SELECTION_QUERY , 184SELECTION_SET_INITIAL , 185SELECTION_SET , 186SELECTION_INVALID , 187 }state :8 ; 188uint32_t recvpartial ; 189uint32_t sendpartial ; 190 }selection ; 191 }tmp ; 192 193struct { 194const VTermSelectionCallbacks * callbacks ; 195void * user ; 196char * buffer ; 197size_t buflen ; 198 }selection ; 199 200// Maintain two stacks, one for primary screen and one for altscreen 201struct VTermKeyEncodingStack key_encoding_stacks [2 ]; 202}; 203 204struct VTerm { 205const VTermAllocatorFunctions * allocator ; 206void * allocdata ; 207 208int rows ; 209int cols ; 210 211struct { 212unsigned utf8 :1 ; 213unsigned ctrl8bit :1 ; 214 }mode ; 215 216struct { 217enum VTermParserState { 218NORMAL , 219CSI_LEADER , 220CSI_ARGS , 221CSI_INTERMED , 222DCS_COMMAND , 223// below here are the "string states" 224OSC_COMMAND , 225OSC , 226DCS_VTERM , 227APC , 228PM , 229SOS , 230 }state ; 231 232bool in_esc :1 ; 233 234int intermedlen ; 235char intermed [INTERMED_MAX ]; 236 237union { 238struct { 239int leaderlen ; 240char leader [CSI_LEADER_MAX ]; 241 242int argi ; 243long args [CSI_ARGS_MAX ]; 244 }csi ; 245struct { 246int command ; 247 }osc ; 248struct { 249int commandlen ; 250char command [CSI_LEADER_MAX ]; 251 }dcs ; 252 }v ; 253 254const VTermParserCallbacks * callbacks ; 255void * cbdata ; 256 257bool string_initial ; 258 259bool emit_nul ; 260 }parser ; 261 262// len == malloc()ed size; cur == number of valid bytes 263 264VTermOutputCallback * outfunc ; 265void * outdata ; 266 267char * outbuffer ; 268size_t outbuffer_len ; 269size_t outbuffer_cur ; 270 271char * tmpbuffer ; 272size_t tmpbuffer_len ; 273 274VTermState * state ; 275VTermScreen * screen ; 276}; 277 278struct VTermEncoding { 279void (* init )(VTermEncoding * enc ,void * data ); 280void (* decode )(VTermEncoding * enc ,void * data ,uint32_t cp [],int * cpi ,int cplen , 281const char bytes [],size_t * pos ,size_t len ); 282}; 283 284typedef enum { 285ENC_UTF8 , 286ENC_SINGLE_94 , 287}VTermEncodingType ; 288 289enum { 290C1_SS3 = 0x8f , 291C1_DCS = 0x90 , 292C1_CSI = 0x9b , 293C1_ST = 0x9c , 294C1_OSC = 0x9d , 295};