diff --git a/README.md b/README.md index 76d7a01..69585a1 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ My vWii Channel,0x0001000152494654 The names are the ones `autoboot.cfg` uses: `@wiiu_menu`, `@homebrew_launcher`, `@vwii_system_menu` and `@vwii_homebrew_channel`. Built-in entries without a line follow the listed ones in the usual order, so none can disappear by accident, and an entry that isn't available (the Homebrew Launcher without its installer, the vWii Homebrew Channel without HBC) is still left out. Without `@` lines the menu keeps the usual order. With `@` lines and no autoboot entry, the cursor starts on the first entry instead of the Wii U Menu. +### Menu countdown + +A line `countdown=5` in `bootOptions.cfg` (1 to 30 seconds) shows the menu at every boot even when an autoboot entry is set, with "Starting in 5..." above the bottom bar. When the countdown ends, the autoboot entry starts; pressing any button first stops the countdown and leaves the menu open as usual. Holding START (+) at boot opens the menu without a countdown. Without an autoboot entry there is nothing to count down to, and the menu waits as before. Useful when no GamePad or Wii Remote is in reach at boot. + ## Buildflags ### Logging diff --git a/source/MenuUtils.cpp b/source/MenuUtils.cpp index 48f7090..4b47525 100644 --- a/source/MenuUtils.cpp +++ b/source/MenuUtils.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -60,16 +61,19 @@ static int32_t builtInOptionNamed(const char *name) { return -1; } -std::vector readCustomBootOptions(const std::string &path, std::vector &menuOrder) { +std::vector readCustomBootOptions(const std::string &path, std::vector &menuOrder, + uint32_t &countdownSeconds) { std::vector result; menuOrder.clear(); - FILE *f = fopen(path.c_str(), "r"); + countdownSeconds = 0; + FILE *f = fopen(path.c_str(), "r"); if (!f) { return result; } // One entry per line: "Name,0x0001000152494654". The title id decides how it is launched. - // A line "@name" places a built-in entry ("@wiiu_menu") between them. + // A line "@name" places a built-in entry ("@wiiu_menu") between them, and a line + // "countdown=5" turns on the menu's countdown to the autoboot entry. std::vector order; bool namesBuiltIns = false; bool tooMany = false; @@ -86,6 +90,20 @@ std::vector readCustomBootOptions(const std::string &path, std continue; } + // A line with a comma is a custom entry, even if its name starts with "countdown=". + if (strncasecmp(line, "countdown=", 10) == 0 && !strchr(line, ',')) { + char *valueStr = line + 10; + trimInPlace(valueStr); + char *valueEnd = nullptr; + unsigned long seconds = strtoul(valueStr, &valueEnd, 10); + if (valueEnd == valueStr || *valueEnd != '\0' || seconds < 1 || seconds > MAX_MENU_COUNTDOWN_SECONDS) { + DEBUG_FUNCTION_LINE_WARN("bootOptions.cfg: \"%s\" is not a countdown of 1 to %d seconds", line, MAX_MENU_COUNTDOWN_SECONDS); + continue; + } + countdownSeconds = seconds; + continue; + } + // A line with a comma is a custom entry, even if its name starts with @. if (line[0] == '@' && !strchr(line, ',')) { char *name = line + 1; @@ -204,7 +222,8 @@ void writeAutobootOption(std::string &configPath, int32_t autobootOption, const } } -void drawMenuScreen(const BootMenu &menu, uint32_t selectedIndex, uint32_t autobootIndex, bool updatesBlocked) { +// `secondsLeft` > 0: the countdown to the autoboot entry is running, shown above the bottom bar. +void drawMenuScreen(const BootMenu &menu, uint32_t selectedIndex, uint32_t autobootIndex, bool updatesBlocked, uint32_t secondsLeft) { DrawUtils::beginDraw(); DrawUtils::clear(COLOR_BACKGROUND); @@ -227,6 +246,15 @@ void drawMenuScreen(const BootMenu &menu, uint32_t selectedIndex, uint32_t autob DrawUtils::setFontColor(COLOR_TEXT); + if (secondsLeft > 0 && autobootIndex < menu.size()) { + std::string countdown = string_format("Starting %s in %u... Press any button to stay in this menu.", + menu[autobootIndex].second.c_str(), (unsigned int) secondsLeft); + DrawUtils::setFontSize(20); + DrawUtils::setFontColor(COLOR_AUTOBOOT); + DrawUtils::print(16, SCREEN_HEIGHT - 24 - 8 - 4 - 24, countdown.c_str()); + DrawUtils::setFontColor(COLOR_TEXT); + } + // draw top bar DrawUtils::setFontSize(24); DrawUtils::print(16, 6 + 24, "Boot Selector"); @@ -254,7 +282,7 @@ void drawMenuScreen(const BootMenu &menu, uint32_t selectedIndex, uint32_t autob } int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, const BootMenu &menu, - const std::vector &customOptions, bool ordered) { + const std::vector &customOptions, bool ordered, uint32_t countdownSeconds) { auto screenBuffer = DrawUtils::InitOSScreen(); if (!screenBuffer) { OSFatal("AutobootModule: Failed to alloc memory for screen"); @@ -288,18 +316,39 @@ int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, c // a saved custom entry whose title isn't installed (the menu gets -1 for it). bool autobootChanged = false; + // The countdown runs only towards an autoboot entry the menu shows, and stops for good at the + // first button pressed (or when the pairing screen opens). + bool countingDown = countdownSeconds > 0 && autobootIndex >= 0; + OSTime countdownStarted = OSGetTime(); + { PairMenu pairMenu; int32_t holdUpdateBlockedForFrames = 0; while (true) { if (pairMenu.ProcessPairScreen()) { + countingDown = false; continue; } InputUtils::InputData input = InputUtils::getControllerInput(); + uint32_t secondsLeft = 0; + // The first button only stops the countdown; it isn't also a menu action. + if (countingDown && input.trigger != 0) { + countingDown = false; + input.trigger = 0; + } + if (countingDown) { + uint32_t elapsed = (uint32_t) OSTicksToSeconds(OSGetTime() - countdownStarted); + if (elapsed >= countdownSeconds) { + selectedIndex = autobootIndex; + break; + } + secondsLeft = countdownSeconds - elapsed; + } + if (input.trigger & VPAD_BUTTON_UP) { selectedIndex--; @@ -336,7 +385,7 @@ int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, c holdUpdateBlockedForFrames = 0; } - drawMenuScreen(menu, selectedIndex, autobootIndex, gUpdatesBlocked); + drawMenuScreen(menu, selectedIndex, autobootIndex, gUpdatesBlocked, secondsLeft); } } diff --git a/source/MenuUtils.h b/source/MenuUtils.h index 0dc72db..7777ffc 100644 --- a/source/MenuUtils.h +++ b/source/MenuUtils.h @@ -44,10 +44,16 @@ struct CustomBootOption { uint64_t titleId; }; +// The longest countdown a "countdown=" line may ask for, in seconds. +#define MAX_MENU_COUNTDOWN_SECONDS 30 + // Reads the custom entries from bootOptions.cfg. When the file also has "@" lines naming // built-in entries, `menuOrder` gets every listed entry's option id in the file's order; -// without them it stays empty and the menu keeps the usual order. -std::vector readCustomBootOptions(const std::string &path, std::vector &menuOrder); +// without them it stays empty and the menu keeps the usual order. A line "countdown=5" sets +// `countdownSeconds` (0 without one): the menu then shows at every boot even with an autoboot +// entry, and starts that entry when the countdown ends unless a button is pressed. +std::vector readCustomBootOptions(const std::string &path, std::vector &menuOrder, + uint32_t &countdownSeconds); int32_t readAutobootOption(std::string &configPath, const std::vector &customOptions); @@ -55,8 +61,10 @@ void writeAutobootOption(std::string &configPath, int32_t autobootOption, const // `ordered`: bootOptions.cfg sets the menu order; with no autoboot entry the cursor then // starts on the first entry instead of the Wii U Menu. +// `countdownSeconds` > 0 with an autoboot entry in the menu: that entry starts by itself +// when the countdown ends, unless a button was pressed first. int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, const BootMenu &menu, - const std::vector &customOptions, bool ordered); + const std::vector &customOptions, bool ordered, uint32_t countdownSeconds); nn::act::SlotNo handleAccountSelectScreen(const std::vector> &data); diff --git a/source/main.cpp b/source/main.cpp index 4ccb75e..0be9b06 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -102,9 +102,10 @@ int32_t main(int32_t argc, char **argv) { } std::vector menuOrder; - auto customOptions = readCustomBootOptions(bootOptionsPath, menuOrder); - const bool ordered = !menuOrder.empty(); - int32_t bootSelection = readAutobootOption(configPath, customOptions); + uint32_t countdownSeconds = 0; + auto customOptions = readCustomBootOptions(bootOptionsPath, menuOrder, countdownSeconds); + const bool ordered = !menuOrder.empty(); + int32_t bootSelection = readAutobootOption(configPath, customOptions); // The usual order: the built-in entries, then the custom ones. Entries that bootOptions.cfg // orders with "@" lines come first; the ones it doesn't list follow in the usual order. @@ -164,7 +165,9 @@ int32_t main(int32_t argc, char **argv) { return false; }; - if ((bootSelection == -1) || + // With a countdown set, the menu shows at every boot and counts down to the autoboot entry, + // so a console with no controller in reach still starts that entry by itself. + if ((bootSelection == -1) || (countdownSeconds > 0) || (bootSelection == BOOT_OPTION_HOMEBREW_LAUNCHER && !showHBL) || (bootSelection == BOOT_OPTION_VWII_HOMEBREW_CHANNEL && !showvHBL) || (bootSelection >= BOOT_OPTION_CUSTOM_BASE && !inMenu(bootSelection)) || @@ -176,8 +179,14 @@ int32_t main(int32_t argc, char **argv) { // Launcher or vWii Homebrew Channel) stays saved and comes back once it's available again, // instead of being preselected or overwritten by the menu. int32_t menuInput = (bootSelection >= 0 && !inMenu(bootSelection)) ? -1 : bootSelection; - bootSelection = handleMenuScreen(configPath, menuInput, menu, customOptions, ordered); - hadMenu = true; + // Holding START (+) at boot asks for the menu, so there's no countdown then. The buttons + // are read again too, as START may have been held while the update warning was open, and a + // button pressed in that read stops the countdown, as any first press in the menu does. + auto input = InputUtils::getControllerInput(); + bool startHeld = ((buttons.hold | input.hold) & VPAD_BUTTON_PLUS) != 0; + uint32_t countdown = (startHeld || input.trigger) ? 0 : countdownSeconds; + bootSelection = handleMenuScreen(configPath, menuInput, menu, customOptions, ordered, countdown); + hadMenu = true; } if (bootSelection >= 0) {