Krolyxon
Commit 0f6cb93

Branches

Refs →

nsxivthumbs.c

537 lines · 12.1 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)
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 && cstats.st_mtime == fstats.st_mtime)
72 im = imlib_load_image(cfile);
73 free(cfile);
74 }
75 return im;
76}
77
78void tns_cache_write(thumb_t *t, const fileinfo_t *file, bool force)
79{
80 char *cfile, *dirend;
81 struct stat cstats, fstats;
82 struct utimbuf times;
83 Imlib_Load_Error err = 0;
84
85 if (t == NULL || t->im == NULL)
86 return;
87 if (file == NULL || file->name == NULL || file->path == NULL)
88 return;
89 if (stat(file->path, &fstats) < 0)
90 return;
91
92 if ((cfile = tns_cache_filepath(file->path)) != 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 err = r_mkdir(cfile);
99 *dirend = '/';
100 }
101 if (err == 0) {
102 imlib_context_set_image(t->im);
103 imlib_image_set_format("png");
104 imlib_save_image_with_error_return(cfile, &err);
105 }
106 if (err == 0) {
107 times.actime = fstats.st_atime;
108 times.modtime = fstats.st_mtime;
109 utime(cfile, &times);
110 }
111 }
112 free(cfile);
113 }
114}
115
116void tns_clean_cache(tns_t *tns)
117{
118 int dirlen;
119 bool delete;
120 char *cfile, *filename, *tpos;
121 r_dir_t dir;
122
123 if (cache_dir == NULL)
124 return;
125
126 if (r_opendir(&dir, cache_dir) < 0) {
127 warn("could not open thumbnail cache directory: %s", cache_dir);
128 return;
129 }
130
131 dirlen = strlen(cache_dir);
132
133 while ((cfile = r_readdir(&dir)) != NULL) {
134 filename = cfile + dirlen;
135 delete = false;
136
137 if ((tpos = strrchr(filename, '.')) != NULL) {
138 *tpos = '\0';
139 if (access(filename, F_OK) < 0)
140 delete = true;
141 *tpos = '.';
142 }
143 if (delete) {
144 if (unlink(cfile) < 0)
145 warn("could not delete cache file: %s", cfile);
146 }
147 free(cfile);
148 }
149 r_closedir(&dir);
150}
151
152
153void tns_init(tns_t *tns, const fileinfo_t *files, int cnt, int *sel, win_t *win)
154{
155 int len;
156 const char *homedir, *dsuffix = "";
157
158 if (tns == NULL)
159 return;
160
161 if (cnt > 0) {
162 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
163 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
164 } else {
165 tns->thumbs = NULL;
166 }
167 tns->files = files;
168 tns->cap = cnt;
169 tns->cnt = tns->loadnext = tns->first = 0;
170 tns->sel = sel;
171 tns->win = win;
172 tns->dirty = false;
173
174 if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
175 homedir = getenv("HOME");
176 dsuffix = "/.cache";
177 }
178 if (homedir != NULL) {
179 free(cache_dir);
180 len = strlen(homedir) + strlen(dsuffix) + 6;
181 cache_dir = (char*) s_malloc(len);
182 snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
183 } else {
184 warn("could not locate thumbnail cache directory");
185 }
186}
187
188void tns_free(tns_t *tns)
189{
190 int i;
191
192 if (tns == NULL)
193 return;
194
195 if (tns->thumbs != NULL) {
196 for (i = 0; i < tns->cnt; i++) {
197 if (tns->thumbs[i].im != NULL) {
198 imlib_context_set_image(tns->thumbs[i].im);
199 imlib_free_image();
200 }
201 }
202 free(tns->thumbs);
203 tns->thumbs = NULL;
204 }
205
206 free(cache_dir);
207 cache_dir = NULL;
208}
209
210bool tns_load(tns_t *tns, int n, bool force)
211{
212 int w, h;
213 bool cache_hit = false;
214 float z, zw, zh;
215 thumb_t *t;
216 Imlib_Image im = NULL;
217 const fileinfo_t *file;
218
219 if (tns == NULL || tns->thumbs == NULL)
220 return false;
221 if (n < 0 || n >= tns->cap)
222 return false;
223 file = &tns->files[n];
224 if (file->name == NULL || file->path == NULL)
225 return false;
226
227 t = &tns->thumbs[n];
228
229 if (t->im != NULL) {
230 imlib_context_set_image(t->im);
231 imlib_free_image();
232 }
233
234 if (!force && (im = tns_cache_load(file->path)) != NULL) {
235 cache_hit = true;
236 } else {
237#if HAVE_LIBEXIF
238 if (!force) {
239 int pw = 0, ph = 0, x = 0, y = 0;
240 bool err;
241 ExifData *ed;
242 ExifEntry *entry;
243 ExifContent *ifd;
244 ExifByteOrder byte_order;
245 int tmpfd;
246 char tmppath[] = "/tmp/sxiv-XXXXXX";
247 Imlib_Image tmpim;
248
249 if ((ed = exif_data_new_from_file(file->path)) != NULL &&
250 ed->data != NULL && ed->size > 0)
251 {
252 if ((tmpfd = mkstemp(tmppath)) >= 0) {
253 err = write(tmpfd, ed->data, ed->size) != ed->size;
254 close(tmpfd);
255
256 if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
257 byte_order = exif_data_get_byte_order(ed);
258 ifd = ed->ifd[EXIF_IFD_EXIF];
259 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
260 if (entry != NULL)
261 pw = exif_get_long(entry->data, byte_order);
262 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
263 if (entry != NULL)
264 ph = exif_get_long(entry->data, byte_order);
265
266 imlib_context_set_image(tmpim);
267 w = imlib_image_get_width();
268 h = imlib_image_get_height();
269
270 if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
271 zw = (float) pw / (float) w;
272 zh = (float) ph / (float) h;
273 if (zw < zh) {
274 pw /= zh;
275 x = (w - pw) / 2;
276 w = pw;
277 } else if (zw > zh) {
278 ph /= zw;
279 y = (h - ph) / 2;
280 h = ph;
281 }
282 }
283 if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
284 die("could not allocate memory");
285 imlib_free_image_and_decache();
286 }
287 unlink(tmppath);
288 }
289 exif_data_unref(ed);
290 }
291 }
292#endif
293 if (im == NULL && (access(file->path, R_OK) < 0 ||
294 (im = imlib_load_image(file->path)) == NULL))
295 {
296 warn("could not open image: %s", file->name);
297 return false;
298 }
299 }
300 imlib_context_set_image(im);
301 imlib_context_set_anti_alias(1);
302
303#if HAVE_LIBEXIF
304 if (!cache_hit)
305 exif_auto_orientate(file);
306#endif
307
308 w = imlib_image_get_width();
309 h = imlib_image_get_height();
310 zw = (float) THUMB_SIZE / (float) w;
311 zh = (float) THUMB_SIZE / (float) h;
312 z = MIN(zw, zh);
313 z = MIN(z, 1.0);
314 t->w = z * w;
315 t->h = z * h;
316
317 t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
318 if (t->im == NULL)
319 die("could not allocate memory");
320
321 imlib_free_image_and_decache();
322
323 if (!cache_hit)
324 tns_cache_write(t, file, true);
325
326 t->loaded = true;
327 tns->dirty = true;
328 return true;
329}
330
331void tns_check_view(tns_t *tns, bool scrolled)
332{
333 int r;
334
335 if (tns == NULL)
336 return;
337
338 tns->first -= tns->first % tns->cols;
339 r = *tns->sel % tns->cols;
340
341 if (scrolled) {
342 /* move selection into visible area */
343 if (*tns->sel >= tns->first + tns->cols * tns->rows)
344 *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
345 else if (*tns->sel < tns->first)
346 *tns->sel = tns->first + r;
347 } else {
348 /* scroll to selection */
349 if (tns->first + tns->cols * tns->rows <= *tns->sel) {
350 tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
351 tns->dirty = true;
352 } else if (tns->first > *tns->sel) {
353 tns->first = *tns->sel - r;
354 tns->dirty = true;
355 }
356 }
357}
358
359void tns_render(tns_t *tns)
360{
361 thumb_t *t;
362 win_t *win;
363 int i, cnt, r, x, y;
364
365 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
366 return;
367 if (!tns->dirty)
368 return;
369
370 win = tns->win;
371 win_clear(win);
372 imlib_context_set_drawable(win->buf.pm);
373
374 tns->cols = MAX(1, win->w / thumb_dim);
375 tns->rows = MAX(1, win->h / thumb_dim);
376
377 if (tns->cnt < tns->cols * tns->rows) {
378 tns->first = 0;
379 cnt = tns->cnt;
380 } else {
381 tns_check_view(tns, false);
382 cnt = tns->cols * tns->rows;
383 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
384 tns->first -= r - r % tns->cols;
385 if (r > 0)
386 cnt -= r % tns->cols;
387 }
388
389 r = cnt % tns->cols ? 1 : 0;
390 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
391 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
392
393 for (i = 0; i < cnt; i++) {
394 t = &tns->thumbs[tns->first + i];
395 t->x = x + (THUMB_SIZE - t->w) / 2;
396 t->y = y + (THUMB_SIZE - t->h) / 2;
397 imlib_context_set_image(t->im);
398 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
399 t->x, t->y, t->w, t->h);
400 if (tns->files[tns->first + i].marked)
401 tns_mark(tns, tns->first + i, true);
402 if ((i + 1) % tns->cols == 0) {
403 x = tns->x;
404 y += thumb_dim;
405 } else {
406 x += thumb_dim;
407 }
408 }
409 tns->dirty = false;
410 tns_highlight(tns, *tns->sel, true);
411}
412
413void tns_mark(tns_t *tns, int n, bool mark)
414{
415 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
416 return;
417
418 if (n >= 0 && n < tns->cnt) {
419 win_t *win = tns->win;
420 thumb_t *t = &tns->thumbs[n];
421 unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
422 int x = t->x + t->w, y = t->y + t->h;
423
424 win_draw_rect(win, x - 2, y + 1, 1, 2, true, 1, col);
425 win_draw_rect(win, x + 1, y - 2, 2, 1, true, 1, col);
426
427 if (mark)
428 col = win->selcol;
429
430 win_draw_rect(win, x - 1, y + 1, 6, 2, true, 1, col);
431 win_draw_rect(win, x + 1, y - 1, 2, 6, true, 1, col);
432
433 if (!mark && n == *tns->sel)
434 tns_highlight(tns, n, true);
435 }
436}
437
438void tns_highlight(tns_t *tns, int n, bool hl)
439{
440 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
441 return;
442
443 if (n >= 0 && n < tns->cnt) {
444 win_t *win = tns->win;
445 thumb_t *t = &tns->thumbs[n];
446 unsigned long col;
447
448 if (hl)
449 col = win->selcol;
450 else
451 col = win->fullscreen ? win->fscol : win->bgcol;
452
453 win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, false, 2, col);
454
455 if (tns->files[n].marked)
456 tns_mark(tns, n, true);
457 }
458}
459
460bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
461{
462 int old, max;
463
464 if (tns == NULL || tns->thumbs == NULL)
465 return false;
466
467 old = *tns->sel;
468 cnt = cnt > 1 ? cnt : 1;
469
470 switch (dir) {
471 case DIR_UP:
472 *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
473 break;
474 case DIR_DOWN:
475 max = tns->cols * ((tns->cnt - 1) / tns->cols) +
476 MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
477 *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
478 break;
479 case DIR_LEFT:
480 *tns->sel = MAX(*tns->sel - cnt, 0);
481 break;
482 case DIR_RIGHT:
483 *tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1);
484 break;
485 }
486
487 if (*tns->sel != old) {
488 tns_highlight(tns, old, false);
489 tns_check_view(tns, false);
490 if (!tns->dirty)
491 tns_highlight(tns, *tns->sel, true);
492 }
493 return *tns->sel != old;
494}
495
496bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
497{
498 int d, max, old;
499
500 if (tns == NULL)
501 return false;
502
503 old = tns->first;
504 d = tns->cols * (screen ? tns->rows : 1);
505
506 if (dir == DIR_DOWN) {
507 max = tns->cnt - tns->cols * tns->rows;
508 if (tns->cnt % tns->cols != 0)
509 max += tns->cols - tns->cnt % tns->cols;
510 tns->first = MIN(tns->first + d, max);
511 } else if (dir == DIR_UP) {
512 tns->first = MAX(tns->first - d, 0);
513 }
514
515 if (tns->first != old) {
516 tns_check_view(tns, true);
517 tns->dirty = true;
518 }
519 return tns->first != old;
520}
521
522int tns_translate(tns_t *tns, int x, int y)
523{
524 int n;
525
526 if (tns == NULL || tns->thumbs == NULL)
527 return -1;
528 if (x < tns->x || y < tns->y)
529 return -1;
530
531 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
532 (x - tns->x) / thumb_dim;
533 if (n >= tns->cnt)
534 n = -1;
535
536 return n;
537}