summaryrefslogtreecommitdiff
path: root/kernel/src/devices/keyboard.c
blob: d4b06b929964a644c47b1ef6709615a6dbcfde27 (plain)
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
#include <stdint.h>
#include <stdbool.h>

#include <pic.h>
#include <io.h>
#include <graphics.h>
#include <debug.h>
#include <keymap.h>

#define KEYBOARD_DATA_PORT	0x60
#define KEYBOARD_STATUS_PORT	0x64

static uint32_t x;
static uint32_t y;
bool is_pressed[128];

uint32_t strlen(const char *s);

uint32_t stoi(const char *s)
{
	static uint32_t num;
	uint32_t s_len = strlen(s);

	for (int i = s_len - 1; i >= 0; i--) {
		num += s[i] - '0';
	}

	return num;
}

void itos(uint32_t num, char** s)
{
	uint32_t i;
	for (i = 0; num; num/=10, i++) {
		*s[i] = num%10 - '0';
	}
	*s[i] = '\0';
}

void print_char(char c)
{

	if (c == '\n') {
		x = 0;
		y++;
		return;
	}
	if (x * 8 >= fb.width) {
		x = 0;
		y++;
	}
	if (y * 16 >= fb.height) {
		x = 0;
		y = 0;
	}
	fb_draw_character(fb, c, x * 8, y * 16, WHITE, BLACK);
	x++;
}

void print_string(char* s)
{
	for (uint32_t i = 0; i < strlen(s); i++) {
		print_char(s[i]);
	}
}

void keyboard_handler(void)
{
	uint8_t status = inb(KEYBOARD_STATUS_PORT);
	if (!(status & 0x1)) {
		return;
	}
	uint8_t keycode = inb(KEYBOARD_DATA_PORT);
	if (keycode < keymap_len) {
		is_pressed[keycode] = true;
		if (keycode == KEY_SPACE) {
			print_char(' ');

		} else if (keycode == KEY_BACKSPACE) {
			if (!x) return;
			x--;
			print_char(' ');
			x--;
		} else if (keycode == KEY_ENTER) {
			print_char(keymap[keycode]);
		} else {
			if (keymap[keycode] == ' ') return;
			if (is_pressed[KEY_LSHIFT] || is_pressed[KEY_RSHIFT])
				print_char(shift_keymap[keycode]);
			else
				print_char(keymap[keycode]);
		}
	} else {
		is_pressed[keycode - 128] = false;
	}
}