-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathkeyboard.c
169 lines (159 loc) · 2.05 KB
/
keyboard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Implementation for keyboard
// When key is pressed, keyboard controller triggers IRQ1
// http://wiki.osdev.org/Interrupts#From_the_keyboard.27s_perspective
// We need to handle this interrupt and do something
#include "keyboard.h"
#include "screen.h"
#include "../cpu/ports.h"
#include "../cpu/isr.h"
#include "../libc/function.h"
#include "../libc/string.h"
#include "../kernel/kernel.h"
#define BACKSPACE 0x0E
#define ENTER 0x1C
static char key_buffer[256];
#define SC_MAX 57
const char *sc_name[] = {
"ERROR",
"Esc",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"-",
"=",
"Backspace",
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"[",
"]",
"Enter",
"Lctrl",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
";",
"'",
"`",
"LShift",
"\\",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
",",
".",
"/",
"RShift",
"Keypad *",
"LAlt",
"Spacebar"
};
const char sc_ascii[] = {
'?',
'?',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'0',
'-',
'=',
'?',
'?',
'Q',
'W',
'E',
'R',
'T',
'Y',
'U',
'I',
'O',
'P',
'[',
']',
'?',
'?',
'A',
'S',
'D',
'F',
'G',
'H',
'J',
'K',
'L',
';',
'\'',
'`',
'?',
'\\',
'Z',
'X',
'C',
'V',
'B',
'N',
'M',
',',
'.',
'/',
'?',
'?',
'?',
' '
};
// Calls each time when key is pressed
static void keyboard_callback(registers_t regs) {
// The PIC leaves us the scancode in port 0x60
uint8_t scancode = port_byte_in(0x60);
if (scancode > SC_MAX) return;
if (scancode == BACKSPACE) {
backspace(key_buffer);
print_backspace();
} else if (scancode == ENTER) {
print("\n");
user_input(key_buffer);
key_buffer[0] = '\0';
} else {
char letter = sc_ascii[(int)scancode];
char str[2] = {letter, '\0'};
append(key_buffer, letter);
print(str);
}
UNUSED(regs);
}
// Maps keyboard_callback() to IRQ1 interrupt
void init_keyboard() {
register_interrupt_handler(IRQ1, keyboard_callback);
}