Ken Van Hoeylandt c3bcf93698
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.
2025-01-21 17:48:32 +01:00

97 lines
2.8 KiB
C++

#pragma once
#include "Bundle.h"
#include "Mutex.h"
#include "app/AppContext.h"
#include "app/AppManifest.h"
#include "app/ElfApp.h"
#include <memory>
#include <utility>
namespace tt::app {
typedef enum {
StateInitial, // App is being activated in loader
StateStarted, // App is in memory
StateShowing, // App view is created
StateHiding, // App view is destroyed
StateStopped // App is not in memory
} State;
/**
* Thread-safe app instance.
*/
class AppInstance : public AppContext {
private:
Mutex mutex = Mutex(Mutex::Type::Normal);
const std::shared_ptr<AppManifest> manifest;
State state = StateInitial;
Flags flags = { .showStatusbar = true };
/** @brief Optional parameters to start the app with
* When these are stored in the app struct, the struct takes ownership.
* Do not mutate after app creation.
*/
std::shared_ptr<const tt::Bundle> _Nullable parameters;
/** @brief @brief Contextual data related to the running app's instance
* The app can attach its data to this.
* The lifecycle is determined by the on_start and on_stop methods in the AppManifest.
* These manifest methods can optionally allocate/free data that is attached here.
*/
std::shared_ptr<void> _Nullable data;
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 std::shared_ptr<AppManifest>& manifest) :
manifest(manifest),
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;
void setState(State state);
State getState() const;
const AppManifest& getManifest() const override;
Flags getFlags() const;
void setFlags(Flags flags);
Flags& mutableFlags() { return flags; } // TODO: locking mechanism
std::shared_ptr<const Bundle> getParameters() const override;
std::unique_ptr<Paths> getPaths() const override;
std::shared_ptr<App> getApp() const override { return app; }
};
} // namespace