Fixup keyboard handler, add string manipulation to lainlib

This commit is contained in:
Curle 2021-06-16 21:06:16 +01:00
parent 49f3afbecb
commit 31fdb462ab
Signed by: TheCurle
GPG Key ID: 5942F13718443F79
8 changed files with 176 additions and 120 deletions

View File

@ -22,6 +22,7 @@
#include <kernel/system/pci.h>
#include <kernel/system/stack.h>
#include <lainlib/lainlib.h>
#include <lainlib/ethernet/e1000/e1000.h>
//Removed cause "wacky copyrighted stuff"

View File

@ -7,14 +7,14 @@
typedef struct {
char Char;
int Scancode;
char Scancode;
bool Pressed;
} KeyboardData;
typedef void (*KeyboardCallback)(KeyboardData* Frame);
typedef void (*KeyboardCallback)(KeyboardData Frame);
extern KeyboardCallback KeyboardCallbacks[16];
int SetupKBCallback(void (*Handler)(KeyboardData* Frame));
int SetupKBCallback(void (*Handler)(KeyboardData Frame));
void UninstallKBCallback(int Index);

View File

@ -1,4 +1,3 @@
#pragma once
/************************
@ -13,8 +12,6 @@
* If need be, they can also be moved into a trimmed-down "kernel libc" or "libk".
*/
#include <lainlib/vector/vector.h>
#include <lainlib/list/list.h>
@ -22,4 +19,6 @@
#include <lainlib/mutex/spinlock.h>
#include <lainlib/mutex/ticketlock.h>
#include <lainlib/string/str.h>
#include <lainlib/compression/lzg.h>

10
inc/lainlib/string/str.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
/************************
*** Team Kitty, 2021 ***
*** Chroma ***
***********************/
size_t strlen(const char* String);
bool strcmp(char* a, const char* b);

View File

@ -20,7 +20,13 @@ size_t KernelEnd = (size_t) &end;
address_space_t KernelAddressSpace;
void PrintPressedChar(KeyboardData* data);
void PrintPressedChar(KeyboardData data);
int CharPrinterCallbackID;
void TrackInternalBuffer(KeyboardData data);
int InternalBufferID;
size_t BufferLength = 0;
char* InternalBuffer;
int Main(void) {
KernelAddressSpace = (address_space_t) {0};
@ -48,13 +54,14 @@ int Main(void) {
InitMemoryManager();
InitPrint();
InitPaging();
Printf("Paging complete. System initialized.\n\r");
KernelLoaded = true;
InternalBuffer = (char*) kmalloc(4098);
SerialPrintf("[ Mem] Allocated a text buffer at 0x%p\r\n", (size_t) InternalBuffer);
SetForegroundColor(0x00FF0000);
WriteChar('C');
SetForegroundColor(0x0000FF00);
@ -82,22 +89,54 @@ int Main(void) {
SetForegroundColor(0x00FFFFFF);
int CharPrinterCallbackID = SetupKBCallback(&PrintPressedChar);
UNUSED(CharPrinterCallbackID);
CharPrinterCallbackID = SetupKBCallback(&PrintPressedChar);
InternalBufferID = SetupKBCallback(&TrackInternalBuffer);
for (;;) {}
return 0;
}
void PrintPressedChar(KeyboardData* data) {
void PrintPressedChar(KeyboardData data) {
if(!KernelLoaded) return;
if(data->Pressed) {
SerialPrintf("Key released: [\\%c]\r\n", data->Char);
if(data.Pressed) {
SerialPrintf("Key pressed: [\\%c (%x)]\r\n", data.Char, data.Scancode);
Printf("%c", data.Char);
} else {
SerialPrintf("Key pressed: [\\%c (%x)]\r\n", data->Char, data->Scancode);
Printf("%c", data->Char);
SerialPrintf("Key released: [\\%c]\r\n", data.Char);
}
}
void TrackInternalBuffer(KeyboardData data) {
if(!data.Pressed) return;
bool tentative = false;
if(BufferLength > 4097) tentative = true;
if(data.Char == '\b') {
BufferLength--;
tentative = false;
}
if(data.Scancode == 0x1C) {
InternalBuffer[BufferLength] = '\0'; // Null-terminate to make checking easier
SerialPrintf("[ Kbd] Enter pressed.\r\n");
if(strcmp(InternalBuffer, "editor")) {
UninstallKBCallback(InternalBufferID);
StartEditor(CharPrinterCallbackID);
} else {
SerialPrintf("[ Kbd] No match for %s\r\n", InternalBuffer);
memset(InternalBuffer, 0, 4098);
BufferLength = 0;
}
}
if(!tentative && data.Scancode < 0x27 && data.Scancode != 0x1C) {
SerialPrintf("[ Kbd] Adding %c to the buffer.\r\n", data.Char);
InternalBuffer[BufferLength] = data.Char;
BufferLength++;
}
}

25
src/lainlib/string/str.c Normal file
View File

@ -0,0 +1,25 @@
/************************
*** Team Kitty, 2021 ***
*** Chroma ***
***********************/
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
size_t strlen(const char* String) {
size_t Len = 0;
while(String[Len] != '\0') {
Len++;
}
return Len;
}
bool strcmp(char* a, const char* b) {
size_t aI = 0, bI = 0;
while(true) {
if(a[aI] != b[bI]) return false;
if(a[aI] == '\0') return true;
aI++;
bI++;
}
}

View File

@ -57,7 +57,7 @@ KeyboardCallback KeyboardCallbacks[16] = {
static int CurrentCallback = 0;
int SetupKBCallback(void (*Handler)(KeyboardData* Frame)) {
int SetupKBCallback(void (*Handler)(KeyboardData Frame)) {
KeyboardCallbacks[CurrentCallback++] = Handler;
return CurrentCallback;
}
@ -81,8 +81,6 @@ void KbdEcho() {
void UpdateKeyboard(uint8_t msg) {
InputBuffer[0] = msg;
switch(msg) {
case 0x0:
KbdFlags.Error = 1;
@ -119,16 +117,16 @@ void UpdateKeyboard(uint8_t msg) {
KeyboardData data = (KeyboardData) {
.Scancode = msg,
.Pressed = msg > 0x80 && msg < 0xD8 ? true : false,
.Pressed = msg > 0x80 && msg < 0xD8 ? false : true,
.Char = msg > 0x80 && msg < 0xD8 ? keys[msg - 0x80] : keys[msg]
};
void (*Handler)(KeyboardData* data);
void (*Handler)(KeyboardData data);
for(size_t handlerNum = 0; handlerNum < 16; handlerNum++) {
Handler = KeyboardCallbacks[handlerNum];
if(Handler) {
Handler(&data);
Handler(data);
}
}
}

View File

@ -282,9 +282,6 @@ static struct liballoc_major *allocate_new_page( unsigned int size )
}
void *PREFIX(malloc)(size_t req_size)
{
int startedBet = 0;
@ -630,14 +627,6 @@ void *PREFIX(malloc)(size_t req_size)
return NULL;
}
void PREFIX(free)(void *ptr)
{
struct liballoc_minor *min;
@ -682,7 +671,6 @@ void PREFIX(free)(void *ptr)
#endif
}
if ( min->magic == LIBALLOC_DEAD )
{
#if defined DEBUG || defined INFO
@ -763,10 +751,6 @@ void PREFIX(free)(void *ptr)
liballoc_unlock(); // release the lock
}
void* PREFIX(calloc)(size_t nobj, size_t size)
{
int real_size;