desktop.c 13 KB

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