MonkOS  v0.1
A simple 64-bit operating system (x86_64)
tty.h
Go to the documentation of this file.
1 //============================================================================
2 /// @file tty.h
3 /// @brief Teletype (console) screen text manipulation routines.
4 //
5 // Copyright 2016 Brett Vickers.
6 // Use of this source code is governed by a BSD-style license that can
7 // be found in the MonkOS LICENSE file.
8 //============================================================================
9 
10 #pragma once
11 
12 #include <core.h>
13 #include <stdarg.h>
14 
15 //----------------------------------------------------------------------------
16 // Constants
17 //----------------------------------------------------------------------------
18 
19 /// The number of available virtual consoles.
20 #define MAX_TTYS 4
21 
22 //----------------------------------------------------------------------------
23 // @enum textcolor_t
24 /// @brief Color values used for tty text.
25 //----------------------------------------------------------------------------
27 {
44 };
45 
46 typedef enum textcolor textcolor_t;
47 
48 //----------------------------------------------------------------------------
49 // @struct screenpos_t
50 /// @brief tty screen text position.
51 //----------------------------------------------------------------------------
52 struct screenpos
53 {
54  uint8_t x; ///< x position in range [0:79].
55  uint8_t y; ///< y position in range [0:24].
56 };
57 
58 typedef struct screenpos screenpos_t;
59 
60 //----------------------------------------------------------------------------
61 // @function tty_init
62 /// @brief Initialize all virtual consoles.
63 /// @details This function must be called before any other console
64 /// functions can be used.
65 //----------------------------------------------------------------------------
66 void
67 tty_init();
68 
69 //----------------------------------------------------------------------------
70 // @function tty_activate
71 /// @brief Activate the requested virtual console.
72 /// @details The virtual console's buffer is immediately displayed on the
73 /// screen.
74 /// @param[in] id Virtual tty id (0-3).
75 //----------------------------------------------------------------------------
76 void
77 tty_activate(int id);
78 
79 //----------------------------------------------------------------------------
80 // @function tty_set_textcolor
81 /// @brief Set the foreground and background colors used to display
82 /// text on the virtual console.
83 /// @param[in] id Virtual tty id (0-3).
84 /// @param[in] fg Foreground color.
85 /// @param[in] bg Background color.
86 //----------------------------------------------------------------------------
87 void
89 
90 //----------------------------------------------------------------------------
91 // @function tty_set_textcolor_fg
92 /// @brief Set the foreground color used to display text on the virtual
93 /// console.
94 /// @param[in] id Virtual tty id (0-3).
95 /// @param[in] fg Foreground color.
96 //----------------------------------------------------------------------------
97 void
99 
100 //----------------------------------------------------------------------------
101 // @function tty_set_textcolor_bg
102 /// @brief Set the background color used to display text on the virtual
103 /// console.
104 /// @param[in] id Virtual tty id (0-3).
105 /// @param[in] bg Background color.
106 //----------------------------------------------------------------------------
107 void
109 
110 //----------------------------------------------------------------------------
111 // @function tty_get_textcolor_fg
112 /// @brief Get the foreground color used to display text on the virtual
113 /// console.
114 /// @param[in] id Virtual tty id (0-3).
115 /// @returns Foreground color.
116 //----------------------------------------------------------------------------
118 tty_get_textcolor_fg(int id);
119 
120 //----------------------------------------------------------------------------
121 // @function tty_get_textcolor_bg
122 /// @brief Get the background color used to display text on the virtual
123 /// console.
124 /// @param[in] id Virtual tty id (0-3).
125 /// @returns Background color.
126 //----------------------------------------------------------------------------
128 tty_get_textcolor_bg(int id);
129 
130 //----------------------------------------------------------------------------
131 // @function tty_clear
132 /// @brief Clear the virtual console screen's contents using the current
133 /// text background color.
134 /// @param[in] id Virtual tty id (0-3).
135 //----------------------------------------------------------------------------
136 void
137 tty_clear(int id);
138 
139 //----------------------------------------------------------------------------
140 // @function tty_setpos
141 /// @brief Set the position of the cursor on the virtual console.
142 /// @details Text written to the console after this function will be
143 /// located at the requested screen position.
144 /// @param[in] id Virtual tty id (0-3).
145 /// @param[in] pos The screen position of the cursor.
146 //----------------------------------------------------------------------------
147 void
148 tty_setpos(int id, screenpos_t pos);
149 
150 //----------------------------------------------------------------------------
151 // @function tty_getpos
152 /// @brief Get the current position of the cursor on the virtual console.
153 /// @param[in] id Virtual tty id (0-3).
154 /// @param[out] pos A pointer to a screenpos_t to receive the position.
155 //----------------------------------------------------------------------------
156 void
157 tty_getpos(int id, screenpos_t *pos);
158 
159 //----------------------------------------------------------------------------
160 // @function tty_print
161 /// @brief Output a null-terminated string to the virtual console using
162 /// the console's current text color and screen position.
163 ///
164 /// @details A newline character (\\n) causes the screen position to
165 /// be updated as though a carriage return and line feed were
166 /// performed.
167 ///
168 /// To change the foreground color on the fly without having to
169 /// call a console function, you may use the escape sequence
170 /// \033[x], where x is a single character representing the
171 /// foreground color to use for all following text. If x is a
172 /// hexadecimal digit, then it represents one of the 16 textcolor
173 /// codes. If x is '-', then it represents the console's
174 /// original foreground color setting.
175 ///
176 /// To change the background color on the fly, use the escape
177 /// sequence \033{x}. The meaning of x is the same as with
178 /// the foreground color escape sequence.
179 /// @param[in] id Virtual tty id (0-3).
180 /// @param[in] str The null-terminated string to be printed.
181 //----------------------------------------------------------------------------
182 void
183 tty_print(int id, const char *str);
184 
185 //----------------------------------------------------------------------------
186 // @function tty_printc
187 /// @brief Output a single character to the virtual console using
188 /// the console's current text color and screen position.
189 /// @details See tty_print for further details.
190 /// @param[in] id Virtual tty id (0-3).
191 /// @param[in] ch The character to be printed.
192 //----------------------------------------------------------------------------
193 void
194 tty_printc(int id, char ch);
195 
196 //----------------------------------------------------------------------------
197 // @function tty_printf
198 /// @brief Output a printf-formatted string to the virtual console using
199 /// the console's current text color and screen position.
200 /// @details See tty_print for further details.
201 /// @param[in] id Virtual tty id (0-3).
202 /// @param[in] format The null-terminated format string used to format the
203 /// text to be printed.
204 /// @param[in] ... Variable arguments list to be initialized with
205 /// va_start.
206 /// @returns The number of characters written to the console.
207 //----------------------------------------------------------------------------
208 int
209 tty_printf(int id, const char *format, ...);
void tty_set_textcolor_fg(int id, textcolor_t fg)
Set the foreground color used to display text on the virtual console.
Definition: tty.c:132
textcolor
Color values used for tty text.
Definition: tty.h:26
void tty_activate(int id)
Activate the requested virtual console.
Definition: tty.c:107
void tty_printc(int id, char ch)
Output a single character to the virtual console using the console&#39;s current text color and screen po...
Definition: tty.c:357
void tty_print(int id, const char *str)
Output a null-terminated string to the virtual console using the console&#39;s current text color and scr...
Definition: tty.c:343
void tty_getpos(int id, screenpos_t *pos)
Get the current position of the cursor on the virtual console.
Definition: tty.c:209
uint8_t y
y position in range [0:24].
Definition: tty.h:55
textcolor_t tty_get_textcolor_fg(int id)
Get the foreground color used to display text on the virtual console.
Definition: tty.c:154
Core include file.
enum textcolor textcolor_t
Definition: tty.h:46
void tty_set_textcolor_bg(int id, textcolor_t bg)
Set the background color used to display text on the virtual console.
Definition: tty.c:143
void tty_set_textcolor(int id, textcolor_t fg, textcolor_t bg)
Set the foreground and background colors used to display text on the virtual console.
Definition: tty.c:122
int tty_printf(int id, const char *format,...)
Output a printf-formatted string to the virtual console using the console&#39;s current text color and sc...
Definition: tty.c:372
void tty_clear(int id)
Clear the virtual console screen&#39;s contents using the current text background color.
Definition: tty.c:174
void tty_init()
Initialize all virtual consoles.
Definition: tty.c:89
void tty_setpos(int id, screenpos_t pos)
Set the position of the cursor on the virtual console.
Definition: tty.c:194
tty screen text position.
Definition: tty.h:52
uint8_t x
x position in range [0:79].
Definition: tty.h:54
textcolor_t tty_get_textcolor_bg(int id)
Get the background color used to display text on the virtual console.
Definition: tty.c:164