Files
KatauOS/src/drivers/keyboard.c
T
2025-11-18 17:54:24 +03:00

162 lines
4.1 KiB
C

#include "../include/keyboard.h"
#include <stdbool.h>
#include "../include/task.h"
#include "../include/string.h"
char stdin_buffer[STDIN_BUFFER_SIZE];
int stdin_pos = 0;
bool stdin_has_line = false;
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 keyboard_wait();
void handle_keyboard() {
uint8_t scancode = inb(0x60);
if (scancode & 0x80) { // Released
scancode -= 0x80;
if (scancode == 0x2A || scancode == 0x36) shift_pressed = false;
} else { // Pressed
if (scancode == 0x2A || scancode == 0x36) {
shift_pressed = true;
return;
}
if (scancode == 0x3A) { // Caps
return;
}
char c;
if (shift_pressed || caps_pressed) c = kbdus2[scancode];
else c = kbdus[scancode];
if (c == '\b') { //Backspace
if (stdin_pos > 0) {
stdin_pos--;
printf("\b \b");
}
} else if (c == '\n') { // Enter
if (stdin_pos < STDIN_BUFFER_SIZE - 1) {
stdin_buffer[stdin_pos++] = '\n';
printf("\n");
}
stdin_has_line = true;
// Wake-up: wake up all Waiting tasks on STDIN
scheduler_lock();
Process *p = queue;
while (p) {
if (p->state == Waiting && p->waiting_reason == STDIN) {
p->state = Ready;
p->waiting_reason = NONE;
}
p = p->next;
}
scheduler_unlock();
} else if (c != 0 && stdin_pos < STDIN_BUFFER_SIZE - 1) {//if printable
stdin_buffer[stdin_pos++] = c;
printf("%c", c);//echo
}
}
}
void init_keyboard()
{
memset(stdin_buffer, 0, STDIN_BUFFER_SIZE);
stdin_pos = 0;
stdin_has_line = false;
}
void keyboard_wait()
{
int status;
while(1)
{
status = inb(0x60);
if(status == 0xfa || status == 0xfe)
return;
}
}