rulimine/stage2/menu.c

205 lines
6.4 KiB
C
Raw Normal View History

2020-06-05 21:27:52 +03:00
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
2020-08-29 21:02:16 +03:00
#include <limine.h>
2020-06-05 21:27:52 +03:00
#include <menu.h>
#include <lib/print.h>
#include <lib/blib.h>
#include <lib/libc.h>
#include <lib/config.h>
2020-09-02 10:55:56 +03:00
#include <lib/term.h>
2020-10-27 10:09:27 +03:00
#include <lib/readline.h>
2020-11-02 11:20:34 +03:00
#include <lib/uri.h>
2020-09-20 13:03:44 +03:00
#include <mm/pmm.h>
#include <drivers/vbe.h>
2020-06-05 21:27:52 +03:00
static char *cmdline;
#define CMDLINE_MAX 1024
static int print_tree(int level, int base_index, int selected_entry,
struct menu_entry *current_entry,
struct menu_entry **selected_menu_entry) {
int max_entries = 0;
for (;;) {
if (current_entry == NULL)
break;
for (int i = 0; i < level; i++)
print(" ");
if (current_entry->sub)
print(current_entry->expanded ? "[-] " : "[+] ");
else
print(" ");
if (base_index + max_entries == selected_entry) {
*selected_menu_entry = current_entry;
print("\e[47m\e[30m");
}
print(" %s \e[0m\n", current_entry->name);
if (current_entry->sub && current_entry->expanded) {
max_entries += print_tree(level + 1, base_index + max_entries + 1,
selected_entry,
current_entry->sub,
selected_menu_entry);
}
max_entries++;
current_entry = current_entry->next;
}
return max_entries;
}
2020-06-05 21:27:52 +03:00
char *menu(char **cmdline_ret) {
2020-09-26 04:56:44 +03:00
cmdline = conv_mem_alloc(CMDLINE_MAX);
2020-11-05 15:29:42 +03:00
char *buf = conv_mem_alloc(256);
struct menu_entry *selected_menu_entry;
int selected_entry = 0;
if (config_get_value(NULL, buf, 0, 16, "DEFAULT_ENTRY")) {
selected_entry = (int)strtoui(buf, NULL, 10);
}
int timeout = 5;
if (config_get_value(NULL, buf, 0, 16, "TIMEOUT")) {
timeout = (int)strtoui(buf, NULL, 10);
}
if (!timeout)
goto autoboot;
// If there is GRAPHICS config key and the value is "yes", enable graphics
if (config_get_value(NULL, buf, 0, 16, "GRAPHICS") && !strcmp(buf, "yes")) {
2020-09-26 04:56:44 +03:00
// default scheme
int margin = 64;
2020-09-30 18:29:07 +03:00
int margin_gradient = 20;
2020-09-26 04:56:44 +03:00
uint32_t colourscheme[] = {
0x00000000, // black
0x00aa0000, // red
0x0000aa00, // green
0x00aa5500, // brown
0x000000aa, // blue
0x00aa00aa, // magenta
0x0000aaaa, // cyan
0x00aaaaaa // grey
};
if (config_get_value(NULL, buf, 0, 256, "THEME_COLOURS")
|| config_get_value(NULL, buf, 0, 256, "THEME_COLORS")) {
2020-11-05 15:29:42 +03:00
const char *first = buf;
for (int i = 0; i < 8; i++) {
const char *last;
uint32_t col = strtoui(first, &last, 16);
if (first == last)
2020-11-05 15:29:42 +03:00
break;
colourscheme[i] = col;
if (*last == 0)
break;
first = last + 1;
2020-11-05 15:29:42 +03:00
}
2020-09-26 04:56:44 +03:00
}
if (config_get_value(NULL, buf, 0, 16, "THEME_MARGIN")) {
margin = (int)strtoui(buf, NULL, 10);
2020-09-26 04:56:44 +03:00
}
if (config_get_value(NULL, buf, 0, 16, "THEME_MARGIN_GRADIENT")) {
margin_gradient = (int)strtoui(buf, NULL, 10);
2020-09-30 18:29:07 +03:00
}
if (!config_get_value(NULL, cmdline, 0, CMDLINE_MAX, "BACKGROUND_PATH"))
goto nobg;
struct file_handle *bg_file = conv_mem_alloc(sizeof(struct file_handle));
if (!uri_open(bg_file, cmdline))
goto nobg;
struct image *bg = conv_mem_alloc(sizeof(struct image));
if (open_image(bg, bg_file))
goto nobg;
2020-09-30 18:29:07 +03:00
term_vbe(colourscheme, margin, margin_gradient, bg);
goto yesbg;
nobg:
2020-09-30 18:29:07 +03:00
term_vbe(colourscheme, margin, margin_gradient, NULL);
yesbg:;
2020-06-05 21:27:52 +03:00
}
2020-09-02 10:55:56 +03:00
disable_cursor();
2020-06-05 21:27:52 +03:00
bool skip_timeout = false;
if (menu_tree == NULL)
panic("Config contains no entries.");
2020-06-05 21:27:52 +03:00
refresh:
2020-09-02 10:55:56 +03:00
clear(true);
2020-08-29 21:02:16 +03:00
print("\n\n \e[36m Limine " LIMINE_VERSION " \e[37m\n\n\n");
2020-06-05 21:27:52 +03:00
print("Select an entry:\n\n");
int max_entries = print_tree(0, 0, selected_entry, menu_tree,
&selected_menu_entry);
2020-06-05 21:27:52 +03:00
print("\nArrows to choose, enter to select, 'e' to edit command line.");
int c;
2020-06-05 21:27:52 +03:00
if (skip_timeout == false) {
print("\n\n");
2020-06-05 21:27:52 +03:00
for (int i = timeout; i; i--) {
print("\rBooting automatically in %u, press any key to stop the countdown...", i);
if ((c = pit_sleep_and_quit_on_keypress(18))) {
2020-06-05 21:27:52 +03:00
skip_timeout = true;
print("\e[2K\r\e[2A");
goto timeout_aborted;
2020-06-05 21:27:52 +03:00
}
}
goto autoboot;
}
for (;;) {
c = getchar();
timeout_aborted:
2020-06-05 21:27:52 +03:00
switch (c) {
case GETCHAR_CURSOR_UP:
if (--selected_entry == -1)
selected_entry = max_entries - 1;
goto refresh;
case GETCHAR_CURSOR_DOWN:
if (++selected_entry == max_entries)
selected_entry = 0;
goto refresh;
case '\r':
autoboot:
if (selected_menu_entry->sub != NULL) {
skip_timeout = true;
selected_menu_entry->expanded = !selected_menu_entry->expanded;
goto refresh;
}
2020-09-02 10:55:56 +03:00
enable_cursor();
if (!config_get_value(selected_menu_entry->body, cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) {
if (!config_get_value(selected_menu_entry->body, cmdline, 0, CMDLINE_MAX, "CMDLINE")) {
2020-06-05 21:27:52 +03:00
cmdline[0] = '\0';
}
}
2020-09-02 10:55:56 +03:00
clear(true);
*cmdline_ret = cmdline;
return selected_menu_entry->body;
2020-06-05 21:27:52 +03:00
case 'e':
if (selected_menu_entry->sub != NULL)
goto refresh;
2020-09-02 10:55:56 +03:00
enable_cursor();
if (!config_get_value(selected_menu_entry->body, cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) {
if (!config_get_value(selected_menu_entry->body, cmdline, 0, CMDLINE_MAX, "CMDLINE")) {
2020-06-05 21:27:52 +03:00
cmdline[0] = '\0';
}
}
print("\n\n> ");
2020-10-27 10:09:27 +03:00
readline(cmdline, cmdline, CMDLINE_MAX);
2020-09-02 10:55:56 +03:00
clear(true);
*cmdline_ret = cmdline;
return selected_menu_entry->body;
2020-06-05 21:27:52 +03:00
}
}
}