flipperzero-firmware/applications/gui/scene_manager.c
SG 274c12fc56
[FL-2274] Inventing streams and moving FFF to them (#981)
* Streams: string stream
* String stream: updated insert/delete api
* Streams: generic stream interface and string stream implementation
* Streams: helpers for insert and delete_and_insert
* FFF: now compatible with streams
* MinUnit: introduced tests with arguments
* FFF: stream access violation
* Streams: copy data between streams
* Streams: file stream
* FFF: documentation
* FFStream: documentation
* FFF: alloc as file
* MinUnit: support for nested tests
* Streams: changed delete_and_insert, now it returns success flag. Added ability dump stream inner parameters and data to cout.
* FFF: simplified file open function
* Streams: unit tests
* FFF: tests
* Streams: declare cache_size constant as define, to allow variable modified arrays
* FFF: lib moved to a separate folder
* iButton: new FFF
* RFID: new FFF
* Animations: new FFF
* IR: new FFF
* NFC: new FFF
* Flipper file format: delete lib
* U2F: new FFF
* Subghz: new FFF and streams
* Streams: read line
* Streams: split
* FuriCore: implement memset with extra asserts
* FuriCore: implement extra heap asserts without inventing memset
* Scene manager: protected access to the scene id stack with a size check
* NFC worker: dirty fix for issue where hal_nfc was busy on app start
* Furi: update allocator to erase memory on allocation. Replace furi_alloc with malloc.
* FuriCore: cleanup memmgr code.
* Furi HAL: furi_hal_init is split into critical and non-critical parts. The critical part is currently clock and console.
* Memmgr: added ability to track allocations and deallocations through console.
* FFStream: some speedup
* Streams, FF: minor fixes
* Tests: restore
* File stream: a slightly more thread-safe version of file_stream_delete_and_insert

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2022-02-18 22:53:46 +03:00

225 lines
8.1 KiB
C
Executable File

#include "scene_manager_i.h"
#include <furi.h>
SceneManager* scene_manager_alloc(const SceneManagerHandlers* app_scene_handlers, void* context) {
furi_assert(context);
SceneManager* scene_manager = malloc(sizeof(SceneManager));
// Set SceneManager context and scene handlers
scene_manager->context = context;
scene_manager->scene_handlers = app_scene_handlers;
// Allocate all scenes
scene_manager->scene = malloc(sizeof(AppScene) * app_scene_handlers->scene_num);
// Initialize ScaneManager array for navigation
SceneManagerIdStack_init(scene_manager->scene_id_stack);
return scene_manager;
}
void scene_manager_free(SceneManager* scene_manager) {
furi_assert(scene_manager);
// Clear ScaneManager array
SceneManagerIdStack_clear(scene_manager->scene_id_stack);
// Clear allocated scenes
free(scene_manager->scene);
// Free SceneManager structure
free(scene_manager);
}
void scene_manager_set_scene_state(SceneManager* scene_manager, uint32_t scene_id, uint32_t state) {
furi_assert(scene_manager);
furi_assert(scene_id < scene_manager->scene_handlers->scene_num);
scene_manager->scene[scene_id].state = state;
}
uint32_t scene_manager_get_scene_state(SceneManager* scene_manager, uint32_t scene_id) {
furi_assert(scene_manager);
furi_assert(scene_id < scene_manager->scene_handlers->scene_num);
return scene_manager->scene[scene_id].state;
}
bool scene_manager_handle_custom_event(SceneManager* scene_manager, uint32_t custom_event) {
furi_assert(scene_manager);
SceneManagerEvent event = {
.type = SceneManagerEventTypeCustom,
.event = custom_event,
};
bool result = false;
if(SceneManagerIdStack_size(scene_manager->scene_id_stack) > 0) {
uint32_t* scene_id_p = SceneManagerIdStack_back(scene_manager->scene_id_stack);
uint32_t scene_id = *scene_id_p;
result = scene_manager->scene_handlers->on_event_handlers[scene_id](
scene_manager->context, event);
}
return result;
}
bool scene_manager_handle_back_event(SceneManager* scene_manager) {
furi_assert(scene_manager);
SceneManagerEvent event = {
.type = SceneManagerEventTypeBack,
};
bool consumed = false;
if(SceneManagerIdStack_size(scene_manager->scene_id_stack) > 0) {
uint32_t* scene_id_p = SceneManagerIdStack_back(scene_manager->scene_id_stack);
uint32_t scene_id = *scene_id_p;
consumed = scene_manager->scene_handlers->on_event_handlers[scene_id](
scene_manager->context, event);
}
if(!consumed) {
consumed = scene_manager_previous_scene(scene_manager);
}
return consumed;
}
void scene_manager_handle_tick_event(SceneManager* scene_manager) {
furi_assert(scene_manager);
SceneManagerEvent event = {
.type = SceneManagerEventTypeTick,
};
if(SceneManagerIdStack_size(scene_manager->scene_id_stack) > 0) {
uint32_t* scene_id_p = SceneManagerIdStack_back(scene_manager->scene_id_stack);
uint32_t scene_id = *scene_id_p;
scene_manager->scene_handlers->on_event_handlers[scene_id](scene_manager->context, event);
}
}
void scene_manager_next_scene(SceneManager* scene_manager, uint32_t next_scene_id) {
furi_assert(scene_manager);
furi_assert(next_scene_id < scene_manager->scene_handlers->scene_num);
// Check if it is not the first scene
if(SceneManagerIdStack_size(scene_manager->scene_id_stack) > 0) {
uint32_t cur_scene_id = *SceneManagerIdStack_back(scene_manager->scene_id_stack);
scene_manager->scene_handlers->on_exit_handlers[cur_scene_id](scene_manager->context);
}
// Add next scene and run on_enter
SceneManagerIdStack_push_back(scene_manager->scene_id_stack, next_scene_id);
scene_manager->scene_handlers->on_enter_handlers[next_scene_id](scene_manager->context);
}
bool scene_manager_previous_scene(SceneManager* scene_manager) {
furi_assert(scene_manager);
if(SceneManagerIdStack_size(scene_manager->scene_id_stack) > 0) {
uint32_t cur_scene_id = 0;
SceneManagerIdStack_pop_back(&cur_scene_id, scene_manager->scene_id_stack);
// Handle exit from start scene separately
if(SceneManagerIdStack_size(scene_manager->scene_id_stack) == 0) {
scene_manager->scene_handlers->on_exit_handlers[cur_scene_id](scene_manager->context);
return false;
}
uint32_t prev_scene_id = *SceneManagerIdStack_back(scene_manager->scene_id_stack);
scene_manager->scene_handlers->on_exit_handlers[cur_scene_id](scene_manager->context);
scene_manager->scene_handlers->on_enter_handlers[prev_scene_id](scene_manager->context);
return true;
} else {
return false;
}
}
bool scene_manager_search_and_switch_to_previous_scene(
SceneManager* scene_manager,
uint32_t scene_id) {
furi_assert(scene_manager);
if(SceneManagerIdStack_size(scene_manager->scene_id_stack) > 0) {
uint32_t prev_scene_id = 0;
uint32_t cur_scene_id = *SceneManagerIdStack_back(scene_manager->scene_id_stack);
SceneManagerIdStack_it_t scene_it;
SceneManagerIdStack_it_last(scene_it, scene_manager->scene_id_stack);
// Search scene with given id in navigation stack
bool scene_found = false;
while(!scene_found) {
SceneManagerIdStack_previous(scene_it);
if(SceneManagerIdStack_end_p(scene_it)) {
return false;
}
prev_scene_id = *SceneManagerIdStack_ref(scene_it);
if(prev_scene_id == scene_id) {
scene_found = true;
}
}
// Remove all scene id from navigation stack
SceneManagerIdStack_next(scene_it);
SceneManagerIdStack_pop_until(scene_manager->scene_id_stack, scene_it);
scene_manager->scene_handlers->on_exit_handlers[cur_scene_id](scene_manager->context);
scene_manager->scene_handlers->on_enter_handlers[prev_scene_id](scene_manager->context);
return true;
} else {
return false;
}
}
bool scene_manager_has_previous_scene(SceneManager* scene_manager, uint32_t scene_id) {
furi_assert(scene_manager);
bool scene_found = false;
if(SceneManagerIdStack_size(scene_manager->scene_id_stack) > 0) {
uint32_t prev_scene_id;
SceneManagerIdStack_it_t scene_it;
SceneManagerIdStack_it_last(scene_it, scene_manager->scene_id_stack);
// Perform search in scene stack
while(!scene_found) {
SceneManagerIdStack_previous(scene_it);
if(SceneManagerIdStack_end_p(scene_it)) {
break;
}
prev_scene_id = *SceneManagerIdStack_ref(scene_it);
if(prev_scene_id == scene_id) {
scene_found = true;
}
}
}
return scene_found;
}
bool scene_manager_search_and_switch_to_another_scene(
SceneManager* scene_manager,
uint32_t scene_id) {
furi_assert(scene_manager);
furi_assert(scene_id < scene_manager->scene_handlers->scene_num);
if(SceneManagerIdStack_size(scene_manager->scene_id_stack) > 0) {
uint32_t cur_scene_id = *SceneManagerIdStack_back(scene_manager->scene_id_stack);
SceneManagerIdStack_it_t scene_it;
SceneManagerIdStack_it(scene_it, scene_manager->scene_id_stack);
SceneManagerIdStack_next(scene_it);
// Remove all scene id from navigation stack until first scene
SceneManagerIdStack_pop_until(scene_manager->scene_id_stack, scene_it);
// Add next scene
SceneManagerIdStack_push_back(scene_manager->scene_id_stack, scene_id);
scene_manager->scene_handlers->on_exit_handlers[cur_scene_id](scene_manager->context);
scene_manager->scene_handlers->on_enter_handlers[scene_id](scene_manager->context);
return true;
} else {
return false;
}
}
void scene_manager_stop(SceneManager* scene_manager) {
furi_assert(scene_manager);
if(SceneManagerIdStack_size(scene_manager->scene_id_stack) > 0) {
uint32_t cur_scene_id = *SceneManagerIdStack_back(scene_manager->scene_id_stack);
scene_manager->scene_handlers->on_exit_handlers[cur_scene_id](scene_manager->context);
}
}