cfenollosa_os-tutorial/23-fixes/drivers/keyboard.c

77 lines
2.9 KiB
C
Raw Normal View History

2015-08-17 16:41:38 +00:00
#include "keyboard.h"
#include "../cpu/ports.h"
#include "../cpu/isr.h"
#include "screen.h"
#include "../libc/string.h"
#include "../libc/function.h"
#include "../kernel/kernel.h"
2015-08-18 08:31:28 +00:00
#include <stdint.h>
2015-08-17 16:41:38 +00:00
#define BACKSPACE 0x0E
2020-11-11 17:09:21 +00:00
#define CapsLock 0x3A
2015-08-17 16:41:38 +00:00
#define ENTER 0x1C
static char key_buffer[256];
2020-11-11 17:09:21 +00:00
static int CapsLockStatus = 0;
2015-08-17 16:41:38 +00:00
2020-11-11 17:09:21 +00:00
#define SC_MAX 58
/* UpperCase Chars */
2015-08-17 16:41:38 +00:00
const char *sc_name[] = { "ERROR", "Esc", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "0", "-", "=", "Backspace", "Tab", "Q", "W", "E",
"R", "T", "Y", "U", "I", "O", "P", "[", "]", "Enter", "Lctrl",
"A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "`",
"LShift", "\\", "Z", "X", "C", "V", "B", "N", "M", ",", ".",
2020-11-11 17:09:21 +00:00
"/", "RShift", "Keypad *", "LAlt", "Spacebar", "CapsLock"};
2015-08-17 16:41:38 +00:00
const char sc_ascii[] = { '?', '?', '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', '?', '?', 'Q', 'W', 'E', 'R', 'T', 'Y',
'U', 'I', 'O', 'P', '[', ']', '?', '?', 'A', 'S', 'D', 'F', 'G',
'H', 'J', 'K', 'L', ';', '\'', '`', '?', '\\', 'Z', 'X', 'C', 'V',
2020-11-11 17:09:21 +00:00
'B', 'N', 'M', ',', '.', '/', '?', '?', '?', ' ', '?'};
/* LowerCase Chars */
const char *Lsc_name[] = { "ERROR", "Esc", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "0", "-", "=", "Backspace", "Tab", "q", "w", "e",
"r", "t", "y", "u", "i", "o", "p", "[", "]", "Enter", "Lctrl",
"a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "`",
"LShift", "\\", "z", "x", "c", "v", "b", "n", "m", ",", ".",
"/", "RShift", "Keypad *", "LAlt", "Spacebar", "CapsLock"};
const char Lsc_ascii[] = { '?', '?', '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', '?', '?', 'q', 'w', 'e', 'r', 't', 'y',
'u', 'i', 'o', 'p', '[', ']', '?', '?', 'a', 's', 'd', 'f', 'g',
'h', 'j', 'k', 'l', ';', '\'', '`', '?', '\\', 'z', 'x', 'c', 'v',
'b', 'n', 'm', ',', '.', '/', '?', '?', '?', ' ', '?'};
2015-08-17 16:41:38 +00:00
2015-08-28 08:52:05 +00:00
static void keyboard_callback(registers_t *regs) {
2015-08-17 16:41:38 +00:00
/* The PIC leaves us the scancode in port 0x60 */
2015-08-18 08:31:28 +00:00
uint8_t scancode = port_byte_in(0x60);
2015-08-17 16:41:38 +00:00
if (scancode > SC_MAX) return;
if (scancode == BACKSPACE) {
backspace(key_buffer);
kprint_backspace();
} else if (scancode == ENTER) {
kprint("\n");
user_input(key_buffer); /* kernel-controlled function */
key_buffer[0] = '\0';
2020-11-11 17:09:21 +00:00
} else if (scancode == CapsLock) {
/* Toggle caps lock */
if (CapsLockStatus)
CapsLockStatus = 0;
else
CapsLockStatus = 1;
2015-08-17 16:41:38 +00:00
} else {
char letter = sc_ascii[(int)scancode];
2020-11-11 17:09:21 +00:00
if (CapsLockStatus == 0)
letter = Lsc_ascii[(int)scancode];
2015-08-17 16:41:38 +00:00
/* Remember that kprint only accepts char[] */
char str[2] = {letter, '\0'};
append(key_buffer, letter);
kprint(str);
}
UNUSED(regs);
}
void init_keyboard() {
register_interrupt_handler(IRQ1, keyboard_callback);
}