2021-05-31 04:47:02 +03:00
|
|
|
/**
|
|
|
|
* @file kernel/misc/fbterm.c
|
|
|
|
* @brief Crude framebuffer terminal for 32bpp framebuffer devices.
|
|
|
|
*
|
|
|
|
* Provides a simple graphical text renderer for early startup, with
|
|
|
|
* support for simple escape sequences, on top of a framebuffer set up
|
|
|
|
* with the `lfbvideo` module.
|
2021-11-26 05:31:10 +03:00
|
|
|
*
|
|
|
|
* @copyright
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2021 K. Lange
|
2021-05-31 04:47:02 +03:00
|
|
|
*/
|
|
|
|
#include <kernel/printf.h>
|
|
|
|
#include <kernel/string.h>
|
|
|
|
#include <kernel/args.h>
|
2021-10-20 12:49:07 +03:00
|
|
|
#include <kernel/mmu.h>
|
2021-05-31 04:47:02 +03:00
|
|
|
|
|
|
|
/* Whether to scroll or wrap when cursor reaches the bottom. */
|
2021-10-20 13:39:47 +03:00
|
|
|
static int fbterm_scroll = 0;
|
2021-10-20 12:49:07 +03:00
|
|
|
static void (*write_char)(int, int, int, uint32_t) = NULL;
|
|
|
|
static int (*get_width)(void) = NULL;
|
|
|
|
static int (*get_height)(void) = NULL;
|
|
|
|
static void (*scroll_terminal)(void) = NULL;
|
|
|
|
|
|
|
|
static int x = 0;
|
|
|
|
static int y = 0;
|
|
|
|
static int term_state = 0;
|
|
|
|
static char term_buf[1024] = {0};
|
|
|
|
static int term_buf_c = 0;
|
2021-05-31 04:47:02 +03:00
|
|
|
|
|
|
|
/* Is this in a header somewhere? */
|
|
|
|
extern uint8_t * lfb_vid_memory;
|
|
|
|
extern uint16_t lfb_resolution_x;
|
|
|
|
extern uint16_t lfb_resolution_y;
|
|
|
|
extern uint16_t lfb_resolution_b;
|
|
|
|
extern uint32_t lfb_resolution_s;
|
|
|
|
extern size_t lfb_memsize;
|
|
|
|
|
|
|
|
/* Bitmap font details */
|
|
|
|
#include "../../apps/terminal-font.h"
|
2021-07-22 11:06:43 +03:00
|
|
|
#define char_height LARGE_FONT_CELL_HEIGHT
|
|
|
|
#define char_width LARGE_FONT_CELL_WIDTH
|
2021-05-31 04:47:02 +03:00
|
|
|
|
|
|
|
/* Default colors */
|
2021-10-26 09:58:23 +03:00
|
|
|
#define BG_COLOR 0xFF000000 /* Background */
|
2021-05-31 04:47:02 +03:00
|
|
|
#define FG_COLOR 0xFFCCCCCC /* Main text color */
|
|
|
|
|
|
|
|
static uint32_t fg_color = FG_COLOR;
|
|
|
|
static uint32_t bg_color = BG_COLOR;
|
|
|
|
|
2021-06-03 15:00:20 +03:00
|
|
|
extern uint32_t lfb_resolution_s;
|
|
|
|
|
2021-10-19 14:29:01 +03:00
|
|
|
static inline void set_point(int x, int y, uint32_t value) {
|
2021-10-25 07:50:08 +03:00
|
|
|
if (lfb_resolution_b == 32) {
|
|
|
|
((uint32_t*)lfb_vid_memory)[y * (lfb_resolution_s/4) + x] = value;
|
2022-02-16 06:38:51 +03:00
|
|
|
#ifdef __aarch64__
|
|
|
|
/* TODO just map it uncached in the first place... */
|
2022-02-16 06:36:03 +03:00
|
|
|
asm volatile ("dc cvac, %0\n" :: "r"((uintptr_t)&((uint32_t*)lfb_vid_memory)[y * (lfb_resolution_s/4) + x]) : "memory");
|
2022-02-16 06:38:51 +03:00
|
|
|
#endif
|
2021-10-25 07:50:08 +03:00
|
|
|
} else if (lfb_resolution_b == 24) {
|
|
|
|
lfb_vid_memory[y * lfb_resolution_s + x * 3 + 0] = (value >> 0) & 0xFF;
|
|
|
|
lfb_vid_memory[y * lfb_resolution_s + x * 3 + 1] = (value >> 8) & 0xFF;
|
|
|
|
lfb_vid_memory[y * lfb_resolution_s + x * 3 + 2] = (value >> 16) & 0xFF;
|
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
}
|
|
|
|
|
2021-10-20 12:49:07 +03:00
|
|
|
static void fb_write_char(int _x, int _y, int val, uint32_t color) {
|
2021-05-31 04:47:02 +03:00
|
|
|
if (val > 128) {
|
|
|
|
val = 4;
|
|
|
|
}
|
2021-10-20 12:49:07 +03:00
|
|
|
|
|
|
|
int x = 1 + _x * char_width;
|
|
|
|
int y = _y * char_height;
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
uint16_t * c = large_font[val];
|
|
|
|
for (uint8_t i = 0; i < char_height; ++i) {
|
|
|
|
for (uint8_t j = 0; j < char_width; ++j) {
|
2021-07-22 11:06:43 +03:00
|
|
|
if (c[i] & (1 << (LARGE_FONT_MASK-j))) {
|
2021-05-31 04:47:02 +03:00
|
|
|
set_point(x+j,y+i,color);
|
|
|
|
} else {
|
|
|
|
set_point(x+j,y+i,bg_color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Basic 16-color ANSI palette with Tango colors.
|
|
|
|
*/
|
|
|
|
static uint32_t term_colors[] = {
|
|
|
|
0xFF000000,
|
|
|
|
0xFFCC0000,
|
|
|
|
0xFF4E9A06,
|
|
|
|
0xFFC4A000,
|
|
|
|
0xFF3465A4,
|
|
|
|
0xFF75507B,
|
|
|
|
0xFF06989A,
|
|
|
|
0xFFD3D7CF,
|
|
|
|
|
|
|
|
0xFF555753,
|
|
|
|
0xFFEF2929,
|
|
|
|
0xFF8AE234,
|
|
|
|
0xFFFCE94F,
|
|
|
|
0xFF729FCF,
|
|
|
|
0xFFAD7FA8,
|
|
|
|
0xFF34E2E2,
|
|
|
|
0xFFEEEEEC,
|
|
|
|
};
|
|
|
|
|
2021-10-20 12:49:07 +03:00
|
|
|
static int fb_get_width(void) {
|
|
|
|
return (lfb_resolution_x - 1) / char_width;
|
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
|
2021-10-20 12:49:07 +03:00
|
|
|
static int fb_get_height(void) {
|
|
|
|
return lfb_resolution_y / char_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fb_scroll_terminal(void) {
|
|
|
|
memmove(lfb_vid_memory, lfb_vid_memory + sizeof(uint32_t) * lfb_resolution_x * char_height, (lfb_resolution_y - char_height) * lfb_resolution_x * 4);
|
2021-10-26 09:58:23 +03:00
|
|
|
memset(lfb_vid_memory + sizeof(uint32_t) * (lfb_resolution_y - char_height) * lfb_resolution_x, 0x00, char_height * lfb_resolution_x * 4);
|
2021-10-20 12:49:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_square(int x, int y) {
|
2021-10-26 09:58:23 +03:00
|
|
|
int center_x = lfb_resolution_x / 2;
|
|
|
|
int center_y = lfb_resolution_y / 2;
|
2021-10-20 12:49:07 +03:00
|
|
|
for (size_t _y = 0; _y < 7; ++_y) {
|
2021-10-26 09:58:23 +03:00
|
|
|
uint32_t color = 0xFF00B2FF - (y * 8 + _y) * 0x200;
|
2021-10-20 12:49:07 +03:00
|
|
|
for (size_t _x = 0; _x < 7; ++_x) {
|
2021-10-26 09:58:23 +03:00
|
|
|
set_point(center_x - 32 + x * 8 + _x, center_y - 32 + y * 8 + _y, color);
|
2021-10-20 12:49:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fbterm_draw_logo(void) {
|
|
|
|
uint64_t logo_squares = 0x981818181818FFFFUL;
|
|
|
|
for (size_t y = 0; y < 8; ++y) {
|
|
|
|
for (size_t x = 0; x < 8; ++x) {
|
|
|
|
if (logo_squares & (1 << x)) {
|
|
|
|
draw_square(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logo_squares >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fbterm_init_framebuffer(void) {
|
|
|
|
write_char = fb_write_char;
|
|
|
|
get_width = fb_get_width;
|
|
|
|
get_height = fb_get_height;
|
|
|
|
scroll_terminal = fb_scroll_terminal;
|
|
|
|
fbterm_draw_logo();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ega_write_char(int x, int y, int ch, uint32_t color) {
|
|
|
|
unsigned short att = 7 << 8;
|
|
|
|
unsigned short *where = (unsigned short*)(mmu_map_from_physical(0xB8000)) + (y * 80 + x);
|
|
|
|
*where = (ch & 0xFF) | att;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ega_get_width(void) { return 80; }
|
|
|
|
static int ega_get_height(void) { return 25; }
|
|
|
|
|
|
|
|
static void ega_scroll_terminal(void) {
|
|
|
|
memmove(mmu_map_from_physical(0xB8000), (char*)mmu_map_from_physical(0xB8000) + sizeof(unsigned short) * 80, sizeof(unsigned short) * (80 * 24));
|
|
|
|
memset((char*)mmu_map_from_physical(0xB8000) + sizeof(unsigned short) * (80 * 24), 0x00, 80 * sizeof(unsigned short));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fbterm_init_ega(void) {
|
|
|
|
write_char = ega_write_char;
|
|
|
|
get_width = ega_get_width;
|
|
|
|
get_height = ega_get_height;
|
|
|
|
scroll_terminal = ega_scroll_terminal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cursor_update(void) {
|
|
|
|
if (x >= get_width()) {
|
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
if (y >= get_height()) {
|
|
|
|
if (fbterm_scroll) {
|
|
|
|
y--;
|
|
|
|
scroll_terminal();
|
|
|
|
} else {
|
|
|
|
y = 0;
|
2021-05-31 04:47:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void process_char(char ch) {
|
|
|
|
if (term_state == 1) {
|
|
|
|
if (ch == '[') {
|
|
|
|
term_buf_c = 0;
|
|
|
|
term_buf[term_buf_c] = '\0';
|
|
|
|
term_state = 2;
|
|
|
|
} else {
|
|
|
|
term_state = 0;
|
|
|
|
process_char(ch);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (term_state == 2) {
|
|
|
|
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
|
|
|
|
/* do the thing */
|
|
|
|
switch (ch) {
|
|
|
|
case 'm': {
|
|
|
|
char * arg = &term_buf[0];
|
|
|
|
char * next;
|
|
|
|
int argC = 0;
|
|
|
|
int isBold = 0;
|
|
|
|
do {
|
|
|
|
next = strchr(arg, ';');
|
|
|
|
if (next) { *next = '\0'; next++; }
|
|
|
|
int asInt = atoi(arg);
|
|
|
|
if (asInt == 0) {
|
|
|
|
fg_color = FG_COLOR;
|
|
|
|
bg_color = BG_COLOR;
|
|
|
|
isBold = 0;
|
|
|
|
} else if (asInt == 1) {
|
|
|
|
isBold = 1;
|
|
|
|
} else if (asInt == 22) {
|
|
|
|
fg_color = FG_COLOR;
|
|
|
|
isBold = 0;
|
|
|
|
} else if (asInt >= 30 && asInt <= 37) {
|
|
|
|
fg_color = term_colors[asInt-30 + (isBold ? 8 : 0)];
|
|
|
|
} else if (asInt >= 90 && asInt <= 97) {
|
|
|
|
fg_color = term_colors[asInt-90 + 8];
|
|
|
|
} else if (asInt >= 40 && asInt <= 47) {
|
|
|
|
bg_color = term_colors[asInt-40 + (isBold ? 8 : 0)];
|
|
|
|
} else if (asInt >= 100 && asInt <= 107) {
|
|
|
|
bg_color = term_colors[asInt-100 + 8];
|
|
|
|
} else if (asInt == 38) {
|
|
|
|
fg_color = FG_COLOR;
|
|
|
|
} else if (asInt == 48) {
|
|
|
|
bg_color = BG_COLOR;
|
|
|
|
} else if (asInt == 7) {
|
|
|
|
uint32_t tmp = fg_color;
|
|
|
|
fg_color = bg_color;
|
|
|
|
bg_color = tmp;
|
|
|
|
}
|
|
|
|
arg = next;
|
|
|
|
argC++;
|
|
|
|
} while (arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'G': {
|
|
|
|
/* Set cursor column */
|
2021-10-20 12:49:07 +03:00
|
|
|
x = atoi(term_buf) - 1;
|
2021-05-31 04:47:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'K': {
|
|
|
|
if (atoi(term_buf) == 0) {
|
2021-10-20 12:49:07 +03:00
|
|
|
for (int i = x; i < get_width(); ++i) {
|
|
|
|
write_char(i,y,' ',bg_color);
|
2021-05-31 04:47:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
term_state = 0;
|
|
|
|
} else {
|
|
|
|
term_buf[term_buf_c++] = ch;
|
|
|
|
term_buf[term_buf_c] = '\0';
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (ch == '\033') {
|
|
|
|
term_state = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_char(x,y,' ',bg_color);
|
|
|
|
switch (ch) {
|
|
|
|
case '\n':
|
2021-10-20 12:49:07 +03:00
|
|
|
x = 0;
|
|
|
|
y++;
|
2021-05-31 04:47:02 +03:00
|
|
|
break;
|
|
|
|
case '\r':
|
2021-10-20 12:49:07 +03:00
|
|
|
x = 0;
|
2021-05-31 04:47:02 +03:00
|
|
|
break;
|
|
|
|
case '\b':
|
2021-10-20 12:49:07 +03:00
|
|
|
if (x) {
|
|
|
|
x--;
|
2021-05-31 04:47:02 +03:00
|
|
|
write_char(x,y,' ',fg_color);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ((unsigned int)ch > 127) return;
|
|
|
|
write_char(x,y,ch,fg_color);
|
2021-10-20 12:49:07 +03:00
|
|
|
x++;
|
2021-05-31 04:47:02 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-10-20 12:49:07 +03:00
|
|
|
cursor_update();
|
2021-05-31 04:47:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t (*previous_writer)(size_t,uint8_t*) = NULL;
|
|
|
|
|
|
|
|
size_t fbterm_write(size_t size, uint8_t *buffer) {
|
|
|
|
if (!buffer) return 0;
|
|
|
|
for (unsigned int i = 0; i < size; ++i) {
|
|
|
|
process_char(buffer[i]);
|
|
|
|
}
|
2022-02-16 06:36:03 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
if (previous_writer) previous_writer(size,buffer);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fbterm_initialize(void) {
|
2021-10-20 12:49:07 +03:00
|
|
|
if (lfb_resolution_x) {
|
2021-10-20 13:39:47 +03:00
|
|
|
if (args_present("fbterm-scroll")) {
|
|
|
|
fbterm_scroll = 1;
|
|
|
|
}
|
2021-10-20 12:49:07 +03:00
|
|
|
fbterm_init_framebuffer();
|
|
|
|
} else {
|
|
|
|
#ifdef __x86_64__
|
2021-10-20 13:39:47 +03:00
|
|
|
fbterm_scroll = 1;
|
2021-10-20 12:49:07 +03:00
|
|
|
fbterm_init_ega();
|
|
|
|
#else
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
2021-10-19 14:29:01 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
previous_writer = printf_output;
|
|
|
|
printf_output = fbterm_write;
|
2021-10-20 12:49:07 +03:00
|
|
|
console_set_output(fbterm_write);
|
2021-10-19 14:29:01 +03:00
|
|
|
|
|
|
|
dprintf("fbterm: Generic framebuffer text output enabled.\n");
|
2021-05-31 04:47:02 +03:00
|
|
|
}
|