desktop_scene_main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. furi_thread_join(desktop->scene_thread);
  58. }
  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. desktop->app_start_stop_subscription = furi_pubsub_subscribe(
  75. loader_get_pubsub(), desktop_scene_main_app_started_callback, desktop);
  76. desktop_main_set_callback(main_view, desktop_scene_main_callback, desktop);
  77. view_port_enabled_set(desktop->lock_viewport, false);
  78. if(scene_manager_get_scene_state(desktop->scene_manager, DesktopSceneMain) ==
  79. DesktopMainEventUnlocked) {
  80. desktop_main_unlocked(desktop->main_view);
  81. }
  82. view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewMain);
  83. }
  84. bool desktop_scene_main_on_event(void* context, SceneManagerEvent event) {
  85. Desktop* desktop = (Desktop*)context;
  86. bool consumed = false;
  87. if(event.type == SceneManagerEventTypeCustom) {
  88. switch(event.event) {
  89. case DesktopMainEventOpenMenu:
  90. loader_show_menu();
  91. consumed = true;
  92. break;
  93. case DesktopMainEventOpenLockMenu:
  94. scene_manager_next_scene(desktop->scene_manager, DesktopSceneLockMenu);
  95. consumed = true;
  96. break;
  97. case DesktopMainEventOpenDebug:
  98. scene_manager_next_scene(desktop->scene_manager, DesktopSceneDebug);
  99. consumed = true;
  100. break;
  101. case DesktopMainEventOpenArchive:
  102. #ifdef APP_ARCHIVE
  103. animation_manager_unload_and_stall_animation(desktop->animation_manager);
  104. desktop_switch_to_app(desktop, &FLIPPER_ARCHIVE);
  105. animation_manager_load_and_continue_animation(desktop->animation_manager);
  106. #endif
  107. consumed = true;
  108. break;
  109. case DesktopMainEventOpenFavorite:
  110. LOAD_DESKTOP_SETTINGS(&desktop->settings);
  111. animation_manager_unload_and_stall_animation(desktop->animation_manager);
  112. if(desktop->settings.favorite < FLIPPER_APPS_COUNT) {
  113. desktop_switch_to_app(desktop, &FLIPPER_APPS[desktop->settings.favorite]);
  114. } else {
  115. FURI_LOG_E("DesktopSrv", "Can't find favorite application");
  116. }
  117. animation_manager_load_and_continue_animation(desktop->animation_manager);
  118. consumed = true;
  119. break;
  120. case DesktopMainEventCheckAnimation:
  121. animation_manager_check_blocking_process(desktop->animation_manager);
  122. consumed = true;
  123. break;
  124. case DesktopMainEventNewIdleAnimation:
  125. animation_manager_new_idle_process(desktop->animation_manager);
  126. consumed = true;
  127. break;
  128. case DesktopMainEventInteractAnimation:
  129. animation_manager_interact_process(desktop->animation_manager);
  130. consumed = true;
  131. break;
  132. case DesktopMainEventBeforeAppStarted:
  133. animation_manager_unload_and_stall_animation(desktop->animation_manager);
  134. osSemaphoreRelease(desktop->unload_animation_semaphore);
  135. consumed = true;
  136. break;
  137. case DesktopMainEventAfterAppFinished:
  138. animation_manager_load_and_continue_animation(desktop->animation_manager);
  139. consumed = true;
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. return consumed;
  146. }
  147. void desktop_scene_main_on_exit(void* context) {
  148. Desktop* desktop = (Desktop*)context;
  149. /**
  150. * We're allowed to leave this scene only when any other app & loader
  151. * is finished, that's why we can be sure there is no task waiting
  152. * for start/stop semaphore
  153. */
  154. furi_pubsub_unsubscribe(loader_get_pubsub(), desktop->app_start_stop_subscription);
  155. furi_assert(osSemaphoreGetCount(desktop->unload_animation_semaphore) == 0);
  156. animation_manager_set_new_idle_callback(desktop->animation_manager, NULL);
  157. animation_manager_set_check_callback(desktop->animation_manager, NULL);
  158. animation_manager_set_interact_callback(desktop->animation_manager, NULL);
  159. animation_manager_set_context(desktop->animation_manager, desktop);
  160. scene_manager_set_scene_state(desktop->scene_manager, DesktopSceneMain, MAIN_VIEW_DEFAULT);
  161. desktop_main_reset_hint(desktop->main_view);
  162. }