增加内核日志功能 #3
|
@ -12,7 +12,7 @@ typedef enum __tty_type
|
|||
// 用于图形功能初始化后,直接连接图形接口
|
||||
tty_type_display = 2,
|
||||
// 用于图形终端的终端模拟器
|
||||
tty_type_vtty = 3,
|
||||
tty_type_vterm = 3,
|
||||
} tty_type;
|
||||
|
||||
typedef enum __framebuffer_pixel_type
|
||||
|
@ -58,6 +58,7 @@ typedef struct __tty
|
|||
tty_type type;
|
||||
tty_typeinfo typeinfo;
|
||||
tty_mode mode;
|
||||
bool enabled;
|
||||
tty_text_state text;
|
||||
} tty;
|
||||
|
||||
|
@ -67,6 +68,7 @@ typedef struct __tty_controller_t
|
|||
#define TTY_MAX_NUM 128
|
||||
tty *ttys[TTY_MAX_NUM];
|
||||
bool map[TTY_MAX_NUM];
|
||||
tty *enabled[TTY_MAX_NUM];
|
||||
} 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))
|
||||
|
||||
/**
|
||||
* @brief 打开某个tty
|
||||
*
|
||||
* @param ttyx
|
||||
* @return true 打开成功
|
||||
* @return false 已经打开
|
||||
* 或作为raw_framebuffer类型的tty,framebuffer已被其它raw_framebuffer
|
||||
* 类型的tty占用
|
||||
*/
|
||||
bool tty_enable(tty *ttyx);
|
||||
|
||||
void tty_disable(tty *ttyx);
|
||||
|
||||
#define TTY_FONT_SCALE 2
|
||||
|
||||
typedef struct __tty_font_t
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
use std::panic;
|
||||
|
||||
use crate::kernel::tty::tty::{Color, MessageBuilder, Tty};
|
||||
|
||||
#[no_mangle]
|
||||
extern "C" fn kmain_rust() {
|
||||
let tty = Tty::from_id(0).unwrap();
|
||||
// let ttyptr = unsafe { tty.get_c_tty_t() };
|
||||
|
||||
tty.enable();
|
||||
let hello = MessageBuilder::new()
|
||||
.message("Hello, ".to_string())
|
||||
.message("Metaverse".to_string())
|
||||
|
|
|
@ -11,26 +11,27 @@ tty_controller_t *tty_controller_new()
|
|||
{
|
||||
memset(tty_ctrler.ttys, 0, sizeof(tty_ctrler.ttys));
|
||||
memset(tty_ctrler.map, 0, sizeof(tty_ctrler.map));
|
||||
memset(tty_ctrler.enabled, 0, sizeof(tty_ctrler.enabled));
|
||||
return &tty_ctrler;
|
||||
}
|
||||
|
||||
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;
|
||||
for (usize i = 0; i < TTY_MAX_NUM; ++i)
|
||||
{
|
||||
if (tty_ctrler.map[i] == false)
|
||||
{
|
||||
res = __tty;
|
||||
__tty->id = i;
|
||||
tty_ctrler.ttys[i] = __tty;
|
||||
res = memm_kernel_allocate(sizeof(tty));
|
||||
res->id = i;
|
||||
tty_ctrler.ttys[i] = res;
|
||||
tty_ctrler.map[i] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
res->type = type;
|
||||
res->mode = mode;
|
||||
res->enabled = false;
|
||||
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.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)
|
||||
{
|
||||
put_pixel(ttyx->typeinfo.raw_framebuffer.pointer,
|
||||
put_pixel(
|
||||
ttyx->typeinfo.raw_framebuffer.pointer,
|
||||
ttyx->typeinfo.raw_framebuffer.width,
|
||||
ttyx->typeinfo.raw_framebuffer.pixsize,
|
||||
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)
|
||||
{
|
||||
put_pixel(ttyx->typeinfo.raw_framebuffer.pointer,
|
||||
put_pixel(
|
||||
ttyx->typeinfo.raw_framebuffer.pointer,
|
||||
ttyx->typeinfo.raw_framebuffer.width,
|
||||
ttyx->typeinfo.raw_framebuffer.pixsize,
|
||||
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++;
|
||||
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.height,
|
||||
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)
|
||||
{
|
||||
if (ttyx->enabled == false)
|
||||
return;
|
||||
if (ttyx->mode != tty_mode_text)
|
||||
return;
|
||||
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();
|
||||
simple_lock_lock(ttyx->text.lock);
|
||||
usize len = strlen(string);
|
||||
simple_lock_lock(ttyx->text.lock);
|
||||
for (char *str = string; string - str < len; string++)
|
||||
{
|
||||
char c = *string;
|
||||
|
@ -173,7 +181,8 @@ void tty_text_print(tty *ttyx, char *string, u32 color, u32 bgcolor)
|
|||
ttyx->text.line++;
|
||||
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.height,
|
||||
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')
|
||||
{ // 滚动一行
|
||||
scroll_buffer(ttyx->typeinfo.raw_framebuffer.pointer,
|
||||
scroll_buffer(
|
||||
ttyx->typeinfo.raw_framebuffer.pointer,
|
||||
ttyx->typeinfo.raw_framebuffer.width,
|
||||
ttyx->typeinfo.raw_framebuffer.height,
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
use std::{
|
||||
io::{BufWriter, Write},
|
||||
ptr::null_mut,
|
||||
};
|
||||
use std::ptr::null_mut;
|
||||
|
||||
extern "C" {
|
||||
fn tty_new(tty_type: u8, mode: u8) -> *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);
|
||||
fn tty_get_id(tty: *mut u8) -> usize;
|
||||
|
||||
fn tty_enable(tty: *mut u8) -> bool;
|
||||
fn tty_disable(tty: *mut u8);
|
||||
}
|
||||
|
||||
pub enum Type {
|
||||
Invalid = 0,
|
||||
RawFramebuffer = 1,
|
||||
Display = 2,
|
||||
VirtualTty = 3,
|
||||
VirtualTerm = 3,
|
||||
}
|
||||
|
||||
pub enum Mode {
|
||||
|
@ -50,12 +50,20 @@ impl Tty {
|
|||
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) {
|
||||
for MessageSection {
|
||||
mut msg,
|
||||
fgcolor,
|
||||
bgcolor,
|
||||
} in msg.into_iter()
|
||||
} in msg.0.into_iter()
|
||||
{
|
||||
unsafe {
|
||||
let string = msg.as_bytes_mut() as *mut [u8] as *mut u8;
|
||||
|
@ -89,7 +97,7 @@ pub struct MessageSection {
|
|||
bgcolor: Color,
|
||||
}
|
||||
|
||||
type Message = Vec<MessageSection>;
|
||||
pub struct Message(Vec<MessageSection>);
|
||||
|
||||
pub struct MessageBuilder {
|
||||
msg: Message,
|
||||
|
@ -97,11 +105,13 @@ pub struct MessageBuilder {
|
|||
|
||||
impl MessageBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self { msg: vec![] }
|
||||
Self {
|
||||
msg: Message(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn message(mut self, msg: String) -> Self {
|
||||
self.msg.push(MessageSection {
|
||||
self.msg.0.push(MessageSection {
|
||||
msg,
|
||||
fgcolor: Color(0xee, 0xee, 0xee),
|
||||
bgcolor: Color(0, 0, 0),
|
||||
|
@ -110,14 +120,14 @@ impl MessageBuilder {
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
self
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
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.right = end;
|
||||
lst->next = nullptr;
|
||||
|
|
Reference in New Issue