增加内核日志功能 #3

Merged
pointer-to-bios merged 17 commits from pointer-to-bios/metaverse-dev:main into main 2024-02-05 22:10:26 +08:00
5 changed files with 110 additions and 52 deletions
Showing only changes of commit 69b6d55d4d - Show all commits

View File

@ -12,7 +12,7 @@ typedef enum __tty_type
// 用于图形功能初始化后,直接连接图形接口 // 用于图形功能初始化后,直接连接图形接口
tty_type_display = 2, tty_type_display = 2,
// 用于图形终端的终端模拟器 // 用于图形终端的终端模拟器
tty_type_vtty = 3, tty_type_vterm = 3,
} tty_type; } tty_type;
typedef enum __framebuffer_pixel_type typedef enum __framebuffer_pixel_type
@ -58,6 +58,7 @@ typedef struct __tty
tty_type type; tty_type type;
tty_typeinfo typeinfo; tty_typeinfo typeinfo;
tty_mode mode; tty_mode mode;
bool enabled;
tty_text_state text; tty_text_state text;
} tty; } tty;
@ -67,6 +68,7 @@ typedef struct __tty_controller_t
#define TTY_MAX_NUM 128 #define TTY_MAX_NUM 128
tty *ttys[TTY_MAX_NUM]; tty *ttys[TTY_MAX_NUM];
bool map[TTY_MAX_NUM]; bool map[TTY_MAX_NUM];
tty *enabled[TTY_MAX_NUM];
} tty_controller_t; } tty_controller_t;
/** /**
@ -118,6 +120,19 @@ void tty_text_print(tty *ttyx, char *string, u32 color, u32 bgcolor);
#define gen_color(r, g, b) (((r) << 16) | ((g) << 8) | (b)) #define gen_color(r, g, b) (((r) << 16) | ((g) << 8) | (b))
/**
* @brief tty
*
* @param ttyx
* @return true
* @return false
* raw_framebuffer类型的ttyframebuffer已被其它raw_framebuffer
* tty占用
*/
bool tty_enable(tty *ttyx);
void tty_disable(tty *ttyx);
#define TTY_FONT_SCALE 2 #define TTY_FONT_SCALE 2
typedef struct __tty_font_t typedef struct __tty_font_t

View File

