Skip to content

Commit 6f7096d

Browse files
committed
Add /dev/vga/mode device file
1 parent d655ee0 commit 6f7096d

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

src/api/fs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ fn device_type(name: &str) -> Result<DeviceType, ()> {
166166
"tcp" => Ok(DeviceType::TcpSocket),
167167
"udp" => Ok(DeviceType::UdpSocket),
168168
"vga-font" => Ok(DeviceType::VgaFont),
169+
"vga-mode" => Ok(DeviceType::VgaMode),
169170
"ata" => Ok(DeviceType::Drive),
170171
_ => Err(()),
171172
}

src/api/vga/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ pub mod palette;
44
pub use color::Color;
55
pub use palette::Palette;
66

7-
use crate::sys::vga;
7+
use crate::api::fs;
88
use crate::usr::shell;
99

1010
pub fn graphic_mode() {
11+
fs::write("/dev/vga/mode", b"320x200").ok();
12+
1113
// TODO: Backup font and palette
12-
vga::set_320x200_mode();
1314
}
1415

1516
pub fn text_mode() {
16-
vga::set_80x25_mode();
17+
fs::write("/dev/vga/mode", b"80x25").ok();
1718

1819
// TODO: Restore font and palette backup instead of this
1920
shell::exec("shell /ini/palettes/gruvbox-dark.sh").ok();

src/sys/fs/device.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::sys::console::Console;
1010
use crate::sys::net::socket::tcp::TcpSocket;
1111
use crate::sys::net::socket::udp::UdpSocket;
1212
use crate::sys::rng::Random;
13-
use crate::sys::vga::VgaFont;
13+
use crate::sys::vga::{VgaFont, VgaMode};
1414

1515
use alloc::vec;
1616
use alloc::vec::Vec;
@@ -31,6 +31,7 @@ pub enum DeviceType {
3131
UdpSocket = 8,
3232
Drive = 9,
3333
VgaFont = 10,
34+
VgaMode = 11,
3435
}
3536

3637
impl TryFrom<&[u8]> for DeviceType {
@@ -49,6 +50,7 @@ impl TryFrom<&[u8]> for DeviceType {
4950
8 => Ok(DeviceType::UdpSocket),
5051
9 => Ok(DeviceType::Drive),
5152
10 => Ok(DeviceType::VgaFont),
53+
11 => Ok(DeviceType::VgaMode),
5254
_ => Err(()),
5355
}
5456
}
@@ -87,6 +89,7 @@ pub enum Device {
8789
TcpSocket(TcpSocket),
8890
UdpSocket(UdpSocket),
8991
VgaFont(VgaFont),
92+
VgaMode(VgaMode),
9093
Drive(Drive),
9194
}
9295

@@ -105,6 +108,7 @@ impl TryFrom<&[u8]> for Device {
105108
DeviceType::TcpSocket => Ok(Device::TcpSocket(TcpSocket::new())),
106109
DeviceType::UdpSocket => Ok(Device::UdpSocket(UdpSocket::new())),
107110
DeviceType::VgaFont => Ok(Device::VgaFont(VgaFont::new())),
111+
DeviceType::VgaMode => Ok(Device::VgaMode(VgaMode::new())),
108112
DeviceType::Drive if buf.len() > 2 => {
109113
let bus = buf[1];
110114
let dsk = buf[2];
@@ -164,6 +168,7 @@ impl FileIO for Device {
164168
Device::TcpSocket(io) => io.read(buf),
165169
Device::UdpSocket(io) => io.read(buf),
166170
Device::VgaFont(io) => io.read(buf),
171+
Device::VgaMode(io) => io.read(buf),
167172
Device::Drive(io) => io.read(buf),
168173
}
169174
}
@@ -180,6 +185,7 @@ impl FileIO for Device {
180185
Device::TcpSocket(io) => io.write(buf),
181186
Device::UdpSocket(io) => io.write(buf),
182187
Device::VgaFont(io) => io.write(buf),
188+
Device::VgaMode(io) => io.write(buf),
183189
Device::Drive(io) => io.write(buf),
184190
}
185191
}
@@ -196,6 +202,7 @@ impl FileIO for Device {
196202
Device::TcpSocket(io) => io.close(),
197203
Device::UdpSocket(io) => io.close(),
198204
Device::VgaFont(io) => io.close(),
205+
Device::VgaMode(io) => io.close(),
199206
Device::Drive(io) => io.close(),
200207
}
201208
}
@@ -212,6 +219,7 @@ impl FileIO for Device {
212219
Device::TcpSocket(io) => io.poll(event),
213220
Device::UdpSocket(io) => io.poll(event),
214221
Device::VgaFont(io) => io.poll(event),
222+
Device::VgaMode(io) => io.poll(event),
215223
Device::Drive(io) => io.poll(event),
216224
}
217225
}

src/sys/vga/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod screen;
33
mod writer;
44

55
pub use font::VgaFont;
6-
pub use screen::{set_80x25_mode, set_320x200_mode, set_640x480_mode};
6+
pub use screen::VgaMode;
77
use writer::WRITER;
88

99
use crate::api::vga::color;

src/sys/vga/screen.rs

+38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::*;
22

3+
use crate::api::fs::{FileIO, IO};
4+
35
// Source: https://www.singlix.com/trdos/archive/vga/Graphics%20in%20pmode.pdf
46
const T_80_25: [u8; 61] = [
57
// MISC
@@ -127,8 +129,44 @@ pub fn set_80x25_mode() {
127129

128130
pub fn set_320x200_mode() {
129131
set_mode(&G_320_200_256);
132+
// TODO: Clear screen
130133
}
131134

132135
pub fn set_640x480_mode() {
133136
set_mode(&G_640_480_16);
137+
// TODO: Clear screen
138+
}
139+
140+
#[derive(Debug, Clone)]
141+
pub struct VgaMode;
142+
143+
impl VgaMode {
144+
pub fn new() -> Self {
145+
Self
146+
}
147+
}
148+
149+
impl FileIO for VgaMode {
150+
fn read(&mut self, _buf: &mut [u8]) -> Result<usize, ()> {
151+
Err(()) // TODO
152+
}
153+
154+
fn write(&mut self, buf: &[u8]) -> Result<usize, ()> {
155+
match buf {
156+
b"80x25" => set_80x25_mode(),
157+
b"320x200" => set_320x200_mode(),
158+
b"640x480" => set_640x480_mode(),
159+
_ => return Err(()),
160+
}
161+
Ok(buf.len())
162+
}
163+
164+
fn close(&mut self) {}
165+
166+
fn poll(&mut self, event: IO) -> bool {
167+
match event {
168+
IO::Read => false, // TODO
169+
IO::Write => true,
170+
}
171+
}
134172
}

src/usr/install.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub fn copy_files(verbose: bool) {
5656
create_dev("/dev/net/tcp", "tcp", verbose);
5757
create_dev("/dev/net/udp", "udp", verbose);
5858
create_dev("/dev/vga/font", "vga-font", verbose);
59+
create_dev("/dev/vga/mode", "vga-mode", verbose);
5960

6061
copy_file!("/ini/banner.txt", verbose);
6162
copy_file!("/ini/boot.sh", verbose);

0 commit comments

Comments
 (0)