Skip to content

Commit

Permalink
WIP doom support
Browse files Browse the repository at this point in the history
  • Loading branch information
xor-bits committed Dec 11, 2023
1 parent 80a1595 commit b56ae36
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 116 deletions.
198 changes: 150 additions & 48 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions crates/kshell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ lock_api.workspace = true
glam = { version = "0.24", default-features = false, features = ["libm"] }
itertools = { version = "0.11", default-features = false }

serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

hyperion-arch.path = "../arch"
hyperion-color.path = "../color"
hyperion-driver-acpi.path = "../driver-acpi"
Expand Down
34 changes: 17 additions & 17 deletions crates/kshell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ macro_rules! load_elf_from {
//

pub async fn kshell() {
hyperion_futures::executor::spawn(spinner());
// hyperion_futures::executor::spawn(spinner());

// TODO: initrd

Expand Down Expand Up @@ -84,22 +84,22 @@ pub async fn kshell() {
term.flush();
}

async fn spinner() {
let mut ticks = ticks(Duration::milliseconds(50));
let mut rng = hyperion_random::next_fast_rng();

while ticks.next().await.is_some() {
let Some(fbo) = Framebuffer::get() else {
continue;
};
let mut fbo = fbo.lock();

let (r, g, b) = rng.gen();
let x = fbo.width - 20;
let y = fbo.height - 20;
fbo.fill(x, y, 10, 10, Color::new(r, g, b));
}
}
// async fn spinner() {
// let mut ticks = ticks(Duration::milliseconds(50));
// let mut rng = hyperion_random::next_fast_rng();

// while ticks.next().await.is_some() {
// let Some(fbo) = Framebuffer::get() else {
// continue;
// };
// let mut fbo = fbo.lock();

// let (r, g, b) = rng.gen();
// let x = fbo.width - 20;
// let y = fbo.height - 20;
// fbo.fill(x, y, 10, 10, Color::new(r, g, b));
// }
// }

//

Expand Down
99 changes: 83 additions & 16 deletions crates/kshell/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::{
sync::Arc,
vec::Vec,
};
use core::{fmt::Write, sync::atomic::Ordering};
use core::{fmt::Write, sync::atomic::Ordering, time::Duration};

use futures_util::stream::select;
use hyperion_color::Color;
Expand Down Expand Up @@ -469,8 +469,8 @@ impl Shell {

let (stdin_tx, stdin_rx) = channel();
let (stdout_tx, stdout_rx) = channel();
// let (stderr_tx, stderr_rx) = pipe();
let stderr_tx = stdout_tx.clone();
let (stderr_tx, stderr_rx) = channel();
// let stderr_tx = stdout_tx.clone();

let (o_tx, o_rx) = hyperion_futures::mpmc::channel();

Expand All @@ -492,6 +492,20 @@ impl Shell {
o_tx_2.send(None);
});

spawn(move || loop {
let mut buf = [0; 128];
let Ok(len) = stderr_rx.recv_slice(&mut buf) else {
debug!("end of stream");
break;
};
let Ok(str) = core::str::from_utf8(&buf[..len]) else {
debug!("invalid utf8");
break;
};

print!("{str}");
});

schedule(move || {
let name = name.as_ref();
hyperion_scheduler::rename(name);
Expand Down Expand Up @@ -526,11 +540,38 @@ impl Shell {
}
});

// hyperion_futures::executor::spawn(async move {
// while let Some(ev) = KeyboardEvents.next().await {
// hyperion_log::debug!("GOT {ev:?}");

// let mut str = [0; 4];

// if let Some(unicode) = ev.unicode {
// let str = unicode.encode_utf8(&mut str);

// // TODO: buffering
// if let Err(err) = stdin_tx.send_slice(str.as_bytes()) {
// hyperion_log::debug!("STDIN send err {err:?}");
// break;
// }
// }
// }
// });

// loop {
// hyperion_log::debug!("sleep");
// hyperion_futures::timer::sleep(time::Duration::SECOND).await;
// }

let mut events = select(KeyboardEvents.map(Ok), o_rx.race_stream().map(Err));

let mut l_ctrl_held = false;
loop {
match events.next().await {
// hyperion_log::debug!("Waiting for events ...");
let ev = events.next().await;
// hyperion_log::debug!("Event {ev:?}");

match ev {
Some(Ok(KeyboardEvent {
state,
keycode,
Expand All @@ -550,23 +591,47 @@ impl Shell {
break;
}

if let Some(unicode) = unicode {
_ = write!(self.term, "{unicode}");
self.term.flush();
// if let Some(unicode) = unicode {
// // _ = write!(self.term, "{unicode}");
// // self.term.flush();

let mut str = [0; 4];
// let mut str = [0; 4];

let str = unicode.encode_utf8(&mut str);
// let str = unicode.encode_utf8(&mut str);

// TODO: buffering
if stdin_tx.send_slice(str.as_bytes()).is_err() {
break;
}
// // TODO: buffering
// if let Err(err) = stdin_tx.send_slice(str.as_bytes()) {
// break;
// }
// }

#[derive(serde::Serialize, serde::Deserialize)]
struct KeyboardEventSer {
// pub scancode: u8,
state: u8,
keycode: u8,
unicode: Option<char>,
}

let ev = serde_json::to_string(&KeyboardEventSer {
state: state as u8,
keycode: keycode as u8,
unicode,
})
.unwrap();
// hyperion_log::debug!("sending {ev}");
if let Err(_) = stdin_tx.send_slice(ev.as_bytes()) {
// hyperion_log::debug!("stdin closed");
break;
}
if let Err(_) = stdin_tx.send_slice("\n".as_bytes()) {
// hyperion_log::debug!("stdin closed");
break;
}
}
Some(Err(Some(s))) => {
_ = write!(self.term, "{s}");
self.term.flush();
// _ = write!(self.term, "{s}");
// self.term.flush();
}
Some(Err(None)) => {
// _ = write!(self.term, "got EOI");
Expand All @@ -577,7 +642,9 @@ impl Shell {
}
}

stdin_tx.close();
hyperion_log::debug!("done");

// stdin_tx.close();

Ok(())
}
Expand Down
54 changes: 41 additions & 13 deletions crates/libstd/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use core::mem::ManuallyDrop;
use core_alloc::string::String;
use hyperion_syscall::{
close,
err::Result,
err::{Error, Result},
fs::{FileDesc, FileOpenFlags, Metadata},
metadata, open, open_dir, read, write,
};
use spin::{Mutex, MutexGuard};

use crate::io::{BufReader, BufWriter, ConstBufReader};
use crate::io::{self, BufReader, BufWriter, ConstBufReader};

//

Expand Down Expand Up @@ -99,6 +99,7 @@ impl Dir {

//

#[derive(Debug)]
pub struct DirEntry<'a> {
pub is_dir: bool, // TODO: mode flags later
pub size: usize,
Expand All @@ -107,8 +108,10 @@ pub struct DirEntry<'a> {

//

#[derive(Debug)]
pub struct File {
desc: FileDesc,
closed: bool,
}

impl File {
Expand All @@ -118,7 +121,10 @@ impl File {
///
/// this transfers the ownership of `desc` and will automatically close the file when dropped
pub const unsafe fn new(desc: FileDesc) -> Self {
Self { desc }
Self {
desc,
closed: false,
}
}

/// # Safety
Expand All @@ -132,7 +138,10 @@ impl File {
///
/// file i/o won't be automatically synchronized
pub const unsafe fn clone(&self) -> Self {
Self { desc: self.desc }
Self {
desc: self.desc,
closed: self.closed,
}
}

pub const fn as_desc(&self) -> FileDesc {
Expand All @@ -143,15 +152,12 @@ impl File {
OpenOptions::new().read(true).write(true).open(path)
}

pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
read(self.desc, buf)
}

pub fn write(&self, buf: &[u8]) -> Result<usize> {
write(self.desc, buf)
}
pub fn close(&mut self) -> Result<()> {
if self.closed {
return Err(Error::CLOSED);
}
self.closed = true;

pub fn close(&self) -> Result<()> {
close(self.desc)
}

Expand All @@ -162,9 +168,31 @@ impl File {
}
}

impl io::Read for File {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
if self.closed {
return Err(Error::CLOSED);
}
read(self.desc, buf)
}
}

impl io::Write for File {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
if self.closed {
return Err(Error::CLOSED);
}
write(self.desc, buf)
}

fn flush(&mut self) -> Result<()> {
Ok(())
}
}

impl Drop for File {
fn drop(&mut self) {
self.close().expect("failed to close the file");
close(self.desc).expect("failed to close the file");
}
}

Expand Down
46 changes: 33 additions & 13 deletions crates/libstd/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ use crate::fs::File;

pub trait Read {
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
}

impl Read for File {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
File::read(self, buf)
fn read_exact(&mut self, mut buf: &mut [u8], bytes_read: &mut usize) -> Result<()> {
while !buf.is_empty() {
match self.read(buf) {
Ok(0) => break,
Ok(n) => {
let tmp = buf;
buf = &mut tmp[n..];
*bytes_read += n;
}
Err(Error::INTERRUPTED) => {}
Err(err) => return Err(err),
}
}

if !buf.is_empty() {
Err(Error::UNEXPECTED_EOF)
} else {
Ok(())
}
}
}

Expand All @@ -28,17 +43,22 @@ impl<T: Read> Read for &mut T {
pub trait Write {
fn write(&mut self, buf: &[u8]) -> Result<usize>;

fn flush(&mut self) -> Result<()>;
}

impl Write for File {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
File::write(self, buf)
}

fn flush(&mut self) -> Result<()> {
fn write_exact(&mut self, mut buf: &[u8], bytes_written: &mut usize) -> Result<()> {
while !buf.is_empty() {
match self.write(buf) {
Ok(0) => return Err(Error::WRITE_ZERO),
Ok(n) => {
buf = &buf[n..];
*bytes_written += n;
}
Err(Error::INTERRUPTED) => {}
Err(err) => return Err(err),
}
}
Ok(())
}

fn flush(&mut self) -> Result<()>;
}

impl<T: Write> Write for &mut T {
Expand Down
2 changes: 1 addition & 1 deletion crates/libstd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ pub fn _eprint(args: fmt::Arguments) {

#[panic_handler]
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
println!("{info}");
eprintln!("{info}");
exit(-1);
}
Loading

0 comments on commit b56ae36

Please sign in to comment.