xref: /DragonOS/kernel/src/driver/video/fbdev/base/fbcon/mod.rs (revision 28fe4ad2a0b0d8b5abf1f0cb402b1c3204b42242)
152da9a59SGnoCiYeH use alloc::{
252da9a59SGnoCiYeH     string::{String, ToString},
352da9a59SGnoCiYeH     sync::{Arc, Weak},
452da9a59SGnoCiYeH     vec::Vec,
552da9a59SGnoCiYeH };
62eab6dd7S曾俊 use log::warn;
752da9a59SGnoCiYeH use system_error::SystemError;
852da9a59SGnoCiYeH 
952da9a59SGnoCiYeH use crate::{
1052da9a59SGnoCiYeH     driver::{
1152da9a59SGnoCiYeH         base::{
1252da9a59SGnoCiYeH             class::Class,
13*28fe4ad2S黄铭涛             device::{
14*28fe4ad2S黄铭涛                 bus::Bus, device_manager, driver::Driver, Device, DeviceCommonData, DeviceType,
15*28fe4ad2S黄铭涛                 IdTable,
16*28fe4ad2S黄铭涛             },
17*28fe4ad2S黄铭涛             kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
1852da9a59SGnoCiYeH             kset::KSet,
1952da9a59SGnoCiYeH         },
2052da9a59SGnoCiYeH         tty::virtual_terminal::virtual_console::{CursorOperation, VcCursor, VirtualConsoleData},
2152da9a59SGnoCiYeH     },
2252da9a59SGnoCiYeH     filesystem::{
2352da9a59SGnoCiYeH         kernfs::KernFSInode,
2452da9a59SGnoCiYeH         sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport},
2552da9a59SGnoCiYeH         vfs::syscall::ModeType,
2652da9a59SGnoCiYeH     },
2752da9a59SGnoCiYeH     libs::{
2852da9a59SGnoCiYeH         rwlock::{RwLockReadGuard, RwLockWriteGuard},
2952da9a59SGnoCiYeH         spinlock::{SpinLock, SpinLockGuard},
3052da9a59SGnoCiYeH     },
3152da9a59SGnoCiYeH };
3252da9a59SGnoCiYeH 
3352da9a59SGnoCiYeH use super::{fbmem::sys_class_graphics_instance, FbCursor, ScrollMode};
3452da9a59SGnoCiYeH 
3552da9a59SGnoCiYeH pub mod framebuffer_console;
3652da9a59SGnoCiYeH 
3752da9a59SGnoCiYeH /// framebuffer console设备管理器实例
3852da9a59SGnoCiYeH static mut FB_CONSOLE_MANAGER: Option<FbConsoleManager> = None;
3952da9a59SGnoCiYeH 
fb_console_manager() -> &'static FbConsoleManager4052da9a59SGnoCiYeH pub fn fb_console_manager() -> &'static FbConsoleManager {
4152da9a59SGnoCiYeH     unsafe { FB_CONSOLE_MANAGER.as_ref().unwrap() }
4252da9a59SGnoCiYeH }
4352da9a59SGnoCiYeH 
4452da9a59SGnoCiYeH /// 初始化framebuffer console
fb_console_init() -> Result<(), SystemError>4552da9a59SGnoCiYeH pub(super) fn fb_console_init() -> Result<(), SystemError> {
4652da9a59SGnoCiYeH     // todo: 对全局的console信号量加锁(linux中是console_lock)
4752da9a59SGnoCiYeH 
4852da9a59SGnoCiYeH     let fbcon_device: Arc<FbConsoleDevice> = FbConsoleDevice::new();
4952da9a59SGnoCiYeH 
5052da9a59SGnoCiYeH     {
5152da9a59SGnoCiYeH         let fbcon_manager = FbConsoleManager::new(fbcon_device.clone());
5252da9a59SGnoCiYeH         unsafe { FB_CONSOLE_MANAGER = Some(fbcon_manager) };
5352da9a59SGnoCiYeH     }
5452da9a59SGnoCiYeH 
5552da9a59SGnoCiYeH     device_manager().register(fbcon_device.clone() as Arc<dyn Device>)?;
5652da9a59SGnoCiYeH     fb_console_manager().init_device()?;
5752da9a59SGnoCiYeH 
5852da9a59SGnoCiYeH     return Ok(());
5952da9a59SGnoCiYeH }
6052da9a59SGnoCiYeH 
6152da9a59SGnoCiYeH /// framebuffer console设备管理器
6252da9a59SGnoCiYeH #[derive(Debug)]
6352da9a59SGnoCiYeH pub struct FbConsoleManager {
6452da9a59SGnoCiYeH     _inner: SpinLock<InnerFbConsoleManager>,
6552da9a59SGnoCiYeH     /// framebuffer console设备实例
6652da9a59SGnoCiYeH     /// (对应`/sys/class/graphics/fbcon`)
6752da9a59SGnoCiYeH     device: Arc<FbConsoleDevice>,
6852da9a59SGnoCiYeH }
6952da9a59SGnoCiYeH 
7052da9a59SGnoCiYeH impl FbConsoleManager {
new(device: Arc<FbConsoleDevice>) -> Self7152da9a59SGnoCiYeH     pub fn new(device: Arc<FbConsoleDevice>) -> Self {
7252da9a59SGnoCiYeH         return Self {
7352da9a59SGnoCiYeH             _inner: SpinLock::new(InnerFbConsoleManager {}),
7452da9a59SGnoCiYeH             device,
7552da9a59SGnoCiYeH         };
7652da9a59SGnoCiYeH     }
7752da9a59SGnoCiYeH 
7852da9a59SGnoCiYeH     #[allow(dead_code)]
7952da9a59SGnoCiYeH     #[inline(always)]
device(&self) -> &Arc<FbConsoleDevice>8052da9a59SGnoCiYeH     pub fn device(&self) -> &Arc<FbConsoleDevice> {
8152da9a59SGnoCiYeH         &self.device
8252da9a59SGnoCiYeH     }
8352da9a59SGnoCiYeH 
8452da9a59SGnoCiYeH     /// 初始化设备
init_device(&self) -> Result<(), SystemError>8552da9a59SGnoCiYeH     fn init_device(&self) -> Result<(), SystemError> {
8652da9a59SGnoCiYeH         return Ok(()); // todo
8752da9a59SGnoCiYeH     }
8852da9a59SGnoCiYeH }
8952da9a59SGnoCiYeH 
9052da9a59SGnoCiYeH #[derive(Debug)]
9152da9a59SGnoCiYeH struct InnerFbConsoleManager {}
9252da9a59SGnoCiYeH 
9352da9a59SGnoCiYeH #[derive(Debug)]
9452da9a59SGnoCiYeH struct InnerFbConsoleDevice {
95*28fe4ad2S黄铭涛     device_common: DeviceCommonData,
96*28fe4ad2S黄铭涛     kobject_common: KObjectCommonData,
9752da9a59SGnoCiYeH }
9852da9a59SGnoCiYeH 
9952da9a59SGnoCiYeH /// `/sys/class/graphics/fbcon`代表的 framebuffer console 设备
10052da9a59SGnoCiYeH #[derive(Debug)]
10152da9a59SGnoCiYeH #[cast_to([sync] Device)]
10252da9a59SGnoCiYeH pub struct FbConsoleDevice {
10352da9a59SGnoCiYeH     inner: SpinLock<InnerFbConsoleDevice>,
10452da9a59SGnoCiYeH     kobj_state: LockedKObjectState,
10552da9a59SGnoCiYeH }
10652da9a59SGnoCiYeH 
10752da9a59SGnoCiYeH impl FbConsoleDevice {
10852da9a59SGnoCiYeH     const NAME: &'static str = "fbcon";
10952da9a59SGnoCiYeH 
new() -> Arc<Self>11052da9a59SGnoCiYeH     pub fn new() -> Arc<Self> {
11152da9a59SGnoCiYeH         return Arc::new(Self {
11252da9a59SGnoCiYeH             inner: SpinLock::new(InnerFbConsoleDevice {
113*28fe4ad2S黄铭涛                 device_common: DeviceCommonData::default(),
114*28fe4ad2S黄铭涛                 kobject_common: KObjectCommonData::default(),
11552da9a59SGnoCiYeH             }),
11652da9a59SGnoCiYeH             kobj_state: LockedKObjectState::new(None),
11752da9a59SGnoCiYeH         });
11852da9a59SGnoCiYeH     }
119*28fe4ad2S黄铭涛 
inner(&self) -> SpinLockGuard<InnerFbConsoleDevice>120*28fe4ad2S黄铭涛     fn inner(&self) -> SpinLockGuard<InnerFbConsoleDevice> {
121*28fe4ad2S黄铭涛         self.inner.lock()
122*28fe4ad2S黄铭涛     }
12352da9a59SGnoCiYeH }
12452da9a59SGnoCiYeH 
12552da9a59SGnoCiYeH impl KObject for FbConsoleDevice {
as_any_ref(&self) -> &dyn core::any::Any12652da9a59SGnoCiYeH     fn as_any_ref(&self) -> &dyn core::any::Any {
12752da9a59SGnoCiYeH         self
12852da9a59SGnoCiYeH     }
12952da9a59SGnoCiYeH 
set_inode(&self, inode: Option<Arc<KernFSInode>>)13052da9a59SGnoCiYeH     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
131*28fe4ad2S黄铭涛         self.inner().kobject_common.kern_inode = inode;
13252da9a59SGnoCiYeH     }
13352da9a59SGnoCiYeH 
inode(&self) -> Option<Arc<KernFSInode>>13452da9a59SGnoCiYeH     fn inode(&self) -> Option<Arc<KernFSInode>> {
135*28fe4ad2S黄铭涛         self.inner().kobject_common.kern_inode.clone()
13652da9a59SGnoCiYeH     }
13752da9a59SGnoCiYeH 
parent(&self) -> Option<Weak<dyn KObject>>13852da9a59SGnoCiYeH     fn parent(&self) -> Option<Weak<dyn KObject>> {
139*28fe4ad2S黄铭涛         self.inner().kobject_common.parent.clone()
14052da9a59SGnoCiYeH     }
14152da9a59SGnoCiYeH 
set_parent(&self, parent: Option<Weak<dyn KObject>>)14252da9a59SGnoCiYeH     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
143*28fe4ad2S黄铭涛         self.inner().kobject_common.parent = parent;
14452da9a59SGnoCiYeH     }
14552da9a59SGnoCiYeH 
kset(&self) -> Option<Arc<KSet>>14652da9a59SGnoCiYeH     fn kset(&self) -> Option<Arc<KSet>> {
147*28fe4ad2S黄铭涛         self.inner().kobject_common.kset.clone()
14852da9a59SGnoCiYeH     }
14952da9a59SGnoCiYeH 
set_kset(&self, kset: Option<Arc<KSet>>)15052da9a59SGnoCiYeH     fn set_kset(&self, kset: Option<Arc<KSet>>) {
151*28fe4ad2S黄铭涛         self.inner().kobject_common.kset = kset;
15252da9a59SGnoCiYeH     }
15352da9a59SGnoCiYeH 
kobj_type(&self) -> Option<&'static dyn KObjType>15452da9a59SGnoCiYeH     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
155*28fe4ad2S黄铭涛         self.inner().kobject_common.kobj_type
15652da9a59SGnoCiYeH     }
15752da9a59SGnoCiYeH 
set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)15852da9a59SGnoCiYeH     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
159*28fe4ad2S黄铭涛         self.inner().kobject_common.kobj_type = ktype;
16052da9a59SGnoCiYeH     }
16152da9a59SGnoCiYeH 
name(&self) -> String16252da9a59SGnoCiYeH     fn name(&self) -> String {
16352da9a59SGnoCiYeH         Self::NAME.to_string()
16452da9a59SGnoCiYeH     }
16552da9a59SGnoCiYeH 
set_name(&self, _name: String)16652da9a59SGnoCiYeH     fn set_name(&self, _name: String) {
16752da9a59SGnoCiYeH         // 不允许修改
1682eab6dd7S曾俊         warn!("fbcon name can not be changed");
16952da9a59SGnoCiYeH     }
17052da9a59SGnoCiYeH 
kobj_state(&self) -> RwLockReadGuard<KObjectState>17152da9a59SGnoCiYeH     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
17252da9a59SGnoCiYeH         self.kobj_state.read()
17352da9a59SGnoCiYeH     }
17452da9a59SGnoCiYeH 
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>17552da9a59SGnoCiYeH     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
17652da9a59SGnoCiYeH         self.kobj_state.write()
17752da9a59SGnoCiYeH     }
17852da9a59SGnoCiYeH 
set_kobj_state(&self, state: KObjectState)17952da9a59SGnoCiYeH     fn set_kobj_state(&self, state: KObjectState) {
18052da9a59SGnoCiYeH         *self.kobj_state.write() = state;
18152da9a59SGnoCiYeH     }
18252da9a59SGnoCiYeH }
18352da9a59SGnoCiYeH impl Device for FbConsoleDevice {
dev_type(&self) -> DeviceType18452da9a59SGnoCiYeH     fn dev_type(&self) -> DeviceType {
18552da9a59SGnoCiYeH         DeviceType::Char
18652da9a59SGnoCiYeH     }
18752da9a59SGnoCiYeH 
id_table(&self) -> IdTable18852da9a59SGnoCiYeH     fn id_table(&self) -> IdTable {
18952da9a59SGnoCiYeH         IdTable::new(Self::NAME.to_string(), None)
19052da9a59SGnoCiYeH     }
19152da9a59SGnoCiYeH 
set_bus(&self, bus: Option<Weak<dyn Bus>>)19252da9a59SGnoCiYeH     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
193*28fe4ad2S黄铭涛         self.inner().device_common.bus = bus;
19452da9a59SGnoCiYeH     }
19552da9a59SGnoCiYeH 
bus(&self) -> Option<Weak<dyn Bus>>19652da9a59SGnoCiYeH     fn bus(&self) -> Option<Weak<dyn Bus>> {
197*28fe4ad2S黄铭涛         self.inner().device_common.bus.clone()
19852da9a59SGnoCiYeH     }
19952da9a59SGnoCiYeH 
set_class(&self, _class: Option<Weak<dyn Class>>)2004256da7fSLoGin     fn set_class(&self, _class: Option<Weak<dyn Class>>) {
20152da9a59SGnoCiYeH         // 不允许修改
2022eab6dd7S曾俊         warn!("fbcon's class can not be changed");
20352da9a59SGnoCiYeH     }
20452da9a59SGnoCiYeH 
class(&self) -> Option<Arc<dyn Class>>20552da9a59SGnoCiYeH     fn class(&self) -> Option<Arc<dyn Class>> {
20652da9a59SGnoCiYeH         sys_class_graphics_instance().map(|ins| ins.clone() as Arc<dyn Class>)
20752da9a59SGnoCiYeH     }
20852da9a59SGnoCiYeH 
driver(&self) -> Option<Arc<dyn Driver>>20952da9a59SGnoCiYeH     fn driver(&self) -> Option<Arc<dyn Driver>> {
210*28fe4ad2S黄铭涛         self.inner()
211*28fe4ad2S黄铭涛             .device_common
21252da9a59SGnoCiYeH             .driver
21352da9a59SGnoCiYeH             .clone()
21452da9a59SGnoCiYeH             .and_then(|driver| driver.upgrade())
21552da9a59SGnoCiYeH     }
21652da9a59SGnoCiYeH 
set_driver(&self, driver: Option<Weak<dyn Driver>>)21752da9a59SGnoCiYeH     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
218*28fe4ad2S黄铭涛         self.inner().device_common.driver = driver;
21952da9a59SGnoCiYeH     }
22052da9a59SGnoCiYeH 
is_dead(&self) -> bool22152da9a59SGnoCiYeH     fn is_dead(&self) -> bool {
222*28fe4ad2S黄铭涛         self.inner().device_common.dead
22352da9a59SGnoCiYeH     }
22452da9a59SGnoCiYeH 
can_match(&self) -> bool22552da9a59SGnoCiYeH     fn can_match(&self) -> bool {
226*28fe4ad2S黄铭涛         self.inner().device_common.can_match
22752da9a59SGnoCiYeH     }
22852da9a59SGnoCiYeH 
set_can_match(&self, can_match: bool)229*28fe4ad2S黄铭涛     fn set_can_match(&self, can_match: bool) {
230*28fe4ad2S黄铭涛         self.inner().device_common.can_match = can_match;
23152da9a59SGnoCiYeH     }
23252da9a59SGnoCiYeH 
state_synced(&self) -> bool23352da9a59SGnoCiYeH     fn state_synced(&self) -> bool {
23452da9a59SGnoCiYeH         todo!()
23552da9a59SGnoCiYeH     }
23652da9a59SGnoCiYeH 
attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]>23752da9a59SGnoCiYeH     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> {
23852da9a59SGnoCiYeH         return Some(&[&AnonymousAttributeGroup]);
23952da9a59SGnoCiYeH     }
240*28fe4ad2S黄铭涛 
dev_parent(&self) -> Option<Weak<dyn Device>>241*28fe4ad2S黄铭涛     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
242*28fe4ad2S黄铭涛         self.inner().device_common.get_parent_weak_or_clear()
243*28fe4ad2S黄铭涛     }
244*28fe4ad2S黄铭涛 
set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>)245*28fe4ad2S黄铭涛     fn set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>) {
246*28fe4ad2S黄铭涛         self.inner().device_common.parent = dev_parent;
247*28fe4ad2S黄铭涛     }
24852da9a59SGnoCiYeH }
24952da9a59SGnoCiYeH 
25052da9a59SGnoCiYeH /// framebuffer console设备的匿名属性组
25152da9a59SGnoCiYeH #[derive(Debug)]
25252da9a59SGnoCiYeH struct AnonymousAttributeGroup;
25352da9a59SGnoCiYeH 
25452da9a59SGnoCiYeH impl AttributeGroup for AnonymousAttributeGroup {
name(&self) -> Option<&str>25552da9a59SGnoCiYeH     fn name(&self) -> Option<&str> {
25652da9a59SGnoCiYeH         None
25752da9a59SGnoCiYeH     }
25852da9a59SGnoCiYeH 
attrs(&self) -> &[&'static dyn Attribute]25952da9a59SGnoCiYeH     fn attrs(&self) -> &[&'static dyn Attribute] {
26052da9a59SGnoCiYeH         return &[&AttrRotate, &AttrRotateAll, &AttrCursorBlink];
26152da9a59SGnoCiYeH     }
26252da9a59SGnoCiYeH 
is_visible( &self, _kobj: Arc<dyn KObject>, attr: &'static dyn Attribute, ) -> Option<ModeType>26352da9a59SGnoCiYeH     fn is_visible(
26452da9a59SGnoCiYeH         &self,
26552da9a59SGnoCiYeH         _kobj: Arc<dyn KObject>,
26652da9a59SGnoCiYeH         attr: &'static dyn Attribute,
26752da9a59SGnoCiYeH     ) -> Option<ModeType> {
26852da9a59SGnoCiYeH         return Some(attr.mode());
26952da9a59SGnoCiYeH     }
27052da9a59SGnoCiYeH }
27152da9a59SGnoCiYeH 
27252da9a59SGnoCiYeH #[derive(Debug)]
27352da9a59SGnoCiYeH struct AttrRotate;
27452da9a59SGnoCiYeH 
27552da9a59SGnoCiYeH impl Attribute for AttrRotate {
name(&self) -> &str27652da9a59SGnoCiYeH     fn name(&self) -> &str {
27752da9a59SGnoCiYeH         "rotate"
27852da9a59SGnoCiYeH     }
27952da9a59SGnoCiYeH 
mode(&self) -> ModeType28052da9a59SGnoCiYeH     fn mode(&self) -> ModeType {
28152da9a59SGnoCiYeH         ModeType::S_IRUGO | ModeType::S_IWUSR
28252da9a59SGnoCiYeH     }
28352da9a59SGnoCiYeH 
support(&self) -> SysFSOpsSupport28452da9a59SGnoCiYeH     fn support(&self) -> SysFSOpsSupport {
28552da9a59SGnoCiYeH         SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE
28652da9a59SGnoCiYeH     }
28752da9a59SGnoCiYeH 
28852da9a59SGnoCiYeH     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbcon.c#3226
show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError>28952da9a59SGnoCiYeH     fn show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
2902eab6dd7S曾俊         warn!("fbcon rotate show not implemented");
29152da9a59SGnoCiYeH         return sysfs_emit_str(buf, "0\n");
29252da9a59SGnoCiYeH     }
29352da9a59SGnoCiYeH 
29452da9a59SGnoCiYeH     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbcon.c#3182
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>29552da9a59SGnoCiYeH     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
2962eab6dd7S曾俊         warn!("fbcon rotate store not implemented");
29752da9a59SGnoCiYeH         return Err(SystemError::ENOSYS);
29852da9a59SGnoCiYeH     }
29952da9a59SGnoCiYeH }
30052da9a59SGnoCiYeH 
30152da9a59SGnoCiYeH #[derive(Debug)]
30252da9a59SGnoCiYeH struct AttrRotateAll;
30352da9a59SGnoCiYeH 
30452da9a59SGnoCiYeH impl Attribute for AttrRotateAll {
name(&self) -> &str30552da9a59SGnoCiYeH     fn name(&self) -> &str {
30652da9a59SGnoCiYeH         "rotate_all"
30752da9a59SGnoCiYeH     }
30852da9a59SGnoCiYeH 
mode(&self) -> ModeType30952da9a59SGnoCiYeH     fn mode(&self) -> ModeType {
31052da9a59SGnoCiYeH         ModeType::S_IWUSR
31152da9a59SGnoCiYeH     }
31252da9a59SGnoCiYeH 
support(&self) -> SysFSOpsSupport31352da9a59SGnoCiYeH     fn support(&self) -> SysFSOpsSupport {
31452da9a59SGnoCiYeH         SysFSOpsSupport::ATTR_STORE
31552da9a59SGnoCiYeH     }
31652da9a59SGnoCiYeH 
31752da9a59SGnoCiYeH     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbcon.c#3204
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>31852da9a59SGnoCiYeH     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
3192eab6dd7S曾俊         warn!("fbcon rotate_all store not implemented");
32052da9a59SGnoCiYeH         return Err(SystemError::ENOSYS);
32152da9a59SGnoCiYeH     }
32252da9a59SGnoCiYeH }
32352da9a59SGnoCiYeH 
32452da9a59SGnoCiYeH #[derive(Debug)]
32552da9a59SGnoCiYeH struct AttrCursorBlink;
32652da9a59SGnoCiYeH 
32752da9a59SGnoCiYeH impl Attribute for AttrCursorBlink {
name(&self) -> &str32852da9a59SGnoCiYeH     fn name(&self) -> &str {
32952da9a59SGnoCiYeH         "cursor_blink"
33052da9a59SGnoCiYeH     }
33152da9a59SGnoCiYeH 
mode(&self) -> ModeType33252da9a59SGnoCiYeH     fn mode(&self) -> ModeType {
33352da9a59SGnoCiYeH         ModeType::S_IRUGO | ModeType::S_IWUSR
33452da9a59SGnoCiYeH     }
33552da9a59SGnoCiYeH 
support(&self) -> SysFSOpsSupport33652da9a59SGnoCiYeH     fn support(&self) -> SysFSOpsSupport {
33752da9a59SGnoCiYeH         SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE
33852da9a59SGnoCiYeH     }
33952da9a59SGnoCiYeH 
34052da9a59SGnoCiYeH     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbcon.c#3245
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>34152da9a59SGnoCiYeH     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
34252da9a59SGnoCiYeH         todo!()
34352da9a59SGnoCiYeH     }
34452da9a59SGnoCiYeH 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>34552da9a59SGnoCiYeH     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
34652da9a59SGnoCiYeH         todo!()
34752da9a59SGnoCiYeH     }
34852da9a59SGnoCiYeH }
34952da9a59SGnoCiYeH 
35052da9a59SGnoCiYeH #[derive(Debug, Default)]
351bd70d2d1SLoGin #[allow(dead_code)]
35252da9a59SGnoCiYeH pub struct FrameBufferConsoleData {
35352da9a59SGnoCiYeH     /// 光标闪烁间隔
35452da9a59SGnoCiYeH     pub cursor_blink_jiffies: i64,
35552da9a59SGnoCiYeH     /// 是否刷新光标
35652da9a59SGnoCiYeH     pub cursor_flash: bool,
35752da9a59SGnoCiYeH     pub display: FbConsoleDisplay,
35852da9a59SGnoCiYeH     /// 光标状态
35952da9a59SGnoCiYeH     pub cursor_state: FbCursor,
36052da9a59SGnoCiYeH     /// 重设光标?
36152da9a59SGnoCiYeH     pub cursor_reset: bool,
36252da9a59SGnoCiYeH     /// cursor 位图数据
36352da9a59SGnoCiYeH     pub cursor_data: Vec<u8>,
36452da9a59SGnoCiYeH }
36552da9a59SGnoCiYeH 
36652da9a59SGnoCiYeH pub trait FrameBufferConsole {
fbcon_data(&self) -> SpinLockGuard<FrameBufferConsoleData>36752da9a59SGnoCiYeH     fn fbcon_data(&self) -> SpinLockGuard<FrameBufferConsoleData>;
36852da9a59SGnoCiYeH 
36952da9a59SGnoCiYeH     /// ## 将位块移动到目标位置
37052da9a59SGnoCiYeH     /// 坐标均以字体为单位而不是pixel
37152da9a59SGnoCiYeH     /// ### 参数
37252da9a59SGnoCiYeH     /// ### sy: 起始位置的y坐标
37352da9a59SGnoCiYeH     /// ### sx: 起始位置的x坐标、
37452da9a59SGnoCiYeH     /// ### dy: 目标位置的y坐标
37552da9a59SGnoCiYeH     /// ### dx: 目标位置的x坐标
37652da9a59SGnoCiYeH     /// ### height: 位图高度
37752da9a59SGnoCiYeH     /// ### width: 位图宽度
378b5b571e0SLoGin     #[allow(clippy::too_many_arguments)]
bmove( &self, vc_data: &VirtualConsoleData, sy: i32, sx: i32, dy: i32, dx: i32, height: u32, width: u32, ) -> Result<(), SystemError>37952da9a59SGnoCiYeH     fn bmove(
38052da9a59SGnoCiYeH         &self,
38152da9a59SGnoCiYeH         vc_data: &VirtualConsoleData,
38252da9a59SGnoCiYeH         sy: i32,
38352da9a59SGnoCiYeH         sx: i32,
38452da9a59SGnoCiYeH         dy: i32,
38552da9a59SGnoCiYeH         dx: i32,
38652da9a59SGnoCiYeH         height: u32,
38752da9a59SGnoCiYeH         width: u32,
38852da9a59SGnoCiYeH     ) -> Result<(), SystemError>;
38952da9a59SGnoCiYeH 
39052da9a59SGnoCiYeH     /// ## 清除位图
39152da9a59SGnoCiYeH     ///
39252da9a59SGnoCiYeH     /// ### 参数
39352da9a59SGnoCiYeH     /// ### sy: 原位置的y坐标
39452da9a59SGnoCiYeH     /// ### sx: 原位置的x坐标、
39552da9a59SGnoCiYeH     /// ### height: 位图高度
39652da9a59SGnoCiYeH     /// ### width: 位图宽度
clear( &self, vc_data: &VirtualConsoleData, sy: u32, sx: u32, height: u32, width: u32, ) -> Result<(), SystemError>39752da9a59SGnoCiYeH     fn clear(
39852da9a59SGnoCiYeH         &self,
39952da9a59SGnoCiYeH         vc_data: &VirtualConsoleData,
40052da9a59SGnoCiYeH         sy: u32,
40152da9a59SGnoCiYeH         sx: u32,
40252da9a59SGnoCiYeH         height: u32,
40352da9a59SGnoCiYeH         width: u32,
40452da9a59SGnoCiYeH     ) -> Result<(), SystemError>;
40552da9a59SGnoCiYeH 
40652da9a59SGnoCiYeH     /// ## 显示字符串
40752da9a59SGnoCiYeH     ///
40852da9a59SGnoCiYeH     /// ### 参数
40952da9a59SGnoCiYeH     /// ### y: 起始位置y坐标
41052da9a59SGnoCiYeH     /// ### x: 起始位置的x坐标、
41152da9a59SGnoCiYeH     /// ### fg: 前景色
41252da9a59SGnoCiYeH     /// ### bg: 背景色
413b5b571e0SLoGin     #[allow(clippy::too_many_arguments)]
put_string( &self, vc_data: &VirtualConsoleData, data: &[u16], count: u32, y: u32, x: u32, fg: u32, bg: u32, ) -> Result<(), SystemError>41452da9a59SGnoCiYeH     fn put_string(
41552da9a59SGnoCiYeH         &self,
41652da9a59SGnoCiYeH         vc_data: &VirtualConsoleData,
41752da9a59SGnoCiYeH         data: &[u16],
41852da9a59SGnoCiYeH         count: u32,
41952da9a59SGnoCiYeH         y: u32,
42052da9a59SGnoCiYeH         x: u32,
42152da9a59SGnoCiYeH         fg: u32,
42252da9a59SGnoCiYeH         bg: u32,
42352da9a59SGnoCiYeH     ) -> Result<(), SystemError>;
42452da9a59SGnoCiYeH 
cursor(&self, vc_data: &VirtualConsoleData, op: CursorOperation, fg: u32, bg: u32)42552da9a59SGnoCiYeH     fn cursor(&self, vc_data: &VirtualConsoleData, op: CursorOperation, fg: u32, bg: u32);
42652da9a59SGnoCiYeH }
42752da9a59SGnoCiYeH 
42852da9a59SGnoCiYeH /// 表示 framebuffer 控制台与低级帧缓冲设备之间接口的数据结构
42952da9a59SGnoCiYeH #[derive(Debug, Default)]
43052da9a59SGnoCiYeH pub struct FbConsoleDisplay {
43152da9a59SGnoCiYeH     /// 硬件滚动的行数
43252da9a59SGnoCiYeH     pub yscroll: u32,
43352da9a59SGnoCiYeH     /// 光标
43452da9a59SGnoCiYeH     pub cursor_shape: VcCursor,
43552da9a59SGnoCiYeH     /// 滚动模式
43652da9a59SGnoCiYeH     pub scroll_mode: ScrollMode,
43752da9a59SGnoCiYeH     virt_rows: u32,
43852da9a59SGnoCiYeH }
43952da9a59SGnoCiYeH 
44052da9a59SGnoCiYeH impl FbConsoleDisplay {
real_y(&self, mut ypos: u32) -> u3244152da9a59SGnoCiYeH     pub fn real_y(&self, mut ypos: u32) -> u32 {
44252da9a59SGnoCiYeH         let rows = self.virt_rows;
44352da9a59SGnoCiYeH         ypos += self.yscroll;
44452da9a59SGnoCiYeH         if ypos < rows {
44552da9a59SGnoCiYeH             return ypos;
44652da9a59SGnoCiYeH         } else {
44752da9a59SGnoCiYeH             return ypos - rows;
44852da9a59SGnoCiYeH         }
44952da9a59SGnoCiYeH     }
45052da9a59SGnoCiYeH }
45152da9a59SGnoCiYeH 
45252da9a59SGnoCiYeH bitflags! {
45352da9a59SGnoCiYeH     pub struct FbConAttr:u8 {
45452da9a59SGnoCiYeH         const UNDERLINE = 1;
45552da9a59SGnoCiYeH         const REVERSE   = 2;
45652da9a59SGnoCiYeH         const BOLD      = 4;
45752da9a59SGnoCiYeH     }
45852da9a59SGnoCiYeH }
45952da9a59SGnoCiYeH 
46052da9a59SGnoCiYeH impl FbConAttr {
get_attr(c: u16, color_depth: u32) -> Self46152da9a59SGnoCiYeH     pub fn get_attr(c: u16, color_depth: u32) -> Self {
46252da9a59SGnoCiYeH         let mut attr = Self::empty();
46352da9a59SGnoCiYeH         if color_depth == 1 {
46452da9a59SGnoCiYeH             if Self::underline(c) {
46552da9a59SGnoCiYeH                 attr.insert(Self::UNDERLINE);
46652da9a59SGnoCiYeH             }
46752da9a59SGnoCiYeH             if Self::reverse(c) {
46852da9a59SGnoCiYeH                 attr.intersects(Self::REVERSE);
46952da9a59SGnoCiYeH             }
47052da9a59SGnoCiYeH             if Self::blod(c) {
47152da9a59SGnoCiYeH                 attr.insert(Self::BOLD);
47252da9a59SGnoCiYeH             }
47352da9a59SGnoCiYeH         }
47452da9a59SGnoCiYeH         attr
47552da9a59SGnoCiYeH     }
47652da9a59SGnoCiYeH 
update_attr(&self, dst: &mut [u8], src: &[u8], vc_data: &VirtualConsoleData)47752da9a59SGnoCiYeH     pub fn update_attr(&self, dst: &mut [u8], src: &[u8], vc_data: &VirtualConsoleData) {
47852da9a59SGnoCiYeH         let mut offset = if vc_data.font.height < 10 { 1 } else { 2 } as usize;
47952da9a59SGnoCiYeH 
48052da9a59SGnoCiYeH         let width = (vc_data.font.width + 7) / 8;
48152da9a59SGnoCiYeH         let cellsize = (vc_data.font.height * width) as usize;
48252da9a59SGnoCiYeH 
48352da9a59SGnoCiYeH         // 大于offset的部分就是下划线
48452da9a59SGnoCiYeH         offset = cellsize - (offset * width as usize);
48552da9a59SGnoCiYeH         for i in 0..cellsize {
48652da9a59SGnoCiYeH             let mut c = src[i];
48752da9a59SGnoCiYeH             if self.contains(Self::UNDERLINE) && i >= offset {
48852da9a59SGnoCiYeH                 // 下划线
48952da9a59SGnoCiYeH                 c = 0xff;
49052da9a59SGnoCiYeH             }
49152da9a59SGnoCiYeH             if self.contains(Self::BOLD) {
49252da9a59SGnoCiYeH                 c |= c >> 1;
49352da9a59SGnoCiYeH             }
49452da9a59SGnoCiYeH             if self.contains(Self::REVERSE) {
49552da9a59SGnoCiYeH                 c = !c;
49652da9a59SGnoCiYeH             }
49752da9a59SGnoCiYeH 
49852da9a59SGnoCiYeH             dst[i] = c;
49952da9a59SGnoCiYeH         }
50052da9a59SGnoCiYeH     }
50152da9a59SGnoCiYeH 
underline(c: u16) -> bool50252da9a59SGnoCiYeH     pub fn underline(c: u16) -> bool {
50352da9a59SGnoCiYeH         c & 0x400 != 0
50452da9a59SGnoCiYeH     }
50552da9a59SGnoCiYeH 
blod(c: u16) -> bool50652da9a59SGnoCiYeH     pub fn blod(c: u16) -> bool {
50752da9a59SGnoCiYeH         c & 0x200 != 0
50852da9a59SGnoCiYeH     }
50952da9a59SGnoCiYeH 
reverse(c: u16) -> bool51052da9a59SGnoCiYeH     pub fn reverse(c: u16) -> bool {
51152da9a59SGnoCiYeH         c & 0x800 != 0
51252da9a59SGnoCiYeH     }
51352da9a59SGnoCiYeH }
514