register.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "../main.h"
  2. /**
  3. * SceneManagerHandlers initialization using the macro.
  4. */
  5. void (*const scene_on_enter_handlers[])(void* context) = {
  6. #define SCENE_ACTION(scene) scene##_on_enter,
  7. #include "list.h"
  8. #undef SCENE_ACTION
  9. };
  10. bool (*const scene_on_event_handlers[])(void* context, SceneManagerEvent event) = {
  11. #define SCENE_ACTION(scene) scene##_on_event,
  12. #include "list.h"
  13. #undef SCENE_ACTION
  14. };
  15. void (*const scene_on_exit_handlers[])(void* context) = {
  16. #define SCENE_ACTION(scene) scene##_on_exit,
  17. #include "list.h"
  18. #undef SCENE_ACTION
  19. };
  20. const SceneManagerHandlers scene_handlers = {
  21. .on_enter_handlers = scene_on_enter_handlers,
  22. .on_event_handlers = scene_on_event_handlers,
  23. .on_exit_handlers = scene_on_exit_handlers,
  24. .scene_num = AppSceneNum,
  25. };
  26. /**
  27. * Register all scenes.
  28. */
  29. void register_scenes(App* app) {
  30. app->scene_manager = scene_manager_alloc(&scene_handlers, app);
  31. furi_assert(app->scene_manager != NULL, "Failed to allocate scene manager.");
  32. app->view_dispatcher = view_dispatcher_alloc();
  33. furi_assert(app->view_dispatcher != NULL, "Failed to allocate view dispatcher.");
  34. if(app->allocated_scenes == NULL) {
  35. app->allocated_scenes = (void**)malloc(sizeof(void*) * AppSceneNum);
  36. }
  37. view_dispatcher_enable_queue(app->view_dispatcher);
  38. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  39. view_dispatcher_set_custom_event_callback(app->view_dispatcher, scene_handler_event_forwarder);
  40. view_dispatcher_set_navigation_event_callback(
  41. app->view_dispatcher, scene_handler_navigation_forwarder);
  42. View* view = NULL;
  43. #define SCENE_ACTION(scene) \
  44. app->allocated_scenes[scene] = (void*)scene##_alloc(); \
  45. furi_assert( \
  46. app->allocated_scenes[scene] != NULL, "Failed to allocate scene: " STRINGIFY(scene)); \
  47. view = scene##_get_view(app->allocated_scenes[scene]); \
  48. furi_assert(view != NULL, "Failed to get view for scene: " STRINGIFY(scene)); \
  49. view_dispatcher_add_view(app->view_dispatcher, scene, view);
  50. #include "list.h"
  51. #undef SCENE_ACTION
  52. }
  53. /**
  54. * Free all scenes.
  55. */
  56. void free_scenes(App* app) {
  57. FURI_LOG_I("DemoApp", "Freeing scenes.");
  58. furi_assert(app != NULL, "App is NULL.");
  59. void* tmp;
  60. #define SCENE_ACTION(scene) \
  61. if(app->allocated_scenes != NULL) { \
  62. tmp = app->allocated_scenes[scene]; \
  63. app->allocated_scenes[scene] = NULL; \
  64. FURI_LOG_I("DemoApp", "Freeing scene " STRINGIFY(scene) "."); \
  65. if(tmp != NULL) scene##_free(tmp); \
  66. FURI_LOG_I("DemoApp", "Free'd scene " STRINGIFY(scene) "."); \
  67. } \
  68. if(app->view_dispatcher != NULL) view_dispatcher_remove_view(app->view_dispatcher, scene); \
  69. FURI_LOG_I("DemoApp", "Removed from dispatcher " STRINGIFY(scene) ".");
  70. #include "list.h"
  71. #undef SCENE_ACTION
  72. FURI_LOG_I("DemoApp", "Freeing allocated scenes.");
  73. furi_assert(app->allocated_scenes != NULL, "Allocated scenes is NULL.");
  74. free(app->allocated_scenes);
  75. app->allocated_scenes = NULL;
  76. FURI_LOG_I("DemoApp", "Freeing View dispatcher.");
  77. furi_assert(app->view_dispatcher != NULL, "View dispatcher is NULL.");
  78. view_dispatcher_free(app->view_dispatcher);
  79. FURI_LOG_I("DemoApp", "Freeing SceneManager");
  80. furi_assert(app->scene_manager != NULL, "Scene manager is NULL.");
  81. scene_manager_free(app->scene_manager);
  82. FURI_LOG_I("DemoApp", "Freeing App");
  83. free(app);
  84. }