desktop.c 7.0 KB

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