hex_viewer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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->model = malloc(sizeof(HexViewerModel));
  21. memset(app->model, 0, sizeof(HexViewerModel));
  22. app->gui = furi_record_open(RECORD_GUI);
  23. app->storage = furi_record_open(RECORD_STORAGE);
  24. app->notification = furi_record_open(RECORD_NOTIFICATION);
  25. //Turn backlight on, believe me this makes testing your app easier
  26. notification_message(app->notification, &sequence_display_backlight_on);
  27. //Scene additions
  28. app->view_dispatcher = view_dispatcher_alloc();
  29. view_dispatcher_enable_queue(app->view_dispatcher);
  30. app->scene_manager = scene_manager_alloc(&hex_viewer_scene_handlers, app);
  31. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  32. view_dispatcher_set_navigation_event_callback(app->view_dispatcher, hex_viewer_navigation_event_callback);
  33. view_dispatcher_set_tick_event_callback(app->view_dispatcher, hex_viewer_tick_event_callback, 100);
  34. view_dispatcher_set_custom_event_callback(app->view_dispatcher, hex_viewer_custom_event_callback);
  35. app->submenu = submenu_alloc();
  36. // Set defaults, in case no config loaded
  37. app->haptic = 1;
  38. app->speaker = 1;
  39. app->led = 1;
  40. app->save_settings = 1;
  41. // Used for File Browser
  42. app->dialogs = furi_record_open(RECORD_DIALOGS);
  43. app->file_path = furi_string_alloc();
  44. // Load configs
  45. hex_viewer_read_settings(app);
  46. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdMenu, submenu_get_view(app->submenu));
  47. app->hex_viewer_startscreen = hex_viewer_startscreen_alloc();
  48. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdStartscreen, hex_viewer_startscreen_get_view(app->hex_viewer_startscreen));
  49. app->hex_viewer_scene_1 = hex_viewer_scene_1_alloc();
  50. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdScene1, hex_viewer_scene_1_get_view(app->hex_viewer_scene_1));
  51. app->hex_viewer_scene_2 = hex_viewer_scene_2_alloc();
  52. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdScene2, hex_viewer_scene_2_get_view(app->hex_viewer_scene_2));
  53. app->button_menu = button_menu_alloc();
  54. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdScene3, button_menu_get_view(app->button_menu));
  55. app->variable_item_list = variable_item_list_alloc();
  56. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdSettings, variable_item_list_get_view(app->variable_item_list));
  57. //End Scene Additions
  58. return app;
  59. }
  60. void hex_viewer_app_free(HexViewer* app) {
  61. furi_assert(app);
  62. if(app->model->stream) buffered_file_stream_close(app->model->stream);
  63. // Scene manager
  64. scene_manager_free(app->scene_manager);
  65. // View Dispatcher
  66. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdMenu);
  67. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdScene1);
  68. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdScene2);
  69. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdSettings);
  70. submenu_free(app->submenu);
  71. view_dispatcher_free(app->view_dispatcher);
  72. furi_record_close(RECORD_STORAGE);
  73. furi_record_close(RECORD_GUI);
  74. app->storage = NULL;
  75. app->gui = NULL;
  76. app->notification = NULL;
  77. // Close File Browser
  78. furi_record_close(RECORD_DIALOGS);
  79. furi_string_free(app->file_path);
  80. free(app->model);
  81. //Remove whatever is left
  82. free(app);
  83. }
  84. int32_t hex_viewer_app(void* p) {
  85. UNUSED(p);
  86. HexViewer* app = hex_viewer_app_alloc();
  87. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  88. scene_manager_next_scene(app->scene_manager, HexViewerSceneStartscreen);
  89. furi_hal_power_suppress_charge_enter();
  90. view_dispatcher_run(app->view_dispatcher);
  91. hex_viewer_save_settings(app);
  92. furi_hal_power_suppress_charge_exit();
  93. hex_viewer_app_free(app);
  94. return 0;
  95. }