LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
console.c
Go to the documentation of this file.
1
9#include <limine/limine.h>
10#include <stddef.h>
11#include <stdbool.h>
12#include <lib/kstring.h>
13#include <lib/color.h>
14#include <kernel/tests.h>
15#include <drivers/console.h>
16
17int font_type;
18
21
22bool console_enabled = false;
24{
25 return console_enabled;
26}
27
28void scroll_up(void) {
29 uint8_t height; // Hold character height
30 uint8_t width = 8; // Hold character width
31
32 // Get character height and width based on font header
33 if (font_type == FONT_PSF1) {
34 height = main_console.font_psf1_info.charsize;
35 } else {
36 height = main_console.font_psf2_info.height;
37 width = main_console.font_psf2_info.width;
38 }
39
40 // Move pixels from one row to the previous
41 for (uint32_t y = 0; y < main_console.fb->height - height; y++) {
42 const uint8_t* src = (uint8_t*)main_console.fb->address + ((y + height) * main_console.fb->pitch);
43 uint8_t* dst = (uint8_t*)main_console.fb->address + (y * main_console.fb->pitch);
44
45 memmove(dst, src, main_console.fb->pitch);
46 }
47
48 uint32_t pixel = ((uint32_t)main_console.bg_color.r << main_console.fb->red_mask_shift) |
49 ((uint32_t)main_console.bg_color.g << main_console.fb->green_mask_shift) |
50 ((uint32_t)main_console.bg_color.b << main_console.fb->blue_mask_shift);
51
52 size_t pixel_count = height * width * main_console.cols;
53
54 uint32_t *pixel_addr = (uint32_t *)(
55 (uint8_t *)main_console.fb->address +
56 ((main_console.rows - 1) * height * main_console.fb->pitch)
57 );
58
59 for (size_t y = 0; y < height; y++) {
60 uint32_t *dest = (uint32_t *)(
61 (uint8_t *)pixel_addr + y * main_console.fb->pitch
62 );
63
64 fill32(dest, pixel, width * main_console.cols);
65 }
66}
67
68void console_init(void) { // Initialize all the values in the main console
69 if (console_enabled) return;
70
71 main_console.bg_color = COLOR_BLACK;
72 main_console.fg_color = COLOR_WHITE_BRIGHT;
73
74 main_console.original_bg_color = main_console.bg_color;
75 main_console.original_fg_color = main_console.fg_color;
76
77 if (font_type == FONT_PSF1) {
78 main_console.font_psf1_info.magic[0] = *(_binary_font_psf_start);
79 main_console.font_psf1_info.magic[1] = *(_binary_font_psf_start + 1);
80 main_console.font_psf1_info.mode = *(_binary_font_psf_start + 2);
81 main_console.font_psf1_info.charsize = *(_binary_font_psf_start + 3);
82
83 main_console.cols = main_console.fb->width / 8;
84 main_console.rows = main_console.fb->height / main_console.font_psf1_info.charsize;
85 } else {
86 main_console.font_psf2_info.magic = *(_binary_font_psf_start);
87 main_console.font_psf2_info.version = *(_binary_font_psf_start + 4);
88 main_console.font_psf2_info.headersize = *(_binary_font_psf_start + 8);
89 main_console.font_psf2_info.flags = *(_binary_font_psf_start + 12);
90 main_console.font_psf2_info.length = *(_binary_font_psf_start + 16);
91 main_console.font_psf2_info.charsize = *(_binary_font_psf_start + 20);
92 main_console.font_psf2_info.height = *(_binary_font_psf_start + 24);
93 main_console.font_psf2_info.width = *(_binary_font_psf_start + 28);
94
95 main_console.cols = main_console.fb->width / main_console.font_psf2_info.width;
96 main_console.rows = main_console.fb->height / main_console.font_psf2_info.height;
97 }
98
99 console_enabled = true;
100}
101
102void set_bg_color (struct rgb color) {main_console.bg_color = color;}
103void set_fg_color (struct rgb color) {main_console.fg_color = color;}
104
105struct rgb get_bg_color (void) {return main_console.bg_color;}
106struct rgb get_fg_color (void) {return main_console.fg_color;}
107
108struct rgb get_default_bg_color (void) {return main_console.original_bg_color;}
109struct rgb get_default_fg_color (void) {return main_console.original_fg_color;}
110
111void draw_char(const char c, const uint8_t col, const uint8_t row) {
112 uint64_t glyph; // Store the address of the glyph
113 uint8_t height; // store the height of a glyph
114 uint8_t width; // store the width of a glyph
115
116 if (font_type == FONT_PSF1) { // Calculate for the psf1 font
117 glyph = (uint64_t) _binary_font_psf_start + 4 + c * main_console.font_psf1_info.charsize; // calculate glyph addr
118 width = 8; // glyph width = 8
119 height = main_console.font_psf1_info.charsize; // glyph height = charsize
120 } else { // Calculate for the psf2 font
121 glyph = (uint64_t) _binary_font_psf_start + 32 + c * main_console.font_psf2_info.charsize; // calculate glyph addr
122 width = main_console.font_psf2_info.width; // glyph width = width
123 height = main_console.font_psf2_info.height;// glyph height = height
124 }
125
126 const int px = col * width; // the starting horizontal pixel of the glyph
127 const int py = row * height; // the starting vertical pixel of the glyph
128
129 const uint8_t bytes_per_row = (width + 7) / 8; // Get the amount of bytes per row in the glyph: (glyph width + 7) / 8 (one byte)
130
131 for (int y = 0; y < height; y++) { // For each row in the glyph...
132 const uint8_t *glyph_row = (uint8_t *)(glyph + y * bytes_per_row); // Get all the bits in the row as bytes: base glyph address + (the current row * bytes per row)
133
134 for (int x = 0; x < width; x++) { // For each pixel in the row (each column)...
135 const uint8_t row_bits = glyph_row[x / 8]; // Get the bit (stored as a whole byte)
136 const uint8_t bit = (row_bits >> (7 - (x % 8))) & 1; // Move the useful bit to the right and isolate it with '& 1'
137
138 const unsigned int pixel_x = px + x; // Get the position of the pixel
139 const unsigned int pixel_y = py + y;
140
141 // Check if its within screen bounds
142 if (pixel_x >= main_console.fb->width || pixel_y >= main_console.fb->height) {
143 return;
144 }
145
146 uint32_t *pixel_addr = (uint32_t *)( // Get the address of the pixel
147 (uint8_t *)main_console.fb->address + (pixel_y * main_console.fb->pitch) + (pixel_x * 4)
148 );
149
150 uint32_t pixel; // Store the pixel color info
151
152 if (bit == 1) {
153 // Draw foreground if the bit is set
154 pixel =
155 ((uint32_t)main_console.fg_color.r << main_console.fb->red_mask_shift) |
156 ((uint32_t)main_console.fg_color.g << main_console.fb->green_mask_shift) |
157 ((uint32_t)main_console.fg_color.b << main_console.fb->blue_mask_shift);
158 } else {
159 // Draw background if the bit is clear
160 pixel =
161 ((uint32_t)main_console.bg_color.r << main_console.fb->red_mask_shift) |
162 ((uint32_t)main_console.bg_color.g << main_console.fb->green_mask_shift) |
163 ((uint32_t)main_console.bg_color.b << main_console.fb->blue_mask_shift);
164 }
165
166 *pixel_addr = pixel; // Set the pixel address value to the pixel color info
167 }
168 }
169
170}
171
172void fill32(uint32_t *dest, uint32_t value, size_t count) {
173 for (size_t i = 0; i < count; i++) {
174 dest[i] = value;
175 }
176}
177
178void console_putc(char c) {
179
180 if (c == '\r') // If its a \r, move to the start of the same line
181 main_console.cursor_col = 0;
182
183 if (c == '\t') // If its a \t, indent by 4 spaces (tab)
184 main_console.cursor_col += 4;
185
186 if (c == '\b') { // If its a backspace character
187 if (main_console.cursor_col == 0) { // If its already in the first column, go to the end of the previous line
188 main_console.cursor_col = main_console.cols - 1;
189 main_console.cursor_row--;
190
191 } else // else go to the previous column
192 main_console.cursor_col--;
193 }
194
195 if (main_console.cursor_col >= main_console.cols || c == '\n') { // If it reached the end of the line or its a newline character
196 main_console.cursor_row++;
197 main_console.cursor_col = 0;
198 }
199
200 if (main_console.cursor_row >= main_console.rows) { // If it reached past the last row
201 main_console.cursor_row = main_console.rows - 1; // Go back a row
202 main_console.cursor_col = 0; // Go to the first column
203 scroll_up(); // scroll up the screen
204 }
205
206 if (c == '\n' || // If its a non readable character, dont print it
207 c == '\t' ||
208 c == '\b' ||
209 c == '\r') {
210
211 return;
212 }
213
214 draw_char(c, main_console.cursor_col, main_console.cursor_row); // Print the current character
215
216 main_console.cursor_col++; // Move cursor
217}
218
219void console_puts(char* str) {
220 // Holds current char
221
222 uint64_t index = 0; // string char index
223
224 while (true) { // for each character...
225 char current = str[index]; // store the current char
226
227 if (current == '\0') break; // If its the end of the string, break
228
229 console_putc(current); // Print the current character
230
231 index++; // increase index and column
232 }
233}
234
235void console_clear(void) {
236 for (int r = 0; r < main_console.rows; r++) {
237 for (int c = 0; c < main_console.cols; c++) {
238 draw_char(' ', c, r); // Display a blank character at each column and row
239 }
240 }
241
242 main_console.cursor_col = 0;
243 main_console.cursor_row = 0;
244}
245
246void console_print_int(uint64_t value) {
247 uint64_t next = value; // Holds the next value to calculate
248 uint64_t index = 0; // Holds how many characters have been calculated
249
250 // Get number length
251 while (next != 0) { // Loop until the next value is 0, so the number is done
252 next /= 10; // move the decimal point over by one to the left
253 index++; // continue, increase length counter
254 }
255
256 // If the value started as 0, set the length to 1
257 if (value == 0)
258 index = 1;
259
260 const uint64_t MAX_LENGTH = index; // Hold the length of the number
261 char chars[MAX_LENGTH]; // Hold the number (backwards) as chars
262
263 next = value; // Reset next value
264 index = 0; // Reset the index
265
266 while (next != 0) {
267 if (index >= MAX_LENGTH) // Check if the index passed the length
268 {
269 break; // Stop the loop if so
270 }
271 chars[index] = ((next % 10) + '0'); // add the remainder of next/10, + '0' to turn it into the corresponding char to the chars array
272
273 next /= 10; // move the decimal, using lossy division
274 index++;
275 }
276
277 if (value == 0) { // If the number started as one
278 chars[0] = '0'; // set the character in chars to 0
279 index = 1; // set index to 1
280 }
281
282 char str[MAX_LENGTH + 1]; // Hold the chars as a string ready to print + space for a null terminator
283
284 for (int i = index - 1; i >= 0; i--) { // For each char in chars
285 str[MAX_LENGTH - i - 1] = chars[i]; // Add the char to the end of the string, but leaving space for the null terminator
286 }
287
288 str[MAX_LENGTH] = '\0'; // Add the null terminator
289
290 console_puts(str); // Print the number
291}
Color utilities.
void fill32(uint32_t *dest, uint32_t value, size_t count)
Fills 32 bytes count times starting at dest with value.
Definition console.c:172
void set_bg_color(struct rgb color)
Sets the background colour.
Definition console.c:102
void console_putc(char c)
Arranges the cursor and prints characters.
Definition console.c:178
struct rgb get_default_bg_color(void)
Gets the default bg colour.
Definition console.c:108
void scroll_up(void)
Scrolls the whole console up.
Definition console.c:28
bool get_console_status(void)
Returns the status of the console (initialized or uninitialized).
Definition console.c:23
void console_puts(char *str)
Prints a non-formatted string.
Definition console.c:219
struct rgb get_fg_color(void)
Gets the current fg colour.
Definition console.c:106
struct rgb get_bg_color(void)
Gets the current bg colour.
Definition console.c:105
void console_clear(void)
Clears the console (fills with bg color).
Definition console.c:235
struct rgb get_default_fg_color(void)
Gets the default fg colour.
Definition console.c:109
void set_fg_color(struct rgb color)
Sets the foreground colour.
Definition console.c:103
void console_print_int(uint64_t value)
Prints an int to the console.
Definition console.c:246
void console_init(void)
Initializes the console struct and fills all the properties.
Definition console.c:68
struct console main_console
The main console info.
Definition console.c:20
void draw_char(const char c, const uint8_t col, const uint8_t row)
Draws a character to the console.
Definition console.c:111
Console utilities and configuration.
Utilities for memory and string operations.
void * memmove(void *dest, const void *src, size_t n)
Copies memory from one address to the other with possible overlap.
Definition kstring.c:26
Console structure, holds all the information about the console.
Definition console.h:41
RGB color values.
Definition color.h:15
Tools for testing different parts of the kernel.