From 00d3a1de7206cde88f17011ee4ca595df4202a79 Mon Sep 17 00:00:00 2001 From: Jacob Hinchliffe Date: Mon, 24 Feb 2025 23:24:03 +0000 Subject: [PATCH] fb: Added support for typing backspaces. --- libk/src/drivers/io/ascii/mod.rs | 27 ++++++++++++++++++++++----- libk/src/drivers/io/keyboard.rs | 5 ++--- libk/src/lib.rs | 1 + 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/libk/src/drivers/io/ascii/mod.rs b/libk/src/drivers/io/ascii/mod.rs index 56e5199..949aa22 100644 --- a/libk/src/drivers/io/ascii/mod.rs +++ b/libk/src/drivers/io/ascii/mod.rs @@ -64,13 +64,19 @@ impl Writer { self.font = font; } + /// This is sent when the user types a backspace. + const BACKSPACE: u16 = 8; + pub fn write_glyph(&mut self, c: u16) { - if c as u8 == b'\n' { + if c == b'\n' as u16 { self.newline(); return; + } else if c == Self::BACKSPACE { + self.backspace(); + return; } - // get the character data from the font array. -- each byte is a row of pixels + // Get the character data from the font array. -- each byte is a row of pixels let data: &[u8] = &self.font.0[c as usize]; if let Some(writer) = FRAMEBUFFER_WRITER.lock().as_mut() { @@ -80,17 +86,17 @@ impl Writer { let pixel_y: u32 = self.text_line * FONT_HEIGHT + row as u32; if line & (0x80 >> col) != 0 { - // write the foreground color + // Write the foreground color writer.write_pixel(pixel_x as usize, pixel_y as usize, self.fg_color); } else { - // write the background color + // Write the background color writer.write_pixel(pixel_x as usize, pixel_y as usize, self.bg_color); } } } } - // go to next position + // Go to next position if self.text_col + 1 >= self.screen_width { self.newline(); } else { @@ -121,6 +127,17 @@ impl Writer { } } + /// Handles the backspace character. TODO: Implement VT-100 style terminal control + /// codes alongside a shell. Not simple. + pub fn backspace(&mut self) { + if self.text_col > 0 { + self.text_col -= 1; + // Blank out the previous char. + self.write_glyph(' ' as u16); + self.text_col -= 1; + } + } + pub fn write_string(&mut self, s: &str) { for c in s.chars() { self.write_glyph(c as u16); diff --git a/libk/src/drivers/io/keyboard.rs b/libk/src/drivers/io/keyboard.rs index 87ce346..e2e9da2 100644 --- a/libk/src/drivers/io/keyboard.rs +++ b/libk/src/drivers/io/keyboard.rs @@ -11,9 +11,7 @@ use pc_keyboard::{ }; use spin::{Lazy, Mutex, Once}; -use crate::println; - -use super::print; +use crate::prelude::*; static KBD_QUEUE: Once> = Once::new(); static WAKER: AtomicWaker = AtomicWaker::new(); @@ -21,6 +19,7 @@ static WAKER: AtomicWaker = AtomicWaker::new(); pub static KEYBOARD: Lazy>> = Lazy::new(|| { Mutex::new(Keyboard::new( ScancodeSet1::new(), + // TODO: Expose an API to change the default KB layout. layouts::Uk105Key, HandleControl::Ignore, )) diff --git a/libk/src/lib.rs b/libk/src/lib.rs index 56a5118..a139d5b 100644 --- a/libk/src/lib.rs +++ b/libk/src/lib.rs @@ -26,6 +26,7 @@ pub mod resources; pub mod std; /// Re-exports most of the IO macros as well as standard allocation stuff +#[allow(unused)] pub mod prelude { pub use crate::std::io::*; pub use alloc::{