Skip to content

Commit

Permalink
kernel/window: load mouse cursor from image file
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Aug 12, 2023
1 parent 59fb818 commit 945ed08
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
1 change: 1 addition & 0 deletions kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ add_custom_command(
"${ISO_BUILD_SCRIPT}"
${CMAKE_CURRENT_SOURCE_DIR}/../limine.cfg
${CMAKE_CURRENT_SOURCE_DIR}/assets/font.qoi
${CMAKE_CURRENT_SOURCE_DIR}/assets/mouse.qoi
kernel
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../"
VERBATIM
Expand Down
Binary file added kernel/assets/mouse.qoi
Binary file not shown.
44 changes: 15 additions & 29 deletions kernel/window/manager.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "kernel/window/qoi.hpp"
#include <kernel/device/pit.hpp>
#include <kernel/log.hpp>
#include <kernel/modules.hpp>
Expand Down Expand Up @@ -56,37 +57,11 @@ void WindowManager::handle_mouse(Point off, bool pressed) {
}
}

constexpr u32 EMPTY = 0x0;
constexpr u32 BLACK = 0xFF000000;
constexpr u32 WHITE = 0xFFFFFFFF;

constexpr usize MOUSE_WIDTH = 9;
constexpr usize MOUSE_HEIGHT = 13;

// clang-format off
u32 mouse_sprite[MOUSE_WIDTH * MOUSE_HEIGHT] = {
BLACK, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
BLACK, BLACK, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
BLACK, WHITE, BLACK, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
BLACK, WHITE, WHITE, BLACK, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
BLACK, WHITE, WHITE, WHITE, BLACK, EMPTY, EMPTY, EMPTY, EMPTY,
BLACK, WHITE, WHITE, WHITE, WHITE, BLACK, EMPTY, EMPTY, EMPTY,
BLACK, WHITE, WHITE, WHITE, WHITE, WHITE, BLACK, EMPTY, EMPTY,
BLACK, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, BLACK, EMPTY,
BLACK, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, BLACK, BLACK,
BLACK, WHITE, WHITE, WHITE, WHITE, BLACK, BLACK, EMPTY, EMPTY,
BLACK, WHITE, BLACK, BLACK, WHITE, BLACK, EMPTY, EMPTY, EMPTY,
BLACK, BLACK, EMPTY, EMPTY, BLACK, WHITE, BLACK, EMPTY, EMPTY,
EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLACK, EMPTY, EMPTY, EMPTY,
};

// clang-format on

void WindowManager::draw_mouse() {
Canvas mouse_canvas(mouse_sprite, MOUSE_WIDTH, MOUSE_HEIGHT);
const auto old_mouse_rect = Rect(m_prev_mouse_pos, Point(MOUSE_WIDTH, MOUSE_HEIGHT));
const auto old_mouse_rect =
Rect(m_prev_mouse_pos, Point(m_mouse_canvas.width(), m_mouse_canvas.height()));
Widget::paint(Span(&old_mouse_rect, 1), true);
m_context->paste_alpha_masked(mouse_canvas, m_mouse_pos.x, m_mouse_pos.y);
m_context->paste_alpha_masked(m_mouse_canvas, m_mouse_pos.x, m_mouse_pos.y);
}

BitmapFont get_default_font() {
Expand All @@ -98,6 +73,17 @@ WindowManager::WindowManager(WindowContext context) :
m_font(get_default_font()) {
m_mouse_pos = this->rect().mid_point();
m_context = &m_real_context;

QOIStreamDecoder decoder(Modules::get().with_path("/assets/mouse.qoi").data);

m_mouse_data.reserve(decoder.width() * decoder.height());

while (!decoder.finished()) {
m_mouse_data.push(decoder.next_pixel());
}

m_mouse_canvas =
Canvas(reinterpret_cast<u32*>(m_mouse_data.data()), decoder.width(), decoder.height());
}

bool manager_initialized = false;
Expand Down
4 changes: 4 additions & 0 deletions kernel/window/manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class WindowManager : public Widget {

BitmapFont m_font;

// TODO: wrap this into an image class?
Vector<Color> m_mouse_data;
Canvas m_mouse_canvas = Canvas(nullptr, 0, 0);

WindowManager(WindowContext context);

public:
Expand Down
1 change: 1 addition & 0 deletions limine.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ TIMEOUT=0
KERNEL_PATH=boot:///kernel

MODULE_PATH=boot:///assets/font.qoi
MODULE_PATH=boot:///assets/mouse.qoi

# Same thing, but with KASLR.
:MAT os (KASLR on)
Expand Down

0 comments on commit 945ed08

Please sign in to comment.