Krolyxon
Commit 50652f6

Branches

Refs →

nsxivthumbs.c

547 lines · 12.6 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 if (cache_dir != NULL)
180 free(cache_dir);
181 len = strlen(homedir) + strlen(dsuffix) + 6;
182 cache_dir = (char*) s_malloc(len);
183 snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
184 } else {
185 warn("could not locate thumbnail cache directory");
186 }
187}
188
189void tns_free(tns_t *tns)
190{
191 int i;
192
193 if (tns == NULL)
194 return;
195
196 if (tns->thumbs != NULL) {
197 for (i = 0; i < tns->cnt; i++) {
198 if (tns->thumbs[i].im != NULL) {
199 imlib_context_set_image(tns->thumbs[i].im);
200 imlib_free_image();
201 }
202 }
203 free(tns->thumbs);
204 tns->thumbs = NULL;
205 }
206
207 if (cache_dir != NULL) {
208 free(cache_dir);
209 cache_dir = NULL;
210 }
211}
212
213bool tns_load(tns_t *tns, int n, bool force)
214{
215 int w, h;
216 bool cache_hit = false;
217 float z, zw, zh;
218 thumb_t *t;
219 Imlib_Image im = NULL;
220 const fileinfo_t *file;
221
222 if (tns == NULL || tns->thumbs == NULL)
223 return false;
224 if (n < 0 || n >= tns->cap)
225 return false;
226 file = &tns->files[n];
227 if (file->name == NULL || file->path == NULL)
228 return false;
229
230 t = &tns->thumbs[n];
231
232 if (t->im != NULL) {
233 imlib_context_set_image(t->im);
234 imlib_free_image();
235 }
236
237 if (!force && (im = tns_cache_load(file->path)) != NULL) {
238 cache_hit = true;
239 } else {
240#if HAVE_LIBEXIF
241 if (!force) {
242 int pw = 0, ph = 0, x = 0, y = 0;
243 bool err;
244 ExifData *ed;
245 ExifEntry *entry;
246 ExifContent *ifd;
247 ExifByteOrder byte_order;
248 int tmpfd;
249 char tmppath[] = "/tmp/sxiv-XXXXXX";
250 Imlib_Image tmpim;
251
252 if ((ed = exif_data_new_from_file(file->path)) != NULL &&
253 ed->data != NULL && ed->size > 0)
254 {
255 if ((tmpfd = mkstemp(tmppath)) >= 0) {
256 err = write(tmpfd, ed->data, ed->size) != ed->size;
257 close(tmpfd);
258
259 if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
260 byte_order = exif_data_get_byte_order(ed);
261 ifd = ed->ifd[EXIF_IFD_EXIF];
262 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
263 if (entry != NULL)
264 pw = exif_get_long(entry->data, byte_order);
265 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
266 if (entry != NULL)
267 ph = exif_get_long(entry->data, byte_order);
268
269 imlib_context_set_image(tmpim);
270 w = imlib_image_get_width();
271 h = imlib_image_get_height();
272
273 if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
274 zw = (float) pw / (float) w;
275 zh = (float) ph / (float) h;
276 if (zw < zh) {
277 pw /= zh;
278 x = (w - pw) / 2;
279 w = pw;
280 } else if (zw > zh) {
281 ph /= zw;
282 y = (h - ph) / 2;
283 h = ph;
284 }
285 }
286 if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
287 die("could not allocate memory");
288 imlib_free_image_and_decache();
289 }
290 unlink(tmppath);
291 }
292 exif_data_unref(ed);
293 }
294 }
295#endif
296 if (im == NULL && (access(file->path, R_OK) < 0 ||
297 (im = imlib_load_image(file->path)) == NULL))
298 {
299 warn("could not open image: %s", file->name);
300 return false;
301 }
302 }
303 imlib_context_set_image(im);
304 imlib_context_set_anti_alias(1);
305
306#if HAVE_LIBEXIF
307 if (!cache_hit)
308 exif_auto_orientate(file);
309#endif
310
311 w = imlib_image_get_width();
312 h = imlib_image_get_height();
313 zw = (float) THUMB_SIZE / (float) w;
314 zh = (float) THUMB_SIZE / (float) h;
315 z = MIN(zw, zh);
316 z = MIN(z, 1.0);
317 t->w = z * w;
318 t->h = z * h;
319
320 t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
321 if (t->im == NULL)
322 die("could not allocate memory");
323
324 imlib_free_image_and_decache();
325
326 if (!cache_hit)
327 tns_cache_write(t, file, true);
328
329 t->loaded = true;
330 tns->dirty = true;
331 return true;
332}
333
334void tns_check_view(tns_t *tns, bool scrolled)
335{
336 int r;
337
338 if (tns == NULL)
339 return;
340
341 tns->first -= tns->first % tns->cols;
342 r = *tns->sel % tns->cols;
343
344 if (scrolled) {
345 /* move selection into visible area */
346 if (*tns->sel >= tns->first + tns->cols * tns->rows)
347 *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
348 else if (*tns->sel < tns->first)
349 *tns->sel = tns->first + r;
350 } else {
351 /* scroll to selection */
352 if (tns->first + tns->cols * tns->rows <= *tns->sel) {
353 tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
354 tns->dirty = true;
355 } else if (tns->first > *tns->sel) {
356 tns->first = *tns->sel - r;
357 tns->dirty = true;
358 }
359 }
360}
361
362void tns_render(tns_t *tns)
363{
364 thumb_t *t;
365 win_t *win;
366 int i, cnt, r, x, y;
367
368 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
369 return;
370 if (!tns->dirty)
371 return;
372
373 win = tns->win;
374 win_clear(win);
375 imlib_context_set_drawable(win->buf.pm);
376
377 tns->cols = MAX(1, win->w / thumb_dim);
378 tns->rows = MAX(1, win->h / thumb_dim);
379
380 if (tns->cnt < tns->cols * tns->rows) {
381 tns->first = 0;
382 cnt = tns->cnt;
383 } else {
384 tns_check_view(tns, false);
385 cnt = tns->cols * tns->rows;
386 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
387 tns->first -= r - r % tns->cols;
388 if (r > 0)
389 cnt -= r % tns->cols;
390 }
391
392 r = cnt % tns->cols ? 1 : 0;
393 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
394 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
395
396 for (i = 0; i < cnt; i++) {
397 t = &tns->thumbs[tns->first + i];
398 t->x = x + (THUMB_SIZE - t->w) / 2;
399 t->y = y + (THUMB_SIZE - t->h) / 2;
400 imlib_context_set_image(t->im);
401 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
402 t->x, t->y, t->w, t->h);
403 if (tns->files[tns->first + i].marked)
404 tns_mark(tns, tns->first + i, true);
405 if ((i + 1) % tns->cols == 0) {
406 x = tns->x;
407 y += thumb_dim;
408 } else {
409 x += thumb_dim;
410 }
411 }
412 tns->dirty = false;
413 tns_highlight(tns, *tns->sel, true);
414}
415
416void tns_mark(tns_t *tns, int n, bool mark)
417{
418 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
419 return;
420
421 if (n >= 0 && n < tns->cnt) {
422 unsigned long col;
423 thumb_t *t = &tns->thumbs[n];
424 win_t *win = tns->win;
425 int x = t->x, y = t->y, w = t->w, h = t->h;
426
427 if (mark || n == *tns->sel)
428 col = win->selcol;
429 else if (win->fullscreen)
430 col = win->fscol;
431 else
432 col = win->bgcol;
433
434 win_draw_rect(win, win->buf.pm, x - 4, y - 4, 8, 2, true, 0, col);
435 win_draw_rect(win, win->buf.pm, x - 4, y - 4, 2, 8, true, 0, col);
436 win_draw_rect(win, win->buf.pm, x + w - 4, y - 4, 8, 2, true, 0, col);
437 win_draw_rect(win, win->buf.pm, x + w + 2, y - 4, 2, 8, true, 0, col);
438 win_draw_rect(win, win->buf.pm, x - 4, y + h + 2, 8, 2, true, 0, col);
439 win_draw_rect(win, win->buf.pm, x - 4, y + h - 4, 2, 8, true, 0, col);
440 win_draw_rect(win, win->buf.pm, x + w - 4, y + h + 2, 8, 2, true, 0, col);
441 win_draw_rect(win, win->buf.pm, x + w + 2, y + h - 4, 2, 8, true, 0, col);
442 }
443}
444
445void tns_highlight(tns_t *tns, int n, bool hl)
446{
447 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
448 return;
449
450 if (n >= 0 && n < tns->cnt) {
451 unsigned long col;
452 thumb_t *t = &tns->thumbs[n];
453 win_t *win = tns->win;
454
455 if (hl)
456 col = win->selcol;
457 else if (win->fullscreen)
458 col = win->fscol;
459 else
460 col = win->bgcol;
461
462 win_draw_rect(win, win->buf.pm, t->x - 3, t->y - 3, t->w + 6, t->h + 6,
463 false, 2, col);
464
465 if (!hl && tns->files[n].marked)
466 tns_mark(tns, n, true);
467 }
468}
469
470bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
471{
472 int old, max;
473
474 if (tns == NULL || tns->thumbs == NULL)
475 return false;
476
477 old = *tns->sel;
478 cnt = cnt > 1 ? cnt : 1;
479
480 switch (dir) {
481 case DIR_UP:
482 *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
483 break;
484 case DIR_DOWN:
485 max = tns->cols * ((tns->cnt - 1) / tns->cols) +
486 MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
487 *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
488 break;
489 case DIR_LEFT:
490 *tns->sel = MAX(*tns->sel - cnt, 0);
491 break;
492 case DIR_RIGHT:
493 *tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1);
494 break;
495 }
496
497 if (*tns->sel != old) {
498 tns_highlight(tns, old, false);
499 tns_check_view(tns, false);
500 if (!tns->dirty)
501 tns_highlight(tns, *tns->sel, true);
502 }
503 return *tns->sel != old;
504}
505
506bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
507{
508 int d, max, old;
509
510 if (tns == NULL)
511 return false;
512
513 old = tns->first;
514 d = tns->cols * (screen ? tns->rows : 1);
515
516 if (dir == DIR_DOWN) {
517 max = tns->cnt - tns->cols * tns->rows;
518 if (tns->cnt % tns->cols != 0)
519 max += tns->cols - tns->cnt % tns->cols;
520 tns->first = MIN(tns->first + d, max);
521 } else if (dir == DIR_UP) {
522 tns->first = MAX(tns->first - d, 0);
523 }
524
525 if (tns->first != old) {
526 tns_check_view(tns, true);
527 tns->dirty = true;
528 }
529 return tns->first != old;
530}
531
532int tns_translate(tns_t *tns, int x, int y)
533{
534 int n;
535
536 if (tns == NULL || tns->thumbs == NULL)
537 return -1;
538 if (x < tns->x || y < tns->y)
539 return -1;
540
541 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
542 (x - tns->x) / thumb_dim;
543 if (n >= tns->cnt)
544 n = -1;
545
546 return n;
547}