Compare commits

...

2 Commits

Author SHA1 Message Date
Ken Van Hoeylandt
4db0d9496d Fix for inputdialog 2026-09-04 08:59:09 +02:00
Ken Van Hoeylandt
d42c68ce3a Comments updated 2026-09-04 08:55:54 +02:00
8 changed files with 96 additions and 71 deletions

View File

@ -24,8 +24,8 @@ struct AppResultEventData {
uint32_t launch_id;
/** The child app instance's own AppMainFn/AppLoaderApi::run() return value. By convention:
* 0 = Ok, 1 = Cancelled, 2 = Error. Apps that need to hand back more than this (e.g. picked
* text, a path) expose their own "get last result" getter instead - see e.g.
* tt::app::inputdialog::getLastText(). */
* text, a path) write it to their own stdout instead, for the caller to read via an
* AppStream bound to it. See e.g. tt::app::inputdialog::start(). */
int32_t result;
};

View File

@ -17,11 +17,9 @@ extern "C" {
/**
* Starts an app directly from @a location, without it having to be pre-registered via
* app_manager_add() first. Independent of the id-based app_start*() family
* (app/manager.h): performs no checks of its own beyond what AppLoaderApi::load() itself
* rejects. Not manifest lookup, and not whether @a location should be allowed to run (see
* app_is_executable() below): callers that need a check must run it themselves before calling
* this.
* app_manager_add() first.
* Performs no checks of its own beyond what AppLoaderApi::load() itself rejects.
* @warning It's advised to validate @a location with app_is_executable() first
* @param[in] stack stack allocation config for the app's task; all-zero uses the scheduler's
* default depth/capability, same as an AppManifest that leaves AppManifest::stack zeroed
* @retval ERROR_NOT_FOUND no AppLoaderApi is registered for @a location.type
@ -36,9 +34,8 @@ error_t app_execute(
);
/**
* Same as app_execute(), but as a modal child of @a parent_instance_id - see
* app_start_for_result()'s own doc for the result-delivery contract. Still no manifest
* and no check of its own beyond what AppLoaderApi::load() itself rejects.
* Same as app_execute(), but as a modal child of @a parent_instance_id.
* See app_start_for_result()'s own doc for the result-delivery contract.
* @retval ERROR_NOT_FOUND no AppLoaderApi is registered for @a location.type
* @retval ERROR_NONE on success
*/
@ -53,7 +50,7 @@ error_t app_execute_for_result(
/**
* Same as app_execute(), but installs @a bindings into the new instance's fd table before its
* task begins executing - see app_start_with_streams()'s own doc for stream ownership.
* task begins executing. See app_start_with_streams()'s own doc for stream ownership.
* @param[in] bindings see app_start_with_streams()
* @retval ERROR_NOT_FOUND no AppLoaderApi is registered for @a location.type
* @retval ERROR_OUT_OF_RANGE a binding's producer_fd is out of range
@ -90,9 +87,9 @@ error_t app_execute_for_result_with_streams(
);
/**
* Reports whether @a location is runnable on this target - the extension and header a loader
* requires (e.g. an ELF's class/data/type/machine), not whether it lives anywhere in particular:
* any executable app is runnable from any path. Cheap enough to call while listing a directory.
* Reports whether @a location is runnable on this target: the extension and header a loader
* requires (e.g. an ELF's class/data/type/machine), not whether it lives anywhere in particular.
* Any executable app is runnable from any path. Cheap enough to call while listing a directory.
* @return false if @a location can't be run, or if no AppLoaderApi is registered for its type
*/
bool app_is_executable(struct AppLocation location);

View File

@ -4,7 +4,7 @@
#include <app/manager.h>
/**
* This file contains functions to start and run apps that were registerd to the app manager.
* This file contains functions to start and run apps that were registered to the app manager.
* It differs from execute.h which runs executables from a specific path.
*/
@ -13,29 +13,27 @@ extern "C" {
#endif
/**
* Same as app_start(), but also passes @a argc/@a argv to the new instance's own main
* function (see app/loader.h's AppMainFn) - modelled on a C program's main(argc, argv). For
* regular (non-modal) navigations that need to pass data to the target app (e.g. "show details
* for this app id") without expecting a result back.
* Starts @a id, a manifest already registered via app_manager_add(), passing @a argc/@a argv to
* the new instance's own main function (see app/loader.h's AppMainFn), modelled on a C program's
* main(argc, argv). For regular (non-modal) navigations that need to pass data to the target app
* (e.g. "show details for this app id") without expecting a result back.
* @param[in] argv @a argc strings; app-module makes its own deep copy before returning, so
* @a argv and the strings it points to may be freed/go out of scope immediately after this call
* returns (e.g. safe to pass a stack-local array of a caller's own std::string::c_str()s).
* @retval ERROR_NOT_FOUND no manifest with this id is registered, or no AppLoaderApi is registered
* @retval ERROR_NONE on success
*/
error_t app_start(const char* id, int argc, const char* const argv[], AppInstanceId* out_app_instance_id);
/**
* Starts @a id as a modal child of @a parent_instance_id, for the purpose of receiving a
* result. The parent keeps running (window_manager's own multi-window stack handles burying its
* window while the child is shown).
* Starts @a id as a child of @a parent_instance_id, for the purpose of receiving a result.
*
* When the child's task exits, an APP_EVENT_RESULT is delivered to @a parent_instance_id -
* result is whatever the child's AppMainFn/AppLoaderApi::run() returned - unless
* When the child's task exits, an APP_EVENT_RESULT is delivered to @a parent_instance_id.
* The result is whatever the child's AppMainFn/AppLoaderApi::run() returned, unless
* @a parent_instance_id is 0, in which case no result is delivered (fire-and-forget, for
* callers with no app_instance_id of their own). The parent is then responsible for calling
* app_manager_stop() on the child's instance id to fully reap it. Children that need to hand
* back more than an int32_t (e.g. picked text, a path) expose their own "get last result"
* getter for the parent to call after receiving the event - see e.g.
* tt::app::inputdialog::getLastText().
* app_manager_stop() on the child's instance id to fully reap it.
*
* @param[in] argv @a argc strings; app-module makes its own deep copy before returning (same as
* app_start()), so @a argv and the strings it points to may be
* freed/go out of scope immediately after this call returns.
@ -62,10 +60,8 @@ error_t app_start_with_streams(const char* id, const struct AppStreamBinding* bi
* Combines app_start_for_result() and app_start_with_streams(): starts @a id as
* a modal child of @a parent_instance_id (see app_start_for_result()'s own doc for the
* result-delivery contract) with @a bindings installed into its fd table before its task begins
* executing (see app_start_with_streams()'s own doc for stream ownership). For a child
* that needs to hand back more than an int32_t (e.g. a path) via its own stdout instead of the
* "get last result" getter pattern (see app_start_for_result()) - see e.g.
* tt::app::fileselection::startForExistingFile().
* executing (see app_start_with_streams()'s own doc for stream ownership).
* For a child that needs to hand back more than an int32_t (e.g. a path) via its own stdout.
* @param[in] argv see app_start_for_result().
* @param[in] bindings see app_start_with_streams().
* @retval ERROR_NOT_FOUND no manifest with this id is registered, or no AppLoaderApi is registered

View File

@ -1,8 +1,12 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include <app/stream.h>
#include <tactility/concurrent/task_event_group.h>
/**
* Show a dialog with a title, a message and a text field.
*/
@ -10,19 +14,21 @@ namespace tt::app::inputdialog {
/**
* Show a dialog with the provided title, message and prefilled text, as a modal child of
* @a callerAppInstanceId (a new-model app - see app/manager.h). The caller receives the result
* as an APP_EVENT_RESULT in its own event loop: 0 = OK (call getLastText() for the entered
* text), 1 = Cancelled or dismissed without a press. The caller is responsible for calling
* app_manager_stop() on the returned instance id once it has handled the result.
* @a callerAppInstanceId (see app_start_for_result_with_streams()). Result (0 = OK, 1 =
* Cancelled or dismissed without a press) is delivered back via APP_EVENT_RESULT once this app's
* thread exits. On result == 0, read the entered text with app_stream_read(&stream, ...) then
* app_stream_unsubscribe(&stream); on any other result, just app_stream_unsubscribe(&stream).
* The caller must call app_manager_stop() on the returned instance id once that event arrives,
* to fully reap this instance.
* @param[in,out] stream caller-owned storage bound to the started app's stdout; must stay valid
* until app_stream_unsubscribe() is called on it (see above).
* @param[in] buffer caller-owned backing storage for @a stream's ring buffer; must stay valid
* for the same duration as @a stream.
* @param[in] bufferCapacity size of @a buffer in bytes.
* @param[in] eventGroup the caller's own event group, reused for the stream's readiness bits
* (see app_stream_subscribe()). The caller isn't required to actually wait on them itself.
* @return the new dialog's app instance id
*/
uint32_t start(uint32_t callerAppInstanceId, const std::string& title, const std::string& message, const std::string& prefilled = "");
/**
* @return the text entered the last time any InputDialog instance was closed with OK. Only one
* dialog is expected to be open at a time - call this right after receiving its
* APP_EVENT_RESULT with result == 0.
*/
std::string getLastText();
uint32_t start(uint32_t callerAppInstanceId, const std::string& title, const std::string& message, const std::string& prefilled, AppStream& stream, void* buffer, size_t bufferCapacity, TaskEventGroup* eventGroup);
}

View File

@ -2,6 +2,9 @@
#include "./State.h"
#include <app/stream.h>
#include <tactility/concurrent/task_event_group.h>
#include <cstdint>
#include <lvgl.h>
#include <memory>
@ -10,8 +13,12 @@ namespace tt::app::files {
class View final {
std::shared_ptr<State> state;
TaskEventGroup* eventGroup = nullptr;
uint32_t appInstanceId = 0;
AppStream inputDialogStream {};
uint8_t inputDialogBuffer[256] {};
size_t current_start_index = 0;
size_t last_loaded_index = 0;
const size_t MAX_BATCH = 50;
@ -39,7 +46,7 @@ class View final {
public:
explicit View(const std::shared_ptr<State>& state) : state(state) {}
View(const std::shared_ptr<State>& state, TaskEventGroup* eventGroup) : state(state), eventGroup(eventGroup) {}
void init(uint32_t appInstanceId, lv_obj_t* parent);
void update(size_t start_index = 0);

View File

@ -31,12 +31,13 @@ void createWidgets(lv_obj_t* parent, void* userData) {
int32_t appMain(int argc, char* argv[]) {
uint32_t appInstanceId = app_scheduler_current_app_id();
auto state = std::make_shared<State>();
View view(state);
CreateContext createContext { &view, appInstanceId };
TaskEventGroup event_group {};
task_event_group_construct(&event_group);
View view(state, &event_group);
CreateContext createContext { &view, appInstanceId };
AppEventSubscription sub {};
check(app_event_subscribe(&sub, &event_group) == ERROR_NONE);

View File

@ -1,6 +1,7 @@
#include <app/event.h>
#include <app/execute.h>
#include <app/install.h>
#include <app/stream.h>
#include <lvgl/lvgl.h>
#include <lvgl/widgets/toolbar.h>
@ -378,7 +379,7 @@ void View::onRenamePressed() {
std::string entry_name = state->getSelectedChildEntry();
LOG_I(TAG, "Pending rename %s", entry_name.c_str());
state->setPendingAction(State::ActionRename);
inputdialog::start(appInstanceId, "Rename", "", entry_name);
inputdialog::start(appInstanceId, "Rename", "", entry_name, inputDialogStream, inputDialogBuffer, sizeof(inputDialogBuffer), eventGroup);
}
void View::onDeletePressed() {
@ -393,13 +394,13 @@ void View::onDeletePressed() {
void View::onNewFilePressed() {
LOG_I(TAG, "Creating new file");
state->setPendingAction(State::ActionCreateFile);
inputdialog::start(appInstanceId, "New File", "Enter filename:", "");
inputdialog::start(appInstanceId, "New File", "Enter filename:", "", inputDialogStream, inputDialogBuffer, sizeof(inputDialogBuffer), eventGroup);
}
void View::onNewFolderPressed() {
LOG_I(TAG, "Creating new folder");
state->setPendingAction(State::ActionCreateFolder);
inputdialog::start(appInstanceId, "New Folder", "Enter folder name:", "");
inputdialog::start(appInstanceId, "New Folder", "Enter folder name:", "", inputDialogStream, inputDialogBuffer, sizeof(inputDialogBuffer), eventGroup);
}
void View::addCommonFileActions() {
@ -597,10 +598,21 @@ void View::onResult(uint32_t launchId, int32_t result) {
std::string filepath = state->getSelectedChildPath();
LOG_I(TAG, "Result for %s", filepath.c_str());
// Text-entry result (rename/new file/new folder); empty for Cancel, or for a dialog that
// doesn't produce text (delete/paste confirmations) - those switch cases below only look at
// `result`, not this.
std::string resultText = (result == 0) ? inputdialog::getLastText() : std::string();
// Text-entry result (rename/new file/new folder), read from the AppStream bound to that
// dialog's stdout. Empty for Cancel. Other pending actions (delete/paste confirmations) never
// bound this stream; their switch cases below only look at `result`, not `resultText`.
bool isTextEntryAction = state->getPendingAction() == State::ActionRename ||
state->getPendingAction() == State::ActionCreateFile ||
state->getPendingAction() == State::ActionCreateFolder;
std::string resultText;
if (isTextEntryAction) {
if (result == 0) {
char buffer[sizeof(inputDialogBuffer)];
size_t length = app_stream_read(&inputDialogStream, buffer, sizeof(buffer));
resultText = std::string(buffer, length);
}
app_stream_unsubscribe(&inputDialogStream);
}
switch (state->getPendingAction()) {
case State::ActionDelete: {

View File

@ -5,6 +5,7 @@
#include <app/start.h>
#include <app/manifest.h>
#include <app/scheduler.h>
#include <app/stream.h>
#include <lvgl_window_manager/window_manager.h>
@ -14,6 +15,8 @@
#include <lvgl.h>
#include <unistd.h>
namespace tt::app::inputdialog {
constexpr auto* TAG = "InputDialog";
@ -31,7 +34,8 @@ struct Context {
// The eventual appMain() return value - see AlertDialog.cpp's Context::result for why this
// is a plain (non-atomic) field safely shared between the LVGL thread (writer, before
// emitting APP_EVENT_CLOSE) and this dialog's own thread (reader, after waking from it).
int32_t result = 1; // Cancelled - safety-net default if closed without pressing a button
int32_t resultCode = 1; // Cancelled - safety-net default if closed without pressing a button
std::string resultText;
};
struct ButtonContext {
@ -40,12 +44,6 @@ struct ButtonContext {
lv_obj_t* textarea;
};
// The last text entered via OK. Static rather than per-instance: simple, and in practice only
// one InputDialog is ever open at a time. Written on the LVGL thread (onButtonPressed(), before
// emitting APP_EVENT_CLOSE); read by the parent via getLastText() after receiving that event -
// safe without a lock for the same reason Context::result is (see AlertDialog.cpp).
std::string lastText;
void onButtonDeleted(lv_event_t* e) {
delete static_cast<ButtonContext*>(lv_event_get_user_data(e));
}
@ -54,11 +52,11 @@ void onButtonPressed(lv_event_t* e) {
auto* btnCtx = static_cast<ButtonContext*>(lv_event_get_user_data(e));
if (btnCtx->textarea != nullptr) {
LOG_I(TAG, "OK pressed");
lastText = lv_textarea_get_text(btnCtx->textarea);
btnCtx->ctx->result = 0;
btnCtx->ctx->resultText = lv_textarea_get_text(btnCtx->textarea);
btnCtx->ctx->resultCode = 0;
} else {
LOG_I(TAG, "Cancel pressed");
btnCtx->ctx->result = 1;
btnCtx->ctx->resultCode = 1;
}
app_event_emit_close(btnCtx->ctx->appInstanceId);
}
@ -138,22 +136,30 @@ int32_t appMain(int argc, char* argv[]) {
check(app_event_unsubscribe(&sub) == ERROR_NONE);
task_event_group_destruct(&event_group);
return ctx.result;
if (ctx.resultCode == 0) {
// The caller captures this via an AppStream bound to our stdout (see start()); see
// AppStdioWrap.cpp for how printf() itself gets routed there on POSIX.
printf("%s", ctx.resultText.c_str());
}
return ctx.resultCode;
}
} // namespace
uint32_t start(uint32_t callerAppInstanceId, const std::string& title, const std::string& message, const std::string& prefilled) {
uint32_t start(uint32_t callerAppInstanceId, const std::string& title, const std::string& message, const std::string& prefilled, AppStream& stream, void* buffer, size_t bufferCapacity, TaskEventGroup* eventGroup) {
const char* argv[] = { title.c_str(), message.c_str(), prefilled.c_str() };
AppStreamBinding binding = {
.producer_fd = STDOUT_FILENO,
.stream = &stream,
.buffer = buffer,
.buffer_capacity = bufferCapacity,
.event_group = eventGroup,
};
uint32_t instanceId = 0;
app_start_for_result(manifest.id, 3, argv, callerAppInstanceId, &instanceId);
app_start_for_result_with_streams(manifest.id, 3, argv, &binding, 1, callerAppInstanceId, &instanceId);
return instanceId;
}
std::string getLastText() {
return lastText;
}
extern const ::AppManifest manifest = {
.id = "tactility.inputdialog",
.name = "Input Dialog",