152da9a59SGnoCiYeH use core::sync::atomic::Ordering; 252da9a59SGnoCiYeH 352da9a59SGnoCiYeH use alloc::{string::String, sync::Arc, vec::Vec}; 452da9a59SGnoCiYeH use system_error::SystemError; 552da9a59SGnoCiYeH 652da9a59SGnoCiYeH use crate::{ 752da9a59SGnoCiYeH driver::{ 852da9a59SGnoCiYeH base::device::{ 952da9a59SGnoCiYeH device_number::{DeviceNumber, Major}, 1052da9a59SGnoCiYeH device_register, IdTable, 1152da9a59SGnoCiYeH }, 1252da9a59SGnoCiYeH video::fbdev::base::fbcon::framebuffer_console::BlittingFbConsole, 1352da9a59SGnoCiYeH }, 1452da9a59SGnoCiYeH filesystem::devfs::devfs_register, 1552da9a59SGnoCiYeH libs::spinlock::SpinLock, 1652da9a59SGnoCiYeH }; 1752da9a59SGnoCiYeH 1852da9a59SGnoCiYeH use self::virtual_console::{VirtualConsoleData, CURRENT_VCNUM}; 1952da9a59SGnoCiYeH 2052da9a59SGnoCiYeH use super::{ 2152da9a59SGnoCiYeH console::ConsoleSwitch, 2252da9a59SGnoCiYeH termios::{InputMode, TTY_STD_TERMIOS}, 2352da9a59SGnoCiYeH tty_core::{TtyCore, TtyCoreData}, 2452da9a59SGnoCiYeH tty_device::TtyDevice, 2552da9a59SGnoCiYeH tty_driver::{TtyDriver, TtyDriverManager, TtyDriverType, TtyOperation}, 2652da9a59SGnoCiYeH }; 2752da9a59SGnoCiYeH 2852da9a59SGnoCiYeH pub mod console_map; 2952da9a59SGnoCiYeH pub mod virtual_console; 3052da9a59SGnoCiYeH 3152da9a59SGnoCiYeH pub const MAX_NR_CONSOLES: u32 = 63; 3252da9a59SGnoCiYeH pub const VC_MAXCOL: usize = 32767; 3352da9a59SGnoCiYeH pub const VC_MAXROW: usize = 32767; 3452da9a59SGnoCiYeH 3552da9a59SGnoCiYeH pub const DEFAULT_RED: [u16; 16] = [ 3652da9a59SGnoCiYeH 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 3752da9a59SGnoCiYeH ]; 3852da9a59SGnoCiYeH 3952da9a59SGnoCiYeH pub const DEFAULT_GREEN: [u16; 16] = [ 4052da9a59SGnoCiYeH 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa, 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff, 4152da9a59SGnoCiYeH ]; 4252da9a59SGnoCiYeH 4352da9a59SGnoCiYeH pub const DEFAULT_BLUE: [u16; 16] = [ 4452da9a59SGnoCiYeH 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff, 4552da9a59SGnoCiYeH ]; 4652da9a59SGnoCiYeH 47*b5b571e0SLoGin pub const COLOR_TABLE: &[u8] = &[0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15]; 4852da9a59SGnoCiYeH 4952da9a59SGnoCiYeH lazy_static! { 5052da9a59SGnoCiYeH pub static ref VIRT_CONSOLES: Vec<Arc<SpinLock<VirtualConsoleData>>> = { 5152da9a59SGnoCiYeH let mut v = Vec::with_capacity(MAX_NR_CONSOLES as usize); 5252da9a59SGnoCiYeH for i in 0..MAX_NR_CONSOLES as usize { 5352da9a59SGnoCiYeH v.push(Arc::new(SpinLock::new(VirtualConsoleData::new(i)))); 5452da9a59SGnoCiYeH } 5552da9a59SGnoCiYeH 5652da9a59SGnoCiYeH v 5752da9a59SGnoCiYeH }; 5852da9a59SGnoCiYeH } 5952da9a59SGnoCiYeH 6052da9a59SGnoCiYeH #[derive(Debug, Clone, Copy, Default)] 6152da9a59SGnoCiYeH pub struct Color { 6252da9a59SGnoCiYeH pub red: u16, 6352da9a59SGnoCiYeH pub green: u16, 6452da9a59SGnoCiYeH pub blue: u16, 6552da9a59SGnoCiYeH pub transp: u16, 6652da9a59SGnoCiYeH } 6752da9a59SGnoCiYeH 6852da9a59SGnoCiYeH impl Color { 6952da9a59SGnoCiYeH pub fn from_256(col: u32) -> Self { 7052da9a59SGnoCiYeH let mut color = Self::default(); 7152da9a59SGnoCiYeH if col < 8 { 7252da9a59SGnoCiYeH color.red = if col & 1 != 0 { 0xaa } else { 0x00 }; 7352da9a59SGnoCiYeH color.green = if col & 2 != 0 { 0xaa } else { 0x00 }; 7452da9a59SGnoCiYeH color.blue = if col & 4 != 0 { 0xaa } else { 0x00 }; 7552da9a59SGnoCiYeH } else if col < 16 { 7652da9a59SGnoCiYeH color.red = if col & 1 != 0 { 0xff } else { 0x55 }; 7752da9a59SGnoCiYeH color.green = if col & 2 != 0 { 0xff } else { 0x55 }; 7852da9a59SGnoCiYeH color.blue = if col & 4 != 0 { 0xff } else { 0x55 }; 7952da9a59SGnoCiYeH } else if col < 232 { 8052da9a59SGnoCiYeH color.red = ((col - 16) / 36 * 85 / 2) as u16; 8152da9a59SGnoCiYeH color.green = ((col - 16) / 6 % 6 * 85 / 2) as u16; 8252da9a59SGnoCiYeH color.blue = ((col - 16) % 6 * 85 / 2) as u16; 8352da9a59SGnoCiYeH } else { 8452da9a59SGnoCiYeH let col = (col * 10 - 2312) as u16; 8552da9a59SGnoCiYeH color.red = col; 8652da9a59SGnoCiYeH color.green = col; 8752da9a59SGnoCiYeH color.blue = col; 8852da9a59SGnoCiYeH } 8952da9a59SGnoCiYeH 9052da9a59SGnoCiYeH color 9152da9a59SGnoCiYeH } 9252da9a59SGnoCiYeH } 9352da9a59SGnoCiYeH 9452da9a59SGnoCiYeH #[derive(Debug)] 9552da9a59SGnoCiYeH pub struct TtyConsoleDriverInner { 9652da9a59SGnoCiYeH console: Arc<BlittingFbConsole>, 9752da9a59SGnoCiYeH } 9852da9a59SGnoCiYeH 9952da9a59SGnoCiYeH impl TtyConsoleDriverInner { 10052da9a59SGnoCiYeH pub fn new() -> Result<Self, SystemError> { 10152da9a59SGnoCiYeH Ok(Self { 10252da9a59SGnoCiYeH console: Arc::new(BlittingFbConsole::new()?), 10352da9a59SGnoCiYeH }) 10452da9a59SGnoCiYeH } 10552da9a59SGnoCiYeH 10652bcb59eSGnoCiYeH fn do_write(&self, tty: &TtyCoreData, buf: &[u8], mut nr: usize) -> Result<usize, SystemError> { 10752da9a59SGnoCiYeH // 关闭中断 10852da9a59SGnoCiYeH let mut vc_data = tty.vc_data_irqsave(); 10952da9a59SGnoCiYeH 11052da9a59SGnoCiYeH let mut offset = 0; 11152da9a59SGnoCiYeH 11252da9a59SGnoCiYeH // 这个参数是用来扫描unicode字符的,但是这部分目前未完成,先写着 11352da9a59SGnoCiYeH let mut rescan = false; 11452da9a59SGnoCiYeH let mut ch: u32 = 0; 11552da9a59SGnoCiYeH 11652da9a59SGnoCiYeH let mut draw = DrawRegion::default(); 11752da9a59SGnoCiYeH 11852da9a59SGnoCiYeH // 首先隐藏光标再写 11952da9a59SGnoCiYeH vc_data.hide_cursor(); 12052da9a59SGnoCiYeH 12152da9a59SGnoCiYeH while nr != 0 { 12252da9a59SGnoCiYeH if !rescan { 12352da9a59SGnoCiYeH ch = buf[offset] as u32; 12452da9a59SGnoCiYeH offset += 1; 12552da9a59SGnoCiYeH nr -= 1; 12652da9a59SGnoCiYeH } 12752da9a59SGnoCiYeH 12852da9a59SGnoCiYeH let (tc, rescan_last) = vc_data.translate(&mut ch); 12952da9a59SGnoCiYeH if tc.is_none() { 13052da9a59SGnoCiYeH // 表示未转换完成 13152da9a59SGnoCiYeH continue; 13252da9a59SGnoCiYeH } 13352da9a59SGnoCiYeH 13452da9a59SGnoCiYeH let tc = tc.unwrap(); 13552da9a59SGnoCiYeH rescan = rescan_last; 13652da9a59SGnoCiYeH 13752da9a59SGnoCiYeH if vc_data.is_control(tc, ch) { 13852da9a59SGnoCiYeH vc_data.flush(&mut draw); 13952da9a59SGnoCiYeH vc_data.do_control(ch); 14052da9a59SGnoCiYeH continue; 14152da9a59SGnoCiYeH } 14252da9a59SGnoCiYeH 14352da9a59SGnoCiYeH if !vc_data.console_write_normal(tc, ch, &mut draw) { 14452da9a59SGnoCiYeH continue; 14552da9a59SGnoCiYeH } 14652da9a59SGnoCiYeH } 14752da9a59SGnoCiYeH 14852da9a59SGnoCiYeH vc_data.flush(&mut draw); 14952da9a59SGnoCiYeH 15052da9a59SGnoCiYeH // TODO: notify update 15152da9a59SGnoCiYeH return Ok(offset); 15252da9a59SGnoCiYeH } 15352bcb59eSGnoCiYeH } 15452da9a59SGnoCiYeH 15552bcb59eSGnoCiYeH impl TtyOperation for TtyConsoleDriverInner { 15652bcb59eSGnoCiYeH fn install(&self, _driver: Arc<TtyDriver>, tty: Arc<TtyCore>) -> Result<(), SystemError> { 15752bcb59eSGnoCiYeH let tty_core = tty.core(); 15852bcb59eSGnoCiYeH let mut vc_data = VIRT_CONSOLES[tty_core.index()].lock(); 15952bcb59eSGnoCiYeH 16052bcb59eSGnoCiYeH self.console.con_init(&mut vc_data, true)?; 16152bcb59eSGnoCiYeH if vc_data.complement_mask == 0 { 16252bcb59eSGnoCiYeH vc_data.complement_mask = if vc_data.color_mode { 0x7700 } else { 0x0800 }; 16352bcb59eSGnoCiYeH } 16452bcb59eSGnoCiYeH vc_data.s_complement_mask = vc_data.complement_mask; 16552bcb59eSGnoCiYeH // vc_data.bytes_per_row = vc_data.cols << 1; 16652bcb59eSGnoCiYeH vc_data.index = tty_core.index(); 16752bcb59eSGnoCiYeH vc_data.bottom = vc_data.rows; 16852bcb59eSGnoCiYeH vc_data.set_driver_funcs(Arc::downgrade( 16952bcb59eSGnoCiYeH &(self.console.clone() as Arc<dyn ConsoleSwitch>), 17052bcb59eSGnoCiYeH )); 17152bcb59eSGnoCiYeH 17252bcb59eSGnoCiYeH // todo: unicode字符集处理? 17352bcb59eSGnoCiYeH 17452bcb59eSGnoCiYeH if vc_data.cols > VC_MAXCOL || vc_data.rows > VC_MAXROW { 17552bcb59eSGnoCiYeH return Err(SystemError::EINVAL); 17652bcb59eSGnoCiYeH } 17752bcb59eSGnoCiYeH 17852bcb59eSGnoCiYeH vc_data.init(None, None, true); 17952bcb59eSGnoCiYeH vc_data.update_attr(); 18052bcb59eSGnoCiYeH 18152bcb59eSGnoCiYeH let window_size = tty_core.window_size_upgradeable(); 18252bcb59eSGnoCiYeH if window_size.col == 0 && window_size.row == 0 { 18352bcb59eSGnoCiYeH let mut window_size = window_size.upgrade(); 18452bcb59eSGnoCiYeH window_size.col = vc_data.cols as u16; 18552bcb59eSGnoCiYeH window_size.row = vc_data.rows as u16; 18652bcb59eSGnoCiYeH } 18752bcb59eSGnoCiYeH 18852bcb59eSGnoCiYeH if vc_data.utf { 18952bcb59eSGnoCiYeH tty_core.termios_write().input_mode.insert(InputMode::IUTF8); 19052bcb59eSGnoCiYeH } else { 19152bcb59eSGnoCiYeH tty_core.termios_write().input_mode.remove(InputMode::IUTF8); 19252bcb59eSGnoCiYeH } 19352bcb59eSGnoCiYeH 19452bcb59eSGnoCiYeH // 加入sysfs? 19552bcb59eSGnoCiYeH 19652bcb59eSGnoCiYeH Ok(()) 19752bcb59eSGnoCiYeH } 19852bcb59eSGnoCiYeH 19952bcb59eSGnoCiYeH fn open(&self, _tty: &TtyCoreData) -> Result<(), SystemError> { 20052bcb59eSGnoCiYeH Ok(()) 20152bcb59eSGnoCiYeH } 20252bcb59eSGnoCiYeH 20352bcb59eSGnoCiYeH fn write_room(&self, _tty: &TtyCoreData) -> usize { 20452bcb59eSGnoCiYeH 32768 20552bcb59eSGnoCiYeH } 20652bcb59eSGnoCiYeH 20752bcb59eSGnoCiYeH /// 参考: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/tty/vt/vt.c#2894 20852bcb59eSGnoCiYeH #[inline(never)] 20952bcb59eSGnoCiYeH fn write(&self, tty: &TtyCoreData, buf: &[u8], nr: usize) -> Result<usize, SystemError> { 21052bcb59eSGnoCiYeH let ret = self.do_write(tty, buf, nr); 21152bcb59eSGnoCiYeH self.flush_chars(tty); 21252bcb59eSGnoCiYeH ret 21352bcb59eSGnoCiYeH } 21452bcb59eSGnoCiYeH 21552bcb59eSGnoCiYeH #[inline(never)] 21652da9a59SGnoCiYeH fn flush_chars(&self, tty: &TtyCoreData) { 21752da9a59SGnoCiYeH let mut vc_data = tty.vc_data_irqsave(); 21852da9a59SGnoCiYeH vc_data.set_cursor(); 21952da9a59SGnoCiYeH } 22052da9a59SGnoCiYeH 22152da9a59SGnoCiYeH fn put_char(&self, tty: &TtyCoreData, ch: u8) -> Result<(), SystemError> { 22252da9a59SGnoCiYeH self.write(tty, &[ch], 1)?; 22352da9a59SGnoCiYeH Ok(()) 22452da9a59SGnoCiYeH } 22552da9a59SGnoCiYeH 22652da9a59SGnoCiYeH fn ioctl(&self, _tty: Arc<TtyCore>, _cmd: u32, _arg: usize) -> Result<(), SystemError> { 22752da9a59SGnoCiYeH // TODO 22852da9a59SGnoCiYeH Err(SystemError::ENOIOCTLCMD) 22952da9a59SGnoCiYeH } 23052da9a59SGnoCiYeH } 23152da9a59SGnoCiYeH 23252da9a59SGnoCiYeH #[derive(Debug, Clone)] 23352da9a59SGnoCiYeH pub struct VtModeData { 23452da9a59SGnoCiYeH mode: VtMode, 23552da9a59SGnoCiYeH /// 释放请求时触发的信号 23652da9a59SGnoCiYeH relsig: u16, 23752da9a59SGnoCiYeH /// 获取请求时触发的信号 23852da9a59SGnoCiYeH acqsig: u16, 23952da9a59SGnoCiYeH } 24052da9a59SGnoCiYeH 24152da9a59SGnoCiYeH #[allow(dead_code)] 24252da9a59SGnoCiYeH #[derive(Debug, Clone)] 24352da9a59SGnoCiYeH pub enum VtMode { 24452da9a59SGnoCiYeH /// 自动切换模式,即在请求输入时自动切换到终端 24552da9a59SGnoCiYeH Auto, 24652da9a59SGnoCiYeH /// 手动切换模式,需要通过 ioctl 请求切换到终端 24752da9a59SGnoCiYeH Process, 24852da9a59SGnoCiYeH /// 等待终端确认,即在切换到终端时等待终端的确认信号 24952da9a59SGnoCiYeH Ackacq, 25052da9a59SGnoCiYeH } 25152da9a59SGnoCiYeH 25252da9a59SGnoCiYeH /// 用于给vc确定要写入的buf位置 25352da9a59SGnoCiYeH #[derive(Debug, Default)] 25452da9a59SGnoCiYeH pub struct DrawRegion { 25552da9a59SGnoCiYeH /// 偏移量 25652da9a59SGnoCiYeH pub offset: usize, 25752da9a59SGnoCiYeH /// 写入数量 25852da9a59SGnoCiYeH pub size: usize, 25952da9a59SGnoCiYeH pub x: Option<u32>, 26052da9a59SGnoCiYeH } 26152da9a59SGnoCiYeH 26252da9a59SGnoCiYeH // 初始化虚拟终端 26352da9a59SGnoCiYeH #[inline(never)] 26452da9a59SGnoCiYeH pub fn vty_init() -> Result<(), SystemError> { 26552da9a59SGnoCiYeH // 注册虚拟终端设备并将虚拟终端设备加入到文件系统 26652da9a59SGnoCiYeH let vc0 = TtyDevice::new( 26752da9a59SGnoCiYeH "vc0", 26852da9a59SGnoCiYeH IdTable::new( 26952da9a59SGnoCiYeH String::from("vc0"), 27052da9a59SGnoCiYeH Some(DeviceNumber::new(Major::TTY_MAJOR, 0)), 27152da9a59SGnoCiYeH ), 27252da9a59SGnoCiYeH ); 27352da9a59SGnoCiYeH // 注册tty设备 27452da9a59SGnoCiYeH // CharDevOps::cdev_add( 27552da9a59SGnoCiYeH // vc0.clone() as Arc<dyn CharDevice>, 27652da9a59SGnoCiYeH // IdTable::new( 27752da9a59SGnoCiYeH // String::from("vc0"), 27852da9a59SGnoCiYeH // Some(DeviceNumber::new(Major::TTY_MAJOR, 0)), 27952da9a59SGnoCiYeH // ), 28052da9a59SGnoCiYeH // 1, 28152da9a59SGnoCiYeH // )?; 28252da9a59SGnoCiYeH 28352da9a59SGnoCiYeH // CharDevOps::register_chardev_region(DeviceNumber::new(Major::TTY_MAJOR, 0), 1, "/dev/vc/0")?; 28452da9a59SGnoCiYeH device_register(vc0.clone())?; 28552da9a59SGnoCiYeH devfs_register("vc0", vc0)?; 28652da9a59SGnoCiYeH 28752da9a59SGnoCiYeH // vcs_init? 28852da9a59SGnoCiYeH 28952da9a59SGnoCiYeH let console_driver = TtyDriver::new( 29052da9a59SGnoCiYeH MAX_NR_CONSOLES, 29152da9a59SGnoCiYeH "tty", 29252da9a59SGnoCiYeH 1, 29352da9a59SGnoCiYeH Major::TTY_MAJOR, 29452da9a59SGnoCiYeH 0, 29552da9a59SGnoCiYeH TtyDriverType::Console, 296*b5b571e0SLoGin *TTY_STD_TERMIOS, 29752da9a59SGnoCiYeH Arc::new(TtyConsoleDriverInner::new()?), 29852da9a59SGnoCiYeH ); 29952da9a59SGnoCiYeH 30052da9a59SGnoCiYeH TtyDriverManager::tty_register_driver(console_driver)?; 30152da9a59SGnoCiYeH 30252da9a59SGnoCiYeH CURRENT_VCNUM.store(0, Ordering::SeqCst); 30352da9a59SGnoCiYeH 30452da9a59SGnoCiYeH // 初始化键盘? 30552da9a59SGnoCiYeH 30652da9a59SGnoCiYeH // TODO: 为vc 30752da9a59SGnoCiYeH 30852da9a59SGnoCiYeH Ok(()) 30952da9a59SGnoCiYeH } 310