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
+74 -88
View File
@@ -6,8 +6,9 @@
#include "RtosCompat.h"
#ifdef ESP_PLATFORM
#include "esp_heap_caps.h"
#include "TactilityHeadless.h"
#include "app/ElfApp.h"
#include "esp_heap_caps.h"
#else
#include "lvgl/LvglSync.h"
@@ -61,17 +62,22 @@ void stopApp() {
loader_singleton->dispatcherThread->dispatch(onStopAppMessage, nullptr);
}
app::AppContext* _Nullable getCurrentApp() {
std::shared_ptr<app::AppContext> _Nullable getCurrentAppContext() {
tt_assert(loader_singleton);
if (loader_singleton->mutex.lock(10 / portTICK_PERIOD_MS)) {
app::AppInstance* app = loader_singleton->appStack.top();
auto app = loader_singleton->appStack.top();
loader_singleton->mutex.unlock();
return dynamic_cast<app::AppContext*>(app);
return std::move(app);
} else {
return nullptr;
}
}
std::shared_ptr<app::App> _Nullable getCurrentApp() {
auto app_context = getCurrentAppContext();
return app_context != nullptr ? app_context->getApp() : nullptr;
}
std::shared_ptr<PubSub> getPubsub() {
tt_assert(loader_singleton);
// it's safe to return pubsub without locking
@@ -97,9 +103,9 @@ static const char* appStateToString(app::State state) {
}
}
static void transitionAppToState(app::AppInstance& app, app::State state) {
const app::AppManifest& manifest = app.getManifest();
const app::State old_state = app.getState();
static void transitionAppToState(std::shared_ptr<app::AppInstance> app, app::State state) {
const app::AppManifest& manifest = app->getManifest();
const app::State old_state = app->getState();
TT_LOG_I(
TAG,
@@ -111,49 +117,35 @@ static void transitionAppToState(app::AppInstance& app, app::State state) {
switch (state) {
case app::StateInitial:
app.setState(app::StateInitial);
app->setState(app::StateInitial);
break;
case app::StateStarted:
if (manifest.onStart != nullptr) {
manifest.onStart(app);
}
app.setState(app::StateStarted);
app->getApp()->onStart(*app);
app->setState(app::StateStarted);
break;
case app::StateShowing: {
LoaderEvent event_showing = {
.type = LoaderEventTypeApplicationShowing,
.app_showing = {
.app = app
}
};
LoaderEvent event_showing = { .type = LoaderEventTypeApplicationShowing };
tt_pubsub_publish(loader_singleton->pubsubExternal, &event_showing);
app.setState(app::StateShowing);
app->setState(app::StateShowing);
break;
}
case app::StateHiding: {
LoaderEvent event_hiding = {
.type = LoaderEventTypeApplicationHiding,
.app_hiding = {
.app = app
}
};
LoaderEvent event_hiding = { .type = LoaderEventTypeApplicationHiding };
tt_pubsub_publish(loader_singleton->pubsubExternal, &event_hiding);
app.setState(app::StateHiding);
app->setState(app::StateHiding);
break;
}
case app::StateStopped:
if (manifest.onStop) {
manifest.onStop(app);
}
app.setData(nullptr);
app.setState(app::StateStopped);
// TODO: Verify manifest
app->getApp()->onStop(*app);
app->setState(app::StateStopped);
break;
}
}
static LoaderStatus startAppWithManifestInternal(
const app::AppManifest* manifest,
std::shared_ptr<const Bundle> _Nullable parameters
const std::shared_ptr<app::AppManifest>& manifest,
const std::shared_ptr<const Bundle> _Nullable& parameters
) {
tt_check(loader_singleton != nullptr);
@@ -165,29 +157,23 @@ static LoaderStatus startAppWithManifestInternal(
}
auto previous_app = !loader_singleton->appStack.empty() ? loader_singleton->appStack.top() : nullptr;
auto new_app = new app::AppInstance(*manifest, parameters);
new_app->mutableFlags().showStatusbar = (manifest->type != app::TypeBoot);
auto new_app = std::make_shared<app::AppInstance>(manifest, parameters);
new_app->mutableFlags().showStatusbar = (manifest->type != app::Type::Boot);
loader_singleton->appStack.push(new_app);
transitionAppToState(*new_app, app::StateInitial);
transitionAppToState(*new_app, app::StateStarted);
transitionAppToState(new_app, app::StateInitial);
transitionAppToState(new_app, app::StateStarted);
// We might have to hide the previous app first
if (previous_app != nullptr) {
transitionAppToState(*previous_app, app::StateHiding);
transitionAppToState(previous_app, app::StateHiding);
}
transitionAppToState(*new_app, app::StateShowing);
transitionAppToState(new_app, app::StateShowing);
LoaderEventInternal event_internal = {.type = LoaderEventTypeApplicationStarted};
tt_pubsub_publish(loader_singleton->pubsubInternal, &event_internal);
LoaderEvent event_external = {
.type = LoaderEventTypeApplicationStarted,
.app_started = {
.app = *new_app
}
};
LoaderEvent event_external = { .type = LoaderEventTypeApplicationStarted };
tt_pubsub_publish(loader_singleton->pubsubExternal, &event_external);
return LoaderStatus::Ok;
@@ -208,7 +194,7 @@ static LoaderStatus startAppInternal(
) {
TT_LOG_I(TAG, "Start by id %s", id.c_str());
const app::AppManifest* manifest = app::findAppById(id);
auto manifest = app::findAppById(id);
if (manifest == nullptr) {
TT_LOG_E(TAG, "App not found: %s", id.c_str());
return LoaderStatus::ErrorUnknownApp;
@@ -233,75 +219,75 @@ static void stopAppInternal() {
}
// Stop current app
app::AppInstance* app_to_stop = loader_singleton->appStack.top();
auto app_to_stop = loader_singleton->appStack.top();
if (original_stack_size == 1 && app_to_stop->getManifest().type != app::TypeBoot) {
if (original_stack_size == 1 && app_to_stop->getManifest().type != app::Type::Boot) {
TT_LOG_E(TAG, "Stop app: can't stop root app");
return;
}
auto result_holder = std::move(app_to_stop->getResult());
bool result_set = false;
app::Result result;
std::unique_ptr<Bundle> result_bundle;
if (app_to_stop->getApp()->moveResult(result, result_bundle)) {
result_set = true;
}
const app::AppManifest& manifest = app_to_stop->getManifest();
transitionAppToState(*app_to_stop, app::StateHiding);
transitionAppToState(*app_to_stop, app::StateStopped);
transitionAppToState(app_to_stop, app::StateHiding);
transitionAppToState(app_to_stop, app::StateStopped);
loader_singleton->appStack.pop();
delete app_to_stop;
// We only expect the app to be referenced within the current scope
if (app_to_stop.use_count() > 1) {
TT_LOG_W(TAG, "Memory leak: Stopped %s, but use count is %ld", app_to_stop->getManifest().id.c_str(), app_to_stop.use_count() - 1);
}
// Refcount is expected to be 2: 1 within app_to_stop and 1 within the current scope
if (app_to_stop->getApp().use_count() > 2) {
TT_LOG_W(TAG, "Memory leak: Stopped %s, but use count is %ld", app_to_stop->getManifest().id.c_str(), app_to_stop->getApp().use_count() - 2);
}
#ifdef ESP_PLATFORM
TT_LOG_I(TAG, "Free heap: %zu", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
#endif
app::AppOnResult on_result = nullptr;
app::AppInstance* app_to_resume = nullptr;
std::shared_ptr<app::AppInstance> instance_to_resume;
// If there's a previous app, resume it
if (!loader_singleton->appStack.empty()) {
app_to_resume = loader_singleton->appStack.top();
tt_assert(app_to_resume);
transitionAppToState(*app_to_resume, app::StateShowing);
on_result = app_to_resume->getManifest().onResult;
instance_to_resume = loader_singleton->appStack.top();
tt_assert(instance_to_resume);
transitionAppToState(instance_to_resume, app::StateShowing);
}
// Unlock so that we can send results to app and they can also start/stop new apps while processing these results
scoped_lock->unlock();
// WARNING: After this point we cannot change the app states from this method directly anymore as we don't have a lock!
LoaderEventInternal event_internal = {.type = LoaderEventTypeApplicationStopped};
tt_pubsub_publish(loader_singleton->pubsubInternal, &event_internal);
LoaderEvent event_external = {
.type = LoaderEventTypeApplicationStopped,
.app_stopped = {
.manifest = manifest
}
};
LoaderEvent event_external = { .type = LoaderEventTypeApplicationStopped };
tt_pubsub_publish(loader_singleton->pubsubExternal, &event_external);
if (on_result != nullptr && app_to_resume != nullptr) {
if (result_holder != nullptr) {
auto result_bundle = result_holder->resultData.get();
if (instance_to_resume != nullptr) {
if (result_set) {
if (result_bundle != nullptr) {
on_result(
*app_to_resume,
result_holder->result,
*result_bundle
instance_to_resume->getApp()->onResult(
*instance_to_resume,
result,
std::move(result_bundle)
);
} else {
const Bundle empty_bundle;
on_result(
*app_to_resume,
result_holder->result,
empty_bundle
instance_to_resume->getApp()->onResult(
*instance_to_resume,
result,
nullptr
);
}
} else {
const Bundle empty_bundle;
on_result(
*app_to_resume,
app::ResultCancelled,
empty_bundle
instance_to_resume->getApp()->onResult(
*instance_to_resume,
app::Result::Cancelled,
nullptr
);
}
}