desktop_scene_main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <applications.h>
  4. #include <assets_icons.h>
  5. #include <loader/loader.h>
  6. #include "../desktop_i.h"
  7. #include "../views/desktop_events.h"
  8. #include "../views/desktop_view_main.h"
  9. #include "desktop_scene.h"
  10. #include "desktop_scene_i.h"
  11. #define TAG "DesktopSrv"
  12. static void desktop_scene_main_new_idle_animation_callback(void* context) {
  13. furi_assert(context);
  14. Desktop* desktop = context;
  15. view_dispatcher_send_custom_event(
  16. desktop->view_dispatcher, DesktopAnimationEventNewIdleAnimation);
  17. }
  18. static void desktop_scene_main_check_animation_callback(void* context) {
  19. furi_assert(context);
  20. Desktop* desktop = context;
  21. view_dispatcher_send_custom_event(
  22. desktop->view_dispatcher, DesktopAnimationEventCheckAnimation);
  23. }
  24. static void desktop_scene_main_interact_animation_callback(void* context) {
  25. furi_assert(context);
  26. Desktop* desktop = context;
  27. view_dispatcher_send_custom_event(
  28. desktop->view_dispatcher, DesktopAnimationEventInteractAnimation);
  29. }
  30. #ifdef APP_ARCHIVE
  31. static void desktop_switch_to_app(Desktop* desktop, const FlipperApplication* flipper_app) {
  32. furi_assert(desktop);
  33. furi_assert(flipper_app);
  34. furi_assert(flipper_app->app);
  35. furi_assert(flipper_app->name);
  36. if(furi_thread_get_state(desktop->scene_thread) != FuriThreadStateStopped) {
  37. FURI_LOG_E("Desktop", "Thread is already running");
  38. return;
  39. }
  40. FuriHalRtcHeapTrackMode mode = furi_hal_rtc_get_heap_track_mode();
  41. if(mode > FuriHalRtcHeapTrackModeNone) {
  42. furi_thread_enable_heap_trace(desktop->scene_thread);
  43. } else {
  44. furi_thread_disable_heap_trace(desktop->scene_thread);
  45. }
  46. furi_thread_set_name(desktop->scene_thread, flipper_app->name);
  47. furi_thread_set_stack_size(desktop->scene_thread, flipper_app->stack_size);
  48. furi_thread_set_callback(desktop->scene_thread, flipper_app->app);
  49. furi_thread_start(desktop->scene_thread);
  50. }
  51. #endif
  52. void desktop_scene_main_callback(DesktopEvent event, void* context) {
  53. Desktop* desktop = (Desktop*)context;
  54. view_dispatcher_send_custom_event(desktop->view_dispatcher, event);
  55. }
  56. void desktop_scene_main_on_enter(void* context) {
  57. Desktop* desktop = (Desktop*)context;
  58. DesktopMainView* main_view = desktop->main_view;
  59. animation_manager_set_context(desktop->animation_manager, desktop);
  60. animation_manager_set_new_idle_callback(
  61. desktop->animation_manager, desktop_scene_main_new_idle_animation_callback);
  62. animation_manager_set_check_callback(
  63. desktop->animation_manager, desktop_scene_main_check_animation_callback);
  64. animation_manager_set_interact_callback(
  65. desktop->animation_manager, desktop_scene_main_interact_animation_callback);
  66. desktop_main_set_callback(main_view, desktop_scene_main_callback, desktop);
  67. view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdMain);
  68. }
  69. bool desktop_scene_main_on_event(void* context, SceneManagerEvent event) {
  70. Desktop* desktop = (Desktop*)context;
  71. bool consumed = false;
  72. if(event.type == SceneManagerEventTypeCustom) {
  73. switch(event.event) {
  74. case DesktopMainEventOpenMenu:
  75. loader_show_menu();
  76. consumed = true;
  77. break;
  78. case DesktopMainEventOpenLockMenu:
  79. scene_manager_next_scene(desktop->scene_manager, DesktopSceneLockMenu);
  80. consumed = true;
  81. break;
  82. case DesktopMainEventOpenDebug:
  83. scene_manager_next_scene(desktop->scene_manager, DesktopSceneDebug);
  84. consumed = true;
  85. break;
  86. case DesktopMainEventOpenArchive:
  87. #ifdef APP_ARCHIVE
  88. desktop_switch_to_app(desktop, &FLIPPER_ARCHIVE);
  89. #endif
  90. consumed = true;
  91. break;
  92. case DesktopMainEventOpenPowerOff: {
  93. LoaderStatus status = loader_start(desktop->loader, "Power", "off");
  94. if(status != LoaderStatusOk) {
  95. FURI_LOG_E(TAG, "loader_start failed: %d", status);
  96. }
  97. consumed = true;
  98. break;
  99. }
  100. case DesktopMainEventOpenFavoritePrimary:
  101. DESKTOP_SETTINGS_LOAD(&desktop->settings);
  102. if(desktop->settings.favorite_primary.is_external) {
  103. LoaderStatus status = loader_start(
  104. desktop->loader,
  105. FAP_LOADER_APP_NAME,
  106. desktop->settings.favorite_primary.name_or_path);
  107. if(status != LoaderStatusOk) {
  108. FURI_LOG_E(TAG, "loader_start failed: %d", status);
  109. }
  110. } else {
  111. LoaderStatus status = loader_start(
  112. desktop->loader, desktop->settings.favorite_primary.name_or_path, NULL);
  113. if(status != LoaderStatusOk) {
  114. FURI_LOG_E(TAG, "loader_start failed: %d", status);
  115. }
  116. }
  117. consumed = true;
  118. break;
  119. case DesktopMainEventOpenFavoriteSecondary:
  120. DESKTOP_SETTINGS_LOAD(&desktop->settings);
  121. if(desktop->settings.favorite_secondary.is_external) {
  122. LoaderStatus status = loader_start(
  123. desktop->loader,
  124. FAP_LOADER_APP_NAME,
  125. desktop->settings.favorite_secondary.name_or_path);
  126. if(status != LoaderStatusOk) {
  127. FURI_LOG_E(TAG, "loader_start failed: %d", status);
  128. }
  129. } else {
  130. LoaderStatus status = loader_start(
  131. desktop->loader, desktop->settings.favorite_secondary.name_or_path, NULL);
  132. if(status != LoaderStatusOk) {
  133. FURI_LOG_E(TAG, "loader_start failed: %d", status);
  134. }
  135. }
  136. consumed = true;
  137. break;
  138. case DesktopAnimationEventCheckAnimation:
  139. animation_manager_check_blocking_process(desktop->animation_manager);
  140. consumed = true;
  141. break;
  142. case DesktopAnimationEventNewIdleAnimation:
  143. animation_manager_new_idle_process(desktop->animation_manager);
  144. consumed = true;
  145. break;
  146. case DesktopAnimationEventInteractAnimation:
  147. if(!animation_manager_interact_process(desktop->animation_manager)) {
  148. LoaderStatus status = loader_start(desktop->loader, "Passport", NULL);
  149. if(status != LoaderStatusOk) {
  150. FURI_LOG_E(TAG, "loader_start failed: %d", status);
  151. }
  152. }
  153. consumed = true;
  154. break;
  155. case DesktopMainEventOpenPassport: {
  156. LoaderStatus status = loader_start(desktop->loader, "Passport", NULL);
  157. if(status != LoaderStatusOk) {
  158. FURI_LOG_E(TAG, "loader_start failed: %d", status);
  159. }
  160. break;
  161. }
  162. case DesktopMainEventOpenGameMenu: {
  163. LoaderStatus status = loader_start(
  164. desktop->loader, FAP_LOADER_APP_NAME, EXT_PATH("/apps/Games/snake_game.fap"));
  165. if(status != LoaderStatusOk) {
  166. FURI_LOG_E(TAG, "loader_start failed: %d", status);
  167. }
  168. break;
  169. }
  170. case DesktopLockedEventUpdate:
  171. desktop_view_locked_update(desktop->locked_view);
  172. consumed = true;
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. return consumed;
  179. }
  180. void desktop_scene_main_on_exit(void* context) {
  181. Desktop* desktop = (Desktop*)context;
  182. animation_manager_set_new_idle_callback(desktop->animation_manager, NULL);
  183. animation_manager_set_check_callback(desktop->animation_manager, NULL);
  184. animation_manager_set_interact_callback(desktop->animation_manager, NULL);
  185. animation_manager_set_context(desktop->animation_manager, desktop);
  186. }