The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
vt.h File Reference

libvt: a terminal emulator without a screen. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  vt_cell_t
struct  vt_line_t
 a line of cells: on the screen, or in the scrollback. More...
struct  vt

Macros

#define VT_COLOR_DEFAULT   0u
 a colour of a cell: the default (the theme's text or background), one of the 256 palette colours, or a 24-bit colour.
#define VT_COLOR_INDEX   0x01000000u
#define VT_COLOR_RGB   0x02000000u
#define VT_COLOR_KIND(c)
#define VT_BOLD   0x0001u
#define VT_DIM   0x0002u
#define VT_ITALIC   0x0004u
#define VT_UNDERLINE   0x0008u
#define VT_BLINK   0x0010u
#define VT_INVERSE   0x0020u
#define VT_HIDDEN   0x0040u
#define VT_STRIKE   0x0080u
#define VT_MOD_SHIFT   0x01
#define VT_MOD_ALT   0x02
#define VT_MOD_CTRL   0x04

Typedefs

typedef 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, through the pty.
typedef struct vt vt_t

Enumerations

enum  vt_cursor_shape_t { VT_CURSOR_BLOCK , VT_CURSOR_UNDERLINE , VT_CURSOR_BAR }
enum  vt_mouse_mode_t { VT_MOUSE_OFF , VT_MOUSE_CLICKS , VT_MOUSE_DRAG , VT_MOUSE_MOTION }
enum  vt_key_t {
  VT_KEY_UP = 1 , VT_KEY_DOWN , VT_KEY_RIGHT , VT_KEY_LEFT ,
  VT_KEY_HOME , VT_KEY_END , VT_KEY_INSERT , VT_KEY_DELETE ,
  VT_KEY_PAGE_UP , VT_KEY_PAGE_DOWN , VT_KEY_ENTER , VT_KEY_TAB ,
  VT_KEY_BACKSPACE , VT_KEY_ESCAPE , VT_KEY_F1 , VT_KEY_F12 = VT_KEY_F1 + 11
}
enum  vt_mouse_t {
  VT_MOUSE_PRESS , VT_MOUSE_RELEASE , VT_MOUSE_MOVE , VT_MOUSE_WHEEL_UP ,
  VT_MOUSE_WHEEL_DOWN
}

Functions

vt_t * vt_new (int cols, int rows, int scrollback)
 a terminal of a size, with room for so many lines of scrollback.
void vt_free (vt_t *vt)
void vt_set_reply (vt_t *vt, vt_reply_t reply, void *user)
 where the answers go (without it, they are dropped).
void vt_write (vt_t *vt, const void *data, size_t length)
 the bytes the program wrote.
void vt_resize (vt_t *vt, int cols, int rows)
 a new size.
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, down to -vt_scrollback_lines).
int vt_scrollback_lines (const vt_t *vt)
 the lines in the scrollback now
bool vt_line_dirty (const vt_t *vt, int y)
 true if the screen line y changed since vt_clean
void vt_clean (vt_t *vt)
 forget what changed: the app drew it
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.
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.
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, ESC [ 1 ; m A).
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, Alt puts ESC in front.
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.
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, else "".
const char * vt_paste_end (const vt_t *vt)

Detailed Description

libvt: a terminal emulator without a screen.

the bytes a program writes to its terminal go in (vt_write); a grid of cells comes out, with the scrollback, the cursor and the modes the program set. libvt also makes the bytes for keys, the mouse and paste, as the program asked for them (vt_key, vt_mouse, vt_paste_begin).

vt_t *vt = vt_new(80, 24, 10000); vt_write(vt, buf, n); // what the pty gave for (int y = 0; y < vt->rows; y++) if (vt_line_dirty(vt, y)) draw_line(vt_line(vt, y)); vt_clean(vt);

the escape sequences are those of xterm that full-screen programs use: VT100 and VT220 cursor and erase, scroll regions, insert and delete, SGR with 16, 256 and 24-bit colours, the alternate screen, bracketed paste, mouse reports (X10, button and any-event, SGR), cursor shapes, the DEC line drawing characters and the window title (OSC 0 and 2). TERM is xterm-256color.

not yet: wide characters (CJK, emoji) take one cell, combining marks take a cell of their own, and the lines are not wrapped again on a resize.

Macro Definition Documentation

◆ VT_COLOR_DEFAULT

#define VT_COLOR_DEFAULT   0u

