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

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 {
left_button_pressed: AtomicU8,
@@ -9,8 +9,15 @@ pub struct Mouse {
x_delta: AtomicI16,
y_delta: AtomicI16,
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 {
const fn new() -> Mouse {
Mouse {
@@ -20,6 +27,8 @@ impl Mouse {
x_delta: AtomicI16::new(0),
y_delta: AtomicI16::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 {
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();