KeyboardDevice.cpp 5.8 KB

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