2019-04-06 15:29:29 +03:00
|
|
|
#include <AK/Types.h>
|
2018-10-16 12:01:38 +03:00
|
|
|
#include "i386.h"
|
|
|
|
#include "IO.h"
|
|
|
|
#include "PIC.h"
|
2019-04-03 13:36:40 +03:00
|
|
|
#include <Kernel/Devices/KeyboardDevice.h>
|
2019-04-03 13:28:45 +03:00
|
|
|
#include <Kernel/TTY/VirtualConsole.h>
|
2018-10-22 13:58:29 +03:00
|
|
|
#include <AK/Assertions.h>
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2019-01-23 10:30:48 +03:00
|
|
|
//#define KEYBOARD_DEBUG
|
|
|
|
|
2018-10-16 12:01:38 +03:00
|
|
|
#define IRQ_KEYBOARD 1
|
|
|
|
#define I8042_BUFFER 0x60
|
|
|
|
#define I8042_STATUS 0x64
|
|
|
|
#define I8042_ACK 0xFA
|
2019-02-07 09:55:53 +03:00
|
|
|
#define I8042_BUFFER_FULL 0x01
|
|
|
|
#define I8042_WHICH_BUFFER 0x20
|
|
|
|
#define I8042_MOUSE_BUFFER 0x20
|
|
|
|
#define I8042_KEYBOARD_BUFFER 0x00
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2019-01-21 09:05:31 +03:00
|
|
|
static char map[0x80] =
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2019-03-03 15:34:29 +03:00
|
|
|
0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08, '\t',
|
2019-01-21 09:05:31 +03:00
|
|
|
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0,
|
2018-10-16 12:01:38 +03:00
|
|
|
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\',
|
|
|
|
'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
|
2018-10-25 13:10:25 +03:00
|
|
|
0, 0, 0, ' '
|
2018-10-16 12:01:38 +03:00
|
|
|
};
|
|
|
|
|
2019-01-21 09:05:31 +03:00
|
|
|
static char shift_map[0x80] =
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2019-03-03 15:34:29 +03:00
|
|
|
0, '\033', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0x08, '\t',
|
2019-01-21 09:05:31 +03:00
|
|
|
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0,
|
2018-10-16 12:01:38 +03:00
|
|
|
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0, '|',
|
|
|
|
'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
|
2018-10-25 13:10:25 +03:00
|
|
|
0, 0, 0, ' '
|
2018-10-16 12:01:38 +03:00
|
|
|
};
|
|
|
|
|
2019-01-21 09:05:31 +03:00
|
|
|
static KeyCode unshifted_key_map[0x80] =
|
2018-10-30 17:33:37 +03:00
|
|
|
{
|
2019-01-23 10:30:48 +03:00
|
|
|
Key_Invalid, Key_Escape,
|
2019-01-21 09:05:31 +03:00
|
|
|
Key_1, Key_2, Key_3, Key_4, Key_5, Key_6, Key_7, Key_8, Key_9, Key_0, Key_Minus, Key_Equal, Key_Backspace,
|
2019-03-03 15:34:29 +03:00
|
|
|
Key_Tab, //15
|
2019-01-21 09:05:31 +03:00
|
|
|
Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_LeftBracket, Key_RightBracket,
|
|
|
|
Key_Return, // 28
|
|
|
|
Key_Control, // 29
|
|
|
|
Key_A, Key_S, Key_D, Key_F, Key_G, Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Apostrophe, Key_Backtick,
|
2019-04-23 21:47:45 +03:00
|
|
|
Key_LeftShift, // 42
|
2019-01-21 09:05:31 +03:00
|
|
|
Key_Backslash,
|
|
|
|
Key_Z, Key_X, Key_C, Key_V, Key_B, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash,
|
2019-04-23 21:47:45 +03:00
|
|
|
Key_RightShift, // 54
|
|
|
|
Key_Invalid,
|
|
|
|
Key_Alt, // 56
|
2019-01-30 23:18:13 +03:00
|
|
|
Key_Space, // 57
|
2019-03-03 04:52:22 +03:00
|
|
|
Key_Invalid, // 58
|
|
|
|
Key_F1, Key_F2, Key_F3, Key_F4, Key_F5, Key_F6, Key_F7, Key_F8, Key_F9, Key_F10,
|
|
|
|
Key_Invalid,
|
2019-01-30 23:18:13 +03:00
|
|
|
Key_Invalid, // 70
|
2019-03-07 15:19:05 +03:00
|
|
|
Key_Home,
|
2019-01-30 23:18:13 +03:00
|
|
|
Key_Up,
|
2019-03-03 01:58:09 +03:00
|
|
|
Key_PageUp,
|
2019-01-30 23:18:13 +03:00
|
|
|
Key_Invalid,
|
|
|
|
Key_Left,
|
|
|
|
Key_Invalid,
|
|
|
|
Key_Right, // 77
|
|
|
|
Key_Invalid,
|
2019-03-07 15:19:05 +03:00
|
|
|
Key_End,
|
2019-01-30 23:18:13 +03:00
|
|
|
Key_Down, // 80
|
2019-03-03 01:58:09 +03:00
|
|
|
Key_PageDown,
|
2019-03-03 04:52:22 +03:00
|
|
|
Key_Invalid,
|
2019-03-07 18:20:00 +03:00
|
|
|
Key_Delete, // 83
|
2019-03-03 04:52:22 +03:00
|
|
|
Key_Invalid,
|
|
|
|
Key_Invalid,
|
|
|
|
Key_Invalid,
|
|
|
|
Key_F11,
|
|
|
|
Key_F12,
|
2019-03-03 14:56:48 +03:00
|
|
|
Key_Invalid,
|
|
|
|
Key_Invalid,
|
|
|
|
Key_Logo,
|
2019-01-21 09:05:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static KeyCode shifted_key_map[0x100] =
|
|
|
|
{
|
2019-01-23 10:30:48 +03:00
|
|
|
Key_Invalid, Key_Escape,
|
2019-01-21 09:05:31 +03:00
|
|
|
Key_ExclamationPoint, Key_AtSign, Key_Hashtag, Key_Dollar, Key_Percent, Key_Circumflex, Key_Ampersand, Key_Asterisk, Key_LeftParen, Key_RightParen, Key_Underscore, Key_Plus, Key_Backspace,
|
2019-03-03 15:34:29 +03:00
|
|
|
Key_Tab,
|
2019-01-21 09:05:31 +03:00
|
|
|
Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_LeftBrace, Key_RightBrace,
|
|
|
|
Key_Return,
|
|
|
|
Key_Control,
|
|
|
|
Key_A, Key_S, Key_D, Key_F, Key_G, Key_H, Key_J, Key_K, Key_L, Key_Colon, Key_DoubleQuote, Key_Tilde,
|
2019-04-23 21:47:45 +03:00
|
|
|
Key_LeftShift, // 42
|
2019-01-21 09:05:31 +03:00
|
|
|
Key_Pipe,
|
|
|
|
Key_Z, Key_X, Key_C, Key_V, Key_B, Key_N, Key_M, Key_LessThan, Key_GreaterThan, Key_QuestionMark,
|
2019-04-23 21:47:45 +03:00
|
|
|
Key_RightShift, // 54
|
|
|
|
Key_Invalid,
|
2019-01-21 09:05:31 +03:00
|
|
|
Key_Alt,
|
2019-03-03 04:52:22 +03:00
|
|
|
Key_Space, // 57
|
|
|
|
Key_Invalid, // 58
|
|
|
|
Key_F1, Key_F2, Key_F3, Key_F4, Key_F5, Key_F6, Key_F7, Key_F8, Key_F9, Key_F10,
|
|
|
|
Key_Invalid,
|
2019-01-30 23:18:13 +03:00
|
|
|
Key_Invalid, // 70
|
2019-03-07 15:19:05 +03:00
|
|
|
Key_Home,
|
2019-01-30 23:18:13 +03:00
|
|
|
Key_Up,
|
2019-03-03 01:58:09 +03:00
|
|
|
Key_PageUp,
|
2019-01-30 23:18:13 +03:00
|
|
|
Key_Invalid,
|
|
|
|
Key_Left,
|
|
|
|
Key_Invalid,
|
|
|
|
Key_Right, // 77
|
|
|
|
Key_Invalid,
|
2019-03-07 15:19:05 +03:00
|
|
|
Key_End,
|
2019-01-30 23:18:13 +03:00
|
|
|
Key_Down, // 80
|
2019-03-03 01:58:09 +03:00
|
|
|
Key_PageDown,
|
2019-03-03 04:52:22 +03:00
|
|
|
Key_Invalid,
|
2019-03-07 18:20:00 +03:00
|
|
|
Key_Delete, // 83
|
2019-03-03 04:52:22 +03:00
|
|
|
Key_Invalid,
|
|
|
|
Key_Invalid,
|
|
|
|
Key_Invalid,
|
|
|
|
Key_F11,
|
|
|
|
Key_F12,
|
2019-03-03 14:56:48 +03:00
|
|
|
Key_Invalid,
|
|
|
|
Key_Invalid,
|
|
|
|
Key_Logo,
|
2019-01-21 09:05:31 +03:00
|
|
|
};
|
|
|
|
|
2019-02-17 10:39:09 +03:00
|
|
|
void KeyboardDevice::key_state_changed(byte raw, bool pressed)
|
2019-01-21 09:05:31 +03:00
|
|
|
{
|
|
|
|
Event event;
|
|
|
|
event.key = (m_modifiers & Mod_Shift) ? shifted_key_map[raw] : unshifted_key_map[raw];
|
|
|
|
event.character = (m_modifiers & Mod_Shift) ? shift_map[raw] : map[raw];
|
|
|
|
event.flags = m_modifiers;
|
|
|
|
if (pressed)
|
|
|
|
event.flags |= Is_Press;
|
2018-10-30 17:33:37 +03:00
|
|
|
if (m_client)
|
2019-01-21 09:05:31 +03:00
|
|
|
m_client->on_key_pressed(event);
|
|
|
|
m_queue.enqueue(event);
|
2018-10-30 17:33:37 +03:00
|
|
|
}
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2019-02-17 10:39:09 +03:00
|
|
|
void KeyboardDevice::handle_irq()
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2019-02-07 09:55:53 +03:00
|
|
|
for (;;) {
|
|
|
|
byte status = IO::in8(I8042_STATUS);
|
|
|
|
if (!(((status & I8042_WHICH_BUFFER) == I8042_KEYBOARD_BUFFER) && (status & I8042_BUFFER_FULL)))
|
|
|
|
return;
|
2019-01-21 09:30:23 +03:00
|
|
|
byte raw = IO::in8(I8042_BUFFER);
|
2019-01-21 09:05:31 +03:00
|
|
|
byte ch = raw & 0x7f;
|
|
|
|
bool pressed = !(raw & 0x80);
|
|
|
|
|
2019-01-23 10:30:48 +03:00
|
|
|
#ifdef KEYBOARD_DEBUG
|
|
|
|
dbgprintf("Keyboard::handle_irq: %b %s\n", ch, pressed ? "down" : "up");
|
|
|
|
#endif
|
2018-10-16 12:01:38 +03:00
|
|
|
switch (ch) {
|
2019-04-23 21:47:45 +03:00
|
|
|
case 0x38:
|
|
|
|
update_modifier(Mod_Alt, pressed);
|
|
|
|
break;
|
|
|
|
case 0x1d:
|
|
|
|
update_modifier(Mod_Ctrl, pressed);
|
|
|
|
break;
|
|
|
|
case 0x5b:
|
|
|
|
update_modifier(Mod_Logo, pressed);
|
|
|
|
break;
|
|
|
|
case 0x2a:
|
|
|
|
case 0x36:
|
|
|
|
update_modifier(Mod_Shift, pressed);
|
|
|
|
break;
|
2019-03-03 14:56:48 +03:00
|
|
|
}
|
|
|
|
switch (ch) {
|
2019-01-21 09:30:23 +03:00
|
|
|
case I8042_ACK: break;
|
2018-10-16 12:01:38 +03:00
|
|
|
default:
|
2019-01-21 09:30:23 +03:00
|
|
|
if (m_modifiers & Mod_Alt) {
|
2018-10-30 15:59:29 +03:00
|
|
|
switch (map[ch]) {
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
2018-10-31 02:27:34 +03:00
|
|
|
case '4':
|
2018-11-01 16:09:21 +03:00
|
|
|
VirtualConsole::switch_to(map[ch] - '0' - 1);
|
2018-10-30 15:59:29 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-21 09:05:31 +03:00
|
|
|
key_state_changed(ch, pressed);
|
|
|
|
}
|
2018-10-16 12:01:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 10:39:09 +03:00
|
|
|
static KeyboardDevice* s_the;
|
2018-10-30 17:33:37 +03:00
|
|
|
|
2019-02-17 10:39:09 +03:00
|
|
|
KeyboardDevice& KeyboardDevice::the()
|
2018-10-30 17:33:37 +03:00
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2019-02-17 10:39:09 +03:00
|
|
|
KeyboardDevice::KeyboardDevice()
|
2018-10-22 13:58:29 +03:00
|
|
|
: IRQHandler(IRQ_KEYBOARD)
|
2018-10-30 15:59:29 +03:00
|
|
|
, CharacterDevice(85, 1)
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
2018-10-30 17:33:37 +03:00
|
|
|
s_the = this;
|
|
|
|
|
2018-10-16 12:01:38 +03:00
|
|
|
// Empty the buffer of any pending data.
|
|
|
|
// I don't care what you've been pressing until now!
|
2019-02-07 09:55:53 +03:00
|
|
|
while (IO::in8(I8042_STATUS) & I8042_BUFFER_FULL)
|
2018-10-16 12:01:38 +03:00
|
|
|
IO::in8(I8042_BUFFER);
|
|
|
|
|
2018-12-03 02:39:25 +03:00
|
|
|
enable_irq();
|
2018-10-16 12:01:38 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 10:39:09 +03:00
|
|
|
KeyboardDevice::~KeyboardDevice()
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-17 10:39:09 +03:00
|
|
|
bool KeyboardDevice::can_read(Process&) const
|
2018-10-25 14:07:59 +03:00
|
|
|
{
|
2018-12-03 02:39:25 +03:00
|
|
|
return !m_queue.is_empty();
|
2018-10-25 14:07:59 +03:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:19:57 +03:00
|
|
|
ssize_t KeyboardDevice::read(Process&, byte* buffer, ssize_t size)
|
2018-10-23 11:12:50 +03:00
|
|
|
{
|
|
|
|
ssize_t nread = 0;
|
2019-02-25 23:19:57 +03:00
|
|
|
while (nread < size) {
|
2018-12-03 02:39:25 +03:00
|
|
|
if (m_queue.is_empty())
|
2018-10-23 11:12:50 +03:00
|
|
|
break;
|
2019-01-16 19:20:58 +03:00
|
|
|
// Don't return partial data frames.
|
2019-02-25 23:19:57 +03:00
|
|
|
if ((size - nread) < (ssize_t)sizeof(Event))
|
2019-01-16 19:20:58 +03:00
|
|
|
break;
|
2019-01-21 09:05:31 +03:00
|
|
|
auto event = m_queue.dequeue();
|
|
|
|
memcpy(buffer, &event, sizeof(Event));
|
|
|
|
nread += sizeof(Event);
|
2018-10-23 11:12:50 +03:00
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:19:57 +03:00
|
|
|
ssize_t KeyboardDevice::write(Process&, const byte*, ssize_t)
|
2018-10-23 11:12:50 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-30 15:59:29 +03:00
|
|
|
|
|
|
|
KeyboardClient::~KeyboardClient()
|
|
|
|
{
|
|
|
|
}
|