1#include "keyboard.h"
 2#include "io.h"
 3#include "idt.h"
 4
 5// Forward declaration of the ASM stub
 6extern void keyboard_handler_stub();
 7extern void shell_handle_char(char c);
 8
 9unsigned char kbd_us[128] = {
10    0,  27, '1', '2', '3', '4', '5', '6', '7', '8',	/* 9 */
11  '9', '0', '-', '=', '\b',	/* Backspace */
12  '\t',			/* Tab */
13  'q', 'w', 'e', 'r',	/* 19 */
14  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',	/* Enter key */
15    0,			/* 29   - Control */
16  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',	/* 39 */
17 '\'', '`',   0,		/* Left shift */
18 '\\', 'z', 'x', 'c', 'v', 'b', 'n',			/* 49 */
19  'm', ',', '.', '/',   0,				/* Right shift */
20  '*',
21    0,	/* Alt */
22  ' ',	/* Space bar */
23    0,	/* Caps lock */
24    0,	/* 59 - F1 key ... > */
25    0,   0,   0,   0,   0,   0,   0,   0,
26    0,	/* < ... F10 */
27    0,	/* 69 - Num lock*/
28    0,	/* Scroll Lock */
29    0,	/* Home key */
30    0,	/* Up Arrow */
31    0,	/* Page Up */
32  '-',
33    0,	/* Left Arrow */
34    0,
35    0,	/* Right Arrow */
36  '+',
37    0,	/* 79 - End key*/
38    0,	/* Down Arrow */
39    0,	/* Page Down */
40    0,	/* Insert Key */
41    0,	/* Delete Key */
42    0,   0,   0,
43    0,	/* F11 Key */
44    0,	/* F12 Key */
45    0,	/* All other keys are undefined */
46};
47
48void init_keyboard() {
49	set_idt_gate(0x21, (uint32_t)keyboard_handler_stub);
50}
51
52void keyboard_handler() {
53	unsigned char scancode = inb(0x60);
54
55	// Check if the key was pressed (bit 7 is 0)
56	if (!(scancode & 0x80)) {
57		char c = kbd_us[scancode];
58		if (c != 0) {
59			shell_handle_char(c);
60		}
61	}
62
63	// Acknowledge interrupt (End Of Interrupt)
64	outb(0x20, 0x20);
65}