unleashed-firmware/applications/gui/gui.c

177 lines
4.3 KiB
C
Raw Normal View History

#include "gui.h"
#include "gui_i.h"
#include <flipper.h>
2020-10-15 15:56:47 +00:00
#include <flipper_v2.h>
#include <stdio.h>
#include <m-array.h>
#include "gui_event.h"
#include "canvas.h"
#include "canvas_i.h"
#include "widget.h"
#include "widget_i.h"
ARRAY_DEF(WidgetArray, Widget*, M_PTR_OPLIST);
2020-10-15 15:05:28 +00:00
struct Gui {
GuiEvent* event;
CanvasApi* canvas_api;
WidgetArray_t widgets_status_bar;
WidgetArray_t widgets;
WidgetArray_t widgets_fs;
WidgetArray_t widgets_dialog;
};
2020-10-15 15:56:47 +00:00
void gui_add_widget(GuiApi* gui_api, Widget* widget, WidgetLayer layer) {
assert(gui_api);
assert(gui_api->gui);
// TODO add mutex on widget array
2020-10-15 08:32:48 +00:00
WidgetArray_t* widget_array = NULL;
switch(layer) {
case WidgetLayerStatusBar:
2020-10-15 15:56:47 +00:00
widget_array = &gui_api->gui->widgets_status_bar;
2020-10-15 08:32:48 +00:00
break;
case WidgetLayerMain:
2020-10-15 15:56:47 +00:00
widget_array = &gui_api->gui->widgets;
2020-10-15 08:32:48 +00:00
break;
case WidgetLayerFullscreen:
2020-10-15 15:56:47 +00:00
widget_array = &gui_api->gui->widgets_fs;
2020-10-15 08:32:48 +00:00
break;
case WidgetLayerDialog:
2020-10-15 15:56:47 +00:00
widget_array = &gui_api->gui->widgets_dialog;
2020-10-15 08:32:48 +00:00
break;
2020-10-15 15:56:47 +00:00
default: break;
2020-10-15 08:32:48 +00:00
}
assert(widget);
2020-10-15 08:32:48 +00:00
assert(widget_array);
2020-10-15 15:56:47 +00:00
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);
2020-10-15 15:56:47 +00:00
gui_update(gui_api->gui);
}
2020-10-15 15:05:28 +00:00
void gui_update(Gui* gui) {
assert(gui);
2020-10-15 15:05:28 +00:00
GuiMessage message;
message.type = GuiMessageTypeRedraw;
gui_event_messsage_send(gui->event, &message);
}
Widget* gui_widget_find_enabled(WidgetArray_t array) {
size_t widgets_count = WidgetArray_size(array);
for(size_t i = 0; i < widgets_count; i++) {
Widget* widget = *WidgetArray_get(array, widgets_count - i - 1);
if(widget_is_enabled(widget)) {
return widget;
}
}
return NULL;
}
2020-10-15 15:05:28 +00:00
bool gui_redraw_fs(Gui* gui) {
2020-10-15 15:13:18 +00:00
canvas_frame_set(gui->canvas_api, 0, 0, 128, 64);
Widget* widget = gui_widget_find_enabled(gui->widgets_fs);
if(widget) {
2020-10-15 15:13:18 +00:00
widget_draw(widget, gui->canvas_api);
return true;
} else {
return false;
}
}
2020-10-15 15:05:28 +00:00
void gui_redraw_status_bar(Gui* gui) {
2020-10-15 15:13:18 +00:00
canvas_frame_set(gui->canvas_api, 0, 0, 128, 64);
Widget* widget = gui_widget_find_enabled(gui->widgets_status_bar);
2020-10-15 15:13:18 +00:00
if(widget) widget_draw(widget, gui->canvas_api);
}
2020-10-15 15:05:28 +00:00
void gui_redraw_normal(Gui* gui) {
2020-10-15 15:13:18 +00:00
canvas_frame_set(gui->canvas_api, 0, 9, 128, 55);
Widget* widget = gui_widget_find_enabled(gui->widgets);
2020-10-15 15:13:18 +00:00
if(widget) widget_draw(widget, gui->canvas_api);
}
2020-10-15 15:05:28 +00:00
void gui_redraw_dialogs(Gui* gui) {
2020-10-15 15:13:18 +00:00
canvas_frame_set(gui->canvas_api, 10, 20, 118, 44);
Widget* widget = gui_widget_find_enabled(gui->widgets_dialog);
2020-10-15 15:13:18 +00:00
if(widget) widget_draw(widget, gui->canvas_api);
}
2020-10-15 15:05:28 +00:00
void gui_redraw(Gui* gui) {
assert(gui);
if(!gui_redraw_fs(gui)) {
gui_redraw_status_bar(gui);
gui_redraw_normal(gui);
}
gui_redraw_dialogs(gui);
2020-10-15 15:56:47 +00:00
canvas_commit(gui->canvas_api);
}
2020-10-15 15:05:28 +00:00
void gui_input(Gui* gui, InputEvent* input_event) {
assert(gui);
Widget* widget = gui_widget_find_enabled(gui->widgets_dialog);
if(!widget) widget = gui_widget_find_enabled(gui->widgets_fs);
if(!widget) widget = gui_widget_find_enabled(gui->widgets);
if(widget) {
widget_input(widget, input_event);
}
}
2020-10-15 15:05:28 +00:00
Gui* gui_alloc() {
Gui* gui = furi_alloc(sizeof(Gui));
// Initialize widget arrays
WidgetArray_init(gui->widgets_status_bar);
WidgetArray_init(gui->widgets);
WidgetArray_init(gui->widgets_fs);
WidgetArray_init(gui->widgets_dialog);
2020-10-15 08:32:48 +00:00
// Event dispatcher
gui->event = gui_event_alloc();
2020-10-15 15:05:28 +00:00
// Drawing canvas api
2020-10-15 15:13:18 +00:00
gui->canvas_api = canvas_api_init();
return gui;
}
void gui_task(void* p) {
2020-10-15 15:05:28 +00:00
Gui* gui = gui_alloc();
2020-10-15 08:32:48 +00:00
GuiApi gui_api = {
.add_widget = gui_add_widget,
.gui = gui,
};
// Create FURI record
2020-10-15 08:32:48 +00:00
if(!furi_create("gui", &gui_api)) {
printf("[gui_task] cannot create the gui record\n");
furiac_exit(NULL);
}
2020-10-15 08:32:48 +00:00
furiac_ready();
2020-10-15 08:32:48 +00:00
// Forever dispatch
while(1) {
2020-10-15 15:05:28 +00:00
GuiMessage message = gui_event_message_next(gui->event);
if(message.type == GuiMessageTypeRedraw) {
gui_redraw(gui);
2020-10-15 15:05:28 +00:00
} else if(message.type == GuiMessageTypeInput) {
gui_input(gui, &message.input);
}
}
}