/
kbd.c
executable file
·71 lines (61 loc) · 1.29 KB
/
kbd.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
#include "types.h"
#include "x86.h"
#include "defs.h"
#include "kbd.h"
#include "msg.h"
#include "spinlock.h"
static struct spinlock kbdlock;
int
kbdgetc(void)
{
static uint shift;
static uchar *charcode[4] = {
normalmap, shiftmap, ctlmap, ctlmap
};
uint st, data, c;
st = inb(KBSTATP);
if((st & KBS_DIB) == 0)
return -1;
data = inb(KBDATAP);
message msg;
if(data == 0xE0){
shift |= E0ESC;
return 0;
} else if(data & 0x80){
// Key released
data = (shift & E0ESC ? data : data & 0x7F);
shift &= ~(shiftcode[data] | E0ESC);
c = normalmap[data];
msg.msg_type = M_KEY_UP;
msg.params[0] = c;
msg.params[1] = shift;
handleMessage(&msg);
return 0;
} else if(shift & E0ESC){
// Last character was an E0 escape; or with 0x80
data |= 0x80;
shift &= ~E0ESC;
}
shift |= shiftcode[data];
shift ^= togglecode[data];
msg.msg_type = M_KEY_DOWN;
msg.params[0] = normalmap[data];
msg.params[1] = shift;
handleMessage(&msg);
c = charcode[shift & (CTL | SHIFT)][data];
if(shift & CAPSLOCK){
if('a' <= c && c <= 'z')
c += 'A' - 'a';
else if('A' <= c && c <= 'Z')
c += 'a' - 'A';
}
return c;
}
void
kbdintr(void)
{
// consoleintr(kbdgetc);
acquire(&kbdlock);
kbdgetc();
release(&kbdlock);
}