From de898bfb6d8d8adacdb7dfe7d122b437acfa8f5b Mon Sep 17 00:00:00 2001 From: Artem Senichev Date: Fri, 22 Dec 2023 12:13:47 +0300 Subject: [PATCH] Automatically reload image on exec Resets cache and reloads the current image after executing external command. Signed-off-by: Artem Senichev --- src/imagelist.c | 30 +++++++++++++++++++++++------- src/imagelist.h | 8 ++++---- src/viewer.c | 13 ++++++++++--- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/imagelist.c b/src/imagelist.c index 4cd766d..a2047ad 100644 --- a/src/imagelist.c +++ b/src/imagelist.c @@ -461,7 +461,7 @@ struct image_entry image_list_current(const struct image_list* ctx) return entry; } -int image_list_cur_exec(const struct image_list* ctx) +int image_list_exec(const struct image_list* ctx) { const char* template = ctx->config->exec_cmd; const char* path = ctx->current->file_path; @@ -511,14 +511,30 @@ int image_list_cur_exec(const struct image_list* ctx) return rc; } -bool image_list_cur_reload(struct image_list* ctx) +bool image_list_reset(struct image_list* ctx) { - struct image* image = image_from_file(ctx->current->file_path); - if (image) { - image_free(ctx->current); - ctx->current = image; + // reset cache + preloader_ctl(ctx, false); + if (ctx->prev) { + image_free(ctx->prev); + ctx->prev = NULL; + } + if (ctx->next) { + image_free(ctx->next); + ctx->next = NULL; + } + + // reload current image + image_free(ctx->current); + ctx->current = image_from_file(ctx->entries[ctx->index]->path); + if (ctx->current) { + preloader_ctl(ctx, true); + return true; } - return !!image; + + // open nearest image + return image_list_jump(ctx, jump_next_file) || + image_list_jump(ctx, jump_prev_file); } bool image_list_jump(struct image_list* ctx, enum list_jump jump) diff --git a/src/imagelist.h b/src/imagelist.h index 84a0352..747e802 100644 --- a/src/imagelist.h +++ b/src/imagelist.h @@ -63,14 +63,14 @@ struct image_entry image_list_current(const struct image_list* ctx); * @param ctx image list context * @return error code from the system call */ -int image_list_cur_exec(const struct image_list* ctx); +int image_list_exec(const struct image_list* ctx); /** - * Reload the current image. + * Reset cache and reload current image. * @param ctx image list context - * @return false if reload failed + * @return false if reset failed (no more images) */ -bool image_list_cur_reload(struct image_list* ctx); +bool image_list_reset(struct image_list* ctx); /** * Move through image list. diff --git a/src/viewer.c b/src/viewer.c index 6e27221..d553cc3 100644 --- a/src/viewer.c +++ b/src/viewer.c @@ -444,24 +444,31 @@ bool viewer_on_keyboard(void* data, struct ui* ui, xkb_keysym_t key) ctx->config->antialiasing ? "on" : "off"); return true; case cfgact_reload: - if (image_list_cur_reload(ctx->list)) { + if (image_list_reset(ctx->list)) { reset_state(ctx, ui); set_message(ctx, "Image reloaded"); return true; } else { - set_message(ctx, "Reload failed"); + printf("No more images, exit\n"); + ui_stop(ui); return false; } case cfgact_info: ctx->config->show_info = !ctx->config->show_info; return true; case cfgact_exec: { - const int rc = image_list_cur_exec(ctx->list); + const int rc = image_list_exec(ctx->list); if (rc) { set_message(ctx, "Execute failed: code %d", rc); } else { set_message(ctx, "Execute success"); } + if (!image_list_reset(ctx->list)) { + printf("No more images, exit\n"); + ui_stop(ui); + return false; + } + reset_state(ctx, ui); return true; } case cfgact_quit: