blob: 50a840a316f4bbd7485c963746ab9f3cecf231b2 [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 */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200123 VTERM_PROP_MOUSE, /* number */
124 VTERM_PROP_CURSORCOLOR /* string */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125} VTermProp;
126
127enum {
128 VTERM_PROP_CURSORSHAPE_BLOCK = 1,
129 VTERM_PROP_CURSORSHAPE_UNDERLINE,
130 VTERM_PROP_CURSORSHAPE_BAR_LEFT
131};
132
133enum {
134 VTERM_PROP_MOUSE_NONE = 0,
135 VTERM_PROP_MOUSE_CLICK,
136 VTERM_PROP_MOUSE_DRAG,
137 VTERM_PROP_MOUSE_MOVE
138};
139
140typedef struct {
141 const uint32_t *chars;
142 int width;
143 unsigned int protected_cell:1; /* DECSCA-protected against DECSEL/DECSED */
144 unsigned int dwl:1; /* DECDWL or DECDHL double-width line */
145 unsigned int dhl:2; /* DECDHL double-height line (1=top 2=bottom) */
146} VTermGlyphInfo;
147
148typedef struct {
149 unsigned int doublewidth:1; /* DECDWL or DECDHL line */
150 unsigned int doubleheight:2; /* DECDHL line (1=top 2=bottom) */
151} VTermLineInfo;
152
153typedef struct {
154 /* libvterm relies on the allocated memory to be zeroed out before it is
155 * returned by the allocator. */
156 void *(*malloc)(size_t size, void *allocdata);
157 void (*free)(void *ptr, void *allocdata);
158} VTermAllocatorFunctions;
159
160/* Allocate and initialize a new terminal with default allocators. */
161VTerm *vterm_new(int rows, int cols);
162
163/* Allocate and initialize a new terminal with specified allocators. */
164VTerm *vterm_new_with_allocator(int rows, int cols, VTermAllocatorFunctions *funcs, void *allocdata);
165
166/* Free and cleanup a terminal and all its data. */
167void vterm_free(VTerm* vt);
168
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200169/* Get the current size of the terminal and store in "rowsp" and "colsp". */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200170void vterm_get_size(const VTerm *vt, int *rowsp, int *colsp);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200171
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200172void vterm_set_size(VTerm *vt, int rows, int cols);
173
174int vterm_get_utf8(const VTerm *vt);
175void vterm_set_utf8(VTerm *vt, int is_utf8);
176
177size_t vterm_input_write(VTerm *vt, const char *bytes, size_t len);
178
179size_t vterm_output_get_buffer_size(const VTerm *vt);
180size_t vterm_output_get_buffer_current(const VTerm *vt);
181size_t vterm_output_get_buffer_remaining(const VTerm *vt);
182
183size_t vterm_output_read(VTerm *vt, char *buffer, size_t len);
184
185void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod);
186void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod);
187
188void vterm_keyboard_start_paste(VTerm *vt);
189void vterm_keyboard_end_paste(VTerm *vt);
190
191void vterm_mouse_move(VTerm *vt, int row, int col, VTermModifier mod);
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200192/* "button" is 1 for left, 2 for middle, 3 for right.
193 * Button 4 is scroll wheel down, button 5 is scroll wheel up. */
Bram Moolenaarb2a76ec2017-07-25 21:34:46 +0200194void vterm_mouse_button(VTerm *vt, int button, int pressed, VTermModifier mod);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200195
196/* ------------
197 * Parser layer
198 * ------------ */
199
200/* Flag to indicate non-final subparameters in a single CSI parameter.
201 * Consider
202 * CSI 1;2:3:4;5a
203 * 1 4 and 5 are final.
204 * 2 and 3 are non-final and will have this bit set
205 *
206 * Don't confuse this with the final byte of the CSI escape; 'a' in this case.
207 */
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200208#define CSI_ARG_FLAG_MORE (1<<30)
209#define CSI_ARG_MASK (~(1<<30))
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200210
211#define CSI_ARG_HAS_MORE(a) ((a) & CSI_ARG_FLAG_MORE)
212#define CSI_ARG(a) ((a) & CSI_ARG_MASK)
213
214/* Can't use -1 to indicate a missing argument; use this instead */
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200215#define CSI_ARG_MISSING ((1<<30)-1)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200216
217#define CSI_ARG_IS_MISSING(a) (CSI_ARG(a) == CSI_ARG_MISSING)
218#define CSI_ARG_OR(a,def) (CSI_ARG(a) == CSI_ARG_MISSING ? (def) : CSI_ARG(a))
219#define CSI_ARG_COUNT(a) (CSI_ARG(a) == CSI_ARG_MISSING || CSI_ARG(a) == 0 ? 1 : CSI_ARG(a))
220
221typedef struct {
222 int (*text)(const char *bytes, size_t len, void *user);
223 int (*control)(unsigned char control, void *user);
224 int (*escape)(const char *bytes, size_t len, void *user);
225 int (*csi)(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user);
226 int (*osc)(const char *command, size_t cmdlen, void *user);
227 int (*dcs)(const char *command, size_t cmdlen, void *user);
228 int (*resize)(int rows, int cols, void *user);
229} VTermParserCallbacks;
230
231void vterm_parser_set_callbacks(VTerm *vt, const VTermParserCallbacks *callbacks, void *user);
232void *vterm_parser_get_cbdata(VTerm *vt);
233
234/* -----------
235 * State layer
236 * ----------- */
237
238typedef struct {
239 int (*putglyph)(VTermGlyphInfo *info, VTermPos pos, void *user);
240 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
241 int (*scrollrect)(VTermRect rect, int downward, int rightward, void *user);
242 int (*moverect)(VTermRect dest, VTermRect src, void *user);
243 int (*erase)(VTermRect rect, int selective, void *user);
244 int (*initpen)(void *user);
245 int (*setpenattr)(VTermAttr attr, VTermValue *val, void *user);
Bram Moolenaarb2a76ec2017-07-25 21:34:46 +0200246 /* Callback for setting various properties. Must return 1 if the property
247 * was accepted, 0 otherwise. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
249 int (*bell)(void *user);
250 int (*resize)(int rows, int cols, VTermPos *delta, void *user);
251 int (*setlineinfo)(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user);
252} VTermStateCallbacks;
253
254VTermState *vterm_obtain_state(VTerm *vt);
255
256void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user);
257void *vterm_state_get_cbdata(VTermState *state);
258
259/* Only invokes control, csi, osc, dcs */
260void vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermParserCallbacks *fallbacks, void *user);
261void *vterm_state_get_unrecognised_fbdata(VTermState *state);
262
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200263/* Initialize the state. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264void vterm_state_reset(VTermState *state, int hard);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200265
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200266void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos);
267void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg);
268void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col);
269void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg);
270void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col);
271void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright);
272int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val);
273int vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val);
274const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row);
275
276/* ------------
277 * Screen layer
278 * ------------ */
279
280typedef struct {
281 unsigned int bold : 1;
282 unsigned int underline : 2;
283 unsigned int italic : 1;
284 unsigned int blink : 1;
285 unsigned int reverse : 1;
286 unsigned int strike : 1;
287 unsigned int font : 4; /* 0 to 9 */
288 unsigned int dwl : 1; /* On a DECDWL or DECDHL line */
289 unsigned int dhl : 2; /* On a DECDHL line (1=top 2=bottom) */
290} VTermScreenCellAttrs;
291
292typedef struct {
293#define VTERM_MAX_CHARS_PER_CELL 6
294 uint32_t chars[VTERM_MAX_CHARS_PER_CELL];
295 char width;
296 VTermScreenCellAttrs attrs;
297 VTermColor fg, bg;
298} VTermScreenCell;
299
300/* All fields are optional, NULL when not used. */
301typedef struct {
302 int (*damage)(VTermRect rect, void *user);
303 int (*moverect)(VTermRect dest, VTermRect src, void *user);
304 int (*movecursor)(VTermPos pos, VTermPos oldpos, int visible, void *user);
305 int (*settermprop)(VTermProp prop, VTermValue *val, void *user);
306 int (*bell)(void *user);
307 int (*resize)(int rows, int cols, void *user);
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200308 /* A line was pushed off the top of the window.
309 * "cells[cols]" contains the cells of that line.
310 * Return value is unused. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200311 int (*sb_pushline)(int cols, const VTermScreenCell *cells, void *user);
312 int (*sb_popline)(int cols, VTermScreenCell *cells, void *user);
313} VTermScreenCallbacks;
314
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200315/* Return the screen of the vterm. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200316VTermScreen *vterm_obtain_screen(VTerm *vt);
317
318/*
319 * Install screen callbacks. These are invoked when the screen contents is
320 * changed. "user" is passed into to the callback.
321 */
322void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user);
323void *vterm_screen_get_cbdata(VTermScreen *screen);
324
325/* Only invokes control, csi, osc, dcs */
326void vterm_screen_set_unrecognised_fallbacks(VTermScreen *screen, const VTermParserCallbacks *fallbacks, void *user);
327void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen);
328
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200329/* Enable support for using the alternate screen if "altscreen" is non-zero.
330 * Before that switching to the alternate screen won't work.
331 * Calling with "altscreen" zero has no effect. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200332void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen);
333
334typedef enum {
335 VTERM_DAMAGE_CELL, /* every cell */
336 VTERM_DAMAGE_ROW, /* entire rows */
337 VTERM_DAMAGE_SCREEN, /* entire screen */
338 VTERM_DAMAGE_SCROLL /* entire screen + scrollrect */
339} VTermDamageSize;
340
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200341/* Invoke the relevant callbacks to update the screen. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200342void vterm_screen_flush_damage(VTermScreen *screen);
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200343
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200344void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size);
345
Bram Moolenaar9cc5f752017-07-23 22:07:27 +0200346/*
347 * Reset the screen. Also invokes vterm_state_reset().
348 * Must be called before the terminal can be used.
349 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200350void vterm_screen_reset(VTermScreen *screen, int hard);
351
352/* Neither of these functions NUL-terminate the buffer */
353size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect);
354size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect);
355
356typedef enum {
357 VTERM_ATTR_BOLD_MASK = 1 << 0,
358 VTERM_ATTR_UNDERLINE_MASK = 1 << 1,
359 VTERM_ATTR_ITALIC_MASK = 1 << 2,
360 VTERM_ATTR_BLINK_MASK = 1 << 3,
361 VTERM_ATTR_REVERSE_MASK = 1 << 4,
362 VTERM_ATTR_STRIKE_MASK = 1 << 5,
363 VTERM_ATTR_FONT_MASK = 1 << 6,
364 VTERM_ATTR_FOREGROUND_MASK = 1 << 7,
365 VTERM_ATTR_BACKGROUND_MASK = 1 << 8
366} VTermAttrMask;
367
368int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs);
369
370int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell);
371
372int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos);
373
374/* ---------
375 * Utilities
376 * --------- */
377
378VTermValueType vterm_get_attr_type(VTermAttr attr);
379VTermValueType vterm_get_prop_type(VTermProp prop);
380
381void vterm_scroll_rect(VTermRect rect,
382 int downward,
383 int rightward,
384 int (*moverect)(VTermRect src, VTermRect dest, void *user),
385 int (*eraserect)(VTermRect rect, int selective, void *user),
386 void *user);
387
388void vterm_copy_cells(VTermRect dest,
389 VTermRect src,
390 void (*copycell)(VTermPos dest, VTermPos src, void *user),
391 void *user);
392
393#ifdef __cplusplus
394}
395#endif
396
397#endif