Improved keyboard IRQ handler, added getc() function, improved read()

This commit is contained in:
2024-05-15 20:06:38 +02:00
parent dba9e5889b
commit 9812f3f58d
4 changed files with 84 additions and 74 deletions
+40 -70
View File
@@ -3,95 +3,65 @@
#include <interrupt/idt.h> #include <interrupt/idt.h>
#include <interrupt/pic.h> #include <interrupt/pic.h>
#include <ports/io.h>
#include <screen/text.h> #include <screen/text.h>
#include <stdint.h> #include <keyboard/keys.h>
#include <stdbool.h> #include <stdbool.h>
// Keycodes #define MAX_INPUT_LENGTH 256
#define LEFT_SHIFT_PRESS 0x2a
#define LEFT_SHIFT_RELEASE (LEFT_SHIFT_PRESS | 0x80)
#define RIGHT_SHIFT_PRESS 0x36
#define RIGHT_SHIFT_RELEASE (RIGHT_SHIFT_PRESS | 0x80)
#define LEFT_CTRL_PRESS 0x1D
#define LEFT_CTRL_RELEASE (LEFT_CTRL_PRESS | 0x80)
#define RIGHT_CTRL_PRESS 0x1D
#define RIGHT_CTRL_RELEASE (RIGHT_CTRL_PRESS | 0x80)
#define LEFT_ALT_PRESS 0x38
#define LEFT_ALT_RELEASE (LEFT_ALT_PRESS | 0x80)
#define RIGHT_ALT_PRESS 0x38
#define RIGHT_ALT_RELEASE (RIGHT_ALT_PRESS | 0x80)
#define MAX_BUFFER_SIZE 256 typedef struct {
char character;
uint8_t shift;
} keyboardState_t;
const char US_Keyboard[2][128] = { // TODO: Put at set address
{ keyboardState_t keyboard;
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',
'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',
0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0,
'*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', // Numpad
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
},
{
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0, 'A', 'S',
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0, '|', 'Z', 'X', 'C', 'V',
'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}
};
uint8_t shift = 0;
uint16_t bufferLen = 0;
char buffer[MAX_BUFFER_SIZE + 1] = { 0 };
bool echo = false;
bool canBackspace = false;
// TODO: Refactor this
__attribute__ ((interrupt)) __attribute__ ((interrupt))
void keyboardHandler(intFrame_t* iframe) { void keyboardHandler(intFrame_t* iframe) {
uint8_t scancode; uint8_t scancode = inb(0x60);
scancode = inb(0x60); char c = US_Keyboard[keyboard.shift][scancode];
if (scancode == LEFT_SHIFT_PRESS || scancode == RIGHT_SHIFT_PRESS) shift = 1; if (scancode == LEFT_SHIFT_PRESS || scancode == RIGHT_SHIFT_PRESS) {
else if (scancode == LEFT_SHIFT_RELEASE || scancode == RIGHT_SHIFT_RELEASE) shift = 0; keyboard.shift = 1;
} else if (scancode == LEFT_SHIFT_RELEASE || scancode == RIGHT_SHIFT_RELEASE) {
char c = US_Keyboard[shift][scancode]; keyboard.shift = 0;
}
if (scancode & 0x80) { // Keyup if (scancode & 0x80) { // Keyup
} else if (c != 0) { } else if (c != '\0') {
if (!canBackspace && c == '\b') return; keyboard.character = c;
if (echo) putc(c);
buffer[bufferLen] = c;
if (c == '\b') bufferLen--;
else bufferLen++;
if (bufferLen > 0) canBackspace = true;
else canBackspace = false;
} }
sendPicEOI(1); sendPicEOI(1);
} }
// TODO: Add a 'char readc();' function that returns a single read character and use it for read() char getc() {
// Also this should be refactored to not use so many flag variables keyboard.character = '\0';
char* read(char terminator) { while (keyboard.character == '\0') __asm__ volatile ("hlt");
bufferLen = 0; return keyboard.character;
echo = true; }
while (buffer[bufferLen-1] != terminator && bufferLen < MAX_BUFFER_SIZE); // Reads until a newline
char inputBuffer[MAX_INPUT_LENGTH];
char* read() {
char c;
uint8_t len = 0;
echo = false; while ((c = getc()) != '\n') {
canBackspace = false; if (c == '\b' && len > 0) {
buffer[bufferLen] = '\0'; len--;
putc(c);
} else if (len + 1 != MAX_INPUT_LENGTH) { // Check if we can add a new character
inputBuffer[len++] = c;
putc(c);
}
}
return buffer; inputBuffer[len++] = c; // Add newline
putc(c);
inputBuffer[len] = '\0'; // Null terminate
return inputBuffer;
} }
void setupKeyboard() { void setupKeyboard() {
+40
View File
@@ -0,0 +1,40 @@
#ifndef KEYS_H
#define KEYS_H
#include <stdbool.h>
#define LEFT_SHIFT_PRESS 0x2a
#define LEFT_SHIFT_RELEASE (LEFT_SHIFT_PRESS | 0x80)
#define RIGHT_SHIFT_PRESS 0x36
#define RIGHT_SHIFT_RELEASE (RIGHT_SHIFT_PRESS | 0x80)
#define LEFT_CTRL_PRESS 0x1D
#define LEFT_CTRL_RELEASE (LEFT_CTRL_PRESS | 0x80)
#define RIGHT_CTRL_PRESS 0x1D
#define RIGHT_CTRL_RELEASE (RIGHT_CTRL_PRESS | 0x80)
#define LEFT_ALT_PRESS 0x38
#define LEFT_ALT_RELEASE (LEFT_ALT_PRESS | 0x80)
#define RIGHT_ALT_PRESS 0x38
#define RIGHT_ALT_RELEASE (RIGHT_ALT_PRESS | 0x80)
// Basic US Keyboard layout
static const char US_Keyboard[2][128] = {
{
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',
'\t','q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'','`',
0, '\\','z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0,
'*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', // Numpad
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
},
{
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0, 'A', 'S',
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0, '|', 'Z', 'X', 'C', 'V',
'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}
};
#endif
+2 -2
View File
@@ -64,7 +64,7 @@ void main() {
// TODO: Make it in another file // TODO: Make it in another file
while (1) { while (1) {
print(PROMPT); print(PROMPT);
char* input = read('\n'); char* input = read();
if (input[0] == '\n') continue; if (input[0] == '\n') continue;
@@ -169,7 +169,7 @@ void main() {
drawCircle(1500, 600, 300, WHITE); drawCircle(1500, 600, 300, WHITE);
fillCircle(950, 750, 75, rgb(0xa2, 0xd6, 0xf9)); fillCircle(950, 750, 75, rgb(0xa2, 0xd6, 0xf9));
read('q'); // Wait until we get a 'q' getc(); // Wait until we get a 'q'
clear(); clear();
} else if (strcmp(input, "help") == 0) { } else if (strcmp(input, "help") == 0) {
print("Available Commands:\n"); print("Available Commands:\n");
+2 -2
View File
@@ -36,13 +36,13 @@ Legend:
- [x] Rectange :) - [x] Rectange :)
- [x] Circle! - [x] Circle!
- [x] Filled verisons - [x] Filled verisons
- [ ] Interrupts - [x] Interrupts
- [x] IDT Setup (idt.h) - [x] IDT Setup (idt.h)
- [x] Exception handling - [x] Exception handling
- [x] Hardware/software interrupts - [x] Hardware/software interrupts
- [x] Syscalls! (int 0x80) - [x] Syscalls! (int 0x80)
- [x] IRQ 0 - PIT - [x] IRQ 0 - PIT
- [ ] IRQ 1 - Keyboard - [x] IRQ 1 - Keyboard
- [x] IRQ 8 - CMOS Real time clock - [x] IRQ 8 - CMOS Real time clock
- [?] Other IRQs (idk which ones) - [?] Other IRQs (idk which ones)
- [ ] Math - [ ] Math