keyboard.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/console.h>
  23. #include <lib/string.h>
  24. #include <lib/fifo.h>
  25. #include <kernel/mm.h>
  26. #include <kernel/keyboard.h>
  27. #include <kernel/int.h>
  28. #include <arch/x86/int.h>
  29. #include <arch/io.h>
  30. #include <arch/sys.h>
  31. struct fifo kbd_fifo;
  32. __uint8_t ctrl = 0, shift = 0, alt, capslock = 0;
  33. /* the 0xe0 */
  34. __uint8_t ext = 0;
  35. errno_t kbd_init(void) {
  36. __fifo_buf_t *key_buf = kmalloc(&pool, MAX_KEYS * sizeof(__fifo_buf_t));
  37. memset(key_buf, 0x00, MAX_KEYS * sizeof(__uint64_t));
  38. fifo_init(&kbd_fifo, key_buf, MAX_KEYS);
  39. return 0;
  40. }
  41. void do_keyboard(void) {
  42. __uint8_t code = inb(KBD_DATA);
  43. /* insert it to the fifo */
  44. fifo_put(&kbd_fifo, code);
  45. return;
  46. }
  47. /* only displayable numbers/alphabet */
  48. static char keycode_alphabet[] = {
  49. /* nul, esc */
  50. 0, 0, '1', '2', '3', '4', '5', '6',
  51. /* backspace, tab */
  52. '7', '8', '9', '0', '-', '=', 0, 0,
  53. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  54. /* enter, lctrl */
  55. 'o', 'p', '[', ']', 0, 0, 'a', 's',
  56. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  57. /* lshift */
  58. '\'', '`', 0, '\\', 'z', 'x', 'c', 'v',
  59. /* rshift */
  60. 'b', 'n', 'm', ',', '.', '/', 0, '*',
  61. /* capslock, f1 - f5 */
  62. 0, ' ', 0, 0, 0, 0, 0, 0,
  63. /* f6 - f10, numlock, scrlock */
  64. 0, 0, 0, 0, 0, 0, 0, '7',
  65. '8', '9', '-', '4', '5', '6', '+', '1',
  66. '2', '3', '0', '.'
  67. };
  68. static char keycode_alphabet_shift[] = {
  69. 0, 0, '!', '@', '#', '$', '%', '^',
  70. '&', '*', '(', ')', '_', '+', 0, 0,
  71. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  72. 'O', 'P', '{', '}', 0, 0, 'A', 'S',
  73. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  74. '"', '~', 0, '|', 'Z', 'X', 'C', 'V',
  75. 'B', 'N', 'M', '<', '>', '?', 0, '*',
  76. 0, ' ', 0, 0, 0, 0, 0, 0,
  77. 0, 0, 0, 0, 0, 0, 0, '7',
  78. '8', '9', '-', '4', '5', '6', '+', '1',
  79. '2', '3', '0', '.'
  80. };
  81. void put_keycode(void) {
  82. __fifo_buf_t i;
  83. i = fifo_get(&kbd_fifo);
  84. if (!i)
  85. while (!kbd_fifo.need_read)
  86. hlt();
  87. if (i == EXT) {
  88. ext = 1;
  89. return;
  90. }
  91. /* the limit of keycode_alphabet */
  92. if (!ext && i < 0x54) {
  93. char a;
  94. if (!shift && !capslock) {
  95. if ((a = keycode_alphabet[i]))
  96. putc(a, white);
  97. else
  98. goto undisplayable;
  99. }
  100. else {
  101. if ((a = keycode_alphabet_shift[i]))
  102. putc(a, white);
  103. else
  104. goto undisplayable;
  105. }
  106. /* its displayable, so return */
  107. return;
  108. }
  109. undisplayable:
  110. if (i == EXT) {
  111. ext = 1;
  112. return;
  113. }
  114. if (i == CTRL) {
  115. if (ext)
  116. /* got next character */
  117. ext = 0;
  118. ctrl = 1;
  119. return;
  120. }
  121. if (i == BREAK + CTRL) {
  122. if (ext)
  123. ext = 0;
  124. ctrl = 0;
  125. return;
  126. }
  127. if (i == LSHIFT || i == RSHIFT) {
  128. shift = 1;
  129. return;
  130. }
  131. if (i == BREAK + LSHIFT || i == BREAK + RSHIFT) {
  132. shift = 0;
  133. return;
  134. }
  135. if (i == ALT) {
  136. if (ext)
  137. ext = 0;
  138. alt = 1;
  139. return;
  140. }
  141. if (i == BREAK + ALT) {
  142. if (ext)
  143. ext = 0;
  144. alt = 0;
  145. return;
  146. }
  147. if (i == CAPSLK) {
  148. capslock = !capslock;
  149. return;
  150. }
  151. if (i == BREAK + CAPSLK)
  152. return;
  153. if (i == BACKSP) {
  154. putc('\b', white);
  155. return;
  156. }
  157. if (i == ENTER) {
  158. putc('\n', white);
  159. return;
  160. }
  161. if (i == TAB) {
  162. putc('\t', white);
  163. return;
  164. }
  165. ext = 0;
  166. return;
  167. }