keyboard.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/schk.h>
  26. #include <kernel/mm.h>
  27. #include <kernel/keyboard.h>
  28. #include <kernel/int.h>
  29. #include <arch/x86/int.h>
  30. #include <arch/io.h>
  31. #include <arch/sys.h>
  32. struct fifo kbd_fifo;
  33. __uint8_t ctrl = 0, shift = 0, alt, capslock = 0;
  34. /* the 0xe0 */
  35. __uint8_t ext = 0;
  36. errno_t kbd_init(void) {
  37. schk_init();
  38. __fifo_buf_t *key_buf = kmalloc(&pool, MAX_KEYS * sizeof(__fifo_buf_t));
  39. memset(key_buf, 0x00, MAX_KEYS * sizeof(__uint64_t));
  40. fifo_init(&kbd_fifo, key_buf, MAX_KEYS);
  41. schk_leave();
  42. return 0;
  43. }
  44. void do_keyboard(void) {
  45. __uint8_t code = inb(KBD_DATA);
  46. /* insert it to the fifo */
  47. fifo_put(&kbd_fifo, code);
  48. return;
  49. }
  50. /* only displayable numbers/alphabet */
  51. static char keycode_alphabet[] = {
  52. /* nul, esc */
  53. 0, 0, '1', '2', '3', '4', '5', '6',
  54. /* backspace, tab */
  55. '7', '8', '9', '0', '-', '=', 0, 0,
  56. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  57. /* enter, lctrl */
  58. 'o', 'p', '[', ']', 0, 0, 'a', 's',
  59. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  60. /* lshift */
  61. '\'', '`', 0, '\\', 'z', 'x', 'c', 'v',
  62. /* rshift */
  63. 'b', 'n', 'm', ',', '.', '/', 0, '*',
  64. /* capslock, f1 - f5 */
  65. 0, ' ', 0, 0, 0, 0, 0, 0,
  66. /* f6 - f10, numlock, scrlock */
  67. 0, 0, 0, 0, 0, 0, 0, '7',
  68. '8', '9', '-', '4', '5', '6', '+', '1',
  69. '2', '3', '0', '.'
  70. };
  71. static char keycode_alphabet_shift[] = {
  72. 0, 0, '!', '@', '#', '$', '%', '^',
  73. '&', '*', '(', ')', '_', '+', 0, 0,
  74. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  75. 'O', 'P', '{', '}', 0, 0, 'A', 'S',
  76. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  77. '"', '~', 0, '|', 'Z', 'X', 'C', 'V',
  78. 'B', 'N', 'M', '<', '>', '?', 0, '*',
  79. 0, ' ', 0, 0, 0, 0, 0, 0,
  80. 0, 0, 0, 0, 0, 0, 0, '7',
  81. '8', '9', '-', '4', '5', '6', '+', '1',
  82. '2', '3', '0', '.'
  83. };
  84. void put_keycode(void) {
  85. __fifo_buf_t i;
  86. i = fifo_get(&kbd_fifo);
  87. if (!i)
  88. while (!kbd_fifo.need_read)
  89. hlt();
  90. if (i == EXT) {
  91. ext = 1;
  92. return;
  93. }
  94. /* the limit of keycode_alphabet */
  95. if (!ext && i < 0x54) {
  96. char a;
  97. if (!shift && !capslock) {
  98. if ((a = keycode_alphabet[i]))
  99. putc(a, white);
  100. else
  101. goto undisplayable;
  102. }
  103. else {
  104. if ((a = keycode_alphabet_shift[i]))
  105. putc(a, white);
  106. else
  107. goto undisplayable;
  108. }
  109. /* its displayable, so return */
  110. return;
  111. }
  112. undisplayable:
  113. if (i == EXT) {
  114. ext = 1;
  115. return;
  116. }
  117. if (i == CTRL) {
  118. if (ext)
  119. /* got next character */
  120. ext = 0;
  121. ctrl = 1;
  122. return;
  123. }
  124. if (i == BREAK + CTRL) {
  125. if (ext)
  126. ext = 0;
  127. ctrl = 0;
  128. return;
  129. }
  130. if (i == LSHIFT || i == RSHIFT) {
  131. shift = 1;
  132. return;
  133. }
  134. if (i == BREAK + LSHIFT || i == BREAK + RSHIFT) {
  135. shift = 0;
  136. return;
  137. }
  138. if (i == ALT) {
  139. if (ext)
  140. ext = 0;
  141. alt = 1;
  142. return;
  143. }
  144. if (i == BREAK + ALT) {
  145. if (ext)
  146. ext = 0;
  147. alt = 0;
  148. return;
  149. }
  150. if (i == CAPSLK) {
  151. capslock = !capslock;
  152. return;
  153. }
  154. if (i == BREAK + CAPSLK)
  155. return;
  156. if (i == BACKSP) {
  157. putc('\b', white);
  158. return;
  159. }
  160. if (i == ENTER) {
  161. putc('\n', white);
  162. return;
  163. }
  164. if (i == TAB) {
  165. putc('\t', white);
  166. return;
  167. }
  168. ext = 0;
  169. return;
  170. }