Просмотр исходного кода

cleanup free callback (this saved 2k bytes lol)

jblanked 9 месяцев назад
Родитель
Сommit
e07202eef4
8 измененных файлов с 280 добавлено и 283 удалено
  1. 19 55
      alloc/alloc.c
  2. 4 2
      alloc/alloc.h
  3. 5 221
      callback/callback.c
  4. 0 3
      callback/callback.h
  5. 191 0
      callback/free.c
  6. 9 0
      callback/free.h
  7. 50 0
      callback/loader.c
  8. 2 2
      callback/loader.h

+ 19 - 55
alloc/alloc.c

@@ -1,74 +1,38 @@
 #include <alloc/alloc.h>
 #include <callback/callback.h>
 #include <callback/loader.h>
+#include <callback/free.h>
 
-/**
- * @brief Navigation callback for exiting the application
- * @param context The context - unused
- * @return next view id (VIEW_NONE to exit the app)
- */
-static uint32_t callback_exit_app(void *context)
+uint32_t callback_exit_app(void *context)
 {
     UNUSED(context);
-    return VIEW_NONE; // Return VIEW_NONE to exit the app
+    return VIEW_NONE;
 }
 
-void *global_app;
-void flip_world_show_submenu()
+uint32_t callback_to_submenu(void *context)
 {
-    FlipWorldApp *app = (FlipWorldApp *)global_app;
-    if (app->submenu)
-    {
-        view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
-    }
+    UNUSED(context);
+    return FlipWorldViewSubmenu;
 }
 
-bool alloc_view_loader(void *context)
+uint32_t callback_to_wifi_settings(void *context)
 {
-    FlipWorldApp *app = (FlipWorldApp *)context;
-    furi_check(app, "FlipWorldApp is NULL");
-    if (app->view_loader)
-    {
-        FURI_LOG_E(TAG, "View loader already allocated");
-        return false;
-    }
-    if (app->widget_result)
-    {
-        FURI_LOG_E(TAG, "Widget result already allocated");
-        return false;
-    }
-
-    view_dispatcher_set_custom_event_callback(app->view_dispatcher, loader_custom_event_callback);
-
-    if (!easy_flipper_set_view(&app->view_loader, FlipWorldViewLoader, loader_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app))
-    {
-        return false;
-    }
-
-    loader_init(app->view_loader);
-
-    return easy_flipper_set_widget(&app->widget_result, FlipWorldViewWidgetResult, "", callback_to_submenu, &app->view_dispatcher);
+    UNUSED(context);
+    return FlipWorldViewVariableItemList;
 }
-
-void free_view_loader(void *context)
+uint32_t callback_to_settings(void *context)
 {
-    FlipWorldApp *app = (FlipWorldApp *)context;
-    furi_check(app, "FlipWorldApp is NULL");
-    // Free Widget(s)
-    if (app->widget_result)
-    {
-        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewWidgetResult);
-        widget_free(app->widget_result);
-        app->widget_result = NULL;
-    }
+    UNUSED(context);
+    return FlipWorldViewSubmenuOther;
+}
 
-    // Free View(s)
-    if (app->view_loader)
+void *global_app;
+void flip_world_show_submenu()
+{
+    FlipWorldApp *app = (FlipWorldApp *)global_app;
+    if (app->submenu)
     {
-        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewLoader);
-        loader_free_model(app->view_loader);
-        view_free(app->view_loader);
-        app->view_loader = NULL;
+        view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
     }
 }
 

+ 4 - 2
alloc/alloc.h

@@ -3,5 +3,7 @@
 FlipWorldApp *flip_world_app_alloc();
 void flip_world_app_free(FlipWorldApp *app);
 void flip_world_show_submenu();
-bool alloc_view_loader(void *context);
-void free_view_loader(void *context);
+uint32_t callback_exit_app(void *context);
+uint32_t callback_to_submenu(void *context);
+uint32_t callback_to_wifi_settings(void *context);
+uint32_t callback_to_settings(void *context);

+ 5 - 221
callback/callback.c

@@ -1,5 +1,6 @@
 #include <callback/callback.h>
 #include <callback/loader.h>
+#include <callback/free.h>
 #include "engine/engine.h"
 #include "engine/game_engine.h"
 #include "engine/game_manager_i.h"
