Home | History | Annotate | Download | only in include
      1 #ifndef __VTERM_H__
      2 #define __VTERM_H__
      3 
      4 #ifdef __cplusplus
      5 extern "C" {
      6 #endif
      7 
      8 #include <stdint.h>
      9 #include <stdlib.h>
     10 #include <stdbool.h>
     11 
     12 #include "vterm_keycodes.h"
     13 
     14 typedef struct VTerm VTerm;
     15 typedef struct VTermState VTermState;
     16 typedef struct VTermScreen VTermScreen;
     17 
     18 typedef struct {
     19   int row;
     20   int col;
     21 } VTermPos;
     22 
     23 /* some small utility functions; we can just keep these static here */
     24 
     25 /* order points by on-screen flow order */
     26 static inline int vterm_pos_cmp(VTermPos a, VTermPos b)
     27 {
     28   return (a.row == b.row) ? a.col - b.col : a.row - b.row;
     29 }
     30 
     31 typedef struct {
     32   int start_row;
     33   int end_row;
     34   int start_col;
     35   int end_col;
     36 } VTermRect;
     37 
     38 /* true if the rect contains the point */
     39 static inline int vterm_rect_contains(VTermRect r, VTermPos p)
     40 {
     41   return p.row >= r.start_row && p.row < r.end_row &&
     42          p.col >= r.start_col && p.col < r.end_col;
     43 }
     44 
     45 /* move a rect */
     46 static inline void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta)
     47 {
     48   rect->start_row += row_delta; rect->end_row += row_delta;
     49   rect->start_col += col_delta; rect->end_col += col_delta;
     50 }
     51 
     52 /**
     53  * Bit-field describing the content of the tagged union `VTermColor`.
     54  */
     55 typedef enum {
     56   /**
     57    * If the lower bit of `type` is not set, the colour is 24-bit RGB.
     58    */
     59   VTERM_COLOR_RGB = 0x00,
     60 
     61   /**
     62    * The colour is an index into a palette of 256 colours.
     63    */
     64   VTERM_COLOR_INDEXED = 0x01,
     65 
     66   /**
     67    * Mask that can be used to extract the RGB/Indexed bit.
     68    */
     69   VTERM_COLOR_TYPE_MASK = 0x01,
     70 
     71   /**
     72    * If set, indicates that this colour should be the default foreground
     73    * color, i.e. there was no SGR request for another colour. When
     74    * rendering this colour it is possible to ignore "idx" and just use a
     75    * colour that is not in the palette.
     76    */
     77   VTERM_COLOR_DEFAULT_FG = 0x02,
     78 
     79   /**
     80    * If set, indicates that this colour should be the default background
     81    * color, i.e. there was no SGR request for another colour. A common
     82    * option when rendering this colour is to not render a background at
     83    * all, for example by rendering the window transparently at this spot.
     84    */
     85   VTERM_COLOR_DEFAULT_BG = 0x04,
     86 
     87   /**
     88    * Mask that can be used to extract the default foreground/background bit.
     89    */
     90   VTERM_COLOR_DEFAULT_MASK = 0x06
     91 } VTermColorType;
     92 
     93 /**
     94  * Returns true if the VTERM_COLOR_RGB `type` flag is set, indicating that the
     95  * given VTermColor instance is an indexed colour.
     96  */
     97 #define VTERM_COLOR_IS_INDEXED(col) \
     98   (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_INDEXED)
     99 
    100 /**
    101  * Returns true if the VTERM_COLOR_INDEXED `type` flag is set, indicating that
    102  * the given VTermColor instance is an rgb colour.
    103  */
    104 #define VTERM_COLOR_IS_RGB(col) \
    105   (((col)->type & VTERM_COLOR_TYPE_MASK) == VTERM_COLOR_RGB)
    106 
    107 /**
    108  * Returns true if the VTERM_COLOR_DEFAULT_FG `type` flag is set, indicating
    109  * that the given VTermColor instance corresponds to the default foreground
    110  * color.
    111  */
    112 #define VTERM_COLOR_IS_DEFAULT_FG(col) \
    113   (!!((col)->type & VTERM_COLOR_DEFAULT_FG))
    114 
    115 /**
    116  * Returns true if the VTERM_COLOR_DEFAULT_BG `type` flag is set, indicating
    117  * that the given VTermColor instance corresponds to the default background
    118  * color.
    119  */
    120 #define VTERM_COLOR_IS_DEFAULT_BG(col) \
    121   (!!((col)->type & VTERM_COLOR_DEFAULT_BG))
    122 
    123 /**
    124  * Tagged union storing either an RGB color or an index into a colour palette.
    125  * In order to convert indexed colours to RGB, you may use the
    126  * vterm_state_convert_color_to_rgb() or vterm_screen_convert_color_to_rgb()
    127  * functions which lookup the RGB colour from the palette maintained by a
    128  * VTermState or VTermScreen instance.
    129  */
    130 typedef union {
    131   /**
    132    * Tag indicating which union member is actually valid. This variable
    133    * coincides with the `type` member of the `rgb` and the `indexed` struct
    134    * in memory. Please use the `VTERM_COLOR_IS_*` test macros to check whether
    135    * a particular type flag is set.
    136    */
    137   uint8_t type;
    138 
    139   /**
    140    * Valid if `VTERM_COLOR_IS_RGB(type)` is true. Holds the RGB colour values.
    141    */
    142   struct {
    143     /**
    144      * Same as the top-level `type` member stored in VTermColor.
    145      */
    146     uint8_t type;
    147 
    148     /**
    149      * The actual 8-bit red, green, blue colour values.
    150      */
    151     uint8_t red, green, blue;
    152   } rgb;
    153 
    154   /**
    155    * If `VTERM_COLOR_IS_INDEXED(type)` is true, this member holds the index into
    156    * the colour palette.
    157    */
    158   struct {
    159     /**
    160      * Same as the top-level `type` member stored in VTermColor.
    161      */
    162     uint8_t type;
    163 
    164     /**
    165      * Index into the colour map.
    166      */
    167     uint8_t idx;
    168   } indexed;
    169 } VTermColor;
    170 
    171 /**
    172  * Constructs a new VTermColor instance representing the given RGB values.
    173  */
    174 static inline void vterm_color_rgb(VTermColor *col, uint8_t red, uint8_t green,
    175                                    uint8_t blue)
    176 {
    177   col->type = VTERM_COLOR_RGB;
    178   col->rgb.red   = red;
    179   col->rgb.green = green;
    180   col->rgb.blue  = blue;
    181 }
    182 
    183 /**
    184  * Construct a new VTermColor instance representing an indexed color with the
    185  * given index.
    186  */
    187 static inline void vterm_color_indexed(VTermColor *col, uint8_t idx)
    188 {
    189   col->type = VTERM_COLOR_INDEXED;
    190   col->indexed.idx = idx;
    191 }
    192 
    193 /**
    194  * Compares two colours. Returns true if the colors are equal, false otherwise.
    195  */
    196 int vterm_color_is_equal(const VTermColor *a, const VTermColor *b);
    197 
    198 typedef enum {
    199   /* VTERM_VALUETYPE_NONE = 0 */
    200   VTERM_VALUETYPE_BOOL = 1,
    201   VTERM_VALUETYPE_INT,
    202   VTERM_VALUETYPE_STRING,
    203   VTERM_VALUETYPE_COLOR,
    204 
    205   VTERM_N_VALUETYPES
    206 } VTermValueType;
    207 
    208 typedef union {
    209   int boolean;
    210   int number;
    211   char *string;
    212   VTermColor color;
    213 } VTermValue;
    214 
    215 typedef enum {
    216   /* VTERM_ATTR_NONE = 0 */
    217   VTERM_ATTR_BOLD = 1,   // bool:   1, 22
    218   VTERM_ATTR_UNDERLINE,  // number: 4, 21, 24
    219   VTERM_ATTR_ITALIC,     // bool:   3, 23
    220   VTERM_ATTR_BLINK,      // bool:   5, 25
    221   VTERM_ATTR_REVERSE,    // bool:   7, 27
    222   VTERM_ATTR_STRIKE,     // bool:   9, 29
    223   VTERM_ATTR_FONT,       // number: 10-19
    224   VTERM_ATTR_FOREGROUND, // color:  30-39 90-97
    225   VTERM_ATTR_BACKGROUND, // color:  40-49 100-107
    226 
    227   VTERM_N_ATTRS
    228 } VTermAttr;
    229 
    230 typedef enum {
    231   /* VTERM_PROP_NONE = 0 */
    232   VTERM_PROP_CURSORVISIBLE = 1, // bool
    233   VTERM_PROP_CURSORBLINK,       // bool
    234   VTERM_PROP_ALTSCREEN,         // bool
    235   VTERM_PROP_TITLE,             // string
    236   VTERM_PROP_ICONNAME,          // string
    237   VTERM_PROP_REVERSE,           // bool
    238   VTERM_PROP_CURSORSHAPE,       // number
    239   VTERM_PROP_MOUSE,             // number
    240 
    241   VTERM_N_PROPS
    242 } VTermProp;
    243 
    244 enum {
    245   VTERM_PROP_CURSORSHAPE_BLOCK = 1,
    246   VTERM_PROP_CURSORSHAPE_UNDERLINE,
    247   VTERM_PROP_CURSORSHAPE_BAR_LEFT,
    248 
    249   VTERM_N_PROP_CURSORSHAPES
    250 };
    251 
    252 enum {
    253   VTERM_PROP_MOUSE_NONE = 0,
    254   VTERM_PROP_MOUSE_CLICK,
    255   VTERM_PROP_MOUSE_DRAG,
    256   VTERM_PROP_MOUSE_MOVE,
    257 
    258   VTERM_N_PROP_MOUSES
    259 };
    260 
    261 typedef struct {
    262   const uint32_t *chars;
    263   int             width;
    264   unsigned int    protected_cell:1;  /* DECSCA-protected against DECSEL/DECSED */
    265   unsigned int    dwl:1;             /* DECDWL or DECDHL double-width line */
    266   unsigned int    dhl:2;             /* DECDHL double-height line (1=top 2=bottom) */
    267 } VTermGlyphInfo;
    268 
    269 typedef struct {
    270   unsigned int    doublewidth:1;     /* DECDWL or DECDHL line */
    271   unsigned int    doubleheight:2;    /* DECDHL line (1=top 2=bottom) */
    272 } VTermLineInfo;
    273 
    274 typedef struct {
    275   /* libvterm relies on this memory to be zeroed out before it is returned
    276    * by the allocator. */
    277   void *(*malloc)(size_t size, void *allocdata);
    278   void  (*free)(void *ptr, void *allocdata);
    279 } VTermAllocatorFunctions;
    280 
    281 VTerm *vterm_new(int rows, int cols);
    282 VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata);
    283 void   vterm_free(VTerm* vt);
    284 
    285 void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp);
    286 void vterm_set_size(VTerm *vt, int rows, int cols);
    287 
    288 int  vterm_get_utf8(const VTerm *vt);
    289 void vterm_set_utf8(VTerm *vt, int is_utf8);
    290 
    291 size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len);
    292 
    293 size_t vterm_output_get_buffer_size(const VTerm *vt);
    294 size_t vterm_output_get_buffer_current(const VTerm *vt);
    295 size_t vterm_output_get_buffer_remaining(const VTerm *vt);
    296 
    297 size_t vterm_output_read(VTerm *vt, char *buffer, size_t len);
    298 
    299 void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod);
    300 void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod);
    301 
    302 void vterm_keyboard_start_paste(VTerm *vt);
    303 void vterm_keyboard_end_paste(VTerm *vt);
    304 
    305 void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod);
    306 void vterm_mouse_button(VTerm *vt, int button, bool pressed, VTermModifier mod);
    307 
    308 // ------------
    309 // Parser layer
    310 // ------------
    311 
    312 /* Flag to indicate non-final subparameters in a single CSI parameter.
    313  * Consider
    314  *   CSI 1;2:3:4;5a
    315  * 1 4 and 5 are final.
    316  * 2 and 3 are non-final and will have this bit set
    317  *
    318  * Don't confuse this with the final byte of the CSI escape; 'a' in this case.
    319  */
    320 #define CSI_ARG_FLAG_MORE (1U<<31)
    321 #define CSI_ARG_MASK      (~(1U<<31))
    322 
    323 #define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE)
    324 #define CSI_ARG(a)          ((a) & CSI_ARG_MASK)
    325 
    326 /* Can't use -1 to indicate a missing argument; use this instead */
    327 #define CSI_ARG_MISSING ((1UL<<31)-1)
    328 
    329 #define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING)
    330 #define CSI_ARG_OR(a,def)     (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a))
    331 #define CSI_ARG_COUNT(a)      (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a))
    332 
    333 typedef struct {
    334   int (*text)(const char *bytes, size_t len, void *user);
    335   int (*control)(unsigned char control, void *user);
    336   int (*escape)(const char *bytes, size_t len, void *user);
    337   int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
    338   int (*osc)(const char *command, size_t cmdlen, void *user);
    339   int (*dcs)(const char *command, size_t cmdlen, void *user);
    340   int (*resize)(int rows, int cols, void *user);
    341 } VTermParserCallbacks;
    342 
    343 void  vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user);
    344 void *vterm_parser_get_cbdata(VTerm *vt);
    345 
    346 // -----------
    347 // State layer
    348 // -----------
    349 
    350 typedef struct {
    351   int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user);
    352   int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
    353   int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user);
    354   int (*moverect)(VTermRect dest, VTermRect src, void *user);
    355   int (*erase)(VTermRect rect, int selective, void *user);
    356   int (*initpen)(void *user);
    357   int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user);
    358   int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
    359   int (*bell)(void *user);
    360   int (*resize)(int rows, int cols, VTermPos *delta, void *user);
    361   int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user);
    362 } VTermStateCallbacks;
    363 
    364 VTermState *vterm_obtain_state(VTerm *vt);
    365 
    366 void  vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user);
    367 void *vterm_state_get_cbdata(VTermState *state);
    368 
    369 // Only invokes control, csi, osc, dcs
    370 void  vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermParserCallbacks *fallbacks, void *user);
    371 void *vterm_state_get_unrecognised_fbdata(VTermState *state);
    372 
    373 void vterm_state_reset(VTermState *state, int hard);
    374 void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos);
    375 void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg);
    376 void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col);
    377 void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg);
    378 void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col);
    379 void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright);
    380 int  vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val);
    381 int  vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val);
    382 void vterm_state_focus_in(VTermState *state);
    383 void vterm_state_focus_out(VTermState *state);
    384 const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row);
    385 
    386 /**
    387  * Makes sure that the given color `col` is indeed an RGB colour. After this
    388  * function returns, VTERM_COLOR_IS_RGB(col) will return true, while all other
    389  * flags stored in `col->type` will have been reset.
    390  *
    391  * @param state is the VTermState instance from which the colour palette should
    392  * be extracted.
    393  * @param col is a pointer at the VTermColor instance that should be converted
    394  * to an RGB colour.
    395  */
    396 void vterm_state_convert_color_to_rgb(const VTermState *state, VTermColor *col);
    397 
    398 // ------------
    399 // Screen layer
    400 // ------------
    401 
    402 typedef struct {
    403     unsigned int bold      : 1;
    404     unsigned int underline : 2;
    405     unsigned int italic    : 1;
    406     unsigned int blink     : 1;
    407     unsigned int reverse   : 1;
    408     unsigned int strike    : 1;
    409     unsigned int font      : 4; /* 0 to 9 */
    410     unsigned int dwl       : 1; /* On a DECDWL or DECDHL line */
    411     unsigned int dhl       : 2; /* On a DECDHL line (1=top 2=bottom) */
    412 } VTermScreenCellAttrs;
    413 
    414 typedef struct {
    415 #define VTERM_MAX_CHARS_PER_CELL 6
    416   uint32_t chars[VTERM_MAX_CHARS_PER_CELL];
    417   char     width;
    418   VTermScreenCellAttrs attrs;
    419   VTermColor fg, bg;
    420 } VTermScreenCell;
    421 
    422 typedef struct {
    423   int (*damage)(VTermRect rect, void *user);
    424   int (*moverect)(VTermRect dest, VTermRect src, void *user);
    425   int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
    426   int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
    427   int (*bell)(void *user);
    428   int (*resize)(int rows, int cols, void *user);
    429   int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user);
    430   int (*sb_popline)(int cols, VTermScreenCell *cells, void *user);
    431 } VTermScreenCallbacks;
    432 
    433 VTermScreen *vterm_obtain_screen(VTerm *vt);
    434 
    435 void  vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user);
    436 void *vterm_screen_get_cbdata(VTermScreen *screen);
    437 
    438 // Only invokes control, csi, osc, dcs
    439 void  vterm_screen_set_unrecognised_fallbacks(VTermScreen *screen, const VTermParserCallbacks *fallbacks, void *user);
    440 void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen);
    441 
    442 void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen);
    443 
    444 typedef enum {
    445   VTERM_DAMAGE_CELL,    /* every cell */
    446   VTERM_DAMAGE_ROW,     /* entire rows */
    447   VTERM_DAMAGE_SCREEN,  /* entire screen */
    448   VTERM_DAMAGE_SCROLL,  /* entire screen + scrollrect */
    449 
    450   VTERM_N_DAMAGES
    451 } VTermDamageSize;
    452 
    453 void vterm_screen_flush_damage(VTermScreen *screen);
    454 void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size);
    455 
    456 void   vterm_screen_reset(VTermScreen *screen, int hard);
    457 
    458 /* Neither of these functions NUL-terminate the buffer */
    459 size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect);
    460 size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect);
    461 
    462 typedef enum {
    463   VTERM_ATTR_BOLD_MASK       = 1 << 0,
    464   VTERM_ATTR_UNDERLINE_MASK  = 1 << 1,
    465   VTERM_ATTR_ITALIC_MASK     = 1 << 2,
    466   VTERM_ATTR_BLINK_MASK      = 1 << 3,
    467   VTERM_ATTR_REVERSE_MASK    = 1 << 4,
    468   VTERM_ATTR_STRIKE_MASK     = 1 << 5,
    469   VTERM_ATTR_FONT_MASK       = 1 << 6,
    470   VTERM_ATTR_FOREGROUND_MASK = 1 << 7,
    471   VTERM_ATTR_BACKGROUND_MASK = 1 << 8,
    472 
    473   VTERM_ALL_ATTRS_MASK = (1 << 9) - 1
    474 } VTermAttrMask;
    475 
    476 int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs);
    477 
    478 int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell);
    479 
    480 int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos);
    481 
    482 /**
    483  * Same as vterm_state_convert_color_to_rgb(), but takes a `screen` instead of a `state`
    484  * instance.
    485  */
    486 void vterm_screen_convert_color_to_rgb(const VTermScreen *screen, VTermColor *col);
    487 
    488 // ---------
    489 // Utilities
    490 // ---------
    491 
    492 VTermValueType vterm_get_attr_type(VTermAttr attr);
    493 VTermValueType vterm_get_prop_type(VTermProp prop);
    494 
    495 void vterm_scroll_rect(VTermRect rect,
    496                        int downward,
    497                        int rightward,
    498                        int (*moverect)(VTermRect src, VTermRect dest, void *user),
    499                        int (*eraserect)(VTermRect rect, int selective, void *user),
    500                        void *user);
    501 
    502 void vterm_copy_cells(VTermRect dest,
    503                       VTermRect src,
    504                       void (*copycell)(VTermPos dest, VTermPos src, void *user),
    505                       void *user);
    506 
    507 #ifdef __cplusplus
    508 }
    509 #endif
    510 
    511 #endif
    512