fap_loader_app.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <gui/view_dispatcher.h>
  4. #include <storage/storage.h>
  5. #include <gui/modules/loading.h>
  6. #include <dialogs/dialogs.h>
  7. #include <flipper_application/flipper_application.h>
  8. #include "elf_cpp/elf_hashtable.h"
  9. #include "fap_loader_app.h"
  10. #define TAG "fap_loader_app"
  11. struct FapLoader {
  12. FlipperApplication* app;
  13. Storage* storage;
  14. DialogsApp* dialogs;
  15. Gui* gui;
  16. FuriString* fap_path;
  17. ViewDispatcher* view_dispatcher;
  18. Loading* loading;
  19. };
  20. bool fap_loader_load_name_and_icon(
  21. FuriString* path,
  22. Storage* storage,
  23. uint8_t** icon_ptr,
  24. FuriString* item_name) {
  25. FlipperApplication* app = flipper_application_alloc(storage, &hashtable_api_interface);
  26. FlipperApplicationPreloadStatus preload_res =
  27. flipper_application_preload_manifest(app, furi_string_get_cstr(path));
  28. bool load_success = false;
  29. if(preload_res == FlipperApplicationPreloadStatusSuccess) {
  30. const FlipperApplicationManifest* manifest = flipper_application_get_manifest(app);
  31. if(manifest->has_icon) {
  32. memcpy(*icon_ptr, manifest->icon, FAP_MANIFEST_MAX_ICON_SIZE);
  33. }
  34. furi_string_set(item_name, manifest->name);
  35. load_success = true;
  36. } else {
  37. FURI_LOG_E(TAG, "FAP Loader failed to preload %s", furi_string_get_cstr(path));
  38. load_success = false;
  39. }
  40. flipper_application_free(app);
  41. return load_success;
  42. }
  43. static bool fap_loader_item_callback(
  44. FuriString* path,
  45. void* context,
  46. uint8_t** icon_ptr,
  47. FuriString* item_name) {
  48. FapLoader* fap_loader = context;
  49. furi_assert(fap_loader);
  50. return fap_loader_load_name_and_icon(path, fap_loader->storage, icon_ptr, item_name);
  51. }
  52. static bool fap_loader_run_selected_app(FapLoader* loader) {
  53. furi_assert(loader);
  54. FuriString* error_message;
  55. error_message = furi_string_alloc_set("unknown error");
  56. bool file_selected = false;
  57. bool show_error = true;
  58. do {
  59. file_selected = true;
  60. loader->app = flipper_application_alloc(loader->storage, &hashtable_api_interface);
  61. size_t start = furi_get_tick();
  62. FURI_LOG_I(TAG, "FAP Loader is loading %s", furi_string_get_cstr(loader->fap_path));
  63. FlipperApplicationPreloadStatus preload_res =
  64. flipper_application_preload(loader->app, furi_string_get_cstr(loader->fap_path));
  65. if(preload_res != FlipperApplicationPreloadStatusSuccess) {
  66. const char* err_msg = flipper_application_preload_status_to_string(preload_res);
  67. furi_string_printf(error_message, "Preload failed: %s", err_msg);
  68. FURI_LOG_E(
  69. TAG,
  70. "FAP Loader failed to preload %s: %s",
  71. furi_string_get_cstr(loader->fap_path),
  72. err_msg);
  73. break;
  74. }
  75. FURI_LOG_I(TAG, "FAP Loader is mapping");
  76. FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(loader->app);
  77. if(load_status != FlipperApplicationLoadStatusSuccess) {
  78. const char* err_msg = flipper_application_load_status_to_string(load_status);
  79. furi_string_printf(error_message, "Load failed: %s", err_msg);
  80. FURI_LOG_E(
  81. TAG,
  82. "FAP Loader failed to map to memory %s: %s",
  83. furi_string_get_cstr(loader->fap_path),
  84. err_msg);
  85. break;
  86. }
  87. FURI_LOG_I(TAG, "Loaded in %ums", (size_t)(furi_get_tick() - start));
  88. FURI_LOG_I(TAG, "FAP Loader is starting app");
  89. FuriThread* thread = flipper_application_spawn(loader->app, NULL);
  90. furi_thread_start(thread);
  91. furi_thread_join(thread);
  92. show_error = false;
  93. int ret = furi_thread_get_return_code(thread);
  94. FURI_LOG_I(TAG, "FAP app returned: %i", ret);
  95. } while(0);
  96. if(show_error) {
  97. DialogMessage* message = dialog_message_alloc();
  98. dialog_message_set_header(message, "Error", 64, 0, AlignCenter, AlignTop);
  99. dialog_message_set_buttons(message, NULL, NULL, NULL);
  100. FuriString* buffer;
  101. buffer = furi_string_alloc();
  102. furi_string_printf(buffer, "%s", furi_string_get_cstr(error_message));
  103. furi_string_replace(buffer, ":", "\n");
  104. dialog_message_set_text(
  105. message, furi_string_get_cstr(buffer), 64, 32, AlignCenter, AlignCenter);
  106. dialog_message_show(loader->dialogs, message);
  107. dialog_message_free(message);
  108. furi_string_free(buffer);
  109. }
  110. furi_string_free(error_message);
  111. if(file_selected) {
  112. flipper_application_free(loader->app);
  113. }
  114. return file_selected;
  115. }
  116. static bool fap_loader_select_app(FapLoader* loader) {
  117. const DialogsFileBrowserOptions browser_options = {
  118. .extension = ".fap",
  119. .skip_assets = true,
  120. .icon = &I_unknown_10px,
  121. .hide_ext = true,
  122. .item_loader_callback = fap_loader_item_callback,
  123. .item_loader_context = loader,
  124. };
  125. return dialog_file_browser_show(
  126. loader->dialogs, loader->fap_path, loader->fap_path, &browser_options);
  127. }
  128. static FapLoader* fap_loader_alloc(const char* path) {
  129. FapLoader* loader = malloc(sizeof(FapLoader));
  130. loader->fap_path = furi_string_alloc_set(path);
  131. loader->storage = furi_record_open(RECORD_STORAGE);
  132. loader->dialogs = furi_record_open(RECORD_DIALOGS);
  133. loader->gui = furi_record_open(RECORD_GUI);
  134. loader->view_dispatcher = view_dispatcher_alloc();
  135. loader->loading = loading_alloc();
  136. view_dispatcher_attach_to_gui(
  137. loader->view_dispatcher, loader->gui, ViewDispatcherTypeFullscreen);
  138. view_dispatcher_add_view(loader->view_dispatcher, 0, loading_get_view(loader->loading));
  139. return loader;
  140. }
  141. static void fap_loader_free(FapLoader* loader) {
  142. view_dispatcher_remove_view(loader->view_dispatcher, 0);
  143. loading_free(loader->loading);
  144. view_dispatcher_free(loader->view_dispatcher);
  145. furi_string_free(loader->fap_path);
  146. furi_record_close(RECORD_GUI);
  147. furi_record_close(RECORD_DIALOGS);
  148. furi_record_close(RECORD_STORAGE);
  149. free(loader);
  150. }
  151. int32_t fap_loader_app(void* p) {
  152. FapLoader* loader;
  153. if(p) {
  154. loader = fap_loader_alloc((const char*)p);
  155. view_dispatcher_switch_to_view(loader->view_dispatcher, 0);
  156. fap_loader_run_selected_app(loader);
  157. } else {
  158. loader = fap_loader_alloc(EXT_PATH("apps"));
  159. while(fap_loader_select_app(loader)) {
  160. view_dispatcher_switch_to_view(loader->view_dispatcher, 0);
  161. fap_loader_run_selected_app(loader);
  162. };
  163. }
  164. fap_loader_free(loader);
  165. return 0;
  166. }