From f0800e579f785c71998cae4ed03f36826a482392 Mon Sep 17 00:00:00 2001 From: Aleksandr Kutuzov Date: Fri, 16 Oct 2020 18:25:06 +0300 Subject: [PATCH] GUI: GuiApi isolation --- applications/gui/gui.c | 37 ++++++++++++++++--------------------- applications/gui/gui.h | 8 ++------ applications/gui/gui_i.h | 2 ++ applications/gui/widget.c | 2 +- applications/gui/widget_i.h | 2 +- 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/applications/gui/gui.c b/applications/gui/gui.c index 6be883155..382435622 100644 --- a/applications/gui/gui.c +++ b/applications/gui/gui.c @@ -15,6 +15,7 @@ ARRAY_DEF(WidgetArray, Widget*, M_PTR_OPLIST); struct Gui { + GuiApi api; GuiEvent* event; CanvasApi* canvas_api; WidgetArray_t widgets_status_bar; @@ -25,39 +26,37 @@ struct Gui { void gui_add_widget(GuiApi* gui_api, Widget* widget, WidgetLayer layer) { assert(gui_api); - assert(gui_api->gui); + assert(widget); + Gui* gui = (Gui*)gui_api; // TODO add mutex on widget array - WidgetArray_t* widget_array = NULL; switch(layer) { case WidgetLayerStatusBar: - widget_array = &gui_api->gui->widgets_status_bar; + widget_array = &gui->widgets_status_bar; break; case WidgetLayerMain: - widget_array = &gui_api->gui->widgets; + widget_array = &gui->widgets; break; case WidgetLayerFullscreen: - widget_array = &gui_api->gui->widgets_fs; + widget_array = &gui->widgets_fs; break; case WidgetLayerDialog: - widget_array = &gui_api->gui->widgets_dialog; + widget_array = &gui->widgets_dialog; break; - default: break; } - assert(widget); assert(widget_array); - gui_event_lock(gui_api->gui->event); - WidgetArray_push_back((struct WidgetArray_s*)widget_array, widget); - widget_gui_set(widget, gui_api->gui); - gui_event_unlock(gui_api->gui->event); + gui_event_lock(gui->event); + WidgetArray_push_back(*widget_array, widget); + widget_gui_set(widget, gui); + gui_event_unlock(gui->event); - gui_update(gui_api->gui); + gui_update(gui); } void gui_update(Gui* gui) { @@ -133,6 +132,7 @@ void gui_input(Gui* gui, InputEvent* input_event) { Gui* gui_alloc() { Gui* gui = furi_alloc(sizeof(Gui)); + // Initialize widget arrays WidgetArray_init(gui->widgets_status_bar); WidgetArray_init(gui->widgets); @@ -143,22 +143,17 @@ Gui* gui_alloc() { gui->event = gui_event_alloc(); // Drawing canvas api - gui->canvas_api = canvas_api_init(); + gui->api.add_widget = gui_add_widget; + return gui; } void gui_task(void* p) { Gui* gui = gui_alloc(); - - GuiApi gui_api = { - .add_widget = gui_add_widget, - .gui = gui, - }; - // Create FURI record - if(!furi_create("gui", &gui_api)) { + if(!furi_create("gui", gui)) { printf("[gui_task] cannot create the gui record\n"); furiac_exit(NULL); } diff --git a/applications/gui/gui.h b/applications/gui/gui.h index 371c0434e..8db1cc203 100644 --- a/applications/gui/gui.h +++ b/applications/gui/gui.h @@ -11,12 +11,8 @@ typedef enum { } WidgetLayer; typedef struct Widget Widget; -typedef struct Gui Gui; -struct _GuiApi; -typedef struct _GuiApi GuiApi; - -struct _GuiApi { +typedef struct GuiApi GuiApi; +struct GuiApi { void (*add_widget)(GuiApi* gui_api, Widget* widget, WidgetLayer layer); - Gui* gui; }; diff --git a/applications/gui/gui_i.h b/applications/gui/gui_i.h index 6a9bf2a0e..a64874c1e 100644 --- a/applications/gui/gui_i.h +++ b/applications/gui/gui_i.h @@ -1,3 +1,5 @@ #pragma once +typedef struct Gui Gui; + void gui_update(Gui* gui); diff --git a/applications/gui/widget.c b/applications/gui/widget.c index 3277c24d2..4b22679de 100644 --- a/applications/gui/widget.c +++ b/applications/gui/widget.c @@ -10,7 +10,7 @@ // TODO add mutex to widget ops struct Widget { - void* gui; + Gui* gui; bool is_enabled; WidgetDrawCallback draw_callback; void* draw_callback_context; diff --git a/applications/gui/widget_i.h b/applications/gui/widget_i.h index f6cc2f149..b3d481e94 100644 --- a/applications/gui/widget_i.h +++ b/applications/gui/widget_i.h @@ -1,6 +1,6 @@ #pragma once -#include "gui.h" +#include "gui_i.h" void widget_gui_set(Widget* widget, Gui* gui);