From: Duje Mihanović <duje.mihanovic@skole.hr>
Date: Tue, 14 Jun 2022 19:06:49 +0000 (+0200)
Subject: Implement input part of stdio
X-Git-Url: https://git.dujemihanovic.xyz/?a=commitdiff_plain;h=41cf62fc4e0ab87d8fbb584678a2d2e1dccdc065;p=nameless-os.git

Implement input part of stdio

This is not very good and will definitely have to be redone later, but for now
it does the job.
---

diff --git a/include/arch/x86/input/ps2.h b/include/arch/x86/input/ps2.h
index 8b90a73..c0c47de 100644
--- a/include/arch/x86/input/ps2.h
+++ b/include/arch/x86/input/ps2.h
@@ -82,11 +82,14 @@ static const char scancodes[] = {
 	[0x3a] = 'm',
 
 	/* special */
-	[0x29] = ' '
+	[0x29] = ' ',
+	[0x5a] = '\n',
+	[0x66] = 0x7f /* backspace */
 };
 
 extern int ps2_initialize();
 extern void ps2_input_wait();
 extern void ps2_output_wait();
+extern char ps2_get_keystroke();
 
 #endif
diff --git a/include/arch/x86/tty.h b/include/arch/x86/tty.h
index 2224737..83350c8 100644
--- a/include/arch/x86/tty.h
+++ b/include/arch/x86/tty.h
@@ -22,7 +22,10 @@
 
 extern void screen_clear(void);
 extern void kprint(const char *string, uint8_t color);
+extern void kprintc(const char character, uint8_t color);
 extern void kprintb(const uint8_t byte);
 extern void kprintw(const uint16_t word);
 extern void kprintd(const uint32_t dword);
+
+extern char *kgets(void);
 #endif
diff --git a/kernel/arch/x86/tty/tty.c b/kernel/arch/x86/tty/tty.c
index 07d734e..ca0c3e7 100644
--- a/kernel/arch/x86/tty/tty.c
+++ b/kernel/arch/x86/tty/tty.c
@@ -1,6 +1,7 @@
 #include <io.h>
 #include <tty.h>
 #include <stdint.h>
+#include <input/ps2.h>
 
 #define VGA_WIDTH 80
 #define VGA_HEIGHT 25
@@ -11,6 +12,7 @@ static int cursor_x = 0; /* keep track of where cursor is */
 static int cursor_y = 0;
 
 char *hex_chars = "0123456789ABCDEF";
+char kgets_buffer[100];
 
 void screen_clear(void)
 {
@@ -74,6 +76,16 @@ void kprintc(const char character, uint8_t color)
 	uint16_t crtc_port;
 
 	if ( character == '\n') { cursor_x = 0; cursor_y++; }
+	if (character == 0x7f) {
+		cursor_x--;
+		if (cursor_x < 0) {
+			if (cursor_y > 0) {
+				cursor_x = VGA_WIDTH - 1;
+				cursor_y--;
+			} else cursor_x = 0;
+		}
+		video_memory[(cursor_y * VGA_WIDTH + cursor_x) * 2] = ' ';
+	}
 	else { video_memory[(cursor_y * VGA_WIDTH + cursor_x) * 2] = character; video_memory[((cursor_y * VGA_WIDTH + cursor_x++) * 2)+ 1] = color != 0 ? color : VGA_COLOR_LIGHT_GRAY; }
 	if ( cursor_x >= VGA_WIDTH ) { cursor_x = 0; cursor_y++; }
 	if ( cursor_y >= VGA_HEIGHT ) { scroll_up(); }
@@ -137,3 +149,29 @@ void kprintd(uint32_t dword)
 	temp = dword & 0xF;
 	kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
 }
+
+char *kgets()
+{
+	for (int i=0; i<100; i++) {
+		kgets_buffer[i] = '\0';
+	}
+
+	int i=0;
+	while (i<100) {
+		char temp = ps2_get_keystroke();
+		if (temp == '\n') break;
+		if (temp == 0x7f) {
+			if (i>0) {
+				i-=1;
+				kgets_buffer[i] = '\0';
+				kprintc(temp, 0);
+			}
+			continue;
+		}
+		kgets_buffer[i] = temp;
+		kprintc(temp, 0);
+		i++;
+	}
+
+	return kgets_buffer;
+}
diff --git a/kernel/drivers/input/ps2.c b/kernel/drivers/input/ps2.c
index ab4be5c..365c0ce 100644
--- a/kernel/drivers/input/ps2.c
+++ b/kernel/drivers/input/ps2.c
@@ -5,6 +5,7 @@
 #include <irq/interrupt.h>
 
 static int was_released = 0, is_caps = 0;
+static char buffer;
 
 int ps2_keyb_handler()
 {
@@ -27,14 +28,21 @@ int ps2_keyb_handler()
 			return 0;
 		}
 		if (!is_caps) {
-			kprintc(scancodes[scancode], 0);
+			buffer = scancodes[scancode];
 		} else {
-			kprintc(scancodes[scancode] - ('a'-'A'), 0);
+			buffer = scancodes[scancode] - ('a'-'A');
 		}
 	}
 	return 0;
 }
 
+char ps2_get_keystroke()
+{
+	buffer = 0;
+	while (!buffer);
+	return buffer;
+}
+
 int ps2_initialize()
 {
 	uint8_t ccb, is_2channel, port_1_test, port_2_test;
diff --git a/kernel/kernel.c b/kernel/kernel.c
index c82bd4d..0ce9b6d 100644
--- a/kernel/kernel.c
+++ b/kernel/kernel.c
@@ -5,6 +5,7 @@
 #include <irq/interrupt.h>
 #include <irq/i8259a.h>
 #include <input/ps2.h>
+#include <stddef.h>
 
 struct idt_descriptor idt[256] __attribute__((aligned(0x10)));
 struct idtr idtr __attribute__((aligned(0x10)));
@@ -82,5 +83,11 @@ void kmain(void)
 	}
 	asm volatile ("sti");
 	kprint("All done\n", 0);
-	while(1);
+
+	while(1) {
+		char *string = NULL;
+		string = kgets();
+		kprint(string, VGA_COLOR_LIME);
+		kprint("\n", 0);
+	}
 }