Refactor app launching (#174)

- Refactor the way apps work: Instead of a C interface, they are now C++ classes. The main reasoning is that attaching data to an app was cumbersome. Having different implementations for different kinds of apps was cumbersome too. (3 or 4 layers of manifest nesting for the TactilityC project)
- External apps are still written in C, but they get a createData/destroyData in their manifest, so:
- External apps now have their own manifest.
- All functions in the original AppManifest are removed and replaced by a single `createApp` function
- External apps now automatically register (each app individually!) when they run the first time. As a side-effect they become visible in the `AppList` app!
- Adapted all apps for the new interface.
- Adapted all internal logic for these changes (Gui, ViewPort, Loader, AppContext, AppInstance, etc.)
- Rewrote parts of Loader to use std::shared_ptr to make the code much safer.
- Added a refcount check for the `AppInstance` and `App` at the end of their lifecycle. Show warning if refcount is too high.
This commit is contained in:
Ken Van Hoeylandt
2025-01-21 17:48:32 +01:00
committed by GitHub
parent 2bbd44a8b5
commit c3bcf93698
73 changed files with 2561 additions and 2581 deletions
+8 -11
View File
@@ -19,9 +19,8 @@ Gui* gui = nullptr;
void onLoaderMessage(const void* message, TT_UNUSED void* context) {
auto* event = static_cast<const loader::LoaderEvent*>(message);
if (event->type == loader::LoaderEventTypeApplicationShowing) {
app::AppContext& app = event->app_showing.app;
const app::AppManifest& app_manifest = app.getManifest();
showApp(app, app_manifest.onShow, app_manifest.onHide);
auto app_instance = service::loader::getCurrentAppContext();
showApp(app_instance);
} else if (event->type == loader::LoaderEventTypeApplicationHiding) {
hideApp();
}
@@ -94,27 +93,25 @@ void requestDraw() {
thread_flags_set(thread_id, GUI_THREAD_FLAG_DRAW);
}
void showApp(app::AppContext& app, ViewPortShowCallback on_show, ViewPortHideCallback on_hide) {
void showApp(std::shared_ptr<app::AppContext> app) {
lock();
tt_check(gui->appViewPort == nullptr);
gui->appViewPort = view_port_alloc(app, on_show, on_hide);
tt_check(gui->appToRender == nullptr);
gui->appToRender = std::move(app);
unlock();
requestDraw();
}
void hideApp() {
lock();
ViewPort* view_port = gui->appViewPort;
tt_check(view_port != nullptr);
tt_check(gui->appToRender != nullptr);
// We must lock the LVGL port, because the viewport hide callbacks
// might call LVGL APIs (e.g. to remove the keyboard from the screen root)
tt_check(lvgl::lock(configTICK_RATE_HZ));
view_port_hide(view_port);
gui->appToRender->getApp()->onHide(*gui->appToRender);
lvgl::unlock();
view_port_free(view_port);
gui->appViewPort = nullptr;
gui->appToRender = nullptr;
unlock();
}
+2 -4
View File
@@ -1,7 +1,7 @@
#pragma once
#include "app/AppInstance.h"
#include "app/AppContext.h"
#include "ViewPort.h"
namespace tt::service::gui {
@@ -10,10 +10,8 @@ typedef struct Gui Gui;
/**
* Set the app viewport in the gui state and request the gui to draw it.
* @param[in] app
* @param[in] onShow
* @param[in] onHide
*/
void showApp(app::AppContext& app, ViewPortShowCallback onShow, ViewPortHideCallback onHide);
void showApp(std::shared_ptr<app::AppContext> app);
/**
* Hide the current app's viewport.
+6 -7
View File
@@ -9,9 +9,10 @@ namespace tt::service::gui {
#define TAG "gui"
static lv_obj_t* createAppViews(Gui* gui, lv_obj_t* parent, app::AppContext& app) {
static lv_obj_t* createAppViews(Gui* gui, lv_obj_t* parent) {
lv_obj_send_event(gui->statusbarWidget, LV_EVENT_DRAW_MAIN, nullptr);
lv_obj_t* child_container = lv_obj_create(parent);
lv_obj_set_style_pad_all(child_container, 0, 0);
lv_obj_set_width(child_container, LV_PCT(100));
lv_obj_set_flex_grow(child_container, 1);
@@ -34,19 +35,17 @@ void redraw(Gui* gui) {
if (lvgl::lock(1000)) {
lv_obj_clean(gui->appRootWidget);
ViewPort* view_port = gui->appViewPort;
if (view_port != nullptr) {
app::AppContext& app = view_port->app;
if (gui->appToRender != nullptr) {
app::Flags flags = app.getFlags();
app::Flags flags = std::static_pointer_cast<app::AppInstance>(gui->appToRender)->getFlags();
if (flags.showStatusbar) {
lv_obj_remove_flag(gui->statusbarWidget, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_add_flag(gui->statusbarWidget, LV_OBJ_FLAG_HIDDEN);
}
lv_obj_t* container = createAppViews(gui, gui->appRootWidget, app);
view_port_show(view_port, container);
lv_obj_t* container = createAppViews(gui, gui->appRootWidget);
gui->appToRender->getApp()->onShow(*gui->appToRender, container);
} else {
TT_LOG_W(TAG, "nothing to draw");
}
-44
View File
@@ -1,44 +0,0 @@
#include "ViewPort.h"
#include "Check.h"
#include "service/gui/ViewPort_i.h"
#include "lvgl/Style.h"
namespace tt::service::gui {
#define TAG "viewport"
ViewPort* view_port_alloc(
app::AppContext& app,
ViewPortShowCallback on_show,
ViewPortHideCallback on_hide
) {
return new ViewPort(
app,
on_show,
on_hide
);
}
void view_port_free(ViewPort* view_port) {
tt_assert(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->onShow) {
lvgl::obj_set_style_no_padding(parent);
view_port->onShow(view_port->app, parent);
}
}
void view_port_hide(ViewPort* view_port) {
tt_assert(view_port);
if (view_port->onHide) {
view_port->onHide(view_port->app);
}
}
} // namespace
-47
View File
@@ -1,47 +0,0 @@
#pragma once
#include "app/AppContext.h"
#include "lvgl.h"
namespace tt::service::gui {
/** ViewPort Draw callback
* @warning called from GUI thread
*/
typedef void (*ViewPortShowCallback)(app::AppContext& app, lv_obj_t* parent);
typedef void (*ViewPortHideCallback)(app::AppContext& app);
// TODO: Move internally, use handle publicly
typedef struct ViewPort {
app::AppContext& app;
ViewPortShowCallback onShow;
ViewPortHideCallback _Nullable onHide;
ViewPort(
app::AppContext& app,
ViewPortShowCallback on_show,
ViewPortHideCallback _Nullable on_hide
) : app(app), onShow(on_show), onHide(on_hide) {}
} ViewPort;
/** ViewPort allocator
* always returns view_port or stops system if not enough memory.
* @param app
* @param onShow Called to create LVGL widgets
* @param onHide Called before clearing the LVGL widget parent
* @return ViewPort instance
*/
ViewPort* view_port_alloc(
app::AppContext& app,
ViewPortShowCallback onShow,
ViewPortHideCallback onHide
);
/** ViewPort destruction
* Ensure that view_port was unregistered in GUI system before use.
* @param viewPort ViewPort instance
*/
void view_port_free(ViewPort* viewPort);
} // namespace