diff options
author | Marvin Borner | 2020-08-25 22:11:04 +0200 |
---|---|---|
committer | Marvin Borner | 2020-08-25 22:11:04 +0200 |
commit | ec5c4a7398fcfcfb5a809292cbe029b1bb69a320 (patch) | |
tree | dc1cba8e1fa543c2a0f98955a35d0d7e40762d02 /kernel | |
parent | d2017fca3efefdb4d514f2b356855a0fda1fccfd (diff) |
Added mouse driver and better event system
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/Makefile | 1 | ||||
-rw-r--r-- | kernel/drivers/keyboard.c | 11 | ||||
-rw-r--r-- | kernel/drivers/mouse.c | 157 | ||||
-rw-r--r-- | kernel/inc/mouse.h | 8 | ||||
-rw-r--r-- | kernel/main.c | 2 |
5 files changed, 176 insertions, 3 deletions
diff --git a/kernel/Makefile b/kernel/Makefile index e119933..07f985a 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -4,6 +4,7 @@ COBJS = main.o \ drivers/interrupts.o \ drivers/interrupts_asm.o \ drivers/keyboard.o \ + drivers/mouse.o \ drivers/ide.o \ drivers/timer.o \ features/fs.o \ diff --git a/kernel/drivers/keyboard.c b/kernel/drivers/keyboard.c index b60e437..14445d4 100644 --- a/kernel/drivers/keyboard.c +++ b/kernel/drivers/keyboard.c @@ -4,9 +4,12 @@ #include <def.h> #include <event.h> #include <interrupts.h> +#include <mem.h> #include <print.h> +#include <sys.h> char keymap[128]; +struct event_keyboard *event; int state = 0; int merged = 0; @@ -26,9 +29,10 @@ void keyboard_handler() // TODO: "Merge" scancode to linux keycode? /* printf("%x %x = %x\n", scancode, state ? 0xe0 : 0, merged); */ - if ((scancode & 0x80) == 0) { // PRESS - event_trigger(EVENT_KEYBOARD, (u32 *)scancode); - } + event->magic = KEYBOARD_MAGIC; + event->press = (scancode & 0x80) == 0; + event->scancode = event->press ? scancode : scancode & ~0x80; + event_trigger(EVENT_KEYBOARD, event); state = 0; merged = 0; @@ -50,6 +54,7 @@ void keyboard_rate() void keyboard_install() { //keyboard_rate(); TODO: Fix keyboard rate? + event = malloc(sizeof(*event)); irq_install_handler(1, keyboard_handler); } diff --git a/kernel/drivers/mouse.c b/kernel/drivers/mouse.c new file mode 100644 index 0000000..64204c3 --- /dev/null +++ b/kernel/drivers/mouse.c @@ -0,0 +1,157 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#include <cpu.h> +#include <event.h> +#include <interrupts.h> +#include <mem.h> +#include <print.h> +#include <sys.h> + +char mouse_cycle = 0; +char mouse_byte[3]; + +struct event_mouse *event; + +void mouse_handler() +{ + switch (mouse_cycle) { + case 0: + mouse_byte[0] = inb(0x60); + if (((mouse_byte[0] >> 3) & 1) == 1) + mouse_cycle++; + else + mouse_cycle = 0; + break; + case 1: + mouse_byte[1] = inb(0x60); + mouse_cycle++; + break; + case 2: + mouse_byte[2] = inb(0x60); + + event->magic = MOUSE_MAGIC; + event->diff_x = mouse_byte[1]; + event->diff_y = mouse_byte[2]; + event->but1 = mouse_byte[0] & 1; + event->but2 = (mouse_byte[0] >> 1) & 1; + event->but3 = (mouse_byte[0] >> 2) & 1; + event_trigger(EVENT_MOUSE, event); + + mouse_cycle = 0; + break; + default: + break; + } +} + +void mouse_wait(u8 a_type) +{ + u32 time_out = 100000; + if (a_type == 0) { + while (time_out--) + if ((inb(0x64) & 1) == 1) + return; + return; + } else { + while (time_out--) + if ((inb(0x64) & 2) == 0) + return; + return; + } +} + +void mouse_write(u8 a_write) +{ + mouse_wait(1); + outb(0x64, 0xD4); + mouse_wait(1); + outb(0x60, a_write); +} + +char mouse_read() +{ + mouse_wait(0); + return inb(0x60); +} + +void mouse_install() +{ + event = malloc(sizeof(*event)); + + u8 status; + + // Enable auxiliary mouse device + mouse_wait(1); + outb(0x64, 0xA8); + + // Enable interrupts + mouse_wait(1); + outb(0x64, 0x20); + mouse_wait(0); + status = (u8)(inb(0x60) | 3); + mouse_wait(1); + outb(0x64, 0x60); + mouse_wait(1); + outb(0x60, status); + + // Enable mousewheel + mouse_write(0xF2); + mouse_read(); + mouse_read(); + mouse_write(0xF3); + mouse_read(); + mouse_write(200); + mouse_read(); + mouse_write(0xF3); + mouse_read(); + mouse_write(100); + mouse_read(); + mouse_write(0xF3); + mouse_read(); + mouse_write(80); + mouse_read(); + mouse_write(0xF2); + mouse_read(); + status = (u8)mouse_read(); + if (status == 3) + printf("Scrollwheel support!\n"); + + // Activate 4th and 5th mouse buttons + mouse_write(0xF2); + mouse_read(); + mouse_read(); + mouse_write(0xF3); + mouse_read(); + mouse_write(200); + mouse_read(); + mouse_write(0xF3); + mouse_read(); + mouse_write(200); + mouse_read(); + mouse_write(0xF3); + mouse_read(); + mouse_write(80); + mouse_read(); + mouse_write(0xF2); + mouse_read(); + status = (u8)mouse_read(); + if (status == 4) + printf("4th and 5th mouse button support!\n"); + + /* TODO: Fix mouse laggyness + mouse_write(0xE8); + mouse_read(); + mouse_write(0x03); + mouse_read(); + mouse_write(0xF3); + mouse_read(); + mouse_write(200); + mouse_read(); */ + + // Enable mouse + mouse_write(0xF4); + mouse_read(); + + // Setup the mouse handler + irq_install_handler(12, mouse_handler); +} diff --git a/kernel/inc/mouse.h b/kernel/inc/mouse.h new file mode 100644 index 0000000..ccea383 --- /dev/null +++ b/kernel/inc/mouse.h @@ -0,0 +1,8 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef MOUSE_H +#define MOUSE_H + +void mouse_install(); + +#endif diff --git a/kernel/main.c b/kernel/main.c index 42f0d54..e97dad1 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -6,6 +6,7 @@ #include <keyboard.h> #include <load.h> #include <mem.h> +#include <mouse.h> #include <serial.h> #include <syscall.h> #include <timer.h> @@ -25,6 +26,7 @@ void kernel_main(struct vid_info *vid_info) interrupts_install(); timer_install(); keyboard_install(); + mouse_install(); // Enable drivers sti(); |