not working

This commit is contained in:
2025-02-27 01:16:07 +00:00
parent ac0b47a45c
commit 3b6e272fd2
22 changed files with 448 additions and 50 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ libk is an attempt to move away from the godforsaken git submodules that plagued
A lot of things must be improved and worked on, feel free to add extra TODOs below.
[] Create a kernel logging abstraction similar to Linux. (this should probably just use the serial drivers)
- [] Create a kernel logging abstraction similar to Linux. (this should probably just use the serial drivers)
The rationale behind this is that we want some sort of debug log/tracing support so we aren't just printf debugging forever.
+1 -1
View File
@@ -72,7 +72,7 @@ impl Writer {
}
// Get the character data from the font array. -- each byte is a row of pixels
let data: &[u8] = &self.font.0[c as usize];
let data: &[u8] = self.font.glyph_for(c as u16);
if let Some(writer) = FRAMEBUFFER_WRITER.lock().as_mut() {
for (row, line) in data.iter().enumerate().take(16) {
+9 -2
View File
@@ -61,9 +61,16 @@ impl FramebufferWriter {
}
}
pub fn render_frame(&self, buffer: &[Colour; 1280 * 800]) {
for (y, row) in buffer.chunks(1280).enumerate() {
pub fn render_frame(&self, buffer: &[&[Colour]]) {
// TODO: this should return errors
for (y, &row) in buffer.iter().enumerate() {
if y >= self.height() as usize {
break;
}
for (x, pixel) in row.iter().enumerate() {
if x >= self.width() as usize {
break;
}
self.write_pixel(x, y, *pixel);
}
}
+42 -7
View File
@@ -2,12 +2,47 @@ use libm::include_font;
pub mod ibm_vga_8x16;
pub static FONT_SPLEEN_8X16: Font = Font(include_font!(
"./libk/resources/font/spleen-8x16.psf"
));
pub static FONT_SPLEEN_8X16: Font =
Font::new(include_font!("./libk/resources/font/spleen-8x16.psf"));
pub static FONT_CP850_8X16: Font = Font(include_font!(
"./libk/resources/font/cp850-8x16.psf"
));
pub static FONT_CP850_8X16: Font = Font::new(include_font!("./libk/resources/font/cp850-8x16.psf"));
pub struct Font(pub [[u8; 16]; 512]);
// pub struct Font(pub [[u8; 16]; 512]);
pub struct Font {
width: usize,
height: usize,
length: u16,
data: [[u8; 16]; 512],
}
impl Font {
pub const fn new(data: [[u8; 16]; 512]) -> Font {
Font {
width: 8,
height: 16,
length: data.len() as u16,
data,
}
}
pub const fn glyph_for(&self, c: u16) -> &[u8] {
if c > self.length {
return &self.data[0];
}
&self.data[c as usize]
}
pub const fn width(&self) -> usize {
self.width
}
pub const fn height(&self) -> usize {
self.height
}
pub const fn default() -> &'static Font {
&FONT_CP850_8X16
}
}
+3 -3
View File
@@ -1,8 +1,8 @@
use crate::prelude::*;
mod frame;
mod render;
mod window;
pub mod frame;
pub mod render;
pub mod window;
pub trait Application {
type Output;
+26 -5
View File
@@ -1,19 +1,40 @@
use crate::{drivers::io::framebuffer::colour::Colour, std::maths::geometry::Vec2};
use super::{
render::{ColouredChar, RenderError},
window::Window,
window::{self, Window},
};
use alloc::vec::Vec;
use alloc::{vec, vec::Vec};
pub struct Frame<'f> {
data: Vec<Vec<ColouredChar>>,
data: Vec<Vec<Colour>>,
window: &'f Window,
}
impl Frame<'_> {
impl<'a> Frame<'a> {
pub fn new(window: &'a Window) -> Self {
Self {
data: vec![vec![Colour::Black; window.dimensions().x()]; window.dimensions().y()],
window,
}
}
pub fn render(&self) -> Result<(), RenderError> {
let data: Vec<&[ColouredChar]> = self.data.iter().map(|v| v.as_slice()).collect::<Vec<_>>();
let data: Vec<&[Colour]> = self.data.iter().map(|v| v.as_slice()).collect::<Vec<_>>();
self.window
.render(data.as_slice())
.map_err(|_| RenderError::Generic)
}
pub fn write_pixel(&mut self, x: usize, y: usize, color: Colour) -> Result<(), RenderError> {
if x >= self.window.dimensions().x() || y >= self.window.dimensions().y() {
return Err(RenderError::Generic);
}
self.data[y][x] = color;
Ok(())
}
pub const fn dimensions(&self) -> Vec2<usize> {
self.window.dimensions()
}
}
+14 -4
View File
@@ -1,6 +1,12 @@
use crate::{prelude::*, std::maths::geometry::Vec2};
use limine::framebuffer;
use super::render::{ColouredChar, RenderError};
use crate::{
drivers::io::framebuffer::{colour::Colour, display::FRAMEBUFFER_WRITER},
prelude::*,
std::maths::geometry::Vec2,
};
use super::render::RenderError;
pub struct Window {
dimensions: Vec2<usize>,
@@ -21,8 +27,12 @@ impl Window {
}
}
pub fn render(&self, _data: &[&[ColouredChar]]) -> Result<(), RenderError> {
todo!();
pub fn render(&self, _data: &[&[Colour]]) -> Result<(), RenderError> {
// TODO: error handling!! the kernel should return an error in some cases
if let Some(fb) = FRAMEBUFFER_WRITER.lock().as_mut() {
fb.render_frame(_data);
}
Ok(())
}
pub const fn is_bordered(&self) -> bool {
+59
View File
@@ -0,0 +1,59 @@
use alloc::vec::Vec;
use crate::{drivers::io::framebuffer::colour::Colour, resources::font::Font};
use super::{
application::{frame::Frame, render::RenderError},
maths::geometry::Vec2,
};
pub struct Writer<'a> {
font: &'a Font,
}
impl<'a> Writer<'a> {
pub const fn new(font: &'a Font) -> Self {
Self { font }
}
pub const fn set_font(&mut self, font: &'a Font) {
self.font = font;
}
pub const fn font_size(&self) -> Vec2<usize> {
Vec2::new(self.font.width(), self.font.height())
}
pub fn render_glyph(
&self,
frame: &mut Frame,
pos: Vec2<usize>,
c: u8,
scale: usize,
) -> Result<(), RenderError> {
// get a reference to the character glyph from the font.
let data: &[u8] = self.font.glyph_for(c as u16);
if pos.x() + self.font.width() * scale > frame.dimensions().x()
|| pos.y() + self.font.height() * scale > frame.dimensions().y()
{
return Err(RenderError::Generic);
}
for (row, line) in data.iter().enumerate().take(self.font.height()) {
for col in 0..self.font.width() {
let pixel_x: usize = pos.x() + col * scale;
let pixel_y: usize = pos.y() + row * scale;
if line & (0x80 >> col) != 0 {
for i in 0..scale {
for j in 0..scale {
frame.write_pixel(pixel_x + i, pixel_y + j, Colour::White)?;
}
}
}
}
}
Ok(())
}
}
+6
View File
@@ -71,3 +71,9 @@ impl<T: Coordinate> DivAssign<T> for Vec2<T> {
self.y /= rhs;
}
}
impl<T: Coordinate> From<Vec2<T>> for (T, T) {
fn from(value: Vec2<T>) -> Self {
(value.x, value.y)
}
}
+1
View File
@@ -1,3 +1,4 @@
pub mod application;
pub mod ascii;
pub mod io;
pub mod maths;
+174
View File
@@ -0,0 +1,174 @@
use crate::drivers::io::keyboard::{KeyStroke, get_keystroke_async};
use crate::prelude::*;
use crate::resources::font::{FONT_CP850_8X16, FONT_SPLEEN_8X16, Font};
use crate::std::application::frame::Frame;
use crate::std::application::render::RenderError;
use crate::std::application::window::Window;
use crate::std::application::{Application, Error};
use crate::std::ascii::Writer;
use crate::std::maths::geometry::Vec2;
use alloc::string::ToString;
pub struct Editor {
cursor_line: usize,
cursor_col: usize,
mode: Mode,
buffer: String,
window: Window,
}
impl Default for Editor {
fn default() -> Self {
Self::new()
}
}
impl<'a> Editor {
const PADDING: usize = 8;
pub const fn new() -> Self {
Self {
cursor_line: 0,
cursor_col: 0,
mode: Mode::Nor,
buffer: String::new(),
window: Window::new(),
}
}
fn render(&'a self) -> Result<Frame<'a>, RenderError> {
let mut frame = Frame::new(&self.window);
let writer = Writer::new(Font::default());
let (width, height) = writer.font_size().into();
let mut col = Self::PADDING;
let mut line = Self::PADDING;
let mut scale = 1;
for ch in self.buffer.chars() {
if ch == '\n' {
line += scale * height;
col = Self::PADDING;
scale = 1;
continue;
}
if width * scale + col > frame.dimensions().x() {
line += scale * height;
col = Self::PADDING;
}
writer.render_glyph(&mut frame, Vec2::new(col, line), ch as u8, scale)?;
col += scale * width;
}
Ok(frame)
}
fn get_lines(&self) -> Vec<&str> {
self.buffer.split('\n').collect::<Vec<&str>>();
todo!()
}
#[allow(unused_variables, dead_code, clippy::needless_pass_by_ref_mut)]
fn move_cursor(&mut self, x: i32, y: i32) {
todo!()
}
#[allow(unused_variables, dead_code, clippy::needless_pass_by_ref_mut)]
fn delete_char(&mut self) {
todo!()
}
#[allow(unused_variables, dead_code, clippy::needless_pass_by_ref_mut)]
fn insert_char(&mut self, c: char) {
todo!()
}
#[allow(unused_variables, dead_code, clippy::needless_pass_by_ref_mut)]
fn splitline(&mut self) {
todo!()
}
}
impl Application for Editor {
type Output = ();
async fn run(&mut self, _args: Vec<alloc::string::String>) -> Result<Self::Output, Error> {
self.window.set_dimensions(Vec2::new(1280, 800));
self.window.set_position(Vec2::new(0, 0));
self.window.open();
self.buffer = "Hello world, this is a test init. idk test \n ewntuiewi gjk gfdfg gndf ngdfgnmdfg ndfgmndfg gdfndfnkg njkdgjkndfjnkg ngnjfgnfgnfg fgn fn gfj gnfg jnfgjfngjk fgnjfgnjk jnkdgjnkdfg gfnd njkgdfgjn d fjnkgjkndfgjkndfgjn gndfjnk njkgdfng jnkfgdjknd jnfkgnjk".to_string();
loop {
if let Err(_err) = self.render().and_then(|frame| frame.render()) {
// TODO: Handle error
return Err(Error::ApplicationFailed("Rendering failed".to_string()));
}
let keystroke = get_keystroke_async().await;
match self.mode {
Mode::Nor => match keystroke {
KeyStroke::Char('i') => self.mode = Mode::Ins,
KeyStroke::Char('`') => return Ok(()),
_ => {}
},
Mode::Ins => {
match keystroke {
KeyStroke::Char(c) => {
match c {
// escape
'\x1B' => self.mode = Mode::Nor,
// delete
'\x7F' => self.delete_char(),
// backspace
'\x08' => {
self.move_cursor(-1, 0);
self.delete_char();
}
// enter
'\n' => self.splitline(),
_ => {
self.insert_char(c);
self.move_cursor(1, 0);
}
}
}
KeyStroke::Left => {
self.move_cursor(-1, 0);
}
KeyStroke::Right => {
self.move_cursor(1, 0);
}
KeyStroke::Up => {
self.move_cursor(0, -1);
}
KeyStroke::Down => {
self.move_cursor(0, 1);
}
KeyStroke::None => {}
_ => {}
}
}
}
}
}
}
pub enum Mode {
Nor,
Ins,
}
impl core::fmt::Display for Mode {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Nor => write!(f, "Normal"),
Self::Ins => write!(f, "Insert"),
}
}
}
+1
View File
@@ -1 +1,2 @@
pub mod editor;
pub mod shell;
+10 -1
View File
@@ -1,6 +1,11 @@
// use x86_64::registers::rflags::read;
use crate::{drivers::io::ascii::clear_screen, prelude::stdin::read_line, print, println};
use alloc::vec::Vec;
use crate::{
drivers::io::ascii::clear_screen, prelude::stdin::read_line, print, println,
std::application::Application, util::editor::Editor,
};
static FETCH: &str = "
$$$$$$$$\\ $$\\
@@ -31,6 +36,10 @@ pub async fn shell() {
"fetch" => {
println!("{}", FETCH);
}
"editor" => {
let mut editor = Editor::new();
editor.run(Vec::new()).await.unwrap();
}
"clear" => clear_screen(),
_ => {
println!("Unknown command: {}", line);