fap_loader_app.c 6.9 KB

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