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

186 lines
4.9 KiB
C
Raw Normal View History

#include "view_i.h"
View* view_alloc() {
[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 19:53:46 +00:00
View* view = malloc(sizeof(View));
view->orientation = ViewOrientationHorizontal;
return view;
}
void view_free(View* view) {
furi_assert(view);
view_free_model(view);
free(view);
}
void view_tie_icon_animation(View* view, IconAnimation* icon_animation) {
furi_assert(view);
icon_animation_set_update_callback(icon_animation, view_icon_animation_callback, view);
}
void view_set_draw_callback(View* view, ViewDrawCallback callback) {
furi_assert(view);
view->draw_callback = callback;
}
void view_set_input_callback(View* view, ViewInputCallback callback) {
furi_assert(view);
view->input_callback = callback;
}
void view_set_custom_callback(View* view, ViewCustomCallback callback) {
furi_assert(view);
view->custom_callback = callback;
}
void view_set_previous_callback(View* view, ViewNavigationCallback callback) {
furi_assert(view);
view->previous_callback = callback;
}
void view_set_enter_callback(View* view, ViewCallback callback) {
furi_assert(view);
view->enter_callback = callback;
}
void view_set_exit_callback(View* view, ViewCallback callback) {
furi_assert(view);
view->exit_callback = callback;
}
void view_set_update_callback(View* view, ViewUpdateCallback callback) {
furi_assert(view);
view->update_callback = callback;
}
void view_set_update_callback_context(View* view, void* context) {
furi_assert(view);
view->update_callback_context = context;
}
void view_set_context(View* view, void* context) {
furi_assert(view);
view->context = context;
}
void view_set_orientation(View* view, ViewOrientation orientation) {
furi_assert(view);
view->orientation = orientation;
}
void view_allocate_model(View* view, ViewModelType type, size_t size) {
furi_assert(view);
furi_assert(size > 0);
furi_assert(view->model_type == ViewModelTypeNone);
furi_assert(view->model == NULL);
view->model_type = type;
if(view->model_type == ViewModelTypeLockFree) {
[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 19:53:46 +00:00
view->model = malloc(size);
} else if(view->model_type == ViewModelTypeLocking) {
[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 19:53:46 +00:00
ViewModelLocking* model = malloc(sizeof(ViewModelLocking));
model->mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
furi_check(model->mutex);
[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 19:53:46 +00:00
model->data = malloc(size);
view->model = model;
} else {
furi_crash(NULL);
}
}
void view_free_model(View* view) {
furi_assert(view);
if(view->model_type == ViewModelTypeNone) {
return;
} else if(view->model_type == ViewModelTypeLockFree) {
free(view->model);
} else if(view->model_type == ViewModelTypeLocking) {
ViewModelLocking* model = view->model;
furi_mutex_free(model->mutex);
free(model->data);
free(model);
view->model = NULL;
} else {
furi_crash(NULL);
}
}
void* view_get_model(View* view) {
furi_assert(view);
if(view->model_type == ViewModelTypeLocking) {
ViewModelLocking* model = (ViewModelLocking*)(view->model);
furi_check(furi_mutex_acquire(model->mutex, FuriWaitForever) == FuriStatusOk);
return model->data;
}
return view->model;
}
void view_commit_model(View* view, bool update) {
furi_assert(view);
view_unlock_model(view);
if(update && view->update_callback) {
view->update_callback(view, view->update_callback_context);
}
}
void view_icon_animation_callback(IconAnimation* instance, void* context) {
UNUSED(instance);
furi_assert(context);
View* view = context;
if(view->update_callback) {
view->update_callback(view, view->update_callback_context);
}
}
void view_unlock_model(View* view) {
furi_assert(view);
if(view->model_type == ViewModelTypeLocking) {
ViewModelLocking* model = (ViewModelLocking*)(view->model);
furi_check(furi_mutex_release(model->mutex) == FuriStatusOk);
}
}
void view_draw(View* view, Canvas* canvas) {
furi_assert(view);
if(view->draw_callback) {
void* data = view_get_model(view);
view->draw_callback(canvas, data);
view_unlock_model(view);
}
}
bool view_input(View* view, InputEvent* event) {
furi_assert(view);
if(view->input_callback) {
return view->input_callback(event, view->context);
} else {
return false;
}
}
bool view_custom(View* view, uint32_t event) {
furi_assert(view);
if(view->custom_callback) {
return view->custom_callback(event, view->context);
} else {
return false;
}
}
uint32_t view_previous(View* view) {
furi_assert(view);
if(view->previous_callback) {
return view->previous_callback(view->context);
} else {
return VIEW_IGNORE;
}
}
void view_enter(View* view) {
furi_assert(view);
if(view->enter_callback) view->enter_callback(view->context);
}
void view_exit(View* view) {
furi_assert(view);
if(view->exit_callback) view->exit_callback(view->context);
}