desktop_scene_main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "../desktop_i.h"
  2. #include "../views/desktop_main.h"
  3. #include "applications.h"
  4. #include "assets_icons.h"
  5. #include "cmsis_os2.h"
  6. #include "desktop/desktop.h"
  7. #include "desktop/views/desktop_events.h"
  8. #include "dolphin/dolphin.h"
  9. #include "furi/pubsub.h"
  10. #include "furi/record.h"
  11. #include "furi/thread.h"
  12. #include "storage/storage_glue.h"
  13. #include <loader/loader.h>
  14. #include <m-list.h>
  15. #define MAIN_VIEW_DEFAULT (0UL)
  16. static void desktop_scene_main_app_started_callback(const void* message, void* context) {
  17. furi_assert(context);
  18. Desktop* desktop = context;
  19. const LoaderEvent* event = message;
  20. if(event->type == LoaderEventTypeApplicationStarted) {
  21. view_dispatcher_send_custom_event(
  22. desktop->view_dispatcher, DesktopMainEventBeforeAppStarted);
  23. osSemaphoreAcquire(desktop->unload_animation_semaphore, osWaitForever);
  24. } else if(event->type == LoaderEventTypeApplicationStopped) {
  25. view_dispatcher_send_custom_event(
  26. desktop->view_dispatcher, DesktopMainEventAfterAppFinished);
  27. }
  28. }
  29. static void desktop_scene_main_new_idle_animation_callback(void* context) {
  30. furi_assert(context);
  31. Desktop* desktop = context;
  32. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopMainEventNewIdleAnimation);
  33. }
  34. static void desktop_scene_main_check_animation_callback(void* context) {
  35. furi_assert(context);
  36. Desktop* desktop = context;
  37. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopMainEventCheckAnimation);
  38. }
  39. static void desktop_scene_main_interact_animation_callback(void* context) {
  40. furi_assert(context);
  41. Desktop* desktop = context;
  42. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopMainEventInteractAnimation);
  43. }
  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. void desktop_scene_main_callback(DesktopEvent event, void* context) {
  59. Desktop* desktop = (Desktop*)context;
  60. view_dispatcher_send_custom_event(desktop->view_dispatcher, event);
  61. }
  62. void desktop_scene_main_on_enter(void* context) {
  63. Desktop* desktop = (Desktop*)context;
  64. DesktopMainView* main_view = desktop->main_view;
  65. animation_manager_set_context(desktop->animation_manager, desktop);
  66. animation_manager_set_new_idle_callback(
  67. desktop->animation_manager, desktop_scene_main_new_idle_animation_callback);
  68. animation_manager_set_check_callback(
  69. desktop->animation_manager, desktop_scene_main_check_animation_callback);
  70. animation_manager_set_interact_callback(
  71. desktop->animation_manager, desktop_scene_main_interact_animation_callback);
  72. furi_assert(osSemaphoreGetCount(desktop->unload_animation_semaphore) == 0);
  73. desktop->app_start_stop_subscription = furi_pubsub_subscribe(
  74. loader_get_pubsub(), desktop_scene_main_app_started_callback, desktop);
  75. desktop_main_set_callback(main_view, desktop_scene_main_callback, desktop);
  76. view_port_enabled_set(desktop->lock_viewport, false);
  77. if(scene_manager_get_scene_state(desktop->scene_manager, DesktopSceneMain) ==
  78. DesktopMainEventUnlocked) {
  79. desktop_main_unlocked(desktop->main_view);
  80. }
  81. view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewMain);
  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. animation_manager_unload_and_stall_animation(desktop->animation_manager);
  103. desktop_switch_to_app(desktop, &FLIPPER_ARCHIVE);
  104. animation_manager_load_and_continue_animation(desktop->animation_manager);
  105. #endif
  106. consumed = true;
  107. break;
  108. case DesktopMainEventOpenFavorite:
  109. LOAD_DESKTOP_SETTINGS(&desktop->settings);
  110. animation_manager_unload_and_stall_animation(desktop->animation_manager);
  111. if(desktop->settings.favorite < FLIPPER_APPS_COUNT) {
  112. desktop_switch_to_app(desktop, &FLIPPER_APPS[desktop->settings.favorite]);
  113. } else {
  114. FURI_LOG_E("DesktopSrv", "Can't find favorite application");
  115. }
  116. animation_manager_load_and_continue_animation(desktop->animation_manager);
  117. consumed = true;
  118. break;
  119. case DesktopMainEventCheckAnimation:
  120. animation_manager_check_blocking_process(desktop->animation_manager);
  121. consumed = true;
  122. break;
  123. case DesktopMainEventNewIdleAnimation:
  124. animation_manager_new_idle_process(desktop->animation_manager);
  125. consumed = true;
  126. break;
  127. case DesktopMainEventInteractAnimation:
  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. default:
  141. break;
  142. }
  143. }
  144. return consumed;
  145. }
  146. void desktop_scene_main_on_exit(void* context) {
  147. Desktop* desktop = (Desktop*)context;
  148. /**
  149. * We're allowed to leave this scene only when any other app & loader
  150. * is finished, that's why we can be sure there is no task waiting
  151. * for start/stop semaphore
  152. */
  153. furi_pubsub_unsubscribe(loader_get_pubsub(), desktop->app_start_stop_subscription);
  154. furi_assert(osSemaphoreGetCount(desktop->unload_animation_semaphore) == 0);
  155. animation_manager_set_new_idle_callback(desktop->animation_manager, NULL);
  156. animation_manager_set_check_callback(desktop->animation_manager, NULL);
  157. animation_manager_set_interact_callback(desktop->animation_manager, NULL);
  158. animation_manager_set_context(desktop->animation_manager, desktop);
  159. scene_manager_set_scene_state(desktop->scene_manager, DesktopSceneMain, MAIN_VIEW_DEFAULT);
  160. desktop_main_reset_hint(desktop->main_view);
  161. }