Compare commits

...

7 Commits

Author SHA1 Message Date
Ken Van Hoeylandt
a9340ae345 Fix for resuming windows when lvgl was stopped and started again 2026-08-09 17:21:55 +02:00
Ken Van Hoeylandt
9487441b97 Fix for duplicate wifi events on P4 2026-08-09 17:06:03 +02:00
Ken Van Hoeylandt
8a75bc501e Fixes for keyboard 2026-08-09 16:17:42 +02:00
Ken Van Hoeylandt
7aac44bc35 Fix for touch 2026-08-09 15:08:46 +02:00
Ken Van Hoeylandt
0e8d642448 Fixes and improvements 2026-08-09 14:44:11 +02:00
Ken Van Hoeylandt
52c1ec7060 Add missing symbols 2026-08-09 11:51:11 +02:00
Ken Van Hoeylandt
07a571f68d Fix for installing and running apps 2026-08-09 11:51:08 +02:00
15 changed files with 236 additions and 31 deletions

View File

@ -67,6 +67,11 @@ static void create_gt911_touch(Device* i2c0) {
// Reset is pulsed via io_expander0 (detect.cpp's pulse_display_reset_pins), not a direct SoC GPIO.
.pin_reset = GPIO_PIN_SPEC_NONE,
.pin_interrupt = GPIO_PIN_SPEC_NONE,
.reset_pulses = 0, // no-op: pin_reset is NONE, so reset_controller_pin() skips anyway
.x_offset = 0,
.y_offset = 0,
.x_scale = 1000,
.y_scale = 1000,
};
gt911_device.config = &gt911_config;

View File

