akern-gkgoat-fork/ableos/src/stdio.rs

60 lines
1.6 KiB
Rust
Raw Normal View History

2022-03-16 09:38:47 -05:00
use {
crate::devices::Device::{Character, Vterm},
2022-03-16 09:38:47 -05:00
core::fmt::{Arguments, Error, Write},
kernel::device_interface::character::CharacterDevice,
};
2022-03-16 09:38:47 -05:00
#[derive(Debug, Clone)]
pub struct StdIo {
2022-03-16 09:38:47 -05:00
device: String,
}
impl StdIo {
pub fn new(device: String) -> StdIo {
StdIo { device }
}
2022-03-16 09:38:47 -05:00
pub fn write(&mut self, args: Arguments) -> Result<(), Error> {
use crate::devices::DEVICE_TABLE;
let mut dt = DEVICE_TABLE.lock();
let key_device = dt.devices.get_mut(&self.device).unwrap();
match key_device {
Character(dev) => {
let mut buf = String::new();
write!(buf, "{}", args).unwrap();
for c in buf.chars() {
dev.write_char(c);
}
Ok(())
}
2022-03-16 09:38:47 -05:00
Vterm(vterm) => {
let mut buf = String::new();
write!(buf, "{}", args).unwrap();
for c in buf.chars() {
vterm.write_char(c);
}
Ok(())
}
}
}
2022-04-11 17:23:11 -05:00
pub fn read(&mut self) {
2022-04-19 02:15:45 -05:00
use crate::devices::DEVICE_TABLE;
let mut dt = DEVICE_TABLE.lock();
let key_device = dt.devices.get_mut(&self.device).unwrap();
match key_device {
Character(dev) => {
let mut buf = String::new();
dev.read_char().map(|c| buf.push(c));
println!("{}", buf);
}
Vterm(vterm) => {
let mut buf = String::new();
vterm.read_char().map(|c| buf.push(c));
println!("{}", buf);
}
}
}
}