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:
committed by
GitHub
parent
2bbd44a8b5
commit
c3bcf93698
@@ -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
|
||||
@@ -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; }
|
||||
|
||||
Reference in New Issue
Block a user