desktop_scene_main.c 7.2 KB

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