70 lines
1.7 KiB
C
70 lines
1.7 KiB
C
//
|
|
// Created by rick on 23-03-20.
|
|
//
|
|
#include "keyboard.h"
|
|
|
|
#include <kprint.h>
|
|
#include <drivers/ports.h>
|
|
#include <cpu/isr.h>
|
|
#include <libc/libc.h>
|
|
|
|
char scancodes_ascii[] = {
|
|
0, 0,
|
|
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0,
|
|
0, 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 0,
|
|
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',
|
|
0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0,
|
|
'*',
|
|
0, ' ',
|
|
0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // F1-F10
|
|
0, 0,
|
|
'7', '8', '9', '-',
|
|
'4', '5', '6', '+',
|
|
'1', '2', '3',
|
|
'0', 0,
|
|
0, // sysrq
|
|
0, 0, // weird
|
|
0, 0, // F11 F12
|
|
// weid
|
|
};
|
|
|
|
char *MSG_KEY = "Clicked on key 'x'\n";
|
|
|
|
void print_scancode(unsigned char scancode);
|
|
|
|
static void keyboard_callback(registers_t regs) {
|
|
unsigned char status = port_byte_in(PORT_PS2_STATUS);
|
|
// check if data available
|
|
if ((status & 0b00000001) == 0) {
|
|
return;
|
|
}
|
|
unsigned char scancode = port_byte_in(0x60);
|
|
print_scancode(scancode);
|
|
}
|
|
|
|
void init_keyboard() {
|
|
register_interrupt_handler(IRQ1, keyboard_callback);
|
|
}
|
|
|
|
void print_scancode(unsigned char scancode) {
|
|
char msg[256];
|
|
char release = 0;
|
|
if (scancode > 0x80) {
|
|
// release
|
|
release = 1;
|
|
scancode -= 0x80;
|
|
}
|
|
char code = scancodes_ascii[scancode];
|
|
if (code == 0) {
|
|
// special
|
|
} else {
|
|
if (release && code > 0x60 && code < 0x7B) {
|
|
code -= 0x20; // to lowercase
|
|
}
|
|
|
|
memcpy(msg, MSG_KEY, strlen(MSG_KEY));
|
|
msg[strlen(msg) - 3] = code;
|
|
kprint(msg);
|
|
}
|
|
} |