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
+36 -31
View File
@@ -1,9 +1,10 @@
#pragma once
#include "app/AppContext.h"
#include "app/AppManifest.h"
#include "Bundle.h"
#include "Mutex.h"
#include "app/AppContext.h"
#include "app/AppManifest.h"
#include "app/ElfApp.h"
#include <memory>
#include <utility>
@@ -17,19 +18,6 @@ typedef enum {
StateStopped // App is not in memory
} State;
struct ResultHolder {
Result result;
std::shared_ptr<const Bundle> resultData;
explicit ResultHolder(Result result) : result(result), resultData(nullptr) {}
ResultHolder(Result result, std::shared_ptr<const Bundle> resultData) :
result(result),
resultData(std::move(resultData))
{}
};
/**
* Thread-safe app instance.
*/
@@ -38,7 +26,7 @@ class AppInstance : public AppContext {
private:
Mutex mutex = Mutex(Mutex::Type::Normal);
const AppManifest& manifest;
const std::shared_ptr<AppManifest> manifest;
State state = StateInitial;
Flags flags = { .showStatusbar = true };
/** @brief Optional parameters to start the app with
@@ -52,16 +40,40 @@ private:
* These manifest methods can optionally allocate/free data that is attached here.
*/
std::shared_ptr<void> _Nullable data;
std::unique_ptr<ResultHolder> _Nullable resultHolder;
std::shared_ptr<App> app;
static std::shared_ptr<app::App> createApp(
const std::shared_ptr<app::AppManifest>& manifest
) {
if (manifest->location.isInternal()) {
tt_assert(manifest->createApp != nullptr);
return manifest->createApp();
} else if (manifest->location.isExternal()) {
if (manifest->createApp != nullptr) {
TT_LOG_W("", "Manifest specifies createApp, but this is not used with external apps");
}
#ifdef ESP_PLATFORM
return app::createElfApp(manifest);
#else
tt_crash("not supported");
#endif
} else {
tt_crash("not implemented");
}
}
public:
explicit AppInstance(const AppManifest& manifest) :
manifest(manifest) {}
AppInstance(const AppManifest& manifest, std::shared_ptr<const Bundle> parameters) :
explicit AppInstance(const std::shared_ptr<AppManifest>& manifest) :
manifest(manifest),
parameters(std::move(parameters)) {}
app(createApp(manifest))
{}
AppInstance(const std::shared_ptr<AppManifest>& manifest, std::shared_ptr<const Bundle> parameters) :
manifest(manifest),
parameters(std::move(parameters)),
app(createApp(manifest)) {}
~AppInstance() override = default;
@@ -70,22 +82,15 @@ public:
const AppManifest& getManifest() const override;
Flags getFlags() const override;
Flags getFlags() const;
void setFlags(Flags flags);
Flags& mutableFlags() { return flags; } // TODO: locking mechanism
std::shared_ptr<void> _Nullable getData() const override;
void setData(std::shared_ptr<void> data) override;
std::shared_ptr<const Bundle> getParameters() const override;
void setResult(Result result) override;
void setResult(Result result, std::shared_ptr<const Bundle> bundle) override;
bool hasResult() const override;
std::unique_ptr<Paths> getPaths() const override;
std::unique_ptr<ResultHolder>& getResult() { return resultHolder; }
std::shared_ptr<App> getApp() const override { return app; }
};
} // namespace
@@ -1,34 +0,0 @@
#pragma once
#include "./View.h"
#include "./State.h"
#include "app/AppManifest.h"
#include <lvgl.h>
#include <dirent.h>
#include <memory>
namespace tt::app::files {
class Files {
std::unique_ptr<View> view;
std::shared_ptr<State> state;
public:
Files() {
state = std::make_shared<State>();
view = std::make_unique<View>(state);
}
void onShow(lv_obj_t* parent) {
view->init(parent);
}
void onResult(Result result, const Bundle& bundle) {
view->onResult(result, bundle);
}
};
} // namespace
+1 -1
View File
@@ -35,7 +35,7 @@ public:
void onRenamePressed();
void onDeletePressed();
void onDirEntryListScrollBegin();
void onResult(Result result, const Bundle& bundle);
void onResult(Result result, std::unique_ptr<Bundle> bundle);
};
}
@@ -19,17 +19,7 @@ enum ScanState {
};
struct Data {
// Core
Mutex mutex = Mutex(Mutex::Type::Recursive);
std::unique_ptr<Timer> scanTimer = nullptr;
// State
ScanState scanState;
i2c_port_t port = I2C_NUM_0;
std::vector<uint8_t> scannedAddresses;
// Widgets
lv_obj_t* scanButtonLabelWidget = nullptr;
lv_obj_t* portDropdownWidget = nullptr;
lv_obj_t* scanListWidget = nullptr;
};
void onScanTimerFinished(std::shared_ptr<Data> data);
@@ -1,42 +0,0 @@
#include "Timer.h"
#include "TactilityConfig.h"
#if TT_FEATURE_SCREENSHOT_ENABLED
#pragma once
#include "app/AppContext.h"
#include "lvgl.h"
namespace tt::app::screenshot {
class ScreenshotUi {
lv_obj_t* modeDropdown = nullptr;
lv_obj_t* pathTextArea = nullptr;
lv_obj_t* startStopButtonLabel = nullptr;
lv_obj_t* timerWrapper = nullptr;
lv_obj_t* delayTextArea = nullptr;
std::unique_ptr<Timer> updateTimer;
void createTimerSettingsWidgets(lv_obj_t* parent);
void createModeSettingWidgets(lv_obj_t* parent);
void createFilePathWidgets(lv_obj_t* parent);
void updateScreenshotMode();
public:
ScreenshotUi();
~ScreenshotUi();
void createWidgets(const AppContext& app, lv_obj_t* parent);
void onStartPressed();
void onModeSet();
void onTimerTick();
};
} // namespace
#endif
@@ -1,5 +1,6 @@
#pragma once
#include "app/App.h"
#include "app/wificonnect/Bindings.h"
#include "app/wificonnect/State.h"
#include "app/wificonnect/View.h"
@@ -9,7 +10,10 @@
namespace tt::app::wificonnect {
class WifiConnect {
class WifiConnect : public App {
private:
Mutex mutex;
State state;
Bindings bindings = {
@@ -28,8 +32,8 @@ public:
void lock();
void unlock();
void onShow(AppContext& app, lv_obj_t* parent);
void onHide(AppContext& app);
void onShow(AppContext& app, lv_obj_t* parent) override;
void onHide(AppContext& app) override;
State& getState() { return state; }
Bindings& getBindings() { return bindings; }
@@ -1,5 +1,6 @@
#pragma once
#include "app/App.h"
#include "Mutex.h"
#include "./View.h"
#include "./State.h"
@@ -7,7 +8,9 @@
namespace tt::app::wifimanage {
class WifiManage {
class WifiManage : public App {
private:
PubSubSubscription* wifiSubscription = nullptr;
Mutex mutex;
@@ -23,8 +26,8 @@ public:
void lock();
void unlock();
void onShow(AppContext& app, lv_obj_t* parent);
void onHide(AppContext& app);
void onShow(AppContext& app, lv_obj_t* parent) override;
void onHide(AppContext& app) override;
Bindings& getBindings() { return bindings; }
State& getState() { return state; }
+2 -3
View File
@@ -4,9 +4,8 @@
#include "Mutex.h"
#include "Pubsub.h"
#include "service/gui/Gui.h"
#include "service/gui/ViewPort.h"
#include "service/gui/ViewPort_i.h"
#include <cstdio>
#include <lvgl.h>
namespace tt::service::gui {
@@ -27,7 +26,7 @@ struct Gui {
lv_obj_t* statusbarWidget = nullptr;
// App-specific
ViewPort* appViewPort = nullptr;
std::shared_ptr<app::AppContext> appToRender = nullptr;
lv_obj_t* _Nullable keyboard = nullptr;
lv_group_t* keyboardGroup = nullptr;
@@ -1,23 +0,0 @@
#pragma once
#include "service/gui/ViewPort.h"
namespace tt::service::gui {
/** Process draw call. Calls onShow callback.
* To be used by GUI, called on redraw.
*
* @param view_port ViewPort instance
* @param canvas canvas to draw at
*/
void view_port_show(ViewPort* view_port, lv_obj_t* parent);
/**
* Process draw clearing call. Calls on_hdie callback.
* To be used by GUI, called on redraw.
*
* @param view_port
*/
void view_port_hide(ViewPort* view_port);
} // namespace
+3 -27
View File
@@ -6,7 +6,6 @@
#include "MessageQueue.h"
#include "Pubsub.h"
#include "Thread.h"
#include "service/gui/ViewPort.h"
#include "service/loader/Loader.h"
#include "RtosCompatSemaphore.h"
#include <stack>
@@ -25,31 +24,9 @@ typedef enum {
LoaderEventTypeApplicationStopped
} LoaderEventType;
typedef struct {
app::AppInstance& app;
} LoaderEventAppStarted;
typedef struct {
app::AppInstance& app;
} LoaderEventAppShowing;
typedef struct {
app::AppInstance& app;
} LoaderEventAppHiding;
typedef struct {
const app::AppManifest& manifest;
} LoaderEventAppStopped;
typedef struct {
struct LoaderEvent {
LoaderEventType type;
union {
LoaderEventAppStarted app_started;
LoaderEventAppShowing app_showing;
LoaderEventAppHiding app_hiding;
LoaderEventAppStopped app_stopped;
};
} LoaderEvent;
};
// endregion LoaderEvent
@@ -77,10 +54,9 @@ public:
// endregion LoaderMessage
struct Loader {
std::shared_ptr<PubSub> pubsubInternal = std::make_shared<PubSub>();
std::shared_ptr<PubSub> pubsubExternal = std::make_shared<PubSub>();
Mutex mutex = Mutex(Mutex::Type::Recursive);
std::stack<app::AppInstance*> appStack;
std::stack<std::shared_ptr<app::AppInstance>> appStack;
/** The dispatcher thread needs a callstack large enough to accommodate all the dispatched methods.
* This includes full LVGL redraw via Gui::redraw()
*/