Format code

This commit is contained in:
pjht 2024-09-11 13:50:03 -05:00
parent ec3200625e
commit 6144d2ef19
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -21,10 +21,17 @@
use core::str; use core::str;
use std::{ use std::{
borrow::Cow, collections::HashMap, fmt::{self, Write}, os::mikros::{address_space::ACTIVE_SPACE, ipc, syscalls}, usize borrow::Cow,
collections::HashMap,
fmt::{self, Write},
os::mikros::{address_space::ACTIVE_SPACE, ipc, syscalls},
usize,
}; };
use fontdue::{layout::{CoordinateSystem, GlyphRasterConfig, Layout, LayoutSettings, TextStyle, WrapStyle}, Font, FontSettings}; use fontdue::{
layout::{CoordinateSystem, GlyphRasterConfig, Layout, LayoutSettings, TextStyle, WrapStyle},
Font, FontSettings,
};
use itertools::Itertools; use itertools::Itertools;
use parking_lot::Mutex; use parking_lot::Mutex;
use x86_64::structures::paging::PageTableFlags; use x86_64::structures::paging::PageTableFlags;
@ -61,13 +68,19 @@ pub struct Bga {
yres: usize, yres: usize,
} }
unsafe impl Send for Bga {} unsafe impl Send for Bga {}
unsafe impl Sync for Bga {} unsafe impl Sync for Bga {}
impl Bga { impl Bga {
pub unsafe fn new(mmio_base: *mut u8, framebuffer_base: *mut u8) -> Self { pub unsafe fn new(mmio_base: *mut u8, framebuffer_base: *mut u8) -> Self {
Self { mmio_base, framebuffer_base, draw_buffer: Vec::new(), scroll_offset: 0, xres: 0, yres: 0} Self {
mmio_base,
framebuffer_base,
draw_buffer: Vec::new(),
scroll_offset: 0,
xres: 0,
yres: 0,
}
} }
// Changes the screenn resolution. Also clears the screen. // Changes the screenn resolution. Also clears the screen.
@ -80,7 +93,8 @@ impl Bga {
vbe_base.wrapping_add(VBE_BPP_NUM).write_volatile(0x20); vbe_base.wrapping_add(VBE_BPP_NUM).write_volatile(0x20);
vbe_base.wrapping_add(VBE_ENABLE_NUM).write_volatile(0x41); vbe_base.wrapping_add(VBE_ENABLE_NUM).write_volatile(0x41);
} }
self.draw_buffer.resize(xres as usize * yres as usize * 4 * 2, 0); self.draw_buffer
.resize(xres as usize * yres as usize * 4 * 2, 0);
self.draw_buffer.fill(0); self.draw_buffer.fill(0);
self.scroll_offset = 0; self.scroll_offset = 0;
self.xres = xres as usize; self.xres = xres as usize;
@ -97,18 +111,23 @@ impl Bga {
return; return;
} }
let row_offset = self.row_byte_offset(row + self.scroll_offset); let row_offset = self.row_byte_offset(row + self.scroll_offset);
self.draw_buffer[row_offset+col*4..][..4].fill(value); self.draw_buffer[row_offset + col * 4..][..4].fill(value);
} }
pub fn update_screen(&mut self) { pub fn update_screen(&mut self) {
let scroll_top_byte_offset = self.row_byte_offset(self.scroll_offset); let scroll_top_byte_offset = self.row_byte_offset(self.scroll_offset);
let scroll_bot_byte_offset = self.row_byte_offset(self.scroll_offset + self.yres); let scroll_bot_byte_offset = self.row_byte_offset(self.scroll_offset + self.yres);
let visible_buf = &self.draw_buffer[scroll_top_byte_offset..scroll_bot_byte_offset]; let visible_buf = &self.draw_buffer[scroll_top_byte_offset..scroll_bot_byte_offset];
unsafe { self.framebuffer_base.copy_from_nonoverlapping(&visible_buf[0], visible_buf.len()) }; unsafe {
self.framebuffer_base
.copy_from_nonoverlapping(&visible_buf[0], visible_buf.len())
};
} }
fn vbe_base(&self) -> *mut u16 { fn vbe_base(&self) -> *mut u16 {
self.mmio_base.wrapping_add(BGA_MMIO_VBE_OFFSET).cast::<u16>() self.mmio_base
.wrapping_add(BGA_MMIO_VBE_OFFSET)
.cast::<u16>()
} }
fn row_byte_offset(&self, row: usize) -> usize { fn row_byte_offset(&self, row: usize) -> usize {
@ -116,7 +135,7 @@ impl Bga {
} }
fn clear_raw_row(&mut self, row: usize) { fn clear_raw_row(&mut self, row: usize) {
if row >= self.yres*2 { if row >= self.yres * 2 {
return; return;
} }
let offset = self.row_byte_offset(row); let offset = self.row_byte_offset(row);
@ -138,12 +157,12 @@ impl Bga {
let old_offset = self.scroll_offset; let old_offset = self.scroll_offset;
self.scroll_offset += offset; self.scroll_offset += offset;
if self.scroll_offset >= self.yres { if self.scroll_offset >= self.yres {
for row in (old_offset + self.yres)..(self.yres*2) { for row in (old_offset + self.yres)..(self.yres * 2) {
self.clear_raw_row(row); self.clear_raw_row(row);
} }
self.scroll_offset -= self.yres; self.scroll_offset -= self.yres;
let half_buf_size = self.row_byte_offset(self.yres); let half_buf_size = self.row_byte_offset(self.yres);
let (scr_1,scr_2) = self.draw_buffer.split_at_mut(half_buf_size); let (scr_1, scr_2) = self.draw_buffer.split_at_mut(half_buf_size);
scr_1.copy_from_slice(scr_2); scr_1.copy_from_slice(scr_2);
for row in (self.yres)..(self.yres + self.scroll_offset) { for row in (self.yres)..(self.yres + self.scroll_offset) {
self.clear_raw_row(row); self.clear_raw_row(row);
@ -188,20 +207,21 @@ impl FramebufferWriter {
fn update_screen(&mut self, fbuf: &mut Bga) { fn update_screen(&mut self, fbuf: &mut Bga) {
self.layout.clear(); self.layout.clear();
self.layout.append(&[&self.font], &TextStyle::new(&self.out_string, 12.0, 0)); self.layout
.append(&[&self.font], &TextStyle::new(&self.out_string, 12.0, 0));
let text_height = self.layout.height(); let text_height = self.layout.height();
self.layout.append(&[&self.font], &TextStyle::new("\u{2588}", 12.0, 0)); self.layout
.append(&[&self.font], &TextStyle::new("\u{2588}", 12.0, 0));
for row in self.cursor_top..self.cursor_bot { for row in self.cursor_top..self.cursor_bot {
fbuf.clear_row(row); fbuf.clear_row(row);
} }
if (self.next_line_y + self.layout.height() as usize) > fbuf.yres { if (self.next_line_y + self.layout.height() as usize) > fbuf.yres {
let excess_y = let excess_y = self.layout.height() as usize - (fbuf.yres - self.next_line_y);
self.layout.height() as usize - (fbuf.yres - self.next_line_y);
fbuf.scroll_by(excess_y); fbuf.scroll_by(excess_y);
self.next_line_y -= excess_y; self.next_line_y -= excess_y;
} }
let glyphs = self.layout.glyphs(); let glyphs = self.layout.glyphs();
let lines = self.layout.lines().unwrap(); let lines = self.layout.lines().unwrap();
@ -238,16 +258,20 @@ impl FramebufferWriter {
let cursor_glyph = glyphs.last().unwrap(); let cursor_glyph = glyphs.last().unwrap();
self.cursor_top = cursor_glyph.y as usize - first_line_top_offset + self.next_line_y; self.cursor_top = cursor_glyph.y as usize - first_line_top_offset + self.next_line_y;
self.cursor_bot = cursor_glyph.y as usize - first_line_top_offset + self.next_line_y + cursor_glyph.height; self.cursor_bot = cursor_glyph.y as usize - first_line_top_offset
+ self.next_line_y
+ cursor_glyph.height;
let last_line = self.layout.lines().unwrap().last().unwrap(); let last_line = self.layout.lines().unwrap().last().unwrap();
let log_line_slice = if last_line.glyph_start == last_line.glyph_end { let log_line_slice = if last_line.glyph_start == last_line.glyph_end {
"" ""
} else { } else {
let first_char_offset = glyphs[last_line.glyph_start].byte_offset; let first_char_offset = glyphs[last_line.glyph_start].byte_offset;
let last_char_offset = glyphs[last_line.glyph_end-1].byte_offset; let last_char_offset = glyphs[last_line.glyph_end - 1].byte_offset;
let mut last_char_end = last_char_offset + 1; let mut last_char_end = last_char_offset + 1;
while last_char_end < self.out_string.len() && !self.out_string.is_char_boundary(last_char_end) { while last_char_end < self.out_string.len()
&& !self.out_string.is_char_boundary(last_char_end)
{
last_char_end += 1; last_char_end += 1;
} }
&self.out_string[first_char_offset..last_char_end] &self.out_string[first_char_offset..last_char_end]
@ -405,7 +429,11 @@ fn main() {
.unwrap(); .unwrap();
bga.set_resolution(1920, 1050); bga.set_resolution(1920, 1050);
let font = Font::from_bytes(include_bytes!("../FiraCode-Regular.ttf").as_slice(), FontSettings::default()).unwrap(); let font = Font::from_bytes(
include_bytes!("../FiraCode-Regular.ttf").as_slice(),
FontSettings::default(),
)
.unwrap();
let writer = FramebufferWriter::new(font, &bga); let writer = FramebufferWriter::new(font, &bga);
dev_driver_rpc::register_server(Box::new(DevServ)); dev_driver_rpc::register_server(Box::new(DevServ));