PatchworkOS
19e446b
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
terminal.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
ansi.h
"
4
5
#include <
patchwork/patchwork.h
>
6
#include <
stdint.h
>
7
#include <
sys/fs.h
>
8
#include <
sys/kbd.h
>
9
#include <
threads.h
>
10
11
/**
12
* @brief Terminal Program.
13
* @defgroup programs_terminal Terminal
14
* @ingroup programs
15
*
16
* A simple terminal emulator program.
17
*
18
* The terminal always acts in raw mode, meaning that it does not process any input itself, instead it just sends all
19
* input directly to the shell program running inside it.
20
*
21
* @see [Terminals OSDev Wiki](https://wiki.osdev.org/Terminals)
22
* @see [ANSI Escape Codes](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797)
23
*
24
* @{
25
*/
26
27
/**
28
* @brief Terminal blink rate.
29
*/
30
#define TERMINAL_BLINK_INTERVAL (CLOCKS_PER_SEC / 2)
31
32
/**
33
* @brief Terminal columns.
34
*/
35
#define TERMINAL_COLUMNS 80
36
37
/**
38
* @brief Terminal rows.
39
*/
40
#define TERMINAL_ROWS 30
41
42
/**
43
* @brief Maximum size of the buffer used to batch data.
44
*/
45
#define TERMINAL_MAX_DATA (TERMINAL_COLUMNS * TERMINAL_ROWS)
46
47
/**
48
* @brief Maximum terminal frames per second.
49
*
50
* The terminal will batch any data received within a "frame" and only actually flush the data at the end of the frame.
51
*/
52
#define TERMINAL_MAX_FPS 30
53
54
/**
55
* @brief Terminal flags.
56
* @enum terminal_flags_t
57
*
58
* Used for the ANSI state machine and character attributes.
59
*/
60
typedef
enum
61
{
62
TERMINAL_NONE
= 0,
63
TERMINAL_BOLD
= (1 << 0),
64
TERMINAL_DIM
= (1 << 1),
65
TERMINAL_ITALIC
= (1 << 2),
66
TERMINAL_UNDERLINE
= (1 << 3),
67
TERMINAL_BLINK
= (1 << 4),
68
TERMINAL_INVERSE
= (1 << 5),
69
TERMINAL_HIDDEN
= (1 << 6),
70
TERMINAL_STRIKETHROUGH
= (1 << 7)
71
}
terminal_flags_t
;
72
73
/**
74
* @brief Terminal character.
75
* @struct terminal_char_t
76
*/
77
typedef
struct
78
{
79
char
chr
;
80
pixel_t
foreground
;
81
pixel_t
background
;
82
terminal_flags_t
flags
;
83
uint16_t
col
;
84
uint16_t
physicalRow
;
85
}
terminal_char_t
;
86
87
/**
88
* @brief Terminal structure.
89
* @struct terminal_t
90
*/
91
typedef
struct
terminal
92
{
93
window_t
*
win
;
94
font_t
*
font
;
95
bool
cursorBlink
;
96
bool
isCursorVisible
;
97
fd_t
stdin
[2];
98
fd_t
stdout
[2];
// Also does stderr
99
pixel_t
foreground
;
100
pixel_t
background
;
101
terminal_flags_t
flags
;
102
ansi_sending_t
ansi
;
103
terminal_char_t
screen[
TERMINAL_ROWS
][
TERMINAL_COLUMNS
];
104
uint64_t
firstRow
;
// For scrolling
105
terminal_char_t
*
savedCursor
;
106
terminal_char_t
*
prevCursor
;
107
terminal_char_t
*
cursor
;
108
pid_t
shell
;
109
}
terminal_t
;
110
111
/**
112
* @brief Terminal initialization context.
113
* @struct terminal_init_ctx_t
114
*
115
* Used while creating the window to pass in the font to use.
116
*/
117
typedef
struct
118
{
119
font_t
*
font
;
120
}
terminal_init_ctx_t
;
121
122
/**
123
* @brief Create a new terminal window.
124
*
125
* @param disp The display to create the window on.
126
* @return On success, the terminal window. On failure, `NULL` and `errno` is set.
127
*/
128
window_t
*
terminal_new
(
display_t
* disp);
129
130
/**
131
* @brief Terminal main loop.
132
*
133
* @param win The terminal window.
134
*/
135
void
terminal_loop
(
window_t
* win);
136
137
/** @} */
fd_t
__UINT64_TYPE__ fd_t
File descriptor type.
Definition
fd_t.h:10
pid_t
__UINT64_TYPE__ pid_t
Process Identifier.
Definition
pid_t.h:11
terminal_loop
void terminal_loop(window_t *win)
Terminal main loop.
Definition
terminal.c:734
terminal_flags_t
terminal_flags_t
Terminal flags.
Definition
terminal.h:61
TERMINAL_COLUMNS
#define TERMINAL_COLUMNS
Terminal columns.
Definition
terminal.h:35
terminal_new
window_t * terminal_new(display_t *disp)
Create a new terminal window.
Definition
terminal.c:706
TERMINAL_ROWS
#define TERMINAL_ROWS
Terminal rows.
Definition
terminal.h:40
TERMINAL_DIM
@ TERMINAL_DIM
Definition
terminal.h:64
TERMINAL_STRIKETHROUGH
@ TERMINAL_STRIKETHROUGH
Definition
terminal.h:70
TERMINAL_BLINK
@ TERMINAL_BLINK
Definition
terminal.h:67
TERMINAL_ITALIC
@ TERMINAL_ITALIC
Definition
terminal.h:65
TERMINAL_NONE
@ TERMINAL_NONE
Definition
terminal.h:62
TERMINAL_UNDERLINE
@ TERMINAL_UNDERLINE
Definition
terminal.h:66
TERMINAL_HIDDEN
@ TERMINAL_HIDDEN
Definition
terminal.h:69
TERMINAL_INVERSE
@ TERMINAL_INVERSE
Definition
terminal.h:68
TERMINAL_BOLD
@ TERMINAL_BOLD
Definition
terminal.h:63
fs.h
kbd.h
patchwork.h
pixel_t
uint32_t pixel_t
Definition
pixel.h:11
ansi.h
stdint.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
uint16_t
__UINT16_TYPE__ uint16_t
Definition
stdint.h:13
stdout
FILE * stdout
Definition
std_streams.c:18
stdin
FILE * stdin
Definition
std_streams.c:17
ansi_sending_t
ANSI sending structure.
Definition
ansi.h:140
display_t
Opaque display structure.
Definition
internal.h:68
font_t
Definition
internal.h:16
terminal_char_t
Terminal character.
Definition
terminal.h:78
terminal_char_t::chr
char chr
Definition
terminal.h:79
terminal_char_t::foreground
pixel_t foreground
Definition
terminal.h:80
terminal_char_t::background
pixel_t background
Definition
terminal.h:81
terminal_char_t::col
uint16_t col
Definition
terminal.h:83
terminal_char_t::physicalRow
uint16_t physicalRow
Definition
terminal.h:84
terminal_char_t::flags
terminal_flags_t flags
Definition
terminal.h:82
terminal_init_ctx_t
Terminal initialization context.
Definition
terminal.h:118
terminal_init_ctx_t::font
font_t * font
Definition
terminal.h:119
terminal_t
Terminal structure.
Definition
terminal.h:92
terminal_t::background
pixel_t background
Definition
terminal.h:100
terminal_t::savedCursor
terminal_char_t * savedCursor
Definition
terminal.h:105
terminal_t::prevCursor
terminal_char_t * prevCursor
Definition
terminal.h:106
terminal_t::flags
terminal_flags_t flags
Definition
terminal.h:101
terminal_t::font
font_t * font
Definition
terminal.h:94
terminal_t::win
window_t * win
Definition
terminal.h:93
terminal_t::shell
pid_t shell
Definition
terminal.h:108
terminal_t::isCursorVisible
bool isCursorVisible
Definition
terminal.h:96
terminal_t::foreground
pixel_t foreground
Definition
terminal.h:99
terminal_t::cursorBlink
bool cursorBlink
Definition
terminal.h:95
terminal_t::firstRow
uint64_t firstRow
Definition
terminal.h:104
terminal_t::ansi
ansi_sending_t ansi
Definition
terminal.h:102
terminal_t::cursor
terminal_char_t * cursor
Definition
terminal.h:107
window_t
Opaque window structure.
Definition
internal.h:44
threads.h
src
boxes
apps
terminal
terminal.h
Generated on Sat Jan 24 2026 10:59:24 for PatchworkOS by
1.9.8