keyboard.c 3.9 KB

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