desktop_scene_main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_app_started_callback(const void* message, void* context) {
  13. furi_assert(context);
  14. Desktop* desktop = context;
  15. const LoaderEvent* event = message;
  16. if(event->type == LoaderEventTypeApplicationStarted) {
  17. view_dispatcher_send_custom_event(
  18. desktop->view_dispatcher, DesktopMainEventBeforeAppStarted);
  19. osSemaphoreAcquire(desktop->unload_animation_semaphore, osWaitForever);
  20. } else if(event->type == LoaderEventTypeApplicationStopped) {
  21. view_dispatcher_send_custom_event(
  22. desktop->view_dispatcher, DesktopMainEventAfterAppFinished);
  23. }
  24. }
  25. static void desktop_scene_main_new_idle_animation_callback(void* context) {
  26. furi_assert(context);
  27. Desktop* desktop = context;
  28. view_dispatcher_send_custom_event(
  29. desktop->view_dispatcher, DesktopAnimationEventNewIdleAnimation);
  30. }
  31. static void desktop_scene_main_check_animation_callback(void* context) {
  32. furi_assert(context);
  33. Desktop* desktop = context;
  34. view_dispatcher_send_custom_event(
  35. desktop->view_dispatcher, DesktopAnimationEventCheckAnimation);
  36. }
  37. static void desktop_scene_main_interact_animation_callback(void* context) {
  38. furi_assert(context);
  39. Desktop* desktop = context;
  40. view_dispatcher_send_custom_event(
  41. desktop->view_dispatcher, DesktopAnimationEventInteractAnimation);
  42. }
  43. #ifdef APP_ARCHIVE
  44. static void desktop_switch_to_app(Desktop* desktop, const FlipperApplication* flipper_app) {
  45. furi_assert(desktop);
  46. furi_assert(flipper_app);
  47. furi_assert(flipper_app->app);
  48. furi_assert(flipper_app->name);
  49. if(furi_thread_get_state(desktop->scene_thread) != FuriThreadStateStopped) {
  50. FURI_LOG_E("Desktop", "Thread is already running");
  51. return;
  52. }
  53. furi_thread_set_name(desktop->scene_thread, flipper_app->name);
  54. furi_thread_set_stack_size(desktop->scene_thread, flipper_app->stack_size);
  55. furi_thread_set_callback(desktop->scene_thread, flipper_app->app);
  56. furi_thread_start(desktop->scene_thread);
  57. }
  58. #endif
  59. void desktop_scene_main_callback(DesktopEvent event, void* context) {
  60. Desktop* desktop = (Desktop*)context;
  61. view_dispatcher_send_custom_event(desktop->view_dispatcher, event);
  62. }
  63. void desktop_scene_main_on_enter(void* context) {
  64. Desktop* desktop = (Desktop*)context;
  65. DesktopMainView* main_view = desktop->main_view;
  66. animation_manager_set_context(desktop->animation_manager, desktop);
  67. animation_manager_set_new_idle_callback(
  68. desktop->animation_manager, desktop_scene_main_new_idle_animation_callback);
  69. animation_manager_set_check_callback(
  70. desktop->animation_manager, desktop_scene_main_check_animation_callback);
  71. animation_manager_set_interact_callback(
  72. desktop->animation_manager, desktop_scene_main_interact_animation_callback);
  73. furi_assert(osSemaphoreGetCount(desktop->unload_animation_semaphore) == 0);
  74. Loader* loader = furi_record_open("loader");
  75. desktop->app_start_stop_subscription = furi_pubsub_subscribe(
  76. loader_get_pubsub(loader), desktop_scene_main_app_started_callback, desktop);
  77. furi_record_close("loader");
  78. desktop_main_set_callback(main_view, desktop_scene_main_callback, desktop);
  79. view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewIdMain);
  80. }
  81. bool desktop_scene_main_on_event(void* context, SceneManagerEvent event) {
  82. Desktop* desktop = (Desktop*)context;
  83. bool consumed = false;
  84. if(event.type == SceneManagerEventTypeCustom) {
  85. switch(event.event) {
  86. case DesktopMainEventOpenMenu:
  87. loader_show_menu();
  88. consumed = true;
  89. break;
  90. case DesktopMainEventOpenLockMenu:
  91. scene_manager_next_scene(desktop->scene_manager, DesktopSceneLockMenu);
  92. consumed = true;
  93. break;
  94. case DesktopMainEventOpenDebug:
  95. scene_manager_next_scene(desktop->scene_manager, DesktopSceneDebug);
  96. consumed = true;
  97. break;
  98. case DesktopMainEventOpenArchive:
  99. #ifdef APP_ARCHIVE
  100. desktop_switch_to_app(desktop, &FLIPPER_ARCHIVE);
  101. #endif
  102. consumed = true;
  103. break;
  104. case DesktopMainEventOpenFavorite:
  105. LOAD_DESKTOP_SETTINGS(&desktop->settings);
  106. if(desktop->settings.favorite < FLIPPER_APPS_COUNT) {
  107. Loader* loader = furi_record_open("loader");
  108. LoaderStatus status =
  109. loader_start(loader, FLIPPER_APPS[desktop->settings.favorite].name, NULL);
  110. if(status != LoaderStatusOk) {
  111. FURI_LOG_E(TAG, "loader_start failed: %d", status);
  112. }
  113. furi_record_close("loader");
  114. } else {
  115. FURI_LOG_E(TAG, "Can't find favorite application");
  116. }
  117. consumed = true;
  118. break;
  119. case DesktopAnimationEventCheckAnimation:
  120. animation_manager_check_blocking_process(desktop->animation_manager);
  121. consumed = true;
  122. break;
  123. case DesktopAnimationEventNewIdleAnimation:
  124. animation_manager_new_idle_process(desktop->animation_manager);
  125. consumed = true;
  126. break;
  127. case DesktopAnimationEventInteractAnimation:
  128. animation_manager_interact_process(desktop->animation_manager);
  129. consumed = true;
  130. break;
  131. case DesktopMainEventBeforeAppStarted:
  132. animation_manager_unload_and_stall_animation(desktop->animation_manager);
  133. osSemaphoreRelease(desktop->unload_animation_semaphore);
  134. consumed = true;
  135. break;
  136. case DesktopMainEventAfterAppFinished:
  137. animation_manager_load_and_continue_animation(desktop->animation_manager);
  138. consumed = true;
  139. break;
  140. case DesktopLockedEventUpdate:
  141. desktop_view_locked_update(desktop->locked_view);
  142. consumed = true;
  143. break;
  144. default:
  145. break;
  146. }
  147. }
  148. return consumed;
  149. }
  150. void desktop_scene_main_on_exit(void* context) {
  151. Desktop* desktop = (Desktop*)context;
  152. /**
  153. * We're allowed to leave this scene only when any other app & loader
  154. * is finished, that's why we can be sure there is no task waiting
  155. * for start/stop semaphore
  156. */
  157. Loader* loader = furi_record_open("loader");
  158. furi_pubsub_unsubscribe(loader_get_pubsub(loader), desktop->app_start_stop_subscription);
  159. furi_record_close("loader");
  160. furi_assert(osSemaphoreGetCount(desktop->unload_animation_semaphore) == 0);
  161. animation_manager_set_new_idle_callback(desktop->animation_manager, NULL);
  162. animation_manager_set_check_callback(desktop->animation_manager, NULL);
  163. animation_manager_set_interact_callback(desktop->animation_manager, NULL);
  164. animation_manager_set_context(desktop->animation_manager, desktop);
  165. }