:: commit ab1e8710237fe6d62446020e48ce5d877dfa4281

mintsuki <mintsuki@protonmail.com> — 2020-06-05 18:27

parents: 24ddd20e89

Rework boot menu

diff --git a/qloader2.bin b/qloader2.bin
index 78af2bf5..5ee26d82 100644
Binary files a/qloader2.bin and b/qloader2.bin differ
diff --git a/src/drivers/vga_textmode.c b/src/drivers/vga_textmode.c
index d1fe83bf..9c4b5f50 100644
--- a/src/drivers/vga_textmode.c
+++ b/src/drivers/vga_textmode.c
@@ -183,7 +183,7 @@ static void text_putchar(char c) {
     return;
 }
 
-static uint8_t ansi_colours[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
+static uint8_t ansi_colours[] = { 0, 4, 2, 0x0e, 1, 5, 3, 7 };
 
 static void sgr(void) {
 
diff --git a/src/main.c b/src/main.c
index e6e5c1c7..7ffb81f8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -28,73 +28,7 @@ asm (
 #include <protos/stivale.h>
 #include <protos/linux.h>
 #include <protos/chainload.h>
-
-static char *cmdline;
-#define CMDLINE_MAX 1024
-
-static char config_entry_name[1024];
-
-void boot_menu(void) {
-    text_disable_cursor();
-    int selected_entry = 0;
-
-refresh:
-    text_clear();
-    print("qloader2\n\n");
-
-    print("Select an entry:\n\n");
-
-    int max_entries;
-    for (max_entries = 0; ; max_entries++) {
-        if (config_get_entry_name(config_entry_name, max_entries, 1024) == -1)
-            break;
-        if (max_entries == selected_entry)
-            print(" -> %s\n", config_entry_name);
-        else
-            print("    %s\n", config_entry_name);
-    }
-
-    if (max_entries == 0)
-        panic("Config contains no entries.");
-
-    print("\nArrows to choose, enter to select, 'e' to edit command line.");
-
-    for (;;) {
-        int c = getchar();
-        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':
-                config_set_entry(selected_entry);
-                text_enable_cursor();
-                if (!config_get_value(cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) {
-                    if (!config_get_value(cmdline, 0, CMDLINE_MAX, "CMDLINE")) {
-                        cmdline[0] = '\0';
-                    }
-                }
-                text_clear();
-                return;
-            case 'e':
-                config_set_entry(selected_entry);
-                text_enable_cursor();
-                if (!config_get_value(cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) {
-                    if (!config_get_value(cmdline, 0, CMDLINE_MAX, "CMDLINE")) {
-                        cmdline[0] = '\0';
-                    }
-                }
-                print("\n\n> ");
-                gets(cmdline, cmdline, CMDLINE_MAX);
-                text_clear();
-                return;
-        }
-    }
-}
+#include <menu.h>
 
 void main(int boot_drive) {
     // Initial prompt.
@@ -104,8 +38,6 @@ void main(int boot_drive) {
 
     print("Boot drive: %x\n", boot_drive);
 
-    cmdline = balloc(CMDLINE_MAX);
-
     // Look for config file.
     print("Searching for config file...\n");
     struct part parts[4];
@@ -126,34 +58,8 @@ void main(int boot_drive) {
         }
     }
 
-    int timeout; {
-        char buf[32];
-        if (!config_get_value(buf, 0, 32, "TIMEOUT")) {
-            timeout = 5;
-        } else {
-            timeout = (int)strtoui(buf);
-        }
-    }
-
-    print("\n");
-    for (int i = timeout; i; i--) {
-        print("\rBooting in %d (press any key for boot menu, command line edit)...", i);
-        if (pit_sleep_and_quit_on_keypress(18)) {
-            boot_menu();
-            goto got_entry;
-        }
-    }
-    print("\n\n");
-
-    if (config_set_entry(0) == -1) {
-        panic("Invalid config entry.");
-    }
-
-    if (!config_get_value(cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) {
-        cmdline[0] = '\0';
-    }
+    char *cmdline = menu();
 
-got_entry:
     init_e820();
     init_memmap();
 
diff --git a/src/menu.c b/src/menu.c
new file mode 100644
index 00000000..8930fff1
--- /dev/null
+++ b/src/menu.c
@@ -0,0 +1,106 @@
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+#include <menu.h>
+#include <lib/print.h>
+#include <lib/blib.h>
+#include <lib/libc.h>
+#include <lib/config.h>
+#include <drivers/vga_textmode.h>
+
+static char *cmdline;
+#define CMDLINE_MAX 1024
+
+static char config_entry_name[1024];
+
+char *menu(void) {
+    cmdline = balloc(CMDLINE_MAX);
+
+    int timeout; {
+        char buf[32];
+        if (!config_get_value(buf, 0, 32, "TIMEOUT")) {
+            timeout = 5;
+        } else {
+            timeout = (int)strtoui(buf);
+        }
+    }
+
+    text_disable_cursor();
+    int selected_entry = 0;
+    bool skip_timeout = false;
+
+refresh:
+    text_clear();
+    print("\n");
+    print("                              \e[44m                  \e[40m\n");
+    print("                              \e[44m     qloader\e[33m2\e[37m     \e[40m\n");
+    print("                              \e[44m                  \e[40m\n");
+    print("\n");
+
+    print("Select an entry:\n\n");
+
+    int max_entries;
+    for (max_entries = 0; ; max_entries++) {
+        if (config_get_entry_name(config_entry_name, max_entries, 1024) == -1)
+            break;
+        if (max_entries == selected_entry)
+            print("  \e[47m\e[30m %s \e[40m\e[37m\n", config_entry_name);
+        else
+            print("   %s\n", config_entry_name);
+    }
+
+    if (max_entries == 0)
+        panic("Config contains no entries.");
+
+    print("\n");
+
+    if (skip_timeout == false) {
+        for (int i = timeout; i; i--) {
+            print("\rBooting automatically in %u, press any key to stop the countdown...", i);
+            if (pit_sleep_and_quit_on_keypress(18)) {
+                skip_timeout = true;
+                goto refresh;
+            }
+        }
+        goto autoboot;
+    }
+
+    print("Arrows to choose, enter to select, 'e' to edit command line.");
+
+    for (;;) {
+        int c = getchar();
+        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:
+                config_set_entry(selected_entry);
+                text_enable_cursor();
+                if (!config_get_value(cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) {
+                    if (!config_get_value(cmdline, 0, CMDLINE_MAX, "CMDLINE")) {
+                        cmdline[0] = '\0';
+                    }
+                }
+                text_clear();
+                return cmdline;
+            case 'e':
+                config_set_entry(selected_entry);
+                text_enable_cursor();
+                if (!config_get_value(cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) {
+                    if (!config_get_value(cmdline, 0, CMDLINE_MAX, "CMDLINE")) {
+                        cmdline[0] = '\0';
+                    }
+                }
+                print("\n\n> ");
+                gets(cmdline, cmdline, CMDLINE_MAX);
+                text_clear();
+                return cmdline;
+        }
+    }
+}
diff --git a/src/menu.h b/src/menu.h
new file mode 100644
index 00000000..8d04432f
--- /dev/null
+++ b/src/menu.h
@@ -0,0 +1,6 @@
+#ifndef __MENU_H__
+#define __MENU_H__
+
+char *menu(void);
+
+#endif
tab: 248 wrap: offon