2022-03-16 09:38:47 -05:00
|
|
|
use {
|
2022-08-08 12:07:43 -05:00
|
|
|
crate::devices::Device::{Block, Character, Vterm},
|
2022-03-16 09:38:47 -05:00
|
|
|
core::fmt::{Arguments, Error, Write},
|
2022-08-08 12:07:43 -05:00
|
|
|
kernel::device_interface::CharacterDevice,
|
2022-03-16 09:38:47 -05:00
|
|
|
};
|
2022-03-16 05:39:01 -05:00
|
|
|
|
2022-03-16 09:38:47 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2022-04-25 15:34:52 -05:00
|
|
|
pub struct StdIo {
|
2022-03-16 09:38:47 -05:00
|
|
|
device: String,
|
2022-03-16 05:39:01 -05:00
|
|
|
}
|
|
|
|
|
2022-04-25 15:34:52 -05:00
|
|
|
impl StdIo {
|
|
|
|
pub fn new(device: String) -> StdIo {
|
|
|
|
StdIo { device }
|
2022-03-16 05:39:01 -05:00
|
|
|
}
|
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 05:39:01 -05:00
|
|
|
}
|
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-03-16 05:39:01 -05:00
|
|
|
}
|
2022-08-08 12:07:43 -05:00
|
|
|
Block(_) => todo!(),
|
2022-03-16 05:39:01 -05:00
|
|
|
}
|
|
|
|
}
|
2022-04-11 17:23:11 -05:00
|
|
|
|
2022-03-16 05:39:01 -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);
|
|
|
|
}
|
2022-08-08 12:07:43 -05:00
|
|
|
Block(_) => todo!(),
|
2022-04-19 02:15:45 -05:00
|
|
|
}
|
2022-03-16 05:39:01 -05:00
|
|
|
}
|
|
|
|
}
|