From bb87905970e8dd3e902639d34f151efdbf475484 Mon Sep 17 00:00:00 2001 From: Ruslan Isaev Date: Fri, 21 Mar 2025 23:10:03 +0300 Subject: [PATCH] make keyboard actually type things --- Makefile | 5 +- include/keyboard.h | 9 ++++ src/isr.c | 7 +-- src/keyboard.c | 121 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 include/keyboard.h create mode 100644 src/keyboard.c diff --git a/Makefile b/Makefile index 66967d9..8cfd563 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Define the object files -OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o bin/disk.o bin/pic.o bin/pit.o +OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o bin/disk.o bin/pic.o bin/pit.o bin/keyboard.o # Define the compiler and assembler CC = gcc -D__is_katauos @@ -64,6 +64,9 @@ bin/pic.o: src/pic.c bin/pit.o: src/pit.c $(CC) $(CFLAGS) -c $< -o $@ +bin/keyboard.o: src/keyboard.c + $(CC) $(CFLAGS) -c $< -o $@ + clean: rm -rf bin/ rm -f $(OUTPUT) diff --git a/include/keyboard.h b/include/keyboard.h new file mode 100644 index 0000000..0914541 --- /dev/null +++ b/include/keyboard.h @@ -0,0 +1,9 @@ +#ifndef KEYBOARD_H +#define KEYBOARD_H + +#include "../include/utils.h" +#include "../include/stdio.h" + +void handle_keyboard(); + +#endif diff --git a/src/isr.c b/src/isr.c index f6d8e2e..98cf67c 100644 --- a/src/isr.c +++ b/src/isr.c @@ -4,6 +4,7 @@ #include "../include/screen.h" #include "../include/pic.h" #include "../include/pit.h" +#include "../include/keyboard.h" typedef size_t uword_t; struct interrupt_frame @@ -75,12 +76,6 @@ __attribute__((interrupt)) void isr_timer(struct interrupt_frame *frame) pic_end_int(0x0); } -void handle_keyboard() -{ - uint8_t scancode = inb(0x60); - printf("%X ", scancode); -} - __attribute__((interrupt)) void isr_custom(struct interrupt_frame *frame) { //terminal_writestring("YOOOOO!!!!!! I LOVE KATYA!!!!"); diff --git a/src/keyboard.c b/src/keyboard.c new file mode 100644 index 0000000..6ccc014 --- /dev/null +++ b/src/keyboard.c @@ -0,0 +1,121 @@ +#include "../include/keyboard.h" +#include + +unsigned char kbdus[128] = +{ + 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */ + '9', '0', '-', '=', '\b', /* Backspace */ + '\t', /* Tab */ + 'q', 'w', 'e', 'r', /* 19 */ + 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */ + 0, /* 29 - Control */ + 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */ + '\'', '`', 0, /* Left shift */ + '\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */ + 'm', ',', '.', '/', 0, /* Right shift */ + '*', + 0, /* Alt */ + ' ', /* Space bar */ + 0, /* Caps lock */ + 0, /* 59 - F1 key ... > */ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, /* < ... F10 */ + 0, /* 69 - Num lock*/ + 0, /* Scroll Lock */ + 0, /* Home key */ + 0, /* Up Arrow */ + 0, /* Page Up */ + '-', + 0, /* Left Arrow */ + 0, + 0, /* Right Arrow */ + '+', + 0, /* 79 - End key*/ + 0, /* Down Arrow */ + 0, /* Page Down */ + 0, /* Insert Key */ + 0, /* Delete Key */ + 0, 0, 0, + 0, /* F11 Key */ + 0, /* F12 Key */ + 0, /* All other keys are undefined */ +}; + +unsigned char kbdus2[128] = +{ + 0, 27, '!', '@', '#', '$', '%', '^', '&', '*', /* 9 */ + '(', ')', '_', '+', '\b', /* Backspace */ + '\t', /* Tab */ + 'Q', 'W', 'E', 'R', /* 19 */ + 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', /* Enter key */ + 0, /* 29 - Control */ + 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* 39 */ + '\'', '`', 0, /* Left shift */ + '\\', 'Z', 'X', 'C', 'V', 'B', 'N', /* 49 */ + 'M', '<', '>', '?', 0, /* Right shift */ + '*', + 0, /* Alt */ + ' ', /* Space bar */ + 0, /* Caps lock */ + 0, /* 59 - F1 key ... > */ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, /* < ... F10 */ + 0, /* 69 - Num lock*/ + 0, /* Scroll Lock */ + 0, /* Home key */ + 0, /* Up Arrow */ + 0, /* Page Up */ + '-', + 0, /* Left Arrow */ + 0, + 0, /* Right Arrow */ + '+', + 0, /* 79 - End key*/ + 0, /* Down Arrow */ + 0, /* Page Down */ + 0, /* Insert Key */ + 0, /* Delete Key */ + 0, 0, 0, + 0, /* F11 Key */ + 0, /* F12 Key */ + 0, /* All other keys are undefined */ +}; + + +bool shift_pressed; +bool caps_pressed; + +void handle_keyboard() +{//3a + uint8_t scancode = inb(0x60); + + if(scancode & 0x80)//key released + { + scancode = scancode-0x80; + if(scancode == 0x2a || scancode == 0x36) + shift_pressed = false; + //printf("(%X)%c released ", scancode, kbdus[scancode-0x80]); + } + else//key pressed + { + if(scancode == 0x2a || scancode == 0x36) + { + shift_pressed = true; + return; + } + + if(scancode == 0x3a) + { + caps_pressed = !caps_pressed; + outb(0x64, 0xed);//set LED command + int status = inb(0x64); + outb(0x60, 0b100);//bits are scroll lock, number lock, caps lock + return; + } + + if(shift_pressed || caps_pressed) + printf("%c", kbdus2[scancode]); + else + printf("%c", kbdus[scancode]); //printf("(0x%X)%c", scancode, kbdus[scancode]); + } +}