From 9898e645979af08b4a3f443912ba1713fec2f0f8 Mon Sep 17 00:00:00 2001 From: VasilyZa <450270247@qq.com> Date: Sat, 14 Nov 2026 22:26:07 +0800 Subject: [PATCH 1/2] fix(console): support ncurses input and preserve virtual terminal state --- drivers/console/ansi.c | 107 +++++++++++++++++++- drivers/console/dec_graphics.h | 18 ++++ drivers/console/internal.h | 47 +++++++++ drivers/console/panic.c | 5 + drivers/console/render.c | 8 ++ drivers/console/vga_console.c | 13 ++- drivers/console/vga_terminal.c | 15 +-- drivers/console/vt.c | 119 ++++++++++++++++++++--- drivers/tty/pty/keyboard.c | 4 +- include/reliefnt/console.h | 2 +- tools/tests/panic_console_runtime_test.c | 5 + tools/tests/vt_console_test.c | 53 +++++++++- 12 files changed, 356 insertions(+), 40 deletions(-) create mode 100644 drivers/console/dec_graphics.h diff --git a/drivers/console/ansi.c b/drivers/console/ansi.c index b4e879f5..58290cdc 100644 --- a/drivers/console/ansi.c +++ b/drivers/console/ansi.c @@ -22,6 +22,79 @@ static const uint32_t fb_ansi_colors[16] = { }; static uint32_t fb_ansi_params[FB_ANSI_MAX_PARAMS]; static bool fb_ansi_param_colon[FB_ANSI_MAX_PARAMS]; +static bool fb_ansi_dec_private; +static uint8_t fb_ansi_charset_target; + +/** + * @brief Save the serialized renderer and partial ANSI/UTF-8 parser. + * @param state Required writable checkpoint storage owned by the caller. + */ +void fb_console_state_save(struct fb_console_state *state) +{ + state->utf8 = console_utf8; + state->x = fb_x; + state->y = fb_y; + state->cols = fb_cols; + state->rows = fb_rows; + state->col = fb_col; + state->row = fb_row; + state->saved_col = fb_saved_col; + state->saved_row = fb_saved_row; + state->foreground = fb_console_fg_color; + state->background = fb_console_bg_color; + state->bold = fb_console_bold; + state->underline = fb_console_underline; + state->reverse = fb_console_reverse; + state->underline_color = fb_console_underline_color; + state->underline_color_set = fb_console_underline_color_set; + state->scroll_top = fb_scroll_top; + state->scroll_bottom = fb_scroll_bottom; + state->ansi_state = fb_ansi_state; + state->param_count = fb_ansi_param_count; + state->param_value = fb_ansi_param_value; + state->param_active = fb_ansi_param_active; + state->private_mode = fb_ansi_private; + state->dec_private = fb_ansi_dec_private; + state->charset_target = fb_ansi_charset_target; + __builtin_memcpy(state->params, fb_ansi_params, sizeof(state->params)); + __builtin_memcpy(state->param_colon, fb_ansi_param_colon, sizeof(state->param_colon)); +} + +/** + * @brief Restore a renderer checkpoint for its retained pixel surface. + * @param state Required checkpoint saved for the matching surface layout. + */ +void fb_console_state_restore(const struct fb_console_state *state) +{ + console_utf8 = state->utf8; + fb_x = state->x; + fb_y = state->y; + fb_cols = state->cols; + fb_rows = state->rows; + fb_col = state->col; + fb_row = state->row; + fb_saved_col = state->saved_col; + fb_saved_row = state->saved_row; + fb_console_fg_color = state->foreground; + fb_console_bg_color = state->background; + fb_console_bold = state->bold; + fb_console_underline = state->underline; + fb_console_reverse = state->reverse; + fb_console_underline_color = state->underline_color; + fb_console_underline_color_set = state->underline_color_set; + fb_scroll_top = state->scroll_top; + fb_scroll_bottom = state->scroll_bottom; + fb_ansi_state = state->ansi_state; + fb_ansi_param_count = state->param_count; + fb_ansi_param_value = state->param_value; + fb_ansi_param_active = state->param_active; + fb_ansi_private = state->private_mode; + fb_ansi_dec_private = state->dec_private; + fb_ansi_charset_target = state->charset_target; + __builtin_memcpy(fb_ansi_params, state->params, sizeof(state->params)); + __builtin_memcpy(fb_ansi_param_colon, state->param_colon, sizeof(state->param_colon)); +} + static uint32_t fb_ansi_param(uint32_t index, uint32_t fallback) { if (index >= fb_ansi_param_count) { @@ -117,6 +190,18 @@ static bool fb_console_apply_extended_color(uint32_t index, static void fb_console_execute_csi(char final) { uint32_t n = fb_ansi_param_count; + uint32_t number = fb_console_render_vt ? fb_console_render_vt : console_active_vt; + if (fb_ansi_private) { + if (fb_ansi_dec_private && (final == 'h' || final == 'l') && + number && number <= CONSOLE_VT_COUNT) { + struct console_vt_text *state = &vt_text[number - 1]; + for (uint32_t i = 0; i < n; ++i) { + if (fb_ansi_params[i] == 1) state->application_cursor = final == 'h'; + if (fb_ansi_params[i] == 25) state->cursor_hidden = final == 'l'; + } + } + return; + } uint32_t p0 = fb_ansi_param(0, (final == 'J' || final == 'K') ? 0u : 1u); uint32_t p1 = fb_ansi_param(1, 1); uint32_t movement = p0 ? p0 : 1u; @@ -276,11 +361,17 @@ static void fb_console_execute_csi(char final) void fb_console_ansi_feed(char ch) { unsigned char byte = (unsigned char)ch; + uint32_t number = fb_console_render_vt ? fb_console_render_vt : console_active_vt; if (!fb_console_enabled) { return; } if (fb_ansi_state == FB_ANSI_NORMAL) { - if (byte == 0x07u || byte == 0x7fu || byte == 0x00u || byte == 0x0eu || byte == 0x0fu) { + if (byte == 0x0eu || byte == 0x0fu) { + if (number && number <= CONSOLE_VT_COUNT) + vt_text[number - 1].charset_bank = byte == 0x0e; + return; + } + if (byte == 0x07u || byte == 0x7fu || byte == 0x00u) { return; } if (byte == 0x1bU) { @@ -296,6 +387,7 @@ void fb_console_ansi_feed(char ch) fb_ansi_param_count = 0; fb_ansi_param_value = 0; fb_ansi_param_active = false; + fb_ansi_dec_private = false; return; } if (ch == '7') { @@ -305,6 +397,11 @@ void fb_console_ansi_feed(char ch) return; } if (ch == 'c') { + if (number && number <= CONSOLE_VT_COUNT) { + struct console_vt_text *state = &vt_text[number - 1]; + state->application_cursor = state->cursor_hidden = false; + state->charset[0] = state->charset[1] = state->charset_bank = 0; + } fb_col = 0; fb_row = 0; fb_scroll_top = 0; @@ -331,6 +428,7 @@ void fb_console_ansi_feed(char ch) return; } if (ch == '(' || ch == ')') { + fb_ansi_charset_target = ch == ')'; fb_ansi_state = FB_ANSI_CHARSET; return; } @@ -354,6 +452,8 @@ void fb_console_ansi_feed(char ch) return; } if (fb_ansi_state == FB_ANSI_CHARSET) { + if (number && number <= CONSOLE_VT_COUNT) + vt_text[number - 1].charset[fb_ansi_charset_target] = ch == '0'; fb_console_reset_ansi(); return; } @@ -372,6 +472,7 @@ void fb_console_ansi_feed(char ch) if ((ch == '?' || ch == '>') && !fb_ansi_param_count && !fb_ansi_param_active) { fb_ansi_private = true; + fb_ansi_dec_private = ch == '?'; return; } if (byte >= '0' && byte <= '9') { @@ -398,9 +499,7 @@ void fb_console_ansi_feed(char ch) fb_ansi_param_active ? fb_ansi_param_value : 0u; fb_ansi_param_colon[fb_ansi_param_count - 1u] = false; } - if (!fb_ansi_private) { - fb_console_execute_csi(ch); - } + fb_console_execute_csi(ch); fb_console_reset_ansi(); return; } diff --git a/drivers/console/dec_graphics.h b/drivers/console/dec_graphics.h new file mode 100644 index 00000000..bda5ba75 --- /dev/null +++ b/drivers/console/dec_graphics.h @@ -0,0 +1,18 @@ +#ifndef RELIEFNT_CONSOLE_DEC_GRAPHICS_H +#define RELIEFNT_CONSOLE_DEC_GRAPHICS_H +#include + +/** @brief Translate the VT100 special-graphics charset for either text backend. + * @param codepoint Decoded Unicode scalar. @return Mapped scalar or the input. + */ +static inline uint32_t console_dec_graphic(uint32_t codepoint) +{ + static const uint16_t table[] = { + 0x25c6,0x2592,0x2409,0x240c,0x240d,0x240a,0x00b0,0x00b1, + 0x2424,0x240b,0x2518,0x2510,0x250c,0x2514,0x253c,0x23ba, + 0x23bb,0x2500,0x23bc,0x23bd,0x251c,0x2524,0x2534,0x252c, + 0x2502,0x2264,0x2265,0x03c0,0x2260,0x00a3,0x00b7, + }; + return codepoint >= 0x60 && codepoint <= 0x7e ? table[codepoint - 0x60] : codepoint; +} +#endif diff --git a/drivers/console/internal.h b/drivers/console/internal.h index 3c94a1fa..07e69b7d 100644 --- a/drivers/console/internal.h +++ b/drivers/console/internal.h @@ -22,9 +22,42 @@ #define CONSOLE_VT_COUNT 6u #define CONSOLE_VT_TEXT_CAP 65536u +/* Pixel checkpoints retain the complete streaming parser across VT changes. */ +struct fb_console_state { + struct reliefos_utf8_stream utf8; + uint32_t x; + uint32_t y; + uint32_t cols; + uint32_t rows; + uint32_t col; + uint32_t row; + uint32_t saved_col; + uint32_t saved_row; + uint32_t foreground; + uint32_t background; + bool bold; + bool underline; + bool reverse; + uint32_t underline_color; + bool underline_color_set; + uint32_t scroll_top; + uint32_t scroll_bottom; + uint8_t ansi_state; + uint32_t param_count; + uint32_t param_value; + bool param_active; + bool private_mode; + bool dec_private; + uint8_t charset_target; + uint32_t params[FB_ANSI_MAX_PARAMS]; + bool param_colon[FB_ANSI_MAX_PARAMS]; +}; + struct console_vt_text { char bytes[CONSOLE_VT_TEXT_CAP]; size_t length; + bool application_cursor, cursor_hidden; + uint8_t charset[2], charset_bank; }; @@ -157,6 +190,7 @@ extern struct console_vt_text vt_text[CONSOLE_VT_COUNT]; extern uint32_t console_active_vt; extern bool console_vt_graphical; extern const struct framebuffer *fb_console_render_target; +extern uint32_t fb_console_render_vt; extern char log_buffer[CONSOLE_LOG_CAP]; extern size_t log_len; extern size_t log_start; @@ -245,6 +279,19 @@ void fb_console_clear_row_range(uint32_t row, uint32_t first, uint32_t last); void fb_console_ansi_feed(char ch); +/** + * @brief Save the serialized renderer and partial ANSI/UTF-8 parser. + * @param state Required writable checkpoint storage owned by the caller. + */ +void fb_console_state_save(struct fb_console_state *state); + +/** + * @brief Restore a renderer checkpoint for its retained pixel surface. + * @param state Required checkpoint saved for the matching surface layout. + */ +void fb_console_state_restore(const struct fb_console_state *state); + + /** @brief Reset text state and clear the full active rendering surface. */ void fb_console_initialize_fullscreen(void); diff --git a/drivers/console/panic.c b/drivers/console/panic.c index d5305d0a..b497c3e8 100644 --- a/drivers/console/panic.c +++ b/drivers/console/panic.c @@ -65,6 +65,11 @@ struct console_panic_context console_panic_begin(void) fb_rows != active->height / RELIEFOS_FONT_H || fb_col > fb_cols || fb_row >= fb_rows; fb_console_render_target = NULL; + fb_console_render_vt = 0; + if (context.vt >= 1u && context.vt <= CONSOLE_VT_COUNT) { + struct console_vt_text *state = &vt_text[context.vt - 1u]; + state->charset[0] = state->charset[1] = state->charset_bank = 0; + } if (usable && !context.graphical) fb_console_hide_tty_cursor(); console_boot_logo_active = false; console_vt_graphical = false; diff --git a/drivers/console/render.c b/drivers/console/render.c index 65f42693..dd946a55 100644 --- a/drivers/console/render.c +++ b/drivers/console/render.c @@ -1,4 +1,5 @@ #include "internal.h" +#include "dec_graphics.h" /** * @brief Fill a rectangle on the current text-rendering surface. @@ -177,6 +178,8 @@ void fb_console_show_tty_cursor(void) uint32_t row_bytes; if (!fb_console_tty_cursor_active || fb_console_tty_cursor_visible || + (console_active_vt && console_active_vt <= CONSOLE_VT_COUNT && + vt_text[console_active_vt - 1].cursor_hidden) || !fb->available || !fb->pixels || !fb_cols || !fb_rows || fb_col >= fb_cols || fb_row >= fb_rows || !fb->bytes_per_pixel || fb->bytes_per_pixel > 4u) { @@ -324,6 +327,11 @@ static void fb_console_scalar(uint32_t ch) if (ch == 0x7fu) { return; } + uint32_t number = fb_console_render_vt ? fb_console_render_vt : console_active_vt; + if (number && number <= CONSOLE_VT_COUNT) { + const struct console_vt_text *state = &vt_text[number - 1]; + if (state->charset[state->charset_bank]) ch = console_dec_graphic(ch); + } uint32_t cells = framebuffer_codepoint_width(ch); if (fb_console_panic_overlay && cells > fb_cols) { fb_console_panic_clipped = true; diff --git a/drivers/console/vga_console.c b/drivers/console/vga_console.c index f8713584..6aee156e 100644 --- a/drivers/console/vga_console.c +++ b/drivers/console/vga_console.c @@ -101,10 +101,17 @@ void vga_console_panic_write(const char *s, size_t len, uint8_t color) vga_text_hw_present(p->cells,p->cursor.x,p->cursor.y,false,0,false); } -/** @brief Encode common full-screen-tool keys; return zero for ordinary typing. */ +/** @brief Encode xterm navigation and function keys for either console backend. + * @param vt One-based VT, 1..6. @param key Normalized Set-1 keycode. + * @param mod xterm modifier value, 1..8. @param out Writable buffer of 8 bytes. + * @return Encoded byte count, or zero for an invalid/ordinary key. + */ unsigned vga_console_key(uint32_t vt, uint8_t key, unsigned mod, char *out) { if (vt<1 || vt>6 || !out || mod<1 || mod>8) return 0; + if (key == 15 && mod == 2) { + out[0] = 27; out[1] = '['; out[2] = 'Z'; return 3; + } char final=key==72?'A':key==80?'B':key==77?'C':key==75?'D':key==71?'H':key==79?'F':0; unsigned code=key==82?2:key==83?3:key==73?5:key==81?6:0; if (key>=59 && key<=62) final=(char)('P'+key-59); @@ -116,7 +123,9 @@ unsigned vga_console_key(uint32_t vt, uint8_t key, unsigned mod, char *out) if (!final && !code) return 0; unsigned used=0;out[used++]=27; if (final && mod==1) { - out[used++]=(vga_screens[vt-1].application_cursor || (key>=59 && key<=62))?'O':'['; + bool application = console_vga_text_mode ? vga_screens[vt-1].application_cursor + : vt_text[vt-1].application_cursor; + out[used++]=(application || (key>=59 && key<=62))?'O':'['; out[used++]=final; return used; } diff --git a/drivers/console/vga_terminal.c b/drivers/console/vga_terminal.c index 64ae4e63..6c7110d3 100644 --- a/drivers/console/vga_terminal.c +++ b/drivers/console/vga_terminal.c @@ -1,4 +1,5 @@ #include "vga_terminal.h" +#include "dec_graphics.h" #include /** @brief Resolve an individual Unicode mapping from the immutable PSF1 table. */ @@ -87,24 +88,12 @@ void vga_terminal_index(struct vga_page *p, bool reverse, bool carriage) p->pending_wrap = false; } -/** @brief Map the VT100 special-graphics subset to Unicode font mappings. */ -static uint32_t vga_dec_graphic(uint32_t cp) -{ - static const uint16_t table[] = { - 0x25c6,0x2592,0x2409,0x240c,0x240d,0x240a,0x00b0,0x00b1, - 0x2424,0x240b,0x2518,0x2510,0x250c,0x2514,0x253c,0x23ba, - 0x23bb,0x2500,0x23bc,0x23bd,0x251c,0x2524,0x2534,0x252c, - 0x2502,0x2264,0x2265,0x03c0,0x2260,0x00a3,0x00b7, - }; - return cp >= 0x60 && cp <= 0x7e ? table[cp - 0x60] : cp; -} - /** @brief Place a printable scalar with delayed right-margin wrap and insertion. */ void vga_terminal_scalar(struct vga_terminal *t, uint32_t cp) { struct vga_page *p = &t->page[t->alternate]; if (p->pending_wrap && p->cursor.wrap) vga_terminal_index(p, false, true); - if (p->cursor.charset[p->cursor.g1]) cp = vga_dec_graphic(cp); + if (p->cursor.charset[p->cursor.g1]) cp = console_dec_graphic(cp); unsigned at = p->cursor.y * VGA_TEXT_COLS + p->cursor.x; if (p->insert && p->cursor.x + 1 < VGA_TEXT_COLS) __builtin_memmove(p->cells + at + 1, p->cells + at, diff --git a/drivers/console/vt.c b/drivers/console/vt.c index f1c3af4f..8418c53a 100644 --- a/drivers/console/vt.c +++ b/drivers/console/vt.c @@ -23,6 +23,93 @@ static bool console_restore_graphic_frame(uint32_t number); static uint8_t *console_graphic_saved; static size_t console_graphic_saved_bytes; static uint32_t console_graphic_saved_vt; + +struct console_vt_surface { + struct framebuffer frame; + struct fb_console_state state; + bool initialized; +}; + +static struct console_vt_surface console_vt_surfaces[CONSOLE_VT_COUNT]; +uint32_t fb_console_render_vt; + +/** + * @brief Retain a lazily allocated text surface matching the current display. + * @param number Valid one-based virtual terminal number. + * @return Its surface, or NULL when allocation or display format is unavailable. + */ +static struct console_vt_surface *fb_console_vt_surface(uint32_t number) +{ + struct console_vt_surface *surface = &console_vt_surfaces[number - 1u]; + const struct framebuffer *active = framebuffer_get(); + if (!active->available || !active->pixels) return NULL; + if (surface->frame.pixels && surface->frame.width == active->width && + surface->frame.height == active->height && surface->frame.pitch == active->pitch && + surface->frame.bpp == active->bpp && + surface->frame.red_field_position == active->red_field_position && + surface->frame.red_mask_size == active->red_mask_size && + surface->frame.green_field_position == active->green_field_position && + surface->frame.green_mask_size == active->green_mask_size && + surface->frame.blue_field_position == active->blue_field_position && + surface->frame.blue_mask_size == active->blue_mask_size) + return surface; + if (surface->frame.pixels) kernel_free(surface->frame.pixels); + *surface = (struct console_vt_surface){0}; + uint8_t *pixels; + if (!fb_console_allocate_replay_target(&surface->frame, &pixels)) return NULL; + return surface; +} + +/** + * @brief Save the visible text pixels and parser before another VT takes over. + * The caller has removed the software cursor and serializes console access. + */ +static void fb_console_save_active_text(void) +{ + if (!console_active_vt || console_vt_graphical || !fb_console_enabled) return; + struct console_vt_surface *surface = fb_console_vt_surface(console_active_vt); + if (!surface) return; + const struct framebuffer *active = framebuffer_get(); + __builtin_memcpy(surface->frame.pixels, active->pixels, + (size_t)active->pitch * active->height); + fb_console_state_save(&surface->state); + surface->initialized = true; +} + +/** + * @brief Apply background output to its own retained pixels and streaming parser. + * @param number Valid one-based destination VT number, currently off screen. + * @param bytes Output bytes already accepted by the terminal. + * @param length Number of bytes. Caller serializes console access. + */ +static void fb_console_write_background(uint32_t number, const char *bytes, size_t length) +{ + struct console_vt_surface *surface = fb_console_vt_surface(number); + if (!surface) return; + struct fb_console_state previous; + fb_console_state_save(&previous); + const struct framebuffer *previous_target = fb_console_render_target; + uint32_t previous_vt = fb_console_render_vt; + bool previous_enabled = fb_console_enabled; + fb_console_render_target = &surface->frame; + fb_console_render_vt = number; + fb_console_enabled = true; + if (surface->initialized) fb_console_state_restore(&surface->state); + else { + console_utf8 = (struct reliefos_utf8_stream){0}; + fb_console_initialize_fullscreen(); + struct console_vt_text *history = &vt_text[number - 1u]; + for (size_t i = 0; i < history->length; ++i) fb_console_ansi_feed(history->bytes[i]); + } + for (size_t i = 0; i < length; ++i) fb_console_ansi_feed(bytes[i]); + fb_console_state_save(&surface->state); + surface->initialized = true; + fb_console_render_target = previous_target; + fb_console_render_vt = previous_vt; + fb_console_enabled = previous_enabled; + fb_console_state_restore(&previous); +} + /** * @brief Allocate a same-layout RAM surface for one fullscreen text replay. * @param target Receives a copy of the active framebuffer descriptor. @@ -119,6 +206,7 @@ void console_vt_write(uint32_t number, const char *s, size_t len) if (console_vga_text_mode) { vga_console_write(number,s,len); return; } if (number == console_active_vt && !console_vt_graphical) fb_console_hide_tty_cursor(); + else fb_console_write_background(number, s, len); struct console_vt_text *screen = &vt_text[number - 1u]; for (size_t i = 0; i < len; ++i) { if (screen->length == CONSOLE_VT_TEXT_CAP) { @@ -168,6 +256,7 @@ void console_vt_activate(uint32_t number, bool graphical) } if (console_vt_graphical && !graphical) console_save_graphic_frame(); fb_console_hide_tty_cursor(); + if (!preserve_text) fb_console_save_active_text(); console_active_vt = number; console_vt_graphical = graphical; console_service_logs_only = true; @@ -186,32 +275,32 @@ void console_vt_activate(uint32_t number, bool graphical) console_present(); return; } - struct framebuffer replay_target; - uint8_t *replay_pixels = NULL; - bool replay_staged = fb_console_allocate_replay_target(&replay_target, - &replay_pixels); - if (replay_staged) { - fb_console_render_target = &replay_target; + struct console_vt_surface *surface = fb_console_vt_surface(number); + if (surface) fb_console_render_target = &surface->frame; + if (surface && surface->initialized) fb_console_state_restore(&surface->state); + else { + console_utf8 = (struct reliefos_utf8_stream){0}; + fb_console_initialize_fullscreen(); + struct console_vt_text *screen = &vt_text[number - 1u]; + for (size_t i = 0; i < screen->length; ++i) fb_console_ansi_feed(screen->bytes[i]); } - fb_console_initialize_fullscreen(); - struct console_vt_text *screen = &vt_text[number - 1u]; - for (size_t i = 0; i < screen->length; ++i) fb_console_ansi_feed(screen->bytes[i]); - console_present(); - if (replay_staged) { + if (surface) { + fb_console_state_save(&surface->state); + surface->initialized = true; const struct framebuffer *active = framebuffer_get(); size_t visible_row_bytes = (size_t)active->width * active->bytes_per_pixel; for (uint32_t row = 0; row < active->height; ++row) { uint8_t *destination = (uint8_t *)active->pixels + (uint64_t)row * active->pitch; - const uint8_t *source = replay_pixels + - (uint64_t)row * replay_target.pitch; + const uint8_t *source = (const uint8_t *)surface->frame.pixels + + (uint64_t)row * surface->frame.pitch; __builtin_memcpy(destination, source, visible_row_bytes); } fb_console_render_target = NULL; - framebuffer_present(); - kernel_free(replay_pixels); } + console_present(); + framebuffer_present(); } /** @brief Leave a failed VGA transition uncommitted by the VT ownership layer. */ diff --git a/drivers/tty/pty/keyboard.c b/drivers/tty/pty/keyboard.c index 14006058..19ab4bbe 100644 --- a/drivers/tty/pty/keyboard.c +++ b/drivers/tty/pty/keyboard.c @@ -231,8 +231,8 @@ void pty_console_key_event(uint8_t keycode, uint8_t pressed) return; } if (!pressed) return; - length=console_vga_text_mode ? vga_console_key(pty_id,keycode, - 1u+(console_shift_down?1u:0u)+(console_alt_down?2u:0u)+(console_ctrl_down?4u:0u),bytes) : 0; + length=vga_console_key(pty_id,keycode, + 1u+(console_shift_down?1u:0u)+(console_alt_down?2u:0u)+(console_ctrl_down?4u:0u),bytes); if (!length && !console_key_to_bytes(keycode,bytes,&length)) return; /* Feed through the same canonical/ISIG handling used by PTY hosts. */ (void)pty_write_input(session->owner_pid, pty_id, bytes, length); diff --git a/include/reliefnt/console.h b/include/reliefnt/console.h index b42b67f4..9aed1a95 100644 --- a/include/reliefnt/console.h +++ b/include/reliefnt/console.h @@ -45,7 +45,7 @@ int console_prepare_vt(bool graphical); /** @brief Configure a requested VGA backend before creating fixed terminal sessions. * @param cmdline Boot command line. @param legacy_boot Non-EFI firmware handoff. */ void vga_console_configure(const char *cmdline, bool legacy_boot); -/** @brief Encode VGA terminal navigation/function keys using its DEC cursor mode. +/** @brief Encode navigation/function keys using either backend's DEC cursor mode. * @param vt Fixed console 1..6. @param keycode Normalized keyboard make code. * @param modifiers xterm modifier value, 1..8. @param bytes Eight writable bytes. * @return Encoded length, or zero to use the unchanged ordinary key translator. diff --git a/tools/tests/panic_console_runtime_test.c b/tools/tests/panic_console_runtime_test.c index f2e1a9bc..509f8cc1 100644 --- a/tools/tests/panic_console_runtime_test.c +++ b/tools/tests/panic_console_runtime_test.c @@ -71,8 +71,13 @@ static void scenario(uint8_t bytes,bool graphical) framebuffer_drm_scanout_owned=true; } unsigned old_alloc=allocations,old_locks=locking; + fb_console_render_vt=3; + vt_text[console_active_vt-1].charset[0]=1; + vt_text[console_active_vt-1].charset_bank=0; panic_phase=true; struct console_panic_context context=console_panic_begin(); + CHECK(fb_console_render_vt==0); + CHECK(vt_text[context.vt-1].charset[0]==0); CHECK(context.vt==(graphical?1u:2u)); CHECK(context.graphical==graphical); CHECK(console_active_vt==(graphical?1u:2u)); diff --git a/tools/tests/vt_console_test.c b/tools/tests/vt_console_test.c index 7f1e9ff0..5bb5f2c9 100644 --- a/tools/tests/vt_console_test.c +++ b/tools/tests/vt_console_test.c @@ -11,6 +11,7 @@ void kernel_free(void *ptr) { free(ptr); } static uint32_t pixels[80 * 48]; static uint32_t full_clear_count; static uint32_t text_present_count; +static uint32_t last_codepoint; static char serial_output[16384]; static size_t serial_length; static const struct framebuffer display = { @@ -44,10 +45,10 @@ void framebuffer_present_region(uint32_t x, uint32_t y, uint32_t w, uint32_t h) { (void)x; (void)y; (void)w; (void)h; ++text_present_count; } uint32_t framebuffer_codepoint_width(uint32_t c) { (void)c; return 1; } void framebuffer_codepoint(uint32_t x, uint32_t y, uint32_t c, uint32_t fg, uint32_t bg) -{ (void)c; (void)bg; framebuffer_rect(x, y, 8, 16, fg); } +{ last_codepoint = c; (void)bg; framebuffer_rect(x, y, 8, 16, fg); } void framebuffer_codepoint_to(const struct framebuffer *target, uint32_t x, uint32_t y, uint32_t c, uint32_t fg, uint32_t bg) -{ (void)c; (void)bg; framebuffer_rect_to(target, x, y, 8, 16, fg); } +{ last_codepoint = c; (void)bg; framebuffer_rect_to(target, x, y, 8, 16, fg); } void serial_write(const char *text) { size_t length = strlen(text); @@ -139,6 +140,52 @@ int main(void) assert(strstr(vt_text[0].bytes, openrc_output) != NULL); assert(strstr(vt_text[0].bytes, service_output) != NULL); assert(strstr(vt_text[0].bytes, graphical_output) != NULL); - puts("VT boot continuity, terminal/diagnostic separation, serial mirror, graphics isolation and text cursor passed"); + char key[8]; + console_vt_activate(2, false); + console_vt_write(2, "\033[?1h", sizeof("\033[?1h") - 1); + assert(vga_console_key(2, 72, 1, key) == 3 && !memcmp(key, "\033OA", 3)); + assert(vga_console_key(1, 72, 1, key) == 3 && !memcmp(key, "\033[A", 3)); + assert(vga_console_key(2, 61, 1, key) == 3 && !memcmp(key, "\033OR", 3)); + assert(vga_console_key(2, 15, 2, key) == 3 && !memcmp(key, "\033[Z", 3)); + console_vt_write(2, "\033[?1l", sizeof("\033[?1l") - 1); + assert(vga_console_key(2, 72, 1, key) == 3 && !memcmp(key, "\033[A", 3)); + console_vt_write(2, "\033(0q", sizeof("\033(0q") - 1); + assert(last_codepoint == 0x2500); + console_vt_write(2, "\033(Bq", sizeof("\033(Bq") - 1); + assert(last_codepoint == 'q'); + console_vt_write(2, "\033)0\016x", sizeof("\033)0\016x") - 1); + assert(last_codepoint == 0x2502); + console_vt_write(2, "\017x", sizeof("\017x") - 1); + assert(last_codepoint == 'x'); + console_vt_write(2, "\033[?25l", sizeof("\033[?25l") - 1); + assert(!fb_console_tty_cursor_visible); + console_vt_write(2, "\033[?25h", sizeof("\033[?25h") - 1); + assert(fb_console_tty_cursor_visible); + static const char full_screen[] = "\033[?25l\033[44m\033[2J\033[H"; + console_vt_write(2, full_screen, sizeof(full_screen) - 1); + for (unsigned i = 0; i < CONSOLE_VT_TEXT_CAP / 4u + 100u; ++i) + console_vt_write(2, "\033[Hq", 4); + memcpy(before, pixels, sizeof(pixels)); + uint32_t foreground = fb_console_fg_color; + uint32_t background = fb_console_bg_color; + console_vt_activate(3, false); + console_vt_activate(2, false); + assert(memcmp(before, pixels, sizeof(pixels)) == 0); + assert(fb_console_fg_color == foreground && fb_console_bg_color == background); + console_vt_activate(3, false); + console_vt_write(2, "\033[42", 4); + console_vt_write(3, "\033[41m", 5); + console_vt_write(2, "m\033[2J\033[H", 8); + for (unsigned i = 0; i < CONSOLE_VT_TEXT_CAP / 4u + 100u; ++i) + console_vt_write(2, "\033[Hq", 4); + console_vt_activate(2, false); + assert(pixels[79 + 47 * 80] == 0x006fc77au); + assert(fb_console_bg_color == 0x006fc77au); + console_vt_write(2, "\xe4", 1); + console_vt_activate(3, false); + console_vt_write(3, "x", 1); + console_vt_write(2, "\xb8\xad", 2); + assert(last_codepoint == 0x4e2d); + puts("VT continuity, isolation, framebuffer ncurses keys, DEC graphics and cursor modes passed"); return 0; } From 4182d027e9ff5316db46df283e36fe37097492dd Mon Sep 17 00:00:00 2001 From: VasilyZa <450270247@qq.com> Date: Sat, 14 Nov 2026 23:00:34 +0800 Subject: [PATCH 2/2] fix(console): preserve text when backspace moves the cursor --- drivers/console/render.c | 4 +--- tools/tests/vt_console_test.c | 44 +++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/drivers/console/render.c b/drivers/console/render.c index dd946a55..23f4d5a3 100644 --- a/drivers/console/render.c +++ b/drivers/console/render.c @@ -306,11 +306,9 @@ static void fb_console_scalar(uint32_t ch) return; } if (ch == '\b') { + /* Readline uses BS to move across existing text. */ if (fb_col) { --fb_col; - fb_console_rect(fb_x + fb_col * RELIEFOS_FONT_W, - fb_y + fb_row * RELIEFOS_FONT_H, - RELIEFOS_FONT_W, RELIEFOS_FONT_H, fb_console_bg_color); } return; } diff --git a/tools/tests/vt_console_test.c b/tools/tests/vt_console_test.c index 5bb5f2c9..13bf6aa6 100644 --- a/tools/tests/vt_console_test.c +++ b/tools/tests/vt_console_test.c @@ -45,10 +45,10 @@ void framebuffer_present_region(uint32_t x, uint32_t y, uint32_t w, uint32_t h) { (void)x; (void)y; (void)w; (void)h; ++text_present_count; } uint32_t framebuffer_codepoint_width(uint32_t c) { (void)c; return 1; } void framebuffer_codepoint(uint32_t x, uint32_t y, uint32_t c, uint32_t fg, uint32_t bg) -{ last_codepoint = c; (void)bg; framebuffer_rect(x, y, 8, 16, fg); } +{ last_codepoint = c; framebuffer_rect(x, y, 8, 16, c == ' ' ? bg : fg); } void framebuffer_codepoint_to(const struct framebuffer *target, uint32_t x, uint32_t y, uint32_t c, uint32_t fg, uint32_t bg) -{ last_codepoint = c; (void)bg; framebuffer_rect_to(target, x, y, 8, 16, fg); } +{ last_codepoint = c; framebuffer_rect_to(target, x, y, 8, 16, c == ' ' ? bg : fg); } void serial_write(const char *text) { size_t length = strlen(text); @@ -64,6 +64,45 @@ uint32_t smp_current_cpu(void) { return 0; } uint32_t smp_cpu_count(void) { return 1; } void smp_membarrier_poll(void) {} +/** @brief Keep text intact during BS motion and erase only explicitly overwritten cells. */ +static void test_backspace_preserves_text(void) +{ + static const char text[] = "\033[?25l\033[0m\033[2J\033[HABCDE"; + uint32_t saved[80 * 48]; + console_vt_write(2, text, sizeof(text) - 1); + memcpy(saved, pixels, sizeof(saved)); + console_vt_write(2, "\b\b", 2); + assert(fb_col == 3 && fb_row == 0); + assert(memcmp(saved, pixels, sizeof(saved)) == 0); + console_vt_write(2, "\r\b", 2); + assert(fb_col == 0 && fb_row == 0); + assert(memcmp(saved, pixels, sizeof(saved)) == 0); + + /* Canonical erase explicitly writes a space over E, then moves back. */ + static const char erase[] = "\033[6G\b \b"; + console_vt_write(2, erase, sizeof(erase) - 1); + assert(fb_col == 4 && fb_row == 0); + for (unsigned y = 0; y < 16; ++y) + for (unsigned x = 32; x < 40; ++x) saved[y * 80 + x] = 0; + assert(memcmp(saved, pixels, sizeof(saved)) == 0); + + console_vt_write(2, text, sizeof(text) - 1); + memcpy(saved, pixels, sizeof(saved)); + console_vt_write(2, "\033[?25h", sizeof("\033[?25h") - 1); + console_vt_write(2, "\b", 1); + assert(fb_col == 4 && fb_console_tty_cursor_visible); + fb_console_hide_tty_cursor(); + assert(memcmp(saved, pixels, sizeof(saved)) == 0); + + console_vt_activate(3, false); + console_vt_write(2, "\b", 1); + console_vt_activate(2, false); + assert(fb_col == 3); + fb_console_hide_tty_cursor(); + assert(memcmp(saved, pixels, sizeof(saved)) == 0); + puts("BS cursor motion preserves foreground/background text, first-column bounds and explicit erase"); +} + int main(void) { console_log_configure("logcolor=0"); @@ -161,6 +200,7 @@ int main(void) assert(!fb_console_tty_cursor_visible); console_vt_write(2, "\033[?25h", sizeof("\033[?25h") - 1); assert(fb_console_tty_cursor_visible); + test_backspace_preserves_text(); static const char full_screen[] = "\033[?25l\033[44m\033[2J\033[H"; console_vt_write(2, full_screen, sizeof(full_screen) - 1); for (unsigned i = 0; i < CONSOLE_VT_TEXT_CAP / 4u + 100u; ++i)