diff --git a/Documentation/ideas.md b/Documentation/ideas.md index 8f24536ec..625d4388f 100644 --- a/Documentation/ideas.md +++ b/Documentation/ideas.md @@ -85,7 +85,6 @@ - T-Deck: Use trackball as input device (with optional mouse functionality for LVGL) - Show a warning screen if firmware encryption or secure boot are off when saving WiFi credentials. - Remove flex_flow from app_container in Gui.cpp -- Files app: copy/cut/paste actions - ElfAppManifest: change name (remove "manifest" as it's confusing), remove icon and title, publish snapshot SDK on CDN - Bug: CYD 2432S032C screen rotation fails due to touch driver issue - Calculator app should show regular text input field on non-touch devices that have a keyboard (Cardputer, T-Lora Pager) diff --git a/Modules/lvgl-module/source/symbols.c b/Modules/lvgl-module/source/symbols.c index 5e7625171..9ef79ff03 100644 --- a/Modules/lvgl-module/source/symbols.c +++ b/Modules/lvgl-module/source/symbols.c @@ -155,6 +155,8 @@ const struct ModuleSymbol lvgl_module_symbols[] = { DEFINE_MODULE_SYMBOL(lv_obj_class_init_obj), DEFINE_MODULE_SYMBOL(lv_obj_move_foreground), DEFINE_MODULE_SYMBOL(lv_obj_move_to_index), + DEFINE_MODULE_SYMBOL(lv_obj_set_style_min_height), + DEFINE_MODULE_SYMBOL(lv_obj_set_style_max_height), // lv_font DEFINE_MODULE_SYMBOL(lv_font_get_default), // lv_theme @@ -385,6 +387,7 @@ const struct ModuleSymbol lvgl_module_symbols[] = { DEFINE_MODULE_SYMBOL(lv_group_set_editing), DEFINE_MODULE_SYMBOL(lv_group_create), DEFINE_MODULE_SYMBOL(lv_group_delete), + DEFINE_MODULE_SYMBOL(lv_group_get_editing), // lv_mem DEFINE_MODULE_SYMBOL(lv_free), DEFINE_MODULE_SYMBOL(lv_malloc), @@ -407,5 +410,9 @@ const struct ModuleSymbol lvgl_module_symbols[] = { DEFINE_MODULE_SYMBOL(lv_anim_start), DEFINE_MODULE_SYMBOL(lv_anim_path_ease_in_out), DEFINE_MODULE_SYMBOL(lv_anim_path_linear), + DEFINE_MODULE_SYMBOL(lv_anim_path_ease_in), + DEFINE_MODULE_SYMBOL(lv_anim_path_ease_out), + // lv_async + DEFINE_MODULE_SYMBOL(lv_async_call), MODULE_SYMBOL_TERMINATOR }; \ No newline at end of file diff --git a/Tactility/Private/Tactility/app/files/State.h b/Tactility/Private/Tactility/app/files/State.h index ee22228cf..a4b20a3f4 100644 --- a/Tactility/Private/Tactility/app/files/State.h +++ b/Tactility/Private/Tactility/app/files/State.h @@ -2,7 +2,9 @@ #include +#include #include +#include #include #include @@ -17,7 +19,8 @@ class State final { ActionDelete, ActionRename, ActionCreateFile, - ActionCreateFolder + ActionCreateFolder, + ActionPaste }; private: @@ -27,6 +30,10 @@ class State final { std::string current_path; std::string selected_child_entry; PendingAction action = ActionNone; + std::string pending_paste_dst; + std::string clipboard_path; + bool clipboard_is_cut = false; + bool clipboard_active = false; public: @@ -66,6 +73,46 @@ class State final { PendingAction getPendingAction() const { return action; } void setPendingAction(PendingAction newAction) { action = newAction; } + + // These accessors intentionally omit mutex locking: both are only called + // from the UI thread (onPastePressed → onResult), so no concurrent access + // is possible. If that threading assumption changes, add mutex guards here + // to match the clipboard accessors above. + std::string getPendingPasteDst() const { return pending_paste_dst; } + void setPendingPasteDst(const std::string& dst) { pending_paste_dst = dst; } + + void setClipboard(const std::string& path, bool is_cut) { + mutex.withLock([&] { + clipboard_path = path; + clipboard_is_cut = is_cut; + clipboard_active = true; + }); + } + + bool hasClipboard() const { + bool result = false; + mutex.withLock([&] { result = clipboard_active; }); + return result; + } + + /** Returns {path, is_cut} atomically, or nullopt if clipboard is empty. */ + std::optional> getClipboard() const { + std::optional> result; + mutex.withLock([&] { + if (clipboard_active) { + result = { clipboard_path, clipboard_is_cut }; + } + }); + return result; + } + + void clearClipboard() { + mutex.withLock([&] { + clipboard_active = false; + clipboard_path.clear(); + clipboard_is_cut = false; + }); + } }; } diff --git a/Tactility/Private/Tactility/app/files/View.h b/Tactility/Private/Tactility/app/files/View.h index d69f3cc19..c13a2c9d7 100644 --- a/Tactility/Private/Tactility/app/files/View.h +++ b/Tactility/Private/Tactility/app/files/View.h @@ -21,10 +21,12 @@ class View final { lv_obj_t* navigate_up_button = nullptr; lv_obj_t* new_file_button = nullptr; lv_obj_t* new_folder_button = nullptr; + lv_obj_t* paste_button = nullptr; std::string installAppPath = { 0 }; LaunchId installAppLaunchId = 0; + void showActions(); void showActionsForDirectory(); void showActionsForFile(); @@ -46,6 +48,9 @@ class View final { void onDeletePressed(); void onNewFilePressed(); void onNewFolderPressed(); + void onCopyPressed(); + void onCutPressed(); + void onPastePressed(); void onDirEntryListScrollBegin(); void onResult(LaunchId launchId, Result result, std::unique_ptr bundle); void deinit(const AppContext& appContext); @@ -53,6 +58,7 @@ class View final { private: bool resolveDirentFromListIndex(int32_t list_index, dirent& out_entry); + void doPaste(const std::string& src, bool is_cut, const std::string& dst); }; } diff --git a/Tactility/Source/app/files/View.cpp b/Tactility/Source/app/files/View.cpp index 38bad9e61..eb5c1d595 100644 --- a/Tactility/Source/app/files/View.cpp +++ b/Tactility/Source/app/files/View.cpp @@ -74,6 +74,88 @@ static void onNewFolderPressedCallback(lv_event_t* event) { view->onNewFolderPressed(); } +static void onCopyPressedCallback(lv_event_t* event) { + auto* view = static_cast(lv_event_get_user_data(event)); + view->onCopyPressed(); +} + +static void onCutPressedCallback(lv_event_t* event) { + auto* view = static_cast(lv_event_get_user_data(event)); + view->onCutPressed(); +} + +static void onPastePressedCallback(lv_event_t* event) { + auto* view = static_cast(lv_event_get_user_data(event)); + view->onPastePressed(); +} + +// endregion + +// region File helpers + +static bool copyFileContents(const std::string& src, const std::string& dst) { + FILE* in = fopen(src.c_str(), "rb"); + if (in == nullptr) return false; + FILE* out = fopen(dst.c_str(), "wb"); + if (out == nullptr) { + fclose(in); + return false; + } + uint8_t buf[512]; + bool success = true; + size_t n; + while ((n = fread(buf, 1, sizeof(buf), in)) > 0) { + if (fwrite(buf, 1, n, out) != n) { + success = false; + break; + } + } + if (ferror(in)) { + success = false; + } + fclose(in); + if (fclose(out) != 0) { + success = false; + } + if (!success) { + remove(dst.c_str()); + } + return success; +} + +static bool copyRecursive(const std::string& src, const std::string& dst) { + if (file::isDirectory(src)) { + if (!file::findOrCreateDirectory(dst, 0755)) { + return false; + } + std::vector entries; + bool listed = file::listDirectory(src, [&](const dirent& entry) { + if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) return; + entries.push_back(entry); // collect while lock is held; do not recurse here + }); + if (!listed) { + file::deleteRecursively(dst); // remove the directory created above + return false; + } + // listDirectory has returned — device lock is now released + bool success = true; + for (const auto& entry : entries) { + if (!success) break; + auto child_src = file::getChildPath(src, entry.d_name); + auto child_dst = file::getChildPath(dst, entry.d_name); + if (!copyRecursive(child_src, child_dst)) { + success = false; + } + } + if (!success) { + file::deleteRecursively(dst); // remove partial copy + } + return success; + } else { + return copyFileContents(src, dst); + } +} + // endregion void View::viewFile(const std::string& path, const std::string& filename) { @@ -280,9 +362,13 @@ void View::onNewFolderPressed() { inputdialog::start("New Folder", "Enter folder name:", ""); } -void View::showActionsForDirectory() { +void View::showActions() { lv_obj_clean(action_list); + auto* copy_button = lv_list_add_button(action_list, LV_SYMBOL_COPY, "Copy"); + lv_obj_add_event_cb(copy_button, onCopyPressedCallback, LV_EVENT_SHORT_CLICKED, this); + auto* cut_button = lv_list_add_button(action_list, LV_SYMBOL_CUT, "Cut"); + lv_obj_add_event_cb(cut_button, onCutPressedCallback, LV_EVENT_SHORT_CLICKED, this); auto* rename_button = lv_list_add_button(action_list, LV_SYMBOL_EDIT, "Rename"); lv_obj_add_event_cb(rename_button, onRenamePressedCallback, LV_EVENT_SHORT_CLICKED, this); auto* delete_button = lv_list_add_button(action_list, LV_SYMBOL_TRASH, "Delete"); @@ -291,16 +377,8 @@ void View::showActionsForDirectory() { lv_obj_remove_flag(action_list, LV_OBJ_FLAG_HIDDEN); } -void View::showActionsForFile() { - lv_obj_clean(action_list); - - auto* rename_button = lv_list_add_button(action_list, LV_SYMBOL_EDIT, "Rename"); - lv_obj_add_event_cb(rename_button, onRenamePressedCallback, LV_EVENT_SHORT_CLICKED, this); - auto* delete_button = lv_list_add_button(action_list, LV_SYMBOL_TRASH, "Delete"); - lv_obj_add_event_cb(delete_button, onDeletePressedCallback, LV_EVENT_SHORT_CLICKED, this); - - lv_obj_remove_flag(action_list, LV_OBJ_FLAG_HIDDEN); -} +void View::showActionsForDirectory() { showActions(); } +void View::showActionsForFile() { showActions(); } void View::update(size_t start_index) { const bool is_root = (state->getCurrentPath() == "/"); @@ -360,9 +438,15 @@ void View::update(size_t start_index) { }); if (is_root) { - lv_obj_add_flag(navigate_up_button, LV_OBJ_FLAG_HIDDEN); + lv_obj_add_flag(lv_obj_get_parent(navigate_up_button), LV_OBJ_FLAG_HIDDEN); + } else { + lv_obj_remove_flag(lv_obj_get_parent(navigate_up_button), LV_OBJ_FLAG_HIDDEN); + } + + if (state->hasClipboard() && !is_root) { + lv_obj_remove_flag(lv_obj_get_parent(paste_button), LV_OBJ_FLAG_HIDDEN); } else { - lv_obj_remove_flag(navigate_up_button, LV_OBJ_FLAG_HIDDEN); + lv_obj_add_flag(lv_obj_get_parent(paste_button), LV_OBJ_FLAG_HIDDEN); } } @@ -374,6 +458,8 @@ void View::init(const AppContext& appContext, lv_obj_t* parent) { navigate_up_button = lvgl::toolbar_add_image_button_action(toolbar, LV_SYMBOL_UP, &onNavigateUpPressedCallback, this); new_file_button = lvgl::toolbar_add_image_button_action(toolbar, LV_SYMBOL_FILE, &onNewFilePressedCallback, this); new_folder_button = lvgl::toolbar_add_image_button_action(toolbar, LV_SYMBOL_DIRECTORY, &onNewFolderPressedCallback, this); + paste_button = lvgl::toolbar_add_image_button_action(toolbar, LV_SYMBOL_PASTE, &onPastePressedCallback, this); + lv_obj_add_flag(lv_obj_get_parent(paste_button), LV_OBJ_FLAG_HIDDEN); auto* wrapper = lv_obj_create(parent); lv_obj_set_width(wrapper, LV_PCT(100)); @@ -454,7 +540,15 @@ void View::onResult(LaunchId launchId, Result result, std::unique_ptr bu auto lock = file::getLock(filepath); lock->lock(); std::string rename_to = file::getChildPath(state->getCurrentPath(), new_name); - if (rename(filepath.c_str(), rename_to.c_str())) { + struct stat st; + if (stat(rename_to.c_str(), &st) == 0) { + LOGGER.warn("Rename: destination already exists: \"{}\"", rename_to); + lock->unlock(); + state->setPendingAction(State::ActionNone); + alertdialog::start("Rename failed", "\"" + new_name + "\" already exists."); + break; + } + if (rename(filepath.c_str(), rename_to.c_str()) == 0) { LOGGER.info("Renamed \"{}\" to \"{}\"", filepath, rename_to); } else { LOGGER.error("Failed to rename \"{}\" to \"{}\"", filepath, rename_to); @@ -522,11 +616,129 @@ void View::onResult(LaunchId launchId, Result result, std::unique_ptr bu } break; } + case State::ActionPaste: { + if (alertdialog::getResultIndex(*bundle) == 0) { + auto clipboard = state->getClipboard(); + if (clipboard.has_value()) { + std::string dst = state->getPendingPasteDst(); + // Trade-off: dst is removed before the copy attempt. If doPaste + // subsequently fails (e.g. source read error, out of space), the + // original dst data is unrecoverable. Acceptable for an embedded + // file manager; a safer approach would rename dst to a temp path + // first and roll back on failure. + if (file::deleteRecursively(dst)) { + doPaste(clipboard->first, clipboard->second, dst); + } else { + LOGGER.error("Overwrite: failed to remove existing destination: \"{}\"", dst); + state->setPendingAction(State::ActionNone); + alertdialog::start( + "Overwrite failed", + "Could not remove \"" + file::getLastPathSegment(dst) + "\" before overwriting." + ); + } + } + } + break; + } default: break; } } +void View::onCopyPressed() { + std::string path = state->getSelectedChildPath(); + state->setClipboard(path, false); + LOGGER.info("Copied to clipboard: {}", path); + onNavigate(); + update(); +} + +void View::onCutPressed() { + std::string path = state->getSelectedChildPath(); + state->setClipboard(path, true); + LOGGER.info("Cut to clipboard: {}", path); + onNavigate(); + update(); +} + +void View::onPastePressed() { + auto clipboard = state->getClipboard(); + if (!clipboard.has_value()) return; + + std::string src = clipboard->first; + bool is_cut = clipboard->second; + std::string filename = file::getLastPathSegment(src); + std::string dst = file::getChildPath(state->getCurrentPath(), filename); + + // Note: getLock(src) guards the source path; the existence check below is + // against dst, so there is a TOCTOU gap — another writer could create dst + // between this check and the write inside doPaste. Acceptable on a + // single-user embedded device; locking dst instead would be more correct. + if (src == dst) { + LOGGER.info("Paste: source and destination are the same path, skipping"); + return; + } + auto lock = file::getLock(src); + lock->lock(); + + struct stat st; + bool dst_exists = (stat(dst.c_str(), &st) == 0); + lock->unlock(); + + if (dst_exists) { + state->setPendingPasteDst(dst); + state->setPendingAction(State::ActionPaste); + const std::vector choices = {"Overwrite", "Cancel"}; + alertdialog::start("File exists", "Overwrite \"" + filename + "\"?", choices); + return; + } + + doPaste(src, is_cut, dst); +} + +void View::doPaste(const std::string& src, bool is_cut, const std::string& dst) { + bool success = false; + bool src_delete_failed = false; + if (is_cut) { + success = (rename(src.c_str(), dst.c_str()) == 0); + if (!success) { + // Fallback for cross-filesystem moves: copy then delete. + // Only mark success if both halves succeed — if the source removal + // fails we leave success=false so the clipboard is preserved and + // the error is surfaced; the user must remove the source manually. + if (copyRecursive(src, dst)) { + if (file::deleteRecursively(src)) { + success = true; + } else { + src_delete_failed = true; + LOGGER.error("Cut: copied \"{}\" to \"{}\" but failed to remove source — manual cleanup required", src, dst); + } + } + } + } else { + success = copyRecursive(src, dst); + } + + const std::string filename = file::getLastPathSegment(src); + if (success) { + LOGGER.info("{} \"{}\" to \"{}\"", is_cut ? "Moved" : "Copied", src, dst); + state->clearClipboard(); + } else if (src_delete_failed) { + state->setPendingAction(State::ActionNone); // prevent re-trigger on dialog dismiss + alertdialog::start("Move incomplete", "\"" + filename + "\" was copied but the original could not be removed.\nPlease delete it manually."); + } else { + LOGGER.error("Failed to {} \"{}\" to \"{}\"", is_cut ? "move" : "copy", src, dst); + state->setPendingAction(State::ActionNone); // prevent re-trigger on dialog dismiss + alertdialog::start( + std::string("Failed to ") + (is_cut ? "move" : "copy"), + "\"" + filename + "\" could not be " + (is_cut ? "moved." : "copied.") + ); + } + + state->setEntriesForPath(state->getCurrentPath()); + update(); +} + void View::deinit(const AppContext& appContext) { lv_obj_remove_event_cb(dir_entry_list, dirEntryListScrollBeginCallback); } diff --git a/TactilityC/Source/symbols/mbedtls.cpp b/TactilityC/Source/symbols/mbedtls.cpp index adf4871d9..17cdf2248 100644 --- a/TactilityC/Source/symbols/mbedtls.cpp +++ b/TactilityC/Source/symbols/mbedtls.cpp @@ -1,6 +1,5 @@ #include #include -#include #include diff --git a/TactilityC/Source/tt_init.cpp b/TactilityC/Source/tt_init.cpp index f64fda8ee..a065045a2 100644 --- a/TactilityC/Source/tt_init.cpp +++ b/TactilityC/Source/tt_init.cpp @@ -87,6 +87,8 @@ const esp_elfsym main_symbols[] { ESP_ELFSYM_EXPORT(unlink), // strings.h ESP_ELFSYM_EXPORT(explicit_bzero), + ESP_ELFSYM_EXPORT(strcasecmp), + ESP_ELFSYM_EXPORT(strncasecmp), // time.h ESP_ELFSYM_EXPORT(clock_gettime), ESP_ELFSYM_EXPORT(strftime),