feat: kgets()

This commit is contained in:
Karina
2025-12-28 00:10:18 +04:00
parent 794dae7084
commit d0d025dd90
6 changed files with 67 additions and 15 deletions
+1
View File
@@ -14,5 +14,6 @@ void console_set_color(u32 color);
void console_set_cursor_pos(SG_Point *p);
void kprint(const char *str);
void kprintf(const char *fmt, ...);
void kgets(char* buff, u32 lim);
#endif
+9
View File
@@ -3,6 +3,15 @@
#include <types.h>
#define KB_BUFF_SIZE 256
typedef struct {
char buffer[KB_BUFF_SIZE];
u16 head;
u16 tail;
} kb_buffer;
extern kb_buffer kb_buf;
void kb_handler(Registers *regs);
#endif
+39 -1
View File
@@ -5,6 +5,7 @@
#include <drivers/serial.h>
#include <drivers/font.h>
#include <drivers/shitgui.h>
#include <drivers/keyboard.h>
#include <core/math.h>
#include <types.h>
@@ -212,4 +213,41 @@ void kprintf(const char *fmt, ...) {
void console_set_color(u32 color) {
s_color = color;
}
}
static char console_getc() {
while (kb_buf.head == kb_buf.tail) __asm__ volatile ("sti; hlt");
__asm__ volatile ("cli");
char temp = kb_buf.buffer[kb_buf.tail];
kb_buf.tail = (kb_buf.tail + 1) % KB_BUFF_SIZE;
__asm__ volatile ("sti");
return temp;
}
void kgets(char* buff, u32 lim) {
u32 i = 0;
while (true) {
char c = console_getc();
switch (c) {
case '\n': {
buff[i] = '\0';
kprintf("\n");
return;
}
case '\b': {
if (i > 0) {
i--;
kprintf("\b \b");
}
break;
}
default: {
if (i < lim - 1) {
buff[i] = c;
kprintf("%c", c);
i++;
}
}
}
}
}
+7 -3
View File
@@ -83,15 +83,19 @@ unsigned char keyboard_map_shifted[128] = {
};
bool shift_pressed = false;
kb_buffer kb_buf = {0};
void kb_handler(Registers *regs) {
u16 next_head = (kb_buf.head + 1) % KB_BUFF_SIZE;
u8 scancode = inb(0x60);
// make code 0x00 - 0x7F; break code = make code + 0x80
if (scancode < 0x80) {
unsigned char* arr = shift_pressed ? keyboard_map_shifted : keyboard_map;
char ascii = arr[scancode];
if (ascii) {
kprintf("%c", ascii);
if (ascii && next_head != kb_buf.tail) {
kb_buf.buffer[kb_buf.head] = ascii;
kb_buf.head = next_head;
} else {
switch (scancode) {
case 0x2A: shift_pressed = true; break;
@@ -106,4 +110,4 @@ void kb_handler(Registers *regs) {
}
outb(MASTER_COMMAND, 0x20);
// kprintf("Key: %x; shift is %spressed\n", scancode, shift_pressed ? "" : "not ");
}
}
+11 -11
View File
@@ -17,9 +17,6 @@
#include <mm/vmm.h>
extern u64 _kernel_end;
extern u8* bitmap;
extern u64 bitmap_size_g;
extern u64* pml4_kernel;
void kmain(Bootinfo* info) {
u32 *fb = (u32*)info->framebuffer.base;
@@ -31,17 +28,19 @@ void kmain(Bootinfo* info) {
sg_ctx.width = info->framebuffer.width;
sg_ctx.pitch = info->framebuffer.pitch;
SG_Point start_pos = {75, 55}; // greeting
serial_init();
console_init(&sg_ctx);
console_clear(0xFFFFFF);
console_set_cursor_pos(&start_pos);
console_set_color(0x000000);
gdt_init();
idt_init();
pic_remap();
pmm_init(info->mem);
vmm_init(info);
SG_Point start_pos = {75, 55}; // greeting
console_clear(0xFFFFFF);
console_set_cursor_pos(&start_pos);
console_set_color(0x000000);
SG_Point logo_point = {0, 10};
sg_put_img(&sg_ctx, &logo_point, &logo_img);
@@ -50,9 +49,10 @@ void kmain(Bootinfo* info) {
SG_Point text_normal_point = {0, 120}; // not nice to hardcode nums like that but we have what we have
console_set_cursor_pos(&text_normal_point);
pmm_init(info->mem);
vmm_init(info);
kprintf("ksh_> ");
char buff[32];
kgets(buff, 32);
kprintf("You typed: %s", &buff);
__asm__ volatile ("sti");
View File