desktop.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "assets_icons.h"
  2. #include "cmsis_os2.h"
  3. #include "desktop/desktop.h"
  4. #include "desktop_i.h"
  5. #include <dolphin/dolphin.h>
  6. #include <furi/pubsub.h>
  7. #include <furi/record.h>
  8. #include "portmacro.h"
  9. #include "storage/filesystem-api-defines.h"
  10. #include "storage/storage.h"
  11. #include <stdint.h>
  12. #include <power/power_service/power.h>
  13. #include "helpers/desktop_animation.h"
  14. static void desktop_lock_icon_callback(Canvas* canvas, void* context) {
  15. furi_assert(canvas);
  16. canvas_draw_icon(canvas, 0, 0, &I_Lock_8x8);
  17. }
  18. bool desktop_custom_event_callback(void* context, uint32_t event) {
  19. furi_assert(context);
  20. Desktop* desktop = (Desktop*)context;
  21. return scene_manager_handle_custom_event(desktop->scene_manager, event);
  22. }
  23. bool desktop_back_event_callback(void* context) {
  24. furi_assert(context);
  25. Desktop* desktop = (Desktop*)context;
  26. return scene_manager_handle_back_event(desktop->scene_manager);
  27. }
  28. Desktop* desktop_alloc() {
  29. Desktop* desktop = furi_alloc(sizeof(Desktop));
  30. desktop->gui = furi_record_open("gui");
  31. desktop->scene_thread = furi_thread_alloc();
  32. desktop->view_dispatcher = view_dispatcher_alloc();
  33. desktop->scene_manager = scene_manager_alloc(&desktop_scene_handlers, desktop);
  34. desktop->animation = desktop_animation_alloc();
  35. view_dispatcher_enable_queue(desktop->view_dispatcher);
  36. view_dispatcher_attach_to_gui(
  37. desktop->view_dispatcher, desktop->gui, ViewDispatcherTypeDesktop);
  38. view_dispatcher_set_event_callback_context(desktop->view_dispatcher, desktop);
  39. view_dispatcher_set_custom_event_callback(
  40. desktop->view_dispatcher, desktop_custom_event_callback);
  41. view_dispatcher_set_navigation_event_callback(
  42. desktop->view_dispatcher, desktop_back_event_callback);
  43. desktop->main_view = desktop_main_alloc();
  44. desktop->lock_menu = desktop_lock_menu_alloc();
  45. desktop->locked_view = desktop_locked_alloc();
  46. desktop->debug_view = desktop_debug_alloc();
  47. desktop->first_start_view = desktop_first_start_alloc();
  48. desktop->hw_mismatch_popup = popup_alloc();
  49. desktop->code_input = code_input_alloc();
  50. view_dispatcher_add_view(
  51. desktop->view_dispatcher, DesktopViewMain, desktop_main_get_view(desktop->main_view));
  52. view_dispatcher_add_view(
  53. desktop->view_dispatcher,
  54. DesktopViewLockMenu,
  55. desktop_lock_menu_get_view(desktop->lock_menu));
  56. view_dispatcher_add_view(
  57. desktop->view_dispatcher, DesktopViewDebug, desktop_debug_get_view(desktop->debug_view));
  58. view_dispatcher_add_view(
  59. desktop->view_dispatcher,
  60. DesktopViewLocked,
  61. desktop_locked_get_view(desktop->locked_view));
  62. view_dispatcher_add_view(
  63. desktop->view_dispatcher,
  64. DesktopViewFirstStart,
  65. desktop_first_start_get_view(desktop->first_start_view));
  66. view_dispatcher_add_view(
  67. desktop->view_dispatcher,
  68. DesktopViewHwMismatch,
  69. popup_get_view(desktop->hw_mismatch_popup));
  70. view_dispatcher_add_view(
  71. desktop->view_dispatcher, DesktopViewPinSetup, code_input_get_view(desktop->code_input));
  72. // Lock icon
  73. desktop->lock_viewport = view_port_alloc();
  74. view_port_set_width(desktop->lock_viewport, icon_get_width(&I_Lock_8x8));
  75. view_port_draw_callback_set(desktop->lock_viewport, desktop_lock_icon_callback, desktop);
  76. view_port_enabled_set(desktop->lock_viewport, false);
  77. gui_add_view_port(desktop->gui, desktop->lock_viewport, GuiLayerStatusBarLeft);
  78. return desktop;
  79. }
  80. void desktop_free(Desktop* desktop) {
  81. furi_assert(desktop);
  82. desktop_animation_free(desktop->animation);
  83. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewMain);
  84. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewLockMenu);
  85. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewLocked);
  86. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewDebug);
  87. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewFirstStart);
  88. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewHwMismatch);
  89. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewPinSetup);
  90. view_dispatcher_free(desktop->view_dispatcher);
  91. scene_manager_free(desktop->scene_manager);
  92. desktop_main_free(desktop->main_view);
  93. desktop_lock_menu_free(desktop->lock_menu);
  94. desktop_locked_free(desktop->locked_view);
  95. desktop_debug_free(desktop->debug_view);
  96. desktop_first_start_free(desktop->first_start_view);
  97. popup_free(desktop->hw_mismatch_popup);
  98. code_input_free(desktop->code_input);
  99. furi_record_close("gui");
  100. desktop->gui = NULL;
  101. furi_thread_free(desktop->scene_thread);
  102. furi_record_close("menu");
  103. free(desktop);
  104. }
  105. static bool desktop_is_first_start() {
  106. Storage* storage = furi_record_open("storage");
  107. bool exists = storage_common_stat(storage, "/int/first_start", NULL) == FSE_OK;
  108. furi_record_close("storage");
  109. return exists;
  110. }
  111. static void desktop_dolphin_state_changed_callback(const void* message, void* context) {
  112. Desktop* desktop = context;
  113. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopMainEventUpdateAnimation);
  114. }
  115. static void desktop_storage_state_changed_callback(const void* message, void* context) {
  116. Desktop* desktop = context;
  117. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopMainEventUpdateAnimation);
  118. }
  119. int32_t desktop_srv(void* p) {
  120. Desktop* desktop = desktop_alloc();
  121. Dolphin* dolphin = furi_record_open("dolphin");
  122. FuriPubSub* dolphin_pubsub = dolphin_get_pubsub(dolphin);
  123. FuriPubSubSubscription* dolphin_subscription =
  124. furi_pubsub_subscribe(dolphin_pubsub, desktop_dolphin_state_changed_callback, desktop);
  125. Storage* storage = furi_record_open("storage");
  126. FuriPubSub* storage_pubsub = storage_get_pubsub(storage);
  127. FuriPubSubSubscription* storage_subscription =
  128. furi_pubsub_subscribe(storage_pubsub, desktop_storage_state_changed_callback, desktop);
  129. bool loaded = LOAD_DESKTOP_SETTINGS(&desktop->settings);
  130. if(!loaded) {
  131. furi_hal_rtc_reset_flag(FuriHalRtcFlagLock);
  132. memset(&desktop->settings, 0, sizeof(desktop->settings));
  133. SAVE_DESKTOP_SETTINGS(&desktop->settings);
  134. }
  135. scene_manager_next_scene(desktop->scene_manager, DesktopSceneMain);
  136. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock)) {
  137. furi_hal_usb_disable();
  138. scene_manager_set_scene_state(
  139. desktop->scene_manager, DesktopSceneLocked, DesktopLockedWithPin);
  140. scene_manager_next_scene(desktop->scene_manager, DesktopSceneLocked);
  141. }
  142. if(desktop_is_first_start()) {
  143. scene_manager_next_scene(desktop->scene_manager, DesktopSceneFirstStart);
  144. }
  145. if(!furi_hal_version_do_i_belong_here()) {
  146. scene_manager_next_scene(desktop->scene_manager, DesktopSceneHwMismatch);
  147. }
  148. view_dispatcher_run(desktop->view_dispatcher);
  149. furi_pubsub_unsubscribe(dolphin_pubsub, dolphin_subscription);
  150. furi_pubsub_unsubscribe(storage_pubsub, storage_subscription);
  151. desktop_free(desktop);
  152. return 0;
  153. }