From 9be64a1fea7633df95d01b9b9afd596bfda86a69 Mon Sep 17 00:00:00 2001 From: objnull Date: Sun, 8 Sep 2024 21:12:29 -0400 Subject: [PATCH] Created a standardized scene organizer --- project/backend/{app.h => base.h} | 0 project/backend/manager.h | 2 +- project/backend/store.h | 2 +- project/flippypass.c | 2 +- project/ui.c | 690 ------------------------------ project/{ui.h => ui/app.h} | 37 +- project/ui/scenes/create.c | 164 +++++++ project/ui/scenes/create.h | 14 + project/ui/scenes/cred.c | 71 +++ project/ui/scenes/cred.h | 14 + project/ui/scenes/mainmenu.c | 97 +++++ project/ui/scenes/mainmenu.h | 14 + project/ui/scenes/overview.c | 67 +++ project/ui/scenes/overview.h | 14 + project/ui/scenes/send.c | 80 ++++ project/ui/scenes/send.h | 14 + project/ui/scenes/type.c | 49 +++ project/ui/scenes/type.h | 14 + project/ui/scenes/view.c | 93 ++++ project/ui/scenes/view.h | 14 + project/ui/ui.c | 148 +++++++ project/ui/ui.h | 15 + 22 files changed, 911 insertions(+), 704 deletions(-) rename project/backend/{app.h => base.h} (100%) delete mode 100644 project/ui.c rename project/{ui.h => ui/app.h} (53%) create mode 100644 project/ui/scenes/create.c create mode 100644 project/ui/scenes/create.h create mode 100644 project/ui/scenes/cred.c create mode 100644 project/ui/scenes/cred.h create mode 100644 project/ui/scenes/mainmenu.c create mode 100644 project/ui/scenes/mainmenu.h create mode 100644 project/ui/scenes/overview.c create mode 100644 project/ui/scenes/overview.h create mode 100644 project/ui/scenes/send.c create mode 100644 project/ui/scenes/send.h create mode 100644 project/ui/scenes/type.c create mode 100644 project/ui/scenes/type.h create mode 100644 project/ui/scenes/view.c create mode 100644 project/ui/scenes/view.h create mode 100644 project/ui/ui.c create mode 100644 project/ui/ui.h diff --git a/project/backend/app.h b/project/backend/base.h similarity index 100% rename from project/backend/app.h rename to project/backend/base.h diff --git a/project/backend/manager.h b/project/backend/manager.h index d59e7fb..91dde2f 100644 --- a/project/backend/manager.h +++ b/project/backend/manager.h @@ -5,7 +5,7 @@ // Libraries #include -#include "app.h" +#include "base.h" #include "pass.h" #include "store.h" diff --git a/project/backend/store.h b/project/backend/store.h index 4e12a29..e0ccfeb 100644 --- a/project/backend/store.h +++ b/project/backend/store.h @@ -4,7 +4,7 @@ // Libraries #include -#include "app.h" +#include "base.h" // Functions char* store_load(char* type); diff --git a/project/flippypass.c b/project/flippypass.c index 54c2940..aaa08c1 100644 --- a/project/flippypass.c +++ b/project/flippypass.c @@ -1,7 +1,7 @@ // Libraries #include -#include "ui.h" +#include "ui/ui.h" // Entry Point int32_t flippypass_app(void* p) { diff --git a/project/ui.c b/project/ui.c deleted file mode 100644 index 6ee6748..0000000 --- a/project/ui.c +++ /dev/null @@ -1,690 +0,0 @@ -// Header -#include "ui.h" - -// Constants -#define MAX_OPTIONS 4 - -// Structures -/* Scenes */ -typedef enum { - FP_Scene_MainMenu, - FP_Scene_Overview, - FP_Scene_View, - FP_Scene_Cred, - FP_Scene_Send, - FP_Scene_Create, - FP_Scene_Type, - FP_Scene_Archive, - FP_Scene_About, - FP_Scene_Count // Last Index, says how many scenes there are -} FP_Scene; - -/* Types of Views */ -typedef enum { FP_View_VariableItemList, FP_View_Submenu, FP_View_TextBox, FP_View_TextInput, FP_View_Dialog, FP_View_Popup } FP_View; - -/* Scene events */ -typedef enum { - FP_Scene_MainMenu_Event_View, - FP_Scene_MainMenu_Event_Create, - FP_Scene_MainMenu_Event_Archive, - FP_Scene_MainMenu_Event_About -} FP_Scene_MainMenu_Event; -typedef enum { - FP_Scene_View_Event_Username, - FP_Scene_View_Event_Password, - FP_Scene_View_Event_BadUSB -} FP_Scene_View_Event; -typedef enum { - FP_Scene_Create_Event_Name, - FP_Scene_Create_Event_Username, - FP_Scene_Create_Event_Password, - FP_Scene_Create_Event_Done -} FP_Scene_Create_Event; - -// Page functions -void FP_Scene_Callback_Default(void* context, uint32_t index) { - // Setting context - FP_App* app = context; - - // Sending it to the scene manager - scene_manager_handle_custom_event(app->scene_manager, index); -} -void FP_Scene_Callback_Dialog(DialogExResult result, void* context) { - // Setting context - FP_App* app = context; - - // Sending it to the scene manager - scene_manager_handle_custom_event(app->scene_manager, result); -} -void FP_Scene_Callback_Type(void* context) { - // Setting context - FP_App* app = context; - - // DEBUG - FURI_LOG_D(TAG, "Typed: %s", app->temp); - - // Going back a page - scene_manager_previous_scene(app->scene_manager); -} - -void FP_Scene_Enter_MainMenu(void* context) { - // Setting context - FP_App* app = context; - - // Reset menu for next draw - submenu_reset(app->submenu); - - // Adding items to menu - submenu_add_item( - app->submenu, - "View Passwords", - FP_Scene_MainMenu_Event_View, - FP_Scene_Callback_Default, - app - ); - submenu_add_item( - app->submenu, - "Create Password", - FP_Scene_MainMenu_Event_Create, - FP_Scene_Callback_Default, - app - ); - submenu_add_item( - app->submenu, - "Archive Actions", - FP_Scene_MainMenu_Event_Archive, - FP_Scene_Callback_Default, - app - ); - submenu_add_item( - app->submenu, - "About", - FP_Scene_MainMenu_Event_About, - FP_Scene_Callback_Default, - app - ); - - // Switching to current view - view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_Submenu); -} -void FP_Scene_Enter_Overview(void* context) { - // Setting context - FP_App* app = context; - - // Reset menu - submenu_reset(app->submenu); - - // Adding menu items - for (int i = 0; i < app->manager->count; i++) { - submenu_add_item( - app->submenu, - app->manager->names[i], - i, - FP_Scene_Callback_Default, - app - ); - } - - // Sending view to Flipper - view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_Submenu); -} -void FP_Scene_Enter_View(void* context) { - // Setting context - FP_App* app = context; - - // Freeing Dialog - submenu_reset(app->submenu); - - // Adding some content - submenu_add_item( - app->submenu, - "View Username", - FP_Scene_View_Event_Username, - FP_Scene_Callback_Default, - app - ); - submenu_add_item( - app->submenu, - "View Password", - FP_Scene_View_Event_Password, - FP_Scene_Callback_Default, - app - ); - submenu_add_item( - app->submenu, - "BadUSB", - FP_Scene_View_Event_BadUSB, - FP_Scene_Callback_Default, - app - ); - - // Sending view to Flipper - view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_Submenu); -} -void FP_Scene_Enter_Cred(void* context) { - // Set context - FP_App* app = context; - - // Reset view - text_box_reset(app->textbox); - - // Setting text - if (!strcmp(app->temp, "Username")) { - text_box_set_text( - app->textbox, - app->manager->current->user - ); - } else { - text_box_set_text( - app->textbox, - app->manager->current->phrase - ); - } - - // Send view to Flipper - view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_TextBox); -} -void FP_Scene_Enter_Send(void* context) { - // Setting context - FP_App* app = context; - - // Reset view - dialog_ex_reset(app->dialog); - - // Adding dialog content - dialog_ex_set_header( - app->dialog, - "Send via BadUSB", - 0,0, - AlignLeft, - AlignTop - ); - dialog_ex_set_text( - app->dialog, - "This will type out either the Username or Password using BadUSB", - 5,15, - AlignLeft, - AlignTop - ); - dialog_ex_set_left_button_text( - app->dialog, - "Username" - ); - dialog_ex_set_right_button_text( - app->dialog, - "Password" - ); - - // Setting context and callback - dialog_ex_set_result_callback(app->dialog, FP_Scene_Callback_Dialog); - dialog_ex_set_context(app->dialog, app); - - // Send view to Flipper - view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_Dialog); -} -void FP_Scene_Enter_Create(void* context) { - // Setting context - FP_App* app = context; - - // Reset page - submenu_reset(app->submenu); - - // Checking if we typed something before - if (app->temp) { - // Boolean - bool is_field = true; - - // Checking if it's any of the following: - if (!strcmp(app->temp, "Username")) {is_field = false;} - if (!strcmp(app->temp, "Password")) {is_field = false;} - - // We're good! - if(is_field) { - // Did we already start a password? - if (!app->manager->new) { - app->manager->new = malloc(sizeof(Password)); - } - - // Checking what we selected - switch (app->selection) { - case 0: // Name - // Does name already exist? - if (app->manager->new->name) { - free(app->manager->new->name); - } - - // Copying it - app->manager->new->name = malloc(sizeof(char*)); - strcpy(app->manager->new->name, app->temp); - - break; - case 1: // Username - // Does user already exist? - if (app->manager->new->user) { - free(app->manager->new->user); - } - - // Copying it - app->manager->new->user = malloc(sizeof(char*)); - strcpy(app->manager->new->user, app->temp); - break; - case 2: // Password - // Does phrase already exist? - if (app->manager->new->phrase) { - free(app->manager->new->phrase); - } - - // Copying it - app->manager->new->phrase = malloc(sizeof(char*)); - strcpy(app->manager->new->phrase, app->temp); - break; - } - } - - // Freeing temp - free(app->temp); - } - - // Options - submenu_add_item( - app->submenu, - "Set Name", - FP_Scene_Create_Event_Name, - FP_Scene_Callback_Default, - app - ); - submenu_add_item( - app->submenu, - "Set Username", - FP_Scene_Create_Event_Username, - FP_Scene_Callback_Default, - app - ); - submenu_add_item( - app->submenu, - "Set Password", - FP_Scene_Create_Event_Password, - FP_Scene_Callback_Default, - app - ); - submenu_add_item( - app->submenu, - "Finish", - FP_Scene_Create_Event_Done, - FP_Scene_Callback_Default, - app - ); - - // Send view to Flipper - view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_Submenu); -} -void FP_Scene_Enter_Type(void* context) { - // Set Context - FP_App* app = context; - - // Reset view - text_input_reset(app->textinput); - - // Creating temp string - app->temp = malloc(sizeof(char*)); - - // Setting stuff - text_input_set_result_callback( - app->textinput, - FP_Scene_Callback_Type, - app, - app->temp, - 255, - true - ); - - // Send view to Flipper - view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_TextInput); -} - -void FP_Scene_Exit_Default(void* context) { - // Setting context - FP_App* app = context; - - // Reset menu - submenu_reset(app->submenu); -} -void FP_Scene_Exit_Cred(void* context) { - // Setting context - FP_App* app = context; - - // Free the temp string - free(app->temp); - - // Reset menu - dialog_ex_reset(app->dialog); -} - -bool FP_Scene_Event_MainMenu(void* context, SceneManagerEvent event) { - // Setting context - FP_App* app = context; - bool consumed = false; - - // Switching based on event - if (event.type == SceneManagerEventTypeCustom) { - // We consumed it - consumed = true; - - // What to do? - switch (event.event) { - case FP_Scene_MainMenu_Event_About: - scene_manager_next_scene(app->scene_manager, FP_Scene_About); - break; - case FP_Scene_MainMenu_Event_View: - scene_manager_next_scene(app->scene_manager, FP_Scene_Overview); - break; - case FP_Scene_MainMenu_Event_Create: - scene_manager_next_scene(app->scene_manager, FP_Scene_Create); - break; - default: - break; - } - } else { - consumed = false; - } - - // Return result - return consumed; -} -bool FP_Scene_Event_Overview(void* context, SceneManagerEvent event) { - // Setting context - FP_App* app = context; - bool consumed = false; - - // Switching based on event - if (event.type == SceneManagerEventTypeCustom) { - // We consumed it - consumed = true; - - // Loading password - manager_loadpass(app->manager, app->manager->names[event.event]); - - // Is the current password a fake? - if (!app->manager->current) { - FURI_LOG_E(TAG, "Password is NULL!"); - furi_crash(); - } - - // Switch to password view screen - scene_manager_next_scene(app->scene_manager, FP_Scene_View); - } else { - consumed = false; - } - - // Return result - return consumed; -} -bool FP_Scene_Event_View(void* context, SceneManagerEvent event) { - // Setting context - FP_App* app = context; - bool consumed = false; - - // Switching based on event - if (event.type == SceneManagerEventTypeCustom) { - // We consumed it - consumed = true; - - // What did we press? - switch (event.event) { - case FP_Scene_View_Event_Username: - app->temp = malloc(sizeof(char*)); - app->temp = "Username"; - scene_manager_next_scene(app->scene_manager, FP_Scene_Cred); - break; - case FP_Scene_View_Event_Password: - app->temp = malloc(sizeof(char*)); - app->temp = "Password"; - scene_manager_next_scene(app->scene_manager, FP_Scene_Cred); - break; - case FP_Scene_View_Event_BadUSB: - scene_manager_next_scene(app->scene_manager, FP_Scene_Send); - break; - default: - break; - } - } else { - consumed = false; - } - - // Return result - return consumed; -} -bool FP_Scene_Event_Cred(void* context, SceneManagerEvent event) { - // Setting context - FP_App* app = context; - bool consumed = false; - - // Switching based on event - if (event.type == SceneManagerEventTypeCustom) { - // We consumed it - consumed = true; - - // Debug log about what we pressed - FURI_LOG_D(TAG, "Pressed %li", event.event); - - UNUSED(app); - - // What to do? - switch (event.event) { - default: - break; - } - } else { - consumed = false; - } - - // Return result - return consumed; -} -bool FP_Scene_Event_Send(void* context, SceneManagerEvent event) { - // Setting context - FP_App* app = context; - bool consumed = false; - - // Switching based on event - if (event.type == SceneManagerEventTypeCustom) { - // We consumed it - consumed = true; - - UNUSED(app); - - // What to do? - switch (event.event) { - default: - break; - } - } else { - consumed = false; - } - - // Return result - return consumed; -} -bool FP_Scene_Event_Create(void* context, SceneManagerEvent event) { - // Setting context - FP_App* app = context; - bool consumed = false; - - // Switching based on event - if (event.type == SceneManagerEventTypeCustom) { - // We consumed it - consumed = true; - - // What to do? - switch (event.event) { - case FP_Scene_Create_Event_Name: - case FP_Scene_Create_Event_Username: - case FP_Scene_Create_Event_Password: - // Set selection - app->selection = event.event; - - // Switch to keyboard scene - scene_manager_next_scene(app->scene_manager, FP_Scene_Type); - break; - case FP_Scene_Create_Event_Done: - // Save the password - manager_savepass(app->manager->new); - - // Reload manager - manager_free(app->manager); - app->manager = manager_init(); - - // Switching scenes - scene_manager_previous_scene(app->scene_manager); - break; - default: - break; - } - } else { - consumed = false; - } - - // Return result - return consumed; -} -bool FP_Scene_Event_Type(void* context, SceneManagerEvent event) { - // Return result - UNUSED(context); - return event.type == SceneManagerEventTypeCustom; -} - -/* Handlers */ -void (*const FP_Scene_Enter_Handlers[])(void*) = { - FP_Scene_Enter_MainMenu, - FP_Scene_Enter_Overview, - FP_Scene_Enter_View, - FP_Scene_Enter_Cred, - FP_Scene_Enter_Send, - FP_Scene_Enter_Create, - FP_Scene_Enter_Type -}; -bool (*const FP_Scene_Event_Handlers[])(void*, SceneManagerEvent) = { - FP_Scene_Event_MainMenu, - FP_Scene_Event_Overview, - FP_Scene_Event_View, - FP_Scene_Event_Cred, - FP_Scene_Event_Send, - FP_Scene_Event_Create, - FP_Scene_Event_Type -}; -void (*const FP_Scene_Exit_Handlers[])(void*) = { - FP_Scene_Exit_Default, - FP_Scene_Exit_Default, - FP_Scene_Exit_Default, - FP_Scene_Exit_Cred, - FP_Scene_Exit_Default, - FP_Scene_Exit_Default, - FP_Scene_Exit_Default -}; - -/* Event Handlers */ -const SceneManagerHandlers FP_SceneEventHandlers = { - .on_enter_handlers = FP_Scene_Enter_Handlers, - .on_event_handlers = FP_Scene_Event_Handlers, - .on_exit_handlers = FP_Scene_Exit_Handlers, - .scene_num = FP_Scene_Count}; - -// Functions - Private -bool FP_SceneManager_Callback_CustomEvent(void* context, uint32_t custom_event) { - // Is our context okay? - furi_assert(context); - - // Setting context - FP_App* app = context; - - // Return event - return scene_manager_handle_custom_event(app->scene_manager, custom_event); -} -bool FP_SceneManager_Callback_Navigation(void* context) { - // Is our context okay? - furi_assert(context); - - // Setting context - FP_App* app = context; - - // Return navigation - return scene_manager_handle_back_event(app->scene_manager); -} - -// Constructors -FP_App* fp_app_init() { - // Allocate memory for new app variable - FP_App* result = malloc(sizeof(FP_App)); - - // Initilize the manager - result->manager = manager_init(); - - // Init Scene Manager - result->scene_manager = scene_manager_alloc(&FP_SceneEventHandlers, result); - - // Init View Dispatcher - result->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(result->view_dispatcher); - - // Allocating all types of views - result->varitemlist = variable_item_list_alloc(); - result->textinput = text_input_alloc(); - result->dialog = dialog_ex_alloc(); - result->submenu = submenu_alloc(); - result->textbox = text_box_alloc(); - result->popup = popup_alloc(); - - // Event handling - view_dispatcher_set_event_callback_context(result->view_dispatcher, result); - view_dispatcher_set_custom_event_callback(result->view_dispatcher, FP_SceneManager_Callback_CustomEvent); - view_dispatcher_set_navigation_event_callback(result->view_dispatcher, FP_SceneManager_Callback_Navigation); - - // Adding views to dispatcher - view_dispatcher_add_view(result->view_dispatcher, FP_View_VariableItemList, variable_item_list_get_view(result->varitemlist)); - view_dispatcher_add_view(result->view_dispatcher, FP_View_TextInput, text_input_get_view(result->textinput)); - view_dispatcher_add_view(result->view_dispatcher, FP_View_TextBox, text_box_get_view(result->textbox)); - view_dispatcher_add_view(result->view_dispatcher, FP_View_Dialog, dialog_ex_get_view(result->dialog)); - view_dispatcher_add_view(result->view_dispatcher, FP_View_Submenu, submenu_get_view(result->submenu)); - view_dispatcher_add_view(result->view_dispatcher, FP_View_Popup, popup_get_view(result->popup)); - - // Return app - return result; -} - -// Functions -void fp_app_run(FP_App* app) { - // Creating GUI - Gui* gui = furi_record_open(RECORD_GUI); - - // Attaching view dispatcher to GUI - view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen); - - // Starting scene - scene_manager_next_scene(app->scene_manager, FP_Scene_MainMenu); - - // Start view dispatcher - view_dispatcher_run(app->view_dispatcher); - - /* Post Application */ - furi_record_close(RECORD_GUI); -} -void fp_app_free(FP_App* app) { - // Freeing stuff from memory - manager_free(app->manager); - scene_manager_free(app->scene_manager); - view_dispatcher_remove_view(app->view_dispatcher, FP_View_VariableItemList); - view_dispatcher_remove_view(app->view_dispatcher, FP_View_TextInput); - view_dispatcher_remove_view(app->view_dispatcher, FP_View_Submenu); - view_dispatcher_remove_view(app->view_dispatcher, FP_View_TextBox); - view_dispatcher_remove_view(app->view_dispatcher, FP_View_Dialog); - view_dispatcher_remove_view(app->view_dispatcher, FP_View_Popup); - view_dispatcher_free(app->view_dispatcher); - variable_item_list_free(app->varitemlist); - text_input_free(app->textinput); - dialog_ex_free(app->dialog); - text_box_free(app->textbox); - submenu_free(app->submenu); - popup_free(app->popup); - free(app->temp); - free(app); -} \ No newline at end of file diff --git a/project/ui.h b/project/ui/app.h similarity index 53% rename from project/ui.h rename to project/ui/app.h index 26e0b1e..e308f5e 100644 --- a/project/ui.h +++ b/project/ui/app.h @@ -1,15 +1,18 @@ // Define once -#ifndef H_UI -#define H_UI +#ifndef H_FP_APP +#define H_FP_APP // Libraries +/* System */ #include +/* GUI */ #include #include #include #include +/* GUI Modules */ #include #include #include @@ -17,10 +20,29 @@ #include #include -#include "backend/app.h" -#include "backend/manager.h" +/* Our App */ +#include "../backend/base.h" +#include "../backend/manager.h" // Structures +/* View Types */ +typedef enum { FP_View_VariableItemList, FP_View_Submenu, FP_View_TextBox, FP_View_TextInput, FP_View_Dialog, FP_View_Popup } FP_View; + +/* Scenes */ +typedef enum { + FP_Scene_MainMenu, + FP_Scene_Overview, + FP_Scene_View, + FP_Scene_Cred, + FP_Scene_Send, + FP_Scene_Create, + FP_Scene_Type, + FP_Scene_Archive, + FP_Scene_About, + FP_Scene_Count // Last Index, says how many scenes there are +} FP_Scene; + +/* Main App*/ typedef struct { SceneManager* scene_manager; ViewDispatcher* view_dispatcher; @@ -36,11 +58,4 @@ typedef struct { char* temp; } FP_App; -// Constructors -FP_App* fp_app_init(); - -// Functions -void fp_app_run(FP_App* app); -void fp_app_free(FP_App* app); - #endif \ No newline at end of file diff --git a/project/ui/scenes/create.c b/project/ui/scenes/create.c new file mode 100644 index 0000000..2f4be2c --- /dev/null +++ b/project/ui/scenes/create.c @@ -0,0 +1,164 @@ +// Header +#include "create.h" + +// Structures +typedef enum { + FP_Scene_Create_Event_Name, + FP_Scene_Create_Event_Username, + FP_Scene_Create_Event_Password, + FP_Scene_Create_Event_Done +} FP_Scene_Create_Event; + +// Functions +void FP_Scene_Callback_Create(void* context, uint32_t index) { + // Setting context + FP_App* app = context; + + // Sending it to the scene manager + scene_manager_handle_custom_event(app->scene_manager, index); +} +void FP_Scene_Enter_Create(void* context) { + // Setting context + FP_App* app = context; + + // Reset page + submenu_reset(app->submenu); + + // Checking if we typed something before + if (app->temp) { + // Boolean + bool is_field = true; + + // Checking if it's any of the following: + if (!strcmp(app->temp, "Username")) {is_field = false;} + if (!strcmp(app->temp, "Password")) {is_field = false;} + + // We're good! + if(is_field) { + // Did we already start a password? + if (!app->manager->new) { + app->manager->new = malloc(sizeof(Password)); + } + + // Checking what we selected + switch (app->selection) { + case 0: // Name + // Does name already exist? + if (app->manager->new->name) { + free(app->manager->new->name); + } + + // Copying it + app->manager->new->name = malloc(sizeof(char*)); + strcpy(app->manager->new->name, app->temp); + + break; + case 1: // Username + // Does user already exist? + if (app->manager->new->user) { + free(app->manager->new->user); + } + + // Copying it + app->manager->new->user = malloc(sizeof(char*)); + strcpy(app->manager->new->user, app->temp); + break; + case 2: // Password + // Does phrase already exist? + if (app->manager->new->phrase) { + free(app->manager->new->phrase); + } + + // Copying it + app->manager->new->phrase = malloc(sizeof(char*)); + strcpy(app->manager->new->phrase, app->temp); + break; + } + } + + // Freeing temp + free(app->temp); + } + + // Options + submenu_add_item( + app->submenu, + "Set Name", + FP_Scene_Create_Event_Name, + FP_Scene_Callback_Create, + app + ); + submenu_add_item( + app->submenu, + "Set Username", + FP_Scene_Create_Event_Username, + FP_Scene_Callback_Create, + app + ); + submenu_add_item( + app->submenu, + "Set Password", + FP_Scene_Create_Event_Password, + FP_Scene_Callback_Create, + app + ); + submenu_add_item( + app->submenu, + "Finish", + FP_Scene_Create_Event_Done, + FP_Scene_Callback_Create, + app + ); + + // Send view to Flipper + view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_Submenu); +} +void FP_Scene_Exit_Create(void* context) { + // Setting context + FP_App* app = context; + + // Reset menu + submenu_reset(app->submenu); +} +bool FP_Scene_Event_Create(void* context, SceneManagerEvent event) { + // Setting context + FP_App* app = context; + bool consumed = false; + + // Switching based on event + if (event.type == SceneManagerEventTypeCustom) { + // We consumed it + consumed = true; + + // What to do? + switch (event.event) { + case FP_Scene_Create_Event_Name: + case FP_Scene_Create_Event_Username: + case FP_Scene_Create_Event_Password: + // Set selection + app->selection = event.event; + + // Switch to keyboard scene + scene_manager_next_scene(app->scene_manager, FP_Scene_Type); + break; + case FP_Scene_Create_Event_Done: + // Save the password + manager_savepass(app->manager->new); + + // Reload manager + manager_free(app->manager); + app->manager = manager_init(); + + // Switching scenes + scene_manager_previous_scene(app->scene_manager); + break; + default: + break; + } + } else { + consumed = false; + } + + // Return result + return consumed; +} \ No newline at end of file diff --git a/project/ui/scenes/create.h b/project/ui/scenes/create.h new file mode 100644 index 0000000..6cb64f9 --- /dev/null +++ b/project/ui/scenes/create.h @@ -0,0 +1,14 @@ +// Define once +#ifndef H_CREATE +#define H_CREATE + +// Libraries +#include "../app.h" + +// Functions +void FP_Scene_Callback_Create(void* context, uint32_t index); +void FP_Scene_Enter_Create(void* context); +void FP_Scene_Exit_Create(void* context); +bool FP_Scene_Event_Create(void* context, SceneManagerEvent event); + +#endif \ No newline at end of file diff --git a/project/ui/scenes/cred.c b/project/ui/scenes/cred.c new file mode 100644 index 0000000..c65f4da --- /dev/null +++ b/project/ui/scenes/cred.c @@ -0,0 +1,71 @@ +// Header +#include "cred.h" + +// Functions +void FP_Scene_Callback_Cred(void* context, uint32_t index) { + // Setting context + FP_App* app = context; + + // Sending it to the scene manager + scene_manager_handle_custom_event(app->scene_manager, index); +} +void FP_Scene_Enter_Cred(void* context) { + // Set context + FP_App* app = context; + + // Reset view + text_box_reset(app->textbox); + + // Setting text + if (!strcmp(app->temp, "Username")) { + text_box_set_text( + app->textbox, + app->manager->current->user + ); + } else { + text_box_set_text( + app->textbox, + app->manager->current->phrase + ); + } + + // Send view to Flipper + view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_TextBox); +} +void FP_Scene_Exit_Cred(void* context) { + // Setting context + FP_App* app = context; + + // Free the temp string + free(app->temp); + + // Reset menu + dialog_ex_reset(app->dialog); +} +bool FP_Scene_Event_Cred(void* context, SceneManagerEvent event) { + // Setting context + FP_App* app = context; + bool consumed = false; + + // Switching based on event + if (event.type == SceneManagerEventTypeCustom) { + // We consumed it + consumed = true; + + // Debug log about what we pressed + FURI_LOG_D(TAG, "Pressed %li", event.event); + + UNUSED(app); + + // What to do? + switch (event.event) { + default: + break; + } + } else { + consumed = false; + } + + // Return result + return consumed; +} \ No newline at end of file diff --git a/project/ui/scenes/cred.h b/project/ui/scenes/cred.h new file mode 100644 index 0000000..21729f5 --- /dev/null +++ b/project/ui/scenes/cred.h @@ -0,0 +1,14 @@ +// Define once +#ifndef H_CRED +#define H_CRED + +// Libraries +#include "../app.h" + +// Functions +void FP_Scene_Callback_Cred(void* context, uint32_t index); +void FP_Scene_Enter_Cred(void* context); +void FP_Scene_Exit_Cred(void* context); +bool FP_Scene_Event_Cred(void* context, SceneManagerEvent event); + +#endif \ No newline at end of file diff --git a/project/ui/scenes/mainmenu.c b/project/ui/scenes/mainmenu.c new file mode 100644 index 0000000..22f536f --- /dev/null +++ b/project/ui/scenes/mainmenu.c @@ -0,0 +1,97 @@ +// Header file +#include "mainmenu.h" + +// Structures +typedef enum { + FP_Scene_MainMenu_Event_View, + FP_Scene_MainMenu_Event_Create, + FP_Scene_MainMenu_Event_Archive, + FP_Scene_MainMenu_Event_About +} FP_Scene_MainMenu_Event; + +// Functions +void FP_Scene_Callback_MainMenu(void* context, uint32_t index) { + // Setting context + FP_App* app = context; + + // Sending it to the scene manager + scene_manager_handle_custom_event(app->scene_manager, index); +} +void FP_Scene_Enter_MainMenu(void* context) { + // Setting context + FP_App* app = context; + + // Reset menu for next draw + submenu_reset(app->submenu); + + // Adding items to menu + submenu_add_item( + app->submenu, + "View Passwords", + FP_Scene_MainMenu_Event_View, + FP_Scene_Callback_MainMenu, + app + ); + submenu_add_item( + app->submenu, + "Create Password", + FP_Scene_MainMenu_Event_Create, + FP_Scene_Callback_MainMenu, + app + ); + submenu_add_item( + app->submenu, + "Archive Actions", + FP_Scene_MainMenu_Event_Archive, + FP_Scene_Callback_MainMenu, + app + ); + submenu_add_item( + app->submenu, + "About", + FP_Scene_MainMenu_Event_About, + FP_Scene_Callback_MainMenu, + app + ); + + // Switching to current view + view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_Submenu); +} +void FP_Scene_Exit_MainMenu(void* context) { + // Setting context + FP_App* app = context; + + // Reset menu + submenu_reset(app->submenu); +} +bool FP_Scene_Event_MainMenu(void* context, SceneManagerEvent event) { + // Setting context + FP_App* app = context; + bool consumed = false; + + // Switching based on event + if (event.type == SceneManagerEventTypeCustom) { + // We consumed it + consumed = true; + + // What to do? + switch (event.event) { + case FP_Scene_MainMenu_Event_About: + scene_manager_next_scene(app->scene_manager, FP_Scene_About); + break; + case FP_Scene_MainMenu_Event_View: + scene_manager_next_scene(app->scene_manager, FP_Scene_Overview); + break; + case FP_Scene_MainMenu_Event_Create: + scene_manager_next_scene(app->scene_manager, FP_Scene_Create); + break; + default: + break; + } + } else { + consumed = false; + } + + // Return result + return consumed; +} \ No newline at end of file diff --git a/project/ui/scenes/mainmenu.h b/project/ui/scenes/mainmenu.h new file mode 100644 index 0000000..03c6118 --- /dev/null +++ b/project/ui/scenes/mainmenu.h @@ -0,0 +1,14 @@ +// Define once +#ifndef H_MAINMENU +#define H_MAINMENU + +// Libraries +#include "../app.h" + +// Functions +void FP_Scene_Callback_MainMenu(void* context, uint32_t index); +void FP_Scene_Enter_MainMenu(void* context); +void FP_Scene_Exit_MainMenu(void* context); +bool FP_Scene_Event_MainMenu(void* context, SceneManagerEvent event); + +#endif \ No newline at end of file diff --git a/project/ui/scenes/overview.c b/project/ui/scenes/overview.c new file mode 100644 index 0000000..688ffc3 --- /dev/null +++ b/project/ui/scenes/overview.c @@ -0,0 +1,67 @@ +// Header +#include "overview.h" + +// Functions +void FP_Scene_Callback_Overview(void* context, uint32_t index) { + // Setting context + FP_App* app = context; + + // Sending it to the scene manager + scene_manager_handle_custom_event(app->scene_manager, index); +} +void FP_Scene_Enter_Overview(void* context) { + // Setting context + FP_App* app = context; + + // Reset menu + submenu_reset(app->submenu); + + // Adding menu items + for (int i = 0; i < app->manager->count; i++) { + submenu_add_item( + app->submenu, + app->manager->names[i], + i, + FP_Scene_Callback_Overview, + app + ); + } + + // Sending view to Flipper + view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_Submenu); +} +void FP_Scene_Exit_Overview(void* context) { + // Setting context + FP_App* app = context; + + // Reset menu + submenu_reset(app->submenu); +} +bool FP_Scene_Event_Overview(void* context, SceneManagerEvent event) { + // Setting context + FP_App* app = context; + bool consumed = false; + + // Switching based on event + if (event.type == SceneManagerEventTypeCustom) { + // We consumed it + consumed = true; + + // Loading password + manager_loadpass(app->manager, app->manager->names[event.event]); + + // Is the current password a fake? + if (!app->manager->current) { + FURI_LOG_E(TAG, "Password is NULL!"); + furi_crash(); + } + + // Switch to password view screen + scene_manager_next_scene(app->scene_manager, FP_Scene_View); + } else { + consumed = false; + } + + // Return result + return consumed; +} \ No newline at end of file diff --git a/project/ui/scenes/overview.h b/project/ui/scenes/overview.h new file mode 100644 index 0000000..e7f4026 --- /dev/null +++ b/project/ui/scenes/overview.h @@ -0,0 +1,14 @@ +// Define once +#ifndef H_OVERVIEW +#define H_OVERVIEW + +// Libraries +#include "../app.h" + +// Functions +void FP_Scene_Callback_Overview(void* context, uint32_t index); +void FP_Scene_Enter_Overview(void* context); +void FP_Scene_Exit_Overview(void* context); +bool FP_Scene_Event_Overview(void* context, SceneManagerEvent event); + +#endif \ No newline at end of file diff --git a/project/ui/scenes/send.c b/project/ui/scenes/send.c new file mode 100644 index 0000000..3602962 --- /dev/null +++ b/project/ui/scenes/send.c @@ -0,0 +1,80 @@ +// Header +#include "send.h" + +// Functions +void FP_Scene_Callback_Send(DialogExResult result, void* context) { + // Setting context + FP_App* app = context; + + // Sending it to the scene manager + scene_manager_handle_custom_event(app->scene_manager, result); +} +void FP_Scene_Enter_Send(void* context) { + // Setting context + FP_App* app = context; + + // Reset view + dialog_ex_reset(app->dialog); + + // Adding dialog content + dialog_ex_set_header( + app->dialog, + "Send via BadUSB", + 0,0, + AlignLeft, + AlignTop + ); + dialog_ex_set_text( + app->dialog, + "This will type out either the Username or Password using BadUSB", + 5,15, + AlignLeft, + AlignTop + ); + dialog_ex_set_left_button_text( + app->dialog, + "Username" + ); + dialog_ex_set_right_button_text( + app->dialog, + "Password" + ); + + // Setting context and callback + dialog_ex_set_result_callback(app->dialog, FP_Scene_Callback_Send); + dialog_ex_set_context(app->dialog, app); + + // Send view to Flipper + view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_Dialog); +} +void FP_Scene_Exit_Send(void* context) { + // Setting context + FP_App* app = context; + + // Reset menu + submenu_reset(app->submenu); +} +bool FP_Scene_Event_Send(void* context, SceneManagerEvent event) { + // Setting context + FP_App* app = context; + bool consumed = false; + + // Switching based on event + if (event.type == SceneManagerEventTypeCustom) { + // We consumed it + consumed = true; + + UNUSED(app); + + // What to do? + switch (event.event) { + default: + break; + } + } else { + consumed = false; + } + + // Return result + return consumed; +} \ No newline at end of file diff --git a/project/ui/scenes/send.h b/project/ui/scenes/send.h new file mode 100644 index 0000000..2213c13 --- /dev/null +++ b/project/ui/scenes/send.h @@ -0,0 +1,14 @@ +// Define once +#ifndef H_SEND +#define H_SEND + +// Libraries +#include "../app.h" + +// Functions +void FP_Scene_Callback_Send(DialogExResult result, void* context); +void FP_Scene_Enter_Send(void* context); +void FP_Scene_Exit_Send(void* context); +bool FP_Scene_Event_Send(void* context, SceneManagerEvent event); + +#endif \ No newline at end of file diff --git a/project/ui/scenes/type.c b/project/ui/scenes/type.c new file mode 100644 index 0000000..8e6757c --- /dev/null +++ b/project/ui/scenes/type.c @@ -0,0 +1,49 @@ +// Header +#include "type.h" + +// Functions +void FP_Scene_Callback_Type(void* context) { + // Setting context + FP_App* app = context; + + // DEBUG + FURI_LOG_D(TAG, "Typed: %s", app->temp); + + // Going back a page + scene_manager_previous_scene(app->scene_manager); +} +void FP_Scene_Enter_Type(void* context) { + // Set Context + FP_App* app = context; + + // Reset view + text_input_reset(app->textinput); + + // Creating temp string + app->temp = malloc(sizeof(char*)); + + // Setting stuff + text_input_set_result_callback( + app->textinput, + FP_Scene_Callback_Type, + app, + app->temp, + 255, + true + ); + + // Send view to Flipper + view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_TextInput); +} +void FP_Scene_Exit_Type(void* context) { + // Setting context + FP_App* app = context; + + // Reset menu + submenu_reset(app->submenu); +} +bool FP_Scene_Event_Type(void* context, SceneManagerEvent event) { + // Return result + UNUSED(context); + return event.type == SceneManagerEventTypeCustom; +} \ No newline at end of file diff --git a/project/ui/scenes/type.h b/project/ui/scenes/type.h new file mode 100644 index 0000000..b4e56d7 --- /dev/null +++ b/project/ui/scenes/type.h @@ -0,0 +1,14 @@ +// Define once +#ifndef H_TYPE +#define H_TYPE + +// Libraries +#include "../app.h" + +// Functions +void FP_Scene_Callback_Type(void* context); +void FP_Scene_Enter_Type(void* context); +void FP_Scene_Exit_Type(void* context); +bool FP_Scene_Event_Type(void* context, SceneManagerEvent event); + +#endif \ No newline at end of file diff --git a/project/ui/scenes/view.c b/project/ui/scenes/view.c new file mode 100644 index 0000000..42c97d1 --- /dev/null +++ b/project/ui/scenes/view.c @@ -0,0 +1,93 @@ +// Header +#include "view.h" + +// Structures +typedef enum { + FP_Scene_View_Event_Username, + FP_Scene_View_Event_Password, + FP_Scene_View_Event_BadUSB +} FP_Scene_View_Event; + +// Functions +void FP_Scene_Callback_View(void* context, uint32_t index) { + // Setting context + FP_App* app = context; + + // Sending it to the scene manager + scene_manager_handle_custom_event(app->scene_manager, index); +} +void FP_Scene_Enter_View(void* context) { + // Setting context + FP_App* app = context; + + // Freeing Dialog + submenu_reset(app->submenu); + + // Adding some content + submenu_add_item( + app->submenu, + "View Username", + FP_Scene_View_Event_Username, + FP_Scene_Callback_View, + app + ); + submenu_add_item( + app->submenu, + "View Password", + FP_Scene_View_Event_Password, + FP_Scene_Callback_View, + app + ); + submenu_add_item( + app->submenu, + "BadUSB", + FP_Scene_View_Event_BadUSB, + FP_Scene_Callback_View, + app + ); + + // Sending view to Flipper + view_dispatcher_switch_to_view(app->view_dispatcher, FP_View_Submenu); +} +void FP_Scene_Exit_View(void* context) { + // Setting context + FP_App* app = context; + + // Reset menu + submenu_reset(app->submenu); +} +bool FP_Scene_Event_View(void* context, SceneManagerEvent event) { + // Setting context + FP_App* app = context; + bool consumed = false; + + // Switching based on event + if (event.type == SceneManagerEventTypeCustom) { + // We consumed it + consumed = true; + + // What did we press? + switch (event.event) { + case FP_Scene_View_Event_Username: + app->temp = malloc(sizeof(char*)); + app->temp = "Username"; + scene_manager_next_scene(app->scene_manager, FP_Scene_Cred); + break; + case FP_Scene_View_Event_Password: + app->temp = malloc(sizeof(char*)); + app->temp = "Password"; + scene_manager_next_scene(app->scene_manager, FP_Scene_Cred); + break; + case FP_Scene_View_Event_BadUSB: + scene_manager_next_scene(app->scene_manager, FP_Scene_Send); + break; + default: + break; + } + } else { + consumed = false; + } + + // Return result + return consumed; +} \ No newline at end of file diff --git a/project/ui/scenes/view.h b/project/ui/scenes/view.h new file mode 100644 index 0000000..e5889c6 --- /dev/null +++ b/project/ui/scenes/view.h @@ -0,0 +1,14 @@ +// Define once +#ifndef H_VIEW +#define H_VIEW + +// Libraries +#include "../app.h" + +// Functions +void FP_Scene_Callback_View(void* context, uint32_t index); +void FP_Scene_Enter_View(void* context); +void FP_Scene_Exit_View(void* context); +bool FP_Scene_Event_View(void* context, SceneManagerEvent event); + +#endif \ No newline at end of file diff --git a/project/ui/ui.c b/project/ui/ui.c new file mode 100644 index 0000000..0bc9218 --- /dev/null +++ b/project/ui/ui.c @@ -0,0 +1,148 @@ +// Header +#include "app.h" + +// Scenes +#include "scenes/mainmenu.h" +#include "scenes/overview.h" +#include "scenes/view.h" +#include "scenes/cred.h" +#include "scenes/send.h" +#include "scenes/create.h" +#include "scenes/type.h" + +// Handlers +void (*const FP_Scene_Enter_Handlers[])(void*) = { + FP_Scene_Enter_MainMenu, + FP_Scene_Enter_Overview, + FP_Scene_Enter_View, + FP_Scene_Enter_Cred, + FP_Scene_Enter_Send, + FP_Scene_Enter_Create, + FP_Scene_Enter_Type +}; +bool (*const FP_Scene_Event_Handlers[])(void*, SceneManagerEvent) = { + FP_Scene_Event_MainMenu, + FP_Scene_Event_Overview, + FP_Scene_Event_View, + FP_Scene_Event_Cred, + FP_Scene_Event_Send, + FP_Scene_Event_Create, + FP_Scene_Event_Type +}; +void (*const FP_Scene_Exit_Handlers[])(void*) = { + FP_Scene_Exit_MainMenu, + FP_Scene_Exit_Overview, + FP_Scene_Exit_View, + FP_Scene_Exit_Cred, + FP_Scene_Exit_Send, + FP_Scene_Exit_Create, + FP_Scene_Exit_Type +}; + +// Main Handler +const SceneManagerHandlers FP_SceneEventHandlers = { + .on_enter_handlers = FP_Scene_Enter_Handlers, + .on_event_handlers = FP_Scene_Event_Handlers, + .on_exit_handlers = FP_Scene_Exit_Handlers, + .scene_num = FP_Scene_Count}; + + +// Functions - Private +bool FP_SceneManager_Callback_CustomEvent(void* context, uint32_t custom_event) { + // Is our context okay? + furi_assert(context); + + // Setting context + FP_App* app = context; + + // Return event + return scene_manager_handle_custom_event(app->scene_manager, custom_event); +} +bool FP_SceneManager_Callback_Navigation(void* context) { + // Is our context okay? + furi_assert(context); + + // Setting context + FP_App* app = context; + + // Return navigation + return scene_manager_handle_back_event(app->scene_manager); +} + +// Constructors +FP_App* fp_app_init() { + // Allocate memory for new app variable + FP_App* result = malloc(sizeof(FP_App)); + + // Initilize the manager + result->manager = manager_init(); + + // Init Scene Manager + result->scene_manager = scene_manager_alloc(&FP_SceneEventHandlers, result); + + // Init View Dispatcher + result->view_dispatcher = view_dispatcher_alloc(); + view_dispatcher_enable_queue(result->view_dispatcher); + + // Allocating all types of views + result->varitemlist = variable_item_list_alloc(); + result->textinput = text_input_alloc(); + result->dialog = dialog_ex_alloc(); + result->submenu = submenu_alloc(); + result->textbox = text_box_alloc(); + result->popup = popup_alloc(); + + // Event handling + view_dispatcher_set_event_callback_context(result->view_dispatcher, result); + view_dispatcher_set_custom_event_callback(result->view_dispatcher, FP_SceneManager_Callback_CustomEvent); + view_dispatcher_set_navigation_event_callback(result->view_dispatcher, FP_SceneManager_Callback_Navigation); + + // Adding views to dispatcher + view_dispatcher_add_view(result->view_dispatcher, FP_View_VariableItemList, variable_item_list_get_view(result->varitemlist)); + view_dispatcher_add_view(result->view_dispatcher, FP_View_TextInput, text_input_get_view(result->textinput)); + view_dispatcher_add_view(result->view_dispatcher, FP_View_TextBox, text_box_get_view(result->textbox)); + view_dispatcher_add_view(result->view_dispatcher, FP_View_Dialog, dialog_ex_get_view(result->dialog)); + view_dispatcher_add_view(result->view_dispatcher, FP_View_Submenu, submenu_get_view(result->submenu)); + view_dispatcher_add_view(result->view_dispatcher, FP_View_Popup, popup_get_view(result->popup)); + + // Return app + return result; +} + +// Functions +void fp_app_run(FP_App* app) { + // Creating GUI + Gui* gui = furi_record_open(RECORD_GUI); + + // Attaching view dispatcher to GUI + view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen); + + // Starting scene + scene_manager_next_scene(app->scene_manager, FP_Scene_MainMenu); + + // Start view dispatcher + view_dispatcher_run(app->view_dispatcher); + + /* Post Application */ + furi_record_close(RECORD_GUI); +} +void fp_app_free(FP_App* app) { + // Freeing stuff from memory + manager_free(app->manager); + scene_manager_free(app->scene_manager); + view_dispatcher_remove_view(app->view_dispatcher, FP_View_VariableItemList); + view_dispatcher_remove_view(app->view_dispatcher, FP_View_TextInput); + view_dispatcher_remove_view(app->view_dispatcher, FP_View_Submenu); + view_dispatcher_remove_view(app->view_dispatcher, FP_View_TextBox); + view_dispatcher_remove_view(app->view_dispatcher, FP_View_Dialog); + view_dispatcher_remove_view(app->view_dispatcher, FP_View_Popup); + view_dispatcher_free(app->view_dispatcher); + variable_item_list_free(app->varitemlist); + text_input_free(app->textinput); + dialog_ex_free(app->dialog); + text_box_free(app->textbox); + submenu_free(app->submenu); + popup_free(app->popup); + free(app->temp); + free(app); +} \ No newline at end of file diff --git a/project/ui/ui.h b/project/ui/ui.h new file mode 100644 index 0000000..610bb4f --- /dev/null +++ b/project/ui/ui.h @@ -0,0 +1,15 @@ +// Define Once +#ifndef H_UI +#define H_UI + +// Libraries +#include "app.h" + +// Constructors +FP_App* fp_app_init(); + +// Functions +void fp_app_run(FP_App* app); +void fp_app_free(FP_App* app); + +#endif \ No newline at end of file