KeyboardDevice.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include <AK/Assertions.h>
  2. #include <AK/Types.h>
  3. #include <Kernel/Arch/i386/CPU.h>
  4. #include <Kernel/Arch/i386/PIC.h>
  5. #include <Kernel/Devices/KeyboardDevice.h>
  6. #include <Kernel/TTY/VirtualConsole.h>
  7. #include <Kernel/IO.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. char *map;
  18. char *shift_map;
  19. char *alt_map;
  20. char *altgr_map;
  21. static char en_map[0x80] = {
  22. 0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08, '\t',
  23. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0,
  24. 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\',
  25. 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
  26. 0, '*', 0, ' ', 0, 0,
  27. //60
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. //70
  30. 0, 0, 0, 0, '-', 0, 0, 0, '+', 0,
  31. //80
  32. 0, 0, 0, 0, 0, 0, '\\', 0, 0, 0,
  33. };
  34. static char en_shift_map[0x80] = {
  35. 0, '\033', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0x08, '\t',
  36. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0,
  37. 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0, '|',
  38. 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
  39. 0, '*', 0, ' ', 0, 0,
  40. //60
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. //70
  43. 0, 0, 0, 0, '-', 0, 0, 0, '+', 0,
  44. //80
  45. 0, 0, 0, 0, 0, 0, '|', 0, 0, 0,
  46. };
  47. static char numpad_map[13] = { '7', '8', '9', 0, '4', '5', '6', 0, '1', '2', '3', '0', ',' };
  48. static KeyCode unshifted_key_map[0x80] = {
  49. Key_Invalid,
  50. Key_Escape,
  51. Key_1,
  52. Key_2,
  53. Key_3,
  54. Key_4,
  55. Key_5,
  56. Key_6,
  57. Key_7,
  58. Key_8,
  59. Key_9,
  60. Key_0,
  61. Key_Minus,
  62. Key_Equal,
  63. Key_Backspace,
  64. Key_Tab, //15
  65. Key_Q,
  66. Key_W,
  67. Key_E,
  68. Key_R,
  69. Key_T,
  70. Key_Y,
  71. Key_U,
  72. Key_I,
  73. Key_O,
  74. Key_P,
  75. Key_LeftBracket,
  76. Key_RightBracket,
  77. Key_Return, // 28
  78. Key_Control, // 29
  79. Key_A,
  80. Key_S,
  81. Key_D,
  82. Key_F,
  83. Key_G,
  84. Key_H,
  85. Key_J,
  86. Key_K,
  87. Key_L,
  88. Key_Semicolon,
  89. Key_Apostrophe,
  90. Key_Backtick,
  91. Key_LeftShift, // 42
  92. Key_Backslash,
  93. Key_Z,
  94. Key_X,
  95. Key_C,
  96. Key_V,
  97. Key_B,
  98. Key_N,
  99. Key_M,
  100. Key_Comma,
  101. Key_Period,
  102. Key_Slash,
  103. Key_RightShift, // 54
  104. Key_Asterisk,
  105. Key_Alt, // 56
  106. Key_Space, // 57
  107. Key_CapsLock, // 58
  108. Key_F1,
  109. Key_F2,
  110. Key_F3,
  111. Key_F4,
  112. Key_F5,
  113. Key_F6,
  114. Key_F7,
  115. Key_F8,
  116. Key_F9,
  117. Key_F10,
  118. Key_NumLock,
  119. Key_Invalid, // 70
  120. Key_Home,
  121. Key_Up,
  122. Key_PageUp,
  123. Key_Minus,
  124. Key_Left,
  125. Key_Invalid,
  126. Key_Right, // 77
  127. Key_Plus,
  128. Key_End,
  129. Key_Down, // 80
  130. Key_PageDown,
  131. Key_Invalid,
  132. Key_Delete, // 83
  133. Key_Invalid,
  134. Key_Invalid,
  135. Key_Backslash,
  136. Key_F11,
  137. Key_F12,
  138. Key_Invalid,
  139. Key_Invalid,
  140. Key_Logo,
  141. };
  142. static KeyCode shifted_key_map[0x100] = {
  143. Key_Invalid,
  144. Key_Escape,
  145. Key_ExclamationPoint,
  146. Key_AtSign,
  147. Key_Hashtag,
  148. Key_Dollar,
  149. Key_Percent,
  150. Key_Circumflex,
  151. Key_Ampersand,
  152. Key_Asterisk,
  153. Key_LeftParen,
  154. Key_RightParen,
  155. Key_Underscore,
  156. Key_Plus,
  157. Key_Backspace,
  158. Key_Tab,
  159. Key_Q,
  160. Key_W,
  161. Key_E,
  162. Key_R,
  163. Key_T,
  164. Key_Y,
  165. Key_U,
  166. Key_I,
  167. Key_O,
  168. Key_P,
  169. Key_LeftBrace,
  170. Key_RightBrace,
  171. Key_Return,
  172. Key_Control,
  173. Key_A,
  174. Key_S,
  175. Key_D,
  176. Key_F,
  177. Key_G,
  178. Key_H,
  179. Key_J,
  180. Key_K,
  181. Key_L,
  182. Key_Colon,
  183. Key_DoubleQuote,
  184. Key_Tilde,
  185. Key_LeftShift, // 42
  186. Key_Pipe,
  187. Key_Z,
  188. Key_X,
  189. Key_C,
  190. Key_V,
  191. Key_B,
  192. Key_N,
  193. Key_M,
  194. Key_LessThan,
  195. Key_GreaterThan,
  196. Key_QuestionMark,
  197. Key_RightShift, // 54
  198. Key_Asterisk,
  199. Key_Alt,
  200. Key_Space, // 57
  201. Key_CapsLock, // 58
  202. Key_F1,
  203. Key_F2,
  204. Key_F3,
  205. Key_F4,
  206. Key_F5,
  207. Key_F6,
  208. Key_F7,
  209. Key_F8,
  210. Key_F9,
  211. Key_F10,
  212. Key_NumLock,
  213. Key_Invalid, // 70
  214. Key_Home,
  215. Key_Up,
  216. Key_PageUp,
  217. Key_Minus,
  218. Key_Left,
  219. Key_Invalid,
  220. Key_Right, // 77
  221. Key_Plus,
  222. Key_End,
  223. Key_Down, // 80
  224. Key_PageDown,
  225. Key_Invalid,
  226. Key_Delete, // 83
  227. Key_Invalid,
  228. Key_Invalid,
  229. Key_Pipe,
  230. Key_F11,
  231. Key_F12,
  232. Key_Invalid,
  233. Key_Invalid,
  234. Key_Logo,
  235. };
  236. static KeyCode numpad_key_map[13] = { Key_7, Key_8, Key_9, Key_Invalid, Key_4, Key_5, Key_6, Key_Invalid, Key_1, Key_2, Key_3, Key_0, Key_Comma };
  237. void KeyboardDevice::key_state_changed(u8 raw, bool pressed)
  238. {
  239. KeyCode key = (m_modifiers & Mod_Shift) ? shifted_key_map[raw] : unshifted_key_map[raw];
  240. char character = (m_modifiers & Mod_Shift) ? shift_map[raw] : (m_modifiers & Mod_Alt) ? alt_map[raw] : (m_modifiers & Mod_AltGr) ? altgr_map[raw] : map[raw];
  241. if (key == Key_NumLock && pressed)
  242. m_num_lock_on = !m_num_lock_on;
  243. if (m_num_lock_on && !m_has_e0_prefix)
  244. {
  245. if (raw >= 0x47 && raw <= 0x53)
  246. {
  247. u8 index = raw - 0x47;
  248. KeyCode newKey = numpad_key_map[index];
  249. if (newKey != Key_Invalid)
  250. {
  251. key = newKey;
  252. character = numpad_map[index];
  253. }
  254. }
  255. }
  256. else
  257. {
  258. if(m_has_e0_prefix) {
  259. if(key == Key_Slash) {
  260. character = '/'; // On Turkish-QWERTY Keyboard Key_Slash mapped to '.' char, if e0 prefix is true remap to '/' char
  261. }
  262. }
  263. }
  264. if (key == Key_CapsLock && pressed)
  265. m_caps_lock_on = !m_caps_lock_on;
  266. if (m_caps_lock_on && (m_modifiers == 0 || m_modifiers == Mod_Shift))
  267. {
  268. if (character >= 'a' && character <= 'z')
  269. character &= ~0x20;
  270. else if (character >= 'A' && character <= 'Z')
  271. character |= 0x20;
  272. }
  273. Event event;
  274. event.key = key;
  275. event.character = static_cast<u8>(character);
  276. event.flags = m_modifiers;
  277. if (pressed)
  278. event.flags |= Is_Press;
  279. if (m_client)
  280. m_client->on_key_pressed(event);
  281. m_queue.enqueue(event);
  282. m_has_e0_prefix = false;
  283. }
  284. void KeyboardDevice::handle_irq()
  285. {
  286. for (;;) {
  287. u8 status = IO::in8(I8042_STATUS);
  288. if (!(((status & I8042_WHICH_BUFFER) == I8042_KEYBOARD_BUFFER) && (status & I8042_BUFFER_FULL)))
  289. return;
  290. u8 raw = IO::in8(I8042_BUFFER);
  291. u8 ch = raw & 0x7f;
  292. bool pressed = !(raw & 0x80);
  293. if (raw == 0xe0) {
  294. m_has_e0_prefix = true;
  295. return;
  296. }
  297. #ifdef KEYBOARD_DEBUG
  298. dbgprintf("Keyboard::handle_irq: %b %s\n", ch, pressed ? "down" : "up");
  299. #endif
  300. switch (ch) {
  301. case 0x38:
  302. if (m_has_e0_prefix)
  303. update_modifier(Mod_AltGr, pressed);
  304. else
  305. update_modifier(Mod_Alt, pressed);
  306. break;
  307. case 0x1d:
  308. update_modifier(Mod_Ctrl, pressed);
  309. break;
  310. case 0x5b:
  311. update_modifier(Mod_Logo, pressed);
  312. break;
  313. case 0x2a:
  314. case 0x36:
  315. update_modifier(Mod_Shift, pressed);
  316. break;
  317. }
  318. switch (ch) {
  319. case I8042_ACK:
  320. break;
  321. default:
  322. if (m_modifiers & Mod_Alt) {
  323. switch (map[ch]) {
  324. case '1':
  325. case '2':
  326. case '3':
  327. case '4':
  328. VirtualConsole::switch_to(map[ch] - '0' - 1);
  329. break;
  330. default:
  331. key_state_changed(ch, pressed);
  332. break;
  333. }
  334. } else {
  335. key_state_changed(ch, pressed);
  336. }
  337. }
  338. }
  339. }
  340. static KeyboardDevice* s_the;
  341. KeyboardDevice& KeyboardDevice::the()
  342. {
  343. ASSERT(s_the);
  344. return *s_the;
  345. }
  346. KeyboardDevice::KeyboardDevice()
  347. : IRQHandler(IRQ_KEYBOARD)
  348. , CharacterDevice(85, 1)
  349. {
  350. s_the = this;
  351. KeyboardDevice::set_maps(en_map, en_shift_map, en_map, en_map);
  352. // Empty the buffer of any pending data.
  353. // I don't care what you've been pressing until now!
  354. while (IO::in8(I8042_STATUS) & I8042_BUFFER_FULL)
  355. IO::in8(I8042_BUFFER);
  356. enable_irq();
  357. }
  358. KeyboardDevice::~KeyboardDevice()
  359. {
  360. }
  361. bool KeyboardDevice::can_read(const FileDescription&) const
  362. {
  363. return !m_queue.is_empty();
  364. }
  365. ssize_t KeyboardDevice::read(FileDescription&, u8* buffer, ssize_t size)
  366. {
  367. ssize_t nread = 0;
  368. while (nread < size) {
  369. if (m_queue.is_empty())
  370. break;
  371. // Don't return partial data frames.
  372. if ((size - nread) < (ssize_t)sizeof(Event))
  373. break;
  374. auto event = m_queue.dequeue();
  375. memcpy(buffer, &event, sizeof(Event));
  376. nread += sizeof(Event);
  377. }
  378. return nread;
  379. }
  380. ssize_t KeyboardDevice::write(FileDescription&, const u8*, ssize_t)
  381. {
  382. return 0;
  383. }
  384. KeyboardClient::~KeyboardClient()
  385. {
  386. }
  387. void KeyboardDevice::set_maps(const char* n_map, const char* n_shift_map, const char* n_alt_map, const char* n_altgr_map)
  388. {
  389. kfree(map);
  390. kfree(shift_map);
  391. kfree(alt_map);
  392. kfree(altgr_map);
  393. map = (char*) kmalloc(0x80);
  394. shift_map = (char*) kmalloc(0x80);
  395. alt_map = (char*) kmalloc(0x80);
  396. altgr_map = (char*) kmalloc(0x80);
  397. for(int i=0; i < 0x80; i++) {
  398. map[i] = n_map[i];
  399. shift_map[i] = n_shift_map[i];
  400. alt_map[i] = n_alt_map[i];
  401. altgr_map[i] = n_altgr_map[i];
  402. }
  403. }