implemented stdin methods for reading a string (async) and reading keystrokes (sync + async). added a very basic shell on top of it for debugging
This commit is contained in:
+3
-23
@@ -5,10 +5,11 @@ extern crate alloc;
|
|||||||
|
|
||||||
use libk::{
|
use libk::{
|
||||||
drivers::{
|
drivers::{
|
||||||
io::{self, keyboard},
|
io,
|
||||||
scheduling::task::{Executor, Task},
|
scheduling::task::{Executor, Task},
|
||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
util::shell::shell,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
@@ -26,28 +27,7 @@ extern "C" fn kmain() -> ! {
|
|||||||
println!("Dimensions: {}x{} (px)", dimensions2.0, dimensions2.1);
|
println!("Dimensions: {}x{} (px)", dimensions2.0, dimensions2.1);
|
||||||
println!("Dimensions: {}x{} (chars)", dimensions.0, dimensions.1);
|
println!("Dimensions: {}x{} (chars)", dimensions.0, dimensions.1);
|
||||||
|
|
||||||
println!(
|
|
||||||
"
|
|
||||||
$$$$$$$$\\ $$\\
|
|
||||||
$$ _____| $$ |
|
|
||||||
$$ | $$$$$$\\ $$\\ $$\\ $$$$$$$\\ $$$$$$$ | $$$$$$\\ $$\\ $$\\
|
|
||||||
$$$$$\\ $$ __$$\\ $$ | $$ |$$ __$$\\ $$ __$$ |$$ __$$\\ $$ | $$ |
|
|
||||||
$$ __|$$ / $$ |$$ | $$ |$$ | $$ |$$ / $$ |$$ | \\__|$$ | $$ |
|
|
||||||
$$ | $$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ | $$ |
|
|
||||||
$$ | \\$$$$$$ |\\$$$$$$ |$$ | $$ |\\$$$$$$$ |$$ | \\$$$$$$$ |
|
|
||||||
\\__| \\______/ \\______/ \\__| \\__| \\_______|\\__| \\____$$ |
|
|
||||||
$$$$$$\\ $$$$$$\\ $$\\ $$\\ $$\\ $$\\ $$ |
|
|
||||||
$$ __$$\\ $$ __$$\\ $$ | $$ |$$$$ | \\$$$$$$ |
|
|
||||||
$$ / $$ |$$ / \\__| $$ | $$ |\\_$$ | \\______/
|
|
||||||
$$ | $$ |\\$$$$$$\\ \\$$\\ $$ | $$ |
|
|
||||||
$$ | $$ | \\____$$\\ \\$$\\$$ / $$ |
|
|
||||||
$$ | $$ |$$\\ $$ | \\$$$ / $$ |
|
|
||||||
$$$$$$ |\\$$$$$$ | \\$ / $$$$$$\\
|
|
||||||
\\______/ \\______/ \\_/ \\______|
|
|
||||||
"
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut executor = Executor::new();
|
let mut executor = Executor::new();
|
||||||
executor.spawn(Task::new(keyboard::print_keypresses()));
|
executor.spawn(Task::new(shell()));
|
||||||
executor.run();
|
executor.run();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,9 +29,6 @@ pub struct Writer {
|
|||||||
|
|
||||||
fg_color: Colour,
|
fg_color: Colour,
|
||||||
bg_color: Colour,
|
bg_color: Colour,
|
||||||
|
|
||||||
offset1: usize,
|
|
||||||
offset2: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Writer {
|
impl Default for Writer {
|
||||||
@@ -54,8 +51,6 @@ impl Writer {
|
|||||||
text_col: 0,
|
text_col: 0,
|
||||||
fg_color: Colour::White,
|
fg_color: Colour::White,
|
||||||
bg_color: Colour::Black,
|
bg_color: Colour::Black,
|
||||||
offset1: 16,
|
|
||||||
offset2: 0,
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -65,10 +60,10 @@ impl Writer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// This is sent when the user types a backspace.
|
/// This is sent when the user types a backspace.
|
||||||
const BACKSPACE: u16 = 8;
|
const BACKSPACE: u8 = 8;
|
||||||
|
|
||||||
pub fn write_glyph(&mut self, c: u16) {
|
pub fn write_glyph(&mut self, c: u8) {
|
||||||
if c == b'\n' as u16 {
|
if c == b'\n' {
|
||||||
self.newline();
|
self.newline();
|
||||||
return;
|
return;
|
||||||
} else if c == Self::BACKSPACE {
|
} else if c == Self::BACKSPACE {
|
||||||
@@ -104,11 +99,6 @@ impl Writer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn set_offset(&mut self, offset1: usize, offset2: usize) {
|
|
||||||
self.offset1 = offset1;
|
|
||||||
self.offset2 = offset2;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const fn dimensions(&self) -> (u32, u32) {
|
pub const fn dimensions(&self) -> (u32, u32) {
|
||||||
(self.screen_width, self.screen_height)
|
(self.screen_width, self.screen_height)
|
||||||
}
|
}
|
||||||
@@ -133,14 +123,14 @@ impl Writer {
|
|||||||
if self.text_col > 0 {
|
if self.text_col > 0 {
|
||||||
self.text_col -= 1;
|
self.text_col -= 1;
|
||||||
// Blank out the previous char.
|
// Blank out the previous char.
|
||||||
self.write_glyph(' ' as u16);
|
self.write_glyph(b' ');
|
||||||
self.text_col -= 1;
|
self.text_col -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_string(&mut self, s: &str) {
|
pub fn write_string(&mut self, s: &str) {
|
||||||
for c in s.chars() {
|
for c in s.chars() {
|
||||||
self.write_glyph(c as u16);
|
self.write_glyph(c as u8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,6 +193,14 @@ pub fn clear_screen() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reset_cursor() {
|
||||||
|
interrupts::without_interrupts(|| {
|
||||||
|
let mut writer = WRITER.lock();
|
||||||
|
writer.text_line = 0;
|
||||||
|
writer.text_col = 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! println_log {
|
macro_rules! println_log {
|
||||||
() => ($crate::print_log!("\n"));
|
() => ($crate::print_log!("\n"));
|
||||||
|
|||||||
@@ -24,9 +24,8 @@ pub static KEYBOARD: Lazy<Mutex<Keyboard<Uk105Key, ScancodeSet1>>> = Lazy::new(|
|
|||||||
HandleControl::Ignore,
|
HandleControl::Ignore,
|
||||||
))
|
))
|
||||||
});
|
});
|
||||||
|
pub static SCANCODE_STREAM: Lazy<Mutex<ScancodeStream>> =
|
||||||
pub static KEYBOARD_HANDLER: Lazy<Mutex<KeyboardHandler>> =
|
Lazy::new(|| Mutex::new(ScancodeStream::new()));
|
||||||
Lazy::new(|| Mutex::new(KeyboardHandler::new()));
|
|
||||||
|
|
||||||
pub fn add_scancode(scancode: u8) {
|
pub fn add_scancode(scancode: u8) {
|
||||||
if let Some(queue) = KBD_QUEUE.get() {
|
if let Some(queue) = KBD_QUEUE.get() {
|
||||||
@@ -49,6 +48,10 @@ impl ScancodeStream {
|
|||||||
KBD_QUEUE.call_once(|| ArrayQueue::new(5));
|
KBD_QUEUE.call_once(|| ArrayQueue::new(5));
|
||||||
Self { _private: () }
|
Self { _private: () }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn try_next(&mut self) -> Option<u8> {
|
||||||
|
KBD_QUEUE.get().and_then(|queue| queue.pop())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ScancodeStream {
|
impl Default for ScancodeStream {
|
||||||
@@ -77,42 +80,23 @@ impl Stream for ScancodeStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn print_keypresses() {
|
pub async fn get_keystroke_async() -> KeyStroke {
|
||||||
let mut scancodes = ScancodeStream::new();
|
loop {
|
||||||
let mut keyboard = Keyboard::new(
|
if let Some(scancode) = SCANCODE_STREAM.lock().next().await {
|
||||||
ScancodeSet1::new(),
|
if let Ok(keystroke) = KeyStroke::try_from(scancode) {
|
||||||
layouts::Uk105Key,
|
return keystroke;
|
||||||
HandleControl::Ignore,
|
|
||||||
);
|
|
||||||
|
|
||||||
while let Some(scancode) = scancodes.next().await {
|
|
||||||
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
|
|
||||||
if let Some(key) = keyboard.process_keyevent(key_event) {
|
|
||||||
match key {
|
|
||||||
DecodedKey::Unicode(character) => print!("{}", character),
|
|
||||||
DecodedKey::RawKey(key) => print!("{:?}", key),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct KeyboardHandler {
|
pub fn get_keystroke_optional() -> Option<KeyStroke> {
|
||||||
_scancodes: ScancodeStream,
|
if let Some(scancode) = SCANCODE_STREAM.lock().try_next() {
|
||||||
}
|
if let Ok(keystroke) = KeyStroke::try_from(scancode) {
|
||||||
|
return Some(keystroke);
|
||||||
impl KeyboardHandler {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
_scancodes: ScancodeStream::new(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
None
|
||||||
|
|
||||||
impl Default for KeyboardHandler {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
@@ -185,6 +169,17 @@ impl TryFrom<u8> for KeyStroke {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryInto<char> for KeyStroke {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_into(self) -> Result<char, Self::Error> {
|
||||||
|
match self {
|
||||||
|
Self::Char(c) => Ok(c),
|
||||||
|
_ => Err(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl core::fmt::Display for KeyStroke {
|
impl core::fmt::Display for KeyStroke {
|
||||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ extern crate alloc;
|
|||||||
|
|
||||||
pub mod drivers;
|
pub mod drivers;
|
||||||
pub mod resources;
|
pub mod resources;
|
||||||
|
pub mod util;
|
||||||
|
|
||||||
#[allow(unused)] // We aren't using much of this right now.
|
#[allow(unused)] // We aren't using much of this right now.
|
||||||
pub mod std;
|
pub mod std;
|
||||||
|
|||||||
+48
-5
@@ -8,6 +8,11 @@ pub use crate::drivers::io::{
|
|||||||
pub mod stdin {
|
pub mod stdin {
|
||||||
use alloc::string::String;
|
use alloc::string::String;
|
||||||
|
|
||||||
|
use crate::drivers::io::{
|
||||||
|
ascii::WRITER,
|
||||||
|
keyboard::{KeyStroke, get_keystroke_async, get_keystroke_optional},
|
||||||
|
};
|
||||||
|
|
||||||
/// Reads a line of input from standard input asynchronously, returning a `String` containing
|
/// Reads a line of input from standard input asynchronously, returning a `String` containing
|
||||||
/// the input line. Does not include the newline character at the end of the line.
|
/// the input line. Does not include the newline character at the end of the line.
|
||||||
///
|
///
|
||||||
@@ -15,7 +20,45 @@ pub mod stdin {
|
|||||||
///
|
///
|
||||||
/// This function is currently unimplemented.
|
/// This function is currently unimplemented.
|
||||||
pub async fn read_line() -> String {
|
pub async fn read_line() -> String {
|
||||||
todo!()
|
let mut writer = WRITER.lock();
|
||||||
|
|
||||||
|
let mut buff = String::new();
|
||||||
|
loop {
|
||||||
|
match get_keystroke_async().await {
|
||||||
|
KeyStroke::Char(c) => match c {
|
||||||
|
'\n' => {
|
||||||
|
writer.write_glyph(c as u8);
|
||||||
|
return buff;
|
||||||
|
}
|
||||||
|
'\r' => {
|
||||||
|
writer.write_glyph(c as u8);
|
||||||
|
return buff;
|
||||||
|
}
|
||||||
|
'\x08' => {
|
||||||
|
if !buff.is_empty() {
|
||||||
|
buff.pop();
|
||||||
|
writer.backspace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c => {
|
||||||
|
writer.write_glyph(c as u8);
|
||||||
|
buff.push(c)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
KeyStroke::Enter => {
|
||||||
|
writer.write_glyph(b'\n');
|
||||||
|
return buff;
|
||||||
|
}
|
||||||
|
KeyStroke::Backspace => {
|
||||||
|
if !buff.is_empty() {
|
||||||
|
buff.pop();
|
||||||
|
writer.backspace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => continue,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a character from standard input and blocks the current task until a character is
|
/// Reads a character from standard input and blocks the current task until a character is
|
||||||
@@ -24,8 +67,8 @@ pub mod stdin {
|
|||||||
/// # Note
|
/// # Note
|
||||||
///
|
///
|
||||||
/// This function is not yet implemented.
|
/// This function is not yet implemented.
|
||||||
pub async fn read_char() -> char {
|
pub async fn async_keystroke() -> KeyStroke {
|
||||||
todo!()
|
get_keystroke_async().await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to read a character from standard input without blocking the current task.
|
/// Attempt to read a character from standard input without blocking the current task.
|
||||||
@@ -35,7 +78,7 @@ pub mod stdin {
|
|||||||
/// # Note
|
/// # Note
|
||||||
///
|
///
|
||||||
/// This function is not yet implemented.
|
/// This function is not yet implemented.
|
||||||
pub fn try_read_char() -> Option<char> {
|
pub fn keystroke() -> Option<KeyStroke> {
|
||||||
todo!()
|
get_keystroke_optional()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod shell;
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
use x86_64::registers::rflags::read;
|
||||||
|
|
||||||
|
use crate::{drivers::io::ascii::clear_screen, prelude::stdin::read_line, print, println};
|
||||||
|
|
||||||
|
static FETCH: &str = "
|
||||||
|
$$$$$$$$\\ $$\\
|
||||||
|
$$ _____| $$ |
|
||||||
|
$$ | $$$$$$\\ $$\\ $$\\ $$$$$$$\\ $$$$$$$ | $$$$$$\\ $$\\ $$\\
|
||||||
|
$$$$$\\ $$ __$$\\ $$ | $$ |$$ __$$\\ $$ __$$ |$$ __$$\\ $$ | $$ |
|
||||||
|
$$ __|$$ / $$ |$$ | $$ |$$ | $$ |$$ / $$ |$$ | \\__|$$ | $$ |
|
||||||
|
$$ | $$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ | $$ |
|
||||||
|
$$ | \\$$$$$$ |\\$$$$$$ |$$ | $$ |\\$$$$$$$ |$$ | \\$$$$$$$ |
|
||||||
|
\\__| \\______/ \\______/ \\__| \\__| \\_______|\\__| \\____$$ |
|
||||||
|
$$$$$$\\ $$$$$$\\ $$\\ $$\\ $$\\ $$\\ $$ |
|
||||||
|
$$ __$$\\ $$ __$$\\ $$ | $$ |$$$$ | \\$$$$$$ |
|
||||||
|
$$ / $$ |$$ / \\__| $$ | $$ |\\_$$ | \\______/
|
||||||
|
$$ | $$ |\\$$$$$$\\ \\$$\\ $$ | $$ |
|
||||||
|
$$ | $$ | \\____$$\\ \\$$\\$$ / $$ |
|
||||||
|
$$ | $$ |$$\\ $$ | \\$$$ / $$ |
|
||||||
|
$$$$$$ |\\$$$$$$ | \\$ / $$$$$$\\
|
||||||
|
\\______/ \\______/ \\_/ \\______|
|
||||||
|
";
|
||||||
|
|
||||||
|
pub async fn shell() {
|
||||||
|
println!("{}", FETCH);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
print!(" Shell> ");
|
||||||
|
let line = read_line().await;
|
||||||
|
match line.as_str() {
|
||||||
|
"fetch" => {
|
||||||
|
println!("{}", FETCH);
|
||||||
|
}
|
||||||
|
"clear" => clear_screen(),
|
||||||
|
_ => {
|
||||||
|
println!("Unknown command: {}", line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user