Krolyxon
Commit 3b8a79f

Branches

Refs →

nsxivthumbs.c

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