blob: cd7b1f77211ff0f9b648780392de336f01c10164 [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/*
2 * NOTE: This is a MODIFIED version of libvterm, see the README file.
3 */
4#ifndef __VTERM_H__
5#define __VTERM_H__
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
Bram Moolenaare4f25e42017-07-07 11:54:15 +020011#include <stdlib.h>
Bram Moolenaare4f25e42017-07-07 11:54:15 +020012
13#include "vterm_keycodes.h"
14
Bram Moolenaarb2a76ec2017-07-25 21:34:46 +020015#define TRUE 1
16#define FALSE 0
17
Bram Moolenaar607985a2017-07-28 17:04:15 +020018/* from stdint.h */
19typedef unsigned char uint8_t;
20typedef unsigned int uint32_t;
21
Bram Moolenaare4f25e42017-07-07 11:54:15 +020022typedef struct VTerm VTerm;
23typedef struct VTermState VTermState;
24typedef struct VTermScreen VTermScreen;
25
26/* Specifies a screen point. */
27typedef struct {
28 int row;
29 int col;
30} VTermPos;
31
32/*
33 * Some small utility functions; we can just keep these static here.
34 */
35
36/*
37 * Order points by on-screen flow order:
38 * Return < 0 if "a" is before "b"
39 * Return 0 if "a" and "b" are equal
40 * Return > 0 if "a" is after "b".
41 */
42int vterm_pos_cmp(VTermPos a, VTermPos b);
43
44#if defined(DEFINE_INLINES) || USE_INLINE
45INLINE int vterm_pos_cmp(VTermPos a, VTermPos b)
46{
47 return (a.row == b.row) ? a.col - b.col : a.row - b.row;
48}
49#endif
50
51/* Specifies a rectangular screen area. */
52typedef struct {
53 int start_row;
54 int end_row;
55 int start_col;
56 int end_col;
57} VTermRect;
58
59/* Return true if the rect "r" contains the point "p". */
60int vterm_rect_contains(VTermRect r, VTermPos p);
61
62#if defined(DEFINE_INLINES) || USE_INLINE
63INLINE int vterm_rect_contains(VTermRect r, VTermPos p)
64{
65 return p.row >= r.start_row && p.row < r.end_row &&
66 p.col >= r.start_col && p.col < r.end_col;
67}
68#endif
69
70/* Move "rect" "row_delta" down and "col_delta" right.
71 * Does not check boundaries. */
72void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta);
73
74#if defined(DEFINE_INLINES) || USE_INLINE
75INLINE void vterm_rect_move(VTermRect *rect, int row_delta, int col_delta)
76{
77 rect->start_row += row_delta; rect->end_row += row_delta;
78 rect->start_col += col_delta; rect->end_col += col_delta;
79}
80#endif
81
82typedef struct {
83 uint8_t red, green, blue;
84} VTermColor;
85
86typedef enum {
87 /* VTERM_VALUETYPE_NONE = 0 */
88 VTERM_VALUETYPE_BOOL = 1,
89 VTERM_VALUETYPE_INT,
90 VTERM_VALUETYPE_STRING,
91 VTERM_VALUETYPE_COLOR
92} VTermValueType;
93
94typedef union {
95 int boolean;
96 int number;
97 char *string;
98 VTermColor color;
99} VTermValue;
100
101typedef enum {
102 /* VTERM_ATTR_NONE = 0 */
103 VTERM_ATTR_BOLD = 1, /* bool: 1, 22 */
104 VTERM_ATTR_UNDERLINE, /* number: 4, 21, 24 */
105 VTERM_ATTR_ITALIC, /* bool: 3, 23 */
106 VTERM_ATTR_BLINK, /* bool: 5, 25 */
107 VTERM_ATTR_REVERSE, /* bool: 7, 27 */
108 VTERM_ATTR_STRIKE, /* bool: 9, 29 */
109 VTERM_ATTR_FONT, /* number: 10-19 */
110 VTERM_ATTR_FOREGROUND, /* color: 30-39 90-97 */
111 VTERM_ATTR_BACKGROUND /* color: 40-49 100-107 */
112} VTermAttr;
113
114typedef enum {
115 /* VTERM_PROP_NONE = 0 */
116 VTERM_PROP_CURSORVISIBLE = 1, /* bool */
117 VTERM_PROP_CURSORBLINK, /* bool */
118 VTERM_PROP_ALTSCREEN, /* bool */
119 VTERM_PROP_TITLE, /* string */
120 VTERM_PROP_ICONNAME, /* string */
121 VTERM_PROP_REVERSE, /* bool */
122 VTERM_PROP_CURSORSHAPE, /* number */
123 VTERM_PROP_MOUSE /* number */
124} VTermProp;
125
126enum {
127 VTERM_PROP_CURSORSHAPE_BLOCK = 1,
128 VTERM_PROP_CURSORSHAPE_UNDERLINE,
129 VTERM_PROP_CURSORSHAPE_BAR_LEFT
130};
131
132enum {
133 VTERM_PROP_MOUSE_NONE = 0,
134 VTERM_PROP_MOUSE_CLICK,
135 VTERM_PROP_MOUSE_DRAG,
136 VTERM_PROP_MOUSE_MOVE
137};
138
139typedef struct {
140 const uint32_t *chars;
141 int width;
142 unsigned int protected_cell:1; /* DECSCA-protected against DECSEL/DECSED */
143 unsigned int dwl:1; /* DECDWL or DECDHL double-width line */
144 unsigned int dhl:2; /* DECDHL double-height line (1=top 2=bottom) */
145} VTermGlyphInfo;
146
147typedef struct {
148 unsigned int doublewidth:1; /* DECDWL or DECDHL line */
149 unsigned int doubleheight:2; /* DECDHL line (1=top 2=bottom) */
150} VTermLineInfo;
151
152typedef struct {
153 /* libvterm relies on the allocated memory to be zeroed out before it is
154 * returned by the allocator. */
155 void *(*malloc)(size_t size, void *allocdata);
156 void (*free)(void *ptr, void *allocdata);
157} VTermAllocatorFunctions;
158
159/* Allocate and initialize a new terminal with default allocators. */
160VTerm *vterm_new(int rows, int cols);
161
162/* Allocate and initialize a new terminal with specified allocators. */
163VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata);
164
165/* Free and cleanup a terminal and all its data. */
166void vterm_free(VTerm* vt);
167
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200168/* Get the current size of the terminal and store in "rowsp" and "colsp". */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200169void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200170
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200171void vterm_set_size(VTerm *vt, int rows, int cols);
172
173int vterm_get_utf8(const VTerm *vt);
174void vterm_set_utf8(VTerm *vt, int is_utf8);
175
176size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len);
177
178size_t vterm_output_get_buffer_size(const VTerm *vt);
179size_t vterm_output_get_buffer_current(const VTerm *vt);
180size_t vterm_output_get_buffer_remaining(const VTerm *vt);
181
182size_t vterm_output_read(VTerm *vt, char *buffer, size_t len);
183
184void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod);
185void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod);
186
187void vterm_keyboard_start_paste(VTerm *vt);
188void vterm_keyboard_end_paste(VTerm *vt);
189
190void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod);
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200191/* "button" is 1 for left, 2 for middle, 3 for right.
192 * Button 4 is scroll wheel down, button 5 is scroll wheel up. */
Bram Moolenaarb2a76ec2017-07-25 21:34:46 +0200193void vterm_mouse_button(VTerm *vt, int button, int pressed, VTermModifier mod);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200194
195/* ------------
196 * Parser layer
197 * ------------ */
198
199/* Flag to indicate non-final subparameters in a single CSI parameter.
200 * Consider
201 * CSI 1;2:3:4;5a
202 * 1 4 and 5 are final.
203 * 2 and 3 are non-final and will have this bit set
204 *
205 * Don't confuse this with the final byte of the CSI escape; 'a' in this case.
206 */
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200207#define CSI_ARG_FLAG_MORE (1<<30)
208#define CSI_ARG_MASK (~(1<<30))
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200209
210#define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE)
211#define CSI_ARG(a) ((a) & CSI_ARG_MASK)
212
213/* Can't use -1 to indicate a missing argument; use this instead */
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200214#define CSI_ARG_MISSING ((1<<30)-1)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200215
216#define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING)
217#define CSI_ARG_OR(a,def) (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a))
218#define CSI_ARG_COUNT(a) (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a))
219
220typedef struct {
221 int (*text)(const char *bytes, size_t len, void *user);
222 int (*control)(unsigned char control, void *user);
223 int (*escape)(const char *bytes, size_t len, void *user);
224 int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
225 int (*osc)(const char *command, size_t cmdlen, void *user);
226 int (*dcs)(const char *command, size_t cmdlen, void *user);
227 int (*resize)(int rows, int cols, void *user);
228} VTermParserCallbacks;
229
230void vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user);
231void *vterm_parser_get_cbdata(VTerm *vt);
232
233/* -----------
234 * State layer
235 * ----------- */
236
237typedef struct {
238 int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user);
239 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
240 int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user);
241 int (*moverect)(VTermRect dest, VTermRect src, void *user);
242 int (*erase)(VTermRect rect, int selective, void *user);
243 int (*initpen)(void *user);
244 int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user);
Bram Moolenaarb2a76ec2017-07-25 21:34:46 +0200245 /* Callback for setting various properties. Must return 1 if the property
246 * was accepted, 0 otherwise. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
248 int (*bell)(void *user);
249 int (*resize)(int rows, int cols, VTermPos *delta, void *user);
250 int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user);
251} VTermStateCallbacks;
252
253VTermState *vterm_obtain_state(VTerm *vt);
254
255void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user);
256void *vterm_state_get_cbdata(VTermState *state);
257
258/* Only invokes control, csi, osc, dcs */
259void vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermParserCallbacks *fallbacks, void *user);
260void *vterm_state_get_unrecognised_fbdata(VTermState *state);
261
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200262/* Initialize the state. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263void vterm_state_reset(VTermState *state, int hard);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200264
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200265void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos);
266void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg);
267void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col);
268void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg);
269void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col);
270void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright);
271int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val);
272int vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val);
273const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row);
274
275/* ------------
276 * Screen layer
277 * ------------ */
278
279typedef struct {
280 unsigned int bold : 1;
281 unsigned int underline : 2;
282 unsigned int italic : 1;
283 unsigned int blink : 1;
284 unsigned int reverse : 1;
285 unsigned int strike : 1;
286 unsigned int font : 4; /* 0 to 9 */
287 unsigned int dwl : 1; /* On a DECDWL or DECDHL line */
288 unsigned int dhl : 2; /* On a DECDHL line (1=top 2=bottom) */
289} VTermScreenCellAttrs;
290
291typedef struct {
292#define VTERM_MAX_CHARS_PER_CELL 6
293 uint32_t chars[VTERM_MAX_CHARS_PER_CELL];
294 char width;
295 VTermScreenCellAttrs attrs;
296 VTermColor fg, bg;
297} VTermScreenCell;
298
299/* All fields are optional, NULL when not used. */
300typedef struct {
301 int (*damage)(VTermRect rect, void *user);
302 int (*moverect)(VTermRect dest, VTermRect src, void *user);
303 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
304 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
305 int (*bell)(void *user);
306 int (*resize)(int rows, int cols, void *user);
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200307 /* A line was pushed off the top of the window.
308 * "cells[cols]" contains the cells of that line.
309 * Return value is unused. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200310 int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user);
311 int (*sb_popline)(int cols, VTermScreenCell *cells, void *user);
312} VTermScreenCallbacks;
313
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200314/* Return the screen of the vterm. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200315VTermScreen *vterm_obtain_screen(VTerm *vt);
316
317/*
318 * Install screen callbacks. These are invoked when the screen contents is
319 * changed. "user" is passed into to the callback.
320 */
321void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user);
322void *vterm_screen_get_cbdata(VTermScreen *screen);
323
324/* Only invokes control, csi, osc, dcs */
325void vterm_screen_set_unrecognised_fallbacks(VTermScreen *screen, const VTermParserCallbacks *fallbacks, void *user);
326void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen);
327
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200328/* Enable support for using the alternate screen if "altscreen" is non-zero.
329 * Before that switching to the alternate screen won't work.
330 * Calling with "altscreen" zero has no effect. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200331void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen);
332
333typedef enum {
334 VTERM_DAMAGE_CELL, /* every cell */
335 VTERM_DAMAGE_ROW, /* entire rows */
336 VTERM_DAMAGE_SCREEN, /* entire screen */
337 VTERM_DAMAGE_SCROLL /* entire screen + scrollrect */
338} VTermDamageSize;
339
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200340/* Invoke the relevant callbacks to update the screen. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200341void vterm_screen_flush_damage(VTermScreen *screen);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200342
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200343void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size);
344
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200345/*
346 * Reset the screen. Also invokes vterm_state_reset().
347 * Must be called before the terminal can be used.
348 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200349void vterm_screen_reset(VTermScreen *screen, int hard);
350
351/* Neither of these functions NUL-terminate the buffer */
352size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect);
353size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect);
354
355typedef enum {
356 VTERM_ATTR_BOLD_MASK = 1 << 0,
357 VTERM_ATTR_UNDERLINE_MASK = 1 << 1,
358 VTERM_ATTR_ITALIC_MASK = 1 << 2,
359 VTERM_ATTR_BLINK_MASK = 1 << 3,
360 VTERM_ATTR_REVERSE_MASK = 1 << 4,
361 VTERM_ATTR_STRIKE_MASK = 1 << 5,
362 VTERM_ATTR_FONT_MASK = 1 << 6,
363 VTERM_ATTR_FOREGROUND_MASK = 1 << 7,
364 VTERM_ATTR_BACKGROUND_MASK = 1 << 8
365} VTermAttrMask;
366
367int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs);
368
369int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell);
370
371int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos);
372
373/* ---------
374 * Utilities
375 * --------- */
376
377VTermValueType vterm_get_attr_type(VTermAttr attr);
378VTermValueType vterm_get_prop_type(VTermProp prop);
379
380void vterm_scroll_rect(VTermRect rect,
381 int downward,
382 int rightward,
383 int (*moverect)(VTermRect src, VTermRect dest, void *user),
384 int (*eraserect)(VTermRect rect, int selective, void *user),
385 void *user);
386
387void vterm_copy_cells(VTermRect dest,
388 VTermRect src,
389 void (*copycell)(VTermPos dest, VTermPos src, void *user),
390 void *user);
391
392#ifdef __cplusplus
393}
394#endif
395
396#endif