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>
|
2020-09-24 22:48:49 +03:00
|
|
|
#include <drivers/vbe.h>
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2020-12-10 09:32:15 +03:00
|
|
|
static char *menu_branding = NULL;
|
|
|
|
|
2020-11-18 02:13:54 +03:00
|
|
|
static void cursor_back(void) {
|
|
|
|
int x, y;
|
|
|
|
get_cursor_pos(&x, &y);
|
|
|
|
if (x) {
|
|
|
|
x--;
|
|
|
|
} else if (y) {
|
|
|
|
y--;
|
|
|
|
x = term_cols - 1;
|
|
|
|
}
|
|
|
|
set_cursor_pos(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cursor_fwd(void) {
|
|
|
|
int x, y;
|
|
|
|
get_cursor_pos(&x, &y);
|
|
|
|
if (x < term_cols - 1) {
|
|
|
|
x++;
|
|
|
|
} else if (y < term_rows - 1) {
|
|
|
|
y++;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
set_cursor_pos(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define EDITOR_MAX_BUFFER_SIZE 4096
|
|
|
|
|
|
|
|
static size_t get_line_offset(size_t *displacement, size_t index, const char *buffer) {
|
|
|
|
size_t offset = 0;
|
|
|
|
size_t _index = index;
|
|
|
|
for (size_t i = 0; buffer[i]; i++) {
|
|
|
|
if (!_index--)
|
|
|
|
break;
|
|
|
|
if (buffer[i] == '\n')
|
|
|
|
offset = i + 1;
|
|
|
|
}
|
|
|
|
if (displacement)
|
|
|
|
*displacement = index - offset;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_line_length(size_t index, const char *buffer) {
|
|
|
|
size_t i;
|
|
|
|
for (i = index; buffer[i] != '\n' && buffer[i] != 0; i++);
|
|
|
|
return i - index;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_next_line(size_t index, const char *buffer) {
|
|
|
|
if (buffer[index] == 0)
|
|
|
|
return index;
|
|
|
|
size_t displacement;
|
|
|
|
get_line_offset(&displacement, index, buffer);
|
|
|
|
while (buffer[index++] != '\n');
|
|
|
|
size_t next_line_length = get_line_length(index, buffer);
|
|
|
|
if (displacement > next_line_length)
|
|
|
|
displacement = next_line_length;
|
|
|
|
return index + displacement;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_prev_line(size_t index, const char *buffer) {
|
|
|
|
size_t offset, displacement, prev_line_offset, prev_line_length;
|
|
|
|
offset = get_line_offset(&displacement, index, buffer);
|
|
|
|
if (offset) {
|
|
|
|
prev_line_offset = get_line_offset(NULL, offset - 1, buffer);
|
|
|
|
prev_line_length = get_line_length(prev_line_offset, buffer);
|
|
|
|
if (displacement > prev_line_length)
|
|
|
|
displacement = prev_line_length;
|
|
|
|
return prev_line_offset + displacement;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *config_entry_editor(bool *ret, const char *orig_entry) {
|
|
|
|
size_t cursor_offset = 0;
|
|
|
|
size_t entry_size = strlen(orig_entry);
|
|
|
|
|
|
|
|
// Skip leading newlines
|
|
|
|
while (*orig_entry == '\n') {
|
|
|
|
orig_entry++;
|
|
|
|
entry_size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry_size >= EDITOR_MAX_BUFFER_SIZE)
|
|
|
|
panic("Entry is too big to be edited.");
|
|
|
|
|
|
|
|
char *buffer = ext_mem_alloc(EDITOR_MAX_BUFFER_SIZE);
|
|
|
|
memcpy(buffer, orig_entry, entry_size);
|
|
|
|
buffer[entry_size] = 0;
|
|
|
|
|
|
|
|
refresh:
|
|
|
|
clear(true);
|
|
|
|
disable_cursor();
|
2020-12-10 09:32:15 +03:00
|
|
|
print("\n\n \e[36m %s \e[37m\n\n\n", menu_branding);
|
2020-11-18 02:13:54 +03:00
|
|
|
|
|
|
|
print("Editing entry.\n");
|
|
|
|
print("Press esc to return to main menu and discard changes, press F10 to boot.\n");
|
|
|
|
|
|
|
|
print("\n");
|
|
|
|
for (int i = 0; i < term_cols; i++)
|
|
|
|
print("-");
|
|
|
|
|
|
|
|
int cursor_x, cursor_y;
|
|
|
|
for (size_t i = 0; ; i++) {
|
|
|
|
if (i == cursor_offset)
|
|
|
|
get_cursor_pos(&cursor_x, &cursor_y);
|
|
|
|
|
|
|
|
if (buffer[i] == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
print("%c", buffer[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
print("\n");
|
|
|
|
for (int i = 0; i < term_cols; i++)
|
|
|
|
print("-");
|
|
|
|
|
|
|
|
// Hack to redraw the cursor
|
|
|
|
set_cursor_pos(cursor_x, cursor_y);
|
|
|
|
enable_cursor();
|
|
|
|
|
2020-11-20 21:55:18 +03:00
|
|
|
term_double_buffer_flush();
|
|
|
|
|
2020-11-18 02:13:54 +03:00
|
|
|
int c = getchar();
|
|
|
|
switch (c) {
|
|
|
|
case GETCHAR_CURSOR_DOWN:
|
|
|
|
cursor_offset = get_next_line(cursor_offset, buffer);
|
|
|
|
break;
|
|
|
|
case GETCHAR_CURSOR_UP:
|
|
|
|
cursor_offset = get_prev_line(cursor_offset, buffer);
|
|
|
|
break;
|
|
|
|
case GETCHAR_CURSOR_LEFT:
|
|
|
|
if (cursor_offset) {
|
|
|
|
cursor_offset--;
|
|
|
|
cursor_back();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GETCHAR_CURSOR_RIGHT:
|
|
|
|
if (cursor_offset < strlen(buffer)) {
|
|
|
|
cursor_offset++;
|
|
|
|
cursor_fwd();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
if (cursor_offset) {
|
|
|
|
cursor_offset--;
|
|
|
|
cursor_back();
|
|
|
|
case GETCHAR_DELETE:
|
|
|
|
for (size_t i = cursor_offset; ; i++) {
|
|
|
|
buffer[i] = buffer[i+1];
|
|
|
|
if (!buffer[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GETCHAR_F10:
|
|
|
|
*ret = false;
|
|
|
|
disable_cursor();
|
|
|
|
return buffer;
|
|
|
|
case '\e':
|
|
|
|
*ret = true;
|
|
|
|
disable_cursor();
|
|
|
|
return (char *)orig_entry;
|
|
|
|
default:
|
|
|
|
if (strlen(buffer) < EDITOR_MAX_BUFFER_SIZE - 1) {
|
|
|
|
for (size_t i = strlen(buffer); ; i--) {
|
|
|
|
buffer[i+1] = buffer[i];
|
|
|
|
if (i == cursor_offset)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer[cursor_offset++] = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto refresh;
|
|
|
|
}
|
|
|
|
|
2020-11-17 01:31:03 +03:00
|
|
|
static int print_tree(int level, int base_index, int selected_entry,
|
2020-12-03 14:38:53 +03:00
|
|
|
struct menu_entry *current_entry,
|
|
|
|
struct menu_entry **selected_menu_entry) {
|
2020-11-17 01:31:03 +03:00
|
|
|
int max_entries = 0;
|
2020-12-07 19:14:14 +03:00
|
|
|
|
2020-11-17 01:31:03 +03:00
|
|
|
for (;;) {
|
|
|
|
if (current_entry == NULL)
|
|
|
|
break;
|
2020-11-17 02:50:26 +03:00
|
|
|
if (level) {
|
|
|
|
for (int i = level - 1; i > 0; i--) {
|
|
|
|
struct menu_entry *actual_parent = current_entry;
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
actual_parent = actual_parent->parent;
|
|
|
|
if (actual_parent->next != NULL)
|
2020-12-07 19:14:14 +03:00
|
|
|
print(" \xb3");
|
2020-11-17 02:50:26 +03:00
|
|
|
else
|
2020-12-07 19:14:14 +03:00
|
|
|
print(" ");
|
2020-11-17 02:50:26 +03:00
|
|
|
}
|
|
|
|
if (current_entry->next == NULL)
|
2020-12-07 19:14:14 +03:00
|
|
|
print(" \xc0");
|
2020-11-17 02:50:26 +03:00
|
|
|
else
|
2020-12-07 19:14:14 +03:00
|
|
|
print(" \xc3");
|
2020-11-17 02:50:26 +03:00
|
|
|
}
|
2020-11-17 01:31:03 +03:00
|
|
|
if (current_entry->sub)
|
2020-11-17 02:50:26 +03:00
|
|
|
print(current_entry->expanded ? "[-]" : "[+]");
|
|
|
|
else if (level)
|
2020-12-07 19:14:14 +03:00
|
|
|
print("\xc4> ");
|
2020-11-17 01:31:03 +03:00
|
|
|
else
|
2020-11-17 02:50:26 +03:00
|
|
|
print(" ");
|
2020-11-17 01:31:03 +03:00
|
|
|
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
|
|
|
|
2020-12-01 05:09:38 +03:00
|
|
|
char *menu(char **cmdline) {
|
2020-12-10 09:32:15 +03:00
|
|
|
menu_branding = config_get_value(NULL, 0, "MENU_BRANDING");
|
|
|
|
if (menu_branding == NULL)
|
|
|
|
menu_branding = "Limine " LIMINE_VERSION;
|
|
|
|
|
2020-12-03 14:38:53 +03:00
|
|
|
if (menu_tree == NULL)
|
|
|
|
panic("Config contains no valid entries.");
|
|
|
|
|
2020-12-02 21:16:38 +03:00
|
|
|
bool skip_timeout = false;
|
2020-12-03 14:38:53 +03:00
|
|
|
struct menu_entry *selected_menu_entry = NULL;
|
2020-11-17 01:31:03 +03:00
|
|
|
|
2020-09-29 22:48:34 +03:00
|
|
|
int selected_entry = 0;
|
2020-11-27 21:33:34 +03:00
|
|
|
char *default_entry = config_get_value(NULL, 0, "DEFAULT_ENTRY");
|
|
|
|
if (default_entry != NULL) {
|
|
|
|
selected_entry = strtoui(default_entry, NULL, 10);
|
2020-12-03 14:38:53 +03:00
|
|
|
if (selected_entry)
|
|
|
|
selected_entry--;
|
2020-09-29 22:48:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int timeout = 5;
|
2020-11-27 21:33:34 +03:00
|
|
|
char *timeout_config = config_get_value(NULL, 0, "TIMEOUT");
|
|
|
|
if (timeout_config != NULL) {
|
2020-12-03 14:38:53 +03:00
|
|
|
if (!strcmp(timeout_config, "no"))
|
|
|
|
skip_timeout = true;
|
|
|
|
else
|
|
|
|
timeout = strtoui(timeout_config, NULL, 10);
|
2020-09-29 22:48:34 +03:00
|
|
|
}
|
|
|
|
|
2020-12-03 14:38:53 +03:00
|
|
|
if (!timeout) {
|
|
|
|
// Use print tree to load up selected_menu_entry and determine if the
|
|
|
|
// default entry is valid.
|
|
|
|
print_tree(0, 0, selected_entry, menu_tree, &selected_menu_entry);
|
|
|
|
if (selected_menu_entry == NULL || selected_menu_entry->sub != NULL) {
|
|
|
|
print("Default entry is not valid or directory, booting to menu.\n");
|
|
|
|
skip_timeout = true;
|
|
|
|
} else {
|
|
|
|
goto autoboot;
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 22:48:34 +03:00
|
|
|
|
|
|
|
// If there is GRAPHICS config key and the value is "yes", enable graphics
|
2020-11-27 21:33:34 +03:00
|
|
|
char *graphics = config_get_value(NULL, 0, "GRAPHICS");
|
|
|
|
if (graphics != NULL && !strcmp(graphics, "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
|
|
|
|
};
|
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
char *colours = config_get_value(NULL, 0, "THEME_COLOURS");
|
|
|
|
if (colours == NULL)
|
|
|
|
colours = config_get_value(NULL, 0, "THEME_COLORS");
|
|
|
|
if (colours != NULL) {
|
|
|
|
const char *first = colours;
|
2020-11-05 15:29:42 +03:00
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
const char *last;
|
|
|
|
uint32_t col = strtoui(first, &last, 16);
|
2020-11-09 14:31:47 +03:00
|
|
|
if (first == last)
|
2020-11-05 15:29:42 +03:00
|
|
|
break;
|
|
|
|
colourscheme[i] = col;
|
2020-11-09 14:31:47 +03:00
|
|
|
if (*last == 0)
|
|
|
|
break;
|
|
|
|
first = last + 1;
|
2020-11-05 15:29:42 +03:00
|
|
|
}
|
2020-09-26 04:56:44 +03:00
|
|
|
}
|
2020-09-02 11:31:39 +03:00
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
char *theme_margin = config_get_value(NULL, 0, "THEME_MARGIN");
|
|
|
|
if (theme_margin != NULL) {
|
|
|
|
margin = strtoui(theme_margin, NULL, 10);
|
2020-09-26 04:56:44 +03:00
|
|
|
}
|
2020-09-24 22:48:49 +03:00
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
char *theme_margin_gradient = config_get_value(NULL, 0, "THEME_MARGIN_GRADIENT");
|
|
|
|
if (theme_margin_gradient != NULL) {
|
|
|
|
margin_gradient = strtoui(theme_margin_gradient, NULL, 10);
|
2020-09-30 18:29:07 +03:00
|
|
|
}
|
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
struct image *bg = NULL;
|
2020-09-22 01:18:13 +03:00
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
char *background_path = config_get_value(NULL, 0, "BACKGROUND_PATH");
|
|
|
|
if (background_path == NULL)
|
2020-09-22 01:18:13 +03:00
|
|
|
goto nobg;
|
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
struct file_handle *bg_file = ext_mem_alloc(sizeof(struct file_handle));
|
|
|
|
if (!uri_open(bg_file, background_path))
|
2020-09-22 01:18:13 +03:00
|
|
|
goto nobg;
|
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
bg = ext_mem_alloc(sizeof(struct image));
|
|
|
|
if (open_image(bg, bg_file))
|
|
|
|
bg = NULL;
|
2020-09-22 01:18:13 +03:00
|
|
|
|
|
|
|
nobg:
|
2020-11-27 21:33:34 +03:00
|
|
|
term_vbe(colourscheme, margin, margin_gradient, bg);
|
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
|
|
|
|
2020-11-20 21:55:18 +03:00
|
|
|
term_double_buffer(true);
|
|
|
|
|
2020-06-05 21:27:52 +03:00
|
|
|
refresh:
|
2020-09-02 10:55:56 +03:00
|
|
|
clear(true);
|
2020-12-10 09:32:15 +03:00
|
|
|
print("\n\n \e[36m %s \e[37m\n\n\n", menu_branding);
|
2020-06-05 21:27:52 +03:00
|
|
|
|
|
|
|
print("Select an entry:\n\n");
|
|
|
|
|
2020-11-17 01:31:03 +03:00
|
|
|
int max_entries = print_tree(0, 0, selected_entry, menu_tree,
|
|
|
|
&selected_menu_entry);
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2020-12-02 21:16:38 +03:00
|
|
|
print("\nArrows to choose, enter to boot, 'e' to edit selected entry.");
|
|
|
|
|
|
|
|
if (selected_menu_entry->sub != NULL)
|
|
|
|
skip_timeout = true;
|
2020-09-28 23:39:28 +03:00
|
|
|
|
|
|
|
int c;
|
2020-06-05 21:27:52 +03:00
|
|
|
|
|
|
|
if (skip_timeout == false) {
|
2020-09-28 23:39:28 +03:00
|
|
|
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);
|
2020-11-20 21:55:18 +03:00
|
|
|
term_double_buffer_flush();
|
2020-09-28 23:39:28 +03:00
|
|
|
if ((c = pit_sleep_and_quit_on_keypress(18))) {
|
2020-06-05 21:27:52 +03:00
|
|
|
skip_timeout = true;
|
2020-09-28 23:39:28 +03:00
|
|
|
print("\e[2K\r\e[2A");
|
|
|
|
goto timeout_aborted;
|
2020-06-05 21:27:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
goto autoboot;
|
|
|
|
}
|
|
|
|
|
2020-11-20 21:55:18 +03:00
|
|
|
term_double_buffer_flush();
|
|
|
|
|
2020-06-05 21:27:52 +03:00
|
|
|
for (;;) {
|
2020-09-28 23:39:28 +03:00
|
|
|
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:
|
2020-11-17 01:31:03 +03:00
|
|
|
if (selected_menu_entry->sub != NULL) {
|
|
|
|
selected_menu_entry->expanded = !selected_menu_entry->expanded;
|
|
|
|
goto refresh;
|
|
|
|
}
|
2020-09-02 10:55:56 +03:00
|
|
|
enable_cursor();
|
2020-12-01 05:09:38 +03:00
|
|
|
*cmdline = config_get_value(selected_menu_entry->body, 0, "KERNEL_CMDLINE");
|
|
|
|
if (!*cmdline) {
|
|
|
|
*cmdline = config_get_value(selected_menu_entry->body, 0, "CMDLINE");
|
2020-11-27 21:33:34 +03:00
|
|
|
}
|
2020-12-01 05:09:38 +03:00
|
|
|
if (!*cmdline) {
|
|
|
|
*cmdline = "";
|
2020-06-05 21:27:52 +03:00
|
|
|
}
|
2020-09-02 10:55:56 +03:00
|
|
|
clear(true);
|
2020-11-20 21:55:18 +03:00
|
|
|
term_double_buffer(false);
|
2020-11-17 01:31:03 +03:00
|
|
|
return selected_menu_entry->body;
|
2020-11-18 02:13:54 +03:00
|
|
|
case 'e': {
|
2020-11-17 01:31:03 +03:00
|
|
|
if (selected_menu_entry->sub != NULL)
|
|
|
|
goto refresh;
|
2020-09-02 10:55:56 +03:00
|
|
|
enable_cursor();
|
2020-11-18 02:13:54 +03:00
|
|
|
bool ret;
|
|
|
|
selected_menu_entry->body = config_entry_editor(&ret, selected_menu_entry->body);
|
|
|
|
if (ret)
|
|
|
|
goto refresh;
|
|
|
|
goto autoboot;
|
|
|
|
}
|
2020-06-05 21:27:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|