Skip to content

Commit

Permalink
Add /dev/vga/mode device file
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Oct 14, 2024
1 parent d655ee0 commit 6f7096d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/api/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ fn device_type(name: &str) -> Result<DeviceType, ()> {
"tcp" => Ok(DeviceType::TcpSocket),
"udp" => Ok(DeviceType::UdpSocket),
"vga-font" => Ok(DeviceType::VgaFont),
"vga-mode" => Ok(DeviceType::VgaMode),
"ata" => Ok(DeviceType::Drive),
_ => Err(()),
}
Expand Down
7 changes: 4 additions & 3 deletions src/api/vga/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ pub mod palette;
pub use color::Color;
pub use palette::Palette;

use crate::sys::vga;
use crate::api::fs;
use crate::usr::shell;

pub fn graphic_mode() {
fs::write("/dev/vga/mode", b"320x200").ok();

// TODO: Backup font and palette
vga::set_320x200_mode();
}

pub fn text_mode() {
vga::set_80x25_mode();
fs::write("/dev/vga/mode", b"80x25").ok();

// TODO: Restore font and palette backup instead of this
shell::exec("shell /ini/palettes/gruvbox-dark.sh").ok();
Expand Down
10 changes: 9 additions & 1 deletion src/sys/fs/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::sys::console::Console;
use crate::sys::net::socket::tcp::TcpSocket;
use crate::sys::net::socket::udp::UdpSocket;
use crate::sys::rng::Random;
use crate::sys::vga::VgaFont;
use crate::sys::vga::{VgaFont, VgaMode};

use alloc::vec;
use alloc::vec::Vec;
Expand All @@ -31,6 +31,7 @@ pub enum DeviceType {
UdpSocket = 8,
Drive = 9,
VgaFont = 10,
VgaMode = 11,
}

impl TryFrom<&[u8]> for DeviceType {
Expand All @@ -49,6 +50,7 @@ impl TryFrom<&[u8]> for DeviceType {
8 => Ok(DeviceType::UdpSocket),
9 => Ok(DeviceType::Drive),
10 => Ok(DeviceType::VgaFont),
11 => Ok(DeviceType::VgaMode),
_ => Err(()),
}
}
Expand Down Expand Up @@ -87,6 +89,7 @@ pub enum Device {
TcpSocket(TcpSocket),
UdpSocket(UdpSocket),
VgaFont(VgaFont),
VgaMode(VgaMode),
Drive(Drive),
}

Expand All @@ -105,6 +108,7 @@ impl TryFrom<&[u8]> for Device {
DeviceType::TcpSocket => Ok(Device::TcpSocket(TcpSocket::new())),
DeviceType::UdpSocket => Ok(Device::UdpSocket(UdpSocket::new())),
DeviceType::VgaFont => Ok(Device::VgaFont(VgaFont::new())),
DeviceType::VgaMode => Ok(Device::VgaMode(VgaMode::new())),
DeviceType::Drive if buf.len() > 2 => {
let bus = buf[1];
let dsk = buf[2];
Expand Down Expand Up @@ -164,6 +168,7 @@ impl FileIO for Device {
Device::TcpSocket(io) => io.read(buf),
Device::UdpSocket(io) => io.read(buf),
Device::VgaFont(io) => io.read(buf),
Device::VgaMode(io) => io.read(buf),
Device::Drive(io) => io.read(buf),
}
}
Expand All @@ -180,6 +185,7 @@ impl FileIO for Device {
Device::TcpSocket(io) => io.write(buf),
Device::UdpSocket(io) => io.write(buf),
Device::VgaFont(io) => io.write(buf),
Device::VgaMode(io) => io.write(buf),
Device::Drive(io) => io.write(buf),
}
}
Expand All @@ -196,6 +202,7 @@ impl FileIO for Device {
Device::TcpSocket(io) => io.close(),
Device::UdpSocket(io) => io.close(),
Device::VgaFont(io) => io.close(),
Device::VgaMode(io) => io.close(),
Device::Drive(io) => io.close(),
}
}
Expand All @@ -212,6 +219,7 @@ impl FileIO for Device {
Device::TcpSocket(io) => io.poll(event),
Device::UdpSocket(io) => io.poll(event),
Device::VgaFont(io) => io.poll(event),
Device::VgaMode(io) => io.poll(event),
Device::Drive(io) => io.poll(event),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sys/vga/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod screen;
mod writer;

pub use font::VgaFont;
pub use screen::{set_80x25_mode, set_320x200_mode, set_640x480_mode};
pub use screen::VgaMode;
use writer::WRITER;

use crate::api::vga::color;
Expand Down
38 changes: 38 additions & 0 deletions src/sys/vga/screen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;

use crate::api::fs::{FileIO, IO};

// Source: https://www.singlix.com/trdos/archive/vga/Graphics%20in%20pmode.pdf
const T_80_25: [u8; 61] = [
// MISC
Expand Down Expand Up @@ -127,8 +129,44 @@ pub fn set_80x25_mode() {

pub fn set_320x200_mode() {
set_mode(&G_320_200_256);
// TODO: Clear screen
}

pub fn set_640x480_mode() {
set_mode(&G_640_480_16);
// TODO: Clear screen
}

#[derive(Debug, Clone)]
pub struct VgaMode;

impl VgaMode {
pub fn new() -> Self {
Self
}
}

impl FileIO for VgaMode {
fn read(&mut self, _buf: &mut [u8]) -> Result<usize, ()> {
Err(()) // TODO
}

fn write(&mut self, buf: &[u8]) -> Result<usize, ()> {
match buf {
b"80x25" => set_80x25_mode(),
b"320x200" => set_320x200_mode(),
b"640x480" => set_640x480_mode(),
_ => return Err(()),
}
Ok(buf.len())
}

fn close(&mut self) {}

fn poll(&mut self, event: IO) -> bool {
match event {
IO::Read => false, // TODO
IO::Write => true,
}
}
}
1 change: 1 addition & 0 deletions src/usr/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn copy_files(verbose: bool) {
create_dev("/dev/net/tcp", "tcp", verbose);
create_dev("/dev/net/udp", "udp", verbose);
create_dev("/dev/vga/font", "vga-font", verbose);
create_dev("/dev/vga/mode", "vga-mode", verbose);

copy_file!("/ini/banner.txt", verbose);
copy_file!("/ini/boot.sh", verbose);
Expand Down

0 comments on commit 6f7096d

Please sign in to comment.