summaryrefslogtreecommitdiff
path: root/src/kernel/dev/ps2keyboard.cpp
blob: c7fc32203d90d8f5ad0d293c16f2be3854d0ebad (plain) (blame)
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
#include "ps2keyboard.h"

#include <core/sys.h>
#include <task/idt.h>

ps2kbd *kbd = 0;

static void kbd_irq(registers*) {
	kbd->kbdIrq();
}

ps2kbd::ps2kbd(node* parent) : keyboard(parent) {
	uint8_t temp = inb(0x60), temp2 = 0;
	while (temp != temp2) {
		temp2 = temp;
		temp = inb(0x60);
	}

	kbd = this;
	idt_handleIrq(1, kbd_irq);

	escaped = false;

	updateLeds();
}

void ps2kbd::updateLeds() {
	uint8_t tmp = 0;
	if (scroll) tmp |= 1;
	if (num) tmp |= 2;
	if (caps) tmp |= 4;
	outb(0x60, tmp);
}

void ps2kbd::kbdIrq() {
	uint8_t scancode = inb(0x60);
	if (scancode == 0xE0) {
		escaped = true;
	} else {
		if (scancode & 0x80) {
			if (escaped) {
				handle(scancode, false);
			} else {
				handle(scancode & 0x7F, false);
			}
		} else {
			if (escaped) {
				handle(scancode | 0x80, true);
			} else {
				handle(scancode, true);
			}
		}
		escaped = false;
	}
}