unleashed-firmware/applications/services/gui/view_dispatcher.c

407 lines
14 KiB
C
Raw Normal View History

#include "view_dispatcher_i.h"
#define TAG "ViewDispatcher"
ViewDispatcher* view_dispatcher_alloc(void) {
[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
ViewDispatcher* view_dispatcher = malloc(sizeof(ViewDispatcher));
view_dispatcher->view_port = view_port_alloc();
view_port_draw_callback_set(
view_dispatcher->view_port, view_dispatcher_draw_callback, view_dispatcher);
view_port_input_callback_set(
view_dispatcher->view_port, view_dispatcher_input_callback, view_dispatcher);
view_port_enabled_set(view_dispatcher->view_port, false);
ViewDict_init(view_dispatcher->views);
return view_dispatcher;
}
void view_dispatcher_free(ViewDispatcher* view_dispatcher) {
// Detach from gui
if(view_dispatcher->gui) {
gui_remove_view_port(view_dispatcher->gui, view_dispatcher->view_port);
}
// Crash if not all views were freed
furi_check(!ViewDict_size(view_dispatcher->views));
ViewDict_clear(view_dispatcher->views);
// Free ViewPort
view_port_free(view_dispatcher->view_port);
// Free internal queue
if(view_dispatcher->input_queue) {
furi_event_loop_message_queue_unsubscribe(
view_dispatcher->event_loop, view_dispatcher->input_queue);
furi_message_queue_free(view_dispatcher->input_queue);
}
if(view_dispatcher->event_queue) {
furi_event_loop_message_queue_unsubscribe(
view_dispatcher->event_loop, view_dispatcher->event_queue);
furi_message_queue_free(view_dispatcher->event_queue);
}
if(view_dispatcher->event_loop) {
furi_event_loop_free(view_dispatcher->event_loop);
}
// Free dispatcher
free(view_dispatcher);
}
void view_dispatcher_enable_queue(ViewDispatcher* view_dispatcher) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->event_loop == NULL);
view_dispatcher->event_loop = furi_event_loop_alloc();
view_dispatcher->input_queue = furi_message_queue_alloc(16, sizeof(InputEvent));
furi_event_loop_message_queue_subscribe(
view_dispatcher->event_loop,
view_dispatcher->input_queue,
FuriEventLoopEventIn,
view_dispatcher_run_input_callback,
view_dispatcher);
view_dispatcher->event_queue = furi_message_queue_alloc(16, sizeof(uint32_t));
furi_event_loop_message_queue_subscribe(
view_dispatcher->event_loop,
view_dispatcher->event_queue,
FuriEventLoopEventIn,
view_dispatcher_run_event_callback,
view_dispatcher);
}
void view_dispatcher_set_event_callback_context(ViewDispatcher* view_dispatcher, void* context) {
furi_check(view_dispatcher);
view_dispatcher->event_context = context;
}
void view_dispatcher_set_navigation_event_callback(
ViewDispatcher* view_dispatcher,
ViewDispatcherNavigationEventCallback callback) {
furi_check(view_dispatcher);
view_dispatcher->navigation_event_callback = callback;
}
void view_dispatcher_set_custom_event_callback(
ViewDispatcher* view_dispatcher,
ViewDispatcherCustomEventCallback callback) {
furi_check(view_dispatcher);
view_dispatcher->custom_event_callback = callback;
}
void view_dispatcher_set_tick_event_callback(
ViewDispatcher* view_dispatcher,
ViewDispatcherTickEventCallback callback,
uint32_t tick_period) {
furi_check(view_dispatcher);
view_dispatcher->tick_event_callback = callback;
view_dispatcher->tick_period = tick_period;
}
FuriEventLoop* view_dispatcher_get_event_loop(ViewDispatcher* view_dispatcher) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->event_loop);
return view_dispatcher->event_loop;
}
void view_dispatcher_run(ViewDispatcher* view_dispatcher) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->event_loop);
uint32_t tick_period = view_dispatcher->tick_period == 0 ? FuriWaitForever :
view_dispatcher->tick_period;
furi_event_loop_tick_set(
view_dispatcher->event_loop,
tick_period,
view_dispatcher_handle_tick_event,
view_dispatcher);
furi_event_loop_run(view_dispatcher->event_loop);
// Wait till all input events delivered
InputEvent input;
while(view_dispatcher->ongoing_input) {
furi_message_queue_get(view_dispatcher->input_queue, &input, FuriWaitForever);
uint8_t key_bit = (1 << input.key);
if(input.type == InputTypePress) {
view_dispatcher->ongoing_input |= key_bit;
} else if(input.type == InputTypeRelease) {
view_dispatcher->ongoing_input &= ~key_bit;
}
}
}
void view_dispatcher_stop(ViewDispatcher* view_dispatcher) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->event_loop);
furi_event_loop_stop(view_dispatcher->event_loop);
}
void view_dispatcher_add_view(ViewDispatcher* view_dispatcher, uint32_t view_id, View* view) {
furi_check(view_dispatcher);
furi_check(view);
new iButton app (#328) * rename old ibutton app to ibutton-test * more renames * updated onewire library compilation condition * add submenu_clean subroutine * add index for submenu callback * c++ guard for gui modules * add released ibutton app * fix the position of the submenu window if there are too few items * iButton app basis * negative icon position info * fix submenu_clean subroutine * add ibutton app to applications makefile * add onewire key read routine to read mode * rename mode to scene * rename files and folder (mode to scene) * rename ibutton view to view manager * rename get_view to get_view_manager * cpp guards * key read, store and notify features * syntax fix * make iButtonScene functions pure virtual * fix syntax * add text store, add new scene (crc error) * not a key scene * syntax fix * read success scene * app, switching to the previous scene with the number of scenes to be skipped * scene whith menu when key is readed * fix font height calculation, fix offsets * add key write scene * view_dispatcher_remove_view subroutine * generic pause/resume os methods * fix furi_assert usage * key store, worker * fix pointer comparsion * saved keys, saved key action scenes * key delete/confirm delete scenes and routines * use last input subsystem changes * fix syntax * fix new model usage in submenu * fix includes * use vibro pin * use stored key name if valid * emulate scene * random name generator * name and save readed key scenes, new icon * fix icon position * fix text scene exit * fix naming, fix text placement, new info scene * state-driven cyfral decoder * better cyfral decoder * better cyfral decoder * one wire: search command set * metakom decoder * more key types * add next scene to error scenes * universal key reader * use new key reader * syntax fix * warning fix * byte input module template * new thread and insomnia api usage * New element: slightly rounded frame * Use elements_slightly_rounded_frame in text input * Gui test app: byte input usage * Byte input module: data drawing and selection * Byte input: comment currently unused fns * remove volatile qualifier * base byte input realisation * App gui test: remove internal fns visibility * Byne input, final version * test install gcc-arm-none-eabi-10-2020-q4-major * test install gcc-arm-none-eabi-10-2020-q4-major * App iButton: byte input view managment * App iButton: add key manually scenes * App iButton: rename scenes, add popup timeout * App iButton: use new scenes, new fn for rollback to specific prevous scene. * App iButton: remove byte input view on app exit * App iButton: edit key scene * Module byte input: reduce swintch value to uint8_t * Module byte input: switch from switch-case to if, unfortunately we need compile-time constants to use with switch * Icons: new small arrows * Module byte input: new arrangement of elements * OneWire slave lib: fix deattach sequence * App iButton: pulse sequencer * App iButton: add more keys to store * App iButton: split key worker to separate read/write/emulate entitys * App iButton: use new read/emulate entities * fix callback pointer saving * App iButton: use KeyReader error enum instead of KeyWorker error list handling * App iButton: do not use insomnia fns in pulse sequencer * App iButton: use KeyReader error enum in read scene * OneWire slave lib: more READ ROM command variants, call callback only if positive result * GPIO resources: add external gpio * App SD/NFC: removed application * App iButton-test: update to new light api * App iButton: update to new light-api * Outdated apps: add api-light-usage * Gpio: update SD card CS pin settings * API-power: added fns to disable/enable external 3v3 dc-dc * API-gpio: separated SD card detect routines * Resources: removed sd cs pin * SD card: low level init now resets card power supply * App SD-filesystem: use new card detect fns * SD card: fix low level init headers * SD card: more realilable low level init, power reset, exit from command read cycle conditionally * App SD-filesystem: led notifiers, init cycling * SD card: backport to F4 * Api PWM: add c++ guards * App iButton: yellow blink in emulate scene, vibro on * App iButton: one wire keys command set * App iButton: successful write scene * App iButton: key writer * App iButton: syntax fix * App iButton: notify write success * App iButton: fix double scene change * SD card: handle eject in init sequence * SD card: api to set level on detect gpio * SPI: api to set state on bus pins * SD card: set low state on bus pins while power reset * File select: init * File select: fix input consuming * SD Card: fixed dir open api error * SD-card: replace strncpy by strlcpy. Fix buffer overflow error. * API HAL OS: replace CMP based ticks with ARR based one, hard reset lptimer on reconfiguration. * GUI: More stack size for (temporary, wee need to implement sd card api in separate thread) * GUI: File select module. * App iButton-test: remove obsolete app Co-authored-by: rusdacent <rusdacentx0x08@gmail.com> Co-authored-by: coreglitch <mail@s3f.ru> Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-03-12 15:45:18 +03:00
// Check if view id is not used and register view
furi_check(ViewDict_get(view_dispatcher->views, view_id) == NULL);
// Lock gui
if(view_dispatcher->gui) {
gui_lock(view_dispatcher->gui);
}
ViewDict_set_at(view_dispatcher->views, view_id, view);
view_set_update_callback(view, view_dispatcher_update);
view_set_update_callback_context(view, view_dispatcher);
// Unlock gui
if(view_dispatcher->gui) {
gui_unlock(view_dispatcher->gui);
}
}
void view_dispatcher_remove_view(ViewDispatcher* view_dispatcher, uint32_t view_id) {
furi_check(view_dispatcher);
// Lock gui
if(view_dispatcher->gui) {
gui_lock(view_dispatcher->gui);
}
// Get View by ID
View* view = *ViewDict_get(view_dispatcher->views, view_id);
// Disable the view if it is active
if(view_dispatcher->current_view == view) {
view_dispatcher_set_current_view(view_dispatcher, NULL);
}
// Check if view is receiving input
if(view_dispatcher->ongoing_input_view == view) {
view_dispatcher->ongoing_input_view = NULL;
}
// Remove view
furi_check(ViewDict_erase(view_dispatcher->views, view_id));
view_set_update_callback(view, NULL);
view_set_update_callback_context(view, NULL);
// Unlock gui
if(view_dispatcher->gui) {
gui_unlock(view_dispatcher->gui);
}
}
void view_dispatcher_switch_to_view(ViewDispatcher* view_dispatcher, uint32_t view_id) {
furi_check(view_dispatcher);
if(view_id == VIEW_NONE) {
view_dispatcher_set_current_view(view_dispatcher, NULL);
} else if(view_id == VIEW_IGNORE) {
} else {
View** view_pp = ViewDict_get(view_dispatcher->views, view_id);
furi_check(view_pp != NULL);
view_dispatcher_set_current_view(view_dispatcher, *view_pp);
}
}
void view_dispatcher_send_to_front(ViewDispatcher* view_dispatcher) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->gui);
gui_view_port_send_to_front(view_dispatcher->gui, view_dispatcher->view_port);
}
void view_dispatcher_send_to_back(ViewDispatcher* view_dispatcher) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->gui);
[FL-2150] Dolphin animation refactoring (#938) * Dolphin Animation Refactoring, part 1 * Remove animations from desktop * Remove excess, first start * Split animation_manager with callbacks * allocate view inside animation_view * Work on ViewComposed * Draw white rectangles under bubble corners * Fix bubbles sequence * RPC: remove obsolete include "status.pb.h" * Add animations manifest decoding * Flipper file: add strict mode * FFF: Animation structures parsing * Assembling structure of animation * Lot of view fixes: Add multi-line bubbles Add support for passive bubbles (frame_order values starts from passive now) Add hard-coded delay (active_shift) for active state enabling Fix active state handling Fix leaks Fix parsing uncorrect bubble_animation meta file Fix bubble rules of showing * Animation load/unload & view freeze/unfreeze * Blocking & system animations, fixes: View correct activation Refactoring + blocking animation Freeze first passive/active frames Many insert/eject SD tests fixes Add system animations Add Loader events app started/finished Add system no_sd animation * Assets: dolphin packer. Scripts: minor refactoring. * Desktop: update logging tags. Scripts: add metadata to dolphin bundling process, extra sorting for fs traversing. Make: phony assets rules. * Github: rebuild assets on build * Docker: add missing dependencies for assets compilation * Docker: fix run command syntax * ReadMe: update naming rules with link to source * Assets: recompile icons * Loader: add loader event * Desktop, Gui, Furi Core: const shenanigans macros Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2022-01-03 00:39:56 +03:00
gui_view_port_send_to_back(view_dispatcher->gui, view_dispatcher->view_port);
}
void view_dispatcher_attach_to_gui(
ViewDispatcher* view_dispatcher,
Gui* gui,
ViewDispatcherType type) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->gui == NULL);
furi_check(gui);
if(type == ViewDispatcherTypeDesktop) {
gui_add_view_port(gui, view_dispatcher->view_port, GuiLayerDesktop);
} else if(type == ViewDispatcherTypeWindow) {
gui_add_view_port(gui, view_dispatcher->view_port, GuiLayerWindow);
} else if(type == ViewDispatcherTypeFullscreen) {
gui_add_view_port(gui, view_dispatcher->view_port, GuiLayerFullscreen);
} else {
furi_crash();
}
view_dispatcher->gui = gui;
}
void view_dispatcher_draw_callback(Canvas* canvas, void* context) {
ViewDispatcher* view_dispatcher = context;
if(view_dispatcher->current_view) {
view_draw(view_dispatcher->current_view, canvas);
}
}
void view_dispatcher_input_callback(InputEvent* event, void* context) {
ViewDispatcher* view_dispatcher = context;
if(view_dispatcher->input_queue) {
furi_check(
furi_message_queue_put(view_dispatcher->input_queue, event, FuriWaitForever) ==
FuriStatusOk);
} else {
view_dispatcher_handle_input(view_dispatcher, event);
}
}
void view_dispatcher_handle_input(ViewDispatcher* view_dispatcher, InputEvent* event) {
// Check input complementarity
uint8_t key_bit = (1 << event->key);
if(event->type == InputTypePress) {
view_dispatcher->ongoing_input |= key_bit;
} else if(event->type == InputTypeRelease) {
view_dispatcher->ongoing_input &= ~key_bit;
} else if(!(view_dispatcher->ongoing_input & key_bit)) {
FURI_LOG_D(
TAG,
"non-complementary input, discarding key: %s, type: %s, sequence: %p",
input_get_key_name(event->key),
input_get_type_name(event->type),
(void*)event->sequence);
return;
}
// Set ongoing input view if this is event is first press event
if(!(view_dispatcher->ongoing_input & ~key_bit) && event->type == InputTypePress) {
view_dispatcher->ongoing_input_view = view_dispatcher->current_view;
}
// Deliver event
if(view_dispatcher->current_view &&
view_dispatcher->ongoing_input_view == view_dispatcher->current_view) {
// Dispatch input to current view
bool is_consumed = view_input(view_dispatcher->current_view, event);
// Navigate if input is not consumed
if(!is_consumed && (event->key == InputKeyBack) &&
(event->type == InputTypeShort || event->type == InputTypeLong)) {
// Navigate to previous
uint32_t view_id = view_previous(view_dispatcher->current_view);
if(view_id != VIEW_IGNORE) {
// Switch to returned view
view_dispatcher_switch_to_view(view_dispatcher, view_id);
} else if(view_dispatcher->navigation_event_callback) {
// Dispatch navigation event
if(!view_dispatcher->navigation_event_callback(view_dispatcher->event_context)) {
view_dispatcher_stop(view_dispatcher);
return;
}
}
}
} else if(view_dispatcher->ongoing_input_view && event->type == InputTypeRelease) {
FURI_LOG_D(
TAG,
"View changed while key press %p -> %p. Sending key: %s, type: %s, sequence: %p to previous view",
view_dispatcher->ongoing_input_view,
view_dispatcher->current_view,
input_get_key_name(event->key),
input_get_type_name(event->type),
(void*)event->sequence);
view_input(view_dispatcher->ongoing_input_view, event);
}
}
void view_dispatcher_handle_tick_event(void* context) {
ViewDispatcher* view_dispatcher = context;
if(view_dispatcher->tick_event_callback) {
view_dispatcher->tick_event_callback(view_dispatcher->event_context);
}
}
void view_dispatcher_handle_custom_event(ViewDispatcher* view_dispatcher, uint32_t event) {
bool is_consumed = false;
if(view_dispatcher->current_view) {
is_consumed = view_custom(view_dispatcher->current_view, event);
}
// If custom event is not consumed in View, call callback
if(!is_consumed && view_dispatcher->custom_event_callback) {
view_dispatcher->custom_event_callback(view_dispatcher->event_context, event);
}
}
void view_dispatcher_send_custom_event(ViewDispatcher* view_dispatcher, uint32_t event) {
furi_check(view_dispatcher);
furi_check(view_dispatcher->event_loop);
furi_check(
furi_message_queue_put(view_dispatcher->event_queue, &event, FuriWaitForever) ==
FuriStatusOk);
}
static const ViewPortOrientation view_dispatcher_view_port_orientation_table[] = {
[ViewOrientationVertical] = ViewPortOrientationVertical,
[ViewOrientationVerticalFlip] = ViewPortOrientationVerticalFlip,
[ViewOrientationHorizontal] = ViewPortOrientationHorizontal,
[ViewOrientationHorizontalFlip] = ViewPortOrientationHorizontalFlip,
};
void view_dispatcher_set_current_view(ViewDispatcher* view_dispatcher, View* view) {
furi_check(view_dispatcher);
// Dispatch view exit event
if(view_dispatcher->current_view) {
view_exit(view_dispatcher->current_view);
}
// Set current view
view_dispatcher->current_view = view;
// Dispatch view enter event
if(view_dispatcher->current_view) {
ViewPortOrientation orientation =
view_dispatcher_view_port_orientation_table[view->orientation];
if(view_port_get_orientation(view_dispatcher->view_port) != orientation) {
view_port_set_orientation(view_dispatcher->view_port, orientation);
// we just rotated input keys, now it's time to sacrifice some input
view_dispatcher->ongoing_input = 0;
}
view_enter(view_dispatcher->current_view);
view_port_enabled_set(view_dispatcher->view_port, true);
view_port_update(view_dispatcher->view_port);
} else {
view_port_enabled_set(view_dispatcher->view_port, false);
if(view_dispatcher->event_loop) {
view_dispatcher_stop(view_dispatcher);
}
}
}
void view_dispatcher_update(View* view, void* context) {
furi_check(view);
furi_check(context);
ViewDispatcher* view_dispatcher = context;
if(view_dispatcher->current_view == view) {
view_port_update(view_dispatcher->view_port);
}
}
bool view_dispatcher_run_event_callback(FuriMessageQueue* queue, void* context) {
furi_assert(context);
ViewDispatcher* instance = context;
furi_assert(instance->event_queue == queue);
uint32_t event;
furi_check(furi_message_queue_get(instance->event_queue, &event, 0) == FuriStatusOk);
view_dispatcher_handle_custom_event(instance, event);
return true;
}
bool view_dispatcher_run_input_callback(FuriMessageQueue* queue, void* context) {
furi_assert(context);
ViewDispatcher* instance = context;
furi_assert(instance->input_queue == queue);
InputEvent input;
furi_check(furi_message_queue_get(instance->input_queue, &input, 0) == FuriStatusOk);
view_dispatcher_handle_input(instance, &input);
return true;
}