fap_loader_app.c 6.6 KB

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