Keyboard.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 IRQ_KEYBOARD 1
  9. #define I8042_BUFFER 0x60
  10. #define I8042_STATUS 0x64
  11. #define SET_LEDS 0xED
  12. #define DATA_AVAILABLE 0x01
  13. #define I8042_ACK 0xFA
  14. #define MOD_ALT 1
  15. #define MOD_CTRL 2
  16. #define MOD_SHIFT 4
  17. static char map[0x100] =
  18. {
  19. 0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08, 0,
  20. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 0, 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[0x100] =
  26. {
  27. 0, 0, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0, 0,
  28. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 0, 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. void Keyboard::emit(byte ch)
  34. {
  35. Key key;
  36. key.character = ch;
  37. key.modifiers = m_modifiers;
  38. if (m_client)
  39. m_client->on_key_pressed(key);
  40. m_queue.enqueue(key);
  41. }
  42. void Keyboard::handle_irq()
  43. {
  44. while (IO::in8(0x64) & 1) {
  45. byte ch = IO::in8(0x60);
  46. switch (ch) {
  47. case 0x38: m_modifiers |= Mod_Alt; break;
  48. case 0xB8: m_modifiers &= ~Mod_Alt; break;
  49. case 0x1D: m_modifiers |= Mod_Ctrl; break;
  50. case 0x9D: m_modifiers &= ~Mod_Ctrl; break;
  51. case 0x2A: m_modifiers |= Mod_Shift; break;
  52. case 0xAA: m_modifiers &= ~Mod_Shift; break;
  53. case 0x1C: /* enter */ emit('\n'); break;
  54. case 0xFA: /* i8042 ack */ break;
  55. default:
  56. if (ch & 0x80) {
  57. // key has been depressed
  58. break;
  59. }
  60. if (m_modifiers & MOD_ALT) {
  61. switch (map[ch]) {
  62. case '1':
  63. case '2':
  64. case '3':
  65. case '4':
  66. VirtualConsole::switch_to(map[ch] - '0' - 1);
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. if (!m_modifiers)
  73. emit(map[ch]);
  74. else if (m_modifiers & Mod_Shift)
  75. emit(shift_map[ch]);
  76. else if (m_modifiers & Mod_Ctrl)
  77. emit(map[ch]);
  78. }
  79. //break;
  80. }
  81. }
  82. static Keyboard* s_the;
  83. Keyboard& Keyboard::the()
  84. {
  85. ASSERT(s_the);
  86. return *s_the;
  87. }
  88. Keyboard::Keyboard()
  89. : IRQHandler(IRQ_KEYBOARD)
  90. , CharacterDevice(85, 1)
  91. {
  92. s_the = this;
  93. // Empty the buffer of any pending data.
  94. // I don't care what you've been pressing until now!
  95. while (IO::in8(I8042_STATUS ) & DATA_AVAILABLE)
  96. IO::in8(I8042_BUFFER);
  97. enable_irq();
  98. }
  99. Keyboard::~Keyboard()
  100. {
  101. }
  102. bool Keyboard::can_read(Process&) const
  103. {
  104. return !m_queue.is_empty();
  105. }
  106. ssize_t Keyboard::read(Process&, byte* buffer, size_t size)
  107. {
  108. ssize_t nread = 0;
  109. while ((size_t)nread < size) {
  110. if (m_queue.is_empty())
  111. break;
  112. // Don't return partial data frames.
  113. if ((size - nread) < 2)
  114. break;
  115. auto key = m_queue.dequeue();
  116. buffer[nread++] = key.character;
  117. buffer[nread++] = key.modifiers;
  118. }
  119. return nread;
  120. }
  121. ssize_t Keyboard::write(Process&, const byte*, size_t)
  122. {
  123. return 0;
  124. }
  125. KeyboardClient::~KeyboardClient()
  126. {
  127. }