The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
Loading...
Searching...
No Matches
tty.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
common/stdint.h
"
4
5
/**
6
* @file tty.h
7
* @brief terminal (TTY) layer.
8
*
9
* a tty_t is a terminal state machine sitting between byte-oriented callers
10
* (kernel printf, the write syscall) and a character-cell backend (the VGA
11
* text buffer today, a serial line or framebuffer later). it owns the ANSI
12
* escape parser, cursor handling, and line-discipline mode, none of which the
13
* backend needs to know about. the backend only has to place a glyph at a
14
* cell, scroll, clear, and move the hardware cursor.
15
*
16
* splitting this out of the VGA driver lets a future system have several
17
* terminals (virtual consoles, a serial tty) that share one state machine.
18
*/
19
20
// how many decimal parameters a CSI escape sequence may carry (e.g. ESC[r;cH).
21
#define TTY_CSI_PARAMS_MAX 4
22
23
// line-discipline modes, matching the ttyctl() syscall values.
24
#define TTY_MODE_COOKED 0
// line-buffered, editable input
25
#define TTY_MODE_RAW 1
// raw byte/event input, no editing
26
27
// ANSI colour indices 0-7 (normal) and 8-15 (bright). the backend maps these
28
// to its own representation. defaults match a light-grey-on-black console.
29
#define TTY_COLOR_DEFAULT_FG 7
// light grey
30
#define TTY_COLOR_DEFAULT_BG 0
// black
31
32
/**
33
* character-cell backend a tty draws onto. put_at/scroll/clear/set_cursor/
34
* get_cursor/rows/cols are required; set_colors and draw_cursor are optional
35
* (NULL is fine) so a backend that has no notion of colour or needs no
36
* software caret can omit them.
37
* coordinates are zero-based (row, col); the backend defines its own bounds.
38
*/
39
typedef
struct
40
{
41
void (*
put_at
)(
char
c,
uint8_t
row,
uint8_t
col);
// place a glyph at a cell
42
void (*
scroll
)(void);
// scroll up one row
43
void (*
clear
)(void);
// clear screen, cursor home
44
void (*
set_cursor
)(
uint8_t
row,
uint8_t
col);
// move the cursor
45
void (*
get_cursor
)(
uint8_t
*row,
uint8_t
*col);
// read the cursor
46
// set the fg/bg used by subsequent put_at calls, as ANSI colour indices
47
// (0-15). optional; NULL means the backend is monochrome.
48
void (*
set_colors
)(
uint8_t
fg,
uint8_t
bg);
49
// draw (or erase) a caret at a cell. optional; NULL means the backend has
50
// its own cursor (e.g. VGA hardware cursor).
51
void (*
draw_cursor
)(
uint8_t
row,
uint8_t
col,
bool
visible);
52
// push buffered drawing to the display. optional; NULL means the backend
53
// draws directly (e.g. VGA text buffer, or an unbuffered framebuffer).
54
void (*
flush
)(void);
55
uint8_t
rows
;
// backend height in cells
56
uint8_t
cols
;
// backend width in cells
57
}
tty_backend_t
;
58
59
// ANSI escape parser states.
60
typedef
enum
61
{
62
TTY_STATE_NORMAL
,
// ordinary character output
63
TTY_STATE_ESC
,
// saw ESC (0x1B)
64
TTY_STATE_CSI
,
// saw ESC [
65
TTY_STATE_CSI_PRIV
,
// saw ESC [ ? (DEC private mode)
66
}
tty_state_t
;
67
68
/**
69
* a terminal instance.
70
*/
71
typedef
struct
72
{
73
const
tty_backend_t
*
backend
;
// where glyphs and cursor moves go
74
tty_state_t
state
;
// ANSI parser state
75
int
csi_params
[
TTY_CSI_PARAMS_MAX
];
// collected CSI numeric parameters
76
int
csi_param_count
;
// how many parameters have been separated
77
int
csi_current_param
;
// parameter currently being accumulated
78
uint8_t
mode
;
// TTY_MODE_COOKED or TTY_MODE_RAW
79
uint8_t
fg
;
// current foreground ANSI colour index (0-15)
80
uint8_t
bg
;
// current background ANSI colour index (0-15)
81
bool
bold
;
// bold/bright attribute (brightens fg)
82
uint32_t
batch_depth
;
// >0 while inside tty_begin_batch/end_batch
83
}
tty_t
;
84
85
/**
86
* initialize a tty on a backend. starts in cooked mode, normal parser state.
87
* @param tty the terminal to initialize.
88
* @param backend the character-cell backend (must outlive the tty).
89
*/
90
void
tty_init
(
tty_t
*tty,
const
tty_backend_t
*backend);
91
92
/**
93
* feed one output byte through the terminal state machine.
94
* printable characters are drawn; control characters and ANSI escape
95
* sequences are interpreted (cursor movement, erase, etc.).
96
* @param tty the terminal.
97
* @param c the byte to process.
98
*/
99
void
tty_putchar
(
tty_t
*tty,
char
c);
100
101
/**
102
* feed a null-terminated string through tty_putchar(), as one batch.
103
* @param tty the terminal.
104
* @param str the string to write.
105
*/
106
void
tty_write
(
tty_t
*tty,
const
char
*str);
107
108
/**
109
* start a batch of output. until the matching tty_end_batch(), bytes are drawn
110
* but the caret is hidden and nothing is flushed to the display, so the
111
* screen never shows a half-finished update (e.g. a shell redrawing its line,
112
* where the cursor briefly visits column 0). batches nest.
113
* @param tty the terminal.
114
*/
115
void
tty_begin_batch
(
tty_t
*tty);
116
117
/**
118
* end a batch: redraw the caret where the cursor ended up and flush once.
119
* @param tty the terminal.
120
*/
121
void
tty_end_batch
(
tty_t
*tty);
122
123
/**
124
* set the line-discipline mode (TTY_MODE_COOKED or TTY_MODE_RAW).
125
* @param tty the terminal.
126
* @param mode the new mode.
127
* @return the previous mode.
128
*/
129
uint8_t
tty_set_mode
(
tty_t
*tty,
uint8_t
mode);
stdint.h
uint32_t
unsigned int uint32_t
Definition
fb.h:17
uint8_t
unsigned char uint8_t
Definition
fb.h:18
tty_backend_t
character-cell backend a tty draws onto.
Definition
tty.h:40
tty_backend_t::clear
void(*) clear(void)
Definition
tty.h:43
tty_backend_t::draw_cursor
void(*) draw_cursor(uint8_t row, uint8_t col, bool visible)
Definition
tty.h:51
tty_backend_t::set_cursor
void(*) set_cursor(uint8_t row, uint8_t col)
Definition
tty.h:44
tty_backend_t::rows
uint8_t rows
Definition
tty.h:55
tty_backend_t::cols
uint8_t cols
Definition
tty.h:56
tty_backend_t::set_colors
void(*) set_colors(uint8_t fg, uint8_t bg)
Definition
tty.h:48
tty_backend_t::scroll
void(*) scroll(void)
Definition
tty.h:42
tty_backend_t::put_at
void(*) put_at(char c, uint8_t row, uint8_t col)
Definition
tty.h:41
tty_backend_t::flush
void(*) flush(void)
Definition
tty.h:54
tty_backend_t::get_cursor
void(*) get_cursor(uint8_t *row, uint8_t *col)
Definition
tty.h:45
tty_t
a terminal instance.
Definition
tty.h:72
tty_t::mode
uint8_t mode
Definition
tty.h:78
tty_t::csi_param_count
int csi_param_count
Definition
tty.h:76
tty_t::csi_current_param
int csi_current_param
Definition
tty.h:77
tty_t::csi_params
int csi_params[TTY_CSI_PARAMS_MAX]
Definition
tty.h:75
tty_t::state
tty_state_t state
Definition
tty.h:74
tty_t::bg
uint8_t bg
Definition
tty.h:80
tty_t::batch_depth
uint32_t batch_depth
Definition
tty.h:82
tty_t::bold
bool bold
Definition
tty.h:81
tty_t::backend
const tty_backend_t * backend
Definition
tty.h:73
tty_t::fg
uint8_t fg
Definition
tty.h:79
tty_init
void tty_init(tty_t *tty, const tty_backend_t *backend)
initialize a tty on a backend.
Definition
tty.c:296
tty_begin_batch
void tty_begin_batch(tty_t *tty)
start a batch of output.
Definition
tty.c:394
tty_write
void tty_write(tty_t *tty, const char *str)
feed a null-terminated string through tty_putchar(), as one batch.
Definition
tty.c:440
tty_end_batch
void tty_end_batch(tty_t *tty)
end a batch: redraw the caret where the cursor ended up and flush once.
Definition
tty.c:409
tty_set_mode
uint8_t tty_set_mode(tty_t *tty, uint8_t mode)
set the line-discipline mode (TTY_MODE_COOKED or TTY_MODE_RAW).
Definition
tty.c:450
tty_putchar
void tty_putchar(tty_t *tty, char c)
feed one output byte through the terminal state machine.
Definition
tty.c:432
tty_state_t
tty_state_t
Definition
tty.h:61
TTY_STATE_NORMAL
@ TTY_STATE_NORMAL
Definition
tty.h:62
TTY_STATE_ESC
@ TTY_STATE_ESC
Definition
tty.h:63
TTY_STATE_CSI_PRIV
@ TTY_STATE_CSI_PRIV
Definition
tty.h:65
TTY_STATE_CSI
@ TTY_STATE_CSI
Definition
tty.h:64
TTY_CSI_PARAMS_MAX
#define TTY_CSI_PARAMS_MAX
Definition
tty.h:21
src
kernel
tty
tty.h
Generated by
1.18.0