From aedef854c31a72ac0f0077a62f95a0ab09038778 Mon Sep 17 00:00:00 2001 From: chaoskagami Date: Mon, 13 Jun 2016 17:09:05 -0400 Subject: [PATCH] Make the toplevel menu use the same menuing code (and implement a few required bits.) --- source/config.h | 15 ++++--- source/display.c | 20 +++++++++ source/main.c | 8 +--- source/menu.c | 108 ++++++++--------------------------------------- 4 files changed, 48 insertions(+), 103 deletions(-) diff --git a/source/config.h b/source/config.h index 100822a..9c9b0e0 100644 --- a/source/config.h +++ b/source/config.h @@ -30,14 +30,17 @@ extern struct config_file config; enum type { - boolean_val = 0, // Toggle - ranged_val = 1, // N1 - N2, left and right to pick. - mask_val = 2, // Bitmask allowed values. - not_option = 3, // Skip over this. - call_fun = 4, // Call a function. Treat (a) as (void)(*)(void). - boolean_val_n3ds = 5 // Toggle, but only show on n3DS + boolean_val = 0, // Toggle + ranged_val = 1, // N1 - N2, left and right to pick. + mask_val = 2, // Bitmask allowed values. + not_option = 3, // Skip over this. + call_fun = 4, // Call a function. Treat (a) as (void)(*)(void). + boolean_val_n3ds = 5, // Toggle, but only show on n3DS + break_menu = 6 }; +typedef void (*func_call_t)(void); + struct range_str { int a, b; diff --git a/source/display.c b/source/display.c index 892acb2..68fa67a 100644 --- a/source/display.c +++ b/source/display.c @@ -24,6 +24,8 @@ show_menu(struct options_s *options, uint8_t *toggles) int cursor_max = -1; int exit = 0; + clear_screen(TOP_SCREEN); + if (options[0].index == -1) { set_cursor(TOP_SCREEN, 0, 0); header("Any:Back"); @@ -74,6 +76,16 @@ show_menu(struct options_s *options, uint8_t *toggles) putc(TOP_SCREEN, ']'); putc(TOP_SCREEN, '\n'); } + } else if (options[i].allowed == call_fun || options[i].allowed == break_menu) { + if (cursor_y == i) + fprintf(TOP_SCREEN, "\x1b[32m>>\x1b[0m "); + else + fprintf(TOP_SCREEN, " "); + + if (need_redraw) + fprintf(TOP_SCREEN, "%s\n", options[i].name); + else + putc(TOP_SCREEN, '\n'); } else if (options[i].allowed == ranged_val) { if (cursor_y == i) fprintf(TOP_SCREEN, "\x1b[32m>>\x1b[0m "); @@ -120,6 +132,12 @@ show_menu(struct options_s *options, uint8_t *toggles) toggles[options[cursor_y].index] = options[cursor_y].a; else toggles[options[cursor_y].index]++; + } else if (options[cursor_y].allowed == call_fun) { + ((func_call_t)(options[cursor_y].a))(); // Call 'a' as a function. + need_redraw = 1; + } else if (options[cursor_y].allowed == break_menu) { + exit = 1; + need_redraw = 1; } break; case BUTTON_X: @@ -144,5 +162,7 @@ show_menu(struct options_s *options, uint8_t *toggles) cursor_y = cursor_min; } + clear_screen(TOP_SCREEN); + return 0; } diff --git a/source/main.c b/source/main.c index a56356a..0034e12 100644 --- a/source/main.c +++ b/source/main.c @@ -38,12 +38,8 @@ main() if (config.options[OPTION_SILENCE]) shut_up(); // This does exactly what it sounds like. doing_autoboot = 1; - boot_cfw(); // Just boot shit. - } - - int in_menu = 1; - while (in_menu) { - in_menu = menu_handler(); + } else { + menu_handler(); } boot_cfw(); diff --git a/source/menu.c b/source/menu.c index 1d98f6a..b69ffa7 100644 --- a/source/menu.c +++ b/source/menu.c @@ -68,8 +68,6 @@ static struct options_s options[] = { { -1, "", "", 0, -1, -1 }, // cursor_min and cursor_max are stored in the last two. }; -static int cursor_y = 0; -static int which_menu = 1; static int need_redraw = 1; extern void waitcycles(uint32_t cycles); @@ -300,64 +298,26 @@ int menu_saveconfig() { return MENU_MAIN; } +static struct options_s main_s[] = { + // space + { 0, "Options", "", call_fun, (uint32_t)menu_options, 0 }, + { 0, "Patches", "", call_fun, (uint32_t)menu_patches, 0 }, + { 0, "Info", "", call_fun, (uint32_t)menu_info, 0 }, + { 0, "Help/Readme", "", call_fun, (uint32_t)menu_help, 0 }, + { 0, "Reboot", "", call_fun, (uint32_t)menu_reset, 0 }, + { 0, "Power off", "", call_fun, (uint32_t)menu_poweroff, 0 }, + { 0, "Save Configuration", "", call_fun, (uint32_t)menu_saveconfig, 0 }, + { 0, "Boot Firmware", "", break_menu, 0, 0 }, + + // Sentinel. + { -1, "", "", 0, -1, -1 }, // cursor_min and cursor_max are stored in the last two. +}; + int menu_main() { // TODO - Stop using different menu code here. - set_cursor(TOP_SCREEN, 0, 0); - - const char *list[] = { "Options", "Patches", "Info", "Help/Readme", "Reboot", "Power off", "Save Configuration", "Boot Firmware" }; - int menu_max = 8; - - header("A:Enter DPAD:Nav"); - - for (int i = 0; i < menu_max; i++) { - if (!(i + 2 == MENU_HELP && config.options[OPTION_READ_ME])) { - if (cursor_y == i) - fprintf(TOP_SCREEN, "\x1b[32m>>\x1b[0m "); - else - fprintf(TOP_SCREEN, " "); - - if (need_redraw) - fprintf(TOP_SCREEN, "%s\n", list[i]); - else - putc(TOP_SCREEN, '\n'); - } - } - - need_redraw = 0; - - uint32_t key = wait_key(); - - int ret = cursor_y + 2; - - switch (key) { - case BUTTON_UP: - cursor_y -= 1; - if (config.options[OPTION_READ_ME] && cursor_y + 2 == MENU_HELP) - cursor_y -= 1; // Disable help. - break; - case BUTTON_DOWN: - cursor_y += 1; - if (config.options[OPTION_READ_ME] && cursor_y + 2 == MENU_HELP) - cursor_y += 1; // Disable help. - break; - case BUTTON_A: - need_redraw = 1; - cursor_y = 0; - if (ret == MENU_BOOTME) - return MENU_BOOTME; // Boot meh, damnit! - clear_screen(TOP_SCREEN); - if (ret == MENU_OPTIONS) - cursor_y = 0; // Fixup positions - return ret; - } - - // Loop around the cursor. - if (cursor_y < 0) - cursor_y = menu_max - 1; - if (cursor_y > menu_max - 1) - cursor_y = 0; + show_menu(main_s, NULL); return 0; } @@ -365,39 +325,5 @@ menu_main() int menu_handler() { - int to_menu = 0; - switch (which_menu) { - case MENU_MAIN: - to_menu = menu_main(); - break; - case MENU_OPTIONS: - to_menu = menu_options(); - break; - case MENU_PATCHES: - to_menu = menu_patches(); - break; - case MENU_INFO: - to_menu = menu_info(); - break; - case MENU_HELP: - to_menu = menu_help(); - break; - case MENU_SAVECFG: - to_menu = menu_saveconfig(); - break; - case MENU_BOOTME: - return 0; - case MENU_RESET: - menu_reset(); - case MENU_POWER: - menu_poweroff(); - default: - fprintf(stderr, "Attempt to enter wrong menu!\n"); - to_menu = MENU_MAIN; - } - - if (to_menu != 0) - which_menu = to_menu; - - return 1; + return menu_main(); } -- 2.39.5