Added a very hacky text UI

This commit is contained in:
Jozef Nagy 2025-05-20 19:24:16 +02:00
parent 06fb67beca
commit dae7b9c869
Signed by untrusted user who does not match committer: crz
GPG key ID: 459A4811CEAC7068
16 changed files with 296 additions and 33783 deletions

View file

@ -20,9 +20,11 @@
#include <config/config.h>
#include <lib/string.h>
#include <ui/framebuffer.h>
#include <ui/keyboard.h>
#include <ui/mouse.h>
#include <ui/font.h>
#include <ui/ui.h>
#include <loader/loader.h>
#include <time/dt.h>
#include <i18n.h>
#include <axboot.h>
@ -30,6 +32,18 @@
#include <print.h>
#include <stdint.h>
enum {
EVENT_TIME = (1 << 0),
EVENT_TIMEOUT = (1 << 1),
EVENT_MOUSE = (1 << 2),
EVENT_NEXT_ENTRY = (1 << 3),
EVENT_PREV_ENTRY = (1 << 4),
EVENT_NEXT_TAB = (1 << 5),
EVENT_LANG_CHANGE = (1 << 6),
};
struct datetime last_dt;
bool gui_init(struct ui_context *ctx)
@ -47,37 +61,72 @@ bool tui_init(struct ui_context *ctx)
return false;
}
int w;
int width = ctx->fb_modes[ctx->current_mode].width;
int height = ctx->fb_modes[ctx->current_mode].height;
int width_center = width / 2;
int height_center = height / 2;
int w, h;
// display string at the top center of the screen
char *top_string = BOOTLOADER_NAME_STR " v" BOOTLOADER_VERSION_STR;
ssfn_bbox(&(ctx->font), top_string, &w, NULL, NULL, NULL);
terminal_setcur(ctx, ctx->fb_modes[ctx->current_mode].width / 2 - (w / 2), ctx->fb_modes[ctx->current_mode].height / 32);
terminal_setcur(ctx, width_center - (w / 2), height / 32);
terminal_print(ctx, top_string);
// display current language
char lang_str[64];
snprintf((char *)&lang_str, 64, "%s: %s", i18n_curlang->lang->language, i18n_curlang->name);
ssfn_bbox(&(ctx->font), (char *)lang_str, &w, &h, NULL, NULL);
terminal_setcur(ctx, width - w - ctx->font.size, height - h);
terminal_print(ctx, (char *)lang_str);
// display all entries
int entry_count = config_get_entry_count();
int entry_height_offset = height_center - ((h * entry_count) / 2);
struct axboot_entry *entries = config_get_entries();
for (int i = 0; i < entry_count; i++) {
debug("tui_init(): Drawing entries... (%u/%u)\n", i, entry_count);
ssfn_bbox(&(ctx->font), entries[i].name, &w, &h, NULL, NULL);
terminal_setcur(ctx, width_center - (w/2), entry_height_offset + (i * h));
terminal_print(ctx, entries[i].name);
}
// highlight default entry
int default_entry = ctx->current_selection;
char final_selent[128];
int f_w, f_h;
ssfn_bbox(&(ctx->font), " ", &f_w, &f_h, NULL, NULL);
snprintf((char *)&final_selent, 128, "> %s <", entries[default_entry].name);
ssfn_bbox(&(ctx->font), entries[default_entry].name, &w, &h, NULL, NULL);
terminal_setcur(ctx, width_center - (w/2) - (f_w*2), entry_height_offset + (default_entry * h));
terminal_print(ctx, "> ");
terminal_setcur(ctx, width_center + (w/2), entry_height_offset + (default_entry * h));
terminal_print(ctx, " <");
return true;
}
void gui_draw(struct ui_context *ctx, void *mouse_status, void *event)
void gui_draw(struct ui_context *ctx, struct datetime *dt, struct language_selection *newlang, struct mouse_event *mouse_status, uint8_t event)
{
(void)ctx;
(void)dt;
(void)newlang;
(void)mouse_status;
(void)event;
}
void tui_draw(struct ui_context *ctx, void *mouse_status, void *event)
void tui_draw(struct ui_context *ctx, struct datetime *dt, struct language_selection *newlang, struct mouse_event *mouse_status, uint8_t event)
{
(void)mouse_status;
(void)event;
// display the current date and time at the bottom left corner of the screen
int w, h;
struct datetime dt;
get_datetime(&dt);
if (memcmp(&dt, &last_dt, sizeof(struct datetime)) != 0) {
if (event & EVENT_TIME) {
int w, h;
char dt_str[20] = {0}; // YYYY/mm/dd HH:MM:SS
snprintf((char *)&dt_str, 20, "%.4u/%.2u/%.2u %.2u:%.2u:%.2u", dt.year, dt.month, dt.day, dt.h, dt.m, dt.s);
snprintf((char *)&dt_str, 20, "%.4u/%.2u/%.2u %.2u:%.2u:%.2u", dt->year, dt->month, dt->day, dt->h, dt->m, dt->s);
ssfn_bbox(&(ctx->font), (char *)dt_str, &w, &h, NULL, NULL);
@ -89,7 +138,44 @@ void tui_draw(struct ui_context *ctx, void *mouse_status, void *event)
terminal_setcur(ctx, 0, ctx->fb_modes[ctx->current_mode].height - h);
terminal_print(ctx, (char *)dt_str);
last_dt = dt;
// update last datetime
last_dt = *dt;
}
// timeout update
if (event & EVENT_TIMEOUT) {
// TODO
}
// select next entry
if (event & EVENT_NEXT_ENTRY) {
// TODO
}
// select previous entry
if (event & EVENT_PREV_ENTRY) {
// TODO
}
// update language
if (event & EVENT_LANG_CHANGE) {
int w, h;
char curlang_str[64];
snprintf((char *)&curlang_str, 64, "%s: %s", i18n_curlang->lang->language);
ssfn_bbox(&(ctx->font), (char *)curlang_str, &w, &h, NULL, NULL);
for (uint32_t y = ctx->fb_modes[ctx->current_mode].height - (2*h); y < ctx->fb_modes[ctx->current_mode].height - h; y++) {
for (int x = 0; x < w; x++) {
*((uint32_t *)ctx->fb_addr + (ctx->fb_modes[ctx->current_mode].pitch / ctx->fb_modes[ctx->current_mode].bpp) * y + x) = 0xFF000000;
}
}
i18n_curlang = newlang;
char newlang_str[64];
snprintf((char *)&curlang_str, 64, "%s: %s", i18n_curlang->lang->language);
ssfn_bbox(&(ctx->font), (char *)curlang_str, &w, &h, NULL, NULL);
terminal_setcur(ctx, ctx->fb_modes[ctx->current_mode].width - w, ctx->fb_modes[ctx->current_mode].height - h);
terminal_print(ctx, (char *)newlang_str);
}
}
@ -124,7 +210,9 @@ void ui_init()
ctx.font_buf.y = 0;
ctx.font_buf.fg = 0xFFFFFFFF;
void (*ui_callback)(struct ui_context*,void*,void*) = NULL;
ctx.current_selection = config_get_default();
void (*ui_callback)(struct ui_context*,struct datetime*,struct language_selection*,struct mouse_event*,uint8_t) = NULL;
switch (ctx.ui) {
case UI_MODERN: {
@ -146,9 +234,57 @@ void ui_init()
}
}
struct datetime dt;
struct mouse_event me;
while (1) {
ui_callback(&ctx, NULL, NULL);
//get_mouse(&m_x, &m_y, &m_but);
//debug("Mouse X = %u | Mouse Y = %u\n", m_x, m_y);
uint8_t event = 0;
// datetime?
get_datetime(&dt);
if (memcmp(&dt, &last_dt, sizeof(struct datetime)) != 0) {
event |= EVENT_TIME;
}
// mouse movement?
get_mouse(&(me.x), &(me.y), &(me.but));
if (memcmp(&me, &(ctx.last_mouse), sizeof(struct mouse_event)) != 0) {
event |= EVENT_MOUSE;
}
// keypress?
uint16_t scancode;
get_keypress(&scancode);
if (scancode != 0) {
switch (scancode) {
case SCANCODE_ARROW_DOWN:
event |= EVENT_NEXT_ENTRY;
break;
case SCANCODE_ARROW_UP:
event |= EVENT_PREV_ENTRY;
break;
case SCANCODE_ENTER:
// clear the screen
for (uint32_t y = 0; y < ctx.fb_modes[ctx.current_mode].height; y++) {
for (uint32_t x = 0; x < ctx.fb_modes[ctx.current_mode].width; x++) {
*((uint32_t *)ctx.fb_addr + (ctx.fb_modes[ctx.current_mode].pitch / ctx.fb_modes[ctx.current_mode].bpp) * y + x) = 0xFF000000;
}
}
struct axboot_entry *entries = config_get_entries();
loader_load(&entries[ctx.current_selection]);
break;
default:
break;
}
}
if (event != 0) {
ui_callback(&ctx, &dt, NULL, &me, event);
} else {
#ifdef __x86_64
__asm__ volatile("hlt");
#endif
//arch_wait();
}
}
}