Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 103 additions & 4 deletions drivers/console/ansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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') {
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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') {
Expand All @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions drivers/console/dec_graphics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef RELIEFNT_CONSOLE_DEC_GRAPHICS_H
#define RELIEFNT_CONSOLE_DEC_GRAPHICS_H
#include <reliefnt/types.h>

/** @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
47 changes: 47 additions & 0 deletions drivers/console/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions drivers/console/panic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions drivers/console/render.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "internal.h"
#include "dec_graphics.h"

/**
* @brief Fill a rectangle on the current text-rendering surface.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -303,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;
}
Expand All @@ -324,6 +325,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;
Expand Down
13 changes: 11 additions & 2 deletions drivers/console/vga_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
15 changes: 2 additions & 13 deletions drivers/console/vga_terminal.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "vga_terminal.h"
#include "dec_graphics.h"
#include <reliefos/psf_font.h>

/** @brief Resolve an individual Unicode mapping from the immutable PSF1 table. */
Expand Down Expand Up @@ -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,
Expand Down
Loading
Loading