robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
b38173e493
master
1#pragma once 2#include <stdbool.h> 3#include <stddef.h> 4#include <stdint.h> 5 6#include "nvim/types_defs.h" 7 8typedef struct VTerm VTerm ; 9typedef struct VTermState VTermState ; 10typedef struct VTermScreen VTermScreen ; 11 12typedef struct { 13int row ; 14int col ; 15}VTermPos ; 16 17// some small utility functions; we can just keep these static here 18 19typedef struct { 20int start_row ; 21int end_row ; 22int start_col ; 23int end_col ; 24}VTermRect ; 25 26// Tagged union storing either an RGB color or an index into a colour palette. In order to convert 27// indexed colours to RGB, you may use the vterm_state_convert_color_to_rgb() or 28// vterm_screen_convert_color_to_rgb() functions which lookup the RGB colour from the palette 29// maintained by a VTermState or VTermScreen instance. 30typedef union { 31// Tag indicating which union member is actually valid. This variable coincides with the `type` 32// member of the `rgb` and the `indexed` struct in memory. Please use the `VTERM_COLOR_IS_*` test 33// macros to check whether a particular type flag is set. 34uint8_t type ; 35 36// Valid if `VTERM_COLOR_IS_RGB(type)` is true. Holds the RGB colour values. 37struct { 38// Same as the top-level `type` member stored in VTermColor. 39uint8_t type ; 40 41// The actual 8-bit red, green, blue colour values. 42uint8_t red ,green ,blue ; 43 }rgb ; 44 45// If `VTERM_COLOR_IS_INDEXED(type)` is true, this member holds the index into the colour palette. 46struct { 47// Same as the top-level `type` member stored in VTermColor. 48uint8_t type ; 49 50// Index into the colour map. 51uint8_t idx ; 52 }indexed ; 53}VTermColor ; 54 55typedef struct { 56unsigned bold :1 ; 57unsigned underline :2 ; 58unsigned italic :1 ; 59unsigned blink :1 ; 60unsigned reverse :1 ; 61unsigned conceal :1 ; 62unsigned strike :1 ; 63unsigned font :4 ;// 0 to 9 64unsigned dwl :1 ;// On a DECDWL or DECDHL line 65unsigned dhl :2 ;// On a DECDHL line (1=top 2=bottom) 66unsigned small :1 ; 67unsigned baseline :2 ; 68unsigned dim :1 ; 69unsigned overline :1 ; 70}VTermScreenCellAttrs ; 71 72typedef struct { 73schar_T schar ; 74char width ; 75VTermScreenCellAttrs attrs ; 76VTermColor fg ,bg ; 77int uri ; 78}VTermScreenCell ; 79 80typedef enum { 81// VTERM_PROP_NONE = 0 82VTERM_PROP_CURSORVISIBLE = 1 ,// bool 83VTERM_PROP_CURSORBLINK ,// bool 84VTERM_PROP_ALTSCREEN ,// bool 85VTERM_PROP_TITLE ,// string 86VTERM_PROP_ICONNAME ,// string 87VTERM_PROP_REVERSE ,// bool 88VTERM_PROP_CURSORSHAPE ,// number 89VTERM_PROP_MOUSE ,// number 90VTERM_PROP_FOCUSREPORT ,// bool 91VTERM_PROP_THEMEUPDATES ,// bool 92VTERM_PROP_SYNCOUTPUT ,// bool 93 94VTERM_N_PROPS , 95}VTermProp ; 96 97typedef enum { 98VTERM_TERMINATOR_BEL ,// \x07 99VTERM_TERMINATOR_ST ,// \x1b\x5c 100}VTermTerminator ; 101 102typedef struct { 103const char * str ; 104size_t len :30 ; 105bool initial :1 ; 106bool final :1 ; 107VTermTerminator terminator ; 108}VTermStringFragment ; 109 110typedef union { 111int boolean ; 112int number ; 113VTermStringFragment string ; 114VTermColor color ; 115}VTermValue ; 116 117typedef struct { 118int (* damage )(VTermRect rect ,void * user ); 119int (* moverect )(VTermRect dest ,VTermRect src ,void * user ); 120int (* movecursor )(VTermPos pos ,VTermPos oldpos ,int visible ,void * user ); 121int (* settermprop )(VTermProp prop ,VTermValue * val ,void * user ); 122int (* bell )(void * user ); 123int (* resize )(int rows ,int cols ,void * user ); 124int (* theme )(bool * dark ,void * user ); 125int (* sb_pushline )(int cols ,const VTermScreenCell * cells ,void * user ); 126int (* sb_popline )(int cols ,VTermScreenCell * cells ,void * user ); 127int (* sb_clear )(void * user ); 128}VTermScreenCallbacks ; 129 130typedef struct { 131int (* control )(uint8_t control ,void * user ); 132int (* csi )(const char * leader ,const long args [],int argcount ,const char * intermed , 133char command ,void * user ); 134int (* osc )(int command ,VTermStringFragment frag ,void * user ); 135int (* dcs )(const char * command ,size_t commandlen ,VTermStringFragment frag ,void * user ); 136int (* apc )(VTermStringFragment frag ,void * user ); 137int (* pm )(VTermStringFragment frag ,void * user ); 138int (* sos )(VTermStringFragment frag ,void * user ); 139}VTermStateFallbacks ; 140 141typedef enum { 142VTERM_DAMAGE_CELL ,// every cell 143VTERM_DAMAGE_ROW ,// entire rows 144VTERM_DAMAGE_SCREEN ,// entire screen 145VTERM_DAMAGE_SCROLL ,// entire screen + scrollrect 146 147VTERM_N_DAMAGES , 148}VTermDamageSize ; 149 150typedef enum { 151VTERM_ATTR_BOLD_MASK = 1 <<0 , 152VTERM_ATTR_UNDERLINE_MASK = 1 <<1 , 153VTERM_ATTR_ITALIC_MASK = 1 <<2 , 154VTERM_ATTR_BLINK_MASK = 1 <<3 , 155VTERM_ATTR_REVERSE_MASK = 1 <<4 , 156VTERM_ATTR_STRIKE_MASK = 1 <<5 , 157VTERM_ATTR_FONT_MASK = 1 <<6 , 158VTERM_ATTR_FOREGROUND_MASK = 1 <<7 , 159VTERM_ATTR_BACKGROUND_MASK = 1 <<8 , 160VTERM_ATTR_CONCEAL_MASK = 1 <<9 , 161VTERM_ATTR_SMALL_MASK = 1 <<10 , 162VTERM_ATTR_BASELINE_MASK = 1 <<11 , 163VTERM_ATTR_URI_MASK = 1 <<12 , 164VTERM_ATTR_DIM_MASK = 1 <<13 , 165VTERM_ATTR_OVERLINE_MASK = 1 <<14 , 166 167VTERM_ALL_ATTRS_MASK = (1 <<15 )- 1 , 168}VTermAttrMask ; 169 170typedef enum { 171// VTERM_VALUETYPE_NONE = 0 172VTERM_VALUETYPE_BOOL = 1 , 173VTERM_VALUETYPE_INT , 174VTERM_VALUETYPE_STRING , 175VTERM_VALUETYPE_COLOR , 176 177VTERM_N_VALUETYPES , 178}VTermValueType ; 179 180typedef enum { 181// VTERM_ATTR_NONE = 0 182VTERM_ATTR_BOLD = 1 ,// bool: 1, 22 183VTERM_ATTR_UNDERLINE ,// number: 4, 21, 24 184VTERM_ATTR_ITALIC ,// bool: 3, 23 185VTERM_ATTR_BLINK ,// bool: 5, 25 186VTERM_ATTR_REVERSE ,// bool: 7, 27 187VTERM_ATTR_CONCEAL ,// bool: 8, 28 188VTERM_ATTR_STRIKE ,// bool: 9, 29 189VTERM_ATTR_FONT ,// number: 10-19 190VTERM_ATTR_FOREGROUND ,// color: 30-39 90-97 191VTERM_ATTR_BACKGROUND ,// color: 40-49 100-107 192VTERM_ATTR_SMALL ,// bool: 73, 74, 75 193VTERM_ATTR_BASELINE ,// number: 73, 74, 75 194VTERM_ATTR_URI ,// number 195VTERM_ATTR_DIM ,// bool: 2, 22 196VTERM_ATTR_OVERLINE ,// bool: 53, 55 197 198VTERM_N_ATTRS , 199}VTermAttr ; 200 201enum { 202VTERM_PROP_CURSORSHAPE_BLOCK = 1 , 203VTERM_PROP_CURSORSHAPE_UNDERLINE , 204VTERM_PROP_CURSORSHAPE_BAR_LEFT , 205 206VTERM_N_PROP_CURSORSHAPES , 207}; 208 209enum { 210VTERM_PROP_MOUSE_NONE = 0 , 211VTERM_PROP_MOUSE_CLICK , 212VTERM_PROP_MOUSE_DRAG , 213VTERM_PROP_MOUSE_MOVE , 214 215VTERM_N_PROP_MOUSES , 216}; 217 218typedef enum { 219VTERM_SELECTION_CLIPBOARD = (1 <<0 ), 220VTERM_SELECTION_PRIMARY = (1 <<1 ), 221VTERM_SELECTION_SECONDARY = (1 <<2 ), 222VTERM_SELECTION_SELECT = (1 <<3 ), 223VTERM_SELECTION_CUT0 = (1 <<4 ),// also CUT1 .. CUT7 by bitshifting 224}VTermSelectionMask ; 225 226typedef struct { 227schar_T schar ; 228int width ; 229unsigned protected_cell :1 ;// DECSCA-protected against DECSEL/DECSED 230unsigned dwl :1 ;// DECDWL or DECDHL double-width line 231unsigned dhl :2 ;// DECDHL double-height line (1=top 2=bottom) 232}VTermGlyphInfo ; 233 234typedef struct { 235unsigned doublewidth :1 ;// DECDWL or DECDHL line 236unsigned doubleheight :2 ;// DECDHL line (1=top 2=bottom) 237unsigned continuation :1 ;// Line is a flow continuation of the previous 238}VTermLineInfo ; 239 240// Copies of VTermState fields that the 'resize' callback might have reason to edit. 'resize' 241// callback gets total control of these fields and may free-and-reallocate them if required. They 242// will be copied back from the struct after the callback has returned. 243typedef struct { 244VTermPos pos ;// current cursor position 245VTermLineInfo * lineinfos [2 ];// [1] may be NULL 246}VTermStateFields ; 247 248typedef struct { 249// libvterm relies on this memory to be zeroed out before it is returned by the allocator. 250void * (* malloc )(size_t size ,void * allocdata ); 251void (* free )(void * ptr ,void * allocdata ); 252}VTermAllocatorFunctions ; 253 254// Setting output callback will override the buffer logic 255typedef void VTermOutputCallback (const char * s ,size_t len ,void * user ); 256 257struct VTermBuilder { 258int ver ;// currently unused but reserved for some sort of ABI version flag 259 260int rows ,cols ; 261 262const VTermAllocatorFunctions * allocator ; 263void * allocdata ; 264 265// Override default sizes for various structures 266size_t outbuffer_len ;// default: 4096 267size_t tmpbuffer_len ;// default: 4096 268}; 269 270typedef struct { 271int (* putglyph )(VTermGlyphInfo * info ,VTermPos pos ,void * user ); 272int (* movecursor )(VTermPos pos ,VTermPos oldpos ,int visible ,void * user ); 273int (* scrollrect )(VTermRect rect ,int downward ,int rightward ,void * user ); 274int (* moverect )(VTermRect dest ,VTermRect src ,void * user ); 275int (* erase )(VTermRect rect ,int selective ,void * user ); 276int (* initpen )(void * user ); 277int (* setpenattr )(VTermAttr attr ,VTermValue * val ,void * user ); 278int (* settermprop )(VTermProp prop ,VTermValue * val ,void * user ); 279int (* bell )(void * user ); 280int (* resize )(int rows ,int cols ,VTermStateFields * fields ,void * user ); 281int (* theme )(bool * dark ,void * user ); 282int (* setlineinfo )(int row ,const VTermLineInfo * newinfo ,const VTermLineInfo * oldinfo , 283void * user ); 284int (* sb_clear )(void * user ); 285}VTermStateCallbacks ; 286 287typedef struct { 288int (* set )(VTermSelectionMask mask ,VTermStringFragment frag ,void * user ); 289int (* query )(VTermSelectionMask mask ,void * user ); 290}VTermSelectionCallbacks ; 291 292typedef struct { 293int (* text )(const char * bytes ,size_t len ,void * user ); 294int (* control )(uint8_t control ,void * user ); 295int (* escape )(const char * bytes ,size_t len ,void * user ); 296int (* csi )(const char * leader ,const long args [],int argcount ,const char * intermed , 297char command ,void * user ); 298int (* osc )(int command ,VTermStringFragment frag ,void * user ); 299int (* dcs )(const char * command ,size_t commandlen ,VTermStringFragment frag ,void * user ); 300int (* apc )(VTermStringFragment frag ,void * user ); 301int (* pm )(VTermStringFragment frag ,void * user ); 302int (* sos )(VTermStringFragment frag ,void * user ); 303int (* resize )(int rows ,int cols ,void * user ); 304}VTermParserCallbacks ; 305 306// State of the pen at some moment in time, also used in a cell 307typedef struct { 308// After the bitfield 309VTermColor fg ,bg ; 310 311// Opaque ID that maps to a URI in a set 312int uri ; 313 314unsigned bold :1 ; 315unsigned underline :2 ; 316unsigned italic :1 ; 317unsigned blink :1 ; 318unsigned reverse :1 ; 319unsigned conceal :1 ; 320unsigned strike :1 ; 321unsigned font :4 ;// 0 to 9 322unsigned small :1 ; 323unsigned baseline :2 ; 324unsigned dim :1 ; 325unsigned overline :1 ; 326 327// Extra state storage that isn't strictly pen-related 328unsigned protected_cell :1 ; 329unsigned dwl :1 ;// on a DECDWL or DECDHL line 330unsigned dhl :2 ;// on a DECDHL line (1=top 2=bottom) 331}ScreenPen ; 332 333// Internal representation of a screen cell 334typedef struct { 335schar_T schar ; 336ScreenPen pen ; 337}ScreenCell ;