* same with xv6
[mascara-docs.git] / i386 / MIT / src.xv6 / kbd.c
blob32c1463fe3248ae521bbb45a2ab529f10e4f3e18
1 #include "types.h"
2 #include "x86.h"
3 #include "defs.h"
4 #include "kbd.h"
6 int
7 kbdgetc(void)
9 static uint shift;
10 static uchar *charcode[4] = {
11 normalmap, shiftmap, ctlmap, ctlmap
13 uint st, data, c;
15 st = inb(KBSTATP);
16 if((st & KBS_DIB) == 0)
17 return -1;
18 data = inb(KBDATAP);
20 if(data == 0xE0){
21 shift |= E0ESC;
22 return 0;
23 } else if(data & 0x80){
24 // Key released
25 data = (shift & E0ESC ? data : data & 0x7F);
26 shift &= ~(shiftcode[data] | E0ESC);
27 return 0;
28 } else if(shift & E0ESC){
29 // Last character was an E0 escape; or with 0x80
30 data |= 0x80;
31 shift &= ~E0ESC;
34 shift |= shiftcode[data];
35 shift ^= togglecode[data];
36 c = charcode[shift & (CTL | SHIFT)][data];
37 if(shift & CAPSLOCK){
38 if('a' <= c && c <= 'z')
39 c += 'A' - 'a';
40 else if('A' <= c && c <= 'Z')
41 c += 'a' - 'A';
43 return c;
46 void
47 kbdintr(void)
49 consoleintr(kbdgetc);