neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

vterm_defs.h (10691B)


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