Use white instead of black for serial console text, add print macros

This commit is contained in:
csd4ni3l
2026-03-14 16:51:39 +01:00
parent 7a938283f7
commit 8701b83f57
2 changed files with 44 additions and 21 deletions

View File

@@ -2,9 +2,22 @@ use crate::driver::graphics::framebuffer::{with_framebuffer, Framebuffer};
use crate::driver::graphics::font_render::render_text; use crate::driver::graphics::font_render::render_text;
use crate::driver::graphics::base::rgb; use crate::driver::graphics::base::rgb;
use spin::Mutex; use spin::Mutex;
use core::fmt::{self, Write};
const DEFAULT_FONT_SIZE: usize = 3; const DEFAULT_FONT_SIZE: usize = 3;
pub struct ConsoleWriter<'a> {
pub fb: &'a mut Framebuffer,
pub console: &'a mut SerialConsole,
}
impl Write for ConsoleWriter<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.console.render_text(self.fb, s);
Ok(())
}
}
pub struct SerialConsole { pub struct SerialConsole {
start_x: usize, start_x: usize,
current_y: usize current_y: usize
@@ -19,8 +32,8 @@ impl SerialConsole {
} }
pub fn render_text(&mut self, fb: &mut Framebuffer, text: &str) { pub fn render_text(&mut self, fb: &mut Framebuffer, text: &str) {
self.current_y = render_text(fb, self.start_x - ((text.len() - text.matches('\n').count()) * DEFAULT_FONT_SIZE * 4), self.current_y, text, DEFAULT_FONT_SIZE, rgb(0, 0, 0)); self.current_y = render_text(fb, self.start_x - ((text.len() - text.matches('\n').count()) * DEFAULT_FONT_SIZE * 4), self.current_y, text, DEFAULT_FONT_SIZE, rgb(255, 255, 255));
self.current_y = render_text(fb, self.start_x - ((text.len() - text.matches('\n').count()) * DEFAULT_FONT_SIZE * 4), self.current_y, "\n", DEFAULT_FONT_SIZE, rgb(0, 0, 0)); // add a newline self.current_y = render_text(fb, self.start_x - ((text.len() - text.matches('\n').count()) * DEFAULT_FONT_SIZE * 4), self.current_y, "\n", DEFAULT_FONT_SIZE, rgb(255, 255, 255)); // add a newline
} }
pub fn clear(&mut self, start_y: usize) { pub fn clear(&mut self, start_y: usize) {

View File

@@ -2,6 +2,7 @@
#![no_main] #![no_main]
use core::arch::asm; use core::arch::asm;
use core::fmt::Write;
use limine::BaseRevision; use limine::BaseRevision;
use limine::request::{FramebufferRequest, RequestsEndMarker, RequestsStartMarker}; use limine::request::{FramebufferRequest, RequestsEndMarker, RequestsStartMarker};
@@ -10,7 +11,7 @@ pub mod driver;
pub mod arch; pub mod arch;
use spin::Mutex; use spin::Mutex;
use crate::arch::serial::{SerialConsole, init_serial_console, with_serial_console}; use crate::arch::serial::{ConsoleWriter, SerialConsole, init_serial_console, with_serial_console};
use crate::driver::graphics::font_render::render_text; use crate::driver::graphics::font_render::render_text;
use crate::driver::graphics::primitives::{circle_filled, circle_outline, rectangle_filled, rectangle_outline, triangle_outline}; use crate::driver::graphics::primitives::{circle_filled, circle_outline, rectangle_filled, rectangle_outline, triangle_outline};
use crate::driver::graphics::base::rgb; use crate::driver::graphics::base::rgb;
@@ -36,26 +37,32 @@ static _START_MARKER: RequestsStartMarker = RequestsStartMarker::new();
#[unsafe(link_section = ".requests_end_marker")] #[unsafe(link_section = ".requests_end_marker")]
static _END_MARKER: RequestsEndMarker = RequestsEndMarker::new(); static _END_MARKER: RequestsEndMarker = RequestsEndMarker::new();
// #[macro_export] #[macro_export]
// macro_rules! print { macro_rules! print {
// ($($arg:tt)*) => (with_framebuffer(|mut fb|{ ($($arg:tt)*) => {
// with_serial_console(|serial_console| { $crate::_print(core::format_args!($($arg)*))
// serial_console.render_text(&mut fb, format_args!($($arg)*)); };
// }); }
// }));
// }
// #[macro_export] #[macro_export]
// macro_rules! println { macro_rules! println {
// () => ($crate::print!("\n")); () => {
// ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*))); $crate::_print(core::format_args!("\n"))
// } };
($($arg:tt)*) => {
$crate::_print(core::format_args!("{}\n", core::format_args!($($arg)*)))
};
}
// #[doc(hidden)] #[doc(hidden)]
// pub fn _print(args: fmt::Arguments) { pub fn _print(args: core::fmt::Arguments) {
// use core::fmt::Write; with_framebuffer(|fb| {
// WRITER.lock().write_fmt(args).unwrap(); with_serial_console(|console| {
// } let mut writer = ConsoleWriter { fb, console };
let _ = writer.write_fmt(args);
});
});
}
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
unsafe extern "C" fn kmain() -> ! { unsafe extern "C" fn kmain() -> ! {
@@ -95,6 +102,9 @@ fn rust_panic(_info: &core::panic::PanicInfo) -> ! {
serial_console.render_text(&mut fb, "Message:"); serial_console.render_text(&mut fb, "Message:");
serial_console.render_text(&mut fb, message); serial_console.render_text(&mut fb, message);
} }
crate::println!("Kernel Panic! :C");
crate::print!("Message: ");
crate::println!("{}", _info);
}); });
}); });