Add: read keyboard input (getch)

This commit is contained in:
xamidev
2024-07-21 15:23:32 +02:00
parent b8faf9dd8e
commit 05393b36f8
7 changed files with 53 additions and 2 deletions

Binary file not shown.

BIN
os.iso

Binary file not shown.

View File

@@ -2,6 +2,8 @@
#include "../libc/stdio.h" #include "../libc/stdio.h"
#include "system.h" #include "system.h"
#define KEYBOARD_BUFFER_SIZE 256
unsigned char kbdus[128] = unsigned char kbdus[128] =
{ {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */ 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
@@ -42,6 +44,10 @@ unsigned char kbdus[128] =
0, /* All other keys are undefined */ 0, /* All other keys are undefined */
}; };
static char keyboard_buffer[KEYBOARD_BUFFER_SIZE];
static unsigned int keyboard_buffer_start = 0;
static unsigned int keyboard_buffer_end = 0;
void keyboard_handler() void keyboard_handler()
{ {
unsigned char scancode; unsigned char scancode;
@@ -53,7 +59,12 @@ void keyboard_handler()
} }
else else
{ {
putc(kbdus[scancode]); char c = kbdus[scancode];
if (c)
{
keyboard_buffer[keyboard_buffer_end] = c;
keyboard_buffer_end = (keyboard_buffer_end+1) % KEYBOARD_BUFFER_SIZE;
}
} }
} }
@@ -61,3 +72,12 @@ void keyboard_install()
{ {
irq_install_handler(1, keyboard_handler); irq_install_handler(1, keyboard_handler);
} }
char keyboard_getchar()
{
while (keyboard_buffer_start == keyboard_buffer_end);
char c = keyboard_buffer[keyboard_buffer_start];
keyboard_buffer_start = (keyboard_buffer_start+1) % KEYBOARD_BUFFER_SIZE;
return c;
}

View File

@@ -4,6 +4,8 @@
#include "idt.h" #include "idt.h"
#include "system.h" #include "system.h"
#define BUFFER_SIZE 256
int kmain(int retvalue) int kmain(int retvalue)
{ {
@@ -29,8 +31,13 @@ int kmain(int retvalue)
// TODO: Fix scrolling bug (framebuffer driver) // TODO: Fix scrolling bug (framebuffer driver)
// TODO: Fix keyboard driver bug (some keys mapped weirdly) + add suport for SHIFT and backspace (deleting character) // TODO: Fix keyboard driver bug (some keys mapped weirdly) + add suport for SHIFT and backspace (deleting character)
// TODO: Grub modules to load programs // TODO: Grub modules to load programs
//timer_install(); //timer_install();
keyboard_install(); keyboard_install();
char input_buffer[BUFFER_SIZE];
colorputs("Enter something: ", 9);
get_input(input_buffer, BUFFER_SIZE);
printf("\nYou entered: %s\n", input_buffer);
return retvalue; return retvalue;
} }

View File

@@ -20,5 +20,6 @@ void irq_uninstall_handler(int irq);
void timer_install(); void timer_install();
void delay(int ticks); void delay(int ticks);
void keyboard_install(); void keyboard_install();
char keyboard_getchar();
#endif #endif

View File

@@ -2,6 +2,7 @@
#include "stdio.h" #include "stdio.h"
#include "string.h" #include "string.h"
#include "stdint.h" #include "stdint.h"
#include "../kernel/system.h"
char* fb = (char *) 0x000B8000; char* fb = (char *) 0x000B8000;
const unsigned VGA_WIDTH = 80; const unsigned VGA_WIDTH = 80;
@@ -335,3 +336,23 @@ int* printf_number(int* argp, int length, bool sign, int radix)
return argp; return argp;
} }
int getch()
{
return keyboard_getchar();
}
void get_input(char *buffer, int size) {
int index = 0;
char c;
while (index < size-1)
{
c = getch();
if (c == '\n') break;
buffer[index++] = c;
putc(c);
}
buffer[index] = '\0';
}

View File

@@ -37,5 +37,7 @@ void colorputc(char c, unsigned int color);
void printf(const char* fmt, ...); void printf(const char* fmt, ...);
int* printf_number(int* argp, int length, bool sign, int radix); int* printf_number(int* argp, int length, bool sign, int radix);
int getch();
void get_input(char *buffer, int size);
#endif #endif