desktop_scene_main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "../desktop_i.h"
  2. #include "../views/desktop_main.h"
  3. #include "applications.h"
  4. #include "assets_icons.h"
  5. #include "dolphin/dolphin.h"
  6. #include "furi/pubsub.h"
  7. #include "furi/record.h"
  8. #include "storage/storage-glue.h"
  9. #include <loader/loader.h>
  10. #include <m-list.h>
  11. #define MAIN_VIEW_DEFAULT (0UL)
  12. static void desktop_switch_to_app(Desktop* desktop, const FlipperApplication* flipper_app) {
  13. furi_assert(desktop);
  14. furi_assert(flipper_app);
  15. furi_assert(flipper_app->app);
  16. furi_assert(flipper_app->name);
  17. if(furi_thread_get_state(desktop->scene_thread) != FuriThreadStateStopped) {
  18. FURI_LOG_E("Desktop", "Thread is already running");
  19. return;
  20. }
  21. furi_thread_set_name(desktop->scene_thread, flipper_app->name);
  22. furi_thread_set_stack_size(desktop->scene_thread, flipper_app->stack_size);
  23. furi_thread_set_callback(desktop->scene_thread, flipper_app->app);
  24. furi_thread_start(desktop->scene_thread);
  25. }
  26. void desktop_scene_main_callback(DesktopMainEvent event, void* context) {
  27. Desktop* desktop = (Desktop*)context;
  28. view_dispatcher_send_custom_event(desktop->view_dispatcher, event);
  29. }
  30. static void desktop_scene_main_animation_changed_callback(void* context) {
  31. furi_assert(context);
  32. Desktop* desktop = context;
  33. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopMainEventUpdateAnimation);
  34. }
  35. void desktop_scene_main_on_enter(void* context) {
  36. Desktop* desktop = (Desktop*)context;
  37. DesktopMainView* main_view = desktop->main_view;
  38. desktop_main_set_callback(main_view, desktop_scene_main_callback, desktop);
  39. view_port_enabled_set(desktop->lock_viewport, false);
  40. if(scene_manager_get_scene_state(desktop->scene_manager, DesktopSceneMain) ==
  41. DesktopMainEventUnlocked) {
  42. desktop_main_unlocked(desktop->main_view);
  43. }
  44. desktop_animation_activate(desktop->animation);
  45. desktop_animation_set_animation_changed_callback(
  46. desktop->animation, desktop_scene_main_animation_changed_callback, desktop);
  47. bool status_bar_background_black = false;
  48. const Icon* icon =
  49. desktop_animation_get_animation(desktop->animation, &status_bar_background_black);
  50. desktop_main_switch_dolphin_animation(desktop->main_view, icon, status_bar_background_black);
  51. view_dispatcher_switch_to_view(desktop->view_dispatcher, DesktopViewMain);
  52. }
  53. bool desktop_scene_main_on_event(void* context, SceneManagerEvent event) {
  54. Desktop* desktop = (Desktop*)context;
  55. bool consumed = false;
  56. if(event.type == SceneManagerEventTypeCustom) {
  57. switch(event.event) {
  58. case DesktopMainEventOpenMenu:
  59. loader_show_menu();
  60. consumed = true;
  61. break;
  62. case DesktopMainEventOpenLockMenu:
  63. scene_manager_next_scene(desktop->scene_manager, DesktopSceneLockMenu);
  64. consumed = true;
  65. break;
  66. case DesktopMainEventOpenDebug:
  67. scene_manager_next_scene(desktop->scene_manager, DesktopSceneDebug);
  68. consumed = true;
  69. break;
  70. case DesktopMainEventOpenArchive:
  71. #ifdef APP_ARCHIVE
  72. desktop_switch_to_app(desktop, &FLIPPER_ARCHIVE);
  73. #endif
  74. consumed = true;
  75. break;
  76. case DesktopMainEventOpenFavorite:
  77. LOAD_DESKTOP_SETTINGS(&desktop->settings);
  78. if(desktop->settings.favorite) {
  79. desktop_switch_to_app(desktop, &FLIPPER_APPS[desktop->settings.favorite]);
  80. } else {
  81. FURI_LOG_E("DesktopSrv", "Can't find favorite application");
  82. }
  83. consumed = true;
  84. break;
  85. case DesktopMainEventUpdateAnimation: {
  86. bool status_bar_background_black = false;
  87. const Icon* icon =
  88. desktop_animation_get_animation(desktop->animation, &status_bar_background_black);
  89. desktop_main_switch_dolphin_animation(
  90. desktop->main_view, icon, status_bar_background_black);
  91. consumed = true;
  92. break;
  93. }
  94. case DesktopMainEventRightShort: {
  95. DesktopAnimationState state = desktop_animation_handle_right(desktop->animation);
  96. if(state == DesktopAnimationStateLevelUpIsPending) {
  97. scene_manager_next_scene(desktop->scene_manager, DesktopSceneLevelUp);
  98. }
  99. break;
  100. }
  101. default:
  102. break;
  103. }
  104. if(event.event != DesktopMainEventUpdateAnimation) {
  105. desktop_animation_activate(desktop->animation);
  106. }
  107. } else if(event.type != SceneManagerEventTypeTick) {
  108. desktop_animation_activate(desktop->animation);
  109. }
  110. return consumed;
  111. }
  112. void desktop_scene_main_on_exit(void* context) {
  113. Desktop* desktop = (Desktop*)context;
  114. desktop_animation_set_animation_changed_callback(desktop->animation, NULL, NULL);
  115. scene_manager_set_scene_state(desktop->scene_manager, DesktopSceneMain, MAIN_VIEW_DEFAULT);
  116. desktop_main_reset_hint(desktop->main_view);
  117. }