Keyboard.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <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::handleIRQ()
  34. {
  35. while (IO::in8(0x64) & 1) {
  36. BYTE ch = IO::in8(0x60);
  37. switch (ch) {
  38. case 0x38: m_modifiers |= MOD_ALT; break;
  39. case 0xB8: m_modifiers &= ~MOD_ALT; break;
  40. case 0x1D: m_modifiers |= MOD_CTRL; break;
  41. case 0x9D: m_modifiers &= ~MOD_CTRL; break;
  42. case 0x2A: m_modifiers |= MOD_SHIFT; break;
  43. case 0xAA: m_modifiers &= ~MOD_SHIFT; break;
  44. case 0x1C: /* enter */ m_queue.enqueue('\n'); break;
  45. case 0xFA: /* i8042 ack */ break;
  46. default:
  47. if (ch & 0x80) {
  48. // key has been depressed
  49. break;
  50. }
  51. if (!m_modifiers)
  52. m_queue.enqueue(map[ch]);
  53. else if (m_modifiers & MOD_SHIFT)
  54. m_queue.enqueue(shift_map[ch]);
  55. else if (m_modifiers & MOD_CTRL) {
  56. // FIXME: This is obviously not a good enough way to process ctrl+whatever.
  57. m_queue.enqueue('^');
  58. m_queue.enqueue(shift_map[ch]);
  59. }
  60. }
  61. //break;
  62. }
  63. }
  64. Keyboard::Keyboard()
  65. : IRQHandler(IRQ_KEYBOARD)
  66. {
  67. // Empty the buffer of any pending data.
  68. // I don't care what you've been pressing until now!
  69. while (IO::in8(I8042_STATUS ) & DATA_AVAILABLE)
  70. IO::in8(I8042_BUFFER);
  71. enableIRQ();
  72. }
  73. Keyboard::~Keyboard()
  74. {
  75. ASSERT_NOT_REACHED();
  76. }
  77. bool Keyboard::hasDataAvailableForRead() const
  78. {
  79. return !m_queue.isEmpty();
  80. }
  81. ssize_t Keyboard::read(byte* buffer, size_t size)
  82. {
  83. ssize_t nread = 0;
  84. while (nread < size) {
  85. if (m_queue.isEmpty())
  86. break;
  87. buffer[nread++] = m_queue.dequeue();
  88. }
  89. return nread;
  90. }
  91. ssize_t Keyboard::write(const byte* data, size_t size)
  92. {
  93. return 0;
  94. }