From 3085bac5f15bd7ae4bbb6c40031026c6f5d17e88 Mon Sep 17 00:00:00 2001 From: Adolfo Date: Sat, 25 Jul 2026 15:23:54 -0400 Subject: [PATCH] fix(ImmichElias): merge dynamic sizing 480x320 + crash fix, BookPlayer UI, random, cache offline, slideshow 60s --- Apps/ImmichElias/main/CMakeLists.txt | 2 +- Apps/ImmichElias/main/Source/main.c | 855 +++++++++++++++++++-------- 2 files changed, 600 insertions(+), 257 deletions(-) diff --git a/Apps/ImmichElias/main/CMakeLists.txt b/Apps/ImmichElias/main/CMakeLists.txt index b4429a6..3c843ae 100644 --- a/Apps/ImmichElias/main/CMakeLists.txt +++ b/Apps/ImmichElias/main/CMakeLists.txt @@ -6,4 +6,4 @@ idf_component_register( INCLUDE_DIRS Source "$ENV{IDF_PATH}/components/json/cJSON" REQUIRES TactilitySDK esp_http_client ) -target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-format-truncation -Wno-unused-but-set-variable) +target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-format-truncation -Wno-unused-but-set-variable -Wno-unused-function -Wno-unused-variable) diff --git a/Apps/ImmichElias/main/Source/main.c b/Apps/ImmichElias/main/Source/main.c index da02ae8..9b8e2f6 100644 --- a/Apps/ImmichElias/main/Source/main.c +++ b/Apps/ImmichElias/main/Source/main.c @@ -6,13 +6,17 @@ #include #include #include +#include #include #include #include #include #include +#include #include +#include +#include #include #include @@ -28,12 +32,11 @@ static const char* TAG = "ImmichElias"; #define RESIZE_PROXY_URL "http://192.168.68.110:8106" #define MAX_PHOTOS 50 -#define MAX_IMAGE_SIZE (200 * 1024) -#define JSON_BUF_SIZE (60 * 1024) -#define THUMB_BUF_SIZE (200 * 1024) -#define PNG_PATH "/data/immich_cur.png" -#define PNG_PATH_FB1 "/tmp/immich_cur.png" -#define PNG_PATH_FB2 "/sdcard/immich_cur.png" +#define JSON_BUF_SIZE (64 * 1024) +#define THUMB_BUF_SIZE (512 * 1024) + +#define FALLBACK_W 320 +#define FALLBACK_H 240 typedef struct { char id[64]; @@ -47,7 +50,6 @@ typedef struct { static Photo photos[MAX_PHOTOS]; static int photo_count = 0; -static int current_index = 0; typedef struct { uint8_t* buf; @@ -60,8 +62,14 @@ static esp_err_t http_evt_handler(esp_http_client_event_t* evt) { if (hb == NULL) return ESP_OK; if (evt->event_id == HTTP_EVENT_ON_DATA && evt->data && evt->data_len > 0) { size_t copy_len = (size_t)evt->data_len; - if (hb->len + copy_len > hb->cap) copy_len = hb->cap - hb->len; - if (copy_len > 0) { memcpy(hb->buf + hb->len, evt->data, copy_len); hb->len += copy_len; } + if (hb->len + copy_len > hb->cap) { + size_t remaining = (hb->cap > hb->len) ? hb->cap - hb->len : 0; + if (remaining < copy_len) copy_len = remaining; + } + if (copy_len > 0) { + memcpy(hb->buf + hb->len, evt->data, copy_len); + hb->len += copy_len; + } } return ESP_OK; } @@ -72,7 +80,7 @@ static int http_get_with_auth(const char* url, HttpBuf* out) { .url = url, .event_handler = http_evt_handler, .user_data = out, - .timeout_ms = 15000, + .timeout_ms = 8000, .buffer_size = 4096, .buffer_size_tx = 1024, }; @@ -92,7 +100,7 @@ static int http_get_no_auth(const char* url, HttpBuf* out) { .url = url, .event_handler = http_evt_handler, .user_data = out, - .timeout_ms = 20000, + .timeout_ms = 8000, .buffer_size = 4096, .buffer_size_tx = 1024, }; @@ -112,7 +120,7 @@ static int http_post_json(const char* url, const char* json_body, HttpBuf* out) .url = url, .event_handler = http_evt_handler, .user_data = out, - .timeout_ms = 15000, + .timeout_ms = 8000, .buffer_size = 4096, .buffer_size_tx = 2048, }; @@ -128,24 +136,41 @@ static int http_post_json(const char* url, const char* json_body, HttpBuf* out) return (err == ESP_OK) ? status : -1; } +static void shuffle_photos(void) { + if (photo_count <= 1) return; + for (int i = photo_count - 1; i > 0; i--) { + uint32_t rnd = esp_random(); + int j = rnd % (i + 1); + if (i != j) { + Photo tmp = photos[i]; + photos[i] = photos[j]; + photos[j] = tmp; + } + } + ESP_LOGI(TAG, "Photos shuffled random, count %d", photo_count); +} + static int fetch_person_assets(void) { char* json_buf = heap_caps_malloc(JSON_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); - if (!json_buf) { json_buf = malloc(JSON_BUF_SIZE); if (!json_buf) return -1; } + if (!json_buf) json_buf = heap_caps_malloc(JSON_BUF_SIZE, MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT); + if (!json_buf) return -1; HttpBuf hb = { .buf = (uint8_t*)json_buf, .cap = JSON_BUF_SIZE - 1, .len = 0 }; char url[256]; snprintf(url, sizeof(url), "%s/api/search/metadata", IMMICH_BASE_URL); char body[256]; snprintf(body, sizeof(body), "{\"personIds\":[\"%s\"],\"size\":%d}", IMMICH_PERSON_ID, MAX_PHOTOS); int status = http_post_json(url, body, &hb); - if (status != 200) { free(json_buf); return -1; } + if (status != 200) { heap_caps_free(json_buf); return -1; } + if (hb.len >= JSON_BUF_SIZE) hb.len = JSON_BUF_SIZE - 1; json_buf[hb.len] = '\0'; cJSON* root = cJSON_Parse(json_buf); - if (!root) { free(json_buf); return -1; } + heap_caps_free(json_buf); + if (!root) return -1; cJSON* assets = cJSON_GetObjectItem(root, "assets"); cJSON* items = NULL; if (assets) items = cJSON_GetObjectItem(assets, "items"); else { items = cJSON_GetObjectItem(root, "items"); if (!items && cJSON_IsArray(root)) items = root; } - if (!items || !cJSON_IsArray(items)) { cJSON_Delete(root); free(json_buf); return -1; } + if (!items || !cJSON_IsArray(items)) { cJSON_Delete(root); return -1; } int n = cJSON_GetArraySize(items); photo_count = 0; for (int i = 0; i < n && photo_count < MAX_PHOTOS; i++) { @@ -161,124 +186,368 @@ static int fetch_person_assets(void) { cJSON* jfc = cJSON_GetObjectItem(it, "fileCreatedAt"); if (jfc && cJSON_IsString(jfc)) strncpy(p->fileCreatedAt, jfc->valuestring, sizeof(p->fileCreatedAt)-1); cJSON* jfn = cJSON_GetObjectItem(it, "originalFileName"); if (jfn && cJSON_IsString(jfn)) strncpy(p->originalFileName, jfn->valuestring, sizeof(p->originalFileName)-1); cJSON* jt = cJSON_GetObjectItem(it, "type"); if (jt && cJSON_IsString(jt)) strncpy(p->type, jt->valuestring, sizeof(p->type)-1); - char tmp_id[64]; strncpy(tmp_id, p->id, sizeof(tmp_id)-1); tmp_id[sizeof(tmp_id)-1]='\0'; - snprintf(p->thumbnail_url, sizeof(p->thumbnail_url), "%s/api/assets/%s/thumbnail?size=thumbnail", IMMICH_BASE_URL, tmp_id); photo_count++; } cJSON_Delete(root); - free(json_buf); - current_index = 0; + shuffle_photos(); return photo_count; } -static const char* save_buffer_to_file(const uint8_t* buf, size_t len) { - const char* candidates[] = { PNG_PATH, PNG_PATH_FB1, PNG_PATH_FB2, "/data/immich.png", "/tmp/immich.png" }; - for (int i=0;i<5;i++) { - const char* path = candidates[i]; - FILE* f = fopen(path, "wb"); - if (f) { - size_t written = fwrite(buf, 1, len, f); - fclose(f); - ESP_LOGI(TAG, "Saved %d bytes to %s written %d", (int)len, path, (int)written); - if (written == len) return path; - } else { - ESP_LOGW(TAG, "fopen fail %s", path); +static bool ensure_dir_exists(const char* file_path) { + char dir[320]; + strncpy(dir, file_path, sizeof(dir)-1); + dir[sizeof(dir)-1] = '\0'; + char* last_slash = strrchr(dir, '/'); + if (!last_slash) return true; + *last_slash = '\0'; + if (strlen(dir) == 0) return true; + struct stat st; + if (stat(dir, &st) == 0 && S_ISDIR(st.st_mode)) return true; + char tmp[320]; tmp[0]='\0'; + if (dir[0]=='/') strcpy(tmp, "/"); + char work[320]; + strncpy(work, dir, sizeof(work)-1); + work[sizeof(work)-1]='\0'; + char* p = work; + if (p[0]=='/') p++; + char* start = p; + while (1) { + char* slash = strchr(start, '/'); + bool last = false; + if (slash) *slash='\0'; else last=true; + if (strlen(start)>0) { + if (strlen(tmp)>0 && tmp[strlen(tmp)-1]!='/') strncat(tmp, "/", sizeof(tmp)-strlen(tmp)-1); + strncat(tmp, start, sizeof(tmp)-strlen(tmp)-1); + mkdir(tmp, 0777); } + if (last) break; + *slash='/'; + start = slash+1; + if (*start=='\0') break; } - return NULL; -} - -static int download_thumbnail_to_buf(const char* assetId, uint8_t* buf, size_t cap, size_t* out_len) { - char url[320]; - snprintf(url, sizeof(url), "%s/api/assets/%s/thumbnail?size=thumbnail", IMMICH_BASE_URL, assetId); - HttpBuf hb = { .buf = buf, .cap = cap, .len = 0 }; - int status = http_get_with_auth(url, &hb); - if (status == 200) { if (out_len) *out_len = hb.len; return (int)hb.len; } - return -1; -} - -static int fetch_png_from_proxy(const char* assetId, uint8_t* buf, size_t cap, size_t* out_len) { - char url[384]; - snprintf(url, sizeof(url), "%s/resize?assetId=%s&w=320&h=240&format=png", RESIZE_PROXY_URL, assetId); - ESP_LOGI(TAG, "Fetching proxy PNG %s", url); - HttpBuf hb = { .buf = buf, .cap = cap, .len = 0 }; - int status = http_get_no_auth(url, &hb); - if (status == 200 && hb.len > 0) { - if (out_len) *out_len = hb.len; - ESP_LOGI(TAG, "Proxy PNG OK %d bytes", (int)hb.len); - return (int)hb.len; - } - ESP_LOGE(TAG, "Proxy PNG fail id %s status %d", assetId, status); - return -1; + return true; } typedef struct { AppHandle app; - bool visible; - lv_obj_t* lbl_title; - lv_obj_t* lbl_count; + volatile bool visible; + volatile bool stop_requested; + lv_obj_t* player_wrapper; + lv_obj_t* header_bar; + lv_obj_t* ctrl_bar; + lv_obj_t* lbl_book_title; + lv_obj_t* lbl_indicator; + lv_obj_t* img_page; lv_obj_t* lbl_info; lv_obj_t* lbl_status; - lv_obj_t* lbl_index; - lv_obj_t* img; lv_obj_t* btn_prev; lv_obj_t* btn_next; lv_obj_t* btn_reload; + lv_obj_t* btn_back; + lv_timer_t* slideshow_timer; + lv_timer_t* autohide_timer; int current_idx; - char status_text[200]; - bool fetch_in_progress; - bool thumb_in_progress; + char status_text[256]; + volatile bool fetch_in_progress; + volatile bool thumb_in_progress; uint8_t* thumb_buf; size_t thumb_len; TaskHandle_t fetch_task; TaskHandle_t thumb_task; - bool stop_requested; - char saved_img_path[256]; - bool has_image; + char saved_img_path[320]; + volatile bool has_image; + int disp_w; + int disp_h; + int img_w; + int img_h; } AppCtx; +static bool get_cache_dir_path(AppCtx* ctx, char* out_dir, size_t out_size) { + if (ctx && ctx->app) { + size_t sz = out_size; + tt_app_get_user_data_child_path(ctx->app, "cache", out_dir, &sz); + if (out_dir[0]!='\0') { mkdir(out_dir, 0777); ensure_dir_exists(out_dir); return true; } + } + snprintf(out_dir, out_size, "/sdcard/tactility/immich_cache"); + mkdir(out_dir, 0777); + return true; +} + +static bool get_cache_path_for_asset(AppCtx* ctx, const char* assetId, char* out_path, size_t out_size) { + if (!assetId || !out_path || out_size==0) return false; + int w = ctx ? ctx->img_w : FALLBACK_W; + int h = ctx ? ctx->img_h : FALLBACK_H; + if (w<=0) w=FALLBACK_W; + if (h<=0) h=FALLBACK_H; + if (w>800) w=800; + if (h>600) h=600; + if (ctx && ctx->app) { + char child[160]; + snprintf(child, sizeof(child), "cache/%s_%dx%d.png", assetId, w, h); + size_t sz = out_size; + tt_app_get_user_data_child_path(ctx->app, child, out_path, &sz); + if (out_path[0]!='\0') { ensure_dir_exists(out_path); return true; } + } + snprintf(out_path, out_size, "/sdcard/tactility/immich_cache/%s_%dx%d.png", assetId, w, h); + ensure_dir_exists(out_path); + return true; +} + +static bool get_cache_path_legacy(AppCtx* ctx, const char* assetId, char* out_path, size_t out_size) { + if (!assetId || !out_path) return false; + if (ctx && ctx->app) { + char child[128]; + snprintf(child, sizeof(child), "cache/%s.png", assetId); + size_t sz = out_size; + tt_app_get_user_data_child_path(ctx->app, child, out_path, &sz); + if (out_path[0]!='\0') { ensure_dir_exists(out_path); return true; } + } + snprintf(out_path, out_size, "/sdcard/tactility/immich_cache/%s.png", assetId); + ensure_dir_exists(out_path); + return true; +} + +static bool get_image_save_path(AppCtx* ctx, char* out_path, size_t out_size) { + if (ctx && ctx->app) { + size_t sz = out_size; + tt_app_get_user_data_child_path(ctx->app, "immich_cur.png", out_path, &sz); + if (out_path[0]!='\0') { ensure_dir_exists(out_path); return true; } + } + snprintf(out_path, out_size, "/sdcard/immich_cur.png"); + return true; +} + +static const char* save_buffer_to_cache(AppCtx* ctx, const char* assetId, const uint8_t* buf, size_t len, char* out_saved_path, size_t out_saved_size) { + char path[320]; + if (!get_cache_path_for_asset(ctx, assetId, path, sizeof(path))) return NULL; + ensure_dir_exists(path); + FILE* f = fopen(path, "wb"); + if (!f) { + ESP_LOGW(TAG, "fopen fail cache %s", path); + return NULL; + } + size_t written = fwrite(buf, 1, len, f); + fclose(f); + ESP_LOGI(TAG, "Cached %d bytes to %s written %d (req %dx%d)", (int)len, path, (int)written, ctx ? ctx->img_w : 0, ctx ? ctx->img_h : 0); + if (written != len) return NULL; + if (out_saved_path && out_saved_size>0) { strncpy(out_saved_path, path, out_saved_size-1); out_saved_path[out_saved_size-1]='\0'; } + char legacy[320]; + if (get_image_save_path(ctx, legacy, sizeof(legacy))) { + FILE* lf = fopen(legacy, "wb"); + if (lf) { fwrite(buf,1,len,lf); fclose(lf); } + } + return out_saved_path; +} + +static bool load_from_cache(AppCtx* ctx, const char* assetId, char* out_path, size_t out_size, size_t* out_file_size) { + char path[320]; + if (!get_cache_path_for_asset(ctx, assetId, path, sizeof(path))) return false; + struct stat st; + if (stat(path, &st)==0 && st.st_size>0 && st.st_size <= THUMB_BUF_SIZE) { + FILE* f = fopen(path, "rb"); + if (f) { fclose(f); strncpy(out_path, path, out_size-1); out_path[out_size-1]='\0'; if(out_file_size) *out_file_size=(size_t)st.st_size; ESP_LOGI(TAG,"Cache HIT %s size %d (sized)", path, (int)st.st_size); return true; } + } + // Try legacy without size + if (!get_cache_path_legacy(ctx, assetId, path, sizeof(path))) return false; + if (stat(path, &st)==0 && st.st_size>0 && st.st_size <= THUMB_BUF_SIZE) { + FILE* f = fopen(path, "rb"); + if (f) { fclose(f); strncpy(out_path, path, out_size-1); out_path[out_size-1]='\0'; if(out_file_size) *out_file_size=(size_t)st.st_size; ESP_LOGI(TAG,"Cache HIT %s size %d (legacy)", path, (int)st.st_size); return true; } + } + return false; +} + +static int scan_cache_dir_for_photos(AppCtx* ctx) { + char dir_path[320]; + if (!get_cache_dir_path(ctx, dir_path, sizeof(dir_path))) return 0; + DIR* d = opendir(dir_path); + if (!d) return 0; + photo_count = 0; + struct dirent* ent; + while ((ent = readdir(d))!=NULL && photo_count < MAX_PHOTOS) { + if (ent->d_name[0]=='.') continue; + size_t len = strlen(ent->d_name); + if (len<5) continue; + const char* ext = ent->d_name + len -4; + if (!(ext[0]=='.' && (ext[1]=='p'||ext[1]=='P') && (ext[2]=='n'||ext[2]=='N') && (ext[3]=='g'||ext[3]=='G'))) continue; + char id_buf[64]; + strncpy(id_buf, ent->d_name, sizeof(id_buf)-1); + id_buf[sizeof(id_buf)-1]='\0'; + // Strip size suffix _WxH if present + char* us = strrchr(id_buf, '_'); + if (us) { + // Check if after _ is like 320x240.png + char* dot = strrchr(id_buf, '.'); + if (dot) *dot='\0'; + // If us contains x, truncate at _ + // Actually id may contain _, but we want id part before _ + // For cache files like _x.png, we want id before _ + // The id itself is uuid without _, so first _ is separator + // So keep part before _ + if (us != id_buf) { + // Ensure remaining part after _ contains x + if (strchr(us, 'x') || strchr(us, 'X')) { + *us='\0'; + } else { + // No x, it was legacy without size, keep as is but already stripped extension + } + } + } else { + char* dot2 = strrchr(id_buf, '.'); + if (dot2) *dot2='\0'; + } + if (strlen(id_buf)==0) continue; + // Deduplicate: check if id already exists + bool dup=false; + for (int i=0;iid, id_buf, sizeof(p->id)-1); + strncpy(p->originalFileName, ent->d_name, sizeof(p->originalFileName)-1); + strncpy(p->type, "IMAGE", sizeof(p->type)-1); + photo_count++; + } + closedir(d); + if (photo_count>0) shuffle_photos(); + return photo_count; +} + +static int fetch_png_from_proxy(const char* assetId, uint8_t* buf, size_t cap, size_t* out_len, int req_w, int req_h) { + if (req_w<=0) req_w=FALLBACK_W; + if (req_h<=0) req_h=FALLBACK_H; + if (req_w>800) req_w=800; + if (req_h>600) req_h=600; + char url[384]; + snprintf(url, sizeof(url), "%s/resize?assetId=%s&w=%d&h=%d&format=png", RESIZE_PROXY_URL, assetId, req_w, req_h); + ESP_LOGI(TAG, "Fetching proxy PNG %s (req %dx%d)", url, req_w, req_h); + HttpBuf hb = { .buf = buf, .cap = cap, .len = 0 }; + int status = http_get_no_auth(url, &hb); + if (status==200 && hb.len>0) { + if (out_len) *out_len=hb.len; + ESP_LOGI(TAG, "Proxy PNG OK %d bytes for %dx%d", (int)hb.len, req_w, req_h); + return (int)hb.len; + } + ESP_LOGE(TAG, "Proxy PNG fail id %s %dx%d status %d", assetId, req_w, req_h, status); + return -1; +} + static void start_fetch_task(AppCtx* ctx); static void start_thumb_task(AppCtx* ctx); static void ui_update_all(AppCtx* ctx); static void thumb_task_fn(void* arg); static void fetch_task_fn(void* arg); +static void hide_chrome(AppCtx* ctx); +static void show_chrome(AppCtx* ctx); + +static void detect_display_size(AppCtx* ctx, lv_obj_t* parent) { + if (!ctx) return; + int w=FALLBACK_W, h=FALLBACK_H; + lv_display_t* disp=NULL; + if (parent) disp = lv_obj_get_display(parent); + if (!disp) disp = lv_display_get_default(); + if (disp) { + int dw = lv_display_get_horizontal_resolution(disp); + int dh = lv_display_get_vertical_resolution(disp); + if (dw>0 && dh>0) { w=dw; h=dh; } + } + if (w==FALLBACK_W && h==FALLBACK_H) { + int dw2 = lv_display_get_horizontal_resolution(NULL); + int dh2 = lv_display_get_vertical_resolution(NULL); + if (dw2>0 && dh2>0) { w=dw2; h=dh2; } + } + ctx->disp_w=w; ctx->disp_h=h; + // Clamp image size to max 320x240 for memory safety on ESP32, but allow up to 480x320 if display is larger + // For 480x320 display, use 480x200 (width full, height minus chrome) + // To avoid OOM, limit to 320x240 for now, or 480x320 max but with careful memory + int overhead=120; + if (h>=480) overhead=140; + else if (h<=240) overhead=80; + int target_h = h - overhead; + if (target_h<120) target_h = h*2/3; + int target_w = w; + // Memory safety: limit max to 480x320, but prefer 320x240 for stability + // If display is 480x320, use 480x200; else 320x240 + if (w>=480) { + target_w = 480; + if (target_h>240) target_h = 200; // keep area reasonable for 480px wide + } else { + if (target_w>320) target_w=320; + if (target_h>240) target_h=240; + } + if (target_w>480) target_w=480; + if (target_h>320) target_h=320; + ctx->img_w=target_w; ctx->img_h=target_h; + ESP_LOGI(TAG, "Display detected %dx%d -> img target %dx%d (clamped for mem)", w,h,target_w,target_h); +} + +static void hide_chrome(AppCtx* ctx) { + if (!ctx) return; + if (!tt_lvgl_lock(pdMS_TO_TICKS(500))) return; + if (ctx->header_bar) lv_obj_add_flag(ctx->header_bar, LV_OBJ_FLAG_HIDDEN); + if (ctx->ctrl_bar) lv_obj_add_flag(ctx->ctrl_bar, LV_OBJ_FLAG_HIDDEN); + if (ctx->lbl_info) lv_obj_add_flag(ctx->lbl_info, LV_OBJ_FLAG_HIDDEN); + tt_lvgl_unlock(); +} +static void show_chrome(AppCtx* ctx) { + if (!ctx) return; + if (!tt_lvgl_lock(pdMS_TO_TICKS(500))) return; + if (ctx->header_bar) lv_obj_remove_flag(ctx->header_bar, LV_OBJ_FLAG_HIDDEN); + if (ctx->ctrl_bar) lv_obj_remove_flag(ctx->ctrl_bar, LV_OBJ_FLAG_HIDDEN); + if (ctx->lbl_info) lv_obj_remove_flag(ctx->lbl_info, LV_OBJ_FLAG_HIDDEN); + if (ctx->lbl_status) lv_obj_remove_flag(ctx->lbl_status, LV_OBJ_FLAG_HIDDEN); + tt_lvgl_unlock(); + if (tt_lvgl_lock(pdMS_TO_TICKS(300))) { + if (ctx->autohide_timer) { lv_timer_reset(ctx->autohide_timer); lv_timer_resume(ctx->autohide_timer); } + tt_lvgl_unlock(); + } +} +static void autohide_timer_cb(lv_timer_t* timer) { + AppCtx* ctx = (AppCtx*)lv_timer_get_user_data(timer); + if (!ctx || !ctx->visible || ctx->stop_requested) return; + hide_chrome(ctx); + if (tt_lvgl_lock(pdMS_TO_TICKS(200))) { lv_timer_pause(timer); tt_lvgl_unlock(); } +} +static void slideshow_timer_cb(lv_timer_t* timer) { + AppCtx* ctx = (AppCtx*)lv_timer_get_user_data(timer); + if (!ctx || !ctx->visible || ctx->stop_requested) return; + if (ctx->fetch_in_progress || ctx->thumb_in_progress) return; + if (photo_count==0) return; + ESP_LOGI(TAG, "Slideshow auto-advance 60s %dx%d", ctx->img_w, ctx->img_h); + ctx->current_idx++; + if (ctx->current_idx >= photo_count) ctx->current_idx=0; + ctx->thumb_len=0; ctx->has_image=false; + snprintf(ctx->status_text, sizeof(ctx->status_text), "Slideshow %d/%d %dx%d", ctx->current_idx+1, photo_count, ctx->img_w, ctx->img_h); + ui_update_all(ctx); + show_chrome(ctx); + start_thumb_task(ctx); +} static void ui_update_all(AppCtx* ctx) { - if (!ctx || !ctx->visible) return; + if (!ctx || !ctx->visible || ctx->stop_requested) return; if (!tt_lvgl_lock(pdMS_TO_TICKS(1000))) return; - if (!ctx->visible) { tt_lvgl_unlock(); return; } - if (ctx->lbl_count) { - if (photo_count > 0) lv_label_set_text_fmt(ctx->lbl_count, "%s: %d / %s - PNG via 8106", PERSON_NAME, photo_count, PERSON_COUNT_STR); - else lv_label_set_text(ctx->lbl_count, ctx->status_text); - } - if (ctx->lbl_index) { - if (photo_count > 0) lv_label_set_text_fmt(ctx->lbl_index, "%d/%d", ctx->current_idx + 1, photo_count); - else lv_label_set_text(ctx->lbl_index, "0/0"); + if (!ctx->visible || ctx->stop_requested) { tt_lvgl_unlock(); return; } + if (ctx->lbl_book_title) lv_label_set_text_fmt(ctx->lbl_book_title, "%s - %s photos", PERSON_NAME, PERSON_COUNT_STR); + if (ctx->lbl_indicator) { + if (photo_count>0) lv_label_set_text_fmt(ctx->lbl_indicator, "%d/%d", ctx->current_idx+1, photo_count); + else lv_label_set_text(ctx->lbl_indicator, "0/0"); } if (ctx->lbl_info) { - if (photo_count > 0 && ctx->current_idx < photo_count) { - Photo* p = &photos[ctx->current_idx]; - lv_label_set_text_fmt(ctx->lbl_info, "ID: %.36s\nFile: %.32s\nDate: %.24s\n%dx%d %s", p->id, p->originalFileName, p->fileCreatedAt, p->width, p->height, p->type); + if (photo_count>0 && ctx->current_idx < photo_count) { + Photo* p=&photos[ctx->current_idx]; + lv_label_set_text_fmt(ctx->lbl_info, "%s\n%.24s\n%dx%d disp %dx%d img %dx%d", p->originalFileName, p->fileCreatedAt, p->width, p->height, ctx->disp_w, ctx->disp_h, ctx->img_w, ctx->img_h); } else lv_label_set_text(ctx->lbl_info, ctx->status_text); } if (ctx->lbl_status) { - if (ctx->thumb_len > 0) { - Photo* p = &photos[ctx->current_idx]; - if (ctx->has_image) lv_label_set_text_fmt(ctx->lbl_status, "PNG %d bytes OK\n%s\nID %.8s..", (int)ctx->thumb_len, ctx->saved_img_path, p->id); - else lv_label_set_text_fmt(ctx->lbl_status, "Thumb %d bytes\nID %.8s..\nSaved %s", (int)ctx->thumb_len, p->id, ctx->saved_img_path); - } else lv_label_set_text(ctx->lbl_status, ctx->status_text); + if (ctx->thumb_len>0 && ctx->has_image) lv_label_set_text_fmt(ctx->lbl_status, "PNG %d bytes %dx%d", (int)ctx->thumb_len, ctx->img_w, ctx->img_h); + else lv_label_set_text(ctx->lbl_status, ctx->status_text); } - if (ctx->img) { - if (ctx->has_image && ctx->saved_img_path[0] != '\0') { - char lvgl_path[320]; - if (ctx->saved_img_path[0] == '/') snprintf(lvgl_path, sizeof(lvgl_path), "A:%s", ctx->saved_img_path); - else snprintf(lvgl_path, sizeof(lvgl_path), "%s", ctx->saved_img_path); - lv_image_set_src(ctx->img, lvgl_path); - lv_obj_clear_flag(ctx->img, LV_OBJ_FLAG_HIDDEN); - lv_obj_set_size(ctx->img, 320, 240); - ESP_LOGI(TAG, "lv_img_set_src %s", lvgl_path); + if (ctx->img_page) { + if (ctx->has_image && ctx->saved_img_path[0]!='\0') { + FILE* test = fopen(ctx->saved_img_path, "rb"); + if (test) { fclose(test); char lvgl_path[384]; snprintf(lvgl_path, sizeof(lvgl_path), "A:%s", ctx->saved_img_path); lv_image_set_src(ctx->img_page, lvgl_path); lv_obj_set_size(ctx->img_page, ctx->img_w, ctx->img_h); } + else lv_image_set_src(ctx->img_page, LV_SYMBOL_IMAGE); } else { - lv_obj_add_flag(ctx->img, LV_OBJ_FLAG_HIDDEN); + if (photo_count==0) lv_image_set_src(ctx->img_page, LV_SYMBOL_IMAGE); } } tt_lvgl_unlock(); @@ -286,246 +555,320 @@ static void ui_update_all(AppCtx* ctx) { static void thumb_task_fn(void* arg) { AppCtx* ctx = (AppCtx*)arg; + if (!ctx) { vTaskDelete(NULL); return; } + if (ctx->stop_requested) { ctx->thumb_task=NULL; ctx->thumb_in_progress=false; vTaskDelete(NULL); return; } if (!ctx->thumb_buf) { ctx->thumb_buf = heap_caps_malloc(THUMB_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); - if (!ctx->thumb_buf) ctx->thumb_buf = malloc(THUMB_BUF_SIZE); - } - if (!ctx->thumb_buf) { - snprintf(ctx->status_text, sizeof(ctx->status_text), "OOM thumb buf"); - ui_update_all(ctx); - ctx->thumb_task = NULL; ctx->thumb_in_progress = false; vTaskDelete(NULL); return; + if (!ctx->thumb_buf) ctx->thumb_buf = heap_caps_malloc(THUMB_BUF_SIZE, MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT); } + if (!ctx->thumb_buf) { snprintf(ctx->status_text, sizeof(ctx->status_text), "OOM thumb buf"); ui_update_all(ctx); ctx->thumb_task=NULL; ctx->thumb_in_progress=false; vTaskDelete(NULL); return; } int idx = ctx->current_idx; - if (idx < 0 || idx >= photo_count) idx = 0; - Photo* p = &photos[idx]; - ESP_LOGI(TAG, "Downloading PNG idx %d id %s", idx, p->id); - snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetching PNG %d/%d via 8106", idx+1, photo_count); - ui_update_all(ctx); - - size_t out_len = 0; - int r = fetch_png_from_proxy(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &out_len); - if (r > 0) { - ctx->thumb_len = out_len; - const char* saved = save_buffer_to_file(ctx->thumb_buf, out_len); - if (saved) { - strncpy(ctx->saved_img_path, saved, sizeof(ctx->saved_img_path)-1); - ctx->has_image = true; - snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d bytes saved %s", (int)out_len, saved); - } else if (ctx->app) { - char tmp_path[256]; size_t tmp_sz = sizeof(tmp_path); - tt_app_get_user_data_child_path(ctx->app, "immich_cur.png", tmp_path, &tmp_sz); - FILE* f = fopen(tmp_path, "wb"); - if (f) { - fwrite(ctx->thumb_buf, 1, out_len, f); fclose(f); - strncpy(ctx->saved_img_path, tmp_path, sizeof(ctx->saved_img_path)-1); - ctx->has_image = true; - snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d via user_data %s", (int)out_len, tmp_path); - } else { - ctx->has_image = false; - snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d bytes but save fail", (int)out_len); - } - } else { - ctx->has_image = false; - snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d bytes no handle", (int)out_len); - } - } else { - ctx->thumb_len = 0; ctx->has_image = false; - snprintf(ctx->status_text, sizeof(ctx->status_text), "Proxy PNG fail %d fallback thumb", r); - size_t ol2=0; int r2 = download_thumbnail_to_buf(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &ol2); - if (r2>0) { ctx->thumb_len = ol2; snprintf(ctx->status_text, sizeof(ctx->status_text), "Thumb %d fallback", (int)ol2); } + if (idx<0 || idx>=photo_count) idx=0; + Photo* p=&photos[idx]; + char cache_path[320]={0}; size_t cached_size=0; + if (load_from_cache(ctx, p->id, cache_path, sizeof(cache_path), &cached_size)) { + strncpy(ctx->saved_img_path, cache_path, sizeof(ctx->saved_img_path)-1); + ctx->has_image=true; ctx->thumb_len=cached_size; + snprintf(ctx->status_text, sizeof(ctx->status_text), "Cached PNG %d bytes %dx%d", (int)cached_size, ctx->img_w, ctx->img_h); + ui_update_all(ctx); show_chrome(ctx); + ctx->thumb_task=NULL; ctx->thumb_in_progress=false; vTaskDelete(NULL); return; } + ESP_LOGI(TAG, "Downloading PNG idx %d id %s %dx%d", idx, p->id, ctx->img_w, ctx->img_h); + snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetching PNG %d/%d %dx%d", idx+1, photo_count, ctx->img_w, ctx->img_h); ui_update_all(ctx); - ctx->thumb_task = NULL; ctx->thumb_in_progress = false; vTaskDelete(NULL); + if (ctx->stop_requested) { ctx->thumb_task=NULL; ctx->thumb_in_progress=false; vTaskDelete(NULL); return; } + size_t out_len=0; + int r = fetch_png_from_proxy(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &out_len, ctx->img_w, ctx->img_h); + if (ctx->stop_requested) { ctx->thumb_task=NULL; ctx->thumb_in_progress=false; vTaskDelete(NULL); return; } + if (r>0) { + ctx->thumb_len=out_len; + char saved_tmp[320]={0}; + const char* saved = save_buffer_to_cache(ctx, p->id, ctx->thumb_buf, out_len, saved_tmp, sizeof(saved_tmp)); + if (saved) { strncpy(ctx->saved_img_path, saved_tmp, sizeof(ctx->saved_img_path)-1); ctx->has_image=true; snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d bytes %dx%d", (int)out_len, ctx->img_w, ctx->img_h); } + else { ctx->has_image=false; snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d save fail", (int)out_len); } + } else { + ctx->thumb_len=0; ctx->has_image=false; snprintf(ctx->status_text, sizeof(ctx->status_text), "Proxy PNG fail %d", r); + } + ui_update_all(ctx); show_chrome(ctx); + ctx->thumb_task=NULL; ctx->thumb_in_progress=false; vTaskDelete(NULL); } static void start_thumb_task(AppCtx* ctx) { - if (ctx->thumb_in_progress) return; - if (photo_count==0) return; - ctx->thumb_in_progress = true; ctx->thumb_len=0; ctx->has_image=false; - xTaskCreate(thumb_task_fn, "thumb_fetch", 8192, ctx, 5, &ctx->thumb_task); + if (!ctx || ctx->thumb_in_progress || ctx->stop_requested || photo_count==0) return; + ctx->thumb_in_progress=true; ctx->thumb_len=0; ctx->has_image=false; + if (xTaskCreate(thumb_task_fn, "thumb_fetch", 8192, ctx, 5, &ctx->thumb_task)!=pdPASS) { ctx->thumb_in_progress=false; ctx->thumb_task=NULL; } } static void fetch_task_fn(void* arg) { AppCtx* ctx = (AppCtx*)arg; - ctx->fetch_in_progress = true; + if (!ctx) { vTaskDelete(NULL); return; } + if (ctx->stop_requested) { ctx->fetch_task=NULL; ctx->fetch_in_progress=false; vTaskDelete(NULL); return; } + ctx->fetch_in_progress=true; snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetching %s photos...", PERSON_NAME); ui_update_all(ctx); int n = fetch_person_assets(); + if (ctx->stop_requested) { ctx->fetch_task=NULL; ctx->fetch_in_progress=false; vTaskDelete(NULL); return; } if (n>0) { - current_index=0; ctx->current_idx=0; - snprintf(ctx->status_text, sizeof(ctx->status_text), "Loaded %d photos, PNG via 8106...", n); + ctx->current_idx=0; + snprintf(ctx->status_text, sizeof(ctx->status_text), "Loaded %d photos (random %dx%d)", n, ctx->img_w, ctx->img_h); ui_update_all(ctx); if (!ctx->thumb_buf) { ctx->thumb_buf = heap_caps_malloc(THUMB_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); - if (!ctx->thumb_buf) ctx->thumb_buf = malloc(THUMB_BUF_SIZE); + if (!ctx->thumb_buf) ctx->thumb_buf = heap_caps_malloc(THUMB_BUF_SIZE, MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT); } - if (ctx->thumb_buf) { - size_t ol=0; + if (ctx->thumb_buf && !ctx->stop_requested) { Photo* p=&photos[0]; - int r=fetch_png_from_proxy(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &ol); - if (r>0) { - ctx->thumb_len=ol; - const char* saved=save_buffer_to_file(ctx->thumb_buf, ol); - if (saved) { strncpy(ctx->saved_img_path, saved, sizeof(ctx->saved_img_path)-1); ctx->has_image=true; snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d %s", (int)ol, saved); } - else if (ctx->app) { - char tmp_path[256]; size_t tmp_sz=sizeof(tmp_path); - tt_app_get_user_data_child_path(ctx->app, "immich_cur.png", tmp_path, &tmp_sz); - FILE* f=fopen(tmp_path,"wb"); if(f){fwrite(ctx->thumb_buf,1,ol,f); fclose(f); strncpy(ctx->saved_img_path, tmp_path, sizeof(ctx->saved_img_path)-1); ctx->has_image=true; snprintf(ctx->status_text,sizeof(ctx->status_text),"PNG %d user_data %s",(int)ol,tmp_path);} + char cache_path[320]={0}; size_t cached_size=0; + if (load_from_cache(ctx, p->id, cache_path, sizeof(cache_path), &cached_size)) { + strncpy(ctx->saved_img_path, cache_path, sizeof(ctx->saved_img_path)-1); + ctx->has_image=true; ctx->thumb_len=cached_size; + snprintf(ctx->status_text, sizeof(ctx->status_text), "Cached PNG %d random %dx%d", (int)cached_size, ctx->img_w, ctx->img_h); + ui_update_all(ctx); show_chrome(ctx); + } else { + size_t ol=0; + int r=fetch_png_from_proxy(p->id, ctx->thumb_buf, THUMB_BUF_SIZE, &ol, ctx->img_w, ctx->img_h); + if (ctx->stop_requested) { ctx->fetch_task=NULL; ctx->fetch_in_progress=false; vTaskDelete(NULL); return; } + if (r>0) { + ctx->thumb_len=ol; + char saved_tmp[320]={0}; + const char* saved=save_buffer_to_cache(ctx, p->id, ctx->thumb_buf, ol, saved_tmp, sizeof(saved_tmp)); + if (saved) { strncpy(ctx->saved_img_path, saved_tmp, sizeof(ctx->saved_img_path)-1); ctx->has_image=true; snprintf(ctx->status_text, sizeof(ctx->status_text), "PNG %d random %dx%d", (int)ol, ctx->img_w, ctx->img_h); } } + ui_update_all(ctx); show_chrome(ctx); } - ui_update_all(ctx); } } else { - snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetch failed %s", IMMICH_BASE_URL); - ui_update_all(ctx); + int cached = scan_cache_dir_for_photos(ctx); + if (cached>0) { + ctx->current_idx=0; + snprintf(ctx->status_text, sizeof(ctx->status_text), "Offline: %d cached", cached); + ui_update_all(ctx); + Photo* p=&photos[0]; + char cp[320]={0}; size_t sz=0; + if (load_from_cache(ctx, p->id, cp, sizeof(cp), &sz)) { strncpy(ctx->saved_img_path, cp, sizeof(ctx->saved_img_path)-1); ctx->has_image=true; ctx->thumb_len=sz; snprintf(ctx->status_text, sizeof(ctx->status_text), "Offline cached %d %dx%d", (int)sz, ctx->img_w, ctx->img_h); ui_update_all(ctx); show_chrome(ctx); } + } else { + snprintf(ctx->status_text, sizeof(ctx->status_text), "Fetch failed %s - no cache", IMMICH_BASE_URL); + ui_update_all(ctx); + } } ctx->fetch_task=NULL; ctx->fetch_in_progress=false; vTaskDelete(NULL); } static void start_fetch_task(AppCtx* ctx) { - if (ctx->fetch_in_progress) return; + if (!ctx || ctx->fetch_in_progress || ctx->stop_requested) return; ctx->fetch_in_progress=true; - xTaskCreate(fetch_task_fn, "immich_fetch", 8192, ctx, 5, &ctx->fetch_task); + if (xTaskCreate(fetch_task_fn, "immich_fetch", 12288, ctx, 5, &ctx->fetch_task)!=pdPASS) { ctx->fetch_in_progress=false; ctx->fetch_task=NULL; } } +static void on_image_click(lv_event_t* e) { + AppCtx* ctx = (AppCtx*)lv_event_get_user_data(e); + if (!ctx) return; + bool is_hidden = false; + if (tt_lvgl_lock(pdMS_TO_TICKS(200))) { + if (ctx->header_bar) is_hidden = lv_obj_has_flag(ctx->header_bar, LV_OBJ_FLAG_HIDDEN); + tt_lvgl_unlock(); + } + if (is_hidden) show_chrome(ctx); else hide_chrome(ctx); +} static void prev_event_cb(lv_event_t* e) { AppCtx* ctx = (AppCtx*)lv_event_get_user_data(e); - if (!ctx || photo_count==0) return; - if (ctx->thumb_in_progress || ctx->fetch_in_progress) return; + if (!ctx || photo_count==0 || ctx->thumb_in_progress || ctx->fetch_in_progress || ctx->stop_requested) return; ctx->current_idx--; if (ctx->current_idx<0) ctx->current_idx=photo_count-1; - current_index=ctx->current_idx; ctx->thumb_len=0; ctx->has_image=false; - snprintf(ctx->status_text, sizeof(ctx->status_text), "Loading %d/%d 8106 PNG", ctx->current_idx+1, photo_count); - ui_update_all(ctx); start_thumb_task(ctx); + ctx->thumb_len=0; ctx->has_image=false; + snprintf(ctx->status_text, sizeof(ctx->status_text), "Loading %d/%d %dx%d", ctx->current_idx+1, photo_count, ctx->img_w, ctx->img_h); + ui_update_all(ctx); show_chrome(ctx); start_thumb_task(ctx); } static void next_event_cb(lv_event_t* e) { AppCtx* ctx = (AppCtx*)lv_event_get_user_data(e); - if (!ctx || photo_count==0) return; - if (ctx->thumb_in_progress || ctx->fetch_in_progress) return; + if (!ctx || photo_count==0 || ctx->thumb_in_progress || ctx->fetch_in_progress || ctx->stop_requested) return; ctx->current_idx++; if (ctx->current_idx>=photo_count) ctx->current_idx=0; - current_index=ctx->current_idx; ctx->thumb_len=0; ctx->has_image=false; - snprintf(ctx->status_text, sizeof(ctx->status_text), "Loading %d/%d 8106 PNG", ctx->current_idx+1, photo_count); - ui_update_all(ctx); start_thumb_task(ctx); + ctx->thumb_len=0; ctx->has_image=false; + snprintf(ctx->status_text, sizeof(ctx->status_text), "Loading %d/%d %dx%d", ctx->current_idx+1, photo_count, ctx->img_w, ctx->img_h); + ui_update_all(ctx); show_chrome(ctx); start_thumb_task(ctx); } static void reload_event_cb(lv_event_t* e) { AppCtx* ctx = (AppCtx*)lv_event_get_user_data(e); - if (!ctx) return; - if (ctx->fetch_in_progress || ctx->thumb_in_progress) return; - photo_count=0; ctx->current_idx=0; current_index=0; ctx->thumb_len=0; ctx->has_image=false; - snprintf(ctx->status_text, sizeof(ctx->status_text), "Reloading..."); - ui_update_all(ctx); start_fetch_task(ctx); + if (!ctx || ctx->fetch_in_progress || ctx->thumb_in_progress || ctx->stop_requested) return; + photo_count=0; ctx->current_idx=0; ctx->thumb_len=0; ctx->has_image=false; + snprintf(ctx->status_text, sizeof(ctx->status_text), "Reloading %dx%d...", ctx->img_w, ctx->img_h); + ui_update_all(ctx); show_chrome(ctx); start_fetch_task(ctx); } +static void back_event_cb(lv_event_t* e) { (void)e; tt_app_stop(); } static void* create_data(void) { AppCtx* ctx = calloc(1, sizeof(AppCtx)); - if (ctx) { ctx->current_idx=0; snprintf(ctx->status_text, sizeof(ctx->status_text), "Init %s proxy %s", IMMICH_BASE_URL, RESIZE_PROXY_URL); } + if (ctx) { + ctx->current_idx=0; ctx->disp_w=FALLBACK_W; ctx->disp_h=FALLBACK_H; ctx->img_w=FALLBACK_W; ctx->img_h=FALLBACK_H; + snprintf(ctx->status_text, sizeof(ctx->status_text), "Init %s proxy %s (random)", IMMICH_BASE_URL, RESIZE_PROXY_URL); + ctx->thumb_buf = heap_caps_malloc(THUMB_BUF_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + if (!ctx->thumb_buf) ctx->thumb_buf = heap_caps_malloc(THUMB_BUF_SIZE, MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT); + } return ctx; } static void destroy_data(void* data) { AppCtx* ctx = (AppCtx*)data; if (!ctx) return; - if (ctx->thumb_buf) heap_caps_free(ctx->thumb_buf); + if (ctx->fetch_task || ctx->thumb_task) { + int waits=100; + while ((ctx->fetch_task || ctx->thumb_task) && waits-- >0) vTaskDelay(pdMS_TO_TICKS(50)); + } + if (ctx->fetch_task || ctx->thumb_task) return; // leak to avoid crash + if (ctx->thumb_buf) { heap_caps_free(ctx->thumb_buf); ctx->thumb_buf=NULL; } free(ctx); } -static void on_create(AppHandle app, void* data) { AppCtx* ctx=(AppCtx*)data; if(ctx) ctx->app=app; ESP_LOGI(TAG,"onCreate proxy=%s", RESIZE_PROXY_URL); } +static void on_create(AppHandle app, void* data) { AppCtx* ctx=(AppCtx*)data; if(ctx) ctx->app=app; ESP_LOGI(TAG,"onCreate proxy=%s random %dx%d", RESIZE_PROXY_URL, ctx?ctx->img_w:0, ctx?ctx->img_h:0); } static void on_destroy(AppHandle app, void* data) { (void)app;(void)data; } static void on_show(AppHandle app, void* data, lv_obj_t* parent) { AppCtx* ctx=(AppCtx*)data; if(!ctx) return; - ctx->visible=true; ctx->app=app; + ctx->visible=true; ctx->stop_requested=false; ctx->app=app; + detect_display_size(ctx, parent); lv_obj_remove_flag(parent, LV_OBJ_FLAG_SCROLLABLE); - lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN); - lv_obj_set_flex_align(parent, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); - lv_obj_set_style_pad_all(parent, 4, LV_PART_MAIN); - lv_obj_set_style_pad_row(parent, 4, LV_PART_MAIN); + lv_obj_set_style_border_width(parent, 0, LV_PART_MAIN); + lv_obj_set_style_pad_all(parent, 0, LV_PART_MAIN); + lv_obj_set_style_pad_gap(parent, 0, LV_PART_MAIN); lv_obj_set_style_bg_color(parent, lv_color_hex(0x121212), LV_PART_MAIN); lv_obj_set_style_bg_opa(parent, LV_OPA_COVER, LV_PART_MAIN); - lv_obj_t* toolbar=tt_lvgl_toolbar_create_for_app(parent, app); + ctx->player_wrapper = lv_obj_create(parent); + lv_obj_set_size(ctx->player_wrapper, LV_PCT(100), LV_PCT(100)); + lv_obj_set_style_border_width(ctx->player_wrapper, 0, 0); + lv_obj_set_style_pad_all(ctx->player_wrapper, 0, 0); + lv_obj_set_style_pad_gap(ctx->player_wrapper, 0, 0); + lv_obj_set_style_bg_color(ctx->player_wrapper, lv_color_hex(0x121212), LV_PART_MAIN); + lv_obj_set_style_bg_opa(ctx->player_wrapper, LV_OPA_COVER, LV_PART_MAIN); + lv_obj_remove_flag(ctx->player_wrapper, LV_OBJ_FLAG_SCROLLABLE); + // Only image is clickable to avoid double-toggle on wrapper - lv_obj_t* title_cont=lv_obj_create(parent); - lv_obj_set_width(title_cont, lv_pct(100)); lv_obj_set_height(title_cont, LV_SIZE_CONTENT); - lv_obj_set_flex_flow(title_cont, LV_FLEX_FLOW_COLUMN); - lv_obj_set_flex_align(title_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); - lv_obj_set_style_bg_opa(title_cont, LV_OPA_TRANSP, LV_PART_MAIN); - lv_obj_set_style_border_width(title_cont, 0, LV_PART_MAIN); - lv_obj_set_style_pad_all(title_cont, 2, LV_PART_MAIN); + ctx->img_page = lv_image_create(ctx->player_wrapper); + lv_obj_align(ctx->img_page, LV_ALIGN_CENTER, 0, 0); + lv_obj_set_style_bg_opa(ctx->img_page, LV_OPA_TRANSP, 0); + lv_obj_set_style_pad_all(ctx->img_page, 0, 0); + lv_obj_set_style_border_width(ctx->img_page, 0, 0); + lv_obj_remove_flag(ctx->img_page, LV_OBJ_FLAG_SCROLLABLE); + lv_obj_add_flag(ctx->img_page, LV_OBJ_FLAG_CLICKABLE); + lv_obj_add_event_cb(ctx->img_page, on_image_click, LV_EVENT_CLICKED, ctx); - ctx->lbl_title=lv_label_create(title_cont); - lv_label_set_text_fmt(ctx->lbl_title, "%s - %s photos 8106 PNG", PERSON_NAME, PERSON_COUNT_STR); - lv_obj_set_style_text_font(ctx->lbl_title, lvgl_get_text_font(FONT_SIZE_LARGE), LV_PART_MAIN); - lv_obj_set_style_text_color(ctx->lbl_title, lv_color_white(), LV_PART_MAIN); - lv_obj_set_width(ctx->lbl_title, lv_pct(100)); - lv_obj_set_style_text_align(ctx->lbl_title, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN); + ctx->header_bar = lv_obj_create(ctx->player_wrapper); + lv_obj_set_size(ctx->header_bar, LV_PCT(100), 36); + lv_obj_align(ctx->header_bar, LV_ALIGN_TOP_MID, 0, 0); + lv_obj_set_style_radius(ctx->header_bar, 0, 0); + lv_obj_set_style_bg_color(ctx->header_bar, lv_color_hex(0x11111B), LV_PART_MAIN); + lv_obj_set_style_bg_opa(ctx->header_bar, LV_OPA_80, LV_PART_MAIN); + lv_obj_set_style_border_color(ctx->header_bar, lv_color_hex(0x313244), LV_PART_MAIN); + lv_obj_set_style_border_width(ctx->header_bar, 1, LV_PART_MAIN); + lv_obj_remove_flag(ctx->header_bar, LV_OBJ_FLAG_SCROLLABLE); - ctx->lbl_count=lv_label_create(title_cont); - lv_label_set_text(ctx->lbl_count, "Loading..."); - lv_obj_set_style_text_font(ctx->lbl_count, lvgl_get_text_font(FONT_SIZE_SMALL), LV_PART_MAIN); - lv_obj_set_style_text_color(ctx->lbl_count, lv_color_hex(0xAAAAAA), LV_PART_MAIN); - lv_obj_set_width(ctx->lbl_count, lv_pct(100)); - lv_label_set_long_mode(ctx->lbl_count, LV_LABEL_LONG_WRAP); - lv_obj_set_style_text_align(ctx->lbl_count, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN); + ctx->btn_back = lv_btn_create(ctx->header_bar); + lv_obj_set_size(ctx->btn_back, 44, 26); + lv_obj_align(ctx->btn_back, LV_ALIGN_LEFT_MID, 4, 0); + lv_obj_set_style_radius(ctx->btn_back, 6, 0); + lv_obj_set_style_bg_color(ctx->btn_back, lv_color_hex(0x313244), LV_PART_MAIN); + lv_obj_t* lbl_back = lv_label_create(ctx->btn_back); lv_label_set_text(lbl_back, LV_SYMBOL_LEFT); lv_obj_center(lbl_back); + lv_obj_add_event_cb(ctx->btn_back, back_event_cb, LV_EVENT_CLICKED, ctx); - lv_obj_t* main_cont=lv_obj_create(parent); - lv_obj_set_width(main_cont, lv_pct(100)); - lv_obj_set_flex_grow(main_cont, 1); - lv_obj_set_flex_flow(main_cont, LV_FLEX_FLOW_COLUMN); - lv_obj_set_flex_align(main_cont, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); - lv_obj_set_style_bg_color(main_cont, lv_color_hex(0x1E1E1E), LV_PART_MAIN); - lv_obj_set_style_bg_opa(main_cont, LV_OPA_COVER, LV_PART_MAIN); - lv_obj_set_style_radius(main_cont, 8, LV_PART_MAIN); - lv_obj_set_style_pad_all(main_cont, 6, LV_PART_MAIN); + ctx->lbl_book_title = lv_label_create(ctx->header_bar); + lv_obj_align(ctx->lbl_book_title, LV_ALIGN_CENTER, 0, 0); + lv_obj_set_width(ctx->lbl_book_title, 160); + lv_obj_set_style_text_align(ctx->lbl_book_title, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN); + lv_obj_set_style_text_color(ctx->lbl_book_title, lv_color_hex(0xCDD6F4), LV_PART_MAIN); + lv_obj_set_style_text_font(ctx->lbl_book_title, lvgl_get_text_font(FONT_SIZE_SMALL), LV_PART_MAIN); + lv_label_set_long_mode(ctx->lbl_book_title, LV_LABEL_LONG_DOT); - ctx->img=lv_image_create(main_cont); - lv_obj_add_flag(ctx->img, LV_OBJ_FLAG_HIDDEN); - lv_obj_set_size(ctx->img, 320, 240); + ctx->lbl_indicator = lv_label_create(ctx->header_bar); + lv_obj_align(ctx->lbl_indicator, LV_ALIGN_RIGHT_MID, -6, 0); + lv_obj_set_style_text_color(ctx->lbl_indicator, lv_color_hex(0xA6ADC8), LV_PART_MAIN); + lv_obj_set_style_text_font(ctx->lbl_indicator, lvgl_get_text_font(FONT_SIZE_SMALL), LV_PART_MAIN); + lv_label_set_text(ctx->lbl_indicator, "0/0"); - ctx->lbl_status=lv_label_create(main_cont); - lv_label_set_text(ctx->lbl_status, ctx->status_text); - lv_obj_set_width(ctx->lbl_status, lv_pct(100)); - lv_label_set_long_mode(ctx->lbl_status, LV_LABEL_LONG_WRAP); - lv_obj_set_style_text_font(ctx->lbl_status, lvgl_get_text_font(FONT_SIZE_DEFAULT), LV_PART_MAIN); - lv_obj_set_style_text_align(ctx->lbl_status, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN); - - ctx->lbl_info=lv_label_create(parent); - lv_obj_set_width(ctx->lbl_info, lv_pct(100)); - lv_label_set_long_mode(ctx->lbl_info, LV_LABEL_LONG_WRAP); - lv_label_set_text(ctx->lbl_info, "loading..."); + ctx->lbl_info = lv_label_create(ctx->player_wrapper); + lv_obj_align(ctx->lbl_info, LV_ALIGN_TOP_MID, 0, 50); + lv_obj_set_width(ctx->lbl_info, LV_PCT(90)); + lv_obj_set_style_text_align(ctx->lbl_info, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN); + lv_obj_set_style_text_color(ctx->lbl_info, lv_color_hex(0xAAAAAA), LV_PART_MAIN); lv_obj_set_style_text_font(ctx->lbl_info, lvgl_get_text_font(FONT_SIZE_SMALL), LV_PART_MAIN); + lv_label_set_long_mode(ctx->lbl_info, LV_LABEL_LONG_WRAP); + lv_obj_set_style_bg_color(ctx->lbl_info, lv_color_hex(0x11111B), LV_PART_MAIN); + lv_obj_set_style_bg_opa(ctx->lbl_info, LV_OPA_60, LV_PART_MAIN); + lv_obj_set_style_radius(ctx->lbl_info, 6, LV_PART_MAIN); + lv_obj_set_style_pad_all(ctx->lbl_info, 6, LV_PART_MAIN); - lv_obj_t* nav_row=lv_obj_create(parent); - lv_obj_set_width(nav_row, lv_pct(100)); lv_obj_set_height(nav_row, 48); - lv_obj_set_flex_flow(nav_row, LV_FLEX_FLOW_ROW); - lv_obj_set_flex_align(nav_row, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); - lv_obj_set_style_bg_opa(nav_row, LV_OPA_TRANSP, LV_PART_MAIN); - lv_obj_set_style_border_width(nav_row, 0, LV_PART_MAIN); - lv_obj_set_style_pad_all(nav_row, 2, LV_PART_MAIN); + ctx->lbl_status = lv_label_create(ctx->player_wrapper); + lv_obj_align(ctx->lbl_status, LV_ALIGN_BOTTOM_MID, 0, -50); + lv_obj_set_width(ctx->lbl_status, LV_PCT(80)); + lv_obj_set_style_text_align(ctx->lbl_status, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN); + lv_obj_set_style_text_color(ctx->lbl_status, lv_color_hex(0x888888), LV_PART_MAIN); + lv_obj_set_style_text_font(ctx->lbl_status, lvgl_get_text_font(FONT_SIZE_SMALL), LV_PART_MAIN); + lv_label_set_long_mode(ctx->lbl_status, LV_LABEL_LONG_WRAP); - ctx->btn_prev=lv_btn_create(nav_row); lv_obj_set_size(ctx->btn_prev, 60, 40); - lv_obj_t* lp=lv_label_create(ctx->btn_prev); lv_label_set_text(lp, "<"); lv_obj_center(lp); + ctx->ctrl_bar = lv_obj_create(ctx->player_wrapper); + lv_obj_set_size(ctx->ctrl_bar, LV_PCT(100), 44); + lv_obj_align(ctx->ctrl_bar, LV_ALIGN_BOTTOM_MID, 0, 0); + lv_obj_set_style_radius(ctx->ctrl_bar, 0, 0); + lv_obj_set_style_bg_color(ctx->ctrl_bar, lv_color_hex(0x11111B), LV_PART_MAIN); + lv_obj_set_style_bg_opa(ctx->ctrl_bar, LV_OPA_80, LV_PART_MAIN); + lv_obj_set_style_border_width(ctx->ctrl_bar, 0, 0); + lv_obj_set_style_pad_all(ctx->ctrl_bar, 4, 0); + lv_obj_set_style_pad_gap(ctx->ctrl_bar, 6, 0); + lv_obj_set_flex_flow(ctx->ctrl_bar, LV_FLEX_FLOW_ROW); + lv_obj_set_flex_align(ctx->ctrl_bar, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); + lv_obj_remove_flag(ctx->ctrl_bar, LV_OBJ_FLAG_SCROLLABLE); + + ctx->btn_prev = lv_btn_create(ctx->ctrl_bar); + lv_obj_set_size(ctx->btn_prev, 54, 32); + lv_obj_set_style_radius(ctx->btn_prev, 16, 0); + lv_obj_set_style_bg_color(ctx->btn_prev, lv_color_hex(0x313244), LV_PART_MAIN); + lv_obj_t* lp = lv_label_create(ctx->btn_prev); lv_label_set_text(lp, LV_SYMBOL_PREV); lv_obj_center(lp); lv_obj_add_event_cb(ctx->btn_prev, prev_event_cb, LV_EVENT_CLICKED, ctx); - ctx->lbl_index=lv_label_create(nav_row); - lv_label_set_text(ctx->lbl_index, "0/0"); - lv_obj_set_style_text_font(ctx->lbl_index, lvgl_get_text_font(FONT_SIZE_DEFAULT), LV_PART_MAIN); - lv_obj_set_flex_grow(ctx->lbl_index, 1); - lv_obj_set_style_text_align(ctx->lbl_index, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN); - - ctx->btn_next=lv_btn_create(nav_row); lv_obj_set_size(ctx->btn_next, 60, 40); - lv_obj_set_style_bg_color(ctx->btn_next, lv_color_hex(0x6200EE), LV_PART_MAIN); - lv_obj_t* ln=lv_label_create(ctx->btn_next); lv_label_set_text(ln, ">"); lv_obj_center(ln); - lv_obj_add_event_cb(ctx->btn_next, next_event_cb, LV_EVENT_CLICKED, ctx); - - ctx->btn_reload=lv_btn_create(nav_row); lv_obj_set_size(ctx->btn_reload, 40, 40); - lv_obj_t* lr=lv_label_create(ctx->btn_reload); lv_label_set_text(lr, LV_SYMBOL_REFRESH); lv_obj_center(lr); + ctx->btn_reload = lv_btn_create(ctx->ctrl_bar); + lv_obj_set_size(ctx->btn_reload, 44, 32); + lv_obj_set_style_radius(ctx->btn_reload, 16, 0); + lv_obj_set_style_bg_color(ctx->btn_reload, lv_color_hex(0x313244), LV_PART_MAIN); + lv_obj_t* lr = lv_label_create(ctx->btn_reload); lv_label_set_text(lr, LV_SYMBOL_REFRESH); lv_obj_center(lr); lv_obj_add_event_cb(ctx->btn_reload, reload_event_cb, LV_EVENT_CLICKED, ctx); - if (photo_count>0) { ctx->current_idx=current_index; if(ctx->current_idx>=photo_count) ctx->current_idx=0; snprintf(ctx->status_text,sizeof(ctx->status_text),"Cached %d photos via 8106",photo_count); ui_update_all(ctx); start_thumb_task(ctx); } - else { snprintf(ctx->status_text,sizeof(ctx->status_text),"Fetching %s via %s...",PERSON_NAME, RESIZE_PROXY_URL); ui_update_all(ctx); start_fetch_task(ctx); } + ctx->btn_next = lv_btn_create(ctx->ctrl_bar); + lv_obj_set_size(ctx->btn_next, 54, 32); + lv_obj_set_style_radius(ctx->btn_next, 16, 0); + lv_obj_set_style_bg_color(ctx->btn_next, lv_color_hex(0x89B4FA), LV_PART_MAIN); + lv_obj_set_style_text_color(ctx->btn_next, lv_color_hex(0x11111B), LV_PART_MAIN); + lv_obj_t* ln = lv_label_create(ctx->btn_next); lv_label_set_text(ln, LV_SYMBOL_NEXT); lv_obj_center(ln); + lv_obj_add_event_cb(ctx->btn_next, next_event_cb, LV_EVENT_CLICKED, ctx); + + // Timers: slideshow every 60s, autohide chrome after 3s + if (tt_lvgl_lock(pdMS_TO_TICKS(500))) { + if (ctx->slideshow_timer) { lv_timer_delete(ctx->slideshow_timer); ctx->slideshow_timer=NULL; } + ctx->slideshow_timer = lv_timer_create(slideshow_timer_cb, 60000, ctx); + if (ctx->autohide_timer) { lv_timer_delete(ctx->autohide_timer); ctx->autohide_timer=NULL; } + ctx->autohide_timer = lv_timer_create(autohide_timer_cb, 3000, ctx); + lv_timer_set_repeat_count(ctx->autohide_timer, 1); + lv_timer_pause(ctx->autohide_timer); + tt_lvgl_unlock(); + } + + if (photo_count>0) { + if(ctx->current_idx>=photo_count) ctx->current_idx=0; + snprintf(ctx->status_text,sizeof(ctx->status_text),"Cached %d random %dx%d",photo_count, ctx->img_w, ctx->img_h); + ui_update_all(ctx); show_chrome(ctx); start_thumb_task(ctx); + } else { + snprintf(ctx->status_text,sizeof(ctx->status_text),"Fetching %s random %dx%d via %s disp %dx%d",PERSON_NAME, ctx->img_w, ctx->img_h, RESIZE_PROXY_URL, ctx->disp_w, ctx->disp_h); + ui_update_all(ctx); show_chrome(ctx); start_fetch_task(ctx); + } } static void on_hide(AppHandle app, void* data) { (void)app; AppCtx* ctx=(AppCtx*)data; if(!ctx) return; - ctx->visible=false; - int timeout=20; while((ctx->fetch_task||ctx->thumb_task) && timeout>0){ vTaskDelay(pdMS_TO_TICKS(50)); timeout--; } + ESP_LOGI(TAG, "onHide start, stopping tasks"); + ctx->stop_requested=true; ctx->visible=false; + if (tt_lvgl_lock(pdMS_TO_TICKS(500))) { + if (ctx->slideshow_timer) { lv_timer_delete(ctx->slideshow_timer); ctx->slideshow_timer=NULL; } + if (ctx->autohide_timer) { lv_timer_delete(ctx->autohide_timer); ctx->autohide_timer=NULL; } + tt_lvgl_unlock(); + } + int timeout=200; + while((ctx->fetch_task || ctx->thumb_task) && timeout>0){ vTaskDelay(pdMS_TO_TICKS(50)); timeout--; } + if (ctx->fetch_task || ctx->thumb_task) ESP_LOGW(TAG, "onHide timeout fetch=%p thumb=%p", ctx->fetch_task, ctx->thumb_task); + else ESP_LOGI(TAG, "onHide tasks stopped cleanly"); } int main(int argc, char* argv[]) {