From 516e78e1f9b6b46c1e66392d55a95717a5d2b066 Mon Sep 17 00:00:00 2001 From: Nick Chan Date: Sun, 29 Jan 2023 13:08:48 +0800 Subject: [PATCH] organization --- include/tui.h | 16 ++++- src/Makefile | 6 +- src/tui_main.c | 91 +++++++++++++++++++++++++++++ src/tui_screen_enter_dfu.c | 20 +++++++ src/tui_screen_enter_recovery.c | 20 +++++++ src/tui_screen_jailbreak.c | 20 +++++++ src/{window.c => tui_screen_main.c} | 83 +------------------------- src/tui_screen_options.c | 20 +++++++ 8 files changed, 190 insertions(+), 86 deletions(-) create mode 100644 src/tui_main.c create mode 100644 src/tui_screen_enter_dfu.c create mode 100644 src/tui_screen_enter_recovery.c create mode 100644 src/tui_screen_jailbreak.c rename src/{window.c => tui_screen_main.c} (65%) create mode 100644 src/tui_screen_options.c diff --git a/include/tui.h b/include/tui.h index b38e3ca..1558f84 100644 --- a/include/tui.h +++ b/include/tui.h @@ -18,7 +18,10 @@ #define MAIN_START "Start" #define MAIN_EXIT "Exit" -enum tui_screens { +#define MSG_COLOR 1 +#define ICON_COLOR 2 + +typedef enum tui_screens { ERROR_SCREEN = -1, EXIT_SCREEN = 0, MAIN_SCREEN = 1, @@ -26,6 +29,15 @@ enum tui_screens { ENTER_DFU_SCREEN = 3, JAILBREAKING_SCREEN = 4, ENTER_RECOVERY_SCREEN = 5 -}; +} tui_screen_t; + +tui_screen_t tui_screen_main(); +tui_screen_t tui_screen_options(); +tui_screen_t tui_screen_enter_recovery(); +tui_screen_t tui_screen_enter_dfu(); +tui_screen_t tui_screen_jailbreak(); + +int redraw_screen(); + #endif diff --git a/src/Makefile b/src/Makefile index e852167..475baa8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,9 +1,11 @@ all: palera1n OBJECTS = main.o dfuhelper.o devhelper.o lockdown_helper.o exec_checkra1n.o -OBJECTS += pongo_usb.o optparse.o override_file.o pongo_helper.o log.o window.o +OBJECTS += pongo_usb.o optparse.o override_file.o pongo_helper.o log.o OBJECTS += xxd-checkra1n.o xxd-checkra1n-kpf-pongo.o xxd-ramdisk.dmg.o -OBJECTS += xxd-binpack.dmg.o +OBJECTS += xxd-binpack.dmg.o tui_main.o tui_screen_options.o +OBJECTS += tui_screen_enter_recovery.o tui_screen_enter_dfu.o +OBJECTS += tui_screen_jailbreak.o tui_screen_main.o CHECKRA1N_VERSION ?= 0.1337.1 LIPO = lipo diff --git a/src/tui_main.c b/src/tui_main.c new file mode 100644 index 0000000..2054718 --- /dev/null +++ b/src/tui_main.c @@ -0,0 +1,91 @@ +#define NCURSES_WIDECHAR 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +int redraw_screen() { + erase(); + resizeterm(24, 80); + box(stdscr, 0, 0); + mvaddstr(0, 80 - strlen("[palera1n version " PALERAIN_VERSION "]") - 2 , "[palera1n version " PALERAIN_VERSION "]"); + return 0; +} + +int destroy_window() { + endwin(); + return 0; +} + +int init_window() { + setlocale(LC_ALL, NULL); + initscr(); + if (has_colors() == TRUE) { + start_color(); + init_pair(MSG_COLOR, COLOR_YELLOW, COLOR_BLACK); + init_pair(ICON_COLOR, COLOR_WHITE, COLOR_BLACK); + } + int height, width; + getmaxyx(stdscr, height, width); + if (width < 80 || height < 24) { + LOG(LOG_ERROR, "Terminal size must be at least 80x24"); + return -1; + } + if (has_colors() == FALSE) { + LOG(LOG_VERBOSE2, "Terminal without color support detected"); + } + cbreak(); + noecho(); + keypad(stdscr, TRUE); + return 0; +} + +int tui() { + int ret = MAIN_SCREEN; + init_window(); + while (1) { + redraw_screen(); + switch (ret) { + case ERROR_SCREEN: + goto out; + break; + case EXIT_SCREEN: + goto out; + break; + case MAIN_SCREEN: + ret = tui_screen_main(); + break; + case OPTIONS_SCREEN: + ret = tui_screen_options(); + break; + case ENTER_RECOVERY_SCREEN: + ret = tui_screen_enter_recovery(); + break; + case ENTER_DFU_SCREEN: + ret = tui_screen_enter_dfu(); + break; + case JAILBREAKING_SCREEN: + ret = tui_screen_jailbreak(); + break; + default: + assert(0); + goto out; + } + } +out: + destroy_window(); + return ret; +} diff --git a/src/tui_screen_enter_dfu.c b/src/tui_screen_enter_dfu.c new file mode 100644 index 0000000..19cd2a4 --- /dev/null +++ b/src/tui_screen_enter_dfu.c @@ -0,0 +1,20 @@ +#define NCURSES_WIDECHAR 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +tui_screen_t tui_screen_enter_dfu() { return JAILBREAKING_SCREEN; } diff --git a/src/tui_screen_enter_recovery.c b/src/tui_screen_enter_recovery.c new file mode 100644 index 0000000..96ada19 --- /dev/null +++ b/src/tui_screen_enter_recovery.c @@ -0,0 +1,20 @@ +#define NCURSES_WIDECHAR 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +tui_screen_t tui_screen_enter_recovery() { return ENTER_DFU_SCREEN; } diff --git a/src/tui_screen_jailbreak.c b/src/tui_screen_jailbreak.c new file mode 100644 index 0000000..5e40724 --- /dev/null +++ b/src/tui_screen_jailbreak.c @@ -0,0 +1,20 @@ +#define NCURSES_WIDECHAR 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +tui_screen_t tui_screen_jailbreak() { return EXIT_SCREEN; } diff --git a/src/window.c b/src/tui_screen_main.c similarity index 65% rename from src/window.c rename to src/tui_screen_main.c index 14246f5..f0d84d9 100644 --- a/src/window.c +++ b/src/tui_screen_main.c @@ -17,18 +17,7 @@ #include #include -#define MSG_COLOR 1 -#define ICON_COLOR 2 - -int redraw_screen() { - erase(); - resizeterm(24, 80); - box(stdscr, 0, 0); - mvaddstr(0, 80 - strlen("[palera1n version " PALERAIN_VERSION "]") - 2 , "[palera1n version " PALERAIN_VERSION "]"); - return 0; -} - -int display_main_menu() { +tui_screen_t tui_screen_main() { int ret = -1; mvaddstr(1, 2, "Welcome to palera1n!"); move(2,1); @@ -118,73 +107,3 @@ end: refresh(); return ret; } - -int display_enter_dfu_screen() { return JAILBREAKING_SCREEN; } -int display_enter_recovery_screen() { return ENTER_DFU_SCREEN; } -int display_options_screen() { return MAIN_SCREEN; } -int display_jailbreaking_screen() { return EXIT_SCREEN; } - -int init_window() { - setlocale(LC_ALL, NULL); - initscr(); - if (has_colors() == TRUE) { - start_color(); - init_pair(MSG_COLOR, COLOR_YELLOW, COLOR_BLACK); - init_pair(ICON_COLOR, COLOR_WHITE, COLOR_BLACK); - } - int height, width; - getmaxyx(stdscr, height, width); - if (width < 80 || height < 24) { - LOG(LOG_ERROR, "Terminal size must be at least 80x24"); - return -1; - } - if (has_colors() == FALSE) { - LOG(LOG_VERBOSE2, "Terminal without color support detected"); - } - cbreak(); - noecho(); - keypad(stdscr, TRUE); - return 0; -} - -int destroy_window() { - endwin(); - return 0; -} - -int tui() { - int ret = MAIN_SCREEN; - init_window(); - while (1) { - redraw_screen(); - switch (ret) { - case ERROR_SCREEN: - goto out; - break; - case EXIT_SCREEN: - goto out; - break; - case MAIN_SCREEN: - ret = display_main_menu(); - break; - case OPTIONS_SCREEN: - ret = display_options_screen(); - break; - case ENTER_RECOVERY_SCREEN: - ret = display_enter_recovery_screen(); - break; - case ENTER_DFU_SCREEN: - ret = display_enter_dfu_screen(); - break; - case JAILBREAKING_SCREEN: - ret = display_jailbreaking_screen(); - break; - default: - assert(0); - goto out; - } - } -out: - destroy_window(); - return ret; -} diff --git a/src/tui_screen_options.c b/src/tui_screen_options.c new file mode 100644 index 0000000..9baaa7a --- /dev/null +++ b/src/tui_screen_options.c @@ -0,0 +1,20 @@ +#define NCURSES_WIDECHAR 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +tui_screen_t tui_screen_options() { return MAIN_SCREEN; }