@ -575,6 +575,7 @@ static error_t tab5_keyboard_read_key(Device* device, KeyboardKeyData* data) {
static const KeyboardApi tab5_keyboard_api = {
.read_key = tab5_keyboard_read_key,
.is_present = tab5_keyboard_is_attached,
};
// Defined in module.cpp - this driver is registered directly by m5stack-tab5's own module,

View File

@ -231,8 +231,16 @@ error_t register_installed_app_locked(const std::string& app_dir_path, const App
.flags = 0,
};
// Belt-and-braces: app_install()'s earlier app_manager_remove() call is meant to have
// already cleared any stale registration for this id (e.g. left over from
// app_manager_install_path_scan()'s separate registry), but that call happens before the
// tarball is even extracted - remove once more, right before add, so a duplicate id can
// never turn a filesystem-level install success into a reported failure.
app_manager_remove(record->id.c_str());
error_t add_result = app_manager_add(&record->manifest);
if (add_result != ERROR_NONE) {
LOG_E(TAG, "Failed to register app '%s': %s", record->id.c_str(), error_to_string(add_result));
return add_result;
}
@ -362,7 +370,9 @@ error_t app_install(const char* source_path) {
// uninstall_locked() doesn't know about. Clear the app-manager registration unconditionally
// too, or app_manager_add() below rejects the re-add as a duplicate.
uninstall_locked(metadata.app_id);
if (app_manager_remove(metadata.app_id) != ERROR_NONE) {
error_t remove_result = app_manager_remove(metadata.app_id);
if (remove_result != ERROR_NONE && remove_result != ERROR_NOT_FOUND) {
LOG_E(TAG, "Install failed: failed to remove existing installation");
mutex_unlock(&registry.mutex);
delete_recursively(staging_path);

View File

@ -2,7 +2,8 @@
#include <app/event.h>
#include <app/install.h>
#include <app/manager.h>
#include <app/module.h>
#include <app/metadata.h>
#include <app/paths.h>
#include <app/scheduler.h>
#include <service/manager.h>
@ -15,13 +16,15 @@ extern "C" {
extern ServiceManifest app_internal_loader_service_manifest;
const ModuleSymbol app_module_symbols[] = {
// app/scheduler
DEFINE_MODULE_SYMBOL(app_scheduler_current_app_id),
// app/event
DEFINE_MODULE_SYMBOL(app_event_subscribe),
DEFINE_MODULE_SYMBOL(app_event_unsubscribe),
DEFINE_MODULE_SYMBOL(app_event_emit),
DEFINE_MODULE_SYMBOL(app_event_await),
// app/install
DEFINE_MODULE_SYMBOL(app_get_install_path),
DEFINE_MODULE_SYMBOL(app_install),
DEFINE_MODULE_SYMBOL(app_uninstall),
// app/manager
DEFINE_MODULE_SYMBOL(app_manager_start),
DEFINE_MODULE_SYMBOL(app_manager_start_with_parameters),
@ -37,10 +40,15 @@ const ModuleSymbol app_module_symbols[] = {
DEFINE_MODULE_SYMBOL(app_manager_get_topmost_app_id),
DEFINE_MODULE_SYMBOL(app_manager_install_path_add),
DEFINE_MODULE_SYMBOL(app_manager_install_path_scan),
// app/install
DEFINE_MODULE_SYMBOL(app_get_install_path),
DEFINE_MODULE_SYMBOL(app_install),
DEFINE_MODULE_SYMBOL(app_uninstall),
// app/metadata
DEFINE_MODULE_SYMBOL(app_metadata_parse),
// app/paths
DEFINE_MODULE_SYMBOL(app_paths_get_user_data_directory),
DEFINE_MODULE_SYMBOL(app_paths_get_user_data_path),
DEFINE_MODULE_SYMBOL(app_paths_get_assets_directory),
DEFINE_MODULE_SYMBOL(app_paths_get_assets_path),
// app/scheduler
DEFINE_MODULE_SYMBOL(app_scheduler_current_app_id),
// terminator
MODULE_SYMBOL_TERMINATOR
};

View File

@ -106,8 +106,11 @@ bool lvgl_hardware_keyboard_is_available() {
return false;
}
// TODO: Refactor the driver subsystem to so it does proper probing/releasing of such devices
// This work-around exists for the Tab5 keyboard driver.
bool present = keyboard_is_present(keyboard_device);
device_put(keyboard_device);
return true;
return present;
}
void lvgl_hardware_keyboard_add_custom(lv_indev_t* indev) {
@ -137,9 +140,15 @@ static void textarea_show_keyboard(lv_event_t* event) {
}
static void textarea_hide_keyboard(lv_event_t* event) {
if (last_software_keyboard.object != nullptr) {
lvgl_software_keyboard_hide(&last_software_keyboard);
if (last_software_keyboard.object == nullptr) {
return;
}
// Only hide if the keyboard is actually bound to the textarea that triggered this
lv_obj_t* target = lv_event_get_current_target_obj(event);
if (lv_keyboard_get_textarea(last_software_keyboard.object) != target) {
return;
}
lvgl_software_keyboard_hide(&last_software_keyboard);
}
void lvgl_software_keyboard_construct(LvglSoftwareKeyboard* keyboard, lv_obj_t* parent) {
@ -187,6 +196,7 @@ void lvgl_keyboard_add_textarea(LvglSoftwareKeyboard* keyboard, lv_obj_t* textar
lv_obj_add_event_cb(textarea, textarea_show_keyboard, LV_EVENT_FOCUSED, nullptr);
lv_obj_add_event_cb(textarea, textarea_hide_keyboard, LV_EVENT_DEFOCUSED, nullptr);
lv_obj_add_event_cb(textarea, textarea_hide_keyboard, LV_EVENT_READY, nullptr);
lv_obj_add_event_cb(textarea, textarea_hide_keyboard, LV_EVENT_DELETE, nullptr);
}
// lv_obj_t auto-remove themselves from the group when they are destroyed (last checked in LVGL 8.3)

View File

@ -210,12 +210,45 @@ error_t window_manager_start(void) {
return ERROR_RESOURCE;
}
// A previous stop() (see its comment) may have left window records behind for an app that's
// still running (as opposed to a real full shutdown, where every app has already removed its
// own window before this runs, leaving the list empty). Rebuild the topmost one now, exactly
// like window_manager_remove()'s resurface path does when a buried window becomes topmost -
// otherwise that app's task just sits blocked in its own event loop forever with no window
// and no way to know it needs to rebuild one.
WindowCreateWidgetsFn top_create_widgets = nullptr;
void* top_user_data = nullptr;
WindowId top_id = 0;
bool has_top = false;
mutex_lock(&s.mutex);
s.real_root_widget = real_widget;
s.content_root_widget = content_widget;
s.started = true;
if (!s.windows.empty()) {
top_create_widgets = s.windows.back().create_widgets;
top_user_data = s.windows.back().user_data;
top_id = s.windows.back().id;
has_top = true;
}
mutex_unlock(&s.mutex);
if (has_top) {
lv_obj_t* new_widget = build_window_widget(content_widget, top_create_widgets, top_user_data);
mutex_lock(&s.mutex);
bool still_topmost = !s.windows.empty() && s.windows.back().id == top_id;
if (still_topmost) {
s.top_widget = new_widget;
new_widget = nullptr; // consumed
}
mutex_unlock(&s.mutex);
// Something else changed the window stack while we were building (e.g. a concurrent
// remove()) - discard what we just made.
delete_widget(new_widget);
}
mutex_unlock(&s.lifecycle_mutex);
return ERROR_NONE;
}
@ -234,8 +267,8 @@ error_t window_manager_stop(void) {
return ERROR_NONE;
}
lv_obj_t* widget = s.real_root_widget;
// Claim every window's waiter before clearing - normally at most the topmost window's is
// ever set, but every window is being torn down here, so every one is checked.
// Claim every window's waiter before tearing down - normally at most the topmost window's
// is ever set, but every window's widget is being torn down here, so every one is checked.
std::vector<WindowWaitSignal*> waiters;
for (auto& window : s.windows) {
if (auto* signal = claim_waiter_locked(window); signal != nullptr) {
@ -245,7 +278,15 @@ error_t window_manager_stop(void) {
s.real_root_widget = nullptr;
s.content_root_widget = nullptr;
s.top_widget = nullptr;
s.windows.clear();
// Deliberately NOT s.windows.clear(): this only tears down the LVGL widget tree, not the
// window records themselves. A real full shutdown (every app already removed its own window
// via window_manager_remove() before this runs) leaves the list empty anyway, so this is a
// no-op there. But a caller can also stop()/start() this module on its own, temporarily,
// while apps keep running underneath (e.g. an app borrowing the display/touch hardware
// directly) - those apps' tasks are still alive, blocked in their own event loops, with no
// way to know they need to call window_manager_create() again. Keeping the records lets
// window_manager_start() rebuild the topmost one automatically instead of leaving that app
// stuck with no window forever.
s.started = false;
mutex_unlock(&s.mutex);

View File

@ -16,6 +16,7 @@
#include <tactility/drivers/wifi.h>
#include <tactility/error_esp32.h>
#include <tactility/log.h>
#include <tactility/time.h>
#if defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED)
#include <tactility/drivers/esp32_esp_hosted_ota.h>
@ -56,6 +57,15 @@ struct Esp32WifiCtx {
esp_event_handler_instance_t wifiEventHandler = nullptr;
esp_event_handler_instance_t ipEventHandler = nullptr;
// Dedup for WIFI_EVENT/IP_EVENT notifications: on the esp_hosted/Wi-Fi Remote transport
// (e.g. Tab5's P4 host + C6 co-processor), the RPC layer has been observed delivering the
// exact same event twice in a row (same base, same event_id, same millisecond - not two
// genuinely separate occurrences). Native WiFi doesn't exhibit this, but the handler is
// shared, so the guard applies unconditionally; it's a no-op for well-separated real events.
esp_event_base_t lastEventBase = nullptr;
int32_t lastEventId = -1;
TickType_t lastEventTick = 0;
Mutex callbackMutex{};
WifiCallbackEntry callbacks[WIFI_MAX_CALLBACKS] = {};
size_t callbackCount = 0;
@ -100,6 +110,20 @@ void fire_event(Esp32WifiCtx* ctx, WifiEvent event) {
void on_wifi_or_ip_event(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
auto* ctx = static_cast<Esp32WifiCtx*>(arg);
// See Esp32WifiCtx::lastEventBase/lastEventId/lastEventTick - collapse an immediate duplicate
// delivery of the same event (observed on the esp_hosted/Wi-Fi Remote transport) into one.
constexpr uint32_t DEDUP_WINDOW_MS = 50; // well under any real re-occurrence of the same event
TickType_t now = get_ticks();
bool is_duplicate = event_base == ctx->lastEventBase && event_id == ctx->lastEventId &&
(now - ctx->lastEventTick) <= millis_to_ticks(DEDUP_WINDOW_MS);
ctx->lastEventBase = event_base;
ctx->lastEventId = event_id;
ctx->lastEventTick = now;
if (is_duplicate) {
LOG_D(TAG, "Ignoring duplicate WiFi event %d", (int)event_id);
return;
}
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
mutex_lock(&ctx->mutex);
bool was_pending = ctx->stationState == WIFI_STATION_STATE_CONNECTION_PENDING;

View File

@ -42,6 +42,7 @@
#include <gps_meshtastic/module.h>
#include <crypt/module.h>
#include <lvgl/devices/keyboard.h>
#include <lvgl/devices/pointer.h>
#include <lvgl/lvgl.h>
#include <lvgl/module.h>
@ -361,6 +362,9 @@ static void stopAppFromToolbar(lv_event_t*) {
app_event_emit(topmost, &event);
}
// The on-screen keyboard widget itself, constructed during windowManagerScreenInit
static LvglSoftwareKeyboard softwareKeyboard { .object = nullptr };
static lv_obj_t* windowManagerScreenInit(lv_obj_t* root) {
lv_obj_t* vertical_container = lv_obj_create(root);
lv_obj_set_size(vertical_container, LV_PCT(100), LV_PCT(100));
@ -380,6 +384,11 @@ static lv_obj_t* windowManagerScreenInit(lv_obj_t* root) {
lv_obj_set_flex_grow(app_container, 1);
lv_obj_set_flex_flow(app_container, LV_FLEX_FLOW_COLUMN);
// Parented to root (not app_container/vertical_container) so it overlays on top of
// everything, including the statusbar, regardless of which app is showing. Hidden until a
// focused textarea shows it (see lvgl_keyboard_add_textarea()/textarea_show_keyboard()).
lvgl_software_keyboard_construct(&softwareKeyboard, root);
return app_container;
}
@ -440,6 +449,10 @@ static void onLvglStarted() {
}
static void onLvglStopped() {
if (softwareKeyboard.object != nullptr) {
lvgl_software_keyboard_destruct(&softwareKeyboard);
}
module_stop(&lvgl_window_manager_module);
lvgl::stopUsbHidInput();

View File

@ -175,19 +175,25 @@ int32_t appMain(uint32_t appInstanceId, int argc, char* argv[]) {
return 0;
}
ctx.timer = std::make_unique<Timer>(Timer::Type::Periodic, pdMS_TO_TICKS(1000), [&ctx] {
if (lvgl_is_running()) {
lvgl_lock();
updateViewState(&ctx);
lvgl_unlock();
}
});
AppEventSubscription sub {};
sub.app_instance_id = appInstanceId;
app_event_subscribe(&sub);
WindowId window = window_manager_create(appInstanceId, createWidgets, &ctx);
ctx.timer = std::make_unique<Timer>(Timer::Type::Periodic, pdMS_TO_TICKS(1000), [&ctx, window] {
if (lvgl_is_running()) {
lvgl_lock();
// Widgets only exist while this window is topmost - skip otherwise. Another app
// (started non-modally, e.g. via app_manager_start()) can bury this window without
// stopping this instance or notifying it; window_manager deletes a buried window's
// widgets, so touching ctx->statusLabel here would use-after-free it.
if (window_manager_get_state(window) == WINDOW_STATE_GRANTED) {
updateViewState(&ctx);
}
lvgl_unlock();
}
});
ctx.timer->start();
bool shouldClose = false;

View File

@ -1,5 +1,6 @@
#include <tactility/device.h>
#include <tactility/drivers/display.h>
#include <tactility/drivers/sdcard.h>
#include <tactility/drivers/spi_controller.h>
#include <tactility/filesystem/file_mutex.h>
#include <tactility/filesystem/file_system.h>
@ -75,6 +76,29 @@ void initFileMutexForLvgl() {
return true;
});
// SDMMC-backed SD cards aren't parented under SPI_CONTROLLER_TYPE, so the pass above never
// sees them - but on some chips (classic ESP32) SDMMC and SPI still contend for DMA/bus
// access. Lock every SD card mount if a display exists anywhere, regardless of bus topology.
if (!device_exists_of_type(&DISPLAY_TYPE)) {
return;
}
file_system_for_each(nullptr, [](FileSystem* fs, void* context) {
char mount_path[64];
if (file_system_get_path(fs, mount_path, sizeof(mount_path)) != ERROR_NONE) {
return true;
}
auto* owner = file_system_get_owner(fs);
if (owner == nullptr || device_get_type(owner) != &SDCARD_TYPE) {
return true;
}
LOG_I(TAG, "Adding file mutex for %s (SD card) - a display is present and may contend for bus/DMA resources", mount_path);
file_mutex_register(&lvgl_mutex, mount_path);
return true;
});
}
}

View File

@ -1,8 +1,8 @@
#include <Tactility/LogMessages.h>
#include <Tactility/StringUtils.h>
#include <Tactility/file/File.h>
#include <Tactility/network/HttpdReq.h>
#include <tactility/filesystem/file_mutex.h>
#include <tactility/log.h>
#include <memory>
@ -186,30 +186,59 @@ size_t receiveFile(httpd_req_t* request, size_t length, const std::string& fileP
char buffer[BUFFER_SIZE];
size_t bytes_received = 0;
file::FileMutexGuard guard(filePath);
// Locked only around each actual disk I/O call below, not across the httpd_req_recv() waits
// in between - this file's mutex may resolve to lvgl_lock() (see FileMutexLvgl.cpp), and
// holding that for the whole (potentially multi-second) network transfer starves LVGL's own
// task for the entire upload instead of just for each brief write.
FileMutex mutex {};
file_mutex_get(&mutex, filePath.c_str());
file_mutex_lock(&mutex);
auto* file = fopen(filePath.c_str(), "wb");
file_mutex_unlock(&mutex);
if (file == nullptr) {
LOG_E(TAG, "Failed to open file for writing: %s", filePath.c_str());
return 0;
}
constexpr int MAX_TIMEOUT_RETRIES = 5;
int timeout_retries = 0;
while (bytes_received < length) {
auto expected_chunk_size = std::min<size_t>(BUFFER_SIZE, length - bytes_received);
size_t receive_chunk_size = httpd_req_recv(request, buffer, expected_chunk_size);
if (receive_chunk_size <= 0) {
int received = httpd_req_recv(request, buffer, expected_chunk_size);
if (received == HTTPD_SOCK_ERR_TIMEOUT) {
// Timeout - retry with backoff, same as receiveByteArray(). A large file takes many
// more chunks (and much longer overall) than the small reads elsewhere in this file,
// so it's far more likely to hit at least one transient stall somewhere along the way.
timeout_retries++;
if (timeout_retries >= MAX_TIMEOUT_RETRIES) {
LOG_E(TAG, "Recv timeout after %d retries, wrote %zu/%zu bytes", timeout_retries, bytes_received, length);
break;
}
LOG_W(TAG, "Recv timeout, retry %d/%d", timeout_retries, MAX_TIMEOUT_RETRIES);
vTaskDelay(pdMS_TO_TICKS(100 * timeout_retries)); // Exponential backoff
continue;
}
if (received <= 0) {
LOG_E(TAG, "Receive failed, got 0 bytes but expected %zu more", length - bytes_received);
break;
}
if (fwrite(buffer, 1, receive_chunk_size, file) != receive_chunk_size) {
timeout_retries = 0;
size_t receive_chunk_size = (size_t)received;
file_mutex_lock(&mutex);
bool write_ok = fwrite(buffer, 1, receive_chunk_size, file) == receive_chunk_size;
file_mutex_unlock(&mutex);
if (!write_ok) {
LOG_E(TAG, "Failed to write all bytes");
break;
}
bytes_received += receive_chunk_size;
}
// Write file
file_mutex_lock(&mutex);
fclose(file);
file_mutex_unlock(&mutex);
return bytes_received;
}

View File

@ -113,7 +113,7 @@ esp_err_t DevelopmentService::handleAppRun(httpd_req_t* request) {
}
}
app_manager_start(app_id, &instance_id);
app_manager_start(id_key_pos->second.c_str(), &instance_id);
LOG_I(TAG, "[200] /app/run %s", id_key_pos->second.c_str());
httpd_resp_send(request, nullptr, 0);
@ -193,7 +193,7 @@ esp_err_t DevelopmentService::handleAppInstall(httpd_req_t* request) {
LOG_W(TAG, "We have more bytes at the end of the request parsing?!");
}
if (!app_install(file_path.c_str())) {
if (app_install(file_path.c_str()) != ERROR_NONE) {
httpd_resp_send_err(request, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to install");
return ESP_FAIL;
}
@ -231,7 +231,7 @@ esp_err_t DevelopmentService::handleAppUninstall(httpd_req_t* request) {
return ESP_OK;
}
if (app_uninstall(id_key_pos->second.c_str())) {
if (app_uninstall(id_key_pos->second.c_str()) == ERROR_NONE) {
LOG_I(TAG, "[200] /app/uninstall %s", id_key_pos->second.c_str());
httpd_resp_send(request, nullptr, 0);
return ESP_OK;

View File

@ -138,20 +138,24 @@ bool contains(const std::string& ssid) {
bool load(const std::string& ssid, WifiApSettings& apSettings) {
auto service_context = findServiceContext();
if (service_context == nullptr) {
LOG_E(TAG, "No service context");
return false;
}
const auto file_path = getApPropertiesFilePath(service_context->getPaths(), ssid);
if (!file::isFile(file_path)) {
LOG_E(TAG, "Not a file: %s", file_path.c_str());
return false;
}
std::map<std::string, std::string> map;
if (!file::loadPropertiesFile(file_path, map)) {
LOG_E(TAG, "Failed to load properties from %s", file_path.c_str());
return false;
}
// SSID is required
if (!map.contains(AP_PROPERTIES_KEY_SSID)) {
LOG_E(TAG, "File does not contain SSID: %s", file_path.c_str());
return false;
}
@ -166,6 +170,7 @@ bool load(const std::string& ssid, WifiApSettings& apSettings) {
} else if (decrypt(ssid, encrypted_password, password_decrypted)) {
apSettings.password = password_decrypted;
} else {
LOG_E(TAG, "Failed to decrypt password from %s", file_path.c_str());
return false;
}
} else {

View File

@ -66,6 +66,17 @@ struct KeyboardApi {
* @retval ERROR_NOT_SUPPORTED when this device has no backlight
*/
error_t (*get_backlight)(struct Device* device, struct Device** backlight_device);
/**
* @brief Optional: reports whether the keyboard is physically present right now. Only
* meaningful for hot-pluggable/detachable keyboards (e.g. a removable accessory) whose
* kernel device is constructed and started once at boot regardless of physical attachment -
* leave NULL for a keyboard that's always physically present whenever its device is active
* (the common case; callers must treat NULL the same as "always present").
* @param[in] device the keyboard device
* @return true if physically attached/present
*/
bool (*is_present)(struct Device* device);
};
/**
@ -83,6 +94,14 @@ error_t keyboard_read_key(struct Device* device, struct KeyboardKeyData* data);
*/
error_t keyboard_get_backlight(struct Device* device, struct Device** backlight_device);
/**
* @brief Whether the keyboard device is physically present right now. True when the driver
* doesn't implement KeyboardApi::is_present (i.e. it's always physically present whenever its
* device is active) - see that field's doc comment.
* @param[in] device the keyboard device
*/
bool keyboard_is_present(struct Device* device);
extern const struct DeviceType KEYBOARD_TYPE;
#ifdef __cplusplus

View File

@ -28,6 +28,16 @@ error_t keyboard_get_backlight(Device* device, Device** backlight_device) {
return KEYBOARD_DRIVER_API(driver)->get_backlight(device, backlight_device);
}
bool keyboard_is_present(Device* device) {
const auto* driver = device_get_driver(device);
if (KEYBOARD_DRIVER_API(driver)->is_present == nullptr) {
return true;
}
return KEYBOARD_DRIVER_API(driver)->is_present(device);
}
const DeviceType KEYBOARD_TYPE {
.name = "keyboard"
};