a colour of a cell: the default (the theme's text or background), one of the 256 palette colours, or a 24-bit colour.

◆ VT_COLOR_INDEX

#define VT_COLOR_INDEX   0x01000000u

◆ VT_COLOR_RGB

#define VT_COLOR_RGB   0x02000000u

◆ VT_COLOR_KIND

#define VT_COLOR_KIND ( c)
Value:
((c) & 0xFF000000u)

◆ VT_BOLD

#define VT_BOLD   0x0001u

◆ VT_DIM

#define VT_DIM   0x0002u

◆ VT_ITALIC

#define VT_ITALIC   0x0004u

◆ VT_UNDERLINE

#define VT_UNDERLINE   0x0008u

◆ VT_BLINK

#define VT_BLINK   0x0010u

◆ VT_INVERSE

#define VT_INVERSE   0x0020u

◆ VT_HIDDEN

#define VT_HIDDEN   0x0040u

◆ VT_STRIKE

#define VT_STRIKE   0x0080u

◆ VT_MOD_SHIFT

#define VT_MOD_SHIFT   0x01

◆ VT_MOD_ALT

#define VT_MOD_ALT   0x02

◆ VT_MOD_CTRL

#define VT_MOD_CTRL   0x04

Typedef Documentation

◆ vt_reply_t

typedef 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, through the pty.

◆ vt_t

typedef struct vt vt_t

Enumeration Type Documentation

◆ vt_cursor_shape_t

Enumerator
VT_CURSOR_BLOCK 
VT_CURSOR_UNDERLINE 
VT_CURSOR_BAR 

◆ vt_mouse_mode_t

Enumerator
VT_MOUSE_OFF 
VT_MOUSE_CLICKS 
VT_MOUSE_DRAG 
VT_MOUSE_MOTION 

◆ vt_key_t

enum vt_key_t
Enumerator
VT_KEY_UP 
VT_KEY_DOWN 
VT_KEY_RIGHT 
VT_KEY_LEFT 
VT_KEY_HOME 
VT_KEY_END 
VT_KEY_INSERT 
VT_KEY_DELETE 
VT_KEY_PAGE_UP 
VT_KEY_PAGE_DOWN 
VT_KEY_ENTER 
VT_KEY_TAB 
VT_KEY_BACKSPACE 
VT_KEY_ESCAPE 
VT_KEY_F1 
VT_KEY_F12 

◆ vt_mouse_t

enum vt_mouse_t
Enumerator
VT_MOUSE_PRESS 
VT_MOUSE_RELEASE 
VT_MOUSE_MOVE 
VT_MOUSE_WHEEL_UP 
VT_MOUSE_WHEEL_DOWN 

Function Documentation

◆ vt_new()

vt_t * vt_new ( int cols,
int rows,
int scrollback )

a terminal of a size, with room for so many lines of scrollback.

◆ vt_free()

void vt_free ( vt_t * vt)

◆ vt_set_reply()

void vt_set_reply ( vt_t * vt,
vt_reply_t reply,
void * user )

where the answers go (without it, they are dropped).

◆ vt_write()

void vt_write ( vt_t * vt,
const void * data,
size_t length )

the bytes the program wrote.

UTF-8 and escape sequences can be split between calls.

◆ vt_resize()

void vt_resize ( vt_t * vt,
int cols,
int rows )

a new size.

the lines keep their text (cut, or filled with empty cells); when the screen gets lower, the lines above the cursor go into the scrollback so the cursor stays on the screen.

◆ vt_line()

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, down to -vt_scrollback_lines).

NULL past the ends. a line of the scrollback can be narrower or wider than the screen.

◆ vt_scrollback_lines()

int vt_scrollback_lines ( const vt_t * vt)

the lines in the scrollback now

◆ vt_line_dirty()

bool vt_line_dirty ( const vt_t * vt,
int y )

true if the screen line y changed since vt_clean

◆ vt_clean()

void vt_clean ( vt_t * vt)

forget what changed: the app drew it

◆ vt_text()

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.

lines end with "\n" unless they wrapped; the empty cells at the end of a line are left out.

Returns
the bytes it needed (it writes at most size - 1 and a 0).

◆ vt_palette()

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.

the app can use its own 16 colours.

◆ vt_key()

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, ESC [ 1 ; m A).

Parameters
outroom for 16 bytes.
Returns
the length (0: the key sends nothing).

◆ vt_text_key()

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, Alt puts ESC in front.

Parameters
textUTF-8, as the keyboard layout made it.
outroom for 16 bytes.
Returns
the length.

◆ vt_mouse()

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.

Parameters
button0 left, 1 middle, 2 right, -1 none (motion).
x,ythe cell, from 0.
outroom for 32 bytes.
Returns
the length (0: nothing to send; the app can select instead).

◆ vt_paste_begin()

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, else "".

◆ vt_paste_end()

const char * vt_paste_end ( const vt_t * vt)