Skip to content

Commit cd57863

Browse files
committed
feat(cursor): 实现了光标闪烁功能(bitmap only)
1 parent 4741461 commit cd57863

File tree

5 files changed

+70
-28
lines changed

5 files changed

+70
-28
lines changed

kernel/src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
extern crate proka_kernel;
2222
extern crate alloc;
2323

24-
use proka_kernel::{output::console::CONSOLE, BASE_REVISION};
24+
use proka_kernel::{BASE_REVISION, output::console::CONSOLE};
2525
/* The Kernel main code */
2626
// The normal one
2727
#[unsafe(no_mangle)]
@@ -57,13 +57,17 @@ pub extern "C" fn kernel_main() -> ! {
5757
proka_kernel::interrupts::apic::TIMER_VECTOR,
5858
"Timer",
5959
|_context| {
60+
use proka_kernel::output::console::BITFONT_CURSOR_VISIBLE;
6061
use core::sync::atomic::{AtomicU64, Ordering};
6162
static TICKS: AtomicU64 = AtomicU64::new(0);
6263
let t = TICKS.fetch_add(1, Ordering::Relaxed);
63-
if t > 0 && t % 100 == 0 {
64-
println!("System Tick: {}s", t / 100);
64+
if t > 0 && t % 20 == 0 {
65+
unsafe {
66+
let current = BITFONT_CURSOR_VISIBLE.load(Ordering::Relaxed);
67+
BITFONT_CURSOR_VISIBLE.store(!current, Ordering::Relaxed);
68+
CONSOLE.lock().show_cursor(!current);
69+
}
6570
}
66-
6771
proka_kernel::interrupts::apic::registry::IrqResult::Handled
6872
},
6973
)
@@ -105,10 +109,7 @@ pub extern "C" fn kernel_main() -> ! {
105109

106110
let time = proka_kernel::libs::time::time_since_boot();
107111
println!("Time since boot: {time}");
108-
x86_64::instructions::interrupts::without_interrupts(|| {
109-
CONSOLE.lock().cursor_show();
110-
});
111-
112+
112113
let shell = proka_kernel::libs::shell::Shell::new();
113114
shell.run("keyboard");
114115

kernel/src/output/console/console_bitfont.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
extern crate alloc;
22

33
use crate::graphics::{color, Color};
4-
use crate::output::font8x16::FONT8X16;
4+
use crate::output::font8x16::{CURSOR_UNDERLINE, FONT8X16};
55
use crate::FRAMEBUFFER_REQUEST;
66
use alloc::{vec, vec::Vec};
77
use core::fmt::{self, Write};
8+
use core::sync::atomic::AtomicBool;
89

910
// Constants
1011
const FONT_W: u64 = 8;
1112
const FONT_H: u64 = 16;
1213

14+
pub static mut CURSOR_VISIBLE: AtomicBool = AtomicBool::new(false);
15+
1316
/// The ANSI parse status
1417
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1518
enum ParseState {
@@ -313,6 +316,42 @@ impl BitfontConsole {
313316
self.current_param = 0;
314317
}
315318

319+
/// draw a cursor at the current position.
320+
pub fn draw_cursor(&mut self, is_visible: bool) {
321+
let start_x = self.position.0;
322+
let start_y = self.position.1;
323+
324+
for line in 0..FONT_H {
325+
for i in 0..FONT_W {
326+
let mask = 0x80 >> i;
327+
let x = start_x + i;
328+
let y = start_y + line;
329+
330+
if x < self.width && y < self.height {
331+
let pixel_offset = y * self.pitch + x * 4;
332+
333+
if CURSOR_UNDERLINE[line as usize] & mask != 0 {
334+
if is_visible {
335+
unsafe {
336+
self.address
337+
.add(pixel_offset as usize)
338+
.cast::<u32>()
339+
.write(self.fg_color.to_u32(true));
340+
}
341+
} else {
342+
unsafe {
343+
self.address
344+
.add(pixel_offset as usize)
345+
.cast::<u32>()
346+
.write(self.bg_color.to_u32(true));
347+
}
348+
}
349+
}
350+
}
351+
}
352+
}
353+
}
354+
316355
/// Print a string to console.
317356
pub fn print_string(&mut self, s: &str) {
318357
for c in s.bytes() {
@@ -431,11 +470,7 @@ impl crate::output::console::Console for BitfontConsole {
431470
)
432471
}
433472

434-
fn cursor_hide(&mut self) {
435-
// TODO: Implement cursor hide
436-
}
437-
438-
fn cursor_show(&mut self) {
439-
// TODO: Implement cursor show
473+
fn show_cursor(&mut self, is_visible: bool) {
474+
self.draw_cursor(is_visible);
440475
}
441476
}

kernel/src/output/console/console_ttf.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ use crate::FRAMEBUFFER_REQUEST;
55
use ab_glyph::{Font, FontRef, PxScale, ScaleFont};
66
use alloc::{collections::BTreeMap, vec, vec::Vec};
77
use core::fmt::{self, Write};
8+
use core::sync::atomic::AtomicBool;
89
use lazy_static::lazy_static;
910

1011
pub const DEFAULT_FONT_SIZE: f32 = 10.0;
1112
pub const TAB_SPACES: usize = 4;
1213
pub const GLYPH_CACHE_SIZE: usize = 95; // ASCII printable characters
1314
pub const MAX_ANSI_PARAMS: usize = 8;
1415

16+
pub const CURSOR_VISIBLE: AtomicBool = AtomicBool::new(true);
17+
18+
1519
// The default font writer
1620
lazy_static! {
1721
pub static ref DEFAULT_FONT: FontRef<'static> = {
@@ -997,13 +1001,7 @@ impl<'a> crate::output::console::Console for TtfConsole<'a> {
9971001
(self.cursor_x, self.cursor_y)
9981002
}
9991003

1000-
fn cursor_hide(&mut self) {
1001-
self.hidden_cursor = true;
1002-
self.cursor_needs_redraw = true;
1003-
}
1004-
1005-
fn cursor_show(&mut self) {
1006-
self.hidden_cursor = false;
1007-
self.cursor_needs_redraw = true;
1004+
fn show_cursor(&mut self, _is_visible: bool) {
1005+
self.draw_cursor();
10081006
}
10091007
}

kernel/src/output/console/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ pub use console_bitfont::BitfontConsole;
1414
#[cfg(ENABLE_TTF_CONSOLE)]
1515
pub use console_ttf::TtfConsole;
1616

17+
#[cfg(ENABLE_BITFONT_CONSOLE)]
18+
pub use console_bitfont::CURSOR_VISIBLE as BITFONT_CURSOR_VISIBLE;
19+
20+
#[cfg(ENABLE_TTF_CONSOLE)]
21+
pub use console_ttf::CURSOR_VISIBLE as TTF_CURSOR_VISIBLE;
22+
1723
/// General [`Console`] trait, which defined generic APIs.
1824
pub trait Console: Write {
1925
/// Clean screen
@@ -52,11 +58,8 @@ pub trait Console: Write {
5258
/// Get current cursor posision
5359
fn get_cursor_pos(&self) -> (u32, u32);
5460

55-
/// Hide cursor
56-
fn cursor_hide(&mut self);
57-
58-
/// Show cursor
59-
fn cursor_show(&mut self);
61+
/// Draw cursor at current position
62+
fn show_cursor(&mut self, is_visible: bool);
6063
}
6164

6265
pub type ConsoleImpl<'a> = alloc::boxed::Box<dyn Console + Send + 'a>;

kernel/src/output/font8x16.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,8 @@ pub const FONT8X16: [[u8; 16]; 128] = [
517517
0x00,
518518
], // 0x7F, delete
519519
];
520+
521+
/// The definition of the cursor, which is using underline style.
522+
pub const CURSOR_UNDERLINE: [u8; 16] = [
523+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
524+
];

0 commit comments

Comments
 (0)