The manual
ginnOS
The reference: libc, the libraries and the kernel
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 */
39typedef 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
58
59// ANSI escape parser states.
60typedef 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)
67
68/**
69 * a terminal instance.
70 */
71typedef 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 */
90void 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 */
99void 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 */
106void 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 */
115void 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 */
121void 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 */
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
character-cell backend a tty draws onto.
Definition tty.h:40
void(*) clear(void)
Definition tty.h:43
void(*) draw_cursor(uint8_t row, uint8_t col, bool visible)
Definition tty.h:51
void(*) set_cursor(uint8_t row, uint8_t col)
Definition tty.h:44
uint8_t rows
Definition tty.h:55
uint8_t cols
Definition tty.h:56
void(*) set_colors(uint8_t fg, uint8_t bg)
Definition tty.h:48
void(*) scroll(void)
Definition tty.h:42
void(*) put_at(char c, uint8_t row, uint8_t col)
Definition tty.h:41
void(*) flush(void)
Definition tty.h:54
void(*) get_cursor(uint8_t *row, uint8_t *col)
Definition tty.h:45
a terminal instance.
Definition tty.h:72
uint8_t mode
Definition tty.h:78
int csi_param_count
Definition tty.h:76
int csi_current_param
Definition tty.h:77
int csi_params[TTY_CSI_PARAMS_MAX]
Definition tty.h:75
tty_state_t state
Definition tty.h:74
uint8_t bg
Definition tty.h:80
uint32_t batch_depth
Definition tty.h:82
bool bold
Definition tty.h:81
const tty_backend_t * backend
Definition tty.h:73
uint8_t fg
Definition tty.h:79
void tty_init(tty_t *tty, const tty_backend_t *backend)
initialize a tty on a backend.
Definition tty.c:296
void tty_begin_batch(tty_t *tty)
start a batch of output.
Definition tty.c:394
void tty_write(tty_t *tty, const char *str)
feed a null-terminated string through tty_putchar(), as one batch.
Definition tty.c:440
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
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
void tty_putchar(tty_t *tty, char c)
feed one output byte through the terminal state machine.
Definition tty.c:432
tty_state_t
Definition tty.h:61
@ TTY_STATE_NORMAL
Definition tty.h:62
@ TTY_STATE_ESC
Definition tty.h:63
@ TTY_STATE_CSI_PRIV
Definition tty.h:65
@ TTY_STATE_CSI
Definition tty.h:64
#define TTY_CSI_PARAMS_MAX
Definition tty.h:21