2013-02-08 21:52:41 +01:00
|
|
|
/* Copyright 2011 Bert Muennich
|
2011-02-16 21:40:20 +01:00
|
|
|
*
|
2021-09-16 11:32:59 +02:00
|
|
|
* This file is a part of nsxiv.
|
2011-08-18 01:18:26 +02:00
|
|
|
*
|
2021-09-16 11:32:59 +02:00
|
|
|
* nsxiv is free software; you can redistribute it and/or modify
|
2013-02-08 21:52:41 +01:00
|
|
|
* it under the terms of the GNU General Public License as published
|
|
|
|
* by the Free Software Foundation; either version 2 of the License,
|
|
|
|
* or (at your option) any later version.
|
2011-08-18 01:18:26 +02:00
|
|
|
*
|
2021-09-16 11:32:59 +02:00
|
|
|
* nsxiv is distributed in the hope that it will be useful,
|
2013-02-08 21:52:41 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-09-16 11:32:59 +02:00
|
|
|
* along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
|
2011-02-16 21:40:20 +01:00
|
|
|
*/
|
|
|
|
|
2021-09-16 11:32:59 +02:00
|
|
|
#include "nsxiv.h"
|
2017-10-16 21:10:35 +02:00
|
|
|
#define _THUMBS_CONFIG
|
|
|
|
#include "config.h"
|
|
|
|
|
2015-10-28 23:03:37 +01:00
|
|
|
#include <errno.h>
|
2011-02-16 21:40:20 +01:00
|
|
|
#include <stdlib.h>
|
2011-02-16 23:03:42 +01:00
|
|
|
#include <string.h>
|
2011-04-07 01:26:08 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2011-09-08 20:54:24 +02:00
|
|
|
#include <utime.h>
|
2011-02-16 21:40:20 +01:00
|
|
|
|
2014-06-09 22:59:49 +02:00
|
|
|
#if HAVE_LIBEXIF
|
|
|
|
#include <libexif/exif-data.h>
|
|
|
|
void exif_auto_orientate(const fileinfo_t*);
|
|
|
|
#endif
|
2017-11-23 14:35:32 +01:00
|
|
|
Imlib_Image img_open(const fileinfo_t*);
|
2014-06-09 22:59:49 +02:00
|
|
|
|
2014-06-10 23:15:04 +02:00
|
|
|
static char *cache_dir;
|
2021-09-12 20:26:07 +02:00
|
|
|
extern const int fileidx;
|
2013-01-08 20:55:02 +01:00
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
char* tns_cache_filepath(const char *filepath)
|
|
|
|
{
|
2011-04-08 10:24:10 +02:00
|
|
|
size_t len;
|
|
|
|
char *cfile = NULL;
|
|
|
|
|
2015-10-28 20:59:48 +01:00
|
|
|
if (*filepath != '/')
|
2011-04-08 10:24:10 +02:00
|
|
|
return NULL;
|
2021-10-12 01:46:35 +02:00
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
|
|
|
|
/* don't cache images inside the cache directory! */
|
2015-07-22 21:08:00 +02:00
|
|
|
len = strlen(cache_dir) + strlen(filepath) + 2;
|
2015-10-28 22:29:01 +01:00
|
|
|
cfile = (char*) emalloc(len);
|
2015-07-22 21:08:00 +02:00
|
|
|
snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1);
|
2011-04-08 10:24:10 +02:00
|
|
|
}
|
|
|
|
return cfile;
|
|
|
|
}
|
|
|
|
|
2014-09-25 20:57:24 +02:00
|
|
|
Imlib_Image tns_cache_load(const char *filepath, bool *outdated)
|
2013-02-08 22:05:31 +01:00
|
|
|
{
|
2011-04-08 10:24:10 +02:00
|
|
|
char *cfile;
|
|
|
|
struct stat cstats, fstats;
|
2013-12-13 13:04:45 +01:00
|
|
|
Imlib_Image im = NULL;
|
2011-04-08 10:24:10 +02:00
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (stat(filepath, &fstats) < 0)
|
2011-04-08 10:24:10 +02:00
|
|
|
return NULL;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if ((cfile = tns_cache_filepath(filepath)) != NULL) {
|
2014-09-25 20:57:24 +02:00
|
|
|
if (stat(cfile, &cstats) == 0) {
|
|
|
|
if (cstats.st_mtime == fstats.st_mtime)
|
|
|
|
im = imlib_load_image(cfile);
|
|
|
|
else
|
|
|
|
*outdated = true;
|
|
|
|
}
|
2011-04-08 10:24:10 +02:00
|
|
|
free(cfile);
|
|
|
|
}
|
|
|
|
return im;
|
|
|
|
}
|
|
|
|
|
2014-09-28 00:26:02 +02:00
|
|
|
void tns_cache_write(Imlib_Image im, const char *filepath, bool force)
|
2013-02-08 22:05:31 +01:00
|
|
|
{
|
2011-04-08 10:24:10 +02:00
|
|
|
char *cfile, *dirend;
|
|
|
|
struct stat cstats, fstats;
|
2011-09-08 20:54:24 +02:00
|
|
|
struct utimbuf times;
|
2019-03-15 13:04:06 +01:00
|
|
|
Imlib_Load_Error err;
|
2011-04-08 10:24:10 +02:00
|
|
|
|
2017-07-13 02:15:51 +02:00
|
|
|
if (options->private_mode)
|
|
|
|
return;
|
|
|
|
|
2014-09-28 00:26:02 +02:00
|
|
|
if (stat(filepath, &fstats) < 0)
|
2011-04-08 10:24:10 +02:00
|
|
|
return;
|
|
|
|
|
2014-09-28 00:26:02 +02:00
|
|
|
if ((cfile = tns_cache_filepath(filepath)) != NULL) {
|
2011-09-29 12:43:36 +02:00
|
|
|
if (force || stat(cfile, &cstats) < 0 ||
|
|
|
|
cstats.st_mtime != fstats.st_mtime)
|
|
|
|
{
|
|
|
|
if ((dirend = strrchr(cfile, '/')) != NULL) {
|
2011-04-08 10:24:10 +02:00
|
|
|
*dirend = '\0';
|
2019-03-15 13:04:06 +01:00
|
|
|
if (r_mkdir(cfile) == -1) {
|
2015-10-28 23:21:12 +01:00
|
|
|
error(0, errno, "%s", cfile);
|
2019-03-15 13:04:06 +01:00
|
|
|
goto end;
|
2015-07-22 21:08:00 +02:00
|
|
|
}
|
2019-03-15 13:04:06 +01:00
|
|
|
*dirend = '/';
|
2011-04-08 10:24:10 +02:00
|
|
|
}
|
2019-03-15 13:04:06 +01:00
|
|
|
imlib_context_set_image(im);
|
|
|
|
if (imlib_image_has_alpha()) {
|
|
|
|
imlib_image_set_format("png");
|
|
|
|
} else {
|
|
|
|
imlib_image_set_format("jpg");
|
|
|
|
imlib_image_attach_data_value("quality", NULL, 90, NULL);
|
2011-04-08 10:24:10 +02:00
|
|
|
}
|
2019-03-15 13:04:06 +01:00
|
|
|
imlib_save_image_with_error_return(cfile, &err);
|
|
|
|
if (err)
|
|
|
|
goto end;
|
|
|
|
times.actime = fstats.st_atime;
|
|
|
|
times.modtime = fstats.st_mtime;
|
|
|
|
utime(cfile, ×);
|
2011-04-08 10:24:10 +02:00
|
|
|
}
|
2019-03-15 13:04:06 +01:00
|
|
|
end:
|
2011-04-08 10:24:10 +02:00
|
|
|
free(cfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void tns_clean_cache(tns_t *tns)
|
|
|
|
{
|
2011-09-11 21:01:24 +02:00
|
|
|
int dirlen;
|
2018-07-05 02:57:32 +02:00
|
|
|
char *cfile, *filename;
|
2011-04-08 14:44:00 +02:00
|
|
|
r_dir_t dir;
|
|
|
|
|
2016-09-28 18:59:15 +02:00
|
|
|
if (r_opendir(&dir, cache_dir, true) < 0) {
|
2015-10-28 23:03:37 +01:00
|
|
|
error(0, errno, "%s", cache_dir);
|
2011-04-08 14:44:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirlen = strlen(cache_dir);
|
|
|
|
|
2018-07-05 03:39:27 +02:00
|
|
|
while ((cfile = r_readdir(&dir, false)) != NULL) {
|
2011-04-08 14:44:00 +02:00
|
|
|
filename = cfile + dirlen;
|
2018-07-05 02:57:32 +02:00
|
|
|
if (access(filename, F_OK) < 0) {
|
2011-09-29 12:43:36 +02:00
|
|
|
if (unlink(cfile) < 0)
|
2015-10-28 23:03:37 +01:00
|
|
|
error(0, errno, "%s", cfile);
|
2011-09-29 12:43:36 +02:00
|
|
|
}
|
2011-04-08 14:44:00 +02:00
|
|
|
free(cfile);
|
|
|
|
}
|
|
|
|
r_closedir(&dir);
|
|
|
|
}
|
|
|
|
|
2021-10-11 05:07:18 +02:00
|
|
|
void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *win)
|
2013-02-08 22:05:31 +01:00
|
|
|
{
|
2011-04-07 17:23:47 +02:00
|
|
|
int len;
|
2014-01-11 22:47:41 +01:00
|
|
|
const char *homedir, *dsuffix = "";
|
2011-04-07 17:23:47 +02:00
|
|
|
|
2014-10-29 14:20:54 +01:00
|
|
|
if (cnt != NULL && *cnt > 0) {
|
2015-10-28 22:29:01 +01:00
|
|
|
tns->thumbs = (thumb_t*) emalloc(*cnt * sizeof(thumb_t));
|
2014-09-30 21:54:17 +02:00
|
|
|
memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
|
2011-04-08 14:44:00 +02:00
|
|
|
} else {
|
|
|
|
tns->thumbs = NULL;
|
|
|
|
}
|
2014-08-16 22:21:51 +02:00
|
|
|
tns->files = files;
|
2014-09-25 20:57:24 +02:00
|
|
|
tns->cnt = cnt;
|
2015-01-04 21:19:26 +01:00
|
|
|
tns->initnext = tns->loadnext = 0;
|
|
|
|
tns->first = tns->end = tns->r_first = tns->r_end = 0;
|
2014-08-16 18:36:17 +02:00
|
|
|
tns->sel = sel;
|
2011-09-17 17:23:51 +02:00
|
|
|
tns->win = win;
|
2011-09-11 21:01:24 +02:00
|
|
|
tns->dirty = false;
|
2011-04-07 17:23:47 +02:00
|
|
|
|
2015-12-20 13:10:24 +01:00
|
|
|
tns->zl = THUMB_SIZE;
|
|
|
|
tns_zoom(tns, 0);
|
2014-09-29 08:47:01 +02:00
|
|
|
|
2014-01-11 22:47:41 +01:00
|
|
|
if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
|
|
|
|
homedir = getenv("HOME");
|
|
|
|
dsuffix = "/.cache";
|
|
|
|
}
|
|
|
|
if (homedir != NULL) {
|
2021-09-16 11:32:59 +02:00
|
|
|
const char *s = "/nsxiv";
|
2014-08-21 20:51:02 +02:00
|
|
|
free(cache_dir);
|
2021-09-16 11:32:59 +02:00
|
|
|
len = strlen(homedir) + strlen(dsuffix) + strlen(s) + 1;
|
2015-10-28 22:29:01 +01:00
|
|
|
cache_dir = (char*) emalloc(len);
|
2021-09-16 11:32:59 +02:00
|
|
|
snprintf(cache_dir, len, "%s%s%s", homedir, dsuffix, s);
|
2011-04-08 14:44:00 +02:00
|
|
|
} else {
|
2015-10-28 23:03:37 +01:00
|
|
|
error(0, 0, "Cache directory not found");
|
2011-04-07 17:23:47 +02:00
|
|
|
}
|
2011-02-16 21:40:20 +01:00
|
|
|
}
|
|
|
|
|
2015-10-28 23:03:37 +01:00
|
|
|
CLEANUP void tns_free(tns_t *tns)
|
2013-02-08 22:05:31 +01:00
|
|
|
{
|
2011-02-16 21:40:20 +01:00
|
|
|
int i;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (tns->thumbs != NULL) {
|
2014-09-30 21:54:17 +02:00
|
|
|
for (i = 0; i < *tns->cnt; i++) {
|
2011-09-29 12:43:36 +02:00
|
|
|
if (tns->thumbs[i].im != NULL) {
|
2011-04-08 14:44:00 +02:00
|
|
|
imlib_context_set_image(tns->thumbs[i].im);
|
|
|
|
imlib_free_image();
|
|
|
|
}
|
2011-03-15 13:55:52 +01:00
|
|
|
}
|
2011-04-08 14:44:00 +02:00
|
|
|
free(tns->thumbs);
|
|
|
|
tns->thumbs = NULL;
|
2011-03-15 13:55:52 +01:00
|
|
|
}
|
2011-02-16 21:40:20 +01:00
|
|
|
|
2014-08-21 20:51:02 +02:00
|
|
|
free(cache_dir);
|
|
|
|
cache_dir = NULL;
|
2011-02-16 21:40:20 +01:00
|
|
|
}
|
|
|
|
|
2014-09-28 00:26:02 +02:00
|
|
|
Imlib_Image tns_scale_down(Imlib_Image im, int dim)
|
|
|
|
{
|
|
|
|
int w, h;
|
|
|
|
float z, zw, zh;
|
|
|
|
|
|
|
|
imlib_context_set_image(im);
|
|
|
|
w = imlib_image_get_width();
|
|
|
|
h = imlib_image_get_height();
|
|
|
|
zw = (float) dim / (float) w;
|
|
|
|
zh = (float) dim / (float) h;
|
|
|
|
z = MIN(zw, zh);
|
|
|
|
z = MIN(z, 1.0);
|
|
|
|
|
|
|
|
if (z < 1.0) {
|
|
|
|
imlib_context_set_anti_alias(1);
|
2014-09-29 14:05:04 +02:00
|
|
|
im = imlib_create_cropped_scaled_image(0, 0, w, h,
|
|
|
|
MAX(z * w, 1), MAX(z * h, 1));
|
2014-09-28 00:26:02 +02:00
|
|
|
if (im == NULL)
|
2015-10-28 23:03:37 +01:00
|
|
|
error(EXIT_FAILURE, ENOMEM, NULL);
|
2014-09-28 00:26:02 +02:00
|
|
|
imlib_free_image_and_decache();
|
|
|
|
}
|
|
|
|
return im;
|
|
|
|
}
|
|
|
|
|
2015-01-04 21:19:26 +01:00
|
|
|
bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
|
2011-08-19 13:26:58 +02:00
|
|
|
{
|
2014-10-30 09:30:00 +01:00
|
|
|
int maxwh = thumb_sizes[ARRLEN(thumb_sizes)-1];
|
2014-06-10 23:15:04 +02:00
|
|
|
bool cache_hit = false;
|
2014-10-31 09:52:26 +01:00
|
|
|
char *cfile;
|
2011-02-17 11:04:58 +01:00
|
|
|
thumb_t *t;
|
2015-01-04 21:19:26 +01:00
|
|
|
fileinfo_t *file;
|
2014-06-09 22:59:49 +02:00
|
|
|
Imlib_Image im = NULL;
|
2011-02-17 11:04:58 +01:00
|
|
|
|
2014-09-30 21:54:17 +02:00
|
|
|
if (n < 0 || n >= *tns->cnt)
|
2011-09-11 21:01:24 +02:00
|
|
|
return false;
|
2014-08-16 22:21:51 +02:00
|
|
|
file = &tns->files[n];
|
|
|
|
if (file->name == NULL || file->path == NULL)
|
|
|
|
return false;
|
2011-02-27 13:29:24 +01:00
|
|
|
|
2011-03-15 13:55:52 +01:00
|
|
|
t = &tns->thumbs[n];
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (t->im != NULL) {
|
2011-03-15 13:55:52 +01:00
|
|
|
imlib_context_set_image(t->im);
|
|
|
|
imlib_free_image();
|
2015-01-04 21:19:26 +01:00
|
|
|
t->im = NULL;
|
2011-03-15 13:55:52 +01:00
|
|
|
}
|
|
|
|
|
2014-10-30 09:30:00 +01:00
|
|
|
if (!force) {
|
|
|
|
if ((im = tns_cache_load(file->path, &force)) != NULL) {
|
2014-10-31 09:52:26 +01:00
|
|
|
imlib_context_set_image(im);
|
|
|
|
if (imlib_image_get_width() < maxwh &&
|
|
|
|
imlib_image_get_height() < maxwh)
|
|
|
|
{
|
|
|
|
if ((cfile = tns_cache_filepath(file->path)) != NULL) {
|
|
|
|
unlink(cfile);
|
|
|
|
free(cfile);
|
|
|
|
}
|
|
|
|
imlib_free_image_and_decache();
|
|
|
|
im = NULL;
|
|
|
|
} else {
|
|
|
|
cache_hit = true;
|
|
|
|
}
|
2014-06-09 22:59:49 +02:00
|
|
|
#if HAVE_LIBEXIF
|
2017-07-13 02:15:51 +02:00
|
|
|
} else if (!force && !options->private_mode) {
|
2016-12-28 19:16:19 +01:00
|
|
|
int pw = 0, ph = 0, w, h, x = 0, y = 0;
|
2014-06-10 23:15:04 +02:00
|
|
|
bool err;
|
2016-12-28 19:16:19 +01:00
|
|
|
float zw, zh;
|
2014-06-10 23:15:04 +02:00
|
|
|
ExifData *ed;
|
|
|
|
ExifEntry *entry;
|
|
|
|
ExifContent *ifd;
|
|
|
|
ExifByteOrder byte_order;
|
|
|
|
int tmpfd;
|
2021-09-16 11:32:59 +02:00
|
|
|
char tmppath[] = "/tmp/nsxiv-XXXXXX";
|
2014-06-10 23:15:04 +02:00
|
|
|
Imlib_Image tmpim;
|
|
|
|
|
2014-10-31 10:37:35 +01:00
|
|
|
if ((ed = exif_data_new_from_file(file->path)) != NULL) {
|
|
|
|
if (ed->data != NULL && ed->size > 0 &&
|
|
|
|
(tmpfd = mkstemp(tmppath)) >= 0)
|
|
|
|
{
|
2014-06-10 23:15:04 +02:00
|
|
|
err = write(tmpfd, ed->data, ed->size) != ed->size;
|
|
|
|
close(tmpfd);
|
|
|
|
|
|
|
|
if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
|
|
|
|
byte_order = exif_data_get_byte_order(ed);
|
|
|
|
ifd = ed->ifd[EXIF_IFD_EXIF];
|
|
|
|
entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
|
|
|
|
if (entry != NULL)
|
|
|
|
pw = exif_get_long(entry->data, byte_order);
|
|
|
|
entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
|
|
|
|
if (entry != NULL)
|
|
|
|
ph = exif_get_long(entry->data, byte_order);
|
|
|
|
|
|
|
|
imlib_context_set_image(tmpim);
|
|
|
|
w = imlib_image_get_width();
|
|
|
|
h = imlib_image_get_height();
|
|
|
|
|
|
|
|
if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
|
|
|
|
zw = (float) pw / (float) w;
|
|
|
|
zh = (float) ph / (float) h;
|
|
|
|
if (zw < zh) {
|
|
|
|
pw /= zh;
|
|
|
|
x = (w - pw) / 2;
|
|
|
|
w = pw;
|
|
|
|
} else if (zw > zh) {
|
|
|
|
ph /= zw;
|
|
|
|
y = (h - ph) / 2;
|
|
|
|
h = ph;
|
|
|
|
}
|
2014-06-09 22:59:49 +02:00
|
|
|
}
|
2014-10-31 09:52:26 +01:00
|
|
|
if (w >= maxwh || h >= maxwh) {
|
|
|
|
if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
|
2021-09-29 05:44:46 +02:00
|
|
|
error(0, 0, "%s: error generating thumbnail", file->name);
|
2014-10-31 09:52:26 +01:00
|
|
|
}
|
2014-06-10 23:15:04 +02:00
|
|
|
imlib_free_image_and_decache();
|
2014-06-09 22:59:49 +02:00
|
|
|
}
|
2014-06-10 23:15:04 +02:00
|
|
|
unlink(tmppath);
|
2014-06-09 22:59:49 +02:00
|
|
|
}
|
2014-06-10 23:15:04 +02:00
|
|
|
exif_data_unref(ed);
|
2014-06-09 22:59:49 +02:00
|
|
|
}
|
2021-10-11 05:07:18 +02:00
|
|
|
#endif /* HAVE_LIBEXIF */
|
2011-09-29 12:43:36 +02:00
|
|
|
}
|
2011-04-11 16:58:38 +02:00
|
|
|
}
|
2014-10-31 09:52:26 +01:00
|
|
|
|
2016-10-20 10:21:55 +02:00
|
|
|
if (im == NULL) {
|
2017-11-23 14:35:32 +01:00
|
|
|
if ((im = img_open(file)) == NULL)
|
2016-10-20 10:21:55 +02:00
|
|
|
return false;
|
2014-10-30 09:30:00 +01:00
|
|
|
}
|
2015-01-04 21:19:26 +01:00
|
|
|
imlib_context_set_image(im);
|
2011-02-17 11:04:58 +01:00
|
|
|
|
2014-09-28 00:26:02 +02:00
|
|
|
if (!cache_hit) {
|
2014-06-09 22:59:49 +02:00
|
|
|
#if HAVE_LIBEXIF
|
2012-02-15 19:13:44 +01:00
|
|
|
exif_auto_orientate(file);
|
2014-06-09 22:59:49 +02:00
|
|
|
#endif
|
2014-10-30 09:30:00 +01:00
|
|
|
im = tns_scale_down(im, maxwh);
|
2014-10-31 09:52:26 +01:00
|
|
|
imlib_context_set_image(im);
|
|
|
|
if (imlib_image_get_width() == maxwh || imlib_image_get_height() == maxwh)
|
|
|
|
tns_cache_write(im, file->path, true);
|
2014-09-28 00:26:02 +02:00
|
|
|
}
|
2011-09-06 11:09:57 +02:00
|
|
|
|
2015-01-04 21:19:26 +01:00
|
|
|
if (cache_only) {
|
|
|
|
imlib_free_image_and_decache();
|
|
|
|
} else {
|
|
|
|
t->im = tns_scale_down(im, thumb_sizes[tns->zl]);
|
|
|
|
imlib_context_set_image(t->im);
|
|
|
|
t->w = imlib_image_get_width();
|
|
|
|
t->h = imlib_image_get_height();
|
|
|
|
tns->dirty = true;
|
|
|
|
}
|
|
|
|
file->flags |= FF_TN_INIT;
|
|
|
|
|
|
|
|
if (n == tns->initnext)
|
|
|
|
while (++tns->initnext < *tns->cnt && ((++file)->flags & FF_TN_INIT));
|
|
|
|
if (n == tns->loadnext && !cache_only)
|
|
|
|
while (++tns->loadnext < tns->end && (++t)->im != NULL);
|
2011-03-15 13:55:52 +01:00
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
return true;
|
2011-02-17 11:04:58 +01:00
|
|
|
}
|
|
|
|
|
2014-09-25 20:57:24 +02:00
|
|
|
void tns_unload(tns_t *tns, int n)
|
|
|
|
{
|
|
|
|
thumb_t *t;
|
|
|
|
|
2014-09-30 21:54:17 +02:00
|
|
|
if (n < 0 || n >= *tns->cnt)
|
2014-09-25 20:57:24 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
t = &tns->thumbs[n];
|
|
|
|
|
|
|
|
if (t->im != NULL) {
|
|
|
|
imlib_context_set_image(t->im);
|
|
|
|
imlib_free_image();
|
|
|
|
t->im = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void tns_check_view(tns_t *tns, bool scrolled)
|
|
|
|
{
|
2011-02-20 00:08:17 +01:00
|
|
|
int r;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (tns == NULL)
|
2011-02-19 22:12:08 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
tns->first -= tns->first % tns->cols;
|
2014-08-16 18:36:17 +02:00
|
|
|
r = *tns->sel % tns->cols;
|
2011-02-19 22:12:08 +01:00
|
|
|
|
|
|
|
if (scrolled) {
|
|
|
|
/* move selection into visible area */
|
2014-08-16 18:36:17 +02:00
|
|
|
if (*tns->sel >= tns->first + tns->cols * tns->rows)
|
|
|
|
*tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
|
|
|
|
else if (*tns->sel < tns->first)
|
|
|
|
*tns->sel = tns->first + r;
|
2011-02-19 22:12:08 +01:00
|
|
|
} else {
|
|
|
|
/* scroll to selection */
|
2014-08-16 18:36:17 +02:00
|
|
|
if (tns->first + tns->cols * tns->rows <= *tns->sel) {
|
|
|
|
tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
|
2011-09-11 21:01:24 +02:00
|
|
|
tns->dirty = true;
|
2014-08-16 18:36:17 +02:00
|
|
|
} else if (tns->first > *tns->sel) {
|
|
|
|
tns->first = *tns->sel - r;
|
2011-09-11 21:01:24 +02:00
|
|
|
tns->dirty = true;
|
2011-02-19 22:12:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void tns_render(tns_t *tns)
|
|
|
|
{
|
2011-02-19 22:12:08 +01:00
|
|
|
thumb_t *t;
|
2011-09-17 17:23:51 +02:00
|
|
|
win_t *win;
|
|
|
|
int i, cnt, r, x, y;
|
2011-02-16 21:40:20 +01:00
|
|
|
|
2011-04-08 14:44:00 +02:00
|
|
|
if (!tns->dirty)
|
2011-02-16 21:40:20 +01:00
|
|
|
return;
|
|
|
|
|
2011-09-17 17:23:51 +02:00
|
|
|
win = tns->win;
|
2011-02-19 22:12:08 +01:00
|
|
|
win_clear(win);
|
2014-07-28 20:36:32 +02:00
|
|
|
imlib_context_set_drawable(win->buf.pm);
|
2011-02-19 22:12:08 +01:00
|
|
|
|
2014-09-29 08:47:01 +02:00
|
|
|
tns->cols = MAX(1, win->w / tns->dim);
|
|
|
|
tns->rows = MAX(1, win->h / tns->dim);
|
2011-02-16 21:40:20 +01:00
|
|
|
|
2014-09-30 21:54:17 +02:00
|
|
|
if (*tns->cnt < tns->cols * tns->rows) {
|
2011-02-19 22:12:08 +01:00
|
|
|
tns->first = 0;
|
2014-09-30 21:54:17 +02:00
|
|
|
cnt = *tns->cnt;
|
2011-02-19 22:12:08 +01:00
|
|
|
} else {
|
2011-09-11 21:01:24 +02:00
|
|
|
tns_check_view(tns, false);
|
2011-02-19 22:12:08 +01:00
|
|
|
cnt = tns->cols * tns->rows;
|
2014-09-30 21:54:17 +02:00
|
|
|
if ((r = tns->first + cnt - *tns->cnt) >= tns->cols)
|
2011-02-19 22:12:08 +01:00
|
|
|
tns->first -= r - r % tns->cols;
|
|
|
|
if (r > 0)
|
|
|
|
cnt -= r % tns->cols;
|
|
|
|
}
|
|
|
|
r = cnt % tns->cols ? 1 : 0;
|
2014-09-29 09:36:38 +02:00
|
|
|
tns->x = x = (win->w - MIN(cnt, tns->cols) * tns->dim) / 2 + tns->bw + 3;
|
|
|
|
tns->y = y = (win->h - (cnt / tns->cols + r) * tns->dim) / 2 + tns->bw + 3;
|
2014-09-30 21:54:17 +02:00
|
|
|
tns->loadnext = *tns->cnt;
|
2014-09-25 20:57:24 +02:00
|
|
|
tns->end = tns->first + cnt;
|
2011-02-19 22:12:08 +01:00
|
|
|
|
2014-09-25 20:57:24 +02:00
|
|
|
for (i = tns->r_first; i < tns->r_end; i++) {
|
|
|
|
if ((i < tns->first || i >= tns->end) && tns->thumbs[i].im != NULL)
|
|
|
|
tns_unload(tns, i);
|
|
|
|
}
|
|
|
|
tns->r_first = tns->first;
|
|
|
|
tns->r_end = tns->end;
|
|
|
|
|
|
|
|
for (i = tns->first; i < tns->end; i++) {
|
|
|
|
t = &tns->thumbs[i];
|
|
|
|
if (t->im != NULL) {
|
2014-09-29 09:02:29 +02:00
|
|
|
t->x = x + (thumb_sizes[tns->zl] - t->w) / 2;
|
|
|
|
t->y = y + (thumb_sizes[tns->zl] - t->h) / 2;
|
2014-09-25 20:57:24 +02:00
|
|
|
imlib_context_set_image(t->im);
|
|
|
|
imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h);
|
2015-01-04 15:38:49 +01:00
|
|
|
if (tns->files[i].flags & FF_MARK)
|
2014-09-25 20:57:24 +02:00
|
|
|
tns_mark(tns, i, true);
|
|
|
|
} else {
|
|
|
|
tns->loadnext = MIN(tns->loadnext, i);
|
|
|
|
}
|
2011-02-19 22:12:08 +01:00
|
|
|
if ((i + 1) % tns->cols == 0) {
|
2011-02-18 14:57:24 +01:00
|
|
|
x = tns->x;
|
2014-09-29 08:47:01 +02:00
|
|
|
y += tns->dim;
|
2011-02-16 21:40:20 +01:00
|
|
|
} else {
|
2014-09-29 08:47:01 +02:00
|
|
|
x += tns->dim;
|
2011-02-16 21:40:20 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-11 21:01:24 +02:00
|
|
|
tns->dirty = false;
|
2014-08-16 18:36:17 +02:00
|
|
|
tns_highlight(tns, *tns->sel, true);
|
2011-02-16 21:40:20 +01:00
|
|
|
}
|
|
|
|
|
2013-11-14 14:37:08 +01:00
|
|
|
void tns_mark(tns_t *tns, int n, bool mark)
|
2013-02-08 22:05:31 +01:00
|
|
|
{
|
2014-09-30 21:54:17 +02:00
|
|
|
if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
|
2013-11-14 14:37:08 +01:00
|
|
|
win_t *win = tns->win;
|
2014-08-18 15:42:10 +02:00
|
|
|
thumb_t *t = &tns->thumbs[n];
|
2021-10-12 01:46:35 +02:00
|
|
|
unsigned long col = win->win_bg.pixel;
|
2014-08-18 15:42:10 +02:00
|
|
|
int x = t->x + t->w, y = t->y + t->h;
|
|
|
|
|
2014-09-29 09:36:38 +02:00
|
|
|
win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
|
|
|
|
win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col);
|
2013-11-14 14:37:08 +01:00
|
|
|
|
2014-08-17 22:58:02 +02:00
|
|
|
if (mark)
|
2021-10-12 01:46:35 +02:00
|
|
|
col = win->mrk_fg.pixel;
|
2013-11-14 14:37:08 +01:00
|
|
|
|
2014-09-29 09:36:38 +02:00
|
|
|
win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col);
|
2014-08-17 22:58:02 +02:00
|
|
|
|
|
|
|
if (!mark && n == *tns->sel)
|
|
|
|
tns_highlight(tns, n, true);
|
2013-11-14 14:37:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tns_highlight(tns_t *tns, int n, bool hl)
|
|
|
|
{
|
2014-09-30 21:54:17 +02:00
|
|
|
if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
|
2013-11-14 14:37:08 +01:00
|
|
|
win_t *win = tns->win;
|
2014-08-18 15:42:10 +02:00
|
|
|
thumb_t *t = &tns->thumbs[n];
|
2021-10-12 01:46:35 +02:00
|
|
|
unsigned long col = hl ? win->win_fg.pixel : win->win_bg.pixel;
|
2014-09-29 09:36:38 +02:00
|
|
|
int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
|
2011-03-09 11:37:49 +01:00
|
|
|
|
2014-09-29 09:36:38 +02:00
|
|
|
win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
|
|
|
|
false, tns->bw, col);
|
2013-11-14 14:37:08 +01:00
|
|
|
|
2015-01-04 15:38:49 +01:00
|
|
|
if (tns->files[n].flags & FF_MARK)
|
2013-11-14 14:37:08 +01:00
|
|
|
tns_mark(tns, n, true);
|
2011-02-17 16:22:54 +01:00
|
|
|
}
|
2011-02-17 16:57:55 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
|
|
|
|
{
|
2012-07-15 18:52:30 +02:00
|
|
|
int old, max;
|
2011-02-17 16:57:55 +01:00
|
|
|
|
2014-08-16 18:36:17 +02:00
|
|
|
old = *tns->sel;
|
2012-07-15 18:52:30 +02:00
|
|
|
cnt = cnt > 1 ? cnt : 1;
|
2011-02-17 16:57:55 +01:00
|
|
|
|
|
|
|
switch (dir) {
|
2011-07-22 14:49:06 +02:00
|
|
|
case DIR_UP:
|
2014-08-16 18:36:17 +02:00
|
|
|
*tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
|
2011-02-17 16:57:55 +01:00
|
|
|
break;
|
2011-07-22 14:49:06 +02:00
|
|
|
case DIR_DOWN:
|
2014-09-30 21:54:17 +02:00
|
|
|
max = tns->cols * ((*tns->cnt - 1) / tns->cols) +
|
|
|
|
MIN((*tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
|
2014-08-16 18:36:17 +02:00
|
|
|
*tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
|
2012-03-16 20:56:10 +01:00
|
|
|
break;
|
|
|
|
case DIR_LEFT:
|
2014-08-16 18:36:17 +02:00
|
|
|
*tns->sel = MAX(*tns->sel - cnt, 0);
|
2012-03-16 20:56:10 +01:00
|
|
|
break;
|
|
|
|
case DIR_RIGHT:
|
2014-09-30 21:54:17 +02:00
|
|
|
*tns->sel = MIN(*tns->sel + cnt, *tns->cnt - 1);
|
2011-02-17 16:57:55 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-02-18 15:33:52 +01:00
|
|
|
|
2014-08-16 18:36:17 +02:00
|
|
|
if (*tns->sel != old) {
|
2011-09-17 17:23:51 +02:00
|
|
|
tns_highlight(tns, old, false);
|
2011-09-11 21:01:24 +02:00
|
|
|
tns_check_view(tns, false);
|
2011-02-19 22:12:08 +01:00
|
|
|
if (!tns->dirty)
|
2014-08-16 18:36:17 +02:00
|
|
|
tns_highlight(tns, *tns->sel, true);
|
2011-02-18 15:33:52 +01:00
|
|
|
}
|
2021-09-12 20:26:07 +02:00
|
|
|
win_set_title(tns->win, tns->files[fileidx].path);
|
2014-08-16 18:36:17 +02:00
|
|
|
return *tns->sel != old;
|
2011-02-17 16:22:54 +01:00
|
|
|
}
|
2011-02-17 17:28:13 +01:00
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
|
|
|
|
{
|
2011-10-27 16:21:01 +02:00
|
|
|
int d, max, old;
|
2011-02-20 00:08:17 +01:00
|
|
|
|
|
|
|
old = tns->first;
|
2011-10-27 16:21:01 +02:00
|
|
|
d = tns->cols * (screen ? tns->rows : 1);
|
|
|
|
|
|
|
|
if (dir == DIR_DOWN) {
|
2014-09-30 21:54:17 +02:00
|
|
|
max = *tns->cnt - tns->cols * tns->rows;
|
|
|
|
if (*tns->cnt % tns->cols != 0)
|
|
|
|
max += tns->cols - *tns->cnt % tns->cols;
|
2011-10-27 16:21:01 +02:00
|
|
|
tns->first = MIN(tns->first + d, max);
|
|
|
|
} else if (dir == DIR_UP) {
|
|
|
|
tns->first = MAX(tns->first - d, 0);
|
|
|
|
}
|
2011-02-20 00:08:17 +01:00
|
|
|
|
2011-10-27 16:21:01 +02:00
|
|
|
if (tns->first != old) {
|
2011-09-11 21:01:24 +02:00
|
|
|
tns_check_view(tns, true);
|
|
|
|
tns->dirty = true;
|
2011-02-20 00:08:17 +01:00
|
|
|
}
|
2011-02-20 00:28:28 +01:00
|
|
|
return tns->first != old;
|
2011-02-20 00:08:17 +01:00
|
|
|
}
|
|
|
|
|
2014-09-28 00:26:02 +02:00
|
|
|
bool tns_zoom(tns_t *tns, int d)
|
|
|
|
{
|
|
|
|
int i, oldzl;
|
|
|
|
|
|
|
|
oldzl = tns->zl;
|
|
|
|
tns->zl += -(d < 0) + (d > 0);
|
|
|
|
tns->zl = MAX(tns->zl, 0);
|
2014-09-29 09:02:29 +02:00
|
|
|
tns->zl = MIN(tns->zl, ARRLEN(thumb_sizes)-1);
|
2014-09-28 00:26:02 +02:00
|
|
|
|
2014-09-29 09:36:38 +02:00
|
|
|
tns->bw = ((thumb_sizes[tns->zl] - 1) >> 5) + 1;
|
2014-11-27 22:23:14 +01:00
|
|
|
tns->bw = MIN(tns->bw, 4);
|
2014-09-29 09:36:38 +02:00
|
|
|
tns->dim = thumb_sizes[tns->zl] + 2 * tns->bw + 6;
|
2014-09-29 08:47:01 +02:00
|
|
|
|
2014-09-28 00:26:02 +02:00
|
|
|
if (tns->zl != oldzl) {
|
2014-09-30 21:54:17 +02:00
|
|
|
for (i = 0; i < *tns->cnt; i++)
|
2014-09-28 00:26:02 +02:00
|
|
|
tns_unload(tns, i);
|
|
|
|
tns->dirty = true;
|
|
|
|
}
|
|
|
|
return tns->zl != oldzl;
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
int tns_translate(tns_t *tns, int x, int y)
|
|
|
|
{
|
2011-02-17 17:28:13 +01:00
|
|
|
int n;
|
|
|
|
|
2011-04-08 14:44:00 +02:00
|
|
|
if (x < tns->x || y < tns->y)
|
2011-02-17 17:28:13 +01:00
|
|
|
return -1;
|
|
|
|
|
2014-09-29 08:47:01 +02:00
|
|
|
n = tns->first + (y - tns->y) / tns->dim * tns->cols +
|
|
|
|
(x - tns->x) / tns->dim;
|
2014-09-30 21:54:17 +02:00
|
|
|
if (n >= *tns->cnt)
|
2011-08-19 12:20:00 +02:00
|
|
|
n = -1;
|
2011-02-18 15:02:49 +01:00
|
|
|
|
2011-08-19 12:20:00 +02:00
|
|
|
return n;
|
2011-02-17 17:28:13 +01:00
|
|
|
}
|