Krolyxon
Commit c627537

Branches

Refs →

nsxivthumbs.c

596 lines · 13.8 KB · C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021 nsxiv contributors
3 *
4 * This file is a part of nsxiv.
5 *
6 * nsxiv is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
10 *
11 * nsxiv is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "nsxiv.h"
21#define _THUMBS_CONFIG
22#include "config.h"
23
24#include <errno.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <unistd.h>
30#include <utime.h>
31
32#if HAVE_LIBEXIF
33#include <libexif/exif-data.h>
34void exif_auto_orientate(const fileinfo_t*);
35#endif
36Imlib_Image img_open(const fileinfo_t*);
37
38static char *cache_dir;
39extern const int fileidx;
40
41static char* tns_cache_filepath(const char *filepath)
42{
43 size_t len;
44 char *cfile = NULL;
45
46 if (*filepath != '/')
47 return NULL;
48
49 if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
50 /* don't cache images inside the cache directory! */
51 len = strlen(cache_dir) + strlen(filepath) + 2;
52 cfile = emalloc(len);
53 snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1);
54 }
55 return cfile;
56}
57
58static Imlib_Image tns_cache_load(const char *filepath, bool *outdated)
59{
60 char *cfile;
61 struct stat cstats, fstats;
62 Imlib_Image im = NULL;
63
64 if (stat(filepath, &fstats) < 0)
65 return NULL;
66
67 if ((cfile = tns_cache_filepath(filepath)) != NULL) {
68 if (stat(cfile, &cstats) == 0) {
69 if (cstats.st_mtime == fstats.st_mtime)
70 im = imlib_load_image(cfile);
71 else
72 *outdated = true;
73 }
74 free(cfile);
75 }
76 return im;
77}
78
79static void tns_cache_write(Imlib_Image im, const char *filepath, bool force)
80{
81 char *cfile, *dirend;
82 struct stat cstats, fstats;
83 struct utimbuf times;
84 Imlib_Load_Error err;
85
86 if (options->private_mode)
87 return;
88
89 if (stat(filepath, &fstats) < 0)
90 return;
91
92 if ((cfile = tns_cache_filepath(filepath)) != NULL) {
93 if (force || stat(cfile, &cstats) < 0 ||
94 cstats.st_mtime != fstats.st_mtime)
95 {
96 if ((dirend = strrchr(cfile, '/')) != NULL) {
97 *dirend = '\0';
98 if (r_mkdir(cfile) == -1) {
99 error(0, errno, "%s", cfile);
100 goto end;
101 }
102 *dirend = '/';
103 }
104 imlib_context_set_image(im);
105 if (imlib_image_has_alpha()) {
106 imlib_image_set_format("png");
107 } else {
108 imlib_image_set_format("jpg");
109 imlib_image_attach_data_value("quality", NULL, 90, NULL);
110 }
111 imlib_save_image_with_error_return(cfile, &err);
112 if (err)
113 goto end;
114 times.actime = fstats.st_atime;
115 times.modtime = fstats.st_mtime;
116 utime(cfile, &times);
117 }
118end:
119 free(cfile);
120 }
121}
122
123void tns_clean_cache(void)
124{
125 int dirlen;
126 char *cfile, *filename;
127 r_dir_t dir;
128
129 if (r_opendir(&dir, cache_dir, true) < 0) {
130 error(0, errno, "%s", cache_dir);
131 return;
132 }
133
134 dirlen = strlen(cache_dir);
135
136 while ((cfile = r_readdir(&dir, false)) != NULL) {
137 filename = cfile + dirlen;
138 if (access(filename, F_OK) < 0) {
139 if (unlink(cfile) < 0)
140 error(0, errno, "%s", cfile);
141 }
142 free(cfile);
143 }
144 r_closedir(&dir);
145}
146
147void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *win)
148{
149 int len;
150 const char *homedir, *dsuffix = "";
151
152 if (cnt != NULL && *cnt > 0) {
153 tns->thumbs = emalloc(*cnt * sizeof(thumb_t));
154 memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
155 } else {
156 tns->thumbs = NULL;
157 }
158 tns->files = files;
159 tns->cnt = cnt;
160 tns->initnext = tns->loadnext = 0;
161 tns->first = tns->end = tns->r_first = tns->r_end = 0;
162 tns->sel = sel;
163 tns->win = win;
164 tns->dirty = false;
165
166 tns->zl = THUMB_SIZE;
167 tns_zoom(tns, 0);
168
169 if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
170 homedir = getenv("HOME");
171 dsuffix = "/.cache";
172 }
173 if (homedir != NULL) {
174 const char *s = "/nsxiv";
175 free(cache_dir);
176 len = strlen(homedir) + strlen(dsuffix) + strlen(s) + 1;
177 cache_dir = emalloc(len);
178 snprintf(cache_dir, len, "%s%s%s", homedir, dsuffix, s);
179 } else {
180 error(0, 0, "Cache directory not found");
181 }
182}
183
184CLEANUP void tns_free(tns_t *tns)
185{
186 int i;
187
188 if (tns->thumbs != NULL) {
189 for (i = 0; i < *tns->cnt; i++) {
190 if (tns->thumbs[i].im != NULL) {
191 imlib_context_set_image(tns->thumbs[i].im);
192 imlib_free_image();
193 }
194 }
195 free(tns->thumbs);
196 tns->thumbs = NULL;
197 }
198
199 free(cache_dir);
200 cache_dir = NULL;
201}
202
203static Imlib_Image tns_scale_down(Imlib_Image im, int dim)
204{
205 int w, h;
206 float z, zw, zh;
207
208 imlib_context_set_image(im);
209 w = imlib_image_get_width();
210 h = imlib_image_get_height();
211 zw = (float) dim / (float) w;
212 zh = (float) dim / (float) h;
213 z = MIN(zw, zh);
214 z = MIN(z, 1.0);
215
216 if (z < 1.0) {
217 imlib_context_set_anti_alias(1);
218 im = imlib_create_cropped_scaled_image(0, 0, w, h,
219 MAX(z * w, 1), MAX(z * h, 1));
220 if (im == NULL)
221 error(EXIT_FAILURE, ENOMEM, NULL);
222 imlib_free_image_and_decache();
223 }
224 return im;
225}
226
227bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
228{
229 int maxwh = thumb_sizes[ARRLEN(thumb_sizes)-1];
230 bool cache_hit = false;
231 char *cfile;
232 thumb_t *t;
233 fileinfo_t *file;
234 Imlib_Image im = NULL;
235
236 if (n < 0 || n >= *tns->cnt)
237 return false;
238 file = &tns->files[n];
239 if (file->name == NULL || file->path == NULL)
240 return false;
241
242 t = &tns->thumbs[n];
243
244 if (t->im != NULL) {
245 imlib_context_set_image(t->im);
246 imlib_free_image();
247 t->im = NULL;
248 }
249
250 if (!force) {
251 if ((im = tns_cache_load(file->path, &force)) != NULL) {
252 imlib_context_set_image(im);
253 if (imlib_image_get_width() < maxwh &&
254 imlib_image_get_height() < maxwh)
255 {
256 if ((cfile = tns_cache_filepath(file->path)) != NULL) {
257 unlink(cfile);
258 free(cfile);
259 }
260 imlib_free_image_and_decache();
261 im = NULL;
262 } else {
263 cache_hit = true;
264 }
265#if HAVE_LIBEXIF
266 } else if (!force && !options->private_mode) {
267 int pw = 0, ph = 0, w, h, x = 0, y = 0;
268 bool err;
269 float zw, zh;
270 ExifData *ed;
271 ExifEntry *entry;
272 ExifContent *ifd;
273 ExifByteOrder byte_order;
274 int tmpfd;
275 char tmppath[] = "/tmp/nsxiv-XXXXXX";
276 Imlib_Image tmpim;
277
278 if ((ed = exif_data_new_from_file(file->path)) != NULL) {
279 if (ed->data != NULL && ed->size > 0 &&
280 (tmpfd = mkstemp(tmppath)) >= 0)
281 {
282 err = write(tmpfd, ed->data, ed->size) != ed->size;
283 close(tmpfd);
284
285 if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
286 byte_order = exif_data_get_byte_order(ed);
287 ifd = ed->ifd[EXIF_IFD_EXIF];
288 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
289 if (entry != NULL)
290 pw = exif_get_long(entry->data, byte_order);
291 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
292 if (entry != NULL)
293 ph = exif_get_long(entry->data, byte_order);
294
295 imlib_context_set_image(tmpim);
296 w = imlib_image_get_width();
297 h = imlib_image_get_height();
298
299 if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
300 zw = (float) pw / (float) w;
301 zh = (float) ph / (float) h;
302 if (zw < zh) {
303 pw /= zh;
304 x = (w - pw) / 2;
305 w = pw;
306 } else if (zw > zh) {
307 ph /= zw;
308 y = (h - ph) / 2;
309 h = ph;
310 }
311 }
312 if (w >= maxwh || h >= maxwh) {
313 if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
314 error(0, 0, "%s: error generating thumbnail", file->name);
315 }
316 imlib_free_image_and_decache();
317 }
318 unlink(tmppath);
319 }
320 exif_data_unref(ed);
321 }
322#endif /* HAVE_LIBEXIF */
323 }
324 }
325
326 if (im == NULL) {
327 if ((im = img_open(file)) == NULL)
328 return false;
329 }
330 imlib_context_set_image(im);
331
332 if (!cache_hit) {
333#if HAVE_LIBEXIF
334 exif_auto_orientate(file);
335#endif
336 im = tns_scale_down(im, maxwh);
337 imlib_context_set_image(im);
338 if (imlib_image_get_width() == maxwh || imlib_image_get_height() == maxwh)
339 tns_cache_write(im, file->path, true);
340 }
341
342 if (cache_only) {
343 imlib_free_image_and_decache();
344 } else {
345 t->im = tns_scale_down(im, thumb_sizes[tns->zl]);
346 imlib_context_set_image(t->im);
347 t->w = imlib_image_get_width();
348 t->h = imlib_image_get_height();
349 tns->dirty = true;
350 }
351 file->flags |= FF_TN_INIT;
352
353 if (n == tns->initnext)
354 while (++tns->initnext < *tns->cnt && ((++file)->flags & FF_TN_INIT));
355 if (n == tns->loadnext && !cache_only)
356 while (++tns->loadnext < tns->end && (++t)->im != NULL);
357
358 return true;
359}
360
361void tns_unload(tns_t *tns, int n)
362{
363 thumb_t *t;
364
365 if (n < 0 || n >= *tns->cnt)
366 return;
367
368 t = &tns->thumbs[n];
369
370 if (t->im != NULL) {
371 imlib_context_set_image(t->im);
372 imlib_free_image();
373 t->im = NULL;
374 }
375}
376
377static void tns_check_view(tns_t *tns, bool scrolled)
378{
379 int r;
380
381 if (tns == NULL)
382 return;
383
384 tns->first -= tns->first % tns->cols;
385 r = *tns->sel % tns->cols;
386
387 if (scrolled) {
388 /* move selection into visible area */
389 if (*tns->sel >= tns->first + tns->cols * tns->rows)
390 *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
391 else if (*tns->sel < tns->first)
392 *tns->sel = tns->first + r;
393 } else {
394 /* scroll to selection */
395 if (tns->first + tns->cols * tns->rows <= *tns->sel) {
396 tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
397 tns->dirty = true;
398 } else if (tns->first > *tns->sel) {
399 tns->first = *tns->sel - r;
400 tns->dirty = true;
401 }
402 }
403}
404
405void tns_render(tns_t *tns)
406{
407 thumb_t *t;
408 win_t *win;
409 int i, cnt, r, x, y;
410
411 if (!tns->dirty)
412 return;
413
414 win = tns->win;
415 win_clear(win);
416 imlib_context_set_drawable(win->buf.pm);
417
418 tns->cols = MAX(1, win->w / tns->dim);
419 tns->rows = MAX(1, win->h / tns->dim);
420
421 if (*tns->cnt < tns->cols * tns->rows) {
422 tns->first = 0;
423 cnt = *tns->cnt;
424 } else {
425 tns_check_view(tns, false);
426 cnt = tns->cols * tns->rows;
427 if ((r = tns->first + cnt - *tns->cnt) >= tns->cols)
428 tns->first -= r - r % tns->cols;
429 if (r > 0)
430 cnt -= r % tns->cols;
431 }
432 r = cnt % tns->cols ? 1 : 0;
433 tns->x = x = (win->w - MIN(cnt, tns->cols) * tns->dim) / 2 + tns->bw + 3;
434 tns->y = y = (win->h - (cnt / tns->cols + r) * tns->dim) / 2 + tns->bw + 3;
435 tns->loadnext = *tns->cnt;
436 tns->end = tns->first + cnt;
437
438 for (i = tns->r_first; i < tns->r_end; i++) {
439 if ((i < tns->first || i >= tns->end) && tns->thumbs[i].im != NULL)
440 tns_unload(tns, i);
441 }
442 tns->r_first = tns->first;
443 tns->r_end = tns->end;
444
445 for (i = tns->first; i < tns->end; i++) {
446 t = &tns->thumbs[i];
447 if (t->im != NULL) {
448 t->x = x + (thumb_sizes[tns->zl] - t->w) / 2;
449 t->y = y + (thumb_sizes[tns->zl] - t->h) / 2;
450 imlib_context_set_image(t->im);
451 imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h);
452 if (tns->files[i].flags & FF_MARK)
453 tns_mark(tns, i, true);
454 } else {
455 tns->loadnext = MIN(tns->loadnext, i);
456 }
457 if ((i + 1) % tns->cols == 0) {
458 x = tns->x;
459 y += tns->dim;
460 } else {
461 x += tns->dim;
462 }
463 }
464 tns->dirty = false;
465 tns_highlight(tns, *tns->sel, true);
466}
467
468void tns_mark(tns_t *tns, int n, bool mark)
469{
470 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
471 win_t *win = tns->win;
472 thumb_t *t = &tns->thumbs[n];
473 unsigned long col = win->win_bg.pixel;
474 int x = t->x + t->w, y = t->y + t->h;
475
476 win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
477 win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col);
478
479 if (mark)
480 col = win->mrk_fg.pixel;
481
482 win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col);
483
484 if (!mark && n == *tns->sel)
485 tns_highlight(tns, n, true);
486 }
487}
488
489void tns_highlight(tns_t *tns, int n, bool hl)
490{
491 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
492 win_t *win = tns->win;
493 thumb_t *t = &tns->thumbs[n];
494 unsigned long col = hl ? win->win_fg.pixel : win->win_bg.pixel;
495 int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
496
497 win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
498 false, tns->bw, col);
499
500 if (tns->files[n].flags & FF_MARK)
501 tns_mark(tns, n, true);
502 }
503}
504
505bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
506{
507 int old, max;
508
509 old = *tns->sel;
510 cnt = cnt > 1 ? cnt : 1;
511
512 switch (dir) {
513 case DIR_UP:
514 *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
515 break;
516 case DIR_DOWN:
517 max = tns->cols * ((*tns->cnt - 1) / tns->cols) +
518 MIN((*tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
519 *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
520 break;
521 case DIR_LEFT:
522 *tns->sel = MAX(*tns->sel - cnt, 0);
523 break;
524 case DIR_RIGHT:
525 *tns->sel = MIN(*tns->sel + cnt, *tns->cnt - 1);
526 break;
527 }
528
529 if (*tns->sel != old) {
530 tns_highlight(tns, old, false);
531 tns_check_view(tns, false);
532 if (!tns->dirty)
533 tns_highlight(tns, *tns->sel, true);
534 }
535 win_set_title(tns->win, tns->files[fileidx].path);
536 return *tns->sel != old;
537}
538
539bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
540{
541 int d, max, old;
542
543 old = tns->first;
544 d = tns->cols * (screen ? tns->rows : 1);
545
546 if (dir == DIR_DOWN) {
547 max = *tns->cnt - tns->cols * tns->rows;
548 if (*tns->cnt % tns->cols != 0)
549 max += tns->cols - *tns->cnt % tns->cols;
550 tns->first = MIN(tns->first + d, max);
551 } else if (dir == DIR_UP) {
552 tns->first = MAX(tns->first - d, 0);
553 }
554
555 if (tns->first != old) {
556 tns_check_view(tns, true);
557 tns->dirty = true;
558 }
559 return tns->first != old;
560}
561
562bool tns_zoom(tns_t *tns, int d)
563{
564 int i, oldzl;
565
566 oldzl = tns->zl;
567 tns->zl += -(d < 0) + (d > 0);
568 tns->zl = MAX(tns->zl, 0);
569 tns->zl = MIN(tns->zl, ARRLEN(thumb_sizes)-1);
570
571 tns->bw = ((thumb_sizes[tns->zl] - 1) >> 5) + 1;
572 tns->bw = MIN(tns->bw, 4);
573 tns->dim = thumb_sizes[tns->zl] + 2 * tns->bw + 6;
574
575 if (tns->zl != oldzl) {
576 for (i = 0; i < *tns->cnt; i++)
577 tns_unload(tns, i);
578 tns->dirty = true;
579 }
580 return tns->zl != oldzl;
581}
582
583int tns_translate(tns_t *tns, int x, int y)
584{
585 int n;
586
587 if (x < tns->x || y < tns->y)
588 return -1;
589
590 n = tns->first + (y - tns->y) / tns->dim * tns->cols +
591 (x - tns->x) / tns->dim;
592 if (n >= *tns->cnt)
593 n = -1;
594
595 return n;
596}