hex_viewer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. app->scene_manager = scene_manager_alloc(&hex_viewer_scene_handlers, app);
  30. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  31. view_dispatcher_set_navigation_event_callback(
  32. app->view_dispatcher, hex_viewer_navigation_event_callback);
  33. view_dispatcher_set_tick_event_callback(
  34. app->view_dispatcher, hex_viewer_tick_event_callback, 100);
  35. view_dispatcher_set_custom_event_callback(
  36. app->view_dispatcher, hex_viewer_custom_event_callback);
  37. // Set defaults, in case no config loaded
  38. app->haptic = 1;
  39. app->speaker = 1;
  40. app->led = 1;
  41. app->save_settings = 1;
  42. // Used for File Browser
  43. app->dialogs = furi_record_open(RECORD_DIALOGS);
  44. app->file_path = furi_string_alloc();
  45. // Load configs
  46. hex_viewer_read_settings(app);
  47. app->submenu = submenu_alloc();
  48. view_dispatcher_add_view(
  49. app->view_dispatcher, HexViewerViewIdMenu, submenu_get_view(app->submenu));
  50. app->hex_viewer_startscreen = hex_viewer_startscreen_alloc();
  51. view_dispatcher_add_view(
  52. app->view_dispatcher,
  53. HexViewerViewIdStartscreen,
  54. hex_viewer_startscreen_get_view(app->hex_viewer_startscreen));
  55. app->text_input = text_input_alloc();
  56. view_dispatcher_add_view(
  57. app->view_dispatcher, HexViewerViewIdScroll, text_input_get_view(app->text_input));
  58. app->variable_item_list = variable_item_list_alloc();
  59. view_dispatcher_add_view(
  60. app->view_dispatcher,
  61. HexViewerViewIdSettings,
  62. variable_item_list_get_view(app->variable_item_list));
  63. //End Scene Additions
  64. return app;
  65. }
  66. void hex_viewer_app_free(HexViewer* app) {
  67. furi_assert(app);
  68. if(app->model->stream) {
  69. buffered_file_stream_close(app->model->stream);
  70. stream_free(app->model->stream);
  71. }
  72. // Scene manager
  73. scene_manager_free(app->scene_manager);
  74. // View Dispatcher
  75. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdMenu);
  76. submenu_free(app->submenu);
  77. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdStartscreen);
  78. hex_viewer_startscreen_free(app->hex_viewer_startscreen);
  79. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdScroll);
  80. text_input_free(app->text_input);
  81. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdSettings);
  82. variable_item_list_free(app->variable_item_list);
  83. view_dispatcher_free(app->view_dispatcher);
  84. furi_record_close(RECORD_STORAGE);
  85. furi_record_close(RECORD_GUI);
  86. app->storage = NULL;
  87. app->gui = NULL;
  88. app->notification = NULL;
  89. // Close File Browser
  90. furi_record_close(RECORD_DIALOGS);
  91. furi_string_free(app->file_path);
  92. free(app->model);
  93. //Remove whatever is left
  94. free(app);
  95. }
  96. int32_t hex_viewer_app(void* p) {
  97. UNUSED(p);
  98. HexViewer* app = hex_viewer_app_alloc();
  99. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  100. if(p && strlen(p) && hex_viewer_open_file(app, (const char*)p)) {
  101. hex_viewer_read_file(app);
  102. scene_manager_next_scene(app->scene_manager, HexViewerSceneStartscreen);
  103. } else {
  104. scene_manager_next_scene(app->scene_manager, HexViewerSceneStartscreen);
  105. scene_manager_next_scene(app->scene_manager, HexViewerSceneOpen);
  106. }
  107. furi_hal_power_suppress_charge_enter();
  108. view_dispatcher_run(app->view_dispatcher);
  109. hex_viewer_save_settings(app);
  110. furi_hal_power_suppress_charge_exit();
  111. hex_viewer_app_free(app);
  112. return 0;
  113. }