Implemented Escape Sequences!!!!

This commit is contained in:
2024-05-28 20:42:03 +02:00
parent 74c38342d7
commit 7a2afbd653
8 changed files with 126 additions and 27 deletions
+27
View File
@@ -0,0 +1,27 @@
#ifndef COLOR_H
#define COLOR_H
#define BLACK 0x00000000 // 0
#define RED 0x00ff0000 // 1
#define GREEN 0x0000ff00 // 2
#define YELLOW 0x00ffff00 // 3
#define BLUE 0x000000ff // 4
#define MAGENTA 0x00ff00ff // 5
#define CYAN 0x0000ffff // 6
#define WHITE 0x00ffffff // 7
uint32_t codeToColor(uint8_t code) {
switch (code) {
case 0: return BLACK;
case 1: return RED;
case 2: return GREEN;
case 3: return YELLOW;
case 4: return BLUE;
case 5: return MAGENTA;
case 6: return CYAN;
case 7: return WHITE;
default: return BLACK;
}
}
#endif
+4
View File
@@ -11,6 +11,10 @@ void print(const char* s) {
write(STDOUT, s, strlen(s));
}
void clear() {
print("\x1b[J");
}
void putc(char c) {
static char _c; // We need to pass in a static address. Idk why
_c = c;
+2 -3
View File
@@ -18,9 +18,8 @@ void timerHandler(intFrame_t* iframe) {
static uint32_t cursorTicks = 0;
cursorTicks++;
if (!terminal->hideCursor) {
if (cursorTicks % 500 == 0) putcAt(127, terminal->x, terminal->y);
else if (cursorTicks % 250 == 0) putcAt(' ', terminal->x, terminal->y);
if (!terminal->hideCursor) { // TODO: Make a better cursor drawing thing
if (cursorTicks % 500 == 0) toggleCursor();
}
if (*sleepTimerTicks > 0) { --(*sleepTimerTicks); }
+2 -1
View File
@@ -4,6 +4,7 @@
#include <interrupt/pic.h>
#include <interrupt/idt.h>
#include <screen/text.h>
#include <screen/gfxmode.h>
#include <stdbool.h>
#include <string.h>
@@ -132,7 +133,7 @@ __attribute__ ((interrupt)) void rtcHandler(intFrame_t* iframe) {
uint32_t x = gfxMode->xRes - len - 1;
char* str = &buf[0];
for (uint8_t i = 0; i != len; i++, x++) {
putcAt(str[i], x, 0);
drawChar(str[i], x, 0, FG_COLOR, BG_COLOR);
}
}
}
-9
View File
@@ -4,15 +4,6 @@
#include <memory/addresses.h>
#include <stdint.h>
#define BLACK 0x00000000 // 0
#define RED 0x00ff0000 // 1
#define GREEN 0x0000ff00 // 2
#define YELLOW 0x00ffff00 // 3
#define BLUE 0x000000ff // 4
#define MAGENTA 0x00ff00ff // 5
#define CYAN 0x0000ffff // 6
#define WHITE 0x00ffffff // 7
#define BG_COLOR 0x00010b17
#define FG_COLOR 0x00ebddf4
+3 -6
View File
@@ -8,9 +8,6 @@
#include <stdint.h>
#include <string.h>
uint32_t bgColor = BG_COLOR;
uint32_t fgColor = FG_COLOR;
// TODO: Make this check if it's a valid font
// TODO: Make it take a fileEntry_t?
uint8_t loadFont(char* filename) {
@@ -21,7 +18,7 @@ uint8_t loadFont(char* filename) {
}
// TODO: Reimplement larger text
void putcAt(char c, uint32_t x, uint32_t y) {
void drawChar(char c, uint32_t x, uint32_t y, uint32_t fgColor, uint32_t bgColor) {
// Font info
char* font = (char*)FONT_LOC;
uint8_t charWidth = *(uint8_t*)FONT_WIDTH;
@@ -65,7 +62,7 @@ void scroll() { // TODO: Make this faster (still kinda slow, but memcpy32() help
memcpy32((char*)address, (char*)(address - bytesPerLine), bytesPerLine);
}
uint32_t color = convertColor(bgColor);
uint32_t color = convertColor(terminal->bg);
uint8_t* lastLine = (uint8_t*)(vidmem + (lines-1) * bytesPerLine);
uint8_t bytesPerPx = (gfxMode->bpp+1) / 8;
@@ -80,7 +77,7 @@ void scroll() { // TODO: Make this faster (still kinda slow, but memcpy32() help
}
// TODO: Make this comply with the new terminal.h thing!
void clear() {
void clearScreen(uint32_t bgColor) {
uint8_t* vidmem = (uint8_t*)gfxMode->physicalBasePtr;
uint8_t bytesPerPx = (gfxMode->bpp+1) / 8;
+85 -5
View File
@@ -3,8 +3,10 @@
#include <screen/gfxmode.h>
#include <memory/addresses.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <color.h>
typedef struct {
uint32_t x; // X Position of cursor in characters
@@ -31,24 +33,95 @@ int32_t terminalWrite(const void* buf, uint32_t len) {
// Do we need to check for NULL? Can we just skip it/ignore it?
for (i = 0; i < len && str[i] != '\0'; i++) {
if (str[i] == '\n') {
putcAt(' ', terminal->x, terminal->y);
drawChar(' ', terminal->x, terminal->y, terminal->fg, terminal->bg);
terminal->y++;
terminal->x = 0;
} else if (str[i] == '\b') {
putcAt(' ', terminal->x, terminal->y);
drawChar(' ', terminal->x, terminal->y, terminal->fg, terminal->bg);
if (terminal->x == 0) {
terminal->x = terminal->width - 1;
terminal->y--; // Y could be 0?
} else {
terminal->x--;
}
putcAt(' ', terminal->x, terminal->y);
drawChar(' ', terminal->x, terminal->y, terminal->fg, terminal->bg);
} else if (str[i] == '\x1b') { // Escape sequence
if (str[++i] != '[') { // TODO: Support more types of escape sequences
i--;
continue;
}
i++;
uint32_t args[8] = { 0 };
uint8_t argc = 0;
while (1) { // I'm not 100% sure this works, but i'd like to imagine it does
if (argc > 8) break; // Max arg count? idk
uint32_t value = atoi(&str[i]);
args[argc++] = value;
while (value != 0) { // Skip by the number string
value /= 10;
i++;
}
if (str[i] != ';') break;
i++;
}
switch (str[i]) { // TODO: Add default values (see docs/escape.txt)
case 'A': // Cursor up
terminal->y -= args[0];
break;
case 'B': // Cursor down
terminal->y += args[0];
break;
case 'C': // Cursor left
terminal->x -= args[0];
break;
case 'D': // Cursor right
terminal->x += args[0];
break;
case 'E': // nth row
terminal->x = args[0];
break;
case 'F': // nth col
terminal->y = args[0];
break;
case 'G': // show/hide cursor
terminal->hideCursor = args[0] ? false : true;
break;
case 'H': // move cursor to (x,y)
terminal->x = args[0];
terminal->y = args[1];
break;
case 'I': // RESERVED
case 'J': // Erase screen
clearScreen(terminal->bg);
break;
case 'K': // Erase line
break; // TODO
case 'L': // Erase collumn
break; // TODO
case 'M': // Set FG
if (args[0] < 8) {
terminal->fg = codeToColor(args[0]);
} else if (args[0] == 8) {
terminal->fg = FG_COLOR;
}
break;
case 'N': // Set BG
if (args[0] < 8) {
terminal->bg = codeToColor(args[0]);
} else if (args[0] == 8) {
terminal->bg = BG_COLOR;
}
break;
default: break;
}
//i++;
} else {
putcAt(str[i], terminal->x, terminal->y);
drawChar(str[i], terminal->x, terminal->y, terminal->fg, terminal->bg);
terminal->x++;
if (terminal->x >= terminal->width) { // Go to next line
@@ -60,6 +133,13 @@ int32_t terminalWrite(const void* buf, uint32_t len) {
scroll();
return i; // Number of characters that were printed
}
}
void toggleCursor() {
static bool visible = false;
visible = !visible;
drawChar(visible ? 127 : ' ', terminal->x, terminal->y, terminal->fg, terminal->bg);
}
#endif