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 "helpers/pin_lock.h"
  17. #include "helpers/slideshow_filename.h"
  18. static void desktop_auto_lock_arm(Desktop*);
  19. static void desktop_auto_lock_inhibit(Desktop*);
  20. static void desktop_start_auto_lock_timer(Desktop*);
  21. static void desktop_loader_callback(const void* message, void* context) {
  22. furi_assert(context);
  23. Desktop* desktop = context;
  24. const LoaderEvent* event = message;
  25. if(event->type == LoaderEventTypeApplicationStarted) {
  26. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopGlobalBeforeAppStarted);
  27. } else if(event->type == LoaderEventTypeApplicationStopped) {
  28. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopGlobalAfterAppFinished);
  29. }
  30. }
  31. static void desktop_lock_icon_callback(Canvas* canvas, void* context) {
  32. UNUSED(context);
  33. furi_assert(canvas);
  34. canvas_draw_icon(canvas, 0, 0, &I_Lock_8x8);
  35. }
  36. static bool desktop_custom_event_callback(void* context, uint32_t event) {
  37. furi_assert(context);
  38. Desktop* desktop = (Desktop*)context;
  39. switch(event) {
  40. case DesktopGlobalBeforeAppStarted:
  41. animation_manager_unload_and_stall_animation(desktop->animation_manager);
  42. desktop_auto_lock_inhibit(desktop);
  43. return true;
  44. case DesktopGlobalAfterAppFinished:
  45. animation_manager_load_and_continue_animation(desktop->animation_manager);
  46. // TODO: Implement a message mechanism for loading settings and (optionally)
  47. // locking and unlocking
  48. LOAD_DESKTOP_SETTINGS(&desktop->settings);
  49. desktop_auto_lock_arm(desktop);
  50. return true;
  51. case DesktopGlobalAutoLock:
  52. if(!loader_is_locked(desktop->loader)) {
  53. desktop_lock(desktop);
  54. }
  55. return true;
  56. }
  57. return scene_manager_handle_custom_event(desktop->scene_manager, event);
  58. }
  59. static bool desktop_back_event_callback(void* context) {
  60. furi_assert(context);
  61. Desktop* desktop = (Desktop*)context;
  62. return scene_manager_handle_back_event(desktop->scene_manager);
  63. }
  64. static void desktop_tick_event_callback(void* context) {
  65. furi_assert(context);
  66. Desktop* app = context;
  67. scene_manager_handle_tick_event(app->scene_manager);
  68. }
  69. static void desktop_input_event_callback(const void* value, void* context) {
  70. furi_assert(value);
  71. furi_assert(context);
  72. const InputEvent* event = value;
  73. Desktop* desktop = context;
  74. if(event->type == InputTypePress) {
  75. desktop_start_auto_lock_timer(desktop);
  76. }
  77. }
  78. static void desktop_auto_lock_timer_callback(void* context) {
  79. furi_assert(context);
  80. Desktop* desktop = context;
  81. view_dispatcher_send_custom_event(desktop->view_dispatcher, DesktopGlobalAutoLock);
  82. }
  83. static void desktop_start_auto_lock_timer(Desktop* desktop) {
  84. furi_timer_start(
  85. desktop->auto_lock_timer, furi_ms_to_ticks(desktop->settings.auto_lock_delay_ms));
  86. }
  87. static void desktop_stop_auto_lock_timer(Desktop* desktop) {
  88. furi_timer_stop(desktop->auto_lock_timer);
  89. }
  90. static void desktop_auto_lock_arm(Desktop* desktop) {
  91. if(desktop->settings.auto_lock_delay_ms) {
  92. desktop->input_events_subscription = furi_pubsub_subscribe(
  93. desktop->input_events_pubsub, desktop_input_event_callback, desktop);
  94. desktop_start_auto_lock_timer(desktop);
  95. }
  96. }
  97. static void desktop_auto_lock_inhibit(Desktop* desktop) {
  98. desktop_stop_auto_lock_timer(desktop);
  99. if(desktop->input_events_subscription) {
  100. furi_pubsub_unsubscribe(desktop->input_events_pubsub, desktop->input_events_subscription);
  101. desktop->input_events_subscription = NULL;
  102. }
  103. }
  104. void desktop_lock(Desktop* desktop) {
  105. desktop_auto_lock_inhibit(desktop);
  106. scene_manager_set_scene_state(
  107. desktop->scene_manager, DesktopSceneLocked, SCENE_LOCKED_FIRST_ENTER);
  108. scene_manager_next_scene(desktop->scene_manager, DesktopSceneLocked);
  109. notification_message(desktop->notification, &sequence_display_backlight_off_delay_1000);
  110. }
  111. void desktop_unlock(Desktop* desktop) {
  112. view_port_enabled_set(desktop->lock_viewport, false);
  113. Gui* gui = furi_record_open(RECORD_GUI);
  114. gui_set_lockdown(gui, false);
  115. furi_record_close(RECORD_GUI);
  116. desktop_view_locked_unlock(desktop->locked_view);
  117. scene_manager_search_and_switch_to_previous_scene(desktop->scene_manager, DesktopSceneMain);
  118. desktop_auto_lock_arm(desktop);
  119. }
  120. Desktop* desktop_alloc() {
  121. Desktop* desktop = malloc(sizeof(Desktop));
  122. desktop->animation_manager = animation_manager_alloc();
  123. desktop->gui = furi_record_open(RECORD_GUI);
  124. desktop->scene_thread = furi_thread_alloc();
  125. desktop->view_dispatcher = view_dispatcher_alloc();
  126. desktop->scene_manager = scene_manager_alloc(&desktop_scene_handlers, desktop);
  127. view_dispatcher_enable_queue(desktop->view_dispatcher);
  128. view_dispatcher_attach_to_gui(
  129. desktop->view_dispatcher, desktop->gui, ViewDispatcherTypeDesktop);
  130. view_dispatcher_set_tick_event_callback(
  131. desktop->view_dispatcher, desktop_tick_event_callback, 500);
  132. view_dispatcher_set_event_callback_context(desktop->view_dispatcher, desktop);
  133. view_dispatcher_set_custom_event_callback(
  134. desktop->view_dispatcher, desktop_custom_event_callback);
  135. view_dispatcher_set_navigation_event_callback(
  136. desktop->view_dispatcher, desktop_back_event_callback);
  137. desktop->lock_menu = desktop_lock_menu_alloc();
  138. desktop->debug_view = desktop_debug_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->slideshow_view = desktop_view_slideshow_alloc();
  144. desktop->main_view_stack = view_stack_alloc();
  145. desktop->main_view = desktop_main_alloc();
  146. View* dolphin_view = animation_manager_get_animation_view(desktop->animation_manager);
  147. view_stack_add_view(desktop->main_view_stack, desktop_main_get_view(desktop->main_view));
  148. view_stack_add_view(desktop->main_view_stack, dolphin_view);
  149. view_stack_add_view(
  150. desktop->main_view_stack, desktop_view_locked_get_view(desktop->locked_view));
  151. /* locked view (as animation view) attends in 2 scenes: main & locked,
  152. * because it has to draw "Unlocked" label on main scene */
  153. desktop->locked_view_stack = view_stack_alloc();
  154. view_stack_add_view(desktop->locked_view_stack, dolphin_view);
  155. view_stack_add_view(
  156. desktop->locked_view_stack, desktop_view_locked_get_view(desktop->locked_view));
  157. view_dispatcher_add_view(
  158. desktop->view_dispatcher,
  159. DesktopViewIdMain,
  160. view_stack_get_view(desktop->main_view_stack));
  161. view_dispatcher_add_view(
  162. desktop->view_dispatcher,
  163. DesktopViewIdLocked,
  164. view_stack_get_view(desktop->locked_view_stack));
  165. view_dispatcher_add_view(
  166. desktop->view_dispatcher,
  167. DesktopViewIdLockMenu,
  168. desktop_lock_menu_get_view(desktop->lock_menu));
  169. view_dispatcher_add_view(
  170. desktop->view_dispatcher, DesktopViewIdDebug, desktop_debug_get_view(desktop->debug_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. view_dispatcher_add_view(
  184. desktop->view_dispatcher,
  185. DesktopViewIdSlideshow,
  186. desktop_view_slideshow_get_view(desktop->slideshow_view));
  187. // Lock icon
  188. desktop->lock_viewport = view_port_alloc();
  189. view_port_set_width(desktop->lock_viewport, icon_get_width(&I_Lock_8x8));
  190. view_port_draw_callback_set(desktop->lock_viewport, desktop_lock_icon_callback, desktop);
  191. view_port_enabled_set(desktop->lock_viewport, false);
  192. gui_add_view_port(desktop->gui, desktop->lock_viewport, GuiLayerStatusBarLeft);
  193. // Special case: autostart application is already running
  194. desktop->loader = furi_record_open(RECORD_LOADER);
  195. if(loader_is_locked(desktop->loader) &&
  196. animation_manager_is_animation_loaded(desktop->animation_manager)) {
  197. animation_manager_unload_and_stall_animation(desktop->animation_manager);
  198. }
  199. desktop->notification = furi_record_open(RECORD_NOTIFICATION);
  200. desktop->app_start_stop_subscription = furi_pubsub_subscribe(
  201. loader_get_pubsub(desktop->loader), desktop_loader_callback, desktop);
  202. desktop->input_events_pubsub = furi_record_open(RECORD_INPUT_EVENTS);
  203. desktop->input_events_subscription = NULL;
  204. desktop->auto_lock_timer =
  205. furi_timer_alloc(desktop_auto_lock_timer_callback, FuriTimerTypeOnce, desktop);
  206. return desktop;
  207. }
  208. void desktop_free(Desktop* desktop) {
  209. furi_assert(desktop);
  210. furi_pubsub_unsubscribe(
  211. loader_get_pubsub(desktop->loader), desktop->app_start_stop_subscription);
  212. if(desktop->input_events_subscription) {
  213. furi_pubsub_unsubscribe(desktop->input_events_pubsub, desktop->input_events_subscription);
  214. desktop->input_events_subscription = NULL;
  215. }
  216. desktop->loader = NULL;
  217. desktop->input_events_pubsub = NULL;
  218. furi_record_close(RECORD_LOADER);
  219. furi_record_close(RECORD_NOTIFICATION);
  220. furi_record_close(RECORD_INPUT_EVENTS);
  221. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdMain);
  222. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdLockMenu);
  223. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdLocked);
  224. view_dispatcher_remove_view(desktop->view_dispatcher, DesktopViewIdDebug);
  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. popup_free(desktop->hw_mismatch_popup);
  239. desktop_view_pin_timeout_free(desktop->pin_timeout_view);
  240. furi_record_close(RECORD_GUI);
  241. desktop->gui = NULL;
  242. furi_thread_free(desktop->scene_thread);
  243. furi_record_close("menu");
  244. furi_timer_free(desktop->auto_lock_timer);
  245. free(desktop);
  246. }
  247. static bool desktop_check_file_flag(const char* flag_path) {
  248. Storage* storage = furi_record_open(RECORD_STORAGE);
  249. bool exists = storage_common_stat(storage, flag_path, NULL) == FSE_OK;
  250. furi_record_close(RECORD_STORAGE);
  251. return exists;
  252. }
  253. int32_t desktop_srv(void* p) {
  254. UNUSED(p);
  255. Desktop* desktop = desktop_alloc();
  256. bool loaded = LOAD_DESKTOP_SETTINGS(&desktop->settings);
  257. if(!loaded) {
  258. memset(&desktop->settings, 0, sizeof(desktop->settings));
  259. SAVE_DESKTOP_SETTINGS(&desktop->settings);
  260. }
  261. scene_manager_next_scene(desktop->scene_manager, DesktopSceneMain);
  262. desktop_pin_lock_init(&desktop->settings);
  263. if(!desktop_pin_lock_is_locked()) {
  264. if(!loader_is_locked(desktop->loader)) {
  265. desktop_auto_lock_arm(desktop);
  266. }
  267. } else {
  268. desktop_lock(desktop);
  269. }
  270. if(desktop_check_file_flag(SLIDESHOW_FS_PATH)) {
  271. scene_manager_next_scene(desktop->scene_manager, DesktopSceneSlideshow);
  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. }