From 1fc28278b55a5835bfd57821619141bd92eed91d Mon Sep 17 00:00:00 2001 From: NRK Date: Sun, 1 Oct 2023 17:06:04 +0600 Subject: [PATCH] add option to update cache in a background process the cli flag is undocumented for now, since it's experimental. Closes: https://codeberg.org/nsxiv/nsxiv/issues/416 Closes: https://codeberg.org/nsxiv/nsxiv/pulls/425 Co-authored-by: explosion-mental --- main.c | 17 +++++++++++++++++ nsxiv.h | 1 + options.c | 11 ++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index a050aa8..e0ff5b3 100644 --- a/main.c +++ b/main.c @@ -936,6 +936,23 @@ int main(int argc, char *argv[]) filecnt = fileidx; fileidx = options->startnum < filecnt ? options->startnum : 0; + if (options->background_cache && !options->private_mode) { + pid_t ppid = getpid(); /* to check if parent is still alive or not */ + switch (fork()) { + case 0: + tns_init(&tns, files, &filecnt, &fileidx, NULL); + while (filecnt > 0 && getppid() == ppid) { + tns_load(&tns, filecnt - 1, false, true); + remove_file(filecnt - 1, true); + } + exit(0); + break; + case -1: + error(0, errno, "fork failed"); + break; + } + } + win_init(&win); img_init(&img, &win); arl_init(&arl); diff --git a/nsxiv.h b/nsxiv.h index e137bfe..3ffc113 100644 --- a/nsxiv.h +++ b/nsxiv.h @@ -274,6 +274,7 @@ struct opt { bool thumb_mode; bool clean_cache; bool private_mode; + bool background_cache; }; extern const opt_t *options; diff --git a/options.c b/options.c index 89dfe20..2835967 100644 --- a/options.c +++ b/options.c @@ -81,7 +81,8 @@ void parse_options(int argc, char **argv) */ OPT_START = UCHAR_MAX, OPT_AA, - OPT_AL + OPT_AL, + OPT_BG }; static const struct optparse_long longopts[] = { { "framerate", 'A', OPTPARSE_REQUIRED }, @@ -109,6 +110,8 @@ void parse_options(int argc, char **argv) { "null", '0', OPTPARSE_NONE }, { "anti-alias", OPT_AA, OPTPARSE_OPTIONAL }, { "alpha-layer", OPT_AL, OPTPARSE_OPTIONAL }, + /* TODO: document this when it's stable */ + { "bg-cache", OPT_BG, OPTPARSE_OPTIONAL }, { 0 }, /* end */ }; @@ -145,6 +148,7 @@ void parse_options(int argc, char **argv) _options.thumb_mode = false; _options.clean_cache = false; _options.private_mode = false; + _options.background_cache = false; if (argc > 0) { s = strrchr(argv[0], '/'); @@ -264,6 +268,11 @@ void parse_options(int argc, char **argv) error(EXIT_FAILURE, 0, "Invalid argument for option --alpha-layer: %s", op.optarg); _options.alpha_layer = op.optarg == NULL; break; + case OPT_BG: + if (op.optarg != NULL && !STREQ(op.optarg, "no")) + error(EXIT_FAILURE, 0, "Invalid argument for option --bg-cache: %s", op.optarg); + _options.background_cache = op.optarg == NULL; + break; } }