@@ -7,6 +8,7 @@
 #include "engine/entity_i.h"
 #include "game/storage.h"
 #include "alloc/alloc.h"
+#include <flip_storage/storage.h>
 
 static void frame_cb(GameEngine *engine, Canvas *canvas, InputState input, void *context)
 {
@@ -125,22 +127,6 @@ static bool message_input_callback(InputEvent *event, void *context)
     }
     return true;
 }
-uint32_t callback_to_submenu(void *context)
-{
-    UNUSED(context);
-    return FlipWorldViewSubmenu;
-}
-
-static uint32_t callback_to_wifi_settings(void *context)
-{
-    UNUSED(context);
-    return FlipWorldViewVariableItemList;
-}
-static uint32_t callback_to_settings(void *context)
-{
-    UNUSED(context);
-    return FlipWorldViewSubmenuOther;
-}
 static int32_t waiting_app_callback(void *p)
 {
     FlipWorldApp *app = (FlipWorldApp *)p;
@@ -616,159 +602,6 @@ static bool alloc_game_submenu(void *context)
     return true;
 }
 
-void free_game_submenu(void *context)
-{
-    FlipWorldApp *app = (FlipWorldApp *)context;
-    if (!app)
-    {
-        FURI_LOG_E(TAG, "FlipWorldApp is NULL");
-        return;
-    }
-    if (app->submenu_game)
-    {
-        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewGameSubmenu);
-        submenu_free(app->submenu_game);
-        app->submenu_game = NULL;
-    }
-}
-
-static void free_submenu_other(void *context)
-{
-    FlipWorldApp *app = (FlipWorldApp *)context;
-    furi_check(app);
-    if (app->submenu_other)
-    {
-        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
-        submenu_free(app->submenu_other);
-        app->submenu_other = NULL;
-    }
-    for (int i = 0; i < 10; i++)
-    {
-        if (lobby_list[i])
-        {
-            free(lobby_list[i]);
-            lobby_list[i] = NULL;
-        }
-    }
-}
-// free
-static void free_message_view(void *context)
-{
-    FlipWorldApp *app = (FlipWorldApp *)context;
-    if (!app)
-    {
-        FURI_LOG_E(TAG, "FlipWorldApp is NULL");
-        return;
-    }
-    if (app->view_message)
-    {
-        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMessage);
-        view_free(app->view_message);
-        app->view_message = NULL;
-    }
-}
-
-static void free_text_input_view(void *context)
-{
-    FlipWorldApp *app = (FlipWorldApp *)context;
-    if (!app)
-    {
-        FURI_LOG_E(TAG, "FlipWorldApp is NULL");
-        return;
-    }
-    if (app->text_input)
-    {
-        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
-        uart_text_input_free(app->text_input);
-        app->text_input = NULL;
-    }
-    if (app->text_input_buffer)
-    {
-        free(app->text_input_buffer);
-        app->text_input_buffer = NULL;
-    }
-    if (app->text_input_temp_buffer)
-    {
-        free(app->text_input_temp_buffer);
-        app->text_input_temp_buffer = NULL;
-    }
-}
-static void free_variable_item_list(void *context)
-{
-    FlipWorldApp *app = (FlipWorldApp *)context;
-    if (!app)
-    {
-        FURI_LOG_E(TAG, "FlipWorldApp is NULL");
-        return;
-    }
-    if (app->variable_item_list)
-    {
-        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
-        variable_item_list_free(app->variable_item_list);
-        app->variable_item_list = NULL;
-    }
-    if (app->variable_item_wifi_ssid)
-    {
-        free(app->variable_item_wifi_ssid);
-        app->variable_item_wifi_ssid = NULL;
-    }
-    if (app->variable_item_wifi_pass)
-    {
-        free(app->variable_item_wifi_pass);
-        app->variable_item_wifi_pass = NULL;
-    }
-    if (app->variable_item_game_fps)
-    {
-        free(app->variable_item_game_fps);
-        app->variable_item_game_fps = NULL;
-    }
-    if (app->variable_item_game_screen_always_on)
-    {
-        free(app->variable_item_game_screen_always_on);
-        app->variable_item_game_screen_always_on = NULL;
-    }
-    if (app->variable_item_game_download_world)
-    {
-        free(app->variable_item_game_download_world);
-        app->variable_item_game_download_world = NULL;
-    }
-    if (app->variable_item_game_sound_on)
-    {
-        free(app->variable_item_game_sound_on);
-        app->variable_item_game_sound_on = NULL;
-    }
-    if (app->variable_item_game_vibration_on)
-    {
-        free(app->variable_item_game_vibration_on);
-        app->variable_item_game_vibration_on = NULL;
-    }
-    if (app->variable_item_game_player_sprite)
-    {
-        free(app->variable_item_game_player_sprite);
-        app->variable_item_game_player_sprite = NULL;
-    }
-    if (app->variable_item_game_vgm_x)
-    {
-        free(app->variable_item_game_vgm_x);
-        app->variable_item_game_vgm_x = NULL;
-    }
-    if (app->variable_item_game_vgm_y)
-    {
-        free(app->variable_item_game_vgm_y);
-        app->variable_item_game_vgm_y = NULL;
-    }
-    if (app->variable_item_user_username)
-    {
-        free(app->variable_item_user_username);
-        app->variable_item_user_username = NULL;
-    }
-    if (app->variable_item_user_password)
-    {
-        free(app->variable_item_user_password);
-        app->variable_item_user_password = NULL;
-    }
-}
-
 static FuriThread *game_thread;
 static FuriThread *waiting_thread;
 static bool game_thread_running = false;
@@ -801,56 +634,7 @@ static bool start_waiting_thread(void *context)
     waiting_thread_running = true;
     return true;
 }
-void free_all_views(void *context, bool free_variable_list, bool free_settings_other, bool free_submenu_game)
-{
-    FlipWorldApp *app = (FlipWorldApp *)context;
-    furi_check(app);
-    if (free_variable_list)
-    {
-        free_variable_item_list(app);
-    }
-    free_message_view(app);
-    free_text_input_view(app);
-
-    // free game thread
-    if (game_thread_running)
-    {
-        game_thread_running = false;
-        if (game_thread)
-        {
-            furi_thread_flags_set(furi_thread_get_id(game_thread), WorkerEvtStop);
-            furi_thread_join(game_thread);
-            furi_thread_free(game_thread);
-            game_thread = NULL;
-        }
-    }
-
-    if (free_settings_other)
-    {
-        free_submenu_other(app);
-    }
-
-    // free Derek's loader
-    free_view_loader(app);
-
-    if (free_submenu_game)
-    {
-        free_game_submenu(app);
-    }
 
-    // free waiting thread
-    if (waiting_thread_running)
-    {
-        waiting_thread_running = false;
-        if (waiting_thread)
-        {
-            furi_thread_flags_set(furi_thread_get_id(waiting_thread), WorkerEvtStop);
-            furi_thread_join(waiting_thread);
-            furi_thread_free(waiting_thread);
-            waiting_thread = NULL;
-        }
-    }
-}
 static bool fetch_world_list(FlipperHTTP *fhttp)
 {
     if (!fhttp)
@@ -979,7 +763,7 @@ static bool start_game_thread(void *context)
     free_variable_item_list(app);
     free_text_input_view(app);
     // free_submenu_other(app); // free lobby list or settings
-    free_view_loader(app);
+    loader_view_free(app);
     free_game_submenu(app);
 
     // free game thread
@@ -1294,7 +1078,7 @@ static char *_parse_game(DataLoaderModel *model)
 }
 static void switch_to_view_get_game(FlipWorldApp *app)
 {
-    if (!alloc_view_loader(app))
+    if (!loader_view_alloc(app))
     {
         FURI_LOG_E(TAG, "Failed to allocate view loader");
         return;
@@ -2334,7 +2118,7 @@ static char *_parse_worlds(DataLoaderModel *model)
 }
 static void switch_to_view_get_worlds(FlipWorldApp *app)
 {
-    if (!alloc_view_loader(app))
+    if (!loader_view_alloc(app))
     {
         FURI_LOG_E(TAG, "Failed to allocate view loader");
         return;

+ 0 - 3
callback/callback.h

@@ -1,10 +1,7 @@
 #pragma once
 #include <flip_world.h>
-#include <flip_storage/storage.h>
 
-void free_all_views(void *context, bool free_variable_list, bool free_settings_submenu, bool free_submenu_game);
 void callback_submenu_choices(void *context, uint32_t index);
-uint32_t callback_to_submenu(void *context);
 
 typedef enum MessageState MessageState;
 enum MessageState

+ 191 - 0
callback/free.c

@@ -0,0 +1,191 @@
+#include "callback/free.h"
+#include "callback/loader.h"
+
+void free_game_submenu(void *context)
+{
+    FlipWorldApp *app = (FlipWorldApp *)context;
+    furi_check(app);
+    if (app->submenu_game)
+    {
+        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewGameSubmenu);
+        submenu_free(app->submenu_game);
+        app->submenu_game = NULL;
+    }
+}
+
+void free_submenu_other(void *context)
+{
+    FlipWorldApp *app = (FlipWorldApp *)context;
+    furi_check(app);
+    if (app->submenu_other)
+    {
+        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
+        submenu_free(app->submenu_other);
+        app->submenu_other = NULL;
+    }
+    // for (int i = 0; i < 10; i++)
+    // {
+    //     if (lobby_list[i])
+    //     {
+    //         free(lobby_list[i]);
+    //         lobby_list[i] = NULL;
+    //     }
+    // }
+}
+
+void free_message_view(void *context)
+{
+    FlipWorldApp *app = (FlipWorldApp *)context;
+    furi_check(app);
+    if (app->view_message)
+    {
+        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMessage);
+        view_free(app->view_message);
+        app->view_message = NULL;
+    }
+}
+
+void free_text_input_view(void *context)
+{
+    FlipWorldApp *app = (FlipWorldApp *)context;
+    furi_check(app);
+    if (app->text_input)
+    {
+        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
+        uart_text_input_free(app->text_input);
+        app->text_input = NULL;
+    }
+    if (app->text_input_buffer)
+    {
+        free(app->text_input_buffer);
+        app->text_input_buffer = NULL;
+    }
+    if (app->text_input_temp_buffer)
+    {
+        free(app->text_input_temp_buffer);
+        app->text_input_temp_buffer = NULL;
+    }
+}
+
+void free_variable_item_list(void *context)
+{
+    FlipWorldApp *app = (FlipWorldApp *)context;
+    furi_check(app);
+    if (app->variable_item_list)
+    {
+        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
+        variable_item_list_free(app->variable_item_list);
+        app->variable_item_list = NULL;
+    }
+    if (app->variable_item_wifi_ssid)
+    {
+        free(app->variable_item_wifi_ssid);
+        app->variable_item_wifi_ssid = NULL;
+    }
+    if (app->variable_item_wifi_pass)
+    {
+        free(app->variable_item_wifi_pass);
+        app->variable_item_wifi_pass = NULL;
+    }
+    if (app->variable_item_game_fps)
+    {
+        free(app->variable_item_game_fps);
+        app->variable_item_game_fps = NULL;
+    }
+    if (app->variable_item_game_screen_always_on)
+    {
+        free(app->variable_item_game_screen_always_on);
+        app->variable_item_game_screen_always_on = NULL;
+    }
+    if (app->variable_item_game_download_world)
+    {
+        free(app->variable_item_game_download_world);
+        app->variable_item_game_download_world = NULL;
+    }
+    if (app->variable_item_game_sound_on)
+    {
+        free(app->variable_item_game_sound_on);
+        app->variable_item_game_sound_on = NULL;
+    }
+    if (app->variable_item_game_vibration_on)
+    {
+        free(app->variable_item_game_vibration_on);
+        app->variable_item_game_vibration_on = NULL;
+    }
+    if (app->variable_item_game_player_sprite)
+    {
+        free(app->variable_item_game_player_sprite);
+        app->variable_item_game_player_sprite = NULL;
+    }
+    if (app->variable_item_game_vgm_x)
+    {
+        free(app->variable_item_game_vgm_x);
+        app->variable_item_game_vgm_x = NULL;
+    }
+    if (app->variable_item_game_vgm_y)
+    {
+        free(app->variable_item_game_vgm_y);
+        app->variable_item_game_vgm_y = NULL;
+    }
+    if (app->variable_item_user_username)
+    {
+        free(app->variable_item_user_username);
+        app->variable_item_user_username = NULL;
+    }
+    if (app->variable_item_user_password)
+    {
+        free(app->variable_item_user_password);
+        app->variable_item_user_password = NULL;
+    }
+}
+
+void free_all_views(void *context, bool free_variable_list, bool free_settings_other, bool free_submenu_game)
+{
+    FlipWorldApp *app = (FlipWorldApp *)context;
+    furi_check(app);
+    if (free_variable_list)
+    {
+        free_variable_item_list(app);
+    }
+    free_message_view(app);
+    free_text_input_view(app);
+
+    // free game thread
+    // if (game_thread_running)
+    // {
+    //     game_thread_running = false;
+    //     if (game_thread)
+    //     {
+    //         furi_thread_flags_set(furi_thread_get_id(game_thread), WorkerEvtStop);
+    //         furi_thread_join(game_thread);
+    //         furi_thread_free(game_thread);
+    //         game_thread = NULL;
+    //     }
+    // }
+
+    if (free_settings_other)
+    {
+        free_submenu_other(app);
+    }
+
+    // free Derek's loader
+    loader_view_free(app);
+
+    if (free_submenu_game)
+    {
+        free_game_submenu(app);
+    }
+
+    // free waiting thread
+    // if (waiting_thread_running)
+    // {
+    //     waiting_thread_running = false;
+    //     if (waiting_thread)
+    //     {
+    //         furi_thread_flags_set(furi_thread_get_id(waiting_thread), WorkerEvtStop);
+    //         furi_thread_join(waiting_thread);
+    //         furi_thread_free(waiting_thread);
+    //         waiting_thread = NULL;
+    //     }
+    // }
+}

+ 9 - 0
callback/free.h

@@ -0,0 +1,9 @@
+#pragma once
+#include <flip_world.h>
+
+void free_game_submenu(void *context);
+void free_submenu_other(void *context);
+void free_message_view(void *context);
+void free_text_input_view(void *context);
+void free_variable_item_list(void *context);
+void free_all_views(void *context, bool free_variable_list, bool free_settings_other, bool free_submenu_game);

+ 50 - 0
callback/loader.c

@@ -1,4 +1,54 @@
 #include <callback/loader.h>
+#include <alloc/alloc.h>
+
+bool loader_view_alloc(void *context)
+{
+    FlipWorldApp *app = (FlipWorldApp *)context;
+    furi_check(app, "FlipWorldApp is NULL");
+    if (app->view_loader)
+    {
+        FURI_LOG_E(TAG, "View loader already allocated");
+        return false;
+    }
+    if (app->widget_result)
+    {
+        FURI_LOG_E(TAG, "Widget result already allocated");
+        return false;
+    }
+
+    view_dispatcher_set_custom_event_callback(app->view_dispatcher, loader_custom_event_callback);
+
+    if (!easy_flipper_set_view(&app->view_loader, FlipWorldViewLoader, loader_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app))
+    {
+        return false;
+    }
+
+    loader_init(app->view_loader);
+
+    return easy_flipper_set_widget(&app->widget_result, FlipWorldViewWidgetResult, "", callback_to_submenu, &app->view_dispatcher);
+}
+
+void loader_view_free(void *context)
+{
+    FlipWorldApp *app = (FlipWorldApp *)context;
+    furi_check(app, "FlipWorldApp is NULL");
+    // Free Widget(s)
+    if (app->widget_result)
+    {
+        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewWidgetResult);
+        widget_free(app->widget_result);
+        app->widget_result = NULL;
+    }
+
+    // Free View(s)
+    if (app->view_loader)
+    {
+        view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewLoader);
+        loader_free_model(app->view_loader);
+        view_free(app->view_loader);
+        app->view_loader = NULL;
+    }
+}
 
 static void loader_error_draw(Canvas *canvas, DataLoaderModel *model)
 {

+ 2 - 2
callback/loader.h

@@ -39,10 +39,10 @@ struct DataLoaderModel
     FuriTimer *timer;
     FlipperHTTP *fhttp;
 };
+bool loader_view_alloc(void *context);
+void loader_view_free(void *context);
 void loader_switch_to_view(FlipWorldApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id);
-
 void loader_draw_callback(Canvas *canvas, void *model);
-
 void loader_init(View *view);
 void loader_widget_set_text(char *message, Widget **widget);
 void loader_free_model(View *view);