keyboard.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "keyboard.h"
  2. #include "../cpu/ports.h"
  3. #include "../cpu/isr.h"
  4. #include "screen.h"
  5. #include "../libc/string.h"
  6. #include "../libc/function.h"
  7. #include "../kernel/kernel.h"
  8. #include <stdint.h>
  9. #define BACKSPACE 0x0E
  10. #define ENTER 0x1C
  11. static char key_buffer[256];
  12. #define SC_MAX 57
  13. const char *sc_name[] = { "ERROR", "Esc", "1", "2", "3", "4", "5", "6",
  14. "7", "8", "9", "0", "-", "=", "Backspace", "Tab", "Q", "W", "E",
  15. "R", "T", "Y", "U", "I", "O", "P", "[", "]", "Enter", "Lctrl",
  16. "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "`",
  17. "LShift", "\\", "Z", "X", "C", "V", "B", "N", "M", ",", ".",
  18. "/", "RShift", "Keypad *", "LAlt", "Spacebar"};
  19. const char sc_ascii[] = { '?', '?', '1', '2', '3', '4', '5', '6',
  20. '7', '8', '9', '0', '-', '=', '?', '?', 'Q', 'W', 'E', 'R', 'T', 'Y',
  21. 'U', 'I', 'O', 'P', '[', ']', '?', '?', 'A', 'S', 'D', 'F', 'G',
  22. 'H', 'J', 'K', 'L', ';', '\'', '`', '?', '\\', 'Z', 'X', 'C', 'V',
  23. 'B', 'N', 'M', ',', '.', '/', '?', '?', '?', ' '};
  24. static void keyboard_callback(registers_t *regs) {
  25. /* The PIC leaves us the scancode in port 0x60 */
  26. uint8_t scancode = port_byte_in(0x60);
  27. if (scancode > SC_MAX) return;
  28. if (scancode == BACKSPACE) {
  29. backspace(key_buffer);
  30. kprint_backspace();
  31. } else if (scancode == ENTER) {
  32. kprint("\n");
  33. user_input(key_buffer); /* kernel-controlled function */
  34. key_buffer[0] = '\0';
  35. } else {
  36. char letter = sc_ascii[(int)scancode];
  37. /* Remember that kprint only accepts char[] */
  38. char str[2] = {letter, '\0'};
  39. append(key_buffer, letter);
  40. kprint(str);
  41. }
  42. UNUSED(regs);
  43. }
  44. void init_keyboard() {
  45. register_interrupt_handler(IRQ1, keyboard_callback);
  46. }