desktop_scene_main.c 7.0 KB

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