Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.

Commit

Permalink
Fixed mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinAlavik committed Mar 5, 2024
1 parent b910ec2 commit bb3e795
Showing 1 changed file with 83 additions and 40 deletions.
123 changes: 83 additions & 40 deletions src/system/drivers/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,71 @@ char *mouse_img = NULL;
uint32_t mouse_img_size = 0;
bool mouse_img_parsed = false;

void mouse_wait_write() {
while ((inb8(0x64) & 2) != 0) {
void mouse_wait_write()
{
while ((inb8(0x64) & 2) != 0)
{
;
}
}

void mouse_wait_read() {
while ((inb8(0x64) & 1) != 1) {
void mouse_wait_read()
{
while ((inb8(0x64) & 1) != 1)
{
;
}
}

void mouse_write(uint8_t value) {
void mouse_write(uint8_t value)
{
mouse_wait_write();
outb8(0x64, 0xd4);
mouse_wait_write();
outb8(0x60, value);
}

uint8_t mouse_read() {
uint8_t mouse_read()
{
mouse_wait_read();
return inb8(0x60);
}

// Function to load cursor image from file
bool load_cursor_image(const char *file_path) {
bool load_cursor_image(const char *file_path)
{
vfs_op_status status = driver_read(vfs, 0x00000000, file_path, &mouse_img);
if (status == STATUS_OK) {
if (status == STATUS_OK)
{
mouse_img_size = vfs_get_file_size(vfs, 0x00000000, file_path);
return true;
} else {
}
else
{
return false;
}
}

void set_mouse_style(const char *s) {
if (strcmp(current_mouse_style, s) != 0 || !mouse_img_parsed) {
if (mouse_img != NULL) {
void set_mouse_style(const char *s)
{
if (strcmp(current_mouse_style, s) != 0 || !mouse_img_parsed)
{
if (mouse_img != NULL)
{
free(mouse_img);
mouse_img = NULL;
}

char file_path[100];
snprintf(file_path, sizeof(file_path), "/etc/theme/cursors/%s.tga", s);

if (load_cursor_image(file_path)) {
if (load_cursor_image(file_path))
{
current_mouse_style = s;
mouse_img_parsed = false;
} else {
}
else
{
printf("[\e[0;31mMouse Handler\e[0m] Cursor style '%s' not found. "
"Falling back to default.\n",
s);
Expand All @@ -83,29 +99,38 @@ void set_mouse_style(const char *s) {
}
}

void tga_draw(uint32_t x, uint32_t y, char *raw_data, uint32_t data_size) {
void tga_draw(uint32_t x, uint32_t y, char *raw_data, uint32_t data_size)
{
tga_info *tga;
if (!mouse_img_parsed) {
if (!mouse_img_parsed)
{
tga = tga_parse((uint8_t *)raw_data, data_size);
mouse_img_parsed = true;
}

if (tga != NULL) {
if (tga != NULL)
{
draw_tga(x, y, tga);
free(tga->data);
free(tga);
} else {
}
else
{
dprintf("[\e[0;31mTGA\e[0m] Failed to parse TGA data!\n");
}
}

void draw_mouse(int x, int y) {
if (mouse_img == NULL || mouse_img_size == 0) {
void draw_mouse(int x, int y)
{
if (mouse_img == NULL || mouse_img_size == 0)
{
set_mouse_style(current_mouse_style);
}

for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
int pixel_x = x + i;
int pixel_y = y + j;

Expand All @@ -118,35 +143,45 @@ void draw_mouse(int x, int y) {
tga_draw(x, y, mouse_img, mouse_img_size);
}

void remove_mouse(int x, int y) {
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
void remove_mouse(int x, int y)
{
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 32; j++)
{
int pixel_x = x + i;
int pixel_y = y + j;

uint32_t old_pixel = old_pixels[i][j];

uint32_t *framebuffer_address = (uint32_t *)(framebuffer->address + pixel_x * (framebuffer->bpp >> 3) + pixel_y * framebuffer->pitch);
*framebuffer_address = old_pixel;
if (x != 0 && y != 0)
{
uint32_t *framebuffer_address = (uint32_t *)(framebuffer->address + pixel_x * (framebuffer->bpp >> 3) + pixel_y * framebuffer->pitch);
*framebuffer_address = old_pixel;
}
}
}
}

void mouse_update(int8_t accel_x, int8_t accel_y) {
if (mouse_wrap_x + accel_x <= 0) {
void mouse_update(int8_t accel_x, int8_t accel_y)
{
if (mouse_wrap_x + accel_x <= 0)
{
mouse_wrap_x = 0;
return;
}
if (mouse_wrap_y - accel_y <= 0) {
if (mouse_wrap_y - accel_y <= 0)
{
mouse_wrap_y = 0;
return;
}
if (mouse_wrap_x + accel_x > (int32_t)framebuffer->width) {
if (mouse_wrap_x + accel_x > (int32_t)framebuffer->width)
{
mouse_wrap_x = framebuffer->width;
return;
}
if (mouse_wrap_y - accel_y > (int32_t)framebuffer->height) {
if (mouse_wrap_y - accel_y > (int32_t)framebuffer->height)
{
mouse_wrap_y = framebuffer->height;
return;
}
Expand All @@ -157,8 +192,10 @@ void mouse_update(int8_t accel_x, int8_t accel_y) {
mouse_x = (uint32_t)mouse_wrap_x;
mouse_y = (uint32_t)mouse_wrap_y;

if (should_draw_cursor) {
if (old_mouse_x != mouse_x || old_mouse_y != mouse_y) {
if (should_draw_cursor)
{
if (old_mouse_x != mouse_x || old_mouse_y != mouse_y)
{
remove_mouse(old_mouse_x, old_mouse_y);
draw_mouse(mouse_x, mouse_y);
old_mouse_x = mouse_x;
Expand All @@ -167,22 +204,27 @@ void mouse_update(int8_t accel_x, int8_t accel_y) {
}
}

void mouse_handler(int_frame_t *frame) {
void mouse_handler(int_frame_t *frame)
{
(void)frame;
uint8_t byte = inb8(0x64);
if ((!(byte & 1)) == 1) {
if ((!(byte & 1)) == 1)
{
mouse_state = 0;
return;
}
if ((!(byte & 2)) == 0) {
if ((!(byte & 2)) == 0)
{
mouse_state = 0;
return;
}
if (!(byte & 0x20)) {
if (!(byte & 0x20))
{
mouse_state = 0;
return;
}
switch (mouse_state) {
switch (mouse_state)
{
case 0:
mouse_wait_read();
mouse_bytes[0] = mouse_read();
Expand Down Expand Up @@ -211,7 +253,8 @@ void mouse_handler(int_frame_t *frame) {
}
}

void mouse_init() {
void mouse_init()
{
uint8_t data;
mouse_wait_write();
outb8(0x64, 0xa8);
Expand Down

0 comments on commit bb3e795

Please sign in to comment.