use inputd::ProducerHandle; use redox_scheme::scheme::SchemeSync; use redox_scheme::{CallerCtx, OpenResult}; use std::str; use syscall::data::Stat; use syscall::schemev2::NewFdFlags; use syscall::{Error, Result, EACCES, EINVAL, MODE_CHR}; use crate::bga::Bga; pub struct BgaScheme { pub bga: Bga, pub display: Option, } const SCHEME_ROOT_ID: usize = 1; impl SchemeSync for BgaScheme { fn scheme_root(&mut self) -> Result { Ok(SCHEME_ROOT_ID) } fn openat( &mut self, dirfd: usize, _path: &str, _flags: usize, _fcntl_flags: u32, ctx: &CallerCtx, ) -> Result { if dirfd != SCHEME_ROOT_ID { return Err(Error::new(EACCES)); } if ctx.uid == 0 { Ok(OpenResult::ThisScheme { number: 0, flags: NewFdFlags::empty(), }) } else { Err(Error::new(EACCES)) } } fn read( &mut self, _id: usize, buf: &mut [u8], _offset: u64, _fcntl_flags: u32, _ctx: &CallerCtx, ) -> Result { let mut i = 0; let data = format!("{},{}\n", self.bga.width(), self.bga.height()).into_bytes(); while i < buf.len() && i < data.len() { buf[i] = data[i]; i += 1; } Ok(i) } fn write( &mut self, _id: usize, buf: &[u8], _offset: u64, _fcntl_flags: u32, _ctx: &CallerCtx, ) -> Result { let string = str::from_utf8(buf).or(Err(Error::new(EINVAL)))?; let string = string.trim(); let mut parts = string.split(','); let width = if let Some(part) = parts.next() { part.parse::().or(Err(Error::new(EINVAL)))? } else { self.bga.width() }; let height = if let Some(part) = parts.next() { part.parse::().or(Err(Error::new(EINVAL)))? } else { self.bga.height() }; self.bga.set_size(width, height); Ok(buf.len()) } fn fpath(&mut self, _file: usize, buf: &mut [u8], _ctx: &CallerCtx) -> Result { let mut i = 0; let scheme_path = b"bga"; while i < buf.len() && i < scheme_path.len() { buf[i] = scheme_path[i]; i += 1; } Ok(i) } fn fstat(&mut self, _id: usize, stat: &mut Stat, _ctx: &CallerCtx) -> Result<()> { *stat = Stat { st_mode: MODE_CHR | 0o666, ..Default::default() }; Ok(()) } fn fcntl(&mut self, _id: usize, _cmd: usize, _arg: usize, _ctx: &CallerCtx) -> Result { Ok(0) } } impl BgaScheme { pub fn on_close(&mut self, _id: usize) {} }