Remove old next variable from the frame allocator, add a mouse bitmap

for drawing and move mouse drawing and updating functions to the mouse
struct.
This commit is contained in:
csd4ni3l
2026-03-29 15:59:37 +02:00
parent 3ef95940a7
commit 91828daae2
4 changed files with 72 additions and 46 deletions

BIN
assets/cursors/default.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@@ -24,7 +24,6 @@ pub unsafe fn initialize_paging(physical_memory_offset: VirtAddr) -> OffsetPageT
} }
pub struct XunilFrameAllocator<'a> { pub struct XunilFrameAllocator<'a> {
next: usize,
memory_map: &'a [&'a Entry], memory_map: &'a [&'a Entry],
region_index: usize, region_index: usize,
region_offset: usize, region_offset: usize,
@@ -38,7 +37,6 @@ impl<'a> XunilFrameAllocator<'a> {
.unwrap(); .unwrap();
Self { Self {
next: 0,
memory_map, memory_map,
region_index, region_index,
region_offset: 0, region_offset: 0,

View File

@@ -1,6 +1,6 @@
use core::sync::atomic::{AtomicI16, AtomicU8, Ordering}; use core::sync::atomic::{AtomicI16, AtomicU8, AtomicUsize, Ordering};
use spin::Mutex; use crate::driver::graphics::{base::rgb, framebuffer::Framebuffer};
pub struct Mouse { pub struct Mouse {
left_button_pressed: AtomicU8, left_button_pressed: AtomicU8,
@@ -9,8 +9,15 @@ pub struct Mouse {
x_delta: AtomicI16, x_delta: AtomicI16,
y_delta: AtomicI16, y_delta: AtomicI16,
status: AtomicU8, status: AtomicU8,
mouse_x: AtomicUsize,
mouse_y: AtomicUsize,
} }
static CURSOR_BYTES: &[u8] = include_bytes!("../../../assets/cursors/default.bmp");
const BMP_HEADER_SIZE: usize = 138;
const CURSOR_W: usize = 24;
const CURSOR_H: usize = 24;
impl Mouse { impl Mouse {
const fn new() -> Mouse { const fn new() -> Mouse {
Mouse { Mouse {
@@ -20,6 +27,8 @@ impl Mouse {
x_delta: AtomicI16::new(0), x_delta: AtomicI16::new(0),
y_delta: AtomicI16::new(0), y_delta: AtomicI16::new(0),
status: AtomicU8::new(0), status: AtomicU8::new(0),
mouse_x: AtomicUsize::new(100),
mouse_y: AtomicUsize::new(100),
} }
} }
@@ -62,6 +71,61 @@ impl Mouse {
pub fn get_status(&self) -> u8 { pub fn get_status(&self) -> u8 {
self.status.load(Ordering::Relaxed) self.status.load(Ordering::Relaxed)
} }
pub fn update(&self, width: usize, height: usize) {
let x_delta = self.get_x_delta() / 5;
let y_delta = self.get_y_delta() / 5;
if x_delta != 0 {
self.mouse_x.store(
(self.mouse_x.load(Ordering::Relaxed) as isize + x_delta as isize).max(0) as usize,
Ordering::Relaxed,
);
}
if y_delta != 0 {
self.mouse_y.store(
(self.mouse_y.load(Ordering::Relaxed) as isize + y_delta as isize).max(0) as usize,
Ordering::Relaxed,
);
}
if self.mouse_x.load(Ordering::Relaxed) > width {
self.mouse_x.store(width - CURSOR_W, Ordering::Relaxed);
}
if self.mouse_y.load(Ordering::Relaxed) > height {
self.mouse_y.store(height - CURSOR_H, Ordering::Relaxed);
}
}
pub fn draw(&self, fb: &mut Framebuffer) {
let pixels = &CURSOR_BYTES[BMP_HEADER_SIZE..]; // remove header
for row in 0..CURSOR_H {
let src_row = (CURSOR_H - 1 - row) * CURSOR_W * 4;
for col in 0..CURSOR_W {
let i = src_row + col * 4; // 4 because rgba
let b = pixels[i];
let g = pixels[i + 1];
let r = pixels[i + 2];
let a = pixels[i + 3];
if a < 255 {
continue;
}
let color = rgb(r, g, b);
fb.put_pixel(
(self.mouse_x.load(Ordering::Relaxed) + col) as usize,
(self.mouse_y.load(Ordering::Relaxed) + row) as usize,
color,
);
}
}
}
} }
pub static MOUSE: Mouse = Mouse::new(); pub static MOUSE: Mouse = Mouse::new();

View File

@@ -129,74 +129,38 @@ unsafe extern "C" fn kmain() -> ! {
boot_animation(); boot_animation();
let x = Box::new(41);
let mut test_vec: Vec<u16> = Vec::new();
test_vec.push(5);
println!("Before: {:?}", test_vec);
test_vec.push(9);
println!("After: {:?}", test_vec);
sleep(500);
let mut current_mouse_x: usize = 100;
let mut current_mouse_y: usize = 100;
let mut mouse_status = 0; let mut mouse_status = 0;
let mut width = 0; let mut width = 0;
let mut height = 0; let mut height = 0;
let mut x_delta = 0;
let mut y_delta = 0;
loop { loop {
with_serial_console(|serial_console| serial_console.clear(0, 0)); with_serial_console(|serial_console| serial_console.clear(0, 0));
with_framebuffer(|fb| fb.clear(rgb(253, 129, 0))); with_framebuffer(|fb| fb.clear(rgb(253, 129, 0)));
test_performance(|| { test_performance(|| {
mouse_status = MOUSE.get_status();
with_framebuffer(|mut fb| { with_framebuffer(|mut fb| {
width = fb.width; width = fb.width;
height = fb.height; height = fb.height;
MOUSE.update(width, height);
rectangle_filled(&mut fb, 700, 400, 200, 200, rgb(0, 0, 0)); rectangle_filled(&mut fb, 700, 400, 200, 200, rgb(0, 0, 0));
rectangle_outline(&mut fb, 400, 400, 100, 100, rgb(0, 0, 0)); rectangle_outline(&mut fb, 400, 400, 100, 100, rgb(0, 0, 0));
circle_filled(&mut fb, 200, 200, 100, rgb(0, 0, 0)); circle_filled(&mut fb, 200, 200, 100, rgb(0, 0, 0));
circle_outline(&mut fb, 400, 200, 100, rgb(0, 0, 0)); circle_outline(&mut fb, 400, 200, 100, rgb(0, 0, 0));
triangle_outline(&mut fb, 100, 400, 200, 400, 150, 600, rgb(0, 0, 0)); triangle_outline(&mut fb, 100, 400, 200, 400, 150, 600, rgb(0, 0, 0));
MOUSE.draw(fb);
}); });
let (hours, minutes, seconds) = let (hours, minutes, seconds) =
unix_to_hms(TIMER.get_date_at_boot() + (TIMER.now().elapsed()) / 1000); unix_to_hms(TIMER.get_date_at_boot() + (TIMER.now().elapsed()) / 1000);
mouse_status = MOUSE.get_status();
x_delta = MOUSE.get_x_delta() / 5;
y_delta = MOUSE.get_y_delta() / 5;
if x_delta != 0 {
current_mouse_x = (current_mouse_x as isize + x_delta as isize).max(0) as usize;
}
if y_delta != 0 {
current_mouse_y = (current_mouse_y as isize + y_delta as isize).max(0) as usize;
}
if current_mouse_x > width {
current_mouse_x = width - 15;
}
if current_mouse_y > height {
current_mouse_y = height - 15;
}
print!( print!(
"{:?}:{:?}:{:?}\n{:?} {:?} {:?} {:?} {:?}", "{:?}:{:?}:{:?}\nMouse status: {:?}",
hours, hours, minutes, seconds, mouse_status
minutes,
seconds,
mouse_status,
current_mouse_x,
current_mouse_y,
x_delta,
y_delta
); );
}); });
with_framebuffer(|fb| { with_framebuffer(|fb| {
rectangle_filled(fb, current_mouse_x, current_mouse_y, 15, 15, rgb(0, 255, 0));
fb.swap(); fb.swap();
}); });
sleep(16); sleep(16);