KeyboardDevice.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/Debug.h>
  33. #include <Kernel/Devices/KeyboardDevice.h>
  34. #include <Kernel/IO.h>
  35. #include <Kernel/TTY/VirtualConsole.h>
  36. namespace Kernel {
  37. #define IRQ_KEYBOARD 1
  38. static const KeyCode unshifted_key_map[0x80] = {
  39. Key_Invalid,
  40. Key_Escape,
  41. Key_1,
  42. Key_2,
  43. Key_3,
  44. Key_4,
  45. Key_5,
  46. Key_6,
  47. Key_7,
  48. Key_8,
  49. Key_9,
  50. Key_0,
  51. Key_Minus,
  52. Key_Equal,
  53. Key_Backspace,
  54. Key_Tab, //15
  55. Key_Q,
  56. Key_W,
  57. Key_E,
  58. Key_R,
  59. Key_T,
  60. Key_Y,
  61. Key_U,
  62. Key_I,
  63. Key_O,
  64. Key_P,
  65. Key_LeftBracket,
  66. Key_RightBracket,
  67. Key_Return, // 28
  68. Key_Control, // 29
  69. Key_A,
  70. Key_S,
  71. Key_D,
  72. Key_F,
  73. Key_G,
  74. Key_H,
  75. Key_J,
  76. Key_K,
  77. Key_L,
  78. Key_Semicolon,
  79. Key_Apostrophe,
  80. Key_Backtick,
  81. Key_LeftShift, // 42
  82. Key_Backslash,
  83. Key_Z,
  84. Key_X,
  85. Key_C,
  86. Key_V,
  87. Key_B,
  88. Key_N,
  89. Key_M,
  90. Key_Comma,
  91. Key_Period,
  92. Key_Slash,
  93. Key_RightShift, // 54
  94. Key_Asterisk,
  95. Key_Alt, // 56
  96. Key_Space, // 57
  97. Key_CapsLock, // 58
  98. Key_F1,
  99. Key_F2,
  100. Key_F3,
  101. Key_F4,
  102. Key_F5,
  103. Key_F6,
  104. Key_F7,
  105. Key_F8,
  106. Key_F9,
  107. Key_F10,
  108. Key_NumLock,
  109. Key_Invalid, // 70
  110. Key_Home,
  111. Key_Up,
  112. Key_PageUp,
  113. Key_Minus,
  114. Key_Left,
  115. Key_Invalid,
  116. Key_Right, // 77
  117. Key_Plus,
  118. Key_End,
  119. Key_Down, // 80
  120. Key_PageDown,
  121. Key_Invalid,
  122. Key_Delete, // 83
  123. Key_Invalid,
  124. Key_Invalid,
  125. Key_Backslash,
  126. Key_F11,
  127. Key_F12,
  128. Key_Invalid,
  129. Key_Invalid,
  130. Key_Logo,
  131. };
  132. static const KeyCode shifted_key_map[0x100] = {
  133. Key_Invalid,
  134. Key_Escape,
  135. Key_ExclamationPoint,
  136. Key_AtSign,
  137. Key_Hashtag,
  138. Key_Dollar,
  139. Key_Percent,
  140. Key_Circumflex,
  141. Key_Ampersand,
  142. Key_Asterisk,
  143. Key_LeftParen,
  144. Key_RightParen,
  145. Key_Underscore,
  146. Key_Plus,
  147. Key_Backspace,
  148. Key_Tab,
  149. Key_Q,
  150. Key_W,
  151. Key_E,
  152. Key_R,
  153. Key_T,
  154. Key_Y,
  155. Key_U,
  156. Key_I,
  157. Key_O,
  158. Key_P,
  159. Key_LeftBrace,
  160. Key_RightBrace,
  161. Key_Return,
  162. Key_Control,
  163. Key_A,
  164. Key_S,
  165. Key_D,
  166. Key_F,
  167. Key_G,
  168. Key_H,
  169. Key_J,
  170. Key_K,
  171. Key_L,
  172. Key_Colon,
  173. Key_DoubleQuote,
  174. Key_Tilde,
  175. Key_LeftShift, // 42
  176. Key_Pipe,
  177. Key_Z,
  178. Key_X,
  179. Key_C,
  180. Key_V,
  181. Key_B,
  182. Key_N,
  183. Key_M,
  184. Key_LessThan,
  185. Key_GreaterThan,
  186. Key_QuestionMark,
  187. Key_RightShift, // 54
  188. Key_Asterisk,
  189. Key_Alt,
  190. Key_Space, // 57
  191. Key_CapsLock, // 58
  192. Key_F1,
  193. Key_F2,
  194. Key_F3,
  195. Key_F4,
  196. Key_F5,
  197. Key_F6,
  198. Key_F7,
  199. Key_F8,
  200. Key_F9,
  201. Key_F10,
  202. Key_NumLock,
  203. Key_Invalid, // 70
  204. Key_Home,
  205. Key_Up,
  206. Key_PageUp,
  207. Key_Minus,
  208. Key_Left,
  209. Key_Invalid,
  210. Key_Right, // 77
  211. Key_Plus,
  212. Key_End,
  213. Key_Down, // 80
  214. Key_PageDown,
  215. Key_Invalid,
  216. Key_Delete, // 83
  217. Key_Invalid,
  218. Key_Invalid,
  219. Key_Pipe,
  220. Key_F11,
  221. Key_F12,
  222. Key_Invalid,
  223. Key_Invalid,
  224. Key_Logo,
  225. };
  226. 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 };
  227. void KeyboardDevice::key_state_changed(u8 scan_code, bool pressed)
  228. {
  229. KeyCode key = (m_modifiers & Mod_Shift) ? shifted_key_map[scan_code] : unshifted_key_map[scan_code];
  230. if (key == Key_NumLock && pressed)
  231. m_num_lock_on = !m_num_lock_on;
  232. if (m_num_lock_on && !m_has_e0_prefix) {
  233. if (scan_code >= 0x47 && scan_code <= 0x53) {
  234. u8 index = scan_code - 0x47;
  235. KeyCode newKey = numpad_key_map[index];
  236. if (newKey != Key_Invalid) {
  237. key = newKey;
  238. }
  239. }
  240. }
  241. if (key == Key_CapsLock && pressed)
  242. m_caps_lock_on = !m_caps_lock_on;
  243. Event event;
  244. event.key = key;
  245. event.scancode = m_has_e0_prefix ? 0xe000 + scan_code : scan_code;
  246. event.flags = m_modifiers;
  247. event.e0_prefix = m_has_e0_prefix;
  248. event.caps_lock_on = m_caps_lock_on;
  249. event.code_point = m_character_map.get_char(event);
  250. if (pressed)
  251. event.flags |= Is_Press;
  252. if (m_client)
  253. m_client->on_key_pressed(event);
  254. {
  255. ScopedSpinLock lock(m_queue_lock);
  256. m_queue.enqueue(event);
  257. }
  258. m_has_e0_prefix = false;
  259. evaluate_block_conditions();
  260. }
  261. void KeyboardDevice::handle_irq(const RegisterState&)
  262. {
  263. // The controller will read the data and call irq_handle_byte_read
  264. // for the appropriate device
  265. m_controller.irq_process_input_buffer(I8042Controller::Device::Keyboard);
  266. }
  267. void KeyboardDevice::irq_handle_byte_read(u8 byte)
  268. {
  269. u8 ch = byte & 0x7f;
  270. bool pressed = !(byte & 0x80);
  271. m_entropy_source.add_random_event(byte);
  272. if (byte == 0xe0) {
  273. m_has_e0_prefix = true;
  274. return;
  275. }
  276. #if KEYBOARD_DEBUG
  277. dbgln("Keyboard::irq_handle_byte_read: {:#02x} {}", ch, (pressed ? "down" : "up"));
  278. #endif
  279. switch (ch) {
  280. case 0x38:
  281. if (m_has_e0_prefix)
  282. update_modifier(Mod_AltGr, pressed);
  283. else
  284. update_modifier(Mod_Alt, pressed);
  285. break;
  286. case 0x1d:
  287. update_modifier(Mod_Ctrl, pressed);
  288. break;
  289. case 0x5b:
  290. update_modifier(Mod_Logo, pressed);
  291. break;
  292. case 0x2a:
  293. case 0x36:
  294. update_modifier(Mod_Shift, pressed);
  295. break;
  296. }
  297. switch (ch) {
  298. case I8042_ACK:
  299. break;
  300. default:
  301. if (m_modifiers & Mod_Alt) {
  302. switch (ch) {
  303. case 0x02 ... 0x07: // 1 to 6
  304. VirtualConsole::switch_to(ch - 0x02);
  305. break;
  306. default:
  307. key_state_changed(ch, pressed);
  308. break;
  309. }
  310. } else {
  311. key_state_changed(ch, pressed);
  312. }
  313. }
  314. }
  315. static AK::Singleton<KeyboardDevice> s_the;
  316. KeyboardDevice& KeyboardDevice::the()
  317. {
  318. return *s_the;
  319. }
  320. KeyboardDevice::KeyboardDevice()
  321. : IRQHandler(IRQ_KEYBOARD)
  322. , CharacterDevice(85, 1)
  323. , m_controller(I8042Controller::the())
  324. {
  325. }
  326. KeyboardDevice::~KeyboardDevice()
  327. {
  328. }
  329. bool KeyboardDevice::initialize()
  330. {
  331. if (!m_controller.reset_device(I8042Controller::Device::Keyboard)) {
  332. dbgln("KeyboardDevice: I8042 controller failed to reset device");
  333. return false;
  334. }
  335. return true;
  336. }
  337. bool KeyboardDevice::can_read(const FileDescription&, size_t) const
  338. {
  339. return !m_queue.is_empty();
  340. }
  341. KResultOr<size_t> KeyboardDevice::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
  342. {
  343. size_t nread = 0;
  344. ScopedSpinLock lock(m_queue_lock);
  345. while (nread < size) {
  346. if (m_queue.is_empty())
  347. break;
  348. // Don't return partial data frames.
  349. if ((size - nread) < (ssize_t)sizeof(Event))
  350. break;
  351. auto event = m_queue.dequeue();
  352. lock.unlock();
  353. ssize_t n = buffer.write_buffered<sizeof(Event)>(sizeof(Event), [&](u8* data, size_t data_bytes) {
  354. memcpy(data, &event, sizeof(Event));
  355. return (ssize_t)data_bytes;
  356. });
  357. if (n < 0)
  358. return KResult((ErrnoCode)-n);
  359. ASSERT((size_t)n == sizeof(Event));
  360. nread += sizeof(Event);
  361. lock.lock();
  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. dbgln("New Character map '{}' passing to client.", character_map_name);
  377. }
  378. }