Keyboard.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "types.h"
  2. #include "i386.h"
  3. #include "IO.h"
  4. #include "VGA.h"
  5. #include "PIC.h"
  6. #include "Keyboard.h"
  7. #include "VirtualConsole.h"
  8. #include <AK/Assertions.h>
  9. #define IRQ_KEYBOARD 1
  10. #define I8042_BUFFER 0x60
  11. #define I8042_STATUS 0x64
  12. #define SET_LEDS 0xED
  13. #define DATA_AVAILABLE 0x01
  14. #define I8042_ACK 0xFA
  15. #define MOD_ALT 1
  16. #define MOD_CTRL 2
  17. #define MOD_SHIFT 4
  18. static char map[0x100] =
  19. {
  20. 0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08, 0,
  21. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 0, 0,
  22. 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\',
  23. 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
  24. 0, 0, 0, ' '
  25. };
  26. static char shift_map[0x100] =
  27. {
  28. 0, 0, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0, 0,
  29. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 0, 0,
  30. 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0, '|',
  31. 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
  32. 0, 0, 0, ' '
  33. };
  34. void Keyboard::emit(byte ch)
  35. {
  36. if (m_client)
  37. m_client->onKeyPress(ch);
  38. m_queue.enqueue(ch);
  39. }
  40. void Keyboard::handleIRQ()
  41. {
  42. while (IO::in8(0x64) & 1) {
  43. BYTE ch = IO::in8(0x60);
  44. switch (ch) {
  45. case 0x38: m_modifiers |= MOD_ALT; break;
  46. case 0xB8: m_modifiers &= ~MOD_ALT; break;
  47. case 0x1D: m_modifiers |= MOD_CTRL; break;
  48. case 0x9D: m_modifiers &= ~MOD_CTRL; break;
  49. case 0x2A: m_modifiers |= MOD_SHIFT; break;
  50. case 0xAA: m_modifiers &= ~MOD_SHIFT; break;
  51. case 0x1C: /* enter */ emit('\n'); break;
  52. case 0xFA: /* i8042 ack */ break;
  53. default:
  54. if (ch & 0x80) {
  55. // key has been depressed
  56. break;
  57. }
  58. if (m_modifiers & MOD_ALT) {
  59. switch (map[ch]) {
  60. case '1':
  61. case '2':
  62. case '3':
  63. case '4':
  64. VirtualConsole::switchTo(map[ch] - '0' - 1);
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. if (!m_modifiers) {
  71. emit(map[ch]);
  72. } else if (m_modifiers & MOD_SHIFT) {
  73. emit(shift_map[ch]);
  74. } else if (m_modifiers & MOD_CTRL) {
  75. // FIXME: This is obviously not a good enough way to process ctrl+whatever.
  76. emit('^');
  77. emit(shift_map[ch]);
  78. }
  79. }
  80. //break;
  81. }
  82. }
  83. static Keyboard* s_the;
  84. Keyboard& Keyboard::the()
  85. {
  86. ASSERT(s_the);
  87. return *s_the;
  88. }
  89. Keyboard::Keyboard()
  90. : IRQHandler(IRQ_KEYBOARD)
  91. , CharacterDevice(85, 1)
  92. {
  93. s_the = this;
  94. // Empty the buffer of any pending data.
  95. // I don't care what you've been pressing until now!
  96. while (IO::in8(I8042_STATUS ) & DATA_AVAILABLE)
  97. IO::in8(I8042_BUFFER);
  98. enableIRQ();
  99. }
  100. Keyboard::~Keyboard()
  101. {
  102. ASSERT_NOT_REACHED();
  103. }
  104. bool Keyboard::hasDataAvailableForRead() const
  105. {
  106. return !m_queue.isEmpty();
  107. }
  108. ssize_t Keyboard::read(byte* buffer, size_t size)
  109. {
  110. ssize_t nread = 0;
  111. while (nread < size) {
  112. if (m_queue.isEmpty())
  113. break;
  114. buffer[nread++] = m_queue.dequeue();
  115. }
  116. return nread;
  117. }
  118. ssize_t Keyboard::write(const byte* data, size_t size)
  119. {
  120. return 0;
  121. }
  122. KeyboardClient::~KeyboardClient()
  123. {
  124. }