summaryrefslogtreecommitdiff
path: root/kernel/src/devices/keyboard.c
blob: 0a2e36403a8de82f778b3e41403580161a2f7aab (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
#include <types.h>
#include <keyboard.h>
#include <pic.h>
#include <io.h>
#include <keymap.h>
#include <graphics.h>
#include <libk/stdio.h>
#include <libk/math.h>
#include <libk/string.h>

bool is_pressed[128];

#include <stdbuff.h>
#include <heap.h>
#define BUFFER_SIZE 10000
stdbuff* keyboard_buffer;

void keyboard_handler()
{
	if (keyboard_buffer == NULL) {
		keyboard_buffer = init_buff(BUFFER_SIZE);
		memset(keyboard_buffer->data, ' ', BUFFER_SIZE);
	}

	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) {
			write_buff(keyboard_buffer, " ", 1);
		} else if (keycode == KEY_BACKSPACE) {
			if (main_fb.x != 0)
				write_buff(keyboard_buffer, "\b \b", 3);
		} else if (keycode == KEY_ENTER) {
			char* output = kalloc(sizeof(char) + 1);
			output[0] = keymap[keycode];
			write_buff(keyboard_buffer, output, 1);
			kfree(output);
		} else {
			char* output = kalloc(sizeof(char) + 1);
			if (keymap[keycode] == ' ') return;
			if (is_pressed[KEY_LSHIFT] || is_pressed[KEY_RSHIFT])
				output[0] = shift_keymap[keycode];
			else
				output[0] = keymap[keycode];
			write_buff(keyboard_buffer, output, 1);
			kfree(output);
		}
	} else {
		is_pressed[keycode - 128] = false;
	}

	uint32_t len = (uint32_t)(keyboard_buffer->head - keyboard_buffer->tail);
	char* print_buff = kalloc(len + 1);
	read_buff(keyboard_buffer, print_buff, len);
	printf("%s", print_buff);
	kfree(print_buff);
}