From 69b6d55d4d2d7271dd41d9a340b9a5832ee094b3 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Mon, 22 Jan 2024 00:48:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=94=AF=E6=8C=81=E5=A4=9Att?= =?UTF-8?q?y=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/kernel/tty.h | 17 ++++++- src/kernel/main.rs | 5 +- src/kernel/tty/tty.c | 106 ++++++++++++++++++++++++++++-------------- src/kernel/tty/tty.rs | 32 ++++++++----- src/libk/lst.c | 2 +- 5 files changed, 110 insertions(+), 52 deletions(-) diff --git a/include/kernel/tty.h b/include/kernel/tty.h index 6079eeb..1a8395d 100644 --- a/include/kernel/tty.h +++ b/include/kernel/tty.h @@ -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 diff --git a/src/kernel/main.rs b/src/kernel/main.rs index 099a7ee..cf8c6e4 100644 --- a/src/kernel/main.rs +++ b/src/kernel/main.rs @@ -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()) diff --git a/src/kernel/tty/tty.c b/src/kernel/tty/tty.c index b7c5b36..a2af9b2 100644 --- a/src/kernel/tty/tty.c +++ b/src/kernel/tty/tty.c @@ -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,12 +90,13 @@ inline static void putchar( { for (usize b = 0; b < TTY_FONT_SCALE; ++b) { - 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, - ttyx->text.line * font->char_height * TTY_FONT_SCALE + j * TTY_FONT_SCALE + b, - color); + 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, + ttyx->text.line * font->char_height * TTY_FONT_SCALE + j * TTY_FONT_SCALE + b, + color); } } } @@ -102,12 +106,13 @@ inline static void putchar( { for (usize b = 0; b < TTY_FONT_SCALE; ++b) { - 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, - ttyx->text.line * font->char_height * TTY_FONT_SCALE + j * TTY_FONT_SCALE + b, - bgcolor); + 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, + ttyx->text.line * font->char_height * TTY_FONT_SCALE + j * TTY_FONT_SCALE + b, + bgcolor); } } } @@ -121,17 +126,20 @@ inline static void newline(tty *ttyx) ttyx->text.line++; if (ttyx->text.line == ttyx->text.height) { - scroll_buffer(ttyx->typeinfo.raw_framebuffer.pointer, - ttyx->typeinfo.raw_framebuffer.width, - ttyx->typeinfo.raw_framebuffer.height, - ttyx->typeinfo.raw_framebuffer.pixsize, - tty_get_font()->char_height * TTY_FONT_SCALE); + scroll_buffer( + ttyx->typeinfo.raw_framebuffer.pointer, + ttyx->typeinfo.raw_framebuffer.width, + ttyx->typeinfo.raw_framebuffer.height, + ttyx->typeinfo.raw_framebuffer.pixsize, + tty_get_font()->char_height * TTY_FONT_SCALE); ttyx->text.line--; } } 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,11 +181,12 @@ 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, - ttyx->typeinfo.raw_framebuffer.width, - ttyx->typeinfo.raw_framebuffer.height, - ttyx->typeinfo.raw_framebuffer.pixsize, - font->char_height * TTY_FONT_SCALE); + scroll_buffer( + ttyx->typeinfo.raw_framebuffer.pointer, + ttyx->typeinfo.raw_framebuffer.width, + ttyx->typeinfo.raw_framebuffer.height, + ttyx->typeinfo.raw_framebuffer.pixsize, + font->char_height * TTY_FONT_SCALE); ttyx->text.line--; } continue; @@ -192,11 +201,12 @@ void tty_text_print(tty *ttyx, char *string, u32 color, u32 bgcolor) } else if (c == '\f') { // 滚动一行 - scroll_buffer(ttyx->typeinfo.raw_framebuffer.pointer, - ttyx->typeinfo.raw_framebuffer.width, - ttyx->typeinfo.raw_framebuffer.height, - ttyx->typeinfo.raw_framebuffer.pixsize, - font->char_height * TTY_FONT_SCALE); + scroll_buffer( + ttyx->typeinfo.raw_framebuffer.pointer, + ttyx->typeinfo.raw_framebuffer.width, + ttyx->typeinfo.raw_framebuffer.height, + ttyx->typeinfo.raw_framebuffer.pixsize, + font->char_height * TTY_FONT_SCALE); continue; } // 打印字符c @@ -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; +} diff --git a/src/kernel/tty/tty.rs b/src/kernel/tty/tty.rs index 2d6aa59..1f33a1a 100644 --- a/src/kernel/tty/tty.rs +++ b/src/kernel/tty/tty.rs @@ -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; +pub struct Message(Vec); 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 diff --git a/src/libk/lst.c b/src/libk/lst.c index 71b1cef..b858723 100644 --- a/src/libk/lst.c +++ b/src/libk/lst.c @@ -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;