-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.c
410 lines (359 loc) · 9.24 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*************************************************************************//**
*****************************************************************************
* @file keyboard.c
* @brief
* @author Forrest Y. Yu
* @date 2005
*
*
*
*****************************************************************************
*****************************************************************************/
#include "type.h"
#include "stdio.h"
#include "const.h"
#include "protect.h"
#include "string.h"
#include "fs.h"
#include "proc.h"
#include "tty.h"
#include "console.h"
#include "global.h"
#include "keyboard.h"
#include "keymap.h"
#include "proto.h"
PRIVATE struct kb_inbuf kb_in;
PRIVATE int code_with_E0;
PRIVATE int shift_l; /* l shift state */
PRIVATE int shift_r; /* r shift state */
PRIVATE int alt_l; /* l alt state */
PRIVATE int alt_r; /* r left state */
PRIVATE int ctrl_l; /* l ctrl state */
PRIVATE int ctrl_r; /* l ctrl state */
PRIVATE int caps_lock; /* Caps Lock */
PRIVATE int num_lock; /* Num Lock */
PRIVATE int scroll_lock; /* Scroll Lock */
PRIVATE int column;
PRIVATE u8 get_byte_from_kb_buf();
PRIVATE void set_leds();
PRIVATE void kb_wait();
PRIVATE void kb_ack();
/*****************************************************************************
* keyboard_handler
*****************************************************************************/
/**
* <Ring 0> Handles the interrupts generated by the keyboard controller.
*
* @param irq The IRQ corresponding to the keyboard, unused here.
*****************************************************************************/
PUBLIC void keyboard_handler(int irq)
{
u8 scan_code = in_byte(KB_DATA);
if (kb_in.count < KB_IN_BYTES) {
*(kb_in.p_head) = scan_code;
kb_in.p_head++;
if (kb_in.p_head == kb_in.buf + KB_IN_BYTES)
kb_in.p_head = kb_in.buf;
kb_in.count++;
}
key_pressed = 1;
}
/*****************************************************************************
* init_keyboard
*****************************************************************************/
/**
* <Ring 1> Initialize some variables and set keyboard interrupt handler.
*
*****************************************************************************/
PUBLIC void init_keyboard()
{
kb_in.count = 0;
kb_in.p_head = kb_in.p_tail = kb_in.buf;
shift_l = shift_r = 0;
alt_l = alt_r = 0;
ctrl_l = ctrl_r = 0;
caps_lock = 0;
num_lock = 1;
scroll_lock = 0;
column = 0;
set_leds();
put_irq_handler(KEYBOARD_IRQ, keyboard_handler);
enable_irq(KEYBOARD_IRQ);
}
/*****************************************************************************
* keyboard_read
*****************************************************************************/
/**
* Yes, it is an ugly function.
*
* @param tty Which TTY is reading the keyboard input.
*****************************************************************************/
PUBLIC void keyboard_read(TTY* tty)
{
u8 scan_code;
/**
* 1 : make
* 0 : break
*/
int make;
/**
* We use a integer to record a key press.
* For instance, if the key HOME is pressed, key will be evaluated to
* `HOME' defined in keyboard.h.
*/
u32 key = 0;
/**
* This var points to a row in keymap[]. I don't use two-dimension
* array because I don't like it.
*/
u32* keyrow;
while (kb_in.count > 0) {
code_with_E0 = 0;
scan_code = get_byte_from_kb_buf();
/* parse the scan code below */
if (scan_code == 0xE1) {
int i;
u8 pausebreak_scan_code[] = {0xE1, 0x1D, 0x45, 0xE1, 0x9D, 0xC5};
int is_pausebreak = 1;
for (i = 1; i < 6; i++) {
if (get_byte_from_kb_buf() != pausebreak_scan_code[i]) {
is_pausebreak = 0;
break;
}
}
if (is_pausebreak) {
key = PAUSEBREAK;
}
}
else if (scan_code == 0xE0) {
code_with_E0 = 1;
scan_code = get_byte_from_kb_buf();
/* PrintScreen is pressed */
if (scan_code == 0x2A) {
code_with_E0 = 0;
if ((scan_code = get_byte_from_kb_buf()) == 0xE0) {
code_with_E0 = 1;
if ((scan_code = get_byte_from_kb_buf()) == 0x37) {
key = PRINTSCREEN;
make = 1;
}
}
}
/* PrintScreen is released */
else if (scan_code == 0xB7) {
code_with_E0 = 0;
if ((scan_code = get_byte_from_kb_buf()) == 0xE0) {
code_with_E0 = 1;
if ((scan_code = get_byte_from_kb_buf()) == 0xAA) {
key = PRINTSCREEN;
make = 0;
}
}
}
}
if ((key != PAUSEBREAK) && (key != PRINTSCREEN)) {
int caps;
/* make or break */
make = (scan_code & FLAG_BREAK ? 0 : 1);
keyrow = &keymap[(scan_code & 0x7F) * MAP_COLS];
column = 0;
caps = shift_l || shift_r;
if (caps_lock &&
keyrow[0] >= 'a' && keyrow[0] <= 'z')
caps = !caps;
if (caps)
column = 1;
if (code_with_E0)
column = 2;
key = keyrow[column];
switch(key) {
case SHIFT_L:
shift_l = make;
break;
case SHIFT_R:
shift_r = make;
break;
case CTRL_L:
ctrl_l = make;
break;
case CTRL_R:
ctrl_r = make;
break;
case ALT_L:
alt_l = make;
break;
case ALT_R:
alt_l = make;
break;
case CAPS_LOCK:
if (make) {
caps_lock = !caps_lock;
set_leds();
}
break;
case NUM_LOCK:
if (make) {
num_lock = !num_lock;
set_leds();
}
break;
case SCROLL_LOCK:
if (make) {
scroll_lock = !scroll_lock;
set_leds();
}
break;
default:
break;
}
}
if(make){ /* Break Code is ignored */
int pad = 0;
/* deal with the numpad first */
if ((key >= PAD_SLASH) && (key <= PAD_9)) {
pad = 1;
switch(key) { /* '/', '*', '-', '+',
* and 'Enter' in num pad
*/
case PAD_SLASH:
key = '/';
break;
case PAD_STAR:
key = '*';
break;
case PAD_MINUS:
key = '-';
break;
case PAD_PLUS:
key = '+';
break;
case PAD_ENTER:
key = ENTER;
break;
default:
/* the value of these keys
* depends on the Numlock
*/
if (num_lock) { /* '0' ~ '9' and '.' in num pad */
if (key >= PAD_0 && key <= PAD_9)
key = key - PAD_0 + '0';
else if (key == PAD_DOT)
key = '.';
}
else{
switch(key) {
case PAD_HOME:
key = HOME;
break;
case PAD_END:
key = END;
break;
case PAD_PAGEUP:
key = PAGEUP;
break;
case PAD_PAGEDOWN:
key = PAGEDOWN;
break;
case PAD_INS:
key = INSERT;
break;
case PAD_UP:
key = UP;
break;
case PAD_DOWN:
key = DOWN;
break;
case PAD_LEFT:
key = LEFT;
break;
case PAD_RIGHT:
key = RIGHT;
break;
case PAD_DOT:
key = DELETE;
break;
default:
break;
}
}
break;
}
}
key |= shift_l ? FLAG_SHIFT_L : 0;
key |= shift_r ? FLAG_SHIFT_R : 0;
key |= ctrl_l ? FLAG_CTRL_L : 0;
key |= ctrl_r ? FLAG_CTRL_R : 0;
key |= alt_l ? FLAG_ALT_L : 0;
key |= alt_r ? FLAG_ALT_R : 0;
key |= pad ? FLAG_PAD : 0;
in_process(tty, key);
}
}
}
/*****************************************************************************
* get_byte_from_kb_buf
*****************************************************************************/
/**
* Read a byte from the keyboard buffer.
*
* @return The byte read.
*****************************************************************************/
PRIVATE u8 get_byte_from_kb_buf()
{
u8 scan_code;
while (kb_in.count <= 0) {} /* wait for a byte to arrive */
disable_int(); /* for synchronization */
scan_code = *(kb_in.p_tail);
kb_in.p_tail++;
if (kb_in.p_tail == kb_in.buf + KB_IN_BYTES) {
kb_in.p_tail = kb_in.buf;
}
kb_in.count--;
enable_int(); /* for synchronization */
return scan_code;
}
/*****************************************************************************
* kb_wait
*****************************************************************************/
/**
* Wait until the input buffer of 8042 is empty.
*
*****************************************************************************/
PRIVATE void kb_wait() /* 等待 8042 的输入缓冲区空 */
{
u8 kb_stat;
do {
kb_stat = in_byte(KB_CMD);
} while (kb_stat & 0x02);
}
/*****************************************************************************
* kb_ack
*****************************************************************************/
/**
* Read from the keyboard controller until a KB_ACK is received.
*
*****************************************************************************/
PRIVATE void kb_ack()
{
u8 kb_read;
do {
kb_read = in_byte(KB_DATA);
} while (kb_read != KB_ACK);
}
/*****************************************************************************
* set_leds
*****************************************************************************/
/**
* Set the leds according to: caps_lock, num_lock & scroll_lock.
*
*****************************************************************************/
PRIVATE void set_leds()
{
u8 leds = (caps_lock << 2) | (num_lock << 1) | scroll_lock;
kb_wait();
out_byte(KB_DATA, LED_CODE);
kb_ack();
kb_wait();
out_byte(KB_DATA, leds);
kb_ack();
}