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
+4 -14
View File
@@ -1,4 +1,5 @@
#include "tt_app_context.h"
#include <app/App.h>
#include <app/AppContext.h>
struct AppContextDataWrapper {
@@ -9,28 +10,17 @@ extern "C" {
#define HANDLE_AS_APP_CONTEXT(handle) ((tt::app::AppContext*)(handle))
void* _Nullable tt_app_context_get_data(AppContextHandle handle) {
auto wrapper = std::reinterpret_pointer_cast<AppContextDataWrapper>(HANDLE_AS_APP_CONTEXT(handle)->getData());
return wrapper ? wrapper->data : nullptr;
}
void tt_app_context_set_data(AppContextHandle handle, void* _Nullable data) {
auto wrapper = std::make_shared<AppContextDataWrapper>();
wrapper->data = data;
HANDLE_AS_APP_CONTEXT(handle)->setData(std::move(wrapper));
}
BundleHandle _Nullable tt_app_context_get_parameters(AppContextHandle handle) {
return (BundleHandle)HANDLE_AS_APP_CONTEXT(handle)->getParameters().get();
}
void tt_app_context_set_result(AppContextHandle handle, Result result, BundleHandle _Nullable bundle) {
auto shared_bundle = std::shared_ptr<tt::Bundle>((tt::Bundle*)bundle);
HANDLE_AS_APP_CONTEXT(handle)->setResult((tt::app::Result)result, std::move(shared_bundle));
auto shared_bundle = std::unique_ptr<tt::Bundle>((tt::Bundle*)bundle);
HANDLE_AS_APP_CONTEXT(handle)->getApp()->setResult((tt::app::Result)result, std::move(shared_bundle));
}
bool tt_app_context_has_result(AppContextHandle handle) {
return HANDLE_AS_APP_CONTEXT(handle)->hasResult();
return HANDLE_AS_APP_CONTEXT(handle)->getApp()->hasResult();
}
}
-8
View File
@@ -11,14 +11,6 @@ typedef void* AppContextHandle;
/** @return the data that was attached to this app context */
void* _Nullable tt_app_context_get_data(AppContextHandle handle);
/**
* Attach data to an application context.
* Don't forget to manually delete allocated memory when onStopped() is called.
* @param[in] handle the app context handle
* @param[in] data the data to attach
*/
void tt_app_context_set_data(AppContextHandle handle, void* _Nullable data);
/** @return the bundle that belongs to this application, or null */
BundleHandle _Nullable tt_app_context_get_parameters(AppContextHandle handle);
+15 -91
View File
@@ -1,105 +1,29 @@
#include "tt_app_manifest.h"
#include <Check.h>
#include <Log.h>
#include <app/ElfApp.h>
#include <app/AppCompatC.h>
#define TAG "tt_app"
AppOnStart elfOnStart = nullptr;
AppOnStop elfOnStop = nullptr;
AppOnShow elfOnShow = nullptr;
AppOnHide elfOnHide = nullptr;
AppOnResult elfOnResult = nullptr;
static void onStartWrapper(tt::app::AppContext& context) {
if (elfOnStart != nullptr) {
TT_LOG_I(TAG, "onStartWrapper");
elfOnStart(&context);
} else {
TT_LOG_W(TAG, "onStartWrapper not set");
}
}
static void onStopWrapper(tt::app::AppContext& context) {
if (elfOnStop != nullptr) {
TT_LOG_I(TAG, "onStopWrapper");
elfOnStop(&context);
} else {
TT_LOG_W(TAG, "onStopWrapper not set");
}
}
static void onShowWrapper(tt::app::AppContext& context, lv_obj_t* parent) {
if (elfOnShow != nullptr) {
TT_LOG_I(TAG, "onShowWrapper");
elfOnShow(&context, parent);
} else {
TT_LOG_W(TAG, "onShowWrapper not set");
}
}
static void onHideWrapper(tt::app::AppContext& context) {
if (elfOnHide != nullptr) {
TT_LOG_I(TAG, "onHideWrapper");
elfOnHide(&context);
} else {
TT_LOG_W(TAG, "onHideWrapper not set");
}
}
static void onResultWrapper(tt::app::AppContext& context, tt::app::Result result, const tt::Bundle& resultData) {
if (elfOnResult != nullptr) {
TT_LOG_I(TAG, "onResultWrapper");
Result convertedResult = AppResultError;
switch (result) {
case tt::app::ResultOk:
convertedResult = AppResultOk;
break;
case tt::app::ResultCancelled:
convertedResult = AppResultCancelled;
break;
case tt::app::ResultError:
convertedResult = AppResultError;
break;
}
elfOnResult(&context, convertedResult, (BundleHandle)&resultData);
} else {
TT_LOG_W(TAG, "onResultWrapper not set");
}
}
tt::app::AppManifest manifest = {
.id = "ElfWrapperInTactilityC",
.name = "",
.icon = "",
.onStart = onStartWrapper,
.onStop = onStopWrapper,
.onShow = onShowWrapper,
.onHide = onHideWrapper,
.onResult = onResultWrapper
};
extern "C" {
void tt_set_app_manifest(
const char* name,
const char* _Nullable icon,
AppOnStart onStart,
AppOnStop _Nullable onStop,
AppOnShow _Nullable onShow,
AppOnHide _Nullable onHide,
AppOnResult _Nullable onResult
void tt_app_register(
const ExternalAppManifest* manifest
) {
#ifdef ESP_PLATFORM
manifest.name = name;
manifest.icon = icon ? icon : "";
elfOnStart = onStart;
elfOnStop = onStop;
elfOnShow = onShow;
elfOnHide = onHide;
elfOnResult = onResult;
tt::app::setElfAppManifest(manifest);
tt_assert((manifest->createData == nullptr) == (manifest->destroyData == nullptr));
tt::app::setElfAppManifest(
manifest->name,
manifest->icon,
(tt::app::CreateData)manifest->createData,
(tt::app::DestroyData)manifest->destroyData,
(tt::app::OnStart)manifest->onStart,
(tt::app::OnStop)manifest->onStop,
(tt::app::OnShow)manifest->onShow,
(tt::app::OnHide)manifest->onHide,
(tt::app::OnResult)manifest->onResult
);
#else
tt_crash("TactilityC is intended for PC/Simulator");
#endif
+35 -27
View File
@@ -7,39 +7,47 @@
extern "C" {
#endif
/** Important: These values must map to tt::app::Result values exactly */
typedef enum {
AppResultOk,
AppResultCancelled,
AppResultError
AppResultOk = 0,
AppResultCancelled = 1,
AppResultError = 2
} Result;
typedef void* AppContextHandle;
typedef void (*AppOnStart)(AppContextHandle app);
typedef void (*AppOnStop)(AppContextHandle app);
typedef void (*AppOnShow)(AppContextHandle app, lv_obj_t* parent);
typedef void (*AppOnHide)(AppContextHandle app);
typedef void (*AppOnResult)(AppContextHandle app, Result result, BundleHandle resultData);
/** Important: These function types must map to t::app types exactly */
typedef void* (*AppCreateData)();
typedef void (*AppDestroyData)(void* data);
typedef void (*AppOnStart)(AppContextHandle app, void* _Nullable data);
typedef void (*AppOnStop)(AppContextHandle app, void* _Nullable data);
typedef void (*AppOnShow)(AppContextHandle app, void* _Nullable data, lv_obj_t* parent);
typedef void (*AppOnHide)(AppContextHandle app, void* _Nullable data);
typedef void (*AppOnResult)(AppContextHandle app, void* _Nullable data, Result result, BundleHandle resultData);
/**
* This is used to register the manifest of an external app.
* @param[in] name the application's human-readable name
* @param[in] icon the optional application icon (you can use LV_SYMBOL_* too)
* @param[in] onStart called when the app is launched (started)
* @param[in] onStop called when the app is exited (stopped)
* @param[in] onShow called when the app is about to be shown to the user (app becomes visible)
* @param[in] onHide called when the app is about to be invisible to the user (e.g. other app was launched by this app, and this app goes to the background)
* @param[in] onResult called when the app receives a result after launching another app
*/
void tt_set_app_manifest(
const char* name,
const char* _Nullable icon,
AppOnStart onStart,
AppOnStop _Nullable onStop,
AppOnShow _Nullable onShow,
AppOnHide _Nullable onHide,
AppOnResult _Nullable onResult
);
typedef struct {
/** The application's human-readable name */
const char* name;
/** The application icon (you can use LV_SYMBOL_* too) */
const char* _Nullable icon;
/** The application can allocate data to re-use later (e.g. struct with state) */
AppCreateData _Nullable createData;
/** If createData is specified, this one must be specified too */
AppDestroyData _Nullable destroyData;
/** Called when the app is launched (started) */
AppOnStart _Nullable onStart;
/** Called when the app is exited (stopped) */
AppOnStop _Nullable onStop;
/** Called when the app is about to be shown to the user (app becomes visible) */
AppOnShow _Nullable onShow;
/** Called when the app is about to be invisible to the user (e.g. other app was launched by this app, and this app goes to the background) */
AppOnHide _Nullable onHide;
/** Called when the app receives a result after launching another app */
AppOnResult _Nullable onResult;
} ExternalAppManifest;
/** This is used to register the manifest of an external app. */
void tt_app_register(const ExternalAppManifest* manifest);
#ifdef __cplusplus
}
+1 -3
View File
@@ -22,8 +22,7 @@ extern "C" {
const struct esp_elfsym elf_symbols[] {
// Tactility
ESP_ELFSYM_EXPORT(tt_app_context_get_data),
ESP_ELFSYM_EXPORT(tt_app_context_set_data),
ESP_ELFSYM_EXPORT(tt_app_register),
ESP_ELFSYM_EXPORT(tt_app_context_get_parameters),
ESP_ELFSYM_EXPORT(tt_app_context_set_result),
ESP_ELFSYM_EXPORT(tt_app_context_has_result),
@@ -39,7 +38,6 @@ const struct esp_elfsym elf_symbols[] {
ESP_ELFSYM_EXPORT(tt_bundle_put_bool),
ESP_ELFSYM_EXPORT(tt_bundle_put_int32),
ESP_ELFSYM_EXPORT(tt_bundle_put_string),
ESP_ELFSYM_EXPORT(tt_set_app_manifest),
ESP_ELFSYM_EXPORT(tt_hal_i2c_start),
ESP_ELFSYM_EXPORT(tt_hal_i2c_stop),
ESP_ELFSYM_EXPORT(tt_hal_i2c_is_started),
+1 -1
View File
@@ -14,7 +14,7 @@ void tt_service_loader_stop_app() {
}
AppContextHandle tt_service_loader_get_current_app() {
return tt::service::loader::getCurrentApp();
return tt::service::loader::getCurrentAppContext().get();
}
}