Krolyxon
Commit c1e0843

Branches

Refs →

nsxivthumbs.c

569 lines · 12.7 KB · C

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