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