Skip to content

Commit

Permalink
Feat(tty): add dummy console (DragonOS-Community#735)
Browse files Browse the repository at this point in the history
使得riscv能暂时完成stdio_init(将来需要实现riscv的串口console)
  • Loading branch information
fslongjin authored and BrahmaMantra committed Dec 9, 2024
1 parent d8116af commit 4100884
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 18 deletions.
11 changes: 11 additions & 0 deletions kernel/src/driver/tty/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ pub struct WindowSize {
pub ypixel: u16,
}

impl WindowSize {
pub fn new(row: u16, col: u16, xpixel: u16, ypixel: u16) -> Self {
Self {
row,
col,
xpixel,
ypixel,
}
}
}

#[derive(Debug, Clone, Copy)]
pub struct Termios {
pub input_mode: InputMode,
Expand Down
4 changes: 4 additions & 0 deletions kernel/src/driver/tty/tty_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ impl TtyPortData {
pub fn internal_tty(&self) -> Option<Arc<TtyCore>> {
self.internal_tty.upgrade()
}

pub fn tty(&self) -> Option<Arc<TtyCore>> {
self.tty.upgrade()
}
}

#[allow(dead_code)]
Expand Down
34 changes: 22 additions & 12 deletions kernel/src/driver/tty/virtual_terminal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::sync::atomic::Ordering;
use core::{fmt::Formatter, sync::atomic::Ordering};

use alloc::{
string::{String, ToString},
Expand All @@ -8,12 +8,9 @@ use alloc::{
use system_error::SystemError;

use crate::{
driver::{
base::device::{
device_number::{DeviceNumber, Major},
device_register, IdTable,
},
video::fbdev::base::fbcon::framebuffer_console::BlittingFbConsole,
driver::base::device::{
device_number::{DeviceNumber, Major},
device_register, IdTable,
},
filesystem::devfs::devfs_register,
libs::spinlock::SpinLock,
Expand Down Expand Up @@ -95,16 +92,29 @@ impl Color {
}
}

#[derive(Debug)]
pub struct TtyConsoleDriverInner {
console: Arc<BlittingFbConsole>,
console: Arc<dyn ConsoleSwitch>,
}

impl core::fmt::Debug for TtyConsoleDriverInner {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "TtyConsoleDriverInner")
}
}

impl TtyConsoleDriverInner {
pub fn new() -> Result<Self, SystemError> {
Ok(Self {
console: Arc::new(BlittingFbConsole::new()?),
})
let console = {
#[cfg(not(target_arch = "riscv64"))]
{
Arc::new(crate::driver::video::fbdev::base::fbcon::framebuffer_console::BlittingFbConsole::new()?)
}

#[cfg(target_arch = "riscv64")]
crate::driver::video::console::dummycon::dummy_console()
};

Ok(Self { console })
}

fn do_write(&self, tty: &TtyCoreData, buf: &[u8], mut nr: usize) -> Result<usize, SystemError> {
Expand Down
3 changes: 1 addition & 2 deletions kernel/src/driver/tty/virtual_terminal/virtual_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,8 @@ impl VirtualConsoleData {
self.underline_color = 3; // cyan
self.half_color = 0x08; // grey

self.reset(clear);

self.screen_buf.resize(self.cols * self.rows, 0);
self.reset(clear);
}

pub fn should_update(&self) -> bool {
Expand Down
127 changes: 127 additions & 0 deletions kernel/src/driver/video/console/dummycon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use alloc::sync::Arc;
use system_error::SystemError;

use crate::driver::tty::{
console::ConsoleSwitch,
termios::WindowSize,
tty_driver::TtyOperation,
virtual_terminal::virtual_console::{CursorOperation, ScrollDir, VirtualConsoleData},
};

lazy_static! {
pub static ref DUMMY_CONSOLE: Arc<DummyConsole> = Arc::new(DummyConsole::new());
}

#[inline]
#[allow(dead_code)]
pub fn dummy_console() -> Arc<DummyConsole> {
DUMMY_CONSOLE.clone()
}

pub struct DummyConsole;

impl DummyConsole {
pub const COLUNMS: usize = 80;
pub const ROWS: usize = 25;
pub const fn new() -> Self {
DummyConsole
}
}

impl ConsoleSwitch for DummyConsole {
fn con_getxy(
&self,
_vc_data: &VirtualConsoleData,
_pos: usize,
) -> Result<(usize, usize, usize), SystemError> {
Ok((0, 0, 0))
}

fn con_build_attr(
&self,
_vc_data: &VirtualConsoleData,
_color: u8,
_intensity: crate::driver::tty::virtual_terminal::virtual_console::VirtualConsoleIntensity,
_blink: bool,
_underline: bool,
_reverse: bool,
_italic: bool,
) -> Result<u8, SystemError> {
Ok(0)
}
fn con_init(&self, vc_data: &mut VirtualConsoleData, init: bool) -> Result<(), SystemError> {
vc_data.color_mode = true;

if init {
vc_data.cols = Self::COLUNMS;
vc_data.rows = Self::ROWS;
} else {
let tty = vc_data.port().port_data().tty().unwrap();
tty.resize(
tty.clone(),
WindowSize::new(Self::ROWS as u16, Self::COLUNMS as u16, 0, 0),
)?;
}

Ok(())
}

fn con_deinit(&self) -> Result<(), SystemError> {
Ok(())
}

fn con_clear(
&self,
_vc_data: &mut VirtualConsoleData,
_sy: usize,
_sx: usize,
_height: usize,
_width: usize,
) -> Result<(), SystemError> {
Ok(())
}

fn con_putc(
&self,
_vc_data: &VirtualConsoleData,
_ch: u16,
_ypos: u32,
_xpos: u32,
) -> Result<(), SystemError> {
Ok(())
}

fn con_putcs(
&self,
_vc_data: &VirtualConsoleData,
_buf: &[u16],
_count: usize,
_ypos: u32,
_xpos: u32,
) -> Result<(), SystemError> {
Ok(())
}

fn con_cursor(&self, _vc_data: &VirtualConsoleData, _op: CursorOperation) {
// Do nothing
}

fn con_set_palette(
&self,
_vc_data: &VirtualConsoleData,
_color_table: &[u8],
) -> Result<(), SystemError> {
Ok(())
}

fn con_scroll(
&self,
_vc_data: &mut VirtualConsoleData,
_top: usize,
_bottom: usize,
_dir: ScrollDir,
_nr: usize,
) -> bool {
false
}
}
1 change: 1 addition & 0 deletions kernel/src/driver/video/console/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod dummycon;
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ impl BlittingFbConsole {
})
}

pub fn fb(&self) -> Arc<dyn FrameBuffer> {
fn fb(&self) -> Arc<dyn FrameBuffer> {
self.fb.lock().clone().unwrap()
}

pub fn get_color(&self, vc_data: &VirtualConsoleData, c: u16, is_fg: bool) -> u32 {
fn get_color(&self, vc_data: &VirtualConsoleData, c: u16, is_fg: bool) -> u32 {
let fb_info = self.fb();
let mut color = 0;

Expand Down Expand Up @@ -104,7 +104,7 @@ impl BlittingFbConsole {
}

/// ## 计算单色调的函数
pub fn mono_color(&self) -> u32 {
fn mono_color(&self) -> u32 {
let fb_info = self.fb();
let mut max_len = fb_info
.current_fb_var()
Expand All @@ -117,7 +117,7 @@ impl BlittingFbConsole {
return (!(0xfff << max_len)) & 0xff;
}

pub fn bit_put_string(
fn bit_put_string(
&self,
vc_data: &VirtualConsoleData,
buf: &[u16],
Expand Down
1 change: 1 addition & 0 deletions kernel/src/driver/video/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
use alloc::{boxed::Box, sync::Arc};
use system_error::SystemError;

pub mod console;
pub mod fbdev;

static mut __MAMAGER: Option<VideoRefreshManager> = None;
Expand Down
1 change: 1 addition & 0 deletions kernel/src/init/initial_kthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn kernel_init() -> Result<(), SystemError> {
// 由于目前加锁,速度过慢,所以先不开启双缓冲
// scm_enable_double_buffer().expect("Failed to enable double buffer");

#[cfg(target_arch = "x86_64")]
ahci_init().expect("Failed to initialize AHCI");

mount_root_fs().expect("Failed to mount root fs");
Expand Down

0 comments on commit 4100884

Please sign in to comment.