hex_viewer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "hex_viewer.h"
  2. bool hex_viewer_custom_event_callback(void* context, uint32_t event) {
  3. furi_assert(context);
  4. HexViewer* app = context;
  5. return scene_manager_handle_custom_event(app->scene_manager, event);
  6. }
  7. void hex_viewer_tick_event_callback(void* context) {
  8. furi_assert(context);
  9. HexViewer* app = context;
  10. scene_manager_handle_tick_event(app->scene_manager);
  11. }
  12. //leave app if back button pressed
  13. bool hex_viewer_navigation_event_callback(void* context) {
  14. furi_assert(context);
  15. HexViewer* app = context;
  16. return scene_manager_handle_back_event(app->scene_manager);
  17. }
  18. HexViewer* hex_viewer_app_alloc() {
  19. HexViewer* app = malloc(sizeof(HexViewer));
  20. app->gui = furi_record_open(RECORD_GUI);
  21. app->storage = furi_record_open(RECORD_STORAGE);
  22. app->notification = furi_record_open(RECORD_NOTIFICATION);
  23. //Turn backlight on, believe me this makes testing your app easier
  24. notification_message(app->notification, &sequence_display_backlight_on);
  25. //Scene additions
  26. app->view_dispatcher = view_dispatcher_alloc();
  27. view_dispatcher_enable_queue(app->view_dispatcher);
  28. app->scene_manager = scene_manager_alloc(&hex_viewer_scene_handlers, app);
  29. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  30. view_dispatcher_set_navigation_event_callback(app->view_dispatcher, hex_viewer_navigation_event_callback);
  31. view_dispatcher_set_tick_event_callback(app->view_dispatcher, hex_viewer_tick_event_callback, 100);
  32. view_dispatcher_set_custom_event_callback(app->view_dispatcher, hex_viewer_custom_event_callback);
  33. app->submenu = submenu_alloc();
  34. // Set defaults, in case no config loaded
  35. app->haptic = 1;
  36. app->speaker = 1;
  37. app->led = 1;
  38. app->save_settings = 1;
  39. // Used for File Browser
  40. app->dialogs = furi_record_open(RECORD_DIALOGS);
  41. app->file_path = furi_string_alloc();
  42. // Load configs
  43. hex_viewer_read_settings(app);
  44. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdMenu, submenu_get_view(app->submenu));
  45. app->hex_viewer_startscreen = hex_viewer_startscreen_alloc();
  46. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdStartscreen, hex_viewer_startscreen_get_view(app->hex_viewer_startscreen));
  47. app->hex_viewer_scene_1 = hex_viewer_scene_1_alloc();
  48. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdScene1, hex_viewer_scene_1_get_view(app->hex_viewer_scene_1));
  49. app->hex_viewer_scene_2 = hex_viewer_scene_2_alloc();
  50. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdScene2, hex_viewer_scene_2_get_view(app->hex_viewer_scene_2));
  51. app->button_menu = button_menu_alloc();
  52. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdScene3, button_menu_get_view(app->button_menu));
  53. app->variable_item_list = variable_item_list_alloc();
  54. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdSettings, variable_item_list_get_view(app->variable_item_list));
  55. //End Scene Additions
  56. return app;
  57. }
  58. void hex_viewer_app_free(HexViewer* app) {
  59. furi_assert(app);
  60. if(app->model->stream) buffered_file_stream_close(app->model->stream);
  61. // Scene manager
  62. scene_manager_free(app->scene_manager);
  63. // View Dispatcher
  64. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdMenu);
  65. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdScene1);
  66. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdScene2);
  67. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdSettings);
  68. submenu_free(app->submenu);
  69. view_dispatcher_free(app->view_dispatcher);
  70. furi_record_close(RECORD_STORAGE);
  71. furi_record_close(RECORD_GUI);
  72. app->storage = NULL;
  73. app->gui = NULL;
  74. app->notification = NULL;
  75. // Close File Browser
  76. furi_record_close(RECORD_DIALOGS);
  77. furi_string_free(app->file_path);
  78. //Remove whatever is left
  79. free(app);
  80. }
  81. int32_t hex_viewer_app(void* p) {
  82. UNUSED(p);
  83. HexViewer* app = hex_viewer_app_alloc();
  84. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  85. scene_manager_next_scene(app->scene_manager, HexViewerSceneStartscreen); //Start with start screen
  86. //scene_manager_next_scene(app->scene_manager, HexViewerSceneMenu); //if you want to directly start with Menu
  87. furi_hal_power_suppress_charge_enter();
  88. view_dispatcher_run(app->view_dispatcher);
  89. hex_viewer_save_settings(app);
  90. furi_hal_power_suppress_charge_exit();
  91. hex_viewer_app_free(app);
  92. return 0;
  93. }