Keyboard.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "types.h"
  2. #include "i386.h"
  3. #include "IO.h"
  4. #include "PIC.h"
  5. #include "Keyboard.h"
  6. #include "VirtualConsole.h"
  7. #include <AK/Assertions.h>
  8. //#define KEYBOARD_DEBUG
  9. #define IRQ_KEYBOARD 1
  10. #define I8042_BUFFER 0x60
  11. #define I8042_STATUS 0x64
  12. #define SET_LEDS 0xED
  13. #define DATA_AVAILABLE 0x01
  14. #define I8042_ACK 0xFA
  15. static char map[0x80] =
  16. {
  17. 0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08, 0,
  18. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0,
  19. 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\',
  20. 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
  21. 0, 0, 0, ' '
  22. };
  23. static char shift_map[0x80] =
  24. {
  25. 0, '\033', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0x08, 0,
  26. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0,
  27. 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0, '|',
  28. 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
  29. 0, 0, 0, ' '
  30. };
  31. static KeyCode unshifted_key_map[0x80] =
  32. {
  33. Key_Invalid, Key_Escape,
  34. Key_1, Key_2, Key_3, Key_4, Key_5, Key_6, Key_7, Key_8, Key_9, Key_0, Key_Minus, Key_Equal, Key_Backspace,
  35. Key_Invalid, //15
  36. Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_LeftBracket, Key_RightBracket,
  37. Key_Return, // 28
  38. Key_Control, // 29
  39. Key_A, Key_S, Key_D, Key_F, Key_G, Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Apostrophe, Key_Backtick,
  40. Key_Shift, // 42
  41. Key_Backslash,
  42. Key_Z, Key_X, Key_C, Key_V, Key_B, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash,
  43. Key_Alt, // 54
  44. Key_Invalid, Key_Invalid,
  45. Key_Space, // 57
  46. Key_Invalid, Key_Invalid,
  47. Key_Invalid, // 60
  48. Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid,
  49. Key_Invalid, // 70
  50. Key_Invalid,
  51. Key_Up,
  52. Key_Invalid,
  53. Key_Invalid,
  54. Key_Left,
  55. Key_Invalid,
  56. Key_Right, // 77
  57. Key_Invalid,
  58. Key_Invalid,
  59. Key_Down, // 80
  60. };
  61. static KeyCode shifted_key_map[0x100] =
  62. {
  63. Key_Invalid, Key_Escape,
  64. Key_ExclamationPoint, Key_AtSign, Key_Hashtag, Key_Dollar, Key_Percent, Key_Circumflex, Key_Ampersand, Key_Asterisk, Key_LeftParen, Key_RightParen, Key_Underscore, Key_Plus, Key_Backspace,
  65. Key_Invalid,
  66. Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_LeftBrace, Key_RightBrace,
  67. Key_Return,
  68. Key_Control,
  69. Key_A, Key_S, Key_D, Key_F, Key_G, Key_H, Key_J, Key_K, Key_L, Key_Colon, Key_DoubleQuote, Key_Tilde,
  70. Key_Shift,
  71. Key_Pipe,
  72. Key_Z, Key_X, Key_C, Key_V, Key_B, Key_N, Key_M, Key_LessThan, Key_GreaterThan, Key_QuestionMark,
  73. Key_Alt,
  74. Key_Invalid, Key_Invalid,
  75. Key_Space,
  76. Key_Invalid, Key_Invalid,
  77. Key_Invalid, // 60
  78. Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid, Key_Invalid,
  79. Key_Invalid, // 70
  80. Key_Invalid,
  81. Key_Up,
  82. Key_Invalid,
  83. Key_Invalid,
  84. Key_Left,
  85. Key_Invalid,
  86. Key_Right, // 77
  87. Key_Invalid,
  88. Key_Invalid,
  89. Key_Down, // 80
  90. };
  91. void Keyboard::key_state_changed(byte raw, bool pressed)
  92. {
  93. Event event;
  94. event.key = (m_modifiers & Mod_Shift) ? shifted_key_map[raw] : unshifted_key_map[raw];
  95. event.character = (m_modifiers & Mod_Shift) ? shift_map[raw] : map[raw];
  96. event.flags = m_modifiers;
  97. if (pressed)
  98. event.flags |= Is_Press;
  99. if (m_client)
  100. m_client->on_key_pressed(event);
  101. m_queue.enqueue(event);
  102. }
  103. void Keyboard::handle_irq()
  104. {
  105. while (IO::in8(I8042_STATUS) & DATA_AVAILABLE) {
  106. byte raw = IO::in8(I8042_BUFFER);
  107. byte ch = raw & 0x7f;
  108. bool pressed = !(raw & 0x80);
  109. #ifdef KEYBOARD_DEBUG
  110. dbgprintf("Keyboard::handle_irq: %b %s\n", ch, pressed ? "down" : "up");
  111. #endif
  112. switch (ch) {
  113. case 0x38: update_modifier(Mod_Alt, pressed); break;
  114. case 0x1d: update_modifier(Mod_Ctrl, pressed); break;
  115. case 0x2a: update_modifier(Mod_Shift, pressed); break;
  116. case I8042_ACK: break;
  117. default:
  118. if (m_modifiers & Mod_Alt) {
  119. switch (map[ch]) {
  120. case '1':
  121. case '2':
  122. case '3':
  123. case '4':
  124. VirtualConsole::switch_to(map[ch] - '0' - 1);
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. key_state_changed(ch, pressed);
  131. }
  132. }
  133. }
  134. static Keyboard* s_the;
  135. Keyboard& Keyboard::the()
  136. {
  137. ASSERT(s_the);
  138. return *s_the;
  139. }
  140. Keyboard::Keyboard()
  141. : IRQHandler(IRQ_KEYBOARD)
  142. , CharacterDevice(85, 1)
  143. {
  144. s_the = this;
  145. // Empty the buffer of any pending data.
  146. // I don't care what you've been pressing until now!
  147. while (IO::in8(I8042_STATUS ) & DATA_AVAILABLE)
  148. IO::in8(I8042_BUFFER);
  149. enable_irq();
  150. }
  151. Keyboard::~Keyboard()
  152. {
  153. }
  154. bool Keyboard::can_read(Process&) const
  155. {
  156. return !m_queue.is_empty();
  157. }
  158. ssize_t Keyboard::read(Process&, byte* buffer, size_t size)
  159. {
  160. ssize_t nread = 0;
  161. while ((size_t)nread < size) {
  162. if (m_queue.is_empty())
  163. break;
  164. // Don't return partial data frames.
  165. if ((size - nread) < sizeof(Event))
  166. break;
  167. auto event = m_queue.dequeue();
  168. memcpy(buffer, &event, sizeof(Event));
  169. nread += sizeof(Event);
  170. }
  171. return nread;
  172. }
  173. ssize_t Keyboard::write(Process&, const byte*, size_t)
  174. {
  175. return 0;
  176. }
  177. KeyboardClient::~KeyboardClient()
  178. {
  179. }