KeyboardDevice.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/Singleton.h>
  29. #include <AK/StringView.h>
  30. #include <AK/Types.h>
  31. #include <Kernel/Arch/i386/CPU.h>
  32. #include <Kernel/Devices/KeyboardDevice.h>
  33. #include <Kernel/IO.h>
  34. #include <Kernel/TTY/VirtualConsole.h>
  35. //#define KEYBOARD_DEBUG
  36. namespace Kernel {
  37. #define IRQ_KEYBOARD 1
  38. #define I8042_BUFFER 0x60
  39. #define I8042_STATUS 0x64
  40. #define I8042_ACK 0xFA
  41. #define I8042_BUFFER_FULL 0x01
  42. #define I8042_WHICH_BUFFER 0x20
  43. #define I8042_MOUSE_BUFFER 0x20
  44. #define I8042_KEYBOARD_BUFFER 0x00
  45. static const KeyCode unshifted_key_map[0x80] = {
  46. Key_Invalid,
  47. Key_Escape,
  48. Key_1,
  49. Key_2,
  50. Key_3,
  51. Key_4,
  52. Key_5,
  53. Key_6,
  54. Key_7,
  55. Key_8,
  56. Key_9,
  57. Key_0,
  58. Key_Minus,
  59. Key_Equal,
  60. Key_Backspace,
  61. Key_Tab, //15
  62. Key_Q,
  63. Key_W,
  64. Key_E,
  65. Key_R,
  66. Key_T,
  67. Key_Y,
  68. Key_U,
  69. Key_I,
  70. Key_O,
  71. Key_P,
  72. Key_LeftBracket,
  73. Key_RightBracket,
  74. Key_Return, // 28
  75. Key_Control, // 29
  76. Key_A,
  77. Key_S,
  78. Key_D,
  79. Key_F,
  80. Key_G,
  81. Key_H,
  82. Key_J,
  83. Key_K,
  84. Key_L,
  85. Key_Semicolon,
  86. Key_Apostrophe,
  87. Key_Backtick,
  88. Key_LeftShift, // 42
  89. Key_Backslash,
  90. Key_Z,
  91. Key_X,
  92. Key_C,
  93. Key_V,
  94. Key_B,
  95. Key_N,
  96. Key_M,
  97. Key_Comma,
  98. Key_Period,
  99. Key_Slash,
  100. Key_RightShift, // 54
  101. Key_Asterisk,
  102. Key_Alt, // 56
  103. Key_Space, // 57
  104. Key_CapsLock, // 58
  105. Key_F1,
  106. Key_F2,
  107. Key_F3,
  108. Key_F4,
  109. Key_F5,
  110. Key_F6,
  111. Key_F7,
  112. Key_F8,
  113. Key_F9,
  114. Key_F10,
  115. Key_NumLock,
  116. Key_Invalid, // 70
  117. Key_Home,
  118. Key_Up,
  119. Key_PageUp,
  120. Key_Minus,
  121. Key_Left,
  122. Key_Invalid,
  123. Key_Right, // 77
  124. Key_Plus,
  125. Key_End,
  126. Key_Down, // 80
  127. Key_PageDown,
  128. Key_Invalid,
  129. Key_Delete, // 83
  130. Key_Invalid,
  131. Key_Invalid,
  132. Key_Backslash,
  133. Key_F11,
  134. Key_F12,
  135. Key_Invalid,
  136. Key_Invalid,
  137. Key_Logo,
  138. };
  139. static const KeyCode shifted_key_map[0x100] = {
  140. Key_Invalid,
  141. Key_Escape,
  142. Key_ExclamationPoint,
  143. Key_AtSign,
  144. Key_Hashtag,
  145. Key_Dollar,
  146. Key_Percent,
  147. Key_Circumflex,
  148. Key_Ampersand,
  149. Key_Asterisk,
  150. Key_LeftParen,
  151. Key_RightParen,
  152. Key_Underscore,
  153. Key_Plus,
  154. Key_Backspace,
  155. Key_Tab,
  156. Key_Q,
  157. Key_W,
  158. Key_E,
  159. Key_R,
  160. Key_T,
  161. Key_Y,
  162. Key_U,
  163. Key_I,
  164. Key_O,
  165. Key_P,
  166. Key_LeftBrace,
  167. Key_RightBrace,
  168. Key_Return,
  169. Key_Control,
  170. Key_A,
  171. Key_S,
  172. Key_D,
  173. Key_F,
  174. Key_G,
  175. Key_H,
  176. Key_J,
  177. Key_K,
  178. Key_L,
  179. Key_Colon,
  180. Key_DoubleQuote,
  181. Key_Tilde,
  182. Key_LeftShift, // 42
  183. Key_Pipe,
  184. Key_Z,
  185. Key_X,
  186. Key_C,
  187. Key_V,
  188. Key_B,
  189. Key_N,
  190. Key_M,
  191. Key_LessThan,
  192. Key_GreaterThan,
  193. Key_QuestionMark,
  194. Key_RightShift, // 54
  195. Key_Asterisk,
  196. Key_Alt,
  197. Key_Space, // 57
  198. Key_CapsLock, // 58
  199. Key_F1,
  200. Key_F2,
  201. Key_F3,
  202. Key_F4,
  203. Key_F5,
  204. Key_F6,
  205. Key_F7,
  206. Key_F8,
  207. Key_F9,
  208. Key_F10,
  209. Key_NumLock,
  210. Key_Invalid, // 70
  211. Key_Home,
  212. Key_Up,
  213. Key_PageUp,
  214. Key_Minus,
  215. Key_Left,
  216. Key_Invalid,
  217. Key_Right, // 77
  218. Key_Plus,
  219. Key_End,
  220. Key_Down, // 80
  221. Key_PageDown,
  222. Key_Invalid,
  223. Key_Delete, // 83
  224. Key_Invalid,
  225. Key_Invalid,
  226. Key_Pipe,
  227. Key_F11,
  228. Key_F12,
  229. Key_Invalid,
  230. Key_Invalid,
  231. Key_Logo,
  232. };
  233. static const 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 };
  234. void KeyboardDevice::key_state_changed(u8 scan_code, bool pressed)
  235. {
  236. KeyCode key = (m_modifiers & Mod_Shift) ? shifted_key_map[scan_code] : unshifted_key_map[scan_code];
  237. if (key == Key_NumLock && pressed)
  238. m_num_lock_on = !m_num_lock_on;
  239. if (m_num_lock_on && !m_has_e0_prefix) {
  240. if (scan_code >= 0x47 && scan_code <= 0x53) {
  241. u8 index = scan_code - 0x47;
  242. KeyCode newKey = numpad_key_map[index];
  243. if (newKey != Key_Invalid) {
  244. key = newKey;
  245. }
  246. }
  247. }
  248. if (key == Key_CapsLock && pressed)
  249. m_caps_lock_on = !m_caps_lock_on;
  250. Event event;
  251. event.key = key;
  252. event.scancode = m_has_e0_prefix ? 0xe000 + scan_code : scan_code;
  253. event.flags = m_modifiers;
  254. event.e0_prefix = m_has_e0_prefix;
  255. event.caps_lock_on = m_caps_lock_on;
  256. event.code_point = m_character_map.get_char(event);
  257. if (pressed)
  258. event.flags |= Is_Press;
  259. if (m_client)
  260. m_client->on_key_pressed(event);
  261. m_queue.enqueue(event);
  262. m_has_e0_prefix = false;
  263. }
  264. void KeyboardDevice::handle_irq(const RegisterState&)
  265. {
  266. for (;;) {
  267. u8 status = IO::in8(I8042_STATUS);
  268. if (!(((status & I8042_WHICH_BUFFER) == I8042_KEYBOARD_BUFFER) && (status & I8042_BUFFER_FULL)))
  269. return;
  270. u8 raw = IO::in8(I8042_BUFFER);
  271. u8 ch = raw & 0x7f;
  272. bool pressed = !(raw & 0x80);
  273. m_entropy_source.add_random_event(raw);
  274. if (raw == 0xe0) {
  275. m_has_e0_prefix = true;
  276. return;
  277. }
  278. #ifdef KEYBOARD_DEBUG
  279. dbg() << "Keyboard::handle_irq: " << String::format("%b", ch) << " " << (pressed ? "down" : "up");
  280. #endif
  281. switch (ch) {
  282. case 0x38:
  283. if (m_has_e0_prefix)
  284. update_modifier(Mod_AltGr, pressed);
  285. else
  286. update_modifier(Mod_Alt, pressed);
  287. break;
  288. case 0x1d:
  289. update_modifier(Mod_Ctrl, pressed);
  290. break;
  291. case 0x5b:
  292. update_modifier(Mod_Logo, pressed);
  293. break;
  294. case 0x2a:
  295. case 0x36:
  296. update_modifier(Mod_Shift, pressed);
  297. break;
  298. }
  299. switch (ch) {
  300. case I8042_ACK:
  301. break;
  302. default:
  303. if (m_modifiers & Mod_Alt) {
  304. switch (ch) {
  305. case 0x02 ... 0x07: // 1 to 6
  306. VirtualConsole::switch_to(ch - 0x02);
  307. break;
  308. default:
  309. key_state_changed(ch, pressed);
  310. break;
  311. }
  312. } else {
  313. key_state_changed(ch, pressed);
  314. }
  315. }
  316. }
  317. }
  318. static AK::Singleton<KeyboardDevice> s_the;
  319. void KeyboardDevice::initialize()
  320. {
  321. s_the.ensure_instance();
  322. }
  323. KeyboardDevice& KeyboardDevice::the()
  324. {
  325. return *s_the;
  326. }
  327. KeyboardDevice::KeyboardDevice()
  328. : IRQHandler(IRQ_KEYBOARD)
  329. , CharacterDevice(85, 1)
  330. {
  331. // Empty the buffer of any pending data.
  332. // I don't care what you've been pressing until now!
  333. while (IO::in8(I8042_STATUS) & I8042_BUFFER_FULL)
  334. IO::in8(I8042_BUFFER);
  335. enable_irq();
  336. }
  337. KeyboardDevice::~KeyboardDevice()
  338. {
  339. }
  340. bool KeyboardDevice::can_read(const FileDescription&, size_t) const
  341. {
  342. return !m_queue.is_empty();
  343. }
  344. KResultOr<size_t> KeyboardDevice::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
  345. {
  346. size_t nread = 0;
  347. while (nread < size) {
  348. if (m_queue.is_empty())
  349. break;
  350. // Don't return partial data frames.
  351. if ((size - nread) < (ssize_t)sizeof(Event))
  352. break;
  353. auto event = m_queue.dequeue();
  354. ssize_t n = buffer.write_buffered<sizeof(Event)>(sizeof(Event), [&](u8* data, size_t data_bytes) {
  355. memcpy(data, &event, sizeof(Event));
  356. return (ssize_t)data_bytes;
  357. });
  358. if (n < 0)
  359. return KResult(n);
  360. ASSERT((size_t)n == sizeof(Event));
  361. nread += sizeof(Event);
  362. }
  363. return nread;
  364. }
  365. KResultOr<size_t> KeyboardDevice::write(FileDescription&, size_t, const UserOrKernelBuffer&, size_t)
  366. {
  367. return 0;
  368. }
  369. KeyboardClient::~KeyboardClient()
  370. {
  371. }
  372. void KeyboardDevice::set_maps(const Keyboard::CharacterMapData& character_map_data, const String& character_map_name)
  373. {
  374. m_character_map.set_character_map_data(character_map_data);
  375. m_character_map.set_character_map_name(character_map_name);
  376. dbg() << "New Character map \"" << character_map_name << "\" passing to client.";
  377. }
  378. }