Files
my-kern/kernel/drivers/keyboard.c

142 lines
3.9 KiB
C

//
// Created by rick on 23-03-20.
//
#include <stdlib.h>
#include <myke/drivers/keyboard.h>
#include <myke/drivers/ports.h>
#include <myke/cpu/isr.h>
#include <myke/libc/ringqueue.h>
#include <myke/libk/libk.h>
#include <myke/libk/syscall.h>
const char scancode_map_lowercase[] = {
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0
};
const char scancode_map_uppercase[] = {
0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
struct {
uint8_t shift: 1;
uint8_t ctrl: 1;
uint8_t alt: 1;
uint8_t extended: 1;
} keyboard_state;
void *keyboard_event_buffer = NULL;
char getc() {
while (true) {
KeyEvent *event = get_next_event();
char retval = 0;
if (event == NULL) {
goto _getc_end_nofree;
}
if (event->is_release) {
goto _getc_end;
}
if (event->scancode == 0x1C) {
retval = '\n';
goto _getc_end;
}
if (event->ascii_code != 0) {
retval = event->ascii_code;
goto _getc_end;
}
_getc_end:
free_event(event);
_getc_end_nofree:
if (retval != 0) {
return retval;
}
}
}
KeyEvent *get_next_event() {
KeyEvent *target = malloc(sizeof(KeyEvent));
if (!ring_buffer_get(keyboard_event_buffer, target)) {
syscall_yield_irq(1 << 1);
// k_wait_for_interrupt();
free(target);
return NULL;
}
return target;
};
void free_event(KeyEvent *event) {
free(event);
}
//void print_scancode(unsigned char scancode);
void handle_modifier_keys(unsigned char scancode, bool is_release) {
switch (scancode) {
case 42:
case 54:
keyboard_state.shift = !is_release;
break;
case 29:
keyboard_state.ctrl = !is_release;
break;
case 56:
keyboard_state.alt = !is_release;
break;
default:
// not an modifier key
break;
}
}
void publish_key_event(unsigned char scan_code) {
bool is_release = false;
if (scan_code > 0x80) {
is_release = true;
scan_code -= 0x80;
}
handle_modifier_keys(scan_code, is_release);
char ascii_char = scancode_map_lowercase[scan_code];
if (keyboard_state.shift) {
if (scancode_map_uppercase[scan_code] != 0) {
ascii_char = scancode_map_uppercase[scan_code];
}
}
KeyEvent event = {
.scancode = scan_code,
.ascii_code = ascii_char,
.is_release = !is_release,
.shift = keyboard_state.shift,
.alt = keyboard_state.alt,
.ctrl = keyboard_state.ctrl,
};
ring_buffer_put(keyboard_event_buffer, &event);
}
static void keyboard_callback(isr_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(PORT_PS2_DATA);
publish_key_event(scancode);
}
void init_keyboard() {
register_interrupt_handler(IRQ1, keyboard_callback);
keyboard_state.shift = 0;
keyboard_state.ctrl = 0;
keyboard_state.alt = 0;
keyboard_state.extended = 0;
keyboard_event_buffer = create_buffer(256, sizeof(KeyEvent));
}