LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
keyboard.c
Go to the documentation of this file.
1
9
10#include <drivers/keyboard.h>
11#include <arch/x86_64/io.h>
12#include <arch/x86_64/irq.h>
13
14#include <stdbool.h>
15
16#include "arch/x86_64/pic.h"
17
19static volatile uint8_t kbd_buf[KBD_BUF_SIZE];
20
22static volatile uint32_t kbd_head;
24static volatile uint32_t kbd_tail;
25
27static bool e0_pending;
28
30bool shift_down, ctrl_down, alt_down, caps_on;
31
33static const char sc_lower[0x3A] = {
34 0, 27, '1','2','3','4','5','6','7','8','9','0','-','=', '\b', '\t', // 0x00 - 0x0F
35 'q','w','e','r','t','y','u','i','o','p','[',']','\n', 0, 'a','s', // 0x10 - 0x1F
36 'd','f','g','h','j','k','l',';','\'','`', 0, '\\','z','x','c','v', // 0x20 - 0x2F
37 'b','n','m',',','.','/', 0, '*', 0, ' ' // 0x30 - 0x39
38};
39
41static const char sc_upper[0x3A] = {
42 0, 27, '!','@','#','$','%','^','&','*','(',')','_','+', '\b', '\t',
43 'Q','W','E','R','T','Y','U','I','O','P','{','}','\n', 0, 'A','S',
44 'D','F','G','H','J','K','L',':','"','~', 0, '|','Z','X','C','V',
45 'B','N','M','<','>','?', 0, '*', 0, ' '
46};
47
53static bool kbd_buf_push(uint8_t b) { // called from the IRQ handler
54 uint32_t next = (kbd_head + 1) & (KBD_BUF_SIZE - 1);
55 if (next == kbd_tail) return false; // full: drop the byte
56 kbd_buf[kbd_head] = b;
57 kbd_head = next;
58 return true;
59}
60
61int kbd_buf_pop(void) {
62 if (kbd_tail == kbd_head) return -1;
63 uint8_t b = kbd_buf[kbd_tail];
64 kbd_tail = (kbd_tail + 1) & (KBD_BUF_SIZE - 1);
65 return b;
66}
67
68int kbd_getchar(void) {
69 for (;;)
70 {
71 asm volatile ("hlt");
72
73 int raw;
74 if ((raw = kbd_buf_pop()) >= 0) return raw;
75 }
76}
77
78bool kbd_decode(uint8_t raw, struct key_event *ev) {
79 if (raw == 0xE0) { e0_pending = true; return false; } // prefix: remember it, wait for the next byte
80
81 bool ext = e0_pending;
82 e0_pending = false;
83 bool pressed = !(raw & 0x80);
84 uint8_t code = raw & 0x7F;
85
86 // Update modifier state from BOTH make and break codes
87 if (!ext) {
88 if (code == 0x2A || code == 0x36) shift_down = pressed; // left or right shift
89 else if (code == 0x1D) ctrl_down = pressed;
90 else if (code == 0x38) alt_down = pressed;
91 else if (code == 0x3A && pressed) caps_on = !caps_on; // toggles on press only
92 } else {
93 if (code == 0x1D) ctrl_down = pressed; // right ctrl
94 else if (code == 0x38) alt_down = pressed; // right alt (AltGr)
95 }
96
97 // Translate to a character
98 char c = 0;
99 if (!ext && code < 0x3A) {
100 char base = sc_lower[code];
101 bool is_letter = base >= 'a' && base <= 'z';
102 bool use_shift = shift_down ^ (caps_on && is_letter); // Caps Lock only flips letters
103 c = use_shift ? sc_upper[code] : base;
104 }
105
106 ev->scancode = code;
107 ev->extended = ext;
108 ev->pressed = pressed;
109 ev->ascii = c;
110 return true;
111}
112
117static void keyboard_handler(struct interrupt_frame *frame) {
118 (void)frame;
119
120 // Check if there's no byte to read or interrupt came from port 2:
121 if ((inb(PS2_STATUS) & 0x01) == 0 || (inb(PS2_STATUS) & 0b100000) == 1)
122 return;
123
124 uint8_t scancode = inb(PS2_DATA); // read it: this empties the controller's output buffer
125 kbd_buf_push(scancode); // hand it to the main code
126}
127
128void keyboard_init(void) {
129 while (inb(PS2_STATUS) & 0x01) // flush stale bytes
130 (void)inb(PS2_DATA);
131
134}
Provides utilities for IO operations with the hardware.
uint8_t inb(uint16_t port)
Inputs a byte from the specified port.
Definition io.h:31
void irq_register(uint8_t irq, irq_handler_fn fn)
Registers a handler function for the specified IRQ number.
Definition irq.c:18
IRQ Handler and utilities.
static volatile uint32_t kbd_tail
next slot to READ: only the main code changes this
Definition keyboard.c:24
static volatile uint32_t kbd_head
next slot to WRITE: only the handler changes this
Definition keyboard.c:22
static const char sc_lower[0x3A]
The lowercase character conversion table for set 1.
Definition keyboard.c:33
int kbd_buf_pop(void)
Pops byte from the keyboard key buffer.
Definition keyboard.c:61
static const char sc_upper[0x3A]
The uppercase (shifted) character conversion table for set 1.
Definition keyboard.c:41
bool kbd_decode(uint8_t raw, struct key_event *ev)
Convert a keyboard byte into the correct char.
Definition keyboard.c:78
static bool e0_pending
saw 0xE0, the next byte completes the key
Definition keyboard.c:27
void keyboard_init(void)
Initialize the keyboard driver and register the handler with the IRQ handler.
Definition keyboard.c:128
bool shift_down
Keyboard modifier key status.
Definition keyboard.c:30
int kbd_getchar(void)
Waits for a key to be pressed and returns the character.
Definition keyboard.c:68
static volatile uint8_t kbd_buf[KBD_BUF_SIZE]
The buffer holding the keys codes.
Definition keyboard.c:19
static bool kbd_buf_push(uint8_t b)
Pushes byte to key buffer.
Definition keyboard.c:53
static void keyboard_handler(struct interrupt_frame *frame)
Pushes the key code to the key buffer.
Definition keyboard.c:117
Handler and utilities for the keyboard.
void pic_clear_mask(uint8_t irq)
Unmasks (Enables) a line.
Definition pic.c:79
Handles the legacy PIC for IRQ interrupts.
The frame holding the registers when an interrupt happens.
Definition idt.h:16
A decoded key event.
Definition keyboard.h:24
bool extended
came after an 0xE0 prefix
Definition keyboard.h:28
uint8_t scancode
the code with bit 7 removed
Definition keyboard.h:26
char ascii
0 if the key has no printable character
Definition keyboard.h:32
bool pressed
true = key down, false = key up
Definition keyboard.h:30