From 9fec34806e67bb631bd5c0325742b126ee8c855d Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Thu, 25 Dec 2025 16:51:25 -0500 Subject: [PATCH] better mouse demo --- kernel/src/ps2.c | 74 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/kernel/src/ps2.c b/kernel/src/ps2.c index 1c54e68..6da5884 100644 --- a/kernel/src/ps2.c +++ b/kernel/src/ps2.c @@ -16,40 +16,74 @@ */ #include +#include #include //defined in ps2.asm //returns -1 if no byte available int read_ps2_byte(); -static uint32_t color = 1; +static void process_keyboard_byte(uint8_t byte) { + (void)byte; + //TODO +} -static void change_color() { +static uint8_t mouse_packet[3]; +static int mouse_packet_length = 0; - color ^= color << 13; - color ^= color >> 17; - color ^= color << 5; +static int total_x = 0; +static int total_y = 0; - for (int y = 0; y < fb_height; ++y) - for (int x = 0; x < fb_width; ++x) - *(uint32_t *)&fb_base[y * fb_pitch + x * 4] = color & 0x00ffffff; +static void process_mouse_byte(uint8_t byte) { + + mouse_packet[mouse_packet_length] = byte; + if (mouse_packet_length < 2) { + ++mouse_packet_length; + return; + } + mouse_packet_length = 0; + + int x = mouse_packet[1]; + if (mouse_packet[0] & 0x10) + x -= 256; + + int y = mouse_packet[2]; + if (mouse_packet[0] & 0x20) + y -= 256; + + total_x += x; + total_y -= y; + + if (total_x < 0) + total_x = 0; + if (total_x >= fb_width) + total_x = fb_width - 1; + + if (total_y < 0) + total_y = 0; + if (total_y >= fb_height) + total_y = fb_height - 1; + + fb_base[total_y * fb_pitch + total_x * 4] = 0xff; + fb_base[total_y * fb_pitch + total_x * 4 + 1] = 0xff; + fb_base[total_y * fb_pitch + total_x * 4 + 2] = 0xff; } void on_keyboard_irq() { - - while (read_ps2_byte() != -1) - ; - - change_color(); - + while (1) { + int byte = read_ps2_byte(); + if (byte == -1) + return; + process_keyboard_byte(byte); + } } void on_mouse_irq() { - - while (read_ps2_byte() != -1) - ; - - change_color(); - + while (1) { + int byte = read_ps2_byte(); + if (byte == -1) + return; + process_mouse_byte(byte); + } }