xref: /DragonOS/kernel/src/libs/lib_ui/textui.rs (revision a0c98cd4df88474f2efd927930862df50e016b73)
1abe3a6eaShanjiezhou use crate::{
21496ba7bSLoGin     driver::{
3a03c4f9dSLoGin         tty::serial::serial8250::send_to_default_serial8250_port, video::video_refresh_manager,
41496ba7bSLoGin     },
51496ba7bSLoGin     kdebug, kinfo,
61496ba7bSLoGin     libs::{
71496ba7bSLoGin         lib_ui::font::FONT_8x16,
81496ba7bSLoGin         rwlock::RwLock,
91496ba7bSLoGin         spinlock::{SpinLock, SpinLockGuard},
101496ba7bSLoGin     },
11abe3a6eaShanjiezhou     syscall::SystemError,
12abe3a6eaShanjiezhou };
13abe3a6eaShanjiezhou use alloc::{boxed::Box, collections::LinkedList, string::ToString};
14abe3a6eaShanjiezhou use alloc::{sync::Arc, vec::Vec};
15abe3a6eaShanjiezhou use core::{
16abe3a6eaShanjiezhou     fmt::Debug,
17abe3a6eaShanjiezhou     intrinsics::unlikely,
18abe3a6eaShanjiezhou     ops::{Add, AddAssign, Sub},
19abe3a6eaShanjiezhou     sync::atomic::{AtomicBool, AtomicI32, AtomicU32, Ordering},
20abe3a6eaShanjiezhou };
21abe3a6eaShanjiezhou 
22abe3a6eaShanjiezhou use super::{
23abe3a6eaShanjiezhou     screen_manager::{
241496ba7bSLoGin         scm_register, ScmBuffer, ScmBufferInfo, ScmFramworkType, ScmUiFramework,
251496ba7bSLoGin         ScmUiFrameworkMetadata,
26abe3a6eaShanjiezhou     },
27abe3a6eaShanjiezhou     textui_no_alloc::no_init_textui_putchar_window,
28abe3a6eaShanjiezhou };
29abe3a6eaShanjiezhou 
30abe3a6eaShanjiezhou /// 声明全局的TEXTUI_FRAMEWORK
311496ba7bSLoGin static mut __TEXTUI_FRAMEWORK: Option<Arc<TextUiFramework>> = None;
32abe3a6eaShanjiezhou 
33abe3a6eaShanjiezhou /// 每个字符的宽度和高度(像素)
34abe3a6eaShanjiezhou pub const TEXTUI_CHAR_WIDTH: u32 = 8;
35abe3a6eaShanjiezhou 
36abe3a6eaShanjiezhou pub const TEXTUI_CHAR_HEIGHT: u32 = 16;
37abe3a6eaShanjiezhou 
38abe3a6eaShanjiezhou pub static mut TEXTUI_IS_INIT: bool = false;
39abe3a6eaShanjiezhou 
40abe3a6eaShanjiezhou pub static ENABLE_PUT_TO_WINDOW: AtomicBool = AtomicBool::new(true);
41abe3a6eaShanjiezhou 
42abe3a6eaShanjiezhou /// 获取TEXTUI_FRAMEWORK的可变实例
431496ba7bSLoGin pub fn textui_framework() -> Arc<TextUiFramework> {
441496ba7bSLoGin     unsafe {
451496ba7bSLoGin         return __TEXTUI_FRAMEWORK
461496ba7bSLoGin             .as_ref()
471496ba7bSLoGin             .expect("Textui framework has not been initialized yet!")
481496ba7bSLoGin             .clone();
49abe3a6eaShanjiezhou     }
501496ba7bSLoGin }
511496ba7bSLoGin 
52abe3a6eaShanjiezhou /// 初始化TEXTUI_FRAMEWORK
53abe3a6eaShanjiezhou pub unsafe fn textui_framwork_init() {
54abe3a6eaShanjiezhou     if __TEXTUI_FRAMEWORK.is_none() {
55abe3a6eaShanjiezhou         kinfo!("textuiframework init");
56abe3a6eaShanjiezhou         let metadata = ScmUiFrameworkMetadata::new("TextUI".to_string(), ScmFramworkType::Text);
571496ba7bSLoGin         kdebug!("textui metadata: {:?}", metadata);
58abe3a6eaShanjiezhou         // 为textui框架生成第一个窗口
591496ba7bSLoGin         let vlines_num = (metadata.buf_info().height() / TEXTUI_CHAR_HEIGHT) as usize;
60abe3a6eaShanjiezhou 
611496ba7bSLoGin         let chars_num = (metadata.buf_info().width() / TEXTUI_CHAR_WIDTH) as usize;
62abe3a6eaShanjiezhou 
63abe3a6eaShanjiezhou         let initial_window = TextuiWindow::new(
64abe3a6eaShanjiezhou             WindowFlag::TEXTUI_CHROMATIC,
65abe3a6eaShanjiezhou             vlines_num as i32,
66abe3a6eaShanjiezhou             chars_num as i32,
67abe3a6eaShanjiezhou         );
68abe3a6eaShanjiezhou 
69abe3a6eaShanjiezhou         let current_window: Arc<SpinLock<TextuiWindow>> = Arc::new(SpinLock::new(initial_window));
70abe3a6eaShanjiezhou 
71abe3a6eaShanjiezhou         let default_window = current_window.clone();
72abe3a6eaShanjiezhou 
73abe3a6eaShanjiezhou         // 生成窗口链表,并把上面窗口添加进textui框架的窗口链表中
74abe3a6eaShanjiezhou         let window_list: Arc<SpinLock<LinkedList<Arc<SpinLock<TextuiWindow>>>>> =
75abe3a6eaShanjiezhou             Arc::new(SpinLock::new(LinkedList::new()));
76abe3a6eaShanjiezhou         window_list.lock().push_back(current_window.clone());
77abe3a6eaShanjiezhou 
781496ba7bSLoGin         __TEXTUI_FRAMEWORK = Some(Arc::new(TextUiFramework::new(
79abe3a6eaShanjiezhou             metadata,
80abe3a6eaShanjiezhou             window_list,
81abe3a6eaShanjiezhou             current_window,
82abe3a6eaShanjiezhou             default_window,
83abe3a6eaShanjiezhou         )));
841496ba7bSLoGin 
851496ba7bSLoGin         scm_register(textui_framework()).expect("register textui framework failed");
861496ba7bSLoGin         kdebug!("textui framework init success");
871496ba7bSLoGin 
88a03c4f9dSLoGin         send_to_default_serial8250_port("\ntext ui initialized\n\0".as_bytes());
891496ba7bSLoGin         unsafe { TEXTUI_IS_INIT = true };
90abe3a6eaShanjiezhou     } else {
91abe3a6eaShanjiezhou         panic!("Try to init TEXTUI_FRAMEWORK twice!");
92abe3a6eaShanjiezhou     }
93abe3a6eaShanjiezhou }
94abe3a6eaShanjiezhou // window标志位
95abe3a6eaShanjiezhou bitflags! {
96abe3a6eaShanjiezhou     pub struct WindowFlag: u8 {
97abe3a6eaShanjiezhou         // 采用彩色字符
98abe3a6eaShanjiezhou         const TEXTUI_CHROMATIC = 1 << 0;
99abe3a6eaShanjiezhou     }
100abe3a6eaShanjiezhou }
101abe3a6eaShanjiezhou 
102abe3a6eaShanjiezhou /**
103abe3a6eaShanjiezhou  * @brief 黑白字符对象
104abe3a6eaShanjiezhou  *
105abe3a6eaShanjiezhou  */
106abe3a6eaShanjiezhou #[derive(Clone, Debug)]
107abe3a6eaShanjiezhou struct TextuiCharNormal {
108abe3a6eaShanjiezhou     _data: u8,
109abe3a6eaShanjiezhou }
110abe3a6eaShanjiezhou 
111abe3a6eaShanjiezhou #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Default)]
112abe3a6eaShanjiezhou pub struct LineId(i32);
113abe3a6eaShanjiezhou impl LineId {
114abe3a6eaShanjiezhou     pub fn new(num: i32) -> Self {
115abe3a6eaShanjiezhou         LineId(num)
116abe3a6eaShanjiezhou     }
117abe3a6eaShanjiezhou 
118abe3a6eaShanjiezhou     pub fn check(&self, max: i32) -> bool {
119abe3a6eaShanjiezhou         self.0 < max && self.0 >= 0
120abe3a6eaShanjiezhou     }
121abe3a6eaShanjiezhou 
122abe3a6eaShanjiezhou     pub fn data(&self) -> i32 {
123abe3a6eaShanjiezhou         self.0
124abe3a6eaShanjiezhou     }
125abe3a6eaShanjiezhou }
126abe3a6eaShanjiezhou impl Add<i32> for LineId {
127abe3a6eaShanjiezhou     type Output = LineId;
128abe3a6eaShanjiezhou     fn add(self, rhs: i32) -> Self::Output {
129abe3a6eaShanjiezhou         LineId::new(self.0 + rhs)
130abe3a6eaShanjiezhou     }
131abe3a6eaShanjiezhou }
132abe3a6eaShanjiezhou impl Sub<i32> for LineId {
133abe3a6eaShanjiezhou     type Output = LineId;
134abe3a6eaShanjiezhou 
135abe3a6eaShanjiezhou     fn sub(self, rhs: i32) -> Self::Output {
136abe3a6eaShanjiezhou         LineId::new(self.0 - rhs)
137abe3a6eaShanjiezhou     }
138abe3a6eaShanjiezhou }
139abe3a6eaShanjiezhou 
140abe3a6eaShanjiezhou impl Into<i32> for LineId {
141abe3a6eaShanjiezhou     fn into(self) -> i32 {
142abe3a6eaShanjiezhou         self.0.clone()
143abe3a6eaShanjiezhou     }
144abe3a6eaShanjiezhou }
145abe3a6eaShanjiezhou impl Into<u32> for LineId {
146abe3a6eaShanjiezhou     fn into(self) -> u32 {
147abe3a6eaShanjiezhou         self.0.clone() as u32
148abe3a6eaShanjiezhou     }
149abe3a6eaShanjiezhou }
150abe3a6eaShanjiezhou impl Into<usize> for LineId {
151abe3a6eaShanjiezhou     fn into(self) -> usize {
152abe3a6eaShanjiezhou         self.0.clone() as usize
153abe3a6eaShanjiezhou     }
154abe3a6eaShanjiezhou }
155abe3a6eaShanjiezhou impl Sub<LineId> for LineId {
156abe3a6eaShanjiezhou     type Output = LineId;
157abe3a6eaShanjiezhou 
158abe3a6eaShanjiezhou     fn sub(mut self, rhs: LineId) -> Self::Output {
159abe3a6eaShanjiezhou         self.0 -= rhs.0;
160abe3a6eaShanjiezhou         return self;
161abe3a6eaShanjiezhou     }
162abe3a6eaShanjiezhou }
163abe3a6eaShanjiezhou impl AddAssign<LineId> for LineId {
164abe3a6eaShanjiezhou     fn add_assign(&mut self, rhs: LineId) {
165abe3a6eaShanjiezhou         self.0 += rhs.0;
166abe3a6eaShanjiezhou     }
167abe3a6eaShanjiezhou }
168abe3a6eaShanjiezhou #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Default)]
169abe3a6eaShanjiezhou pub struct LineIndex(i32);
170abe3a6eaShanjiezhou impl LineIndex {
171abe3a6eaShanjiezhou     pub fn new(num: i32) -> Self {
172abe3a6eaShanjiezhou         LineIndex(num)
173abe3a6eaShanjiezhou     }
174abe3a6eaShanjiezhou     pub fn check(&self, chars_per_line: i32) -> bool {
175abe3a6eaShanjiezhou         self.0 < chars_per_line && self.0 >= 0
176abe3a6eaShanjiezhou     }
177abe3a6eaShanjiezhou }
178abe3a6eaShanjiezhou impl Add<LineIndex> for LineIndex {
179abe3a6eaShanjiezhou     type Output = LineIndex;
180abe3a6eaShanjiezhou 
181abe3a6eaShanjiezhou     fn add(self, rhs: LineIndex) -> Self::Output {
182abe3a6eaShanjiezhou         LineIndex::new(self.0 + rhs.0)
183abe3a6eaShanjiezhou     }
184abe3a6eaShanjiezhou }
185abe3a6eaShanjiezhou impl Add<i32> for LineIndex {
186abe3a6eaShanjiezhou     // type Output = Self;
187abe3a6eaShanjiezhou     type Output = LineIndex;
188abe3a6eaShanjiezhou 
189abe3a6eaShanjiezhou     fn add(self, rhs: i32) -> Self::Output {
190abe3a6eaShanjiezhou         LineIndex::new(self.0 + rhs)
191abe3a6eaShanjiezhou     }
192abe3a6eaShanjiezhou }
193abe3a6eaShanjiezhou impl Sub<i32> for LineIndex {
194abe3a6eaShanjiezhou     type Output = LineIndex;
195abe3a6eaShanjiezhou 
196abe3a6eaShanjiezhou     fn sub(self, rhs: i32) -> Self::Output {
197abe3a6eaShanjiezhou         LineIndex::new(self.0 - rhs)
198abe3a6eaShanjiezhou     }
199abe3a6eaShanjiezhou }
200abe3a6eaShanjiezhou 
201abe3a6eaShanjiezhou impl Into<i32> for LineIndex {
202abe3a6eaShanjiezhou     fn into(self) -> i32 {
203abe3a6eaShanjiezhou         self.0.clone()
204abe3a6eaShanjiezhou     }
205abe3a6eaShanjiezhou }
206abe3a6eaShanjiezhou impl Into<u32> for LineIndex {
207abe3a6eaShanjiezhou     fn into(self) -> u32 {
208abe3a6eaShanjiezhou         self.0.clone() as u32
209abe3a6eaShanjiezhou     }
210abe3a6eaShanjiezhou }
211abe3a6eaShanjiezhou impl Into<usize> for LineIndex {
212abe3a6eaShanjiezhou     fn into(self) -> usize {
213abe3a6eaShanjiezhou         self.0.clone() as usize
214abe3a6eaShanjiezhou     }
215abe3a6eaShanjiezhou }
216abe3a6eaShanjiezhou #[derive(Copy, Clone, Debug)]
217abe3a6eaShanjiezhou pub struct FontColor(u32);
218abe3a6eaShanjiezhou #[allow(dead_code)]
219abe3a6eaShanjiezhou impl FontColor {
220abe3a6eaShanjiezhou     pub const BLUE: FontColor = FontColor::new(0, 0, 0xff);
221abe3a6eaShanjiezhou     pub const RED: FontColor = FontColor::new(0xff, 0, 0);
222abe3a6eaShanjiezhou     pub const GREEN: FontColor = FontColor::new(0, 0xff, 0);
223abe3a6eaShanjiezhou     pub const WHITE: FontColor = FontColor::new(0xff, 0xff, 0xff);
224abe3a6eaShanjiezhou     pub const BLACK: FontColor = FontColor::new(0, 0, 0);
225abe3a6eaShanjiezhou     pub const YELLOW: FontColor = FontColor::new(0xff, 0xff, 0);
226abe3a6eaShanjiezhou     pub const ORANGE: FontColor = FontColor::new(0xff, 0x80, 0);
227abe3a6eaShanjiezhou     pub const INDIGO: FontColor = FontColor::new(0x00, 0xff, 0xff);
228abe3a6eaShanjiezhou     pub const PURPLE: FontColor = FontColor::new(0x80, 0x00, 0xff);
229abe3a6eaShanjiezhou 
230abe3a6eaShanjiezhou     pub const fn new(r: u8, g: u8, b: u8) -> Self {
231abe3a6eaShanjiezhou         let val = ((r as u32) << 16) | ((g as u32) << 8) | (b as u32);
232abe3a6eaShanjiezhou         return FontColor(val & 0x00ffffff);
233abe3a6eaShanjiezhou     }
234abe3a6eaShanjiezhou }
235abe3a6eaShanjiezhou 
236abe3a6eaShanjiezhou impl From<u32> for FontColor {
237abe3a6eaShanjiezhou     fn from(value: u32) -> Self {
238abe3a6eaShanjiezhou         return Self(value & 0x00ffffff);
239abe3a6eaShanjiezhou     }
240abe3a6eaShanjiezhou }
241abe3a6eaShanjiezhou impl Into<usize> for FontColor {
242abe3a6eaShanjiezhou     fn into(self) -> usize {
243abe3a6eaShanjiezhou         self.0.clone() as usize
244abe3a6eaShanjiezhou     }
245abe3a6eaShanjiezhou }
246abe3a6eaShanjiezhou impl Into<u32> for FontColor {
247abe3a6eaShanjiezhou     fn into(self) -> u32 {
248abe3a6eaShanjiezhou         self.0.clone()
249abe3a6eaShanjiezhou     }
250abe3a6eaShanjiezhou }
251abe3a6eaShanjiezhou impl Into<u16> for FontColor {
252abe3a6eaShanjiezhou     fn into(self) -> u16 {
253abe3a6eaShanjiezhou         self.0.clone() as u16
254abe3a6eaShanjiezhou     }
255abe3a6eaShanjiezhou }
256abe3a6eaShanjiezhou impl Into<u64> for FontColor {
257abe3a6eaShanjiezhou     fn into(self) -> u64 {
258abe3a6eaShanjiezhou         self.0.clone() as u64
259abe3a6eaShanjiezhou     }
260abe3a6eaShanjiezhou }
261abe3a6eaShanjiezhou 
262abe3a6eaShanjiezhou /// 彩色字符对象
263abe3a6eaShanjiezhou 
264abe3a6eaShanjiezhou #[derive(Clone, Debug, Copy)]
265abe3a6eaShanjiezhou pub struct TextuiCharChromatic {
266abe3a6eaShanjiezhou     c: Option<char>,
267abe3a6eaShanjiezhou 
268abe3a6eaShanjiezhou     // 前景色
269abe3a6eaShanjiezhou     frcolor: FontColor, // rgb
270abe3a6eaShanjiezhou 
271abe3a6eaShanjiezhou     // 背景色
272abe3a6eaShanjiezhou     bkcolor: FontColor, // rgb
273abe3a6eaShanjiezhou }
274abe3a6eaShanjiezhou 
275abe3a6eaShanjiezhou #[derive(Debug)]
2761496ba7bSLoGin pub struct TextuiBuf<'a> {
2771496ba7bSLoGin     buf: Option<&'a mut [u32]>,
2781496ba7bSLoGin     guard: Option<SpinLockGuard<'a, Box<[u32]>>>,
2791496ba7bSLoGin }
280abe3a6eaShanjiezhou 
281abe3a6eaShanjiezhou impl TextuiBuf<'_> {
2821496ba7bSLoGin     pub fn new(buf: &mut ScmBufferInfo) -> TextuiBuf {
2831496ba7bSLoGin         let len = buf.buf_size() / 4;
2841496ba7bSLoGin 
2851496ba7bSLoGin         match &buf.buf {
2861496ba7bSLoGin             ScmBuffer::DeviceBuffer(vaddr) => {
2871496ba7bSLoGin                 return TextuiBuf {
2881496ba7bSLoGin                     buf: Some(unsafe {
2891496ba7bSLoGin                         core::slice::from_raw_parts_mut(vaddr.data() as *mut u32, len)
2901496ba7bSLoGin                     }),
2911496ba7bSLoGin                     guard: None,
2921496ba7bSLoGin                 };
2931496ba7bSLoGin             }
2941496ba7bSLoGin 
2951496ba7bSLoGin             ScmBuffer::DoubleBuffer(double_buffer) => {
2961496ba7bSLoGin                 let guard: SpinLockGuard<'_, Box<[u32]>> = double_buffer.lock();
2971496ba7bSLoGin 
2981496ba7bSLoGin                 return TextuiBuf {
2991496ba7bSLoGin                     buf: None,
3001496ba7bSLoGin                     guard: Some(guard),
3011496ba7bSLoGin                 };
3021496ba7bSLoGin             }
3031496ba7bSLoGin         }
3041496ba7bSLoGin     }
3051496ba7bSLoGin 
3061496ba7bSLoGin     pub fn buf_mut(&mut self) -> &mut [u32] {
3071496ba7bSLoGin         if let Some(buf) = &mut self.buf {
3081496ba7bSLoGin             return buf;
3091496ba7bSLoGin         } else {
3101496ba7bSLoGin             return self.guard.as_mut().unwrap().as_mut();
3111496ba7bSLoGin         }
312abe3a6eaShanjiezhou     }
313abe3a6eaShanjiezhou     pub fn put_color_in_pixel(&mut self, color: u32, index: usize) {
3141496ba7bSLoGin         let buf: &mut [u32] = self.buf_mut();
315abe3a6eaShanjiezhou         buf[index] = color;
316abe3a6eaShanjiezhou     }
317abe3a6eaShanjiezhou     pub fn get_index_of_next_line(now_index: usize) -> usize {
3181496ba7bSLoGin         textui_framework().metadata.read().buf_info().width() as usize + now_index
319abe3a6eaShanjiezhou     }
320abe3a6eaShanjiezhou     pub fn get_index_by_x_y(x: usize, y: usize) -> usize {
3211496ba7bSLoGin         textui_framework().metadata.read().buf_info().width() as usize * y + x
322abe3a6eaShanjiezhou     }
323abe3a6eaShanjiezhou     pub fn get_start_index_by_lineid_lineindex(lineid: LineId, lineindex: LineIndex) -> usize {
324abe3a6eaShanjiezhou         //   x 左上角列像素点位置
325abe3a6eaShanjiezhou         //   y 左上角行像素点位置
326abe3a6eaShanjiezhou         let index_x: u32 = lineindex.into();
327abe3a6eaShanjiezhou         let x: u32 = index_x * TEXTUI_CHAR_WIDTH;
328abe3a6eaShanjiezhou 
329abe3a6eaShanjiezhou         let id_y: u32 = lineid.into();
330abe3a6eaShanjiezhou         let y: u32 = id_y * TEXTUI_CHAR_HEIGHT;
331abe3a6eaShanjiezhou 
332abe3a6eaShanjiezhou         TextuiBuf::get_index_by_x_y(x as usize, y as usize)
333abe3a6eaShanjiezhou     }
334abe3a6eaShanjiezhou }
335abe3a6eaShanjiezhou 
336abe3a6eaShanjiezhou #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
337abe3a6eaShanjiezhou pub struct Font([u8; 16]);
338abe3a6eaShanjiezhou impl Font {
339abe3a6eaShanjiezhou     #[inline]
340abe3a6eaShanjiezhou     pub fn get_font(character: char) -> Font {
341abe3a6eaShanjiezhou         let x = FONT_8x16.char_map(character);
342abe3a6eaShanjiezhou 
343abe3a6eaShanjiezhou         let mut data = [0u8; 16];
344abe3a6eaShanjiezhou         data.copy_from_slice(x);
345abe3a6eaShanjiezhou         return Font(data);
346abe3a6eaShanjiezhou     }
347abe3a6eaShanjiezhou     pub fn is_frcolor(&self, height: usize, width: usize) -> bool {
348abe3a6eaShanjiezhou         let w = self.0[height];
349abe3a6eaShanjiezhou         let testbit = 1 << (8 - width);
350abe3a6eaShanjiezhou         w & testbit != 0
351abe3a6eaShanjiezhou     }
352abe3a6eaShanjiezhou }
353abe3a6eaShanjiezhou 
354abe3a6eaShanjiezhou impl TextuiCharChromatic {
355abe3a6eaShanjiezhou     pub fn new(c: Option<char>, frcolor: FontColor, bkcolor: FontColor) -> Self {
356abe3a6eaShanjiezhou         TextuiCharChromatic {
357abe3a6eaShanjiezhou             c,
358abe3a6eaShanjiezhou             frcolor,
359abe3a6eaShanjiezhou             bkcolor,
360abe3a6eaShanjiezhou         }
361abe3a6eaShanjiezhou     }
362abe3a6eaShanjiezhou 
363abe3a6eaShanjiezhou     /// 将该字符对象输出到缓冲区
364abe3a6eaShanjiezhou     /// ## 参数
365abe3a6eaShanjiezhou     /// -line_id 要放入的真实行号
366abe3a6eaShanjiezhou     /// -index 要放入的真实列号
367abe3a6eaShanjiezhou     pub fn textui_refresh_character(
368abe3a6eaShanjiezhou         &self,
369abe3a6eaShanjiezhou         lineid: LineId,
370abe3a6eaShanjiezhou         lineindex: LineIndex,
371abe3a6eaShanjiezhou     ) -> Result<i32, SystemError> {
372abe3a6eaShanjiezhou         // 找到要渲染的字符的像素点数据
373abe3a6eaShanjiezhou 
374abe3a6eaShanjiezhou         let font: Font = Font::get_font(self.c.unwrap_or(' '));
375abe3a6eaShanjiezhou 
376abe3a6eaShanjiezhou         let mut count = TextuiBuf::get_start_index_by_lineid_lineindex(lineid, lineindex);
377abe3a6eaShanjiezhou 
3781496ba7bSLoGin         let mut _binding = textui_framework().metadata.read().buf_info();
3791496ba7bSLoGin 
3801496ba7bSLoGin         let mut buf = TextuiBuf::new(&mut _binding);
3811496ba7bSLoGin 
382abe3a6eaShanjiezhou         // 在缓冲区画出一个字体,每个字体有TEXTUI_CHAR_HEIGHT行,TEXTUI_CHAR_WIDTH列个像素点
383abe3a6eaShanjiezhou         for i in 0..TEXTUI_CHAR_HEIGHT {
384abe3a6eaShanjiezhou             let start = count;
385abe3a6eaShanjiezhou             for j in 0..TEXTUI_CHAR_WIDTH {
386abe3a6eaShanjiezhou                 if font.is_frcolor(i as usize, j as usize) {
387abe3a6eaShanjiezhou                     // 字,显示前景色
388abe3a6eaShanjiezhou                     buf.put_color_in_pixel(self.frcolor.into(), count);
389abe3a6eaShanjiezhou                 } else {
390abe3a6eaShanjiezhou                     // 背景色
391abe3a6eaShanjiezhou                     buf.put_color_in_pixel(self.bkcolor.into(), count);
392abe3a6eaShanjiezhou                 }
393abe3a6eaShanjiezhou                 count += 1;
394abe3a6eaShanjiezhou             }
395abe3a6eaShanjiezhou             count = TextuiBuf::get_index_of_next_line(start);
396abe3a6eaShanjiezhou         }
397abe3a6eaShanjiezhou 
398abe3a6eaShanjiezhou         return Ok(0);
399abe3a6eaShanjiezhou     }
400abe3a6eaShanjiezhou 
401abe3a6eaShanjiezhou     pub fn no_init_textui_render_chromatic(&self, lineid: LineId, lineindex: LineIndex) {
402abe3a6eaShanjiezhou         // 找到要渲染的字符的像素点数据
403abe3a6eaShanjiezhou         let font = Font::get_font(self.c.unwrap_or(' '));
404abe3a6eaShanjiezhou 
405abe3a6eaShanjiezhou         //   x 左上角列像素点位置
406abe3a6eaShanjiezhou         //   y 左上角行像素点位置
407abe3a6eaShanjiezhou         let index_x: u32 = lineindex.into();
408abe3a6eaShanjiezhou         let x: u32 = index_x * TEXTUI_CHAR_WIDTH;
409abe3a6eaShanjiezhou 
410abe3a6eaShanjiezhou         let id_y: u32 = lineid.into();
411abe3a6eaShanjiezhou         let y: u32 = id_y * TEXTUI_CHAR_HEIGHT;
4121496ba7bSLoGin 
4131496ba7bSLoGin         let buf_width = video_refresh_manager().device_buffer().width();
414abe3a6eaShanjiezhou         // 找到输入缓冲区的起始地址位置
4151496ba7bSLoGin         let buf_start =
4161496ba7bSLoGin             if let ScmBuffer::DeviceBuffer(vaddr) = video_refresh_manager().device_buffer().buf {
4171496ba7bSLoGin                 vaddr
4181496ba7bSLoGin             } else {
4191496ba7bSLoGin                 panic!("device buffer is not init");
4201496ba7bSLoGin             };
421abe3a6eaShanjiezhou 
422abe3a6eaShanjiezhou         let mut testbit: u32; // 用来测试特定行的某列是背景还是字体本身
423abe3a6eaShanjiezhou 
424abe3a6eaShanjiezhou         // 在缓冲区画出一个字体,每个字体有TEXTUI_CHAR_HEIGHT行,TEXTUI_CHAR_WIDTH列个像素点
425abe3a6eaShanjiezhou         for i in 0..TEXTUI_CHAR_HEIGHT {
426abe3a6eaShanjiezhou             // 计算出帧缓冲区每一行打印的起始位置的地址(起始位置+(y+i)*缓冲区的宽度+x)
427abe3a6eaShanjiezhou 
4281496ba7bSLoGin             let mut addr: *mut u32 =
4291496ba7bSLoGin                 (buf_start + buf_width as usize * 4 * (y as usize + i as usize) + 4 * x as usize)
4301496ba7bSLoGin                     .data() as *mut u32;
431abe3a6eaShanjiezhou 
432abe3a6eaShanjiezhou             testbit = 1 << (TEXTUI_CHAR_WIDTH + 1);
433abe3a6eaShanjiezhou 
434abe3a6eaShanjiezhou             for _j in 0..TEXTUI_CHAR_WIDTH {
435abe3a6eaShanjiezhou                 //从左往右逐个测试相应位
436abe3a6eaShanjiezhou                 testbit >>= 1;
437abe3a6eaShanjiezhou                 if (font.0[i as usize] & testbit as u8) != 0 {
438abe3a6eaShanjiezhou                     unsafe { *addr = self.frcolor.into() }; // 字,显示前景色
439abe3a6eaShanjiezhou                 } else {
440abe3a6eaShanjiezhou                     unsafe { *addr = self.bkcolor.into() }; // 背景色
441abe3a6eaShanjiezhou                 }
442abe3a6eaShanjiezhou 
443abe3a6eaShanjiezhou                 unsafe {
444abe3a6eaShanjiezhou                     addr = (addr.offset(1)) as *mut u32;
445abe3a6eaShanjiezhou                 }
446abe3a6eaShanjiezhou             }
447abe3a6eaShanjiezhou         }
448abe3a6eaShanjiezhou     }
449abe3a6eaShanjiezhou }
450abe3a6eaShanjiezhou 
451abe3a6eaShanjiezhou /// 单色显示的虚拟行结构体
452abe3a6eaShanjiezhou 
453abe3a6eaShanjiezhou #[derive(Clone, Debug, Default)]
454abe3a6eaShanjiezhou pub struct TextuiVlineNormal {
455abe3a6eaShanjiezhou     _characters: Vec<TextuiCharNormal>, // 字符对象数组
456abe3a6eaShanjiezhou     _index: i16,                        // 当前操作的位置
457abe3a6eaShanjiezhou }
458abe3a6eaShanjiezhou /// 彩色显示的虚拟行结构体
459abe3a6eaShanjiezhou 
460abe3a6eaShanjiezhou #[derive(Clone, Debug, Default)]
461abe3a6eaShanjiezhou pub struct TextuiVlineChromatic {
462abe3a6eaShanjiezhou     chars: Vec<TextuiCharChromatic>, // 字符对象数组
463abe3a6eaShanjiezhou     index: LineIndex,                // 当前操作的位置
464abe3a6eaShanjiezhou }
465abe3a6eaShanjiezhou impl TextuiVlineChromatic {
466abe3a6eaShanjiezhou     pub fn new(char_num: usize) -> Self {
467abe3a6eaShanjiezhou         let mut r = TextuiVlineChromatic {
468abe3a6eaShanjiezhou             chars: Vec::with_capacity(char_num),
469abe3a6eaShanjiezhou             index: LineIndex::new(0),
470abe3a6eaShanjiezhou         };
471abe3a6eaShanjiezhou 
472abe3a6eaShanjiezhou         for _ in 0..char_num {
473abe3a6eaShanjiezhou             r.chars.push(TextuiCharChromatic::new(
474abe3a6eaShanjiezhou                 None,
475abe3a6eaShanjiezhou                 FontColor::BLACK,
476abe3a6eaShanjiezhou                 FontColor::BLACK,
477abe3a6eaShanjiezhou             ));
478abe3a6eaShanjiezhou         }
479abe3a6eaShanjiezhou 
480abe3a6eaShanjiezhou         return r;
481abe3a6eaShanjiezhou     }
482abe3a6eaShanjiezhou }
483abe3a6eaShanjiezhou 
484abe3a6eaShanjiezhou #[derive(Clone, Debug)]
485abe3a6eaShanjiezhou pub enum TextuiVline {
486abe3a6eaShanjiezhou     Chromatic(TextuiVlineChromatic),
487abe3a6eaShanjiezhou     _Normal(TextuiVlineNormal),
488abe3a6eaShanjiezhou }
489abe3a6eaShanjiezhou 
490abe3a6eaShanjiezhou #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
491abe3a6eaShanjiezhou pub struct WindowId(u32);
492abe3a6eaShanjiezhou 
493abe3a6eaShanjiezhou impl WindowId {
494abe3a6eaShanjiezhou     pub fn new() -> Self {
495abe3a6eaShanjiezhou         static MAX_ID: AtomicU32 = AtomicU32::new(0);
496abe3a6eaShanjiezhou         return WindowId(MAX_ID.fetch_add(1, Ordering::SeqCst));
497abe3a6eaShanjiezhou     }
498abe3a6eaShanjiezhou }
499abe3a6eaShanjiezhou #[allow(dead_code)]
500abe3a6eaShanjiezhou #[derive(Clone, Debug)]
501abe3a6eaShanjiezhou pub struct TextuiWindow {
502abe3a6eaShanjiezhou     // 虚拟行是个循环表,头和尾相接
503abe3a6eaShanjiezhou     id: WindowId,
504abe3a6eaShanjiezhou     // 虚拟行总数
505abe3a6eaShanjiezhou     vline_sum: i32,
506abe3a6eaShanjiezhou     // 当前已经使用了的虚拟行总数(即在已经输入到缓冲区(之后显示在屏幕上)的虚拟行数量)
507abe3a6eaShanjiezhou     vlines_used: i32,
508abe3a6eaShanjiezhou     // 位于最顶上的那一个虚拟行的行号
509abe3a6eaShanjiezhou     top_vline: LineId,
510abe3a6eaShanjiezhou     // 储存虚拟行的数组
511abe3a6eaShanjiezhou     vlines: Vec<TextuiVline>,
512abe3a6eaShanjiezhou     // 正在操作的vline
513abe3a6eaShanjiezhou     vline_operating: LineId,
514abe3a6eaShanjiezhou     // 每行最大容纳的字符数
515abe3a6eaShanjiezhou     chars_per_line: i32,
516abe3a6eaShanjiezhou     // 窗口flag
517abe3a6eaShanjiezhou     flags: WindowFlag,
518abe3a6eaShanjiezhou }
519abe3a6eaShanjiezhou 
520abe3a6eaShanjiezhou impl TextuiWindow {
521abe3a6eaShanjiezhou     /// 使用参数初始化window对象
522abe3a6eaShanjiezhou     /// ## 参数
523abe3a6eaShanjiezhou     ///
524abe3a6eaShanjiezhou     /// -flags 标志位
525abe3a6eaShanjiezhou     /// -vlines_num 虚拟行的总数
526abe3a6eaShanjiezhou     /// -chars_num 每行最大的字符数
527abe3a6eaShanjiezhou 
528abe3a6eaShanjiezhou     pub fn new(flags: WindowFlag, vlines_num: i32, chars_num: i32) -> Self {
529abe3a6eaShanjiezhou         let mut initial_vlines = Vec::new();
530abe3a6eaShanjiezhou 
531abe3a6eaShanjiezhou         for _ in 0..vlines_num {
532abe3a6eaShanjiezhou             let vline = TextuiVlineChromatic::new(chars_num as usize);
533abe3a6eaShanjiezhou 
534abe3a6eaShanjiezhou             initial_vlines.push(TextuiVline::Chromatic(vline));
535abe3a6eaShanjiezhou         }
536abe3a6eaShanjiezhou         TextuiWindow {
537abe3a6eaShanjiezhou             id: WindowId::new(),
538abe3a6eaShanjiezhou             flags,
539abe3a6eaShanjiezhou             vline_sum: vlines_num,
540abe3a6eaShanjiezhou             vlines_used: 1,
541abe3a6eaShanjiezhou             top_vline: LineId::new(0),
542abe3a6eaShanjiezhou             vlines: initial_vlines,
543abe3a6eaShanjiezhou             vline_operating: LineId::new(0),
544abe3a6eaShanjiezhou             chars_per_line: chars_num,
545abe3a6eaShanjiezhou         }
546abe3a6eaShanjiezhou     }
547abe3a6eaShanjiezhou 
548abe3a6eaShanjiezhou     /// 刷新某个窗口的缓冲区的某个虚拟行的连续n个字符对象
549abe3a6eaShanjiezhou     /// ## 参数
550abe3a6eaShanjiezhou     /// - window 窗口结构体
551abe3a6eaShanjiezhou     /// - vline_id 要刷新的虚拟行号
552abe3a6eaShanjiezhou     /// - start 起始字符号
553abe3a6eaShanjiezhou     /// - count 要刷新的字符数量
554abe3a6eaShanjiezhou 
555abe3a6eaShanjiezhou     fn textui_refresh_characters(
556abe3a6eaShanjiezhou         &mut self,
557abe3a6eaShanjiezhou         vline_id: LineId,
558abe3a6eaShanjiezhou         start: LineIndex,
559abe3a6eaShanjiezhou         count: i32,
560abe3a6eaShanjiezhou     ) -> Result<(), SystemError> {
561abe3a6eaShanjiezhou         let actual_line_sum = textui_framework().actual_line.load(Ordering::SeqCst);
562abe3a6eaShanjiezhou 
563abe3a6eaShanjiezhou         // 判断虚拟行参数是否合法
564abe3a6eaShanjiezhou         if unlikely(
565abe3a6eaShanjiezhou             !vline_id.check(self.vline_sum)
566abe3a6eaShanjiezhou                 || (<LineIndex as Into<i32>>::into(start) + count) > self.chars_per_line,
567abe3a6eaShanjiezhou         ) {
568abe3a6eaShanjiezhou             return Err(SystemError::EINVAL);
569abe3a6eaShanjiezhou         }
570abe3a6eaShanjiezhou         // 计算虚拟行对应的真实行(即要渲染的行)
571abe3a6eaShanjiezhou         let mut actual_line_id = vline_id - self.top_vline; //为正说明虚拟行不在真实行显示的区域上面
572abe3a6eaShanjiezhou 
573abe3a6eaShanjiezhou         if <LineId as Into<i32>>::into(actual_line_id) < 0 {
574abe3a6eaShanjiezhou             //真实行数小于虚拟行数,则需要加上真实行数的位置,以便正确计算真实行
575abe3a6eaShanjiezhou             actual_line_id = actual_line_id + actual_line_sum;
576abe3a6eaShanjiezhou         }
577abe3a6eaShanjiezhou 
578abe3a6eaShanjiezhou         // 将此窗口的某个虚拟行的连续n个字符对象往缓存区写入
579abe3a6eaShanjiezhou         if self.flags.contains(WindowFlag::TEXTUI_CHROMATIC) {
580abe3a6eaShanjiezhou             let vline = &mut self.vlines[<LineId as Into<usize>>::into(vline_id)];
581abe3a6eaShanjiezhou             let mut i = 0;
582abe3a6eaShanjiezhou             let mut index = start;
583abe3a6eaShanjiezhou 
584abe3a6eaShanjiezhou             while i < count {
585abe3a6eaShanjiezhou                 if let TextuiVline::Chromatic(vline) = vline {
586abe3a6eaShanjiezhou                     vline.chars[<LineIndex as Into<usize>>::into(index)]
587abe3a6eaShanjiezhou                         .textui_refresh_character(actual_line_id, index)?;
588abe3a6eaShanjiezhou 
589abe3a6eaShanjiezhou                     index = index + 1;
590abe3a6eaShanjiezhou                 }
591abe3a6eaShanjiezhou                 i += 1;
592abe3a6eaShanjiezhou             }
593abe3a6eaShanjiezhou         }
594abe3a6eaShanjiezhou 
595abe3a6eaShanjiezhou         return Ok(());
596abe3a6eaShanjiezhou     }
597abe3a6eaShanjiezhou 
598abe3a6eaShanjiezhou     /// 重新渲染某个窗口的某个虚拟行
599abe3a6eaShanjiezhou     /// ## 参数
600abe3a6eaShanjiezhou 
601abe3a6eaShanjiezhou     /// - window 窗口结构体
602abe3a6eaShanjiezhou     /// - vline_id 虚拟行号
603abe3a6eaShanjiezhou 
604abe3a6eaShanjiezhou     fn textui_refresh_vline(&mut self, vline_id: LineId) -> Result<(), SystemError> {
605abe3a6eaShanjiezhou         if self.flags.contains(WindowFlag::TEXTUI_CHROMATIC) {
606abe3a6eaShanjiezhou             return self.textui_refresh_characters(
607abe3a6eaShanjiezhou                 vline_id,
608abe3a6eaShanjiezhou                 LineIndex::new(0),
609abe3a6eaShanjiezhou                 self.chars_per_line,
610abe3a6eaShanjiezhou             );
611abe3a6eaShanjiezhou         } else {
612abe3a6eaShanjiezhou             //todo支持纯文本字符()
613abe3a6eaShanjiezhou             todo!();
614abe3a6eaShanjiezhou         }
615abe3a6eaShanjiezhou     }
616abe3a6eaShanjiezhou 
617abe3a6eaShanjiezhou     // 刷新某个窗口的start 到start + count行(即将这些行输入到缓冲区)
618abe3a6eaShanjiezhou     fn textui_refresh_vlines(&mut self, start: LineId, count: i32) -> Result<i32, SystemError> {
619abe3a6eaShanjiezhou         let mut refresh_count = count;
620abe3a6eaShanjiezhou         for i in <LineId as Into<i32>>::into(start)
621abe3a6eaShanjiezhou             ..(self.vline_sum).min(<LineId as Into<i32>>::into(start) + count)
622abe3a6eaShanjiezhou         {
623abe3a6eaShanjiezhou             self.textui_refresh_vline(LineId::new(i))?;
624abe3a6eaShanjiezhou             refresh_count -= 1;
625abe3a6eaShanjiezhou         }
626abe3a6eaShanjiezhou         //因为虚拟行是循环表
627abe3a6eaShanjiezhou         let mut refresh_start = 0;
628abe3a6eaShanjiezhou         while refresh_count > 0 {
629abe3a6eaShanjiezhou             self.textui_refresh_vline(LineId::new(refresh_start))?;
630abe3a6eaShanjiezhou             refresh_start += 1;
631abe3a6eaShanjiezhou             refresh_count -= 1;
632abe3a6eaShanjiezhou         }
633abe3a6eaShanjiezhou         return Ok(0);
634abe3a6eaShanjiezhou     }
635abe3a6eaShanjiezhou 
636abe3a6eaShanjiezhou     /// 往某个窗口的缓冲区的某个虚拟行插入换行
637abe3a6eaShanjiezhou     /// ## 参数
638abe3a6eaShanjiezhou     /// - window 窗口结构体
639abe3a6eaShanjiezhou     /// - vline_id 虚拟行号
640abe3a6eaShanjiezhou 
641abe3a6eaShanjiezhou     fn textui_new_line(&mut self) -> Result<i32, SystemError> {
642abe3a6eaShanjiezhou         // todo: 支持在两个虚拟行之间插入一个新行
643abe3a6eaShanjiezhou         let actual_line_sum = textui_framework().actual_line.load(Ordering::SeqCst);
644abe3a6eaShanjiezhou         self.vline_operating = self.vline_operating + 1;
645abe3a6eaShanjiezhou         //如果已经到了最大行数,则重新从0开始
646abe3a6eaShanjiezhou         if !self.vline_operating.check(self.vline_sum) {
647abe3a6eaShanjiezhou             self.vline_operating = LineId::new(0);
648abe3a6eaShanjiezhou         }
649abe3a6eaShanjiezhou 
650abe3a6eaShanjiezhou         if let TextuiVline::Chromatic(vline) =
651abe3a6eaShanjiezhou             &mut (self.vlines[<LineId as Into<usize>>::into(self.vline_operating)])
652abe3a6eaShanjiezhou         {
653abe3a6eaShanjiezhou             for i in 0..self.chars_per_line {
654abe3a6eaShanjiezhou                 if let Some(v_char) = vline.chars.get_mut(i as usize) {
655abe3a6eaShanjiezhou                     v_char.c = None;
656abe3a6eaShanjiezhou                     v_char.frcolor = FontColor::BLACK;
657abe3a6eaShanjiezhou                     v_char.bkcolor = FontColor::BLACK;
658abe3a6eaShanjiezhou                 }
659abe3a6eaShanjiezhou             }
660abe3a6eaShanjiezhou             vline.index = LineIndex::new(0);
661abe3a6eaShanjiezhou         }
662abe3a6eaShanjiezhou         // 当已经使用的虚拟行总数等于真实行总数时,说明窗口中已经显示的文本行数已经达到了窗口的最大容量。这时,如果继续在窗口中添加新的文本,就会导致文本溢出窗口而无法显示。因此,需要往下滚动屏幕来显示更多的文本。
663abe3a6eaShanjiezhou 
664abe3a6eaShanjiezhou         if self.vlines_used == actual_line_sum {
665abe3a6eaShanjiezhou             self.top_vline = self.top_vline + 1;
666abe3a6eaShanjiezhou 
667abe3a6eaShanjiezhou             if !self.top_vline.check(self.vline_sum) {
668abe3a6eaShanjiezhou                 self.top_vline = LineId::new(0);
669abe3a6eaShanjiezhou             }
670abe3a6eaShanjiezhou 
671abe3a6eaShanjiezhou             // 刷新所有行
672abe3a6eaShanjiezhou             self.textui_refresh_vlines(self.top_vline, actual_line_sum)?;
673abe3a6eaShanjiezhou         } else {
674abe3a6eaShanjiezhou             //换行说明上一行已经在缓冲区中,所以已经使用的虚拟行总数+1
675abe3a6eaShanjiezhou             self.vlines_used += 1;
676abe3a6eaShanjiezhou         }
677abe3a6eaShanjiezhou 
678abe3a6eaShanjiezhou         return Ok(0);
679abe3a6eaShanjiezhou     }
680abe3a6eaShanjiezhou 
681abe3a6eaShanjiezhou     /// 真正向窗口的缓冲区上输入字符的函数(位置为window.vline_operating,window.vline_operating.index)
682abe3a6eaShanjiezhou     /// ## 参数
683abe3a6eaShanjiezhou     /// - window
684abe3a6eaShanjiezhou     /// - character
685abe3a6eaShanjiezhou 
686abe3a6eaShanjiezhou     fn true_textui_putchar_window(
687abe3a6eaShanjiezhou         &mut self,
688abe3a6eaShanjiezhou         character: char,
689abe3a6eaShanjiezhou         frcolor: FontColor,
690abe3a6eaShanjiezhou         bkcolor: FontColor,
691abe3a6eaShanjiezhou     ) -> Result<(), SystemError> {
692abe3a6eaShanjiezhou         // 启用彩色字符
693abe3a6eaShanjiezhou         if self.flags.contains(WindowFlag::TEXTUI_CHROMATIC) {
694abe3a6eaShanjiezhou             let mut line_index = LineIndex::new(0); //操作的列号
695abe3a6eaShanjiezhou             if let TextuiVline::Chromatic(vline) =
696abe3a6eaShanjiezhou                 &mut (self.vlines[<LineId as Into<usize>>::into(self.vline_operating)])
697abe3a6eaShanjiezhou             {
698abe3a6eaShanjiezhou                 let index = <LineIndex as Into<usize>>::into(vline.index);
699abe3a6eaShanjiezhou 
700abe3a6eaShanjiezhou                 if let Some(v_char) = vline.chars.get_mut(index) {
701abe3a6eaShanjiezhou                     v_char.c = Some(character);
702abe3a6eaShanjiezhou                     v_char.frcolor = frcolor;
703abe3a6eaShanjiezhou                     v_char.bkcolor = bkcolor;
704abe3a6eaShanjiezhou                 }
705abe3a6eaShanjiezhou                 line_index = vline.index;
706abe3a6eaShanjiezhou                 vline.index = vline.index + 1;
707abe3a6eaShanjiezhou             }
708abe3a6eaShanjiezhou 
709abe3a6eaShanjiezhou             self.textui_refresh_characters(self.vline_operating, line_index, 1)?;
710abe3a6eaShanjiezhou 
711abe3a6eaShanjiezhou             // 加入光标后,因为会识别光标,所以需超过该行最大字符数才能创建新行
712abe3a6eaShanjiezhou             if !line_index.check(self.chars_per_line - 1) {
713abe3a6eaShanjiezhou                 self.textui_new_line()?;
714abe3a6eaShanjiezhou             }
715abe3a6eaShanjiezhou         } else {
716abe3a6eaShanjiezhou             // todo: 支持纯文本字符
717abe3a6eaShanjiezhou             todo!();
718abe3a6eaShanjiezhou         }
719abe3a6eaShanjiezhou         return Ok(());
720abe3a6eaShanjiezhou     }
721abe3a6eaShanjiezhou     /// 根据输入的一个字符在窗口上输出
722abe3a6eaShanjiezhou     /// ## 参数
723abe3a6eaShanjiezhou 
724abe3a6eaShanjiezhou     /// - window 窗口
725abe3a6eaShanjiezhou     /// - character 字符
726abe3a6eaShanjiezhou     /// - FRcolor 前景色(RGB)
727abe3a6eaShanjiezhou     /// - BKcolor 背景色(RGB)
728abe3a6eaShanjiezhou 
729abe3a6eaShanjiezhou     fn textui_putchar_window(
730abe3a6eaShanjiezhou         &mut self,
731abe3a6eaShanjiezhou         character: char,
732abe3a6eaShanjiezhou         frcolor: FontColor,
733abe3a6eaShanjiezhou         bkcolor: FontColor,
734abe3a6eaShanjiezhou         is_enable_window: bool,
735abe3a6eaShanjiezhou     ) -> Result<(), SystemError> {
736abe3a6eaShanjiezhou         let actual_line_sum = textui_framework().actual_line.load(Ordering::SeqCst);
737abe3a6eaShanjiezhou 
738abe3a6eaShanjiezhou         //字符'\0'代表ASCII码表中的空字符,表示字符串的结尾
739abe3a6eaShanjiezhou         if unlikely(character == '\0') {
740abe3a6eaShanjiezhou             return Ok(());
741abe3a6eaShanjiezhou         }
742abe3a6eaShanjiezhou 
743abe3a6eaShanjiezhou         if unlikely(character == '\r') {
744abe3a6eaShanjiezhou             return Ok(());
745abe3a6eaShanjiezhou         }
746abe3a6eaShanjiezhou 
747abe3a6eaShanjiezhou         // 暂不支持纯文本窗口
748abe3a6eaShanjiezhou         if !self.flags.contains(WindowFlag::TEXTUI_CHROMATIC) {
749abe3a6eaShanjiezhou             return Ok(());
750abe3a6eaShanjiezhou         }
751*a0c98cd4SJomo         send_to_default_serial8250_port(&[character as u8]);
752abe3a6eaShanjiezhou 
753abe3a6eaShanjiezhou         //进行换行操作
754abe3a6eaShanjiezhou         if character == '\n' {
755abe3a6eaShanjiezhou             // 换行时还需要输出\r
756a03c4f9dSLoGin             send_to_default_serial8250_port(&[b'\r']);
757abe3a6eaShanjiezhou             if is_enable_window == true {
758abe3a6eaShanjiezhou                 self.textui_new_line()?;
759abe3a6eaShanjiezhou             }
760abe3a6eaShanjiezhou             return Ok(());
761abe3a6eaShanjiezhou         }
762abe3a6eaShanjiezhou         // 输出制表符
763abe3a6eaShanjiezhou         else if character == '\t' {
764abe3a6eaShanjiezhou             if is_enable_window == true {
765abe3a6eaShanjiezhou                 if let TextuiVline::Chromatic(vline) =
766abe3a6eaShanjiezhou                     &self.vlines[<LineId as Into<usize>>::into(self.vline_operating)]
767abe3a6eaShanjiezhou                 {
768abe3a6eaShanjiezhou                     //打印的空格数(注意将每行分成一个个表格,每个表格为8个字符)
769abe3a6eaShanjiezhou                     let mut space_to_print = 8 - <LineIndex as Into<usize>>::into(vline.index) % 8;
770abe3a6eaShanjiezhou                     while space_to_print > 0 {
771abe3a6eaShanjiezhou                         self.true_textui_putchar_window(' ', frcolor, bkcolor)?;
772abe3a6eaShanjiezhou                         space_to_print -= 1;
773abe3a6eaShanjiezhou                     }
774abe3a6eaShanjiezhou                 }
775abe3a6eaShanjiezhou             }
776abe3a6eaShanjiezhou         }
777abe3a6eaShanjiezhou         // 字符 '\x08' 代表 ASCII 码中的退格字符。它在输出中的作用是将光标向左移动一个位置,并在该位置上输出后续的字符,从而实现字符的删除或替换。
778abe3a6eaShanjiezhou         else if character == '\x08' {
779abe3a6eaShanjiezhou             if is_enable_window == true {
780abe3a6eaShanjiezhou                 let mut tmp = LineIndex(0);
781abe3a6eaShanjiezhou                 if let TextuiVline::Chromatic(vline) =
782abe3a6eaShanjiezhou                     &mut self.vlines[<LineId as Into<usize>>::into(self.vline_operating)]
783abe3a6eaShanjiezhou                 {
784abe3a6eaShanjiezhou                     vline.index = vline.index - 1;
785abe3a6eaShanjiezhou                     tmp = vline.index;
786abe3a6eaShanjiezhou                 }
787abe3a6eaShanjiezhou                 if <LineIndex as Into<i32>>::into(tmp) >= 0 {
788abe3a6eaShanjiezhou                     if let TextuiVline::Chromatic(vline) =
789abe3a6eaShanjiezhou                         &mut self.vlines[<LineId as Into<usize>>::into(self.vline_operating)]
790abe3a6eaShanjiezhou                     {
791abe3a6eaShanjiezhou                         if let Some(v_char) =
792abe3a6eaShanjiezhou                             vline.chars.get_mut(<LineIndex as Into<usize>>::into(tmp))
793abe3a6eaShanjiezhou                         {
794abe3a6eaShanjiezhou                             v_char.c = Some(' ');
795abe3a6eaShanjiezhou 
796abe3a6eaShanjiezhou                             v_char.bkcolor = bkcolor;
797abe3a6eaShanjiezhou                         }
798abe3a6eaShanjiezhou                     }
799abe3a6eaShanjiezhou                     return self.textui_refresh_characters(self.vline_operating, tmp, 1);
800abe3a6eaShanjiezhou                 }
801abe3a6eaShanjiezhou                 // 需要向上缩一行
802abe3a6eaShanjiezhou                 if <LineIndex as Into<i32>>::into(tmp) < 0 {
803abe3a6eaShanjiezhou                     // 当前行为空,需要重新刷新
804abe3a6eaShanjiezhou                     if let TextuiVline::Chromatic(vline) =
805abe3a6eaShanjiezhou                         &mut self.vlines[<LineId as Into<usize>>::into(self.vline_operating)]
806abe3a6eaShanjiezhou                     {
807abe3a6eaShanjiezhou                         vline.index = LineIndex::new(0);
808abe3a6eaShanjiezhou                         for i in 0..self.chars_per_line {
809abe3a6eaShanjiezhou                             if let Some(v_char) = vline.chars.get_mut(i as usize) {
810abe3a6eaShanjiezhou                                 v_char.c = None;
811abe3a6eaShanjiezhou                                 v_char.frcolor = FontColor::BLACK;
812abe3a6eaShanjiezhou                                 v_char.bkcolor = FontColor::BLACK;
813abe3a6eaShanjiezhou                             }
814abe3a6eaShanjiezhou                         }
815abe3a6eaShanjiezhou                     }
816abe3a6eaShanjiezhou                     // 上缩一行
817abe3a6eaShanjiezhou                     self.vline_operating = self.vline_operating - 1;
818abe3a6eaShanjiezhou                     if self.vline_operating.data() < 0 {
819abe3a6eaShanjiezhou                         self.vline_operating = LineId(self.vline_sum - 1);
820abe3a6eaShanjiezhou                     }
821abe3a6eaShanjiezhou 
822abe3a6eaShanjiezhou                     // 考虑是否向上滚动(在top_vline上退格)
823abe3a6eaShanjiezhou                     if self.vlines_used > actual_line_sum {
824abe3a6eaShanjiezhou                         self.top_vline = self.top_vline - 1;
825abe3a6eaShanjiezhou                         if <LineId as Into<i32>>::into(self.top_vline) < 0 {
826abe3a6eaShanjiezhou                             self.top_vline = LineId(self.vline_sum - 1);
827abe3a6eaShanjiezhou                         }
828abe3a6eaShanjiezhou                     }
829abe3a6eaShanjiezhou                     //因为上缩一行所以显示在屏幕中的虚拟行少一
830abe3a6eaShanjiezhou                     self.vlines_used -= 1;
831abe3a6eaShanjiezhou                     self.textui_refresh_vlines(self.top_vline, actual_line_sum)?;
832abe3a6eaShanjiezhou                 }
833abe3a6eaShanjiezhou             }
834abe3a6eaShanjiezhou         } else {
835abe3a6eaShanjiezhou             if is_enable_window == true {
836abe3a6eaShanjiezhou                 if let TextuiVline::Chromatic(vline) =
837abe3a6eaShanjiezhou                     &self.vlines[<LineId as Into<usize>>::into(self.vline_operating)]
838abe3a6eaShanjiezhou                 {
839abe3a6eaShanjiezhou                     if !vline.index.check(self.chars_per_line) {
840abe3a6eaShanjiezhou                         self.textui_new_line()?;
841abe3a6eaShanjiezhou                     }
842abe3a6eaShanjiezhou 
843abe3a6eaShanjiezhou                     return self.true_textui_putchar_window(character, frcolor, bkcolor);
844abe3a6eaShanjiezhou                 }
845abe3a6eaShanjiezhou             }
846abe3a6eaShanjiezhou         }
847abe3a6eaShanjiezhou 
848abe3a6eaShanjiezhou         return Ok(());
849abe3a6eaShanjiezhou     }
850abe3a6eaShanjiezhou }
851abe3a6eaShanjiezhou impl Default for TextuiWindow {
852abe3a6eaShanjiezhou     fn default() -> Self {
853abe3a6eaShanjiezhou         TextuiWindow {
854abe3a6eaShanjiezhou             id: WindowId(0),
855abe3a6eaShanjiezhou             flags: WindowFlag::TEXTUI_CHROMATIC,
856abe3a6eaShanjiezhou             vline_sum: 0,
857abe3a6eaShanjiezhou             vlines_used: 1,
858abe3a6eaShanjiezhou             top_vline: LineId::new(0),
859abe3a6eaShanjiezhou             vlines: Vec::new(),
860abe3a6eaShanjiezhou             vline_operating: LineId::new(0),
861abe3a6eaShanjiezhou             chars_per_line: 0,
862abe3a6eaShanjiezhou         }
863abe3a6eaShanjiezhou     }
864abe3a6eaShanjiezhou }
865abe3a6eaShanjiezhou #[allow(dead_code)]
866abe3a6eaShanjiezhou #[derive(Debug)]
867abe3a6eaShanjiezhou pub struct TextUiFramework {
8681496ba7bSLoGin     metadata: RwLock<ScmUiFrameworkMetadata>,
869abe3a6eaShanjiezhou     window_list: Arc<SpinLock<LinkedList<Arc<SpinLock<TextuiWindow>>>>>,
870abe3a6eaShanjiezhou     actual_line: AtomicI32, // 真实行的数量(textui的帧缓冲区能容纳的内容的行数)
871abe3a6eaShanjiezhou     current_window: Arc<SpinLock<TextuiWindow>>, // 当前的主窗口
872abe3a6eaShanjiezhou     default_window: Arc<SpinLock<TextuiWindow>>, // 默认print到的窗口
873abe3a6eaShanjiezhou }
874abe3a6eaShanjiezhou 
875abe3a6eaShanjiezhou impl TextUiFramework {
876abe3a6eaShanjiezhou     pub fn new(
877abe3a6eaShanjiezhou         metadata: ScmUiFrameworkMetadata,
878abe3a6eaShanjiezhou         window_list: Arc<SpinLock<LinkedList<Arc<SpinLock<TextuiWindow>>>>>,
879abe3a6eaShanjiezhou         current_window: Arc<SpinLock<TextuiWindow>>,
880abe3a6eaShanjiezhou         default_window: Arc<SpinLock<TextuiWindow>>,
881abe3a6eaShanjiezhou     ) -> Self {
882abe3a6eaShanjiezhou         let actual_line =
8831496ba7bSLoGin             AtomicI32::new((&metadata.buf_info().height() / TEXTUI_CHAR_HEIGHT) as i32);
884abe3a6eaShanjiezhou         let inner = TextUiFramework {
8851496ba7bSLoGin             metadata: RwLock::new(metadata),
886abe3a6eaShanjiezhou             window_list,
887abe3a6eaShanjiezhou             actual_line,
888abe3a6eaShanjiezhou             current_window,
889abe3a6eaShanjiezhou             default_window,
890abe3a6eaShanjiezhou         };
891abe3a6eaShanjiezhou         return inner;
892abe3a6eaShanjiezhou     }
893abe3a6eaShanjiezhou }
894abe3a6eaShanjiezhou 
8951496ba7bSLoGin impl ScmUiFramework for TextUiFramework {
896abe3a6eaShanjiezhou     // 安装ui框架的回调函数
897abe3a6eaShanjiezhou     fn install(&self) -> Result<i32, SystemError> {
898a03c4f9dSLoGin         send_to_default_serial8250_port("\ntextui_install_handler\n\0".as_bytes());
899abe3a6eaShanjiezhou         return Ok(0);
900abe3a6eaShanjiezhou     }
901abe3a6eaShanjiezhou     // 卸载ui框架的回调函数
902abe3a6eaShanjiezhou     fn uninstall(&self) -> Result<i32, SystemError> {
903abe3a6eaShanjiezhou         return Ok(0);
904abe3a6eaShanjiezhou     }
905abe3a6eaShanjiezhou     // 启用ui框架的回调函数
906abe3a6eaShanjiezhou     fn enable(&self) -> Result<i32, SystemError> {
907abe3a6eaShanjiezhou         ENABLE_PUT_TO_WINDOW.store(true, Ordering::SeqCst);
908abe3a6eaShanjiezhou         return Ok(0);
909abe3a6eaShanjiezhou     }
910abe3a6eaShanjiezhou     // 禁用ui框架的回调函数
911abe3a6eaShanjiezhou     fn disable(&self) -> Result<i32, SystemError> {
912abe3a6eaShanjiezhou         ENABLE_PUT_TO_WINDOW.store(false, Ordering::SeqCst);
913abe3a6eaShanjiezhou 
914abe3a6eaShanjiezhou         return Ok(0);
915abe3a6eaShanjiezhou     }
916abe3a6eaShanjiezhou     // 改变ui框架的帧缓冲区的回调函数
917abe3a6eaShanjiezhou     fn change(&self, buf_info: ScmBufferInfo) -> Result<i32, SystemError> {
9181496ba7bSLoGin         let old_buf = textui_framework().metadata.read().buf_info();
9191496ba7bSLoGin 
9201496ba7bSLoGin         textui_framework().metadata.write().set_buf_info(buf_info);
9211496ba7bSLoGin 
9221496ba7bSLoGin         let mut new_buf = textui_framework().metadata.read().buf_info();
9231496ba7bSLoGin 
9241496ba7bSLoGin         new_buf.copy_from_nonoverlapping(&old_buf);
9251496ba7bSLoGin         kdebug!("textui change buf_info: old: {:?}", old_buf);
9261496ba7bSLoGin         kdebug!("textui change buf_info: new: {:?}", new_buf);
9271496ba7bSLoGin 
928abe3a6eaShanjiezhou         return Ok(0);
929abe3a6eaShanjiezhou     }
930abe3a6eaShanjiezhou     ///  获取ScmUiFramework的元数据
931abe3a6eaShanjiezhou     ///  ## 返回值
932abe3a6eaShanjiezhou     ///
933abe3a6eaShanjiezhou     ///  -成功:Ok(ScmUiFramework的元数据)
934abe3a6eaShanjiezhou     ///  -失败:Err(错误码)
935abe3a6eaShanjiezhou     fn metadata(&self) -> Result<ScmUiFrameworkMetadata, SystemError> {
9361496ba7bSLoGin         let metadata = self.metadata.read().clone();
937abe3a6eaShanjiezhou 
938abe3a6eaShanjiezhou         return Ok(metadata);
939abe3a6eaShanjiezhou     }
940abe3a6eaShanjiezhou }
941abe3a6eaShanjiezhou 
942abe3a6eaShanjiezhou /// Mapping from characters to glyph indices.
943abe3a6eaShanjiezhou pub trait GlyphMapping: Sync {
944abe3a6eaShanjiezhou     /// Maps a character to a glyph index.
945abe3a6eaShanjiezhou     ///
946abe3a6eaShanjiezhou     /// If `c` isn't included in the font the index of a suitable replacement glyph is returned.
947abe3a6eaShanjiezhou     fn index(&self, c: char) -> usize;
948abe3a6eaShanjiezhou }
949abe3a6eaShanjiezhou 
950abe3a6eaShanjiezhou impl<F> GlyphMapping for F
951abe3a6eaShanjiezhou where
952abe3a6eaShanjiezhou     F: Sync + Fn(char) -> usize,
953abe3a6eaShanjiezhou {
954abe3a6eaShanjiezhou     fn index(&self, c: char) -> usize {
955abe3a6eaShanjiezhou         self(c)
956abe3a6eaShanjiezhou     }
957abe3a6eaShanjiezhou }
958abe3a6eaShanjiezhou 
959abe3a6eaShanjiezhou /// 在默认窗口上输出一个字符
960abe3a6eaShanjiezhou /// ## 参数
961abe3a6eaShanjiezhou /// - character 字符
962abe3a6eaShanjiezhou /// - FRcolor 前景色(RGB)
963abe3a6eaShanjiezhou /// - BKcolor 背景色(RGB)
964abe3a6eaShanjiezhou 
965abe3a6eaShanjiezhou #[no_mangle]
966abe3a6eaShanjiezhou pub extern "C" fn rs_textui_putchar(character: u8, fr_color: u32, bk_color: u32) -> i32 {
967abe3a6eaShanjiezhou     return textui_putchar(
968abe3a6eaShanjiezhou         character as char,
969abe3a6eaShanjiezhou         FontColor::from(fr_color),
970abe3a6eaShanjiezhou         FontColor::from(bk_color),
971abe3a6eaShanjiezhou     )
972abe3a6eaShanjiezhou     .map(|_| 0)
973abe3a6eaShanjiezhou     .unwrap_or_else(|e| e.to_posix_errno());
974abe3a6eaShanjiezhou }
975abe3a6eaShanjiezhou 
976abe3a6eaShanjiezhou pub fn textui_putchar(
977abe3a6eaShanjiezhou     character: char,
978abe3a6eaShanjiezhou     fr_color: FontColor,
979abe3a6eaShanjiezhou     bk_color: FontColor,
980abe3a6eaShanjiezhou ) -> Result<(), SystemError> {
981abe3a6eaShanjiezhou     if unsafe { TEXTUI_IS_INIT } {
982abe3a6eaShanjiezhou         return textui_framework()
983abe3a6eaShanjiezhou             .current_window
984abe3a6eaShanjiezhou             .lock()
985abe3a6eaShanjiezhou             .textui_putchar_window(
986abe3a6eaShanjiezhou                 character,
987abe3a6eaShanjiezhou                 fr_color,
988abe3a6eaShanjiezhou                 bk_color,
989abe3a6eaShanjiezhou                 ENABLE_PUT_TO_WINDOW.load(Ordering::SeqCst),
990abe3a6eaShanjiezhou             );
991abe3a6eaShanjiezhou     } else {
992abe3a6eaShanjiezhou         //未初始化暴力输出
993abe3a6eaShanjiezhou         return no_init_textui_putchar_window(
994abe3a6eaShanjiezhou             character,
995abe3a6eaShanjiezhou             fr_color,
996abe3a6eaShanjiezhou             bk_color,
997abe3a6eaShanjiezhou             ENABLE_PUT_TO_WINDOW.load(Ordering::SeqCst),
998abe3a6eaShanjiezhou         );
999abe3a6eaShanjiezhou     }
1000abe3a6eaShanjiezhou }
1001abe3a6eaShanjiezhou 
1002fbe6becdSLoGin /// 向默认窗口输出一个字符串
1003fbe6becdSLoGin pub fn textui_putstr(
1004fbe6becdSLoGin     string: &str,
1005fbe6becdSLoGin     fr_color: FontColor,
1006fbe6becdSLoGin     bk_color: FontColor,
1007fbe6becdSLoGin ) -> Result<(), SystemError> {
1008fbe6becdSLoGin     let window = if unsafe { TEXTUI_IS_INIT } {
1009fbe6becdSLoGin         let fw = textui_framework();
1010fbe6becdSLoGin         let w = fw.current_window.clone();
1011fbe6becdSLoGin         Some(w)
1012fbe6becdSLoGin     } else {
1013fbe6becdSLoGin         None
1014fbe6becdSLoGin     };
1015fbe6becdSLoGin 
1016fbe6becdSLoGin     let mut guard = window.as_ref().map(|w| w.lock());
1017fbe6becdSLoGin 
1018fbe6becdSLoGin     for character in string.chars() {
1019fbe6becdSLoGin         if unsafe { TEXTUI_IS_INIT } {
1020fbe6becdSLoGin             guard.as_mut().unwrap().textui_putchar_window(
1021fbe6becdSLoGin                 character,
1022fbe6becdSLoGin                 fr_color,
1023fbe6becdSLoGin                 bk_color,
1024fbe6becdSLoGin                 ENABLE_PUT_TO_WINDOW.load(Ordering::SeqCst),
1025fbe6becdSLoGin             )?;
1026fbe6becdSLoGin         } else {
1027fbe6becdSLoGin             no_init_textui_putchar_window(
1028fbe6becdSLoGin                 character,
1029fbe6becdSLoGin                 fr_color,
1030fbe6becdSLoGin                 bk_color,
1031fbe6becdSLoGin                 ENABLE_PUT_TO_WINDOW.load(Ordering::SeqCst),
1032fbe6becdSLoGin             )?;
1033fbe6becdSLoGin         }
1034fbe6becdSLoGin     }
1035fbe6becdSLoGin 
1036fbe6becdSLoGin     return Ok(());
1037fbe6becdSLoGin }
1038fbe6becdSLoGin 
1039abe3a6eaShanjiezhou /// 初始化text ui框架
1040abe3a6eaShanjiezhou 
1041abe3a6eaShanjiezhou #[no_mangle]
1042abe3a6eaShanjiezhou pub extern "C" fn rs_textui_init() -> i32 {
1043abe3a6eaShanjiezhou     let r = textui_init().unwrap_or_else(|e| e.to_posix_errno());
1044abe3a6eaShanjiezhou     if r.is_negative() {
1045a03c4f9dSLoGin         send_to_default_serial8250_port("textui init failed.\n\0".as_bytes());
1046abe3a6eaShanjiezhou     }
1047abe3a6eaShanjiezhou     return r;
1048abe3a6eaShanjiezhou }
1049abe3a6eaShanjiezhou 
1050abe3a6eaShanjiezhou fn textui_init() -> Result<i32, SystemError> {
1051abe3a6eaShanjiezhou     unsafe { textui_framwork_init() };
1052abe3a6eaShanjiezhou 
1053abe3a6eaShanjiezhou     return Ok(0);
1054abe3a6eaShanjiezhou }
1055