refactor app code (#93)
This commit is contained in:
committed by
GitHub
parent
a312bd5527
commit
d7b151ab88
@@ -20,13 +20,12 @@ static int32_t gui_main(void*);
|
||||
|
||||
Gui* gui = nullptr;
|
||||
|
||||
typedef void (*PubSubCallback)(const void* message, void* context);
|
||||
void loader_callback(const void* message, TT_UNUSED void* context) {
|
||||
auto* event = static_cast<const loader::LoaderEvent*>(message);
|
||||
if (event->type == loader::LoaderEventTypeApplicationShowing) {
|
||||
app::App* app = event->app_showing.app;
|
||||
const app::Manifest& app_manifest = app::tt_app_get_manifest(app);
|
||||
show_app(app, app_manifest.on_show, app_manifest.on_hide);
|
||||
app::App& app = event->app_showing.app;
|
||||
const app::Manifest& app_manifest = app.getManifest();
|
||||
show_app(app, app_manifest.onShow, app_manifest.onHide);
|
||||
} else if (event->type == loader::LoaderEventTypeApplicationHiding) {
|
||||
hide_app();
|
||||
}
|
||||
@@ -83,7 +82,7 @@ void request_draw() {
|
||||
thread_flags_set(thread_id, GUI_THREAD_FLAG_DRAW);
|
||||
}
|
||||
|
||||
void show_app(app::App app, ViewPortShowCallback on_show, ViewPortHideCallback on_hide) {
|
||||
void show_app(app::App& app, ViewPortShowCallback on_show, ViewPortHideCallback on_hide) {
|
||||
lock();
|
||||
tt_check(gui->app_view_port == nullptr);
|
||||
gui->app_view_port = view_port_alloc(app, on_show, on_hide);
|
||||
|
||||
@@ -14,7 +14,7 @@ typedef struct Gui Gui;
|
||||
* @param on_show
|
||||
* @param on_hide
|
||||
*/
|
||||
void show_app(app::App app, ViewPortShowCallback on_show, ViewPortHideCallback on_hide);
|
||||
void show_app(app::App& app, ViewPortShowCallback on_show, ViewPortHideCallback on_hide);
|
||||
|
||||
/**
|
||||
* Hide the current app's viewport.
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace tt::service::gui {
|
||||
|
||||
#define TAG "gui"
|
||||
|
||||
static lv_obj_t* create_app_views(Gui* gui, lv_obj_t* parent, app::App app) {
|
||||
static lv_obj_t* create_app_views(Gui* gui, lv_obj_t* parent, app::App& app) {
|
||||
lvgl::obj_set_style_bg_blacken(parent);
|
||||
|
||||
lv_obj_t* vertical_container = lv_obj_create(parent);
|
||||
@@ -19,8 +19,8 @@ static lv_obj_t* create_app_views(Gui* gui, lv_obj_t* parent, app::App app) {
|
||||
lvgl::obj_set_style_bg_blacken(vertical_container);
|
||||
|
||||
// TODO: Move statusbar into separate ViewPort
|
||||
app::Flags flags = app::tt_app_get_flags(app);
|
||||
if (flags.show_statusbar) {
|
||||
app::Flags flags = app.getFlags();
|
||||
if (flags.showStatusbar) {
|
||||
lvgl::statusbar_create(vertical_container);
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ void redraw(Gui* gui) {
|
||||
|
||||
ViewPort* view_port = gui->app_view_port;
|
||||
if (view_port != nullptr) {
|
||||
app::App app = view_port->app;
|
||||
app::App& app = view_port->app;
|
||||
lv_obj_t* container = create_app_views(gui, gui->lvgl_parent, app);
|
||||
view_port_show(view_port, container);
|
||||
} else {
|
||||
|
||||
@@ -9,35 +9,35 @@ namespace tt::service::gui {
|
||||
#define TAG "viewport"
|
||||
|
||||
ViewPort* view_port_alloc(
|
||||
app::App app,
|
||||
app::App& app,
|
||||
ViewPortShowCallback on_show,
|
||||
ViewPortHideCallback on_hide
|
||||
) {
|
||||
auto* view_port = static_cast<ViewPort*>(malloc(sizeof(ViewPort)));
|
||||
view_port->app = app;
|
||||
view_port->on_show = on_show;
|
||||
view_port->on_hide = on_hide;
|
||||
return view_port;
|
||||
return new ViewPort(
|
||||
app,
|
||||
on_show,
|
||||
on_hide
|
||||
);
|
||||
}
|
||||
|
||||
void view_port_free(ViewPort* view_port) {
|
||||
tt_assert(view_port);
|
||||
free(view_port);
|
||||
delete view_port;
|
||||
}
|
||||
|
||||
void view_port_show(ViewPort* view_port, lv_obj_t* parent) {
|
||||
tt_assert(view_port);
|
||||
tt_assert(parent);
|
||||
if (view_port->on_show) {
|
||||
if (view_port->onShow) {
|
||||
lvgl::obj_set_style_no_padding(parent);
|
||||
view_port->on_show(view_port->app, parent);
|
||||
view_port->onShow(view_port->app, parent);
|
||||
}
|
||||
}
|
||||
|
||||
void view_port_hide(ViewPort* view_port) {
|
||||
tt_assert(view_port);
|
||||
if (view_port->on_hide) {
|
||||
view_port->on_hide(view_port->app);
|
||||
if (view_port->onHide) {
|
||||
view_port->onHide(view_port->app);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,16 +8,21 @@ namespace tt::service::gui {
|
||||
/** ViewPort Draw callback
|
||||
* @warning called from GUI thread
|
||||
*/
|
||||
typedef void (*ViewPortShowCallback)(app::App app, lv_obj_t* parent);
|
||||
typedef void (*ViewPortHideCallback)(app::App app);
|
||||
typedef void (*ViewPortShowCallback)(app::App& app, lv_obj_t* parent);
|
||||
typedef void (*ViewPortHideCallback)(app::App& app);
|
||||
|
||||
// TODO: Move internally, use handle publicly
|
||||
|
||||
typedef struct {
|
||||
app::App app;
|
||||
ViewPortShowCallback on_show;
|
||||
ViewPortHideCallback _Nullable on_hide;
|
||||
bool app_toolbar;
|
||||
typedef struct ViewPort {
|
||||
app::App& app;
|
||||
ViewPortShowCallback onShow;
|
||||
ViewPortHideCallback _Nullable onHide;
|
||||
|
||||
ViewPort(
|
||||
app::App& app,
|
||||
ViewPortShowCallback on_show,
|
||||
ViewPortHideCallback _Nullable on_hide
|
||||
) : app(app), onShow(on_show), onHide(on_hide) {}
|
||||
} ViewPort;
|
||||
|
||||
/** ViewPort allocator
|
||||
@@ -30,7 +35,7 @@ typedef struct {
|
||||
* @return ViewPort instance
|
||||
*/
|
||||
ViewPort* view_port_alloc(
|
||||
app::App app,
|
||||
app::App& app,
|
||||
ViewPortShowCallback on_show,
|
||||
ViewPortHideCallback on_hide
|
||||
);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "Tactility.h"
|
||||
#include <Mutex.h>
|
||||
#include "app/Manifest.h"
|
||||
#include "app/ManifestRegistry.h"
|
||||
#include "service/Manifest.h"
|
||||
@@ -39,8 +40,6 @@ static Loader* loader_alloc() {
|
||||
nullptr
|
||||
);
|
||||
loader_singleton->mutex = tt_mutex_alloc(MutexTypeRecursive);
|
||||
loader_singleton->app_stack_index = -1;
|
||||
memset(loader_singleton->app_stack, 0, sizeof(app::App) * APP_STACK_SIZE);
|
||||
return loader_singleton;
|
||||
}
|
||||
|
||||
@@ -100,14 +99,12 @@ void stop_app() {
|
||||
loader_singleton->queue.put(&message, TtWaitForever);
|
||||
}
|
||||
|
||||
app::App _Nullable get_current_app() {
|
||||
app::App* _Nullable get_current_app() {
|
||||
tt_assert(loader_singleton);
|
||||
loader_lock();
|
||||
app::App app = (loader_singleton->app_stack_index >= 0)
|
||||
? loader_singleton->app_stack[loader_singleton->app_stack_index]
|
||||
: nullptr;
|
||||
app::AppInstance* app = loader_singleton->app_stack.top();
|
||||
loader_unlock();
|
||||
return app;
|
||||
return dynamic_cast<app::App*>(app);
|
||||
}
|
||||
|
||||
PubSub* get_pubsub() {
|
||||
@@ -135,9 +132,9 @@ static const char* app_state_to_string(app::State state) {
|
||||
}
|
||||
}
|
||||
|
||||
static void app_transition_to_state(app::App app, app::State state) {
|
||||
const app::Manifest& manifest = app::tt_app_get_manifest(app);
|
||||
const app::State old_state = app::tt_app_get_state(app);
|
||||
static void app_transition_to_state(app::AppInstance& app, app::State state) {
|
||||
const app::Manifest& manifest = app.getManifest();
|
||||
const app::State old_state = app.getState();
|
||||
|
||||
TT_LOG_I(
|
||||
TAG,
|
||||
@@ -149,42 +146,42 @@ static void app_transition_to_state(app::App app, app::State state) {
|
||||
|
||||
switch (state) {
|
||||
case app::StateInitial:
|
||||
tt_app_set_state(app, app::StateInitial);
|
||||
app.setState(app::StateInitial);
|
||||
break;
|
||||
case app::StateStarted:
|
||||
if (manifest.on_start != nullptr) {
|
||||
manifest.on_start(app);
|
||||
if (manifest.onStart != nullptr) {
|
||||
manifest.onStart(app);
|
||||
}
|
||||
tt_app_set_state(app, app::StateStarted);
|
||||
app.setState(app::StateStarted);
|
||||
break;
|
||||
case app::StateShowing: {
|
||||
LoaderEvent event_showing = {
|
||||
.type = LoaderEventTypeApplicationShowing,
|
||||
.app_showing = {
|
||||
.app = static_cast<app::App*>(app)
|
||||
.app = dynamic_cast<app::App&>(app)
|
||||
}
|
||||
};
|
||||
tt_pubsub_publish(loader_singleton->pubsub_external, &event_showing);
|
||||
tt_app_set_state(app, app::StateShowing);
|
||||
app.setState(app::StateShowing);
|
||||
break;
|
||||
}
|
||||
case app::StateHiding: {
|
||||
LoaderEvent event_hiding = {
|
||||
.type = LoaderEventTypeApplicationHiding,
|
||||
.app_hiding = {
|
||||
.app = static_cast<app::App*>(app)
|
||||
.app = dynamic_cast<app::App&>(app)
|
||||
}
|
||||
};
|
||||
tt_pubsub_publish(loader_singleton->pubsub_external, &event_hiding);
|
||||
tt_app_set_state(app, app::StateHiding);
|
||||
app.setState(app::StateHiding);
|
||||
break;
|
||||
}
|
||||
case app::StateStopped:
|
||||
if (manifest.on_stop) {
|
||||
manifest.on_stop(app);
|
||||
if (manifest.onStop) {
|
||||
manifest.onStop(app);
|
||||
}
|
||||
app::tt_app_set_data(app, nullptr);
|
||||
tt_app_set_state(app, app::StateStopped);
|
||||
app.setData(nullptr);
|
||||
app.setState(app::StateStopped);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -197,27 +194,18 @@ static LoaderStatus loader_do_start_app_with_manifest(
|
||||
|
||||
loader_lock();
|
||||
|
||||
if (loader_singleton->app_stack_index >= (APP_STACK_SIZE - 1)) {
|
||||
TT_LOG_E(TAG, "failed to start app: stack limit of %d reached", APP_STACK_SIZE);
|
||||
return LoaderStatusErrorInternal;
|
||||
}
|
||||
|
||||
int8_t previous_index = loader_singleton->app_stack_index;
|
||||
loader_singleton->app_stack_index++;
|
||||
|
||||
app::App app = tt_app_alloc(*manifest, bundle);
|
||||
tt_check(loader_singleton->app_stack[loader_singleton->app_stack_index] == nullptr);
|
||||
loader_singleton->app_stack[loader_singleton->app_stack_index] = app;
|
||||
app_transition_to_state(app, app::StateInitial);
|
||||
app_transition_to_state(app, app::StateStarted);
|
||||
auto previous_app = !loader_singleton->app_stack.empty() ? loader_singleton->app_stack.top() : nullptr;
|
||||
auto new_app = new app::AppInstance(*manifest, bundle);
|
||||
loader_singleton->app_stack.push(new_app);
|
||||
app_transition_to_state(*new_app, app::StateInitial);
|
||||
app_transition_to_state(*new_app, app::StateStarted);
|
||||
|
||||
// We might have to hide the previous app first
|
||||
if (previous_index != -1) {
|
||||
app::App previous_app = loader_singleton->app_stack[previous_index];
|
||||
app_transition_to_state(previous_app, app::StateHiding);
|
||||
if (previous_app != nullptr) {
|
||||
app_transition_to_state(*previous_app, app::StateHiding);
|
||||
}
|
||||
|
||||
app_transition_to_state(app, app::StateShowing);
|
||||
app_transition_to_state(*new_app, app::StateShowing);
|
||||
|
||||
loader_unlock();
|
||||
|
||||
@@ -227,7 +215,7 @@ static LoaderStatus loader_do_start_app_with_manifest(
|
||||
LoaderEvent event_external = {
|
||||
.type = LoaderEventTypeApplicationStarted,
|
||||
.app_started = {
|
||||
.app = static_cast<app::App*>(app)
|
||||
.app = dynamic_cast<app::App&>(*new_app)
|
||||
}
|
||||
};
|
||||
tt_pubsub_publish(loader_singleton->pubsub_external, &event_external);
|
||||
@@ -241,7 +229,7 @@ static LoaderStatus do_start_by_id(
|
||||
) {
|
||||
TT_LOG_I(TAG, "Start by id %s", id.c_str());
|
||||
|
||||
const app::Manifest* manifest = app::app_manifest_registry_find_by_id(id);
|
||||
const app::Manifest* manifest = app::findAppById(id);
|
||||
if (manifest == nullptr) {
|
||||
return LoaderStatusErrorUnknownApp;
|
||||
} else {
|
||||
@@ -253,38 +241,40 @@ static LoaderStatus do_start_by_id(
|
||||
static void do_stop_app() {
|
||||
loader_lock();
|
||||
|
||||
int8_t current_app_index = loader_singleton->app_stack_index;
|
||||
size_t original_stack_size = loader_singleton->app_stack.size();
|
||||
|
||||
if (current_app_index == -1) {
|
||||
if (original_stack_size == 0) {
|
||||
loader_unlock();
|
||||
TT_LOG_E(TAG, "Stop app: no app running");
|
||||
return;
|
||||
}
|
||||
|
||||
if (current_app_index == 0) {
|
||||
if (original_stack_size == 1) {
|
||||
loader_unlock();
|
||||
TT_LOG_E(TAG, "Stop app: can't stop root app");
|
||||
return;
|
||||
}
|
||||
|
||||
// Stop current app
|
||||
app::App app_to_stop = loader_singleton->app_stack[current_app_index];
|
||||
const app::Manifest& manifest = app::tt_app_get_manifest(app_to_stop);
|
||||
app_transition_to_state(app_to_stop, app::StateHiding);
|
||||
app_transition_to_state(app_to_stop, app::StateStopped);
|
||||
app::AppInstance* app_to_stop = loader_singleton->app_stack.top();
|
||||
const app::Manifest& manifest = app_to_stop->getManifest();
|
||||
app_transition_to_state(*app_to_stop, app::StateHiding);
|
||||
app_transition_to_state(*app_to_stop, app::StateStopped);
|
||||
|
||||
app::tt_app_free(app_to_stop);
|
||||
loader_singleton->app_stack[current_app_index] = nullptr;
|
||||
loader_singleton->app_stack_index--;
|
||||
loader_singleton->app_stack.pop();
|
||||
delete app_to_stop;
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
TT_LOG_I(TAG, "Free heap: %zu", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
|
||||
#endif
|
||||
|
||||
// Resume previous app
|
||||
tt_assert(loader_singleton->app_stack[loader_singleton->app_stack_index] != nullptr);
|
||||
app::App app_to_resume = loader_singleton->app_stack[loader_singleton->app_stack_index];
|
||||
app_transition_to_state(app_to_resume, app::StateShowing);
|
||||
if (original_stack_size > 1) {
|
||||
|
||||
}
|
||||
app::AppInstance* app_to_resume = loader_singleton->app_stack.top();
|
||||
tt_assert(app_to_resume);
|
||||
app_transition_to_state(*app_to_resume, app::StateShowing);
|
||||
|
||||
loader_unlock();
|
||||
|
||||
@@ -294,7 +284,7 @@ static void do_stop_app() {
|
||||
LoaderEvent event_external = {
|
||||
.type = LoaderEventTypeApplicationStopped,
|
||||
.app_stopped = {
|
||||
.manifest = &manifest
|
||||
.manifest = manifest
|
||||
}
|
||||
};
|
||||
tt_pubsub_publish(loader_singleton->pubsub_external, &event_external);
|
||||
|
||||
@@ -24,19 +24,19 @@ typedef enum {
|
||||
} LoaderEventType;
|
||||
|
||||
typedef struct {
|
||||
app::App* app;
|
||||
app::App& app;
|
||||
} LoaderEventAppStarted;
|
||||
|
||||
typedef struct {
|
||||
app::App* app;
|
||||
app::App& app;
|
||||
} LoaderEventAppShowing;
|
||||
|
||||
typedef struct {
|
||||
app::App* app;
|
||||
app::App& app;
|
||||
} LoaderEventAppHiding;
|
||||
|
||||
typedef struct {
|
||||
const app::Manifest* manifest;
|
||||
const app::Manifest& manifest;
|
||||
} LoaderEventAppStopped;
|
||||
|
||||
typedef struct {
|
||||
@@ -63,7 +63,7 @@ LoaderStatus start_app(const std::string& id, bool blocking, const Bundle& bundl
|
||||
*/
|
||||
void stop_app();
|
||||
|
||||
app::App _Nullable get_current_app();
|
||||
app::App* _Nullable get_current_app();
|
||||
|
||||
/**
|
||||
* @brief PubSub for LoaderEvent
|
||||
|
||||
@@ -98,9 +98,9 @@ static int32_t screenshot_task(void* context) {
|
||||
break; // Interrupted loop
|
||||
}
|
||||
} else if (data->work.type == TASK_WORK_TYPE_APPS) {
|
||||
app::App _Nullable app = loader::get_current_app();
|
||||
app::App* _Nullable app = loader::get_current_app();
|
||||
if (app) {
|
||||
const app::Manifest& manifest = app::tt_app_get_manifest(app);
|
||||
const app::Manifest& manifest = app->getManifest();
|
||||
if (manifest.id != last_app_id) {
|
||||
delay_ms(100);
|
||||
last_app_id = manifest.id;
|
||||
@@ -125,7 +125,7 @@ static int32_t screenshot_task(void* context) {
|
||||
|
||||
static void task_start(ScreenshotTaskData* data) {
|
||||
task_lock(data);
|
||||
tt_check(data->thread == NULL);
|
||||
tt_check(data->thread == nullptr);
|
||||
data->thread = new Thread(
|
||||
"screenshot",
|
||||
8192,
|
||||
|
||||
Reference in New Issue
Block a user