The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
vt.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file vt.h
5 * @brief libvt: a terminal emulator without a screen.
6 *
7 * the bytes a program writes to its terminal go in (vt_write); a grid of
8 * cells comes out, with the scrollback, the cursor and the modes the
9 * program set. libvt also makes the bytes for keys, the mouse and paste,
10 * as the program asked for them (vt_key, vt_mouse, vt_paste_begin).
11 *
12 * vt_t *vt = vt_new(80, 24, 10000);
13 * vt_write(vt, buf, n); // what the pty gave
14 * for (int y = 0; y < vt->rows; y++)
15 * if (vt_line_dirty(vt, y))
16 * draw_line(vt_line(vt, y));
17 * vt_clean(vt);
18 *
19 * the escape sequences are those of xterm that full-screen programs use:
20 * VT100 and VT220 cursor and erase, scroll regions, insert and delete,
21 * SGR with 16, 256 and 24-bit colours, the alternate screen, bracketed
22 * paste, mouse reports (X10, button and any-event, SGR), cursor shapes,
23 * the DEC line drawing characters and the window title (OSC 0 and 2).
24 * TERM is xterm-256color.
25 *
26 * not yet: wide characters (CJK, emoji) take one cell, combining marks
27 * take a cell of their own, and the lines are not wrapped again on a
28 * resize.
29 */
30
31#include <stdbool.h>
32#include <stddef.h>
33#include <stdint.h>
34
35/**
36 * a colour of a cell: the default (the theme's text or background), one
37 * of the 256 palette colours, or a 24-bit colour.
38 */
39#define VT_COLOR_DEFAULT 0u
40#define VT_COLOR_INDEX 0x01000000u // | the palette index (0 to 255)
41#define VT_COLOR_RGB 0x02000000u // | 0xRRGGBB
42#define VT_COLOR_KIND(c) ((c) & 0xFF000000u)
43
44// cell attributes (vt_cell_t.attr)
45#define VT_BOLD 0x0001u
46#define VT_DIM 0x0002u
47#define VT_ITALIC 0x0004u
48#define VT_UNDERLINE 0x0008u
49#define VT_BLINK 0x0010u
50#define VT_INVERSE 0x0020u
51#define VT_HIDDEN 0x0040u
52#define VT_STRIKE 0x0080u
53
54typedef struct
55{
56 uint32_t ch; // the code point; 0 is an empty cell (drawn as a space)
57 uint32_t fg; // VT_COLOR_
59 uint16_t attr; // VT_BOLD and the others
60} vt_cell_t;
61
62/**
63 * a line of cells: on the screen, or in the scrollback.
64 */
65typedef struct
66{
67 uint16_t cols; // the width it was made with
68 bool wrapped; // the text goes on in the next line (for copy)
69 bool dirty; // changed since vt_clean (screen lines only)
71} vt_line_t;
72
79
80// the mouse reports a program asked for (vt_t.mouse)
81typedef enum
82{
84 VT_MOUSE_CLICKS, // ?1000: presses and releases
85 VT_MOUSE_DRAG, // ?1002: and motion with a button down
86 VT_MOUSE_MOTION, // ?1003: and all motion
88
89/**
90 * where the terminal sends its answers (the cursor position, the device
91 * attributes): to the program, through the pty.
92 */
93typedef void (*vt_reply_t)(const char *data, size_t length, void *user);
94
95typedef struct vt vt_t;
96
97struct vt
98{
99 // read these; change them only through the functions
100 int cols;
101 int rows;
102 int cursor_x; // 0 to cols - 1
103 int cursor_y; // 0 to rows - 1
107 bool alt_screen; // the alternate screen is shown (no scrollback)
108 bool app_cursor_keys; // DECCKM: arrows send ESC O A
112 bool mouse_sgr; // ?1006: SGR mouse reports
113 bool focus_reports; // ?1004
114 bool reverse_video; // ?5: the whole screen in inverse
115 char title[256]; // from OSC 0 or 2
116 bool title_changed; // set when the title changes; the app clears it
117 bool bell; // a BEL came; the app clears it
118 int scrolled; // lines the whole screen moved up since vt_clean
119
120 struct vt_private *p; // the parser, the screens, the scrollback
121};
122
123/**
124 * a terminal of a size, with room for so many lines of scrollback.
125 */
126vt_t *vt_new(int cols, int rows, int scrollback);
127void vt_free(vt_t *vt);
128
129/**
130 * where the answers go (without it, they are dropped).
131 */
132void vt_set_reply(vt_t *vt, vt_reply_t reply, void *user);
133
134/**
135 * the bytes the program wrote. UTF-8 and escape sequences can be split
136 * between calls.
137 */
138void vt_write(vt_t *vt, const void *data, size_t length);
139
140/**
141 * a new size. the lines keep their text (cut, or filled with empty
142 * cells); when the screen gets lower, the lines above the cursor go into
143 * the scrollback so the cursor stays on the screen.
144 */
145void vt_resize(vt_t *vt, int cols, int rows);
146
147/**
148 * a line of the screen (0 to rows - 1), or of the scrollback (-1 is the
149 * newest line, down to -vt_scrollback_lines). NULL past the ends. a line
150 * of the scrollback can be narrower or wider than the screen.
151 */
152const vt_line_t *vt_line(const vt_t *vt, int y);
153
154/** the lines in the scrollback now */
155int vt_scrollback_lines(const vt_t *vt);
156
157/** true if the screen line y changed since vt_clean */
158bool vt_line_dirty(const vt_t *vt, int y);
159
160/** forget what changed: the app drew it */
161void vt_clean(vt_t *vt);
162
163/**
164 * the text from (x0, y0) to (x1, y1), both included, as UTF-8; y as for
165 * vt_line. lines end with "\n" unless they wrapped; the empty cells at
166 * the end of a line are left out.
167 * @return the bytes it needed (it writes at most size - 1 and a 0).
168 */
169size_t vt_text(const vt_t *vt, int x0, int y0, int x1, int y1, char *out, size_t size);
170
171/**
172 * the RGB colour of palette entry 0 to 255: the 16 colours of xterm, the
173 * 6x6x6 cube and the grey ramp. the app can use its own 16 colours.
174 */
175uint32_t vt_palette(int index);
176
177// keys that are not text
197
198// modifier keys for vt_key, vt_text_key and vt_mouse
199#define VT_MOD_SHIFT 0x01
200#define VT_MOD_ALT 0x02
201#define VT_MOD_CTRL 0x04
202
203/**
204 * the bytes a key sends, as xterm sends them (the arrows follow
205 * DECCKM; with modifiers, ESC [ 1 ; m A).
206 * @param out room for 16 bytes.
207 * @return the length (0: the key sends nothing).
208 */
209int vt_key(const vt_t *vt, vt_key_t key, int mods, char *out);
210
211/**
212 * the bytes typed text sends: Ctrl with a letter or one of @[\‍]^_ and
213 * space gives the control character, Alt puts ESC in front.
214 * @param text UTF-8, as the keyboard layout made it.
215 * @param out room for 16 bytes.
216 * @return the length.
217 */
218int vt_text_key(const char *text, int mods, char *out);
219
220typedef enum
221{
224 VT_MOUSE_MOVE, // with a button down, or (button -1) with none
227} vt_mouse_t;
228
229/**
230 * a mouse report for the program, if its mode asks for this one.
231 * @param button 0 left, 1 middle, 2 right, -1 none (motion).
232 * @param x, y the cell, from 0.
233 * @param out room for 32 bytes.
234 * @return the length (0: nothing to send; the app can select instead).
235 */
236int vt_mouse(const vt_t *vt, vt_mouse_t kind, int button, int x, int y, int mods, char *out);
237
238/**
239 * what goes before and after pasted text: the bracketed paste marks when
240 * the program asked for them, else "".
241 */
242const char *vt_paste_begin(const vt_t *vt);
243const char *vt_paste_end(const vt_t *vt);
unsigned int uint32_t
Definition fb.h:17
NULL, size_t, ptrdiff_t, offsetof.
integer types of fixed width.
unsigned short uint16_t
Definition stdint.h:13
bool, true and false.
Definition vt.h:55
uint16_t attr
Definition vt.h:59
uint32_t bg
Definition vt.h:58
uint32_t fg
Definition vt.h:57
uint32_t ch
Definition vt.h:56
a line of cells: on the screen, or in the scrollback.
Definition vt.h:66
bool dirty
Definition vt.h:69
uint16_t cols
Definition vt.h:67
vt_cell_t cells[]
Definition vt.h:70
bool wrapped
Definition vt.h:68
Definition vt.h:98
char title[256]
Definition vt.h:115
int rows
Definition vt.h:101
bool focus_reports
Definition vt.h:113
bool alt_screen
Definition vt.h:107
vt_cursor_shape_t cursor_shape
Definition vt.h:106
bool bell
Definition vt.h:117
struct vt_private * p
Definition vt.h:120
int scrolled
Definition vt.h:118
bool bracketed_paste
Definition vt.h:110
int cols
Definition vt.h:100
bool reverse_video
Definition vt.h:114
bool app_cursor_keys
Definition vt.h:108
vt_mouse_mode_t mouse
Definition vt.h:111
bool mouse_sgr
Definition vt.h:112
int cursor_x
Definition vt.h:102
int cursor_y
Definition vt.h:103
bool cursor_visible
Definition vt.h:104
bool app_keypad
Definition vt.h:109
bool title_changed
Definition vt.h:116
bool cursor_blink
Definition vt.h:105
int vt_mouse(const vt_t *vt, vt_mouse_t kind, int button, int x, int y, int mods, char *out)
a mouse report for the program, if its mode asks for this one.
Definition vt.c:1553
const char * vt_paste_begin(const vt_t *vt)
what goes before and after pasted text: the bracketed paste marks when the program asked for them,...
Definition vt.c:1596
vt_cursor_shape_t
Definition vt.h:74
@ VT_CURSOR_BAR
Definition vt.h:77
@ VT_CURSOR_UNDERLINE
Definition vt.h:76
@ VT_CURSOR_BLOCK
Definition vt.h:75
size_t vt_text(const vt_t *vt, int x0, int y0, int x1, int y1, char *out, size_t size)
the text from (x0, y0) to (x1, y1), both included, as UTF-8; y as for vt_line.
Definition vt.c:1403
const char * vt_paste_end(const vt_t *vt)
Definition vt.c:1601
void vt_clean(vt_t *vt)
forget what changed: the app drew it
Definition vt.c:1359
void vt_write(vt_t *vt, const void *data, size_t length)
the bytes the program wrote.
Definition vt.c:1266
vt_mouse_t
Definition vt.h:221
@ VT_MOUSE_PRESS
Definition vt.h:222
@ VT_MOUSE_WHEEL_UP
Definition vt.h:225
@ VT_MOUSE_WHEEL_DOWN
Definition vt.h:226
@ VT_MOUSE_MOVE
Definition vt.h:224
@ VT_MOUSE_RELEASE
Definition vt.h:223
int vt_scrollback_lines(const vt_t *vt)
the lines in the scrollback now
Definition vt.c:1349
const vt_line_t * vt_line(const vt_t *vt, int y)
a line of the screen (0 to rows - 1), or of the scrollback (-1 is the newest line,...
Definition vt.c:1338
uint32_t vt_palette(int index)
the RGB colour of palette entry 0 to 255: the 16 colours of xterm, the 6x6x6 cube and the grey ramp.
Definition vt.c:1434
void vt_set_reply(vt_t *vt, vt_reply_t reply, void *user)
where the answers go (without it, they are dropped).
Definition vt.c:328
int vt_key(const vt_t *vt, vt_key_t key, int mods, char *out)
the bytes a key sends, as xterm sends them (the arrows follow DECCKM; with modifiers,...
Definition vt.c:1453
bool vt_line_dirty(const vt_t *vt, int y)
true if the screen line y changed since vt_clean
Definition vt.c:1354
int vt_text_key(const char *text, int mods, char *out)
the bytes typed text sends: Ctrl with a letter or one of []^_ and space gives the control character,...
Definition vt.c:1513
vt_key_t
Definition vt.h:179
@ VT_KEY_ESCAPE
Definition vt.h:193
@ VT_KEY_ENTER
Definition vt.h:190
@ VT_KEY_RIGHT
Definition vt.h:182
@ VT_KEY_TAB
Definition vt.h:191
@ VT_KEY_INSERT
Definition vt.h:186
@ VT_KEY_LEFT
Definition vt.h:183
@ VT_KEY_BACKSPACE
Definition vt.h:192
@ VT_KEY_F12
Definition vt.h:195
@ VT_KEY_END
Definition vt.h:185
@ VT_KEY_PAGE_DOWN
Definition vt.h:189
@ VT_KEY_F1
Definition vt.h:194
@ VT_KEY_PAGE_UP
Definition vt.h:188
@ VT_KEY_DOWN
Definition vt.h:181
@ VT_KEY_DELETE
Definition vt.h:187
@ VT_KEY_UP
Definition vt.h:180
@ VT_KEY_HOME
Definition vt.h:184
void(*) vt_reply_t(const char *data, size_t length, void *user)
where the terminal sends its answers (the cursor position, the device attributes): to the program,...
Definition vt.h:93
vt_mouse_mode_t
Definition vt.h:82
@ VT_MOUSE_CLICKS
Definition vt.h:84
@ VT_MOUSE_OFF
Definition vt.h:83
@ VT_MOUSE_DRAG
Definition vt.h:85
@ VT_MOUSE_MOTION
Definition vt.h:86
void vt_resize(vt_t *vt, int cols, int rows)
a new size.
Definition vt.c:1306
vt_t * vt_new(int cols, int rows, int scrollback)
a terminal of a size, with room for so many lines of scrollback.
Definition vt.c:288
void vt_free(vt_t *vt)
Definition vt.c:315
struct vt vt_t
Definition vt.h:95