diff --git a/arm11/Makefile b/arm11/Makefile index 3be3b6c..70cc265 100644 --- a/arm11/Makefile +++ b/arm11/Makefile @@ -15,7 +15,7 @@ # all directories are relative to this makefile #--------------------------------------------------------------------------------- BUILD := build -SOURCES := ../source ../source/hardware ../source/arm11 ../source/arm11/hardware +SOURCES := ../source ../source/hardware ../source/arm11 ../source/arm11/hardware ../source/arm11/menu INCLUDES := ../include ../thirdparty DEFINES := -DARM11 -D_3DS -DVERS_STRING=\"$(VERS_STRING)\" \ -DVERS_MAJOR=$(VERS_MAJOR) -DVERS_MINOR=$(VERS_MINOR) diff --git a/include/arm11/menu.h b/include/arm11/menu.h deleted file mode 100644 index da745ec..0000000 --- a/include/arm11/menu.h +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once - -/* - * This file is part of fastboot 3DS - * Copyright (C) 2017 derrek, profi200, d0k3 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "types.h" -#include "arm11/console.h" - -#define MENU_MAX_ENTRIES 8 -#define MENU_MAX_DEPTH 4 - -#define MENU_DISP_Y -2 -#define MENU_OFFSET_TITLE 5 -#define MENU_OFFSET_BUTTONS 18 -#define MENU_WIDTH 28 -#define WORDWRAP_WIDTH 58 - -#define MENU_EXIT_REBOOT 0 -#define MENU_EXIT_POWEROFF 1 - -/** - * @brief Menu entry description struct, used by MenuInfo. - */ -typedef struct { - char* name; ///< Displayed name of the menu entry. - char* desc; ///< Description for menu entry. - u32 (*function)(PrintConsole *con, u32 param); ///< Function called by menu entry. - u32 param; ///< Paramater for menu entry function / if function == NULL, offset to is_sub_menu. -} MenuEntry; - -/** - * @brief Menu info description struct, used in an array to represent the menu. - */ -typedef struct { - char* name; ///< Displayed name on top of the menu. - u32 n_entries; ///< Number of entries in the menu. - u32 (*preset)(void); ///< Returns the preset of the current menu in bitwise format. - MenuEntry entries[MENU_MAX_ENTRIES]; ///< An array of menu entries. -} MenuInfo; - -u32 menuProcess(PrintConsole* menu_con, PrintConsole* desc_con, MenuInfo* info); diff --git a/include/arm11/menu/menu.h b/include/arm11/menu/menu.h new file mode 100644 index 0000000..da745ec --- /dev/null +++ b/include/arm11/menu/menu.h @@ -0,0 +1,56 @@ +#pragma once + +/* + * This file is part of fastboot 3DS + * Copyright (C) 2017 derrek, profi200, d0k3 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "types.h" +#include "arm11/console.h" + +#define MENU_MAX_ENTRIES 8 +#define MENU_MAX_DEPTH 4 + +#define MENU_DISP_Y -2 +#define MENU_OFFSET_TITLE 5 +#define MENU_OFFSET_BUTTONS 18 +#define MENU_WIDTH 28 +#define WORDWRAP_WIDTH 58 + +#define MENU_EXIT_REBOOT 0 +#define MENU_EXIT_POWEROFF 1 + +/** + * @brief Menu entry description struct, used by MenuInfo. + */ +typedef struct { + char* name; ///< Displayed name of the menu entry. + char* desc; ///< Description for menu entry. + u32 (*function)(PrintConsole *con, u32 param); ///< Function called by menu entry. + u32 param; ///< Paramater for menu entry function / if function == NULL, offset to is_sub_menu. +} MenuEntry; + +/** + * @brief Menu info description struct, used in an array to represent the menu. + */ +typedef struct { + char* name; ///< Displayed name on top of the menu. + u32 n_entries; ///< Number of entries in the menu. + u32 (*preset)(void); ///< Returns the preset of the current menu in bitwise format. + MenuEntry entries[MENU_MAX_ENTRIES]; ///< An array of menu entries. +} MenuInfo; + +u32 menuProcess(PrintConsole* menu_con, PrintConsole* desc_con, MenuInfo* info); diff --git a/include/arm11/menu/menu_fb3ds.h b/include/arm11/menu/menu_fb3ds.h new file mode 100644 index 0000000..0f80a34 --- /dev/null +++ b/include/arm11/menu/menu_fb3ds.h @@ -0,0 +1,135 @@ +#pragma once + +/* + * This file is part of fastboot 3DS + * Copyright (C) 2017 derrek, profi200, d0k3 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "types.h" +#include "arm11/menu/menu.h" +#include "arm11/menu/menu_func.h" + +#define LOREM "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata" + +#define DESC_CONTINUE "Continue booting the first available boot slot.\nNo function if boot slots are not set up." +#define DESC_BOOT_MENU "Display a boot menu, allowing you to select which boot slot to boot from." +#define DESC_BOOT_FILE "Select a firmware to boot." +#define DESC_NAND_TOOLS "Enter NAND tools submenu, including tools for NAND backup, NAND restore and firmware flash." +#define DESC_OPTIONS "Enter fastboot3ds settings submenu." +#define DESC_MISC "Enter miscellaneous submenu, including the update tool and credits section." + +#define DESC_BOOT_SLOT_1 "Boot slot #1." +#define DESC_BOOT_SLOT_2 "Boot slot #2." +#define DESC_BOOT_SLOT_3 "Boot slot #3." +#define DESC_BOOT_FIRM1 "Boot FIRM1." +#define DESC_CHANGE_SLOT "Setup fastboot3ds boot slots." + +#define DESC_NAND_BACKUP "Backup current NAND to a file." +#define DESC_NAND_RESTORE "Restore current NAND from a file.\nThis option preserves your fastboot3ds installation." +#define DESC_NAND_RESTORE_F "Restore current NAND from a file.\nWARNING: This will overwrite all of your flash memory, also overwriting fastboot3ds installation." +#define DESC_FIRM_FLASH "Flash firmware from file.\nWARNING: This will allow you to flash unsigned firmware, overwriting fastboot3ds. Using unsupported firmware can BRICK your console." + +#define DESC_CHANGE_BOOT "Change fastboot3ds boot mode. This allows you to set up how your console boots." +#define DESC_CHANGE_BRIGHT "Change FastBood3DS brightness. This may also affect firmware launched from within fastboot3ds" +#define DESC_SET_CONTIG "Enable/disable contiguous NAND backups.\nContiguous NAND backups may be required on certain CFWS to be bootable from SD cards." + +#define DESC_BOOT_NORMAL "Set normal boot mode.\nIn normal boot mode, you will be presented with the fastboot3ds menu upon boot." +#define DESC_BOOT_QUICK "Set quick boot mode.\nIn quick boot mode, splash is displayed and the boot is continued via the first available boot slot. To enter fastboot3ds menu, hold a key combo." +#define DESC_BOOT_QUIET "Set quiet boot mode.\nIn quiet boot mode, splash is not displayed and the boot is continued via the first available boot slot. To enter fastboot3ds menu, hold a key combo." + +#define DESC_SET_SLOT_1 "Setup boot slot #1." +#define DESC_SET_SLOT_2 "Setup boot slot #2." +#define DESC_SET_SLOT_3 "Setup boot slot #3." + +#define DESC_ENABLE_CONTIG "Select to enable contiguous NAND backups." +#define DESC_DISABLE_CONTIG "Select to disable contiguous NAND backups." + +#define DESC_UPDATE "Update fastboot3ds. Only signed updates are allowed." +#define DESC_CREDITS "Show fastboot3ds credits." + + +MenuInfo menu_fb3ds[] = +{ + { + "Main Menu", 6, NULL, + { + { "Continue boot", DESC_CONTINUE, &DummyFunc, 0 }, + { "Boot menu...", DESC_BOOT_MENU, NULL, 1 }, + { "Boot from file...", DESC_BOOT_FILE, &DummyFunc, 2 }, + { "NAND tools...", DESC_NAND_TOOLS, NULL, 3 }, + { "Settings...", DESC_OPTIONS, NULL, 4 }, + { "Miscellaneous...", DESC_MISC, NULL, 7 } + } + }, + { // 1 + "Boot Menu", 5, NULL, + { + { "Boot [slot 1]", DESC_BOOT_SLOT_1, &DummyFunc, 0 }, + { "Boot [slot 2]", DESC_BOOT_SLOT_2, &DummyFunc, 1 }, + { "Boot [slot 3]", DESC_BOOT_SLOT_3, &DummyFunc, 2 }, + { "Boot from FIRM1", DESC_BOOT_FIRM1, &DummyFunc, 2 }, + { "Setup boot slots...", DESC_CHANGE_SLOT, NULL, 2 }, + } + }, + { // 2 + "Boot Slot Setup", 3, NULL, + { + { "Setup [slot 1]...", DESC_SET_SLOT_1, &DummyFunc, 0 }, + { "Setup [slot 2]...", DESC_SET_SLOT_2, &DummyFunc, 1 }, + { "Setup [slot 3]...", DESC_SET_SLOT_3, &DummyFunc, 2 } + } + }, + { // 3 + "NAND Tools", 4, NULL, + { + { "Backup NAND", DESC_NAND_BACKUP, &DummyFunc, 3 }, + { "Restore NAND", DESC_NAND_RESTORE, &DummyFunc, 4 }, + { "Restore NAND (forced)", DESC_NAND_RESTORE_F, &DummyFunc, 4 }, + { "Flash firmware", DESC_FIRM_FLASH, &DummyFunc, 2 } + } + }, + { // 4 + "Settings", 3, NULL, + { + { "Change boot mode", DESC_CHANGE_BOOT, NULL, 5 }, + { "Change brightness", DESC_CHANGE_BRIGHT, &DummyFunc, 8 }, + { "Set contiguous backups", DESC_SET_CONTIG, NULL, 6 } + } + }, + { // 5 + "Boot Mode Setup", 3, &menuPresetBootMode, + { + { "Set normal boot", DESC_BOOT_NORMAL, &menuSetBootMode, 0 }, + { "Set quiet boot", DESC_BOOT_QUIET, &menuSetBootMode, 1 }, + { "Set quick boot", DESC_BOOT_QUICK, &menuSetBootMode, 2 } + } + }, + { // 6 + "Backup Setup", 2, NULL, + { + { "Enable contiguous backups", DESC_ENABLE_CONTIG, &DummyFunc, 1 }, + { "Disable contiguous backups", DESC_DISABLE_CONTIG, &DummyFunc, 0 } + } + }, + { // 7 + "Miscellaneous", 3, NULL, + { + { "Update fastboot3DS", DESC_UPDATE, &DummyFunc, 1 }, + { "Credits", DESC_CREDITS, &ShowCredits, 0 }, + { "View current settings", "View current settings", &SetView, 0 } // needs to be removed later + } + } +}; diff --git a/include/arm11/menu/menu_func.h b/include/arm11/menu/menu_func.h new file mode 100644 index 0000000..3a159da --- /dev/null +++ b/include/arm11/menu/menu_func.h @@ -0,0 +1,27 @@ +#pragma once + +/* + * This file is part of fastboot 3DS + * Copyright (C) 2017 derrek, profi200, d0k3 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "types.h" + +u32 menuPresetBootMode(void); +u32 menuSetBootMode(PrintConsole* con, u32 param); +u32 DummyFunc(PrintConsole* con, u32 param); +u32 SetView(PrintConsole* con, u32 param); +u32 ShowCredits(PrintConsole* con, u32 param); diff --git a/include/arm11/menu_fb3ds.h b/include/arm11/menu_fb3ds.h deleted file mode 100644 index 3695716..0000000 --- a/include/arm11/menu_fb3ds.h +++ /dev/null @@ -1,135 +0,0 @@ -#pragma once - -/* - * This file is part of fastboot 3DS - * Copyright (C) 2017 derrek, profi200, d0k3 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "types.h" -#include "menu.h" -#include "menu_func.h" - -#define LOREM "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata" - -#define DESC_CONTINUE "Continue booting the first available boot slot.\nNo function if boot slots are not set up." -#define DESC_BOOT_MENU "Display a boot menu, allowing you to select which boot slot to boot from." -#define DESC_BOOT_FILE "Select a firmware to boot." -#define DESC_NAND_TOOLS "Enter NAND tools submenu, including tools for NAND backup, NAND restore and firmware flash." -#define DESC_OPTIONS "Enter fastboot3ds settings submenu." -#define DESC_MISC "Enter miscellaneous submenu, including the update tool and credits section." - -#define DESC_BOOT_SLOT_1 "Boot slot #1." -#define DESC_BOOT_SLOT_2 "Boot slot #2." -#define DESC_BOOT_SLOT_3 "Boot slot #3." -#define DESC_BOOT_FIRM1 "Boot FIRM1." -#define DESC_CHANGE_SLOT "Setup fastboot3ds boot slots." - -#define DESC_NAND_BACKUP "Backup current NAND to a file." -#define DESC_NAND_RESTORE "Restore current NAND from a file.\nThis option preserves your fastboot3ds installation." -#define DESC_NAND_RESTORE_F "Restore current NAND from a file.\nWARNING: This will overwrite all of your flash memory, also overwriting fastboot3ds installation." -#define DESC_FIRM_FLASH "Flash firmware from file.\nWARNING: This will allow you to flash unsigned firmware, overwriting fastboot3ds. Using unsupported firmware can BRICK your console." - -#define DESC_CHANGE_BOOT "Change fastboot3ds boot mode. This allows you to set up how your console boots." -#define DESC_CHANGE_BRIGHT "Change FastBood3DS brightness. This may also affect firmware launched from within fastboot3ds" -#define DESC_SET_CONTIG "Enable/disable contiguous NAND backups.\nContiguous NAND backups may be required on certain CFWS to be bootable from SD cards." - -#define DESC_BOOT_NORMAL "Set normal boot mode.\nIn normal boot mode, you will be presented with the fastboot3ds menu upon boot." -#define DESC_BOOT_QUICK "Set quick boot mode.\nIn quick boot mode, splash is displayed and the boot is continued via the first available boot slot. To enter fastboot3ds menu, hold a key combo." -#define DESC_BOOT_QUIET "Set quiet boot mode.\nIn quiet boot mode, splash is not displayed and the boot is continued via the first available boot slot. To enter fastboot3ds menu, hold a key combo." - -#define DESC_SET_SLOT_1 "Setup boot slot #1." -#define DESC_SET_SLOT_2 "Setup boot slot #2." -#define DESC_SET_SLOT_3 "Setup boot slot #3." - -#define DESC_ENABLE_CONTIG "Select to enable contiguous NAND backups." -#define DESC_DISABLE_CONTIG "Select to disable contiguous NAND backups." - -#define DESC_UPDATE "Update fastboot3ds. Only signed updates are allowed." -#define DESC_CREDITS "Show fastboot3ds credits." - - -MenuInfo menu_fb3ds[] = -{ - { - "Main Menu", 6, NULL, - { - { "Continue boot", DESC_CONTINUE, &DummyFunc, 0 }, - { "Boot menu...", DESC_BOOT_MENU, NULL, 1 }, - { "Boot from file...", DESC_BOOT_FILE, &DummyFunc, 2 }, - { "NAND tools...", DESC_NAND_TOOLS, NULL, 3 }, - { "Settings...", DESC_OPTIONS, NULL, 4 }, - { "Miscellaneous...", DESC_MISC, NULL, 7 } - } - }, - { // 1 - "Boot Menu", 5, NULL, - { - { "Boot [slot 1]", DESC_BOOT_SLOT_1, &DummyFunc, 0 }, - { "Boot [slot 2]", DESC_BOOT_SLOT_2, &DummyFunc, 1 }, - { "Boot [slot 3]", DESC_BOOT_SLOT_3, &DummyFunc, 2 }, - { "Boot from FIRM1", DESC_BOOT_FIRM1, &DummyFunc, 2 }, - { "Setup boot slots...", DESC_CHANGE_SLOT, NULL, 2 }, - } - }, - { // 2 - "Boot Slot Setup", 3, NULL, - { - { "Setup [slot 1]...", DESC_SET_SLOT_1, &DummyFunc, 0 }, - { "Setup [slot 2]...", DESC_SET_SLOT_2, &DummyFunc, 1 }, - { "Setup [slot 3]...", DESC_SET_SLOT_3, &DummyFunc, 2 } - } - }, - { // 3 - "NAND Tools", 4, NULL, - { - { "Backup NAND", DESC_NAND_BACKUP, &DummyFunc, 3 }, - { "Restore NAND", DESC_NAND_RESTORE, &DummyFunc, 4 }, - { "Restore NAND (forced)", DESC_NAND_RESTORE_F, &DummyFunc, 4 }, - { "Flash firmware", DESC_FIRM_FLASH, &DummyFunc, 2 } - } - }, - { // 4 - "Settings", 3, NULL, - { - { "Change boot mode", DESC_CHANGE_BOOT, NULL, 5 }, - { "Change brightness", DESC_CHANGE_BRIGHT, &DummyFunc, 8 }, - { "Set contiguous backups", DESC_SET_CONTIG, NULL, 6 } - } - }, - { // 5 - "Boot Mode Setup", 3, &menuPresetBootMode, - { - { "Set normal boot", DESC_BOOT_NORMAL, &menuSetBootMode, 0 }, - { "Set quiet boot", DESC_BOOT_QUIET, &menuSetBootMode, 1 }, - { "Set quick boot", DESC_BOOT_QUICK, &menuSetBootMode, 2 } - } - }, - { // 6 - "Backup Setup", 2, NULL, - { - { "Enable contiguous backups", DESC_ENABLE_CONTIG, &DummyFunc, 1 }, - { "Disable contiguous backups", DESC_DISABLE_CONTIG, &DummyFunc, 0 } - } - }, - { // 7 - "Miscellaneous", 3, NULL, - { - { "Update fastboot3DS", DESC_UPDATE, &DummyFunc, 1 }, - { "Credits", DESC_CREDITS, &ShowCredits, 0 }, - { "View current settings", "View current settings", &SetView, 0 } - } - } -}; diff --git a/include/arm11/menu_func.h b/include/arm11/menu_func.h deleted file mode 100644 index 3a159da..0000000 --- a/include/arm11/menu_func.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -/* - * This file is part of fastboot 3DS - * Copyright (C) 2017 derrek, profi200, d0k3 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "types.h" - -u32 menuPresetBootMode(void); -u32 menuSetBootMode(PrintConsole* con, u32 param); -u32 DummyFunc(PrintConsole* con, u32 param); -u32 SetView(PrintConsole* con, u32 param); -u32 ShowCredits(PrintConsole* con, u32 param); diff --git a/source/arm11/main.c b/source/arm11/main.c index f27bbd1..bb849c4 100644 --- a/source/arm11/main.c +++ b/source/arm11/main.c @@ -22,11 +22,11 @@ #include "arm11/hardware/hid.h" #include "arm11/power.h" #include "arm11/hardware/i2c.h" +#include "arm11/menu/menu.h" +#include "arm11/menu/menu_fb3ds.h" #include "hardware/pxi.h" #include "hardware/gfx.h" #include "arm11/firm.h" -#include "arm11/menu.h" -#include "arm11/menu_fb3ds.h" #include "arm11/console.h" #include "arm11/fmt.h" #include "arm11/fsutils.h" diff --git a/source/arm11/menu.c b/source/arm11/menu.c deleted file mode 100644 index a777afa..0000000 --- a/source/arm11/menu.c +++ /dev/null @@ -1,249 +0,0 @@ -/* - * This file is part of fastboot 3DS - * Copyright (C) 2017 derrek, profi200, d0k3 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include "types.h" -#include "arm11/menu.h" -#include "arm11/hardware/hid.h" -#include "arm11/console.h" -#include "arm11/fmt.h" -#include "arm11/power.h" -#include "hardware/gfx.h" - - - -u32 stringGetHeight(const char* str) { - u32 height = 1; - for (char* lf = strchr(str, '\n'); (lf != NULL); lf = strchr(lf + 1, '\n')) - height++; - return height; -} - -u32 stringGetWidth(const char* str) { - u32 width = 0; - char* old_lf = (char*) str; - char* str_end = (char*) str + strlen(str); - for (char* lf = strchr(str, '\n'); lf != NULL; lf = strchr(lf + 1, '\n')) { - if ((u32) (lf - old_lf) > width) width = lf - old_lf; - old_lf = lf; - } - if ((u32) (str_end - old_lf) > width) - width = str_end - old_lf; - return width; -} - -void stringWordWrap(char* str, int llen) { - char* last_brk = str - 1; - char* last_spc = str - 1; - for (char* str_ptr = str;; str_ptr++) { - if (!*str_ptr || (*str_ptr == ' ')) { // on space or string_end - if (str_ptr - last_brk > llen) { // if maximum line lenght is exceeded - if (last_spc > last_brk) { // put a line_brk at the last space - *last_spc = '\n'; - last_brk = last_spc; - last_spc = str_ptr; - } else if (*str_ptr) { // if we have no applicable space - *str_ptr = '\n'; - last_brk = str_ptr; - } - } else if (*str_ptr) last_spc = str_ptr; - } else if (*str_ptr == '\n') last_brk = str_ptr; - if (!*str_ptr) break; - } -} - -void menuShowDesc(MenuInfo* curr_menu, PrintConsole* desc_con, u32 index) -{ - // select and clear description console - consoleSelect(desc_con); - consoleClear(); - - // print title at the top - const char* title = "fastboot 3DS " VERS_STRING; - consoleSetCursor(desc_con, (desc_con->consoleWidth - strlen(title)) >> 1, 1); - ee_printf(title); - - // get description, check if available - MenuEntry* entry = &(curr_menu->entries[index]); - char* name = entry->name; - char* desc = entry->desc; - - // done if no description available - if (!name || !desc) - { - return; - } - - - // word wrap description string - char desc_ww[512]; - strncpy(desc_ww, desc, 512); - stringWordWrap(desc_ww, WORDWRAP_WIDTH); - - // get width, height - int desc_width = stringGetWidth(desc_ww); - int desc_height = stringGetHeight(desc_ww); - desc_width = (desc_width > desc_con->consoleWidth) ? desc_con->consoleWidth : desc_width; - desc_height = (desc_height > desc_con->consoleHeight) ? desc_con->consoleHeight : desc_height; - - // write to console - int desc_x = (desc_con->consoleWidth - desc_width) >> 1; - int desc_y = (desc_con->consoleHeight - 2 - desc_height); - - consoleSetCursor(desc_con, desc_x, desc_y++); - ee_printf("%s:", name); - - for (char* str = strtok(desc_ww, "\n"); str != NULL; str = strtok(NULL, "\n")) { - consoleSetCursor(desc_con, desc_x, desc_y++); - ee_printf(str); - } -} - -/** - * @brief Draws the menu to the given console. - * @param curr_menu A struct containing info about the current menu. - * @param menu_con Console that the menu is displayed on. - * @param index Current placement of the cursor. - */ -void menuDraw(MenuInfo* curr_menu, PrintConsole* menu_con, u32 index) -{ - // const u32 menu_block_height = curr_menu->n_entries + 5; // +5 for title and button instructions - int menu_x = (menu_con->consoleWidth - MENU_WIDTH) >> 1; - // int menu_y = MENU_DISP_Y + ((menu_con->consoleHeight - menu_block_height) >> 1); - int menu_y = MENU_OFFSET_TITLE; - int menu_preset = 0; - - // get menu preset - if (curr_menu->preset) - menu_preset = curr_menu->preset(); - - // select menu console - consoleSelect(menu_con); - consoleClear(); - - // menu title - consoleSetCursor(menu_con, menu_x, menu_y++); - ee_printf(curr_menu->name); - menu_y++; - - // menu entries - for (u32 i = 0; i < curr_menu->n_entries; i++) - { - MenuEntry* entry = &(curr_menu->entries[i]); - char* name = entry->name; - bool is_selected = (i == index); - - consoleSetCursor(menu_con, menu_x, menu_y++); - ee_printf(is_selected ? "\x1b[47;30m%.3s %-*.*s\x1b[0m" : "%.3s %-*.*s", - ((menu_preset >> i) & 0x1) ? "[X]" : "[ ]", MENU_WIDTH-3, MENU_WIDTH-3, name); - } - - // button instructions - menu_y = MENU_OFFSET_BUTTONS; - consoleSetCursor(menu_con, menu_x, menu_y++); - ee_printf("%-*.*s", MENU_WIDTH, MENU_WIDTH, "[A]:Choose [B]:Back"); -} - -/** - * @brief Processes and displays the menu and user input. - * @param menu_con Console that the menu is displayed on. - * @param desc_con Console that the description is displayed on. - * @param info A complete description of the menu. - */ -u32 menuProcess(PrintConsole* menu_con, PrintConsole* desc_con, MenuInfo* info) -{ - MenuInfo* curr_menu = info; - MenuInfo* last_menu = NULL; - MenuInfo* prev_menu[MENU_MAX_DEPTH]; - u32 prev_index[MENU_MAX_DEPTH]; - u32 menu_lvl = 0; - u32 index = 0; - u32 last_index = (u32) -1; - u32 result = MENU_EXIT_REBOOT; - - // main menu processing loop - while (true) { - // update menu and description (on demand) - if ((index != last_index) || (curr_menu != last_menu)) { - menuDraw(curr_menu, menu_con, index); - menuShowDesc(curr_menu, desc_con, index); - last_index = index; - last_menu = curr_menu; - - GX_textureCopy((u64*)RENDERBUF_TOP, 0, (u64*)GFX_getFramebuffer(SCREEN_TOP), - 0, SCREEN_SIZE_TOP + SCREEN_SIZE_SUB); - GFX_swapFramebufs(); - } - GFX_waitForEvent(GFX_EVENT_PDC0, true); // VBlank - - if(hidGetPowerButton(true)) // handle power button - { - power_off(); - } - - hidScanInput(); - const u32 kDown = hidKeysDown(); - // const u32 kHeld = hidKeysHeld(); - - if ((kDown & KEY_A) && (curr_menu->entries[index].function == NULL)) - { - // store previous menu and index for return - if (menu_lvl < MENU_MAX_DEPTH) - { - prev_menu[menu_lvl] = curr_menu; - prev_index[menu_lvl] = index; - menu_lvl++; - } - // enter submenu - curr_menu = info + curr_menu->entries[index].param; - index = 0; - } - else if (kDown & KEY_A) - { - // call menu entry function - MenuEntry* entry = &(curr_menu->entries[index]); - (*(entry->function))(desc_con, entry->param); - // force redraw (somewhat hacky) - last_menu = NULL; - } - else if ((kDown & KEY_B) && (menu_lvl > 0)) - { - // return to previous menu - menu_lvl--; - curr_menu = prev_menu[menu_lvl]; - index = prev_index[menu_lvl]; - } - else if (kDown & KEY_DDOWN) - { - // cursor down - index = (index == curr_menu->n_entries - 1) ? 0 : index + 1; - } - else if (kDown & KEY_DUP) - { - // cursor up - index = (index == 0) ? curr_menu->n_entries - 1 : index - 1; - } - /*if (kDown & KEY_START) - { - result = (kHeld & KEY_DLEFT) ? MENU_EXIT_POWEROFF : MENU_EXIT_REBOOT; - break; - }*/ - } - - return result; -} diff --git a/source/arm11/menu/menu.c b/source/arm11/menu/menu.c new file mode 100644 index 0000000..08e43eb --- /dev/null +++ b/source/arm11/menu/menu.c @@ -0,0 +1,249 @@ +/* + * This file is part of fastboot 3DS + * Copyright (C) 2017 derrek, profi200, d0k3 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include "types.h" +#include "arm11/menu/menu.h" +#include "arm11/hardware/hid.h" +#include "arm11/console.h" +#include "arm11/fmt.h" +#include "arm11/power.h" +#include "hardware/gfx.h" + + + +u32 stringGetHeight(const char* str) { + u32 height = 1; + for (char* lf = strchr(str, '\n'); (lf != NULL); lf = strchr(lf + 1, '\n')) + height++; + return height; +} + +u32 stringGetWidth(const char* str) { + u32 width = 0; + char* old_lf = (char*) str; + char* str_end = (char*) str + strlen(str); + for (char* lf = strchr(str, '\n'); lf != NULL; lf = strchr(lf + 1, '\n')) { + if ((u32) (lf - old_lf) > width) width = lf - old_lf; + old_lf = lf; + } + if ((u32) (str_end - old_lf) > width) + width = str_end - old_lf; + return width; +} + +void stringWordWrap(char* str, int llen) { + char* last_brk = str - 1; + char* last_spc = str - 1; + for (char* str_ptr = str;; str_ptr++) { + if (!*str_ptr || (*str_ptr == ' ')) { // on space or string_end + if (str_ptr - last_brk > llen) { // if maximum line lenght is exceeded + if (last_spc > last_brk) { // put a line_brk at the last space + *last_spc = '\n'; + last_brk = last_spc; + last_spc = str_ptr; + } else if (*str_ptr) { // if we have no applicable space + *str_ptr = '\n'; + last_brk = str_ptr; + } + } else if (*str_ptr) last_spc = str_ptr; + } else if (*str_ptr == '\n') last_brk = str_ptr; + if (!*str_ptr) break; + } +} + +void menuShowDesc(MenuInfo* curr_menu, PrintConsole* desc_con, u32 index) +{ + // select and clear description console + consoleSelect(desc_con); + consoleClear(); + + // print title at the top + const char* title = "fastboot 3DS " VERS_STRING; + consoleSetCursor(desc_con, (desc_con->consoleWidth - strlen(title)) >> 1, 1); + ee_printf(title); + + // get description, check if available + MenuEntry* entry = &(curr_menu->entries[index]); + char* name = entry->name; + char* desc = entry->desc; + + // done if no description available + if (!name || !desc) + { + return; + } + + + // word wrap description string + char desc_ww[512]; + strncpy(desc_ww, desc, 512); + stringWordWrap(desc_ww, WORDWRAP_WIDTH); + + // get width, height + int desc_width = stringGetWidth(desc_ww); + int desc_height = stringGetHeight(desc_ww); + desc_width = (desc_width > desc_con->consoleWidth) ? desc_con->consoleWidth : desc_width; + desc_height = (desc_height > desc_con->consoleHeight) ? desc_con->consoleHeight : desc_height; + + // write to console + int desc_x = (desc_con->consoleWidth - desc_width) >> 1; + int desc_y = (desc_con->consoleHeight - 2 - desc_height); + + consoleSetCursor(desc_con, desc_x, desc_y++); + ee_printf("%s:", name); + + for (char* str = strtok(desc_ww, "\n"); str != NULL; str = strtok(NULL, "\n")) { + consoleSetCursor(desc_con, desc_x, desc_y++); + ee_printf(str); + } +} + +/** + * @brief Draws the menu to the given console. + * @param curr_menu A struct containing info about the current menu. + * @param menu_con Console that the menu is displayed on. + * @param index Current placement of the cursor. + */ +void menuDraw(MenuInfo* curr_menu, PrintConsole* menu_con, u32 index) +{ + // const u32 menu_block_height = curr_menu->n_entries + 5; // +5 for title and button instructions + int menu_x = (menu_con->consoleWidth - MENU_WIDTH) >> 1; + // int menu_y = MENU_DISP_Y + ((menu_con->consoleHeight - menu_block_height) >> 1); + int menu_y = MENU_OFFSET_TITLE; + int menu_preset = 0; + + // get menu preset + if (curr_menu->preset) + menu_preset = curr_menu->preset(); + + // select menu console + consoleSelect(menu_con); + consoleClear(); + + // menu title + consoleSetCursor(menu_con, menu_x, menu_y++); + ee_printf(curr_menu->name); + menu_y++; + + // menu entries + for (u32 i = 0; i < curr_menu->n_entries; i++) + { + MenuEntry* entry = &(curr_menu->entries[i]); + char* name = entry->name; + bool is_selected = (i == index); + + consoleSetCursor(menu_con, menu_x, menu_y++); + ee_printf(is_selected ? "\x1b[47;30m%.3s %-*.*s\x1b[0m" : "%.3s %-*.*s", + ((menu_preset >> i) & 0x1) ? "[X]" : "[ ]", MENU_WIDTH-3, MENU_WIDTH-3, name); + } + + // button instructions + menu_y = MENU_OFFSET_BUTTONS; + consoleSetCursor(menu_con, menu_x, menu_y++); + ee_printf("%-*.*s", MENU_WIDTH, MENU_WIDTH, "[A]:Choose [B]:Back"); +} + +/** + * @brief Processes and displays the menu and user input. + * @param menu_con Console that the menu is displayed on. + * @param desc_con Console that the description is displayed on. + * @param info A complete description of the menu. + */ +u32 menuProcess(PrintConsole* menu_con, PrintConsole* desc_con, MenuInfo* info) +{ + MenuInfo* curr_menu = info; + MenuInfo* last_menu = NULL; + MenuInfo* prev_menu[MENU_MAX_DEPTH]; + u32 prev_index[MENU_MAX_DEPTH]; + u32 menu_lvl = 0; + u32 index = 0; + u32 last_index = (u32) -1; + u32 result = MENU_EXIT_REBOOT; + + // main menu processing loop + while (true) { + // update menu and description (on demand) + if ((index != last_index) || (curr_menu != last_menu)) { + menuDraw(curr_menu, menu_con, index); + menuShowDesc(curr_menu, desc_con, index); + last_index = index; + last_menu = curr_menu; + + GX_textureCopy((u64*)RENDERBUF_TOP, 0, (u64*)GFX_getFramebuffer(SCREEN_TOP), + 0, SCREEN_SIZE_TOP + SCREEN_SIZE_SUB); + GFX_swapFramebufs(); + } + GFX_waitForEvent(GFX_EVENT_PDC0, true); // VBlank + + if(hidGetPowerButton(true)) // handle power button + { + power_off(); + } + + hidScanInput(); + const u32 kDown = hidKeysDown(); + // const u32 kHeld = hidKeysHeld(); + + if ((kDown & KEY_A) && (curr_menu->entries[index].function == NULL)) + { + // store previous menu and index for return + if (menu_lvl < MENU_MAX_DEPTH) + { + prev_menu[menu_lvl] = curr_menu; + prev_index[menu_lvl] = index; + menu_lvl++; + } + // enter submenu + curr_menu = info + curr_menu->entries[index].param; + index = 0; + } + else if (kDown & KEY_A) + { + // call menu entry function + MenuEntry* entry = &(curr_menu->entries[index]); + (*(entry->function))(desc_con, entry->param); + // force redraw (somewhat hacky) + last_menu = NULL; + } + else if ((kDown & KEY_B) && (menu_lvl > 0)) + { + // return to previous menu + menu_lvl--; + curr_menu = prev_menu[menu_lvl]; + index = prev_index[menu_lvl]; + } + else if (kDown & KEY_DDOWN) + { + // cursor down + index = (index == curr_menu->n_entries - 1) ? 0 : index + 1; + } + else if (kDown & KEY_DUP) + { + // cursor up + index = (index == 0) ? curr_menu->n_entries - 1 : index - 1; + } + /*if (kDown & KEY_START) + { + result = (kHeld & KEY_DLEFT) ? MENU_EXIT_POWEROFF : MENU_EXIT_REBOOT; + break; + }*/ + } + + return result; +} diff --git a/source/arm11/menu/menu_func.c b/source/arm11/menu/menu_func.c new file mode 100644 index 0000000..cb1d934 --- /dev/null +++ b/source/arm11/menu/menu_func.c Binary files differ diff --git a/source/arm11/menu_func.c b/source/arm11/menu_func.c deleted file mode 100644 index cb1d934..0000000 --- a/source/arm11/menu_func.c +++ /dev/null Binary files differ