From 00f90246c8259ea171be3e143ec30ea73e923558 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 30 Oct 2022 18:34:40 +0100 Subject: [PATCH] Kernel: Implement very basic escape sequences for TextRenderer --- apps/src/sh.c | 9 +++- kernel/src/render/TextRenderer.cpp | 70 +++++++++++++++++++----------- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/apps/src/sh.c b/apps/src/sh.c index 4bf2a099..c1310fb3 100644 --- a/apps/src/sh.c +++ b/apps/src/sh.c @@ -113,9 +113,14 @@ int command_match_builtins(command* cmd) { if (command_matches(cmd, "exit ")) { exit(atoi(cmd->buffer + 5)); } if (command_matches_exactly(cmd, "exit")) { exit(0); } - if (command_matches_exactly(cmd, "pid")) + if (command_matches_exactly(cmd, "id")) { - printf("pid %ld, ppid %ld\n", getpid(), getppid()); + printf("pid %ld, ppid %ld, uid %d (%s), gid %d\n", getpid(), getppid(), getuid(), username, getgid()); + return 1; + } + if (command_matches_exactly(cmd, "clear")) + { + fputs("\033@", stdout); // clear screen. for now, escape sequences in luna are non-standard. return 1; } return 0; diff --git a/kernel/src/render/TextRenderer.cpp b/kernel/src/render/TextRenderer.cpp index 4084c03f..d59a1a9b 100644 --- a/kernel/src/render/TextRenderer.cpp +++ b/kernel/src/render/TextRenderer.cpp @@ -30,7 +30,7 @@ void TextRenderer::reset() #pragma GCC optimize("O0") static void putchar_at_offset( - char c, [[maybe_unused]] uint32_t cx, [[maybe_unused]] uint32_t cy, [[maybe_unused]] Color& fg, + char c, uint32_t cx, uint32_t cy, [[maybe_unused]] Color& fg, [[maybe_unused]] Color& bg) // FIXME: Rewrite this function to actually work with foreground and background colors. { uint8_t* glyph = &font[c * 16]; @@ -46,38 +46,29 @@ static void putchar_at_offset( } } +static bool g_escape_sequence = false; + #pragma GCC pop_options void TextRenderer::putchar(char chr) { - switch (chr) + if (g_escape_sequence) { - case '\n': { - ypos += FONT_HEIGHT; - if ((ypos + FONT_HEIGHT) >= bootboot.fb_height) + g_escape_sequence = false; + switch (chr) { - memcpy((void*)bootboot.fb_ptr, (char*)bootboot.fb_ptr + (bootboot.fb_scanline * FONT_HEIGHT), - bootboot.fb_size - (bootboot.fb_scanline * FONT_HEIGHT)); - ypos -= FONT_HEIGHT; - framebuffer0.paint_rect(0, ypos, bootboot.fb_width, FONT_HEIGHT, Color::Black); + case '@': + reset(); + framebuffer0.clear(Color::Black); + break; + default: break; } - xpos = 0; - break; } - case '\r': xpos = 0; break; - case '\b': - if (xpos != 0) + else + { + switch (chr) { - xpos -= FONT_WIDTH; - framebuffer0.paint_rect(xpos, ypos, FONT_WIDTH, FONT_HEIGHT, Color::Black); - } - break; - default: { - putchar_at_offset(chr, xpos, ypos, fgColor, bgColor); - xpos += FONT_WIDTH; - if ((xpos + FONT_WIDTH) > bootboot.fb_width) - { - xpos = 0; + case '\n': { ypos += FONT_HEIGHT; if ((ypos + FONT_HEIGHT) >= bootboot.fb_height) { @@ -86,9 +77,36 @@ void TextRenderer::putchar(char chr) ypos -= FONT_HEIGHT; framebuffer0.paint_rect(0, ypos, bootboot.fb_width, FONT_HEIGHT, Color::Black); } + xpos = 0; + break; + } + case '\r': xpos = 0; break; + case '\b': + if (xpos != 0) + { + xpos -= FONT_WIDTH; + framebuffer0.paint_rect(xpos, ypos, FONT_WIDTH, FONT_HEIGHT, Color::Black); + } + break; + case '\033': g_escape_sequence = true; break; + default: { + putchar_at_offset(chr, xpos, ypos, fgColor, bgColor); + xpos += FONT_WIDTH; + if ((xpos + FONT_WIDTH) > bootboot.fb_width) + { + xpos = 0; + ypos += FONT_HEIGHT; + if ((ypos + FONT_HEIGHT) >= bootboot.fb_height) + { + memcpy((void*)bootboot.fb_ptr, (char*)bootboot.fb_ptr + (bootboot.fb_scanline * FONT_HEIGHT), + bootboot.fb_size - (bootboot.fb_scanline * FONT_HEIGHT)); + ypos -= FONT_HEIGHT; + framebuffer0.paint_rect(0, ypos, bootboot.fb_width, FONT_HEIGHT, Color::Black); + } + } + break; + } } - break; - } } }