@ -1,12 +1,9 @@
use std::panic;
use crate::kernel::tty::tty::{Color, MessageBuilder, Tty}; use crate::kernel::tty::tty::{Color, MessageBuilder, Tty};
#[no_mangle] #[no_mangle]
extern "C" fn kmain_rust() { extern "C" fn kmain_rust() {
let tty = Tty::from_id(0).unwrap(); let tty = Tty::from_id(0).unwrap();
// let ttyptr = unsafe { tty.get_c_tty_t() }; tty.enable();
let hello = MessageBuilder::new() let hello = MessageBuilder::new()
.message("Hello, ".to_string()) .message("Hello, ".to_string())
.message("Metaverse".to_string()) .message("Metaverse".to_string())

View File

@ -11,26 +11,27 @@ tty_controller_t *tty_controller_new()
{ {
memset(tty_ctrler.ttys, 0, sizeof(tty_ctrler.ttys)); memset(tty_ctrler.ttys, 0, sizeof(tty_ctrler.ttys));
memset(tty_ctrler.map, 0, sizeof(tty_ctrler.map)); memset(tty_ctrler.map, 0, sizeof(tty_ctrler.map));
memset(tty_ctrler.enabled, 0, sizeof(tty_ctrler.enabled));
return &tty_ctrler; return &tty_ctrler;
} }
tty *tty_new(tty_type type, tty_mode mode) tty *tty_new(tty_type type, tty_mode mode)
{ {
tty *__tty = memm_allocate(sizeof(tty), 0);
memset(__tty, 0, sizeof(tty));
__tty->type = type;
__tty->mode = mode;
tty *res = nullptr; tty *res = nullptr;
for (usize i = 0; i < TTY_MAX_NUM; ++i) for (usize i = 0; i < TTY_MAX_NUM; ++i)
{ {
if (tty_ctrler.map[i] == false) if (tty_ctrler.map[i] == false)
{ {
res = __tty; res = memm_kernel_allocate(sizeof(tty));
__tty->id = i; res->id = i;
tty_ctrler.ttys[i] = __tty; tty_ctrler.ttys[i] = res;
tty_ctrler.map[i] = true; tty_ctrler.map[i] = true;
break;
} }
} }
res->type = type;
res->mode = mode;
res->enabled = false;
return res; return res;
} }
@ -55,6 +56,8 @@ void tty_set_framebuffer(tty *ttyx, framebuffer *fb)
{ {
ttyx->text.width = fb->width / tty_get_font()->char_width; ttyx->text.width = fb->width / tty_get_font()->char_width;
ttyx->text.height = fb->height / tty_get_font()->char_height; ttyx->text.height = fb->height / tty_get_font()->char_height;
ttyx->text.line = 0;
ttyx->text.column = 0;
} }
} }
@ -87,7 +90,8 @@ inline static void putchar(
{ {
for (usize b = 0; b < TTY_FONT_SCALE; ++b) for (usize b = 0; b < TTY_FONT_SCALE; ++b)
{ {
put_pixel(ttyx->typeinfo.raw_framebuffer.pointer, put_pixel(
ttyx->typeinfo.raw_framebuffer.pointer,
ttyx->typeinfo.raw_framebuffer.width, ttyx->typeinfo.raw_framebuffer.width,
ttyx->typeinfo.raw_framebuffer.pixsize, ttyx->typeinfo.raw_framebuffer.pixsize,
ttyx->text.column * font->char_width * TTY_FONT_SCALE + i * TTY_FONT_SCALE + a, ttyx->text.column * font->char_width * TTY_FONT_SCALE + i * TTY_FONT_SCALE + a,
@ -102,7 +106,8 @@ inline static void putchar(
{ {
for (usize b = 0; b < TTY_FONT_SCALE; ++b) for (usize b = 0; b < TTY_FONT_SCALE; ++b)
{ {
put_pixel(ttyx->typeinfo.raw_framebuffer.pointer, put_pixel(
ttyx->typeinfo.raw_framebuffer.pointer,
ttyx->typeinfo.raw_framebuffer.width, ttyx->typeinfo.raw_framebuffer.width,
ttyx->typeinfo.raw_framebuffer.pixsize, ttyx->typeinfo.raw_framebuffer.pixsize,
ttyx->text.column * font->char_width * TTY_FONT_SCALE + i * TTY_FONT_SCALE + a, ttyx->text.column * font->char_width * TTY_FONT_SCALE + i * TTY_FONT_SCALE + a,
@ -121,7 +126,8 @@ inline static void newline(tty *ttyx)
ttyx->text.line++; ttyx->text.line++;
if (ttyx->text.line == ttyx->text.height) if (ttyx->text.line == ttyx->text.height)
{ {
scroll_buffer(ttyx->typeinfo.raw_framebuffer.pointer, scroll_buffer(
ttyx->typeinfo.raw_framebuffer.pointer,
ttyx->typeinfo.raw_framebuffer.width, ttyx->typeinfo.raw_framebuffer.width,
ttyx->typeinfo.raw_framebuffer.height, ttyx->typeinfo.raw_framebuffer.height,
ttyx->typeinfo.raw_framebuffer.pixsize, ttyx->typeinfo.raw_framebuffer.pixsize,
@ -132,6 +138,8 @@ inline static void newline(tty *ttyx)
void tty_text_print(tty *ttyx, char *string, u32 color, u32 bgcolor) void tty_text_print(tty *ttyx, char *string, u32 color, u32 bgcolor)
{ {
if (ttyx->enabled == false)
return;
if (ttyx->mode != tty_mode_text) if (ttyx->mode != tty_mode_text)
return; return;
if (ttyx->typeinfo.raw_framebuffer.pixtype == bgr) if (ttyx->typeinfo.raw_framebuffer.pixtype == bgr)
@ -147,8 +155,8 @@ void tty_text_print(tty *ttyx, char *string, u32 color, u32 bgcolor)
} }
} }
tty_font_t *font = tty_get_font(); tty_font_t *font = tty_get_font();
simple_lock_lock(ttyx->text.lock);
usize len = strlen(string); usize len = strlen(string);
simple_lock_lock(ttyx->text.lock);
for (char *str = string; string - str < len; string++) for (char *str = string; string - str < len; string++)
{ {
char c = *string; char c = *string;
@ -173,7 +181,8 @@ void tty_text_print(tty *ttyx, char *string, u32 color, u32 bgcolor)
ttyx->text.line++; ttyx->text.line++;
if (ttyx->text.line == ttyx->text.height) if (ttyx->text.line == ttyx->text.height)
{ {
scroll_buffer(ttyx->typeinfo.raw_framebuffer.pointer, scroll_buffer(
ttyx->typeinfo.raw_framebuffer.pointer,
ttyx->typeinfo.raw_framebuffer.width, ttyx->typeinfo.raw_framebuffer.width,
ttyx->typeinfo.raw_framebuffer.height, ttyx->typeinfo.raw_framebuffer.height,
ttyx->typeinfo.raw_framebuffer.pixsize, ttyx->typeinfo.raw_framebuffer.pixsize,
@ -192,7 +201,8 @@ void tty_text_print(tty *ttyx, char *string, u32 color, u32 bgcolor)
} }
else if (c == '\f') else if (c == '\f')
{ // 滚动一行 { // 滚动一行
scroll_buffer(ttyx->typeinfo.raw_framebuffer.pointer, scroll_buffer(
ttyx->typeinfo.raw_framebuffer.pointer,
ttyx->typeinfo.raw_framebuffer.width, ttyx->typeinfo.raw_framebuffer.width,
ttyx->typeinfo.raw_framebuffer.height, ttyx->typeinfo.raw_framebuffer.height,
ttyx->typeinfo.raw_framebuffer.pixsize, ttyx->typeinfo.raw_framebuffer.pixsize,
@ -213,3 +223,29 @@ void tty_text_print(tty *ttyx, char *string, u32 color, u32 bgcolor)
putchar(ttyx, '\0', gen_color(0x88, 0x88, 0x88), 0); putchar(ttyx, '\0', gen_color(0x88, 0x88, 0x88), 0);
simple_lock_unlock(ttyx->text.lock); simple_lock_unlock(ttyx->text.lock);
} }
bool tty_enable(tty *ttyx)
{
if (tty_ctrler.enabled[ttyx->id])
return false;
if (ttyx->type == tty_type_raw_framebuffer)
{
for (usize i = 0; i < TTY_MAX_NUM; ++i)
{
if (ttyx->id != i &&
tty_ctrler.enabled[i] != nullptr &&
tty_ctrler.ttys[i]->type == tty_type_raw_framebuffer)
return false;
}
}
ttyx->enabled = true;
tty_ctrler.enabled[ttyx->id] = ttyx;
return true;
}
void tty_disable(tty *ttyx)
{
ttyx->enabled = false;
tty_ctrler.enabled[ttyx->id] = false;
}

View File

@ -1,20 +1,20 @@
use std::{ use std::ptr::null_mut;
io::{BufWriter, Write},
ptr::null_mut,
};
extern "C" { extern "C" {
fn tty_new(tty_type: u8, mode: u8) -> *mut u8; fn tty_new(tty_type: u8, mode: u8) -> *mut u8;
fn tty_get(id: usize) -> *mut *mut u8; fn tty_get(id: usize) -> *mut *mut u8;
pub fn tty_text_print(ttyx: *mut u8, string: *mut u8, color: u32, bgcolor: u32); pub fn tty_text_print(ttyx: *mut u8, string: *mut u8, color: u32, bgcolor: u32);
fn tty_get_id(tty: *mut u8) -> usize; fn tty_get_id(tty: *mut u8) -> usize;
fn tty_enable(tty: *mut u8) -> bool;
fn tty_disable(tty: *mut u8);
} }
pub enum Type { pub enum Type {
Invalid = 0, Invalid = 0,
RawFramebuffer = 1, RawFramebuffer = 1,
Display = 2, Display = 2,
VirtualTty = 3, VirtualTerm = 3,
} }
pub enum Mode { pub enum Mode {
@ -50,12 +50,20 @@ impl Tty {
unsafe { tty_get_id(self.tty_pointer) } unsafe { tty_get_id(self.tty_pointer) }
} }
pub fn enable(&self) {
unsafe { tty_enable(self.tty_pointer) };
}
pub fn disable(&self) {
unsafe { tty_disable(self.tty_pointer) };
}
pub fn print(&self, msg: Message) { pub fn print(&self, msg: Message) {
for MessageSection { for MessageSection {
mut msg, mut msg,
fgcolor, fgcolor,
bgcolor, bgcolor,
} in msg.into_iter() } in msg.0.into_iter()
{ {
unsafe { unsafe {
let string = msg.as_bytes_mut() as *mut [u8] as *mut u8; let string = msg.as_bytes_mut() as *mut [u8] as *mut u8;
@ -89,7 +97,7 @@ pub struct MessageSection {
bgcolor: Color, bgcolor: Color,
} }
type Message = Vec<MessageSection>; pub struct Message(Vec<MessageSection>);
pub struct MessageBuilder { pub struct MessageBuilder {
msg: Message, msg: Message,
@ -97,11 +105,13 @@ pub struct MessageBuilder {
impl MessageBuilder { impl MessageBuilder {
pub fn new() -> Self { pub fn new() -> Self {
Self { msg: vec![] } Self {
msg: Message(vec![]),
}
} }
pub fn message(mut self, msg: String) -> Self { pub fn message(mut self, msg: String) -> Self {
self.msg.push(MessageSection { self.msg.0.push(MessageSection {
msg, msg,
fgcolor: Color(0xee, 0xee, 0xee), fgcolor: Color(0xee, 0xee, 0xee),
bgcolor: Color(0, 0, 0), bgcolor: Color(0, 0, 0),
@ -110,14 +120,14 @@ impl MessageBuilder {
} }
pub fn background_color(mut self, color: Color) -> Self { pub fn background_color(mut self, color: Color) -> Self {
if let Some(msg) = self.msg.last_mut() { if let Some(msg) = self.msg.0.last_mut() {
msg.bgcolor = color; msg.bgcolor = color;
} }
self self
} }
pub fn foreground_color(mut self, color: Color) -> Self { pub fn foreground_color(mut self, color: Color) -> Self {
if let Some(msg) = self.msg.last_mut() { if let Some(msg) = self.msg.0.last_mut() {
msg.fgcolor = color; msg.fgcolor = color;
} }
self self

View File

@ -7,7 +7,7 @@
lst_iterator_t *lst_new(usize start, usize end) lst_iterator_t *lst_new(usize start, usize end)
{ {
lst_iterator_t *lst = memm_kernel_allocate(sizeof(lst_iterator_t), 0); lst_iterator_t *lst = memm_kernel_allocate(sizeof(lst_iterator_t));
lst->line.left = start; lst->line.left = start;
lst->line.right = end; lst->line.right = end;
lst->next = nullptr; lst->next = nullptr;