2022-09-14 16:11:38 +00:00
|
|
|
#include <furi.h>
|
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <gui/view_dispatcher.h>
|
|
|
|
#include <storage/storage.h>
|
2022-10-06 15:37:53 +00:00
|
|
|
#include <gui/modules/loading.h>
|
2022-09-14 16:11:38 +00:00
|
|
|
#include <dialogs/dialogs.h>
|
|
|
|
#include <flipper_application/flipper_application.h>
|
2022-10-06 15:37:53 +00:00
|
|
|
#include "elf_cpp/elf_hashtable.h"
|
|
|
|
#include "fap_loader_app.h"
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
#define TAG "fap_loader_app"
|
|
|
|
|
2022-10-06 15:37:53 +00:00
|
|
|
struct FapLoader {
|
2022-09-14 16:11:38 +00:00
|
|
|
FlipperApplication* app;
|
|
|
|
Storage* storage;
|
|
|
|
DialogsApp* dialogs;
|
|
|
|
Gui* gui;
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* fap_path;
|
2022-09-26 15:59:28 +00:00
|
|
|
ViewDispatcher* view_dispatcher;
|
|
|
|
Loading* loading;
|
2022-10-06 15:37:53 +00:00
|
|
|
};
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-10-06 15:37:53 +00:00
|
|
|
bool fap_loader_load_name_and_icon(
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* path,
|
2022-10-06 15:37:53 +00:00
|
|
|
Storage* storage,
|
2022-10-05 15:15:23 +00:00
|
|
|
uint8_t** icon_ptr,
|
|
|
|
FuriString* item_name) {
|
2022-10-06 15:37:53 +00:00
|
|
|
FlipperApplication* app = flipper_application_alloc(storage, &hashtable_api_interface);
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
FlipperApplicationPreloadStatus preload_res =
|
2022-10-05 15:15:23 +00:00
|
|
|
flipper_application_preload_manifest(app, furi_string_get_cstr(path));
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
bool load_success = false;
|
|
|
|
|
|
|
|
if(preload_res == FlipperApplicationPreloadStatusSuccess) {
|
|
|
|
const FlipperApplicationManifest* manifest = flipper_application_get_manifest(app);
|
|
|
|
if(manifest->has_icon) {
|
|
|
|
memcpy(*icon_ptr, manifest->icon, FAP_MANIFEST_MAX_ICON_SIZE);
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(item_name, manifest->name);
|
2022-09-14 16:11:38 +00:00
|
|
|
load_success = true;
|
|
|
|
} else {
|
2022-10-05 15:15:23 +00:00
|
|
|
FURI_LOG_E(TAG, "FAP Loader failed to preload %s", furi_string_get_cstr(path));
|
2022-09-14 16:11:38 +00:00
|
|
|
load_success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
flipper_application_free(app);
|
|
|
|
return load_success;
|
|
|
|
}
|
|
|
|
|
2022-10-06 15:37:53 +00:00
|
|
|
static bool fap_loader_item_callback(
|
|
|
|
FuriString* path,
|
|
|
|
void* context,
|
|
|
|
uint8_t** icon_ptr,
|
|
|
|
FuriString* item_name) {
|
|
|
|
FapLoader* fap_loader = context;
|
|
|
|
furi_assert(fap_loader);
|
|
|
|
return fap_loader_load_name_and_icon(path, fap_loader->storage, icon_ptr, item_name);
|
|
|
|
}
|
|
|
|
|
2022-09-14 16:11:38 +00:00
|
|
|
static bool fap_loader_run_selected_app(FapLoader* loader) {
|
|
|
|
furi_assert(loader);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* error_message;
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
error_message = furi_string_alloc_set("unknown error");
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
bool file_selected = false;
|
|
|
|
bool show_error = true;
|
|
|
|
do {
|
|
|
|
file_selected = true;
|
|
|
|
loader->app = flipper_application_alloc(loader->storage, &hashtable_api_interface);
|
2022-10-07 17:06:29 +00:00
|
|
|
size_t start = furi_get_tick();
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FURI_LOG_I(TAG, "FAP Loader is loading %s", furi_string_get_cstr(loader->fap_path));
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
FlipperApplicationPreloadStatus preload_res =
|
2022-10-05 15:15:23 +00:00
|
|
|
flipper_application_preload(loader->app, furi_string_get_cstr(loader->fap_path));
|
2022-09-14 16:11:38 +00:00
|
|
|
if(preload_res != FlipperApplicationPreloadStatusSuccess) {
|
|
|
|
const char* err_msg = flipper_application_preload_status_to_string(preload_res);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_printf(error_message, "Preload failed: %s", err_msg);
|
2022-09-14 16:11:38 +00:00
|
|
|
FURI_LOG_E(
|
|
|
|
TAG,
|
|
|
|
"FAP Loader failed to preload %s: %s",
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(loader->fap_path),
|
2022-09-14 16:11:38 +00:00
|
|
|
err_msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FURI_LOG_I(TAG, "FAP Loader is mapping");
|
|
|
|
FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(loader->app);
|
|
|
|
if(load_status != FlipperApplicationLoadStatusSuccess) {
|
|
|
|
const char* err_msg = flipper_application_load_status_to_string(load_status);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_printf(error_message, "Load failed: %s", err_msg);
|
2022-09-14 16:11:38 +00:00
|
|
|
FURI_LOG_E(
|
|
|
|
TAG,
|
|
|
|
"FAP Loader failed to map to memory %s: %s",
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(loader->fap_path),
|
2022-09-14 16:11:38 +00:00
|
|
|
err_msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-07 17:06:29 +00:00
|
|
|
FURI_LOG_I(TAG, "Loaded in %ums", (size_t)(furi_get_tick() - start));
|
2022-09-14 16:11:38 +00:00
|
|
|
FURI_LOG_I(TAG, "FAP Loader is staring app");
|
|
|
|
|
|
|
|
FuriThread* thread = flipper_application_spawn(loader->app, NULL);
|
|
|
|
furi_thread_start(thread);
|
|
|
|
furi_thread_join(thread);
|
|
|
|
|
|
|
|
show_error = false;
|
|
|
|
int ret = furi_thread_get_return_code(thread);
|
|
|
|
|
|
|
|
FURI_LOG_I(TAG, "FAP app returned: %i", ret);
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
if(show_error) {
|
|
|
|
DialogMessage* message = dialog_message_alloc();
|
|
|
|
dialog_message_set_header(message, "Error", 64, 0, AlignCenter, AlignTop);
|
|
|
|
dialog_message_set_buttons(message, NULL, NULL, NULL);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* buffer;
|
|
|
|
buffer = furi_string_alloc();
|
|
|
|
furi_string_printf(buffer, "%s", furi_string_get_cstr(error_message));
|
|
|
|
furi_string_replace(buffer, ":", "\n");
|
2022-09-14 16:11:38 +00:00
|
|
|
dialog_message_set_text(
|
2022-10-05 15:15:23 +00:00
|
|
|
message, furi_string_get_cstr(buffer), 64, 32, AlignCenter, AlignCenter);
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
dialog_message_show(loader->dialogs, message);
|
|
|
|
dialog_message_free(message);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(buffer);
|
2022-09-14 16:11:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(error_message);
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
if(file_selected) {
|
|
|
|
flipper_application_free(loader->app);
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool fap_loader_select_app(FapLoader* loader) {
|
|
|
|
const DialogsFileBrowserOptions browser_options = {
|
|
|
|
.extension = ".fap",
|
|
|
|
.skip_assets = true,
|
2022-10-06 15:37:53 +00:00
|
|
|
.icon = &I_unknown_10px,
|
2022-09-14 16:11:38 +00:00
|
|
|
.hide_ext = true,
|
|
|
|
.item_loader_callback = fap_loader_item_callback,
|
|
|
|
.item_loader_context = loader,
|
|
|
|
};
|
|
|
|
|
|
|
|
return dialog_file_browser_show(
|
|
|
|
loader->dialogs, loader->fap_path, loader->fap_path, &browser_options);
|
|
|
|
}
|
|
|
|
|
2022-10-06 15:37:53 +00:00
|
|
|
static FapLoader* fap_loader_alloc(const char* path) {
|
2022-09-14 16:11:38 +00:00
|
|
|
FapLoader* loader = malloc(sizeof(FapLoader));
|
2022-10-06 15:37:53 +00:00
|
|
|
loader->fap_path = furi_string_alloc_set(path);
|
2022-09-14 16:11:38 +00:00
|
|
|
loader->storage = furi_record_open(RECORD_STORAGE);
|
|
|
|
loader->dialogs = furi_record_open(RECORD_DIALOGS);
|
|
|
|
loader->gui = furi_record_open(RECORD_GUI);
|
2022-09-26 15:59:28 +00:00
|
|
|
loader->view_dispatcher = view_dispatcher_alloc();
|
|
|
|
loader->loading = loading_alloc();
|
|
|
|
view_dispatcher_attach_to_gui(
|
|
|
|
loader->view_dispatcher, loader->gui, ViewDispatcherTypeFullscreen);
|
|
|
|
view_dispatcher_add_view(loader->view_dispatcher, 0, loading_get_view(loader->loading));
|
2022-10-06 15:37:53 +00:00
|
|
|
return loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fap_loader_free(FapLoader* loader) {
|
|
|
|
view_dispatcher_remove_view(loader->view_dispatcher, 0);
|
|
|
|
loading_free(loader->loading);
|
|
|
|
view_dispatcher_free(loader->view_dispatcher);
|
|
|
|
furi_string_free(loader->fap_path);
|
|
|
|
furi_record_close(RECORD_GUI);
|
|
|
|
furi_record_close(RECORD_DIALOGS);
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
|
free(loader);
|
|
|
|
}
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-10-06 15:37:53 +00:00
|
|
|
int32_t fap_loader_app(void* p) {
|
|
|
|
FapLoader* loader;
|
2022-09-14 16:11:38 +00:00
|
|
|
if(p) {
|
2022-10-06 15:37:53 +00:00
|
|
|
loader = fap_loader_alloc((const char*)p);
|
2022-10-13 18:09:37 +00:00
|
|
|
view_dispatcher_switch_to_view(loader->view_dispatcher, 0);
|
2022-09-14 16:11:38 +00:00
|
|
|
fap_loader_run_selected_app(loader);
|
|
|
|
} else {
|
2022-10-06 15:37:53 +00:00
|
|
|
loader = fap_loader_alloc(EXT_PATH("apps"));
|
2022-09-14 16:11:38 +00:00
|
|
|
while(fap_loader_select_app(loader)) {
|
2022-09-26 15:59:28 +00:00
|
|
|
view_dispatcher_switch_to_view(loader->view_dispatcher, 0);
|
2022-09-14 16:11:38 +00:00
|
|
|
fap_loader_run_selected_app(loader);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-06 15:37:53 +00:00
|
|
|
fap_loader_free(loader);
|
2022-09-14 16:11:38 +00:00
|
|
|
return 0;
|
2022-10-06 15:37:53 +00:00
|
|
|
}
|