desktop.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include <storage/storage.h>
  2. #include <assets_icons.h>
  3. #include <gui/gui.h>
  4. #include <gui/view_stack.h>
  5. #include <notification/notification.h>
  6. #include <notification/notification_messages.h>
  7. #include <furi.h>
  8. #include <furi_hal.h>
  9. #include "animations/animation_manager.h"
  10. #include "desktop/scenes/desktop_scene.h"
  11. #include "desktop/scenes/desktop_scene_i.h"
  12. #include "desktop/views/desktop_view_locked.h"
  13. #include "desktop/views/desktop_view_pin_input.h"
  14. #include "desktop/views/desktop_view_pin_timeout.h"
  15. #include "desktop_i.h"
  16. #include "desktop_helpers.h"
  17. static void desktop_auto_lock_arm(Desktop*);
  18. static void desktop_auto_lock_inhibit(Desktop*);
  19. static void desktop_start_auto_lock_timer(Desktop*);
  20. static void desktop_loader_callback(const void* message, void* context) {
  21. furi_assert(context);
  22. Desktop* desktop = context;
  23. const LoaderEvent* event = message;
  24. if(event->type == LoaderEventTypeApplicationStarted) {
  25. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopGlobalBeforeAppStarted);
  26. } else if(event->type == LoaderEventTypeApplicationStopped) {
  27. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopGlobalAfterAppFinished);
  28. }
  29. }
  30. static void desktop_lock_icon_callback(Canvas* canvas, void* context) {
  31. furi_assert(canvas);
  32. canvas_draw_icon(canvas, 0, 0, &I_Lock_8x8);
  33. }
  34. static bool desktop_custom_event_callback(void* context, uint32_t event) {
  35. furi_assert(context);
  36. Desktop* desktop = (Desktop*)context;
  37. switch(event) {
  38. case DesktopGlobalBeforeAppStarted:
  39. animation_manager_unload_and_stall_animation(desktop->animation_manager);
  40. desktop_auto_lock_inhibit(desktop);
  41. return true;
  42. case DesktopGlobalAfterAppFinished:
  43. animation_manager_load_and_continue_animation(desktop->animation_manager);
  44. // TODO: Implement a message mechanism for loading settings and (optionally)
  45. // locking and unlocking
  46. LOAD_DESKTOP_SETTINGS(&desktop->settings);
  47. desktop_auto_lock_arm(desktop);
  48. return true;
  49. case DesktopGlobalAutoLock:
  50. if(!loader_is_locked(desktop->loader)) {
  51. desktop_lock(desktop);
  52. }
  53. return true;
  54. }
  55. return scene_manager_handle_custom_event(desktop->scene_manager, event);
  56. }
  57. static bool desktop_back_event_callback(void* context) {
  58. furi_assert(context);
  59. Desktop* desktop = (Desktop*)context;
  60. return scene_manager_handle_back_event(desktop->scene_manager);
  61. }
  62. static void desktop_tick_event_callback(void* context) {
  63. furi_assert(context);
  64. Desktop* app = context;
  65. scene_manager_handle_tick_event(app->scene_manager);
  66. }
  67. static void desktop_input_event_callback(const void* value, void* context) {
  68. furi_assert(value);
  69. furi_assert(context);
  70. const InputEvent* event = value;
  71. Desktop* desktop = context;
  72. if(event->type == InputTypePress) {
  73. desktop_start_auto_lock_timer(desktop);
  74. }
  75. }
  76. static void desktop_auto_lock_timer_callback(void* context) {
  77. furi_assert(context);
  78. Desktop* desktop = context;
  79. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopGlobalAutoLock);
  80. }
  81. static void desktop_start_auto_lock_timer(Desktop* desktop) {
  82. osTimerStart(
  83. desktop->auto_lock_timer, furi_hal_ms_to_ticks(desktop->settings.auto_lock_delay_ms));
  84. }
  85. static void desktop_stop_auto_lock_timer(Desktop* desktop) {
  86. osTimerStop(desktop->auto_lock_timer);
  87. }
  88. static void desktop_auto_lock_arm(Desktop* desktop) {
  89. if(desktop->settings.auto_lock_delay_ms) {
  90. desktop->input_events_subscription = furi_pubsub_subscribe(
  91. desktop->input_events_pubsub, desktop_input_event_callback, desktop);
  92. desktop_start_auto_lock_timer(desktop);
  93. }
  94. }
  95. static void desktop_auto_lock_inhibit(Desktop* desktop) {
  96. desktop_stop_auto_lock_timer(desktop);
  97. if(desktop->input_events_subscription) {
  98. furi_pubsub_unsubscribe(desktop->input_events_pubsub, desktop->input_events_subscription);
  99. desktop->input_events_subscription = NULL;
  100. }
  101. }
  102. void desktop_lock(Desktop* desktop) {
  103. desktop_auto_lock_inhibit(desktop);
  104. scene_manager_set_scene_state(
  105. desktop->scene_manager, DesktopSceneLocked, SCENE_LOCKED_FIRST_ENTER);
  106. scene_manager_next_scene(desktop->scene_manager, DesktopSceneLocked);
  107. notification_message(desktop->notification, &sequence_display_off_delay_1000);
  108. }
  109. void desktop_unlock(Desktop* desktop) {
  110. furi_hal_rtc_set_pin_fails(0);
  111. desktop_helpers_unlock_system(desktop);
  112. desktop_view_locked_unlock(desktop->locked_view);
  113. scene_manager_search_and_switch_to_previous_scene(desktop->scene_manager, DesktopSceneMain);
  114. desktop_auto_lock_arm(desktop);
  115. }
  116. Desktop* desktop_alloc() {
  117. Desktop* desktop = malloc(sizeof(Desktop));
  118. desktop->animation_manager = animation_manager_alloc();
  119. desktop->gui = furi_record_open("gui");
  120. desktop->scene_thread = furi_thread_alloc();
  121. desktop->view_dispatcher = view_dispatcher_alloc();
  122. desktop->scene_manager = scene_manager_alloc(&desktop_scene_handlers, desktop);
  123. view_dispatcher_enable_queue(desktop->view_dispatcher);
  124. view_dispatcher_attach_to_gui(
  125. desktop->view_dispatcher, desktop->gui, ViewDispatcherTypeDesktop);
  126. view_dispatcher_set_tick_event_callback(
  127. desktop->view_dispatcher, desktop_tick_event_callback, 500);
  128. view_dispatcher_set_event_callback_context(desktop->view_dispatcher, desktop);
  129. view_dispatcher_set_custom_event_callback(
  130. desktop->view_dispatcher, desktop_custom_event_callback);
  131. view_dispatcher_set_navigation_event_callback(
  132. desktop->view_dispatcher, desktop_back_event_callback);
  133. desktop->lock_menu = desktop_lock_menu_alloc();
  134. desktop->debug_view = desktop_debug_alloc();
  135. desktop->first_start_view = desktop_first_start_alloc();
  136. desktop->hw_mismatch_popup = popup_alloc();
  137. desktop->locked_view = desktop_view_locked_alloc();
  138. desktop->pin_input_view = desktop_view_pin_input_alloc();
  139. desktop->pin_timeout_view = desktop_view_pin_timeout_alloc();
  140. desktop->main_view_stack = view_stack_alloc();
  141. desktop->main_view = desktop_main_alloc();
  142. View* dolphin_view = animation_manager_get_animation_view(desktop->animation_manager);
  143. view_stack_add_view(desktop->main_view_stack, desktop_main_get_view(desktop->main_view));
  144. view_stack_add_view(desktop->main_view_stack, dolphin_view);
  145. view_stack_add_view(
  146. desktop->main_view_stack, desktop_view_locked_get_view(desktop->locked_view));
  147. /* locked view (as animation view) attends in 2 scenes: main & locked,
  148. * because it has to draw "Unlocked" label on main scene */
  149. desktop->locked_view_stack = view_stack_alloc();
  150. view_stack_add_view(desktop->locked_view_stack, dolphin_view);
  151. view_stack_add_view(
  152. desktop->locked_view_stack, desktop_view_locked_get_view(desktop->locked_view));
  153. view_dispatcher_add_view(
  154. desktop->view_dispatcher,
  155. DesktopViewIdMain,
  156. view_stack_get_view(desktop->main_view_stack));
  157. view_dispatcher_add_view(
  158. desktop->view_dispatcher,
  159. DesktopViewIdLocked,
  160. view_stack_get_view(desktop->locked_view_stack));
  161. view_dispatcher_add_view(
  162. desktop->view_dispatcher,
  163. DesktopViewIdLockMenu,
  164. desktop_lock_menu_get_view(desktop->lock_menu));
  165. view_dispatcher_add_view(
  166. desktop->view_dispatcher, DesktopViewIdDebug, desktop_debug_get_view(desktop->debug_view));
  167. view_dispatcher_add_view(
  168. desktop->view_dispatcher,
  169. DesktopViewIdFirstStart,
  170. desktop_first_start_get_view(desktop->first_start_view));
  171. view_dispatcher_add_view(
  172. desktop->view_dispatcher,
  173. DesktopViewIdHwMismatch,
  174. popup_get_view(desktop->hw_mismatch_popup));
  175. view_dispatcher_add_view(
  176. desktop->view_dispatcher,
  177. DesktopViewIdPinTimeout,
  178. desktop_view_pin_timeout_get_view(desktop->pin_timeout_view));
  179. view_dispatcher_add_view(
  180. desktop->view_dispatcher,
  181. DesktopViewIdPinInput,
  182. desktop_view_pin_input_get_view(desktop->pin_input_view));
  183. // Lock icon
  184. desktop->lock_viewport = view_port_alloc();
  185. view_port_set_width(desktop->lock_viewport, icon_get_width(&I_Lock_8x8));
  186. view_port_draw_callback_set(desktop->lock_viewport, desktop_lock_icon_callback, desktop);
  187. view_port_enabled_set(desktop->lock_viewport, false);
  188. gui_add_view_port(desktop->gui, desktop->lock_viewport, GuiLayerStatusBarLeft);
  189. // Special case: autostart application is already running
  190. desktop->loader = furi_record_open("loader");
  191. if(loader_is_locked(desktop->loader) &&
  192. animation_manager_is_animation_loaded(desktop->animation_manager)) {
  193. animation_manager_unload_and_stall_animation(desktop->animation_manager);
  194. }
  195. desktop->notification = furi_record_open("notification");
  196. desktop->app_start_stop_subscription = furi_pubsub_subscribe(
  197. loader_get_pubsub(desktop->loader), desktop_loader_callback, desktop);
  198. desktop->input_events_pubsub = furi_record_open("input_events");
  199. desktop->input_events_subscription = NULL;
  200. desktop->auto_lock_timer =
  201. osTimerNew(desktop_auto_lock_timer_callback, osTimerOnce, desktop, NULL);
  202. return desktop;
  203. }
  204. void desktop_free(Desktop* desktop) {
  205. furi_assert(desktop);
  206. furi_pubsub_unsubscribe(
  207. loader_get_pubsub(desktop->loader), desktop->app_start_stop_subscription);
  208. if(desktop->input_events_subscription) {
  209. furi_pubsub_unsubscribe(desktop->input_events_pubsub, desktop->input_events_subscription);
  210. desktop->input_events_subscription = NULL;
  211. }
  212. desktop->loader = NULL;
  213. desktop->input_events_pubsub = NULL;
  214. furi_record_close("loader");
  215. furi_record_close("notification");
  216. furi_record_close("input_events");
  217. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdMain);
  218. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdLockMenu);
  219. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdLocked);
  220. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdDebug);
  221. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdFirstStart);
  222. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdHwMismatch);
  223. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdPinInput);
  224. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdPinTimeout);
  225. view_dispatcher_free(desktop->view_dispatcher);
  226. scene_manager_free(desktop->scene_manager);
  227. animation_manager_free(desktop->animation_manager);
  228. view_stack_free(desktop->main_view_stack);
  229. desktop_main_free(desktop->main_view);
  230. view_stack_free(desktop->locked_view_stack);
  231. desktop_view_locked_free(desktop->locked_view);
  232. desktop_lock_menu_free(desktop->lock_menu);
  233. desktop_view_locked_free(desktop->locked_view);
  234. desktop_debug_free(desktop->debug_view);
  235. desktop_first_start_free(desktop->first_start_view);
  236. popup_free(desktop->hw_mismatch_popup);
  237. desktop_view_pin_timeout_free(desktop->pin_timeout_view);
  238. furi_record_close("gui");
  239. desktop->gui = NULL;
  240. furi_thread_free(desktop->scene_thread);
  241. furi_record_close("menu");
  242. osTimerDelete(desktop->auto_lock_timer);
  243. free(desktop);
  244. }
  245. static bool desktop_is_first_start() {
  246. Storage* storage = furi_record_open("storage");
  247. bool exists = storage_common_stat(storage, "/int/first_start", NULL) == FSE_OK;
  248. furi_record_close("storage");
  249. return exists;
  250. }
  251. int32_t desktop_srv(void* p) {
  252. Desktop* desktop = desktop_alloc();
  253. bool loaded = LOAD_DESKTOP_SETTINGS(&desktop->settings);
  254. if(!loaded) {
  255. furi_hal_rtc_reset_flag(FuriHalRtcFlagLock);
  256. memset(&desktop->settings, 0, sizeof(desktop->settings));
  257. SAVE_DESKTOP_SETTINGS(&desktop->settings);
  258. }
  259. scene_manager_next_scene(desktop->scene_manager, DesktopSceneMain);
  260. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock) && !desktop->settings.pin_code.length) {
  261. furi_hal_rtc_reset_flag(FuriHalRtcFlagLock);
  262. }
  263. if(!furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock)) {
  264. if(!loader_is_locked(desktop->loader)) {
  265. desktop_auto_lock_arm(desktop);
  266. }
  267. } else {
  268. desktop_lock(desktop);
  269. }
  270. if(desktop_is_first_start()) {
  271. scene_manager_next_scene(desktop->scene_manager, DesktopSceneFirstStart);
  272. }
  273. if(!furi_hal_version_do_i_belong_here()) {
  274. scene_manager_next_scene(desktop->scene_manager, DesktopSceneHwMismatch);
  275. }
  276. if(furi_hal_rtc_get_fault_data()) {
  277. scene_manager_next_scene(desktop->scene_manager, DesktopSceneFault);
  278. }
  279. view_dispatcher_run(desktop->view_dispatcher);
  280. desktop_free(desktop);
  281. return 0;
  282. }