This repository has been archived by the owner on Mar 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
kb.c
68 lines (59 loc) · 1.63 KB
/
kb.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
#include <system.h>
#include <irq.h>
#include <kb.h>
/* Keyboard constants */
#define KB_SHIFT 0x11
#define KB_ALT 0x12
#define KB_CTRL 0x13
#define KBD_LAYOUT kbd_us
/* Include keyboard layouts here */
#include <kbd/us.h>
/* Global indicator as to the status of
* various keyboard controls */
unsigned short keystatus = 0x0000;
/* Handles the keyboard interrupt */
void keyboard_handler(struct regs* r)
{
unsigned char scancode;
/* Read from the keyboard's data buffer */
scancode = inportb(0x60);
/* If the top bit of the byte we read from the keyboard is
* set, that means that a key has just been released */
if (scancode & 0x80)
{
/* You can use this one to see if the user released
* shift, alt or control keys... */
if (KBD_LAYOUT[scancode] == KB_SHIFT)
keystatus &= ~0x0100;
else if (KBD_LAYOUT[scancode] == KB_CTRL)
keystatus &= ~0x0010;
else if (KBD_LAYOUT[scancode] == KB_ALT)
keystatus &= ~0x0001;
}
else
{
/* Here, a key was just pressed. Please note that if
* you hold a key down, you will get repeated key press
* interrupts. */
if (KBD_LAYOUT[scancode] == KB_SHIFT)
keystatus |= 0x0100;
else if (KBD_LAYOUT[scancode] == KB_CTRL)
keystatus |= 0x0010;
else if (KBD_LAYOUT[scancode] == KB_ALT)
keystatus |= 0x0001;
/* Example showing keyboard characters outputted to the
* screen */
if ((keystatus & 0x0100) == 0x0100)
putch(KBD_LAYOUT[scancode+128]);
else
putch(KBD_LAYOUT[scancode]);
}
}
#undef KBD_LAYOUT
/* Sets up the keyboard by installing the handler
* into IRQ1 */
void kb_install()
{
/* Installs the IRQ handler */
irq_install_handler(1, keyboard_handler);
}