2021-01-08 07:42:48 +03:00
|
|
|
#include "view_i.h"
|
|
|
|
|
2024-03-19 17:43:52 +03:00
|
|
|
View* view_alloc(void) {
|
2022-02-18 22:53:46 +03:00
|
|
|
View* view = malloc(sizeof(View));
|
2021-05-19 12:43:15 +03:00
|
|
|
view->orientation = ViewOrientationHorizontal;
|
2021-01-08 07:42:48 +03:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
void view_free(View* view) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
view_free_model(view);
|
|
|
|
free(view);
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:00:56 +03:00
|
|
|
void view_tie_icon_animation(View* view, IconAnimation* icon_animation) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-10-02 20:00:56 +03:00
|
|
|
icon_animation_set_update_callback(icon_animation, view_icon_animation_callback, view);
|
|
|
|
}
|
|
|
|
|
2021-01-08 07:42:48 +03:00
|
|
|
void view_set_draw_callback(View* view, ViewDrawCallback callback) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
view->draw_callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void view_set_input_callback(View* view, ViewInputCallback callback) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
view->input_callback = callback;
|
|
|
|
}
|
|
|
|
|
2021-06-30 20:43:29 +03:00
|
|
|
void view_set_custom_callback(View* view, ViewCustomCallback callback) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-06-30 20:43:29 +03:00
|
|
|
view->custom_callback = callback;
|
|
|
|
}
|
|
|
|
|
2021-01-08 07:42:48 +03:00
|
|
|
void view_set_previous_callback(View* view, ViewNavigationCallback callback) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
view->previous_callback = callback;
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:05:46 +03:00
|
|
|
void view_set_enter_callback(View* view, ViewCallback callback) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-02-04 15:05:46 +03:00
|
|
|
view->enter_callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void view_set_exit_callback(View* view, ViewCallback callback) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-02-04 15:05:46 +03:00
|
|
|
view->exit_callback = callback;
|
|
|
|
}
|
|
|
|
|
2021-04-01 15:03:02 +03:00
|
|
|
void view_set_update_callback(View* view, ViewUpdateCallback callback) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-04-01 15:03:02 +03:00
|
|
|
view->update_callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void view_set_update_callback_context(View* view, void* context) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-04-01 15:03:02 +03:00
|
|
|
view->update_callback_context = context;
|
|
|
|
}
|
|
|
|
|
2021-01-08 07:42:48 +03:00
|
|
|
void view_set_context(View* view, void* context) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
view->context = context;
|
|
|
|
}
|
|
|
|
|
2021-05-19 12:43:15 +03:00
|
|
|
void view_set_orientation(View* view, ViewOrientation orientation) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-05-19 12:43:15 +03:00
|
|
|
view->orientation = orientation;
|
|
|
|
}
|
|
|
|
|
2021-01-08 07:42:48 +03:00
|
|
|
void view_allocate_model(View* view, ViewModelType type, size_t size) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
|
|
|
furi_check(size > 0);
|
|
|
|
furi_check(view->model_type == ViewModelTypeNone);
|
|
|
|
furi_check(view->model == NULL);
|
2021-01-08 07:42:48 +03:00
|
|
|
view->model_type = type;
|
|
|
|
if(view->model_type == ViewModelTypeLockFree) {
|
2022-02-18 22:53:46 +03:00
|
|
|
view->model = malloc(size);
|
2021-01-08 07:42:48 +03:00
|
|
|
} else if(view->model_type == ViewModelTypeLocking) {
|
2022-02-18 22:53:46 +03:00
|
|
|
ViewModelLocking* model = malloc(sizeof(ViewModelLocking));
|
2023-03-28 09:31:21 +03:00
|
|
|
model->mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
|
2022-02-18 22:53:46 +03:00
|
|
|
model->data = malloc(size);
|
2021-01-08 07:42:48 +03:00
|
|
|
view->model = model;
|
|
|
|
} else {
|
2023-10-31 13:40:32 +03:00
|
|
|
furi_crash();
|
2021-01-08 07:42:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void view_free_model(View* view) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
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;
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_mutex_free(model->mutex);
|
2021-01-08 07:42:48 +03:00
|
|
|
free(model->data);
|
|
|
|
free(model);
|
|
|
|
} else {
|
2023-10-31 13:40:32 +03:00
|
|
|
furi_crash();
|
2021-01-08 07:42:48 +03:00
|
|
|
}
|
2024-05-18 21:40:01 +03:00
|
|
|
view->model = NULL;
|
|
|
|
view->model_type = ViewModelTypeNone;
|
2021-01-08 07:42:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void* view_get_model(View* view) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
if(view->model_type == ViewModelTypeLocking) {
|
|
|
|
ViewModelLocking* model = (ViewModelLocking*)(view->model);
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_check(furi_mutex_acquire(model->mutex, FuriWaitForever) == FuriStatusOk);
|
2021-01-08 07:42:48 +03:00
|
|
|
return model->data;
|
|
|
|
}
|
|
|
|
return view->model;
|
|
|
|
}
|
|
|
|
|
2021-02-10 12:06:29 +03:00
|
|
|
void view_commit_model(View* view, bool update) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-11 15:20:29 +03:00
|
|
|
view_unlock_model(view);
|
2021-04-01 15:03:02 +03:00
|
|
|
if(update && view->update_callback) {
|
|
|
|
view->update_callback(view, view->update_callback_context);
|
2021-01-11 15:20:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:00:56 +03:00
|
|
|
void view_icon_animation_callback(IconAnimation* instance, void* context) {
|
2022-05-06 16:37:10 +03:00
|
|
|
UNUSED(instance);
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(context);
|
2021-10-02 20:00:56 +03:00
|
|
|
View* view = context;
|
|
|
|
if(view->update_callback) {
|
|
|
|
view->update_callback(view, view->update_callback_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:20:29 +03:00
|
|
|
void view_unlock_model(View* view) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
if(view->model_type == ViewModelTypeLocking) {
|
|
|
|
ViewModelLocking* model = (ViewModelLocking*)(view->model);
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_check(furi_mutex_release(model->mutex) == FuriStatusOk);
|
2021-01-08 07:42:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void view_draw(View* view, Canvas* canvas) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
if(view->draw_callback) {
|
|
|
|
void* data = view_get_model(view);
|
|
|
|
view->draw_callback(canvas, data);
|
2021-01-11 15:20:29 +03:00
|
|
|
view_unlock_model(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool view_input(View* view, InputEvent* event) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
if(view->input_callback) {
|
|
|
|
return view->input_callback(event, view->context);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 20:43:29 +03:00
|
|
|
bool view_custom(View* view, uint32_t event) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-06-30 20:43:29 +03:00
|
|
|
if(view->custom_callback) {
|
|
|
|
return view->custom_callback(event, view->context);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 07:42:48 +03:00
|
|
|
uint32_t view_previous(View* view) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-01-08 07:42:48 +03:00
|
|
|
if(view->previous_callback) {
|
|
|
|
return view->previous_callback(view->context);
|
|
|
|
} else {
|
|
|
|
return VIEW_IGNORE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:05:46 +03:00
|
|
|
void view_enter(View* view) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-02-04 15:05:46 +03:00
|
|
|
if(view->enter_callback) view->enter_callback(view->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void view_exit(View* view) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(view);
|
2021-02-04 15:05:46 +03:00
|
|
|
if(view->exit_callback) view->exit_callback(view->context);
|
|
|
|
}
|