mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-24 03:29:57 -07:00
Main Menu: Unload when opening apps (#161)
* Detached close event * Consistency * Unload main menu when opening apps * Refactor LoaderMenu with ViewHolder * Show loading icon * Remember selected items after reloading menu * Consistency * Similar code structure to ofw * Load/unload custom app list with main menu * Fix momentum app for new mainmenu logic
This commit is contained in:
@@ -18,8 +18,6 @@ bool momentum_app_apply(MomentumApp* app) {
|
||||
Stream* stream = file_stream_alloc(storage);
|
||||
if(file_stream_open(stream, MAINMENU_APPS_PATH, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS)) {
|
||||
stream_write_format(stream, "MenuAppList Version %u\n", 1);
|
||||
CharList_it_t it;
|
||||
CharList_it(it, app->mainmenu_app_exes);
|
||||
for(size_t i = 0; i < CharList_size(app->mainmenu_app_exes); i++) {
|
||||
stream_write_format(stream, "%s\n", *CharList_get(app->mainmenu_app_exes, i));
|
||||
}
|
||||
@@ -161,6 +159,24 @@ static bool momentum_app_back_event_callback(void* context) {
|
||||
return scene_manager_handle_back_event(app->scene_manager);
|
||||
}
|
||||
|
||||
static void momentum_app_push_mainmenu_app(MomentumApp* app, FuriString* label, FuriString* exe) {
|
||||
CharList_push_back(app->mainmenu_app_exes, strdup(furi_string_get_cstr(exe)));
|
||||
// Display logic mimics applications/services/gui/modules/menu.c
|
||||
if(furi_string_equal(label, "Momentum")) {
|
||||
furi_string_set(label, "MNTM");
|
||||
} else if(furi_string_equal(label, "125 kHz RFID")) {
|
||||
furi_string_set(label, "RFID");
|
||||
} else if(furi_string_equal(label, "Sub-GHz")) {
|
||||
furi_string_set(label, "SubGHz");
|
||||
} else if(furi_string_start_with_str(label, "[")) {
|
||||
size_t trim = furi_string_search_str(label, "] ", 1);
|
||||
if(trim != FURI_STRING_FAILURE) {
|
||||
furi_string_right(label, trim + 2);
|
||||
}
|
||||
}
|
||||
CharList_push_back(app->mainmenu_app_labels, strdup(furi_string_get_cstr(label)));
|
||||
}
|
||||
|
||||
MomentumApp* momentum_app_alloc() {
|
||||
MomentumApp* app = malloc(sizeof(MomentumApp));
|
||||
app->gui = furi_record_open(RECORD_GUI);
|
||||
@@ -242,6 +258,7 @@ MomentumApp* momentum_app_alloc() {
|
||||
|
||||
CharList_init(app->mainmenu_app_labels);
|
||||
CharList_init(app->mainmenu_app_exes);
|
||||
// Loading logic mimics applications/services/loader/loader_menu.c
|
||||
Stream* stream = file_stream_alloc(storage);
|
||||
FuriString* line = furi_string_alloc();
|
||||
FuriString* label = furi_string_alloc();
|
||||
@@ -252,17 +269,15 @@ MomentumApp* momentum_app_alloc() {
|
||||
sscanf(furi_string_get_cstr(line), "MenuAppList Version %lu", &version) == 1 &&
|
||||
version <= 1) {
|
||||
while(stream_read_line(stream, line)) {
|
||||
// Loading logic mimics applications/services/loader/loader.c
|
||||
furi_string_replace_all(line, "\r", "");
|
||||
furi_string_replace_all(line, "\n", "");
|
||||
furi_string_trim(line);
|
||||
if(version == 0) {
|
||||
if(!furi_string_cmp(line, "RFID")) {
|
||||
if(furi_string_equal(line, "RFID")) {
|
||||
furi_string_set(line, "125 kHz RFID");
|
||||
} else if(!furi_string_cmp(line, "SubGHz")) {
|
||||
} else if(furi_string_equal(line, "SubGHz")) {
|
||||
furi_string_set(line, "Sub-GHz");
|
||||
}
|
||||
}
|
||||
if(storage_file_exists(storage, furi_string_get_cstr(line))) {
|
||||
if(furi_string_start_with(line, "/")) {
|
||||
if(!flipper_application_load_name_and_icon(line, storage, &unused_icon, label)) {
|
||||
furi_string_reset(label);
|
||||
}
|
||||
@@ -286,21 +301,19 @@ MomentumApp* momentum_app_alloc() {
|
||||
// Ignore unknown apps just like in main menu, prevents "ghost" apps when saving
|
||||
continue;
|
||||
}
|
||||
CharList_push_back(app->mainmenu_app_exes, strdup(furi_string_get_cstr(line)));
|
||||
// Display logic mimics applications/services/gui/modules/menu.c
|
||||
if(!furi_string_cmp(label, "Momentum")) {
|
||||
furi_string_set(label, "MNTM");
|
||||
} else if(!furi_string_cmp(label, "125 kHz RFID")) {
|
||||
furi_string_set(label, "RFID");
|
||||
} else if(!furi_string_cmp(label, "Sub-GHz")) {
|
||||
furi_string_set(label, "SubGHz");
|
||||
} else if(furi_string_start_with_str(label, "[")) {
|
||||
size_t trim = furi_string_search_str(label, "] ", 1);
|
||||
if(trim != FURI_STRING_FAILURE) {
|
||||
furi_string_right(label, trim + 2);
|
||||
}
|
||||
}
|
||||
CharList_push_back(app->mainmenu_app_labels, strdup(furi_string_get_cstr(label)));
|
||||
momentum_app_push_mainmenu_app(app, label, line);
|
||||
}
|
||||
} else {
|
||||
for(size_t i = 0; i < FLIPPER_APPS_COUNT; i++) {
|
||||
furi_string_set(label, FLIPPER_APPS[i].name);
|
||||
furi_string_set(line, FLIPPER_APPS[i].name);
|
||||
momentum_app_push_mainmenu_app(app, label, line);
|
||||
}
|
||||
// Until count - 1 because last app is hardcoded below
|
||||
for(size_t i = 0; i < FLIPPER_EXTERNAL_APPS_COUNT - 1; i++) {
|
||||
furi_string_set(label, FLIPPER_EXTERNAL_APPS[i].name);
|
||||
furi_string_set(line, FLIPPER_EXTERNAL_APPS[i].name);
|
||||
momentum_app_push_mainmenu_app(app, label, line);
|
||||
}
|
||||
}
|
||||
free(unused_icon);
|
||||
|
||||
@@ -141,7 +141,6 @@ bool momentum_app_scene_interface_mainmenu_on_event(void* context, SceneManagerE
|
||||
/* fall through */
|
||||
case VarItemListIndexMoveApp: {
|
||||
app->save_mainmenu_apps = true;
|
||||
app->require_reboot = true;
|
||||
size_t count = CharList_size(app->mainmenu_app_labels);
|
||||
VariableItem* item = variable_item_list_get(app->var_item_list, VarItemListIndexApp);
|
||||
if(count) {
|
||||
|
||||
@@ -53,7 +53,6 @@ static void
|
||||
CharList_push_back(app->mainmenu_app_labels, strdup(furi_string_get_cstr(temp_path)));
|
||||
app->mainmenu_app_index = CharList_size(app->mainmenu_app_labels) - 1;
|
||||
app->save_mainmenu_apps = true;
|
||||
app->require_reboot = true;
|
||||
scene_manager_search_and_switch_to_previous_scene(
|
||||
app->scene_manager, MomentumAppSceneInterfaceMainmenu);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ static void
|
||||
CharList_push_back(app->mainmenu_app_labels, strdup(name));
|
||||
app->mainmenu_app_index = CharList_size(app->mainmenu_app_labels) - 1;
|
||||
app->save_mainmenu_apps = true;
|
||||
app->require_reboot = true;
|
||||
scene_manager_search_and_switch_to_previous_scene(
|
||||
app->scene_manager, MomentumAppSceneInterfaceMainmenu);
|
||||
}
|
||||
|
||||
@@ -31,26 +31,9 @@ bool momentum_app_scene_interface_mainmenu_reset_on_event(void* context, SceneMa
|
||||
if(event.type == SceneManagerEventTypeCustom) {
|
||||
switch(event.event) {
|
||||
case DialogExResultRight:
|
||||
bool reset = false;
|
||||
Stream* stream = file_stream_alloc(furi_record_open(RECORD_STORAGE));
|
||||
if(file_stream_open(stream, MAINMENU_APPS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
|
||||
stream_write_format(stream, "MenuAppList Version %u\n", 1);
|
||||
for(size_t i = 0; i < FLIPPER_APPS_COUNT; i++) {
|
||||
stream_write_format(stream, "%s\n", FLIPPER_APPS[i].name);
|
||||
}
|
||||
for(size_t i = 0; i < FLIPPER_EXTERNAL_APPS_COUNT - 1; i++) {
|
||||
stream_write_format(stream, "%s\n", FLIPPER_EXTERNAL_APPS[i].name);
|
||||
}
|
||||
reset = true;
|
||||
}
|
||||
file_stream_close(stream);
|
||||
stream_free(stream);
|
||||
storage_common_remove(furi_record_open(RECORD_STORAGE), MAINMENU_APPS_PATH);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
if(reset) {
|
||||
app->save_mainmenu_apps = false;
|
||||
app->require_reboot = true;
|
||||
momentum_app_apply(app);
|
||||
}
|
||||
app->save_mainmenu_apps = false;
|
||||
break;
|
||||
case DialogExResultLeft:
|
||||
scene_manager_previous_scene(app->scene_manager);
|
||||
|
||||
@@ -9,11 +9,6 @@
|
||||
#include <toolbox/path.h>
|
||||
#include <flipper_application/flipper_application.h>
|
||||
#include <loader/firmware_api/firmware_api.h>
|
||||
#include <toolbox/stream/file_stream.h>
|
||||
#include <core/dangerous_defines.h>
|
||||
#include <gui/icon_i.h>
|
||||
|
||||
#include <momentum/momentum.h>
|
||||
|
||||
#define TAG "Loader"
|
||||
|
||||
@@ -268,11 +263,6 @@ FuriPubSub* loader_get_pubsub(Loader* loader) {
|
||||
return loader->pubsub;
|
||||
}
|
||||
|
||||
MenuAppList_t* loader_get_menu_apps(Loader* loader) {
|
||||
furi_assert(loader);
|
||||
return &loader->menu_apps;
|
||||
}
|
||||
|
||||
bool loader_signal(Loader* loader, uint32_t signal, void* arg) {
|
||||
furi_check(loader);
|
||||
|
||||
@@ -340,54 +330,6 @@ static void loader_thread_state_callback(FuriThreadState thread_state, void* con
|
||||
|
||||
// implementation
|
||||
|
||||
bool loader_menu_load_fap_meta(
|
||||
Storage* storage,
|
||||
FuriString* path,
|
||||
FuriString* name,
|
||||
const Icon** icon) {
|
||||
*icon = NULL;
|
||||
uint8_t* icon_buf = malloc(CUSTOM_ICON_MAX_SIZE);
|
||||
if(!flipper_application_load_name_and_icon(path, storage, &icon_buf, name)) {
|
||||
free(icon_buf);
|
||||
icon_buf = NULL;
|
||||
return false;
|
||||
}
|
||||
*icon = malloc(sizeof(Icon));
|
||||
FURI_CONST_ASSIGN((*icon)->frame_count, 1);
|
||||
FURI_CONST_ASSIGN((*icon)->frame_rate, 1);
|
||||
FURI_CONST_ASSIGN((*icon)->width, 10);
|
||||
FURI_CONST_ASSIGN((*icon)->height, 10);
|
||||
FURI_CONST_ASSIGN_PTR((*icon)->frames, malloc(sizeof(const uint8_t*)));
|
||||
FURI_CONST_ASSIGN_PTR((*icon)->frames[0], icon_buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void loader_make_menu_file(Storage* storage) {
|
||||
Stream* new = file_stream_alloc(storage);
|
||||
if(!storage_file_exists(storage, MAINMENU_APPS_PATH)) {
|
||||
if(file_stream_open(new, MAINMENU_APPS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
|
||||
stream_write_format(new, "MenuAppList Version %u\n", 1);
|
||||
for(size_t i = 0; i < FLIPPER_APPS_COUNT; i++) {
|
||||
stream_write_format(new, "%s\n", FLIPPER_APPS[i].name);
|
||||
}
|
||||
for(size_t i = 0; i < FLIPPER_EXTERNAL_APPS_COUNT - 1; i++) {
|
||||
stream_write_format(new, "%s\n", FLIPPER_EXTERNAL_APPS[i].name);
|
||||
}
|
||||
// Old additional external apps
|
||||
Stream* old = file_stream_alloc(storage);
|
||||
if(file_stream_open(old, CFG_PATH("xtreme_apps.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
stream_copy(old, new, stream_size(old));
|
||||
}
|
||||
file_stream_close(old);
|
||||
stream_free(old);
|
||||
storage_common_remove(storage, CFG_PATH("xtreme_apps.txt"));
|
||||
}
|
||||
file_stream_close(new);
|
||||
}
|
||||
file_stream_close(new);
|
||||
stream_free(new);
|
||||
}
|
||||
|
||||
static Loader* loader_alloc(void) {
|
||||
Loader* loader = malloc(sizeof(Loader));
|
||||
loader->pubsub = furi_pubsub_alloc();
|
||||
@@ -398,82 +340,6 @@ static Loader* loader_alloc(void) {
|
||||
loader->app.thread = NULL;
|
||||
loader->app.insomniac = false;
|
||||
loader->app.fap = NULL;
|
||||
MenuAppList_init(loader->menu_apps);
|
||||
|
||||
if(!furi_hal_is_normal_boot()) return loader;
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
Stream* stream = file_stream_alloc(storage);
|
||||
FuriString* line = furi_string_alloc();
|
||||
FuriString* name = furi_string_alloc();
|
||||
do {
|
||||
if(!file_stream_open(stream, MAINMENU_APPS_PATH, FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
|
||||
file_stream_close(stream);
|
||||
loader_make_menu_file(storage);
|
||||
if(!file_stream_open(stream, MAINMENU_APPS_PATH, FSAM_READ_WRITE, FSOM_OPEN_EXISTING))
|
||||
break;
|
||||
}
|
||||
|
||||
uint32_t version;
|
||||
if(!stream_read_line(stream, line) ||
|
||||
sscanf(furi_string_get_cstr(line), "MenuAppList Version %lu", &version) != 1 ||
|
||||
version > 1) {
|
||||
file_stream_close(stream);
|
||||
storage_common_remove(storage, MAINMENU_APPS_PATH);
|
||||
loader_make_menu_file(storage);
|
||||
if(!file_stream_open(stream, MAINMENU_APPS_PATH, FSAM_READ_WRITE, FSOM_OPEN_EXISTING))
|
||||
break;
|
||||
if(!stream_read_line(stream, line) ||
|
||||
sscanf(furi_string_get_cstr(line), "MenuAppList Version %lu", &version) != 1 ||
|
||||
version > 1)
|
||||
break;
|
||||
}
|
||||
|
||||
while(stream_read_line(stream, line)) {
|
||||
furi_string_replace_all(line, "\r", "");
|
||||
furi_string_replace_all(line, "\n", "");
|
||||
if(version == 0) {
|
||||
if(!furi_string_cmp(line, "RFID")) {
|
||||
furi_string_set(line, "125 kHz RFID");
|
||||
} else if(!furi_string_cmp(line, "SubGHz")) {
|
||||
furi_string_set(line, "Sub-GHz");
|
||||
}
|
||||
}
|
||||
const char* label = NULL;
|
||||
const Icon* icon = NULL;
|
||||
const char* exe = NULL;
|
||||
if(storage_file_exists(storage, furi_string_get_cstr(line))) {
|
||||
if(loader_menu_load_fap_meta(storage, line, name, &icon)) {
|
||||
label = strdup(furi_string_get_cstr(name));
|
||||
exe = strdup(furi_string_get_cstr(line));
|
||||
}
|
||||
} else {
|
||||
for(size_t i = 0; !exe && i < FLIPPER_APPS_COUNT; i++) {
|
||||
if(!strcmp(furi_string_get_cstr(line), FLIPPER_APPS[i].name)) {
|
||||
label = FLIPPER_APPS[i].name;
|
||||
icon = FLIPPER_APPS[i].icon;
|
||||
exe = FLIPPER_APPS[i].name;
|
||||
}
|
||||
}
|
||||
for(size_t i = 0; !exe && i < FLIPPER_EXTERNAL_APPS_COUNT; i++) {
|
||||
if(!strcmp(furi_string_get_cstr(line), FLIPPER_EXTERNAL_APPS[i].name)) {
|
||||
label = FLIPPER_EXTERNAL_APPS[i].name;
|
||||
icon = FLIPPER_EXTERNAL_APPS[i].icon;
|
||||
exe = FLIPPER_EXTERNAL_APPS[i].name;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(label && exe && icon) {
|
||||
MenuAppList_push_back(
|
||||
loader->menu_apps, (MenuApp){.label = label, .icon = icon, .exe = exe});
|
||||
}
|
||||
}
|
||||
|
||||
} while(false);
|
||||
furi_string_free(name);
|
||||
furi_string_free(line);
|
||||
file_stream_close(stream);
|
||||
stream_free(stream);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
return loader;
|
||||
}
|
||||
|
||||
@@ -741,9 +607,10 @@ static LoaderMessageLoaderStatusResult loader_start_external_app(
|
||||
|
||||
// process messages
|
||||
|
||||
static void loader_do_menu_show(Loader* loader, bool settings) {
|
||||
static void loader_do_menu_show(Loader* loader, bool settings_only) {
|
||||
if(!loader->loader_menu) {
|
||||
loader->loader_menu = loader_menu_alloc(loader_menu_closed_callback, loader, settings);
|
||||
loader->loader_menu =
|
||||
loader_menu_alloc(loader_menu_closed_callback, loader, settings_only);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <flipper_application/flipper_application.h>
|
||||
#include "loader.h"
|
||||
#include "loader_menu.h"
|
||||
#include "loader_menuapp.h"
|
||||
#include "loader_applications.h"
|
||||
|
||||
typedef struct {
|
||||
@@ -20,8 +19,6 @@ struct Loader {
|
||||
LoaderMenu* loader_menu;
|
||||
LoaderApplications* loader_applications;
|
||||
LoaderAppData app;
|
||||
|
||||
MenuAppList_t menu_apps;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <gui/gui.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
#include <gui/view_holder.h>
|
||||
#include <gui/modules/menu.h>
|
||||
#include <gui/modules/submenu.h>
|
||||
#include <assets_icons.h>
|
||||
@@ -7,74 +7,268 @@
|
||||
|
||||
#include "loader.h"
|
||||
#include "loader_menu.h"
|
||||
#include "loader_menuapp.h"
|
||||
|
||||
#include <flipper_application/flipper_application.h>
|
||||
#include <toolbox/stream/file_stream.h>
|
||||
#include <gui/modules/file_browser.h>
|
||||
#include <core/dangerous_defines.h>
|
||||
#include <momentum/momentum.h>
|
||||
#include <gui/icon_i.h>
|
||||
#include <m-list.h>
|
||||
|
||||
#define TAG "LoaderMenu"
|
||||
|
||||
struct LoaderMenu {
|
||||
FuriThread* thread;
|
||||
bool settings;
|
||||
void (*closed_cb)(void*);
|
||||
void* context;
|
||||
};
|
||||
|
||||
static int32_t loader_menu_thread(void* p);
|
||||
|
||||
LoaderMenu* loader_menu_alloc(void (*closed_cb)(void*), void* context, bool settings) {
|
||||
LoaderMenu* loader_menu = malloc(sizeof(LoaderMenu));
|
||||
loader_menu->closed_cb = closed_cb;
|
||||
loader_menu->context = context;
|
||||
loader_menu->settings = settings;
|
||||
loader_menu->thread = furi_thread_alloc_ex(TAG, 1024, loader_menu_thread, loader_menu);
|
||||
furi_thread_start(loader_menu->thread);
|
||||
return loader_menu;
|
||||
}
|
||||
|
||||
void loader_menu_free(LoaderMenu* loader_menu) {
|
||||
furi_assert(loader_menu);
|
||||
furi_thread_join(loader_menu->thread);
|
||||
furi_thread_free(loader_menu->thread);
|
||||
free(loader_menu);
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
LoaderMenuViewPrimary,
|
||||
LoaderMenuViewSettings,
|
||||
} LoaderMenuView;
|
||||
|
||||
struct LoaderMenu {
|
||||
FuriThread* thread;
|
||||
void (*closed_cb)(void*);
|
||||
void* context;
|
||||
|
||||
View* dummy;
|
||||
ViewHolder* view_holder;
|
||||
|
||||
Loader* loader;
|
||||
FuriPubSubSubscription* subscription;
|
||||
|
||||
uint32_t selected_primary;
|
||||
uint32_t selected_setting;
|
||||
LoaderMenuView current_view;
|
||||
bool settings_only;
|
||||
};
|
||||
|
||||
static int32_t loader_menu_thread(void* p);
|
||||
|
||||
static void loader_pubsub_callback(const void* message, void* context) {
|
||||
const LoaderEvent* event = message;
|
||||
LoaderMenu* loader_menu = context;
|
||||
|
||||
if(event->type == LoaderEventTypeApplicationBeforeLoad) {
|
||||
if(loader_menu->thread) {
|
||||
furi_thread_flags_set(furi_thread_get_id(loader_menu->thread), 0);
|
||||
furi_thread_join(loader_menu->thread);
|
||||
furi_thread_free(loader_menu->thread);
|
||||
loader_menu->thread = NULL;
|
||||
}
|
||||
} else if(
|
||||
event->type == LoaderEventTypeApplicationLoadFailed ||
|
||||
event->type == LoaderEventTypeApplicationStopped) {
|
||||
if(!loader_menu->thread) {
|
||||
loader_menu->thread = furi_thread_alloc_ex(TAG, 2048, loader_menu_thread, loader_menu);
|
||||
furi_thread_start(loader_menu->thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void loader_menu_set_view(LoaderMenu* loader_menu, View* view) {
|
||||
view_holder_set_view(loader_menu->view_holder, view);
|
||||
if(view) {
|
||||
view_holder_update(view, loader_menu->view_holder);
|
||||
}
|
||||
}
|
||||
|
||||
static void loader_menu_dummy_draw(Canvas* canvas, void* context) {
|
||||
UNUSED(context);
|
||||
|
||||
uint8_t x = canvas_width(canvas) / 2 - 24 / 2;
|
||||
uint8_t y = canvas_height(canvas) / 2 - 24 / 2;
|
||||
|
||||
canvas_draw_icon(canvas, x, y, &I_LoadingHourglass_24x24);
|
||||
}
|
||||
|
||||
enum {
|
||||
LoaderMenuIndexApplications = (uint32_t)-1,
|
||||
LoaderMenuIndexLast = (uint32_t)-2,
|
||||
LoaderMenuIndexSettings = (uint32_t)-3,
|
||||
};
|
||||
|
||||
LoaderMenu* loader_menu_alloc(void (*closed_cb)(void*), void* context, bool settings_only) {
|
||||
LoaderMenu* loader_menu = malloc(sizeof(LoaderMenu));
|
||||
loader_menu->closed_cb = closed_cb;
|
||||
loader_menu->context = context;
|
||||
loader_menu->selected_primary = LoaderMenuIndexApplications;
|
||||
loader_menu->selected_setting = 0;
|
||||
loader_menu->settings_only = settings_only;
|
||||
loader_menu->current_view = settings_only ? LoaderMenuViewSettings : LoaderMenuViewPrimary;
|
||||
|
||||
loader_menu->dummy = view_alloc();
|
||||
view_set_draw_callback(loader_menu->dummy, loader_menu_dummy_draw);
|
||||
|
||||
Gui* gui = furi_record_open(RECORD_GUI);
|
||||
loader_menu->view_holder = view_holder_alloc();
|
||||
view_holder_attach_to_gui(loader_menu->view_holder, gui);
|
||||
view_holder_set_back_callback(loader_menu->view_holder, NULL, NULL);
|
||||
loader_menu_set_view(loader_menu, loader_menu->dummy);
|
||||
view_holder_start(loader_menu->view_holder);
|
||||
|
||||
loader_menu->loader = furi_record_open(RECORD_LOADER);
|
||||
loader_menu->subscription = furi_pubsub_subscribe(
|
||||
loader_get_pubsub(loader_menu->loader), loader_pubsub_callback, loader_menu);
|
||||
|
||||
loader_menu->thread = furi_thread_alloc_ex(TAG, 2048, loader_menu_thread, loader_menu);
|
||||
furi_thread_start(loader_menu->thread);
|
||||
return loader_menu;
|
||||
}
|
||||
|
||||
void loader_menu_free(LoaderMenu* loader_menu) {
|
||||
furi_assert(loader_menu);
|
||||
|
||||
furi_pubsub_unsubscribe(loader_get_pubsub(loader_menu->loader), loader_menu->subscription);
|
||||
furi_record_close(RECORD_LOADER);
|
||||
|
||||
if(loader_menu->thread) {
|
||||
furi_thread_join(loader_menu->thread);
|
||||
furi_thread_free(loader_menu->thread);
|
||||
}
|
||||
|
||||
view_holder_free(loader_menu->view_holder);
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
view_free(loader_menu->dummy);
|
||||
|
||||
free(loader_menu);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
Gui* gui;
|
||||
ViewDispatcher* view_dispatcher;
|
||||
const char* name;
|
||||
const Icon* icon;
|
||||
const char* path;
|
||||
} MenuApp;
|
||||
|
||||
LIST_DEF(MenuAppList, MenuApp, M_POD_OPLIST)
|
||||
#define M_OPL_MenuAppList_t() LIST_OPLIST(MenuAppList)
|
||||
|
||||
typedef struct {
|
||||
LoaderMenu* loader_menu;
|
||||
Menu* primary_menu;
|
||||
Submenu* settings_menu;
|
||||
bool settings;
|
||||
MenuAppList_t apps_list;
|
||||
} LoaderMenuApp;
|
||||
|
||||
static void loader_menu_start(const char* name) {
|
||||
Loader* loader = furi_record_open(RECORD_LOADER);
|
||||
loader_start_with_gui_error(loader, name, NULL);
|
||||
loader_start_detached_with_gui_error(loader, name, NULL);
|
||||
furi_record_close(RECORD_LOADER);
|
||||
}
|
||||
|
||||
static void loader_menu_callback(void* context, uint32_t index) {
|
||||
static void loader_menu_apps_callback(void* context, uint32_t index) {
|
||||
LoaderMenuApp* app = context;
|
||||
const MenuApp* menu_app = MenuAppList_get(app->apps_list, index);
|
||||
const char* name = menu_app->path ? menu_app->path : menu_app->name;
|
||||
loader_menu_start(name);
|
||||
}
|
||||
|
||||
static void loader_menu_last_callback(void* context, uint32_t index) {
|
||||
UNUSED(index);
|
||||
UNUSED(context);
|
||||
loader_menu_start((const char*)index);
|
||||
const char* path = FLIPPER_EXTERNAL_APPS[FLIPPER_EXTERNAL_APPS_COUNT - 1].name;
|
||||
loader_menu_start(path);
|
||||
}
|
||||
|
||||
static void loader_menu_applications_callback(void* context, uint32_t index) {
|
||||
UNUSED(index);
|
||||
UNUSED(context);
|
||||
const char* name = LOADER_APPLICATIONS_NAME;
|
||||
loader_menu_start(name);
|
||||
}
|
||||
|
||||
static void loader_menu_settings_menu_callback(void* context, uint32_t index) {
|
||||
UNUSED(context);
|
||||
const char* name = FLIPPER_SETTINGS_APPS[index].name;
|
||||
loader_menu_start(name);
|
||||
}
|
||||
|
||||
static void loader_menu_switch_to_settings(void* context, uint32_t index) {
|
||||
UNUSED(index);
|
||||
LoaderMenuApp* app = context;
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, LoaderMenuViewSettings);
|
||||
loader_menu_set_view(app->loader_menu, submenu_get_view(app->settings_menu));
|
||||
app->loader_menu->current_view = LoaderMenuViewSettings;
|
||||
}
|
||||
|
||||
static uint32_t loader_menu_switch_to_primary(void* context) {
|
||||
UNUSED(context);
|
||||
return LoaderMenuViewPrimary;
|
||||
static void loader_menu_back(void* context) {
|
||||
LoaderMenuApp* app = context;
|
||||
if(app->loader_menu->current_view == LoaderMenuViewSettings &&
|
||||
!app->loader_menu->settings_only) {
|
||||
loader_menu_set_view(app->loader_menu, menu_get_view(app->primary_menu));
|
||||
app->loader_menu->current_view = LoaderMenuViewPrimary;
|
||||
} else {
|
||||
furi_thread_flags_set(furi_thread_get_id(app->loader_menu->thread), 0);
|
||||
if(app->loader_menu->closed_cb) {
|
||||
app->loader_menu->closed_cb(app->loader_menu->context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t loader_menu_exit(void* context) {
|
||||
UNUSED(context);
|
||||
return VIEW_NONE;
|
||||
static void loader_menu_add_app_entry(
|
||||
LoaderMenuApp* app,
|
||||
const char* name,
|
||||
const Icon* icon,
|
||||
const char* path) {
|
||||
MenuAppList_push_back(app->apps_list, (MenuApp){name, icon, path});
|
||||
menu_add_item(
|
||||
app->primary_menu,
|
||||
name,
|
||||
icon,
|
||||
MenuAppList_size(app->apps_list) - 1,
|
||||
loader_menu_apps_callback,
|
||||
app);
|
||||
}
|
||||
|
||||
bool loader_menu_load_fap_meta(
|
||||
Storage* storage,
|
||||
FuriString* path,
|
||||
FuriString* name,
|
||||
const Icon** icon) {
|
||||
*icon = NULL;
|
||||
uint8_t* icon_buf = malloc(CUSTOM_ICON_MAX_SIZE);
|
||||
if(!flipper_application_load_name_and_icon(path, storage, &icon_buf, name)) {
|
||||
free(icon_buf);
|
||||
icon_buf = NULL;
|
||||
return false;
|
||||
}
|
||||
*icon = malloc(sizeof(Icon));
|
||||
FURI_CONST_ASSIGN((*icon)->frame_count, 1);
|
||||
FURI_CONST_ASSIGN((*icon)->frame_rate, 1);
|
||||
FURI_CONST_ASSIGN((*icon)->width, 10);
|
||||
FURI_CONST_ASSIGN((*icon)->height, 10);
|
||||
FURI_CONST_ASSIGN_PTR((*icon)->frames, malloc(sizeof(const uint8_t*)));
|
||||
FURI_CONST_ASSIGN_PTR((*icon)->frames[0], icon_buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void loader_menu_find_add_app(LoaderMenuApp* app, Storage* storage, FuriString* line) {
|
||||
const char* name = NULL;
|
||||
const Icon* icon = NULL;
|
||||
const char* path = NULL;
|
||||
if(furi_string_start_with(line, "/")) {
|
||||
path = strdup(furi_string_get_cstr(line));
|
||||
if(!loader_menu_load_fap_meta(storage, line, line, &icon)) {
|
||||
free((void*)path);
|
||||
path = NULL;
|
||||
} else {
|
||||
name = strdup(furi_string_get_cstr(line));
|
||||
}
|
||||
} else {
|
||||
for(size_t i = 0; !name && i < FLIPPER_APPS_COUNT; i++) {
|
||||
if(furi_string_equal(line, FLIPPER_APPS[i].name)) {
|
||||
name = FLIPPER_APPS[i].name;
|
||||
icon = FLIPPER_APPS[i].icon;
|
||||
}
|
||||
}
|
||||
for(size_t i = 0; !name && i < FLIPPER_EXTERNAL_APPS_COUNT; i++) {
|
||||
if(furi_string_equal(line, FLIPPER_EXTERNAL_APPS[i].name)) {
|
||||
name = FLIPPER_EXTERNAL_APPS[i].name;
|
||||
icon = FLIPPER_EXTERNAL_APPS[i].icon;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Path only set for FAPs
|
||||
if(name && icon) {
|
||||
loader_menu_add_app_entry(app, name, icon, path);
|
||||
}
|
||||
}
|
||||
|
||||
static void loader_menu_build_menu(LoaderMenuApp* app, LoaderMenu* menu) {
|
||||
@@ -82,30 +276,62 @@ static void loader_menu_build_menu(LoaderMenuApp* app, LoaderMenu* menu) {
|
||||
app->primary_menu,
|
||||
LOADER_APPLICATIONS_NAME,
|
||||
&A_Plugins_14,
|
||||
(uint32_t)LOADER_APPLICATIONS_NAME,
|
||||
loader_menu_callback,
|
||||
(void*)menu);
|
||||
LoaderMenuIndexApplications,
|
||||
loader_menu_applications_callback,
|
||||
NULL);
|
||||
|
||||
Loader* loader = furi_record_open(RECORD_LOADER);
|
||||
MenuAppList_t* menu_apps = loader_get_menu_apps(loader);
|
||||
for(size_t i = 0; i < MenuAppList_size(*menu_apps); i++) {
|
||||
const MenuApp* menu_app = MenuAppList_get(*menu_apps, i);
|
||||
menu_add_item(
|
||||
app->primary_menu,
|
||||
menu_app->label,
|
||||
menu_app->icon,
|
||||
(uint32_t)menu_app->exe,
|
||||
loader_menu_callback,
|
||||
(void*)menu);
|
||||
MenuAppList_init(app->apps_list);
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
Stream* stream = file_stream_alloc(storage);
|
||||
FuriString* line = furi_string_alloc();
|
||||
uint32_t version;
|
||||
if(file_stream_open(stream, MAINMENU_APPS_PATH, FSAM_READ, FSOM_OPEN_EXISTING) &&
|
||||
stream_read_line(stream, line) &&
|
||||
sscanf(furi_string_get_cstr(line), "MenuAppList Version %lu", &version) == 1 &&
|
||||
version <= 1) {
|
||||
while(stream_read_line(stream, line)) {
|
||||
furi_string_trim(line);
|
||||
if(version == 0) {
|
||||
if(furi_string_equal(line, "RFID")) {
|
||||
furi_string_set(line, "125 kHz RFID");
|
||||
} else if(furi_string_equal(line, "SubGHz")) {
|
||||
furi_string_set(line, "Sub-GHz");
|
||||
}
|
||||
}
|
||||
loader_menu_find_add_app(app, storage, line);
|
||||
}
|
||||
} else {
|
||||
for(size_t i = 0; i < FLIPPER_APPS_COUNT; i++) {
|
||||
loader_menu_add_app_entry(app, FLIPPER_APPS[i].name, FLIPPER_APPS[i].icon, NULL);
|
||||
}
|
||||
// Until count - 1 because last app is hardcoded below
|
||||
for(size_t i = 0; i < FLIPPER_EXTERNAL_APPS_COUNT - 1; i++) {
|
||||
loader_menu_add_app_entry(
|
||||
app, FLIPPER_EXTERNAL_APPS[i].name, FLIPPER_EXTERNAL_APPS[i].icon, NULL);
|
||||
}
|
||||
}
|
||||
furi_record_close(RECORD_LOADER);
|
||||
furi_string_free(line);
|
||||
stream_free(stream);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
const FlipperExternalApplication* last =
|
||||
&FLIPPER_EXTERNAL_APPS[FLIPPER_EXTERNAL_APPS_COUNT - 1];
|
||||
menu_add_item(
|
||||
app->primary_menu, last->name, last->icon, (uint32_t)last->path, loader_menu_callback, app);
|
||||
app->primary_menu,
|
||||
last->name,
|
||||
last->icon,
|
||||
LoaderMenuIndexLast,
|
||||
loader_menu_last_callback,
|
||||
NULL);
|
||||
menu_add_item(
|
||||
app->primary_menu, "Settings", &A_Settings_14, 0, loader_menu_switch_to_settings, app);
|
||||
app->primary_menu,
|
||||
"Settings",
|
||||
&A_Settings_14,
|
||||
LoaderMenuIndexSettings,
|
||||
loader_menu_switch_to_settings,
|
||||
app);
|
||||
|
||||
menu_set_selected_item(app->primary_menu, menu->selected_primary);
|
||||
}
|
||||
|
||||
static void loader_menu_build_submenu(LoaderMenuApp* app, LoaderMenu* loader_menu) {
|
||||
@@ -113,55 +339,62 @@ static void loader_menu_build_submenu(LoaderMenuApp* app, LoaderMenu* loader_men
|
||||
submenu_add_item(
|
||||
app->settings_menu,
|
||||
FLIPPER_SETTINGS_APPS[i].name,
|
||||
(uint32_t)FLIPPER_SETTINGS_APPS[i].name,
|
||||
loader_menu_callback,
|
||||
loader_menu);
|
||||
i,
|
||||
loader_menu_settings_menu_callback,
|
||||
NULL);
|
||||
}
|
||||
submenu_set_selected_item(app->settings_menu, loader_menu->selected_setting);
|
||||
}
|
||||
|
||||
static LoaderMenuApp* loader_menu_app_alloc(LoaderMenu* loader_menu) {
|
||||
LoaderMenuApp* app = malloc(sizeof(LoaderMenuApp));
|
||||
app->gui = furi_record_open(RECORD_GUI);
|
||||
app->view_dispatcher = view_dispatcher_alloc();
|
||||
app->settings = loader_menu->settings;
|
||||
app->loader_menu = loader_menu;
|
||||
|
||||
// Primary menu
|
||||
if(!app->settings) {
|
||||
if(!app->loader_menu->settings_only) {
|
||||
app->primary_menu = menu_alloc();
|
||||
loader_menu_build_menu(app, loader_menu);
|
||||
View* primary_view = menu_get_view(app->primary_menu);
|
||||
view_set_context(primary_view, app->primary_menu);
|
||||
view_set_previous_callback(primary_view, loader_menu_exit);
|
||||
view_dispatcher_add_view(app->view_dispatcher, LoaderMenuViewPrimary, primary_view);
|
||||
}
|
||||
|
||||
// Settings menu
|
||||
app->settings_menu = submenu_alloc();
|
||||
loader_menu_build_submenu(app, loader_menu);
|
||||
View* settings_view = submenu_get_view(app->settings_menu);
|
||||
view_set_context(settings_view, app->settings_menu);
|
||||
view_set_previous_callback(
|
||||
settings_view, app->settings ? loader_menu_exit : loader_menu_switch_to_primary);
|
||||
view_dispatcher_add_view(app->view_dispatcher, LoaderMenuViewSettings, settings_view);
|
||||
|
||||
view_dispatcher_enable_queue(app->view_dispatcher);
|
||||
view_dispatcher_switch_to_view(
|
||||
app->view_dispatcher, app->settings ? LoaderMenuViewSettings : LoaderMenuViewPrimary);
|
||||
View* view = app->loader_menu->current_view == LoaderMenuViewSettings ?
|
||||
submenu_get_view(app->settings_menu) :
|
||||
menu_get_view(app->primary_menu);
|
||||
loader_menu_set_view(app->loader_menu, view);
|
||||
view_holder_set_back_callback(app->loader_menu->view_holder, loader_menu_back, app);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
static void loader_menu_app_free(LoaderMenuApp* app) {
|
||||
if(!app->settings) {
|
||||
view_dispatcher_remove_view(app->view_dispatcher, LoaderMenuViewPrimary);
|
||||
view_holder_set_back_callback(app->loader_menu->view_holder, NULL, NULL);
|
||||
loader_menu_set_view(app->loader_menu, app->loader_menu->dummy);
|
||||
|
||||
if(!app->loader_menu->settings_only) {
|
||||
app->loader_menu->selected_primary = menu_get_selected_item(app->primary_menu);
|
||||
menu_free(app->primary_menu);
|
||||
for
|
||||
M_EACH(menu_app, app->apps_list, MenuAppList_t) {
|
||||
// Path only set for FAPs, if unset then name and
|
||||
// icon point to flash and must not be freed
|
||||
if(menu_app->path) {
|
||||
free((void*)menu_app->name);
|
||||
free((void*)menu_app->icon->frames[0]);
|
||||
free((void*)menu_app->icon->frames);
|
||||
free((void*)menu_app->icon);
|
||||
free((void*)menu_app->path);
|
||||
}
|
||||
}
|
||||
MenuAppList_clear(app->apps_list);
|
||||
}
|
||||
view_dispatcher_remove_view(app->view_dispatcher, LoaderMenuViewSettings);
|
||||
app->loader_menu->selected_setting = app->loader_menu->current_view == LoaderMenuViewSettings ?
|
||||
submenu_get_selected_item(app->settings_menu) :
|
||||
0;
|
||||
submenu_free(app->settings_menu);
|
||||
|
||||
view_dispatcher_free(app->view_dispatcher);
|
||||
|
||||
furi_record_close(RECORD_GUI);
|
||||
free(app);
|
||||
}
|
||||
|
||||
@@ -171,12 +404,7 @@ static int32_t loader_menu_thread(void* p) {
|
||||
|
||||
LoaderMenuApp* app = loader_menu_app_alloc(loader_menu);
|
||||
|
||||
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
|
||||
view_dispatcher_run(app->view_dispatcher);
|
||||
|
||||
if(loader_menu->closed_cb) {
|
||||
loader_menu->closed_cb(loader_menu->context);
|
||||
}
|
||||
furi_thread_flags_wait(0, FuriFlagWaitAll, FuriWaitForever);
|
||||
|
||||
loader_menu_app_free(app);
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ extern "C" {
|
||||
|
||||
typedef struct LoaderMenu LoaderMenu;
|
||||
|
||||
LoaderMenu* loader_menu_alloc(void (*closed_cb)(void*), void* context, bool settings);
|
||||
LoaderMenu* loader_menu_alloc(void (*closed_cb)(void*), void* context, bool settings_only);
|
||||
|
||||
void loader_menu_free(LoaderMenu* loader_menu);
|
||||
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/icon.h>
|
||||
#include <m-list.h>
|
||||
|
||||
typedef struct {
|
||||
const char* label;
|
||||
const Icon* icon;
|
||||
const char* exe;
|
||||
} MenuApp;
|
||||
|
||||
LIST_DEF(MenuAppList, MenuApp, M_POD_OPLIST)
|
||||
|
||||
MenuAppList_t* loader_get_menu_apps(Loader* loader);
|
||||
Reference in New Issue
Block a user