Krolyxon
Commit 0f91d28

Branches

Refs →

nsxivthumbs.c

471 lines · 9.8 KB · C

1/* sxiv: thumbs.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include <stdlib.h>
20#include <string.h>
21#include <sys/time.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25
26#include "thumbs.h"
27#include "util.h"
28
29#define _THUMBS_CONFIG
30#include "config.h"
31
32#ifdef __NetBSD__
33#define st_mtim st_mtimespec
34#define st_atim st_atimespec
35#endif
36
37#ifdef EXIF_SUPPORT
38void exif_auto_orientate(const fileinfo_t*);
39#endif
40
41const int thumb_dim = THUMB_SIZE + 10;
42char *cache_dir = NULL;
43
44int tns_cache_enabled() {
45 struct stat stats;
46
47 return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) &&
48 !access(cache_dir, W_OK);
49}
50
51char* tns_cache_filepath(const char *filepath) {
52 size_t len;
53 char *cfile = NULL;
54
55 if (!cache_dir || !filepath || *filepath != '/')
56 return NULL;
57
58 if (strncmp(filepath, cache_dir, strlen(cache_dir))) {
59 len = strlen(cache_dir) + strlen(filepath) + 6;
60 cfile = (char*) s_malloc(len);
61 snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
62 }
63
64 return cfile;
65}
66
67Imlib_Image* tns_cache_load(const char *filepath) {
68 char *cfile;
69 struct stat cstats, fstats;
70 Imlib_Image *im = NULL;
71
72 if (!filepath)
73 return NULL;
74
75 if (stat(filepath, &fstats))
76 return NULL;
77
78 if ((cfile = tns_cache_filepath(filepath))) {
79 if (!stat(cfile, &cstats) &&
80 cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
81 cstats.st_mtim.tv_nsec / 1000 == fstats.st_mtim.tv_nsec / 1000)
82 {
83 im = imlib_load_image(cfile);
84 }
85 free(cfile);
86 }
87
88 return im;
89}
90
91void tns_cache_write(thumb_t *t, Bool force) {
92 char *cfile, *dirend;
93 struct stat cstats, fstats;
94 struct timeval times[2];
95 Imlib_Load_Error err = 0;
96
97 if (!t || !t->im || !t->file || !t->file->name || !t->file->path)
98 return;
99
100 if (stat(t->file->path, &fstats))
101 return;
102
103 if ((cfile = tns_cache_filepath(t->file->path))) {
104 if (force || stat(cfile, &cstats) ||
105 cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
106 cstats.st_mtim.tv_nsec / 1000 != fstats.st_mtim.tv_nsec / 1000)
107 {
108 if ((dirend = strrchr(cfile, '/'))) {
109 *dirend = '\0';
110 err = r_mkdir(cfile);
111 *dirend = '/';
112 }
113
114 if (!err) {
115 imlib_context_set_image(t->im);
116 imlib_image_set_format("png");
117 imlib_save_image_with_error_return(cfile, &err);
118 }
119
120 if (err) {
121 warn("could not cache thumbnail: %s", t->file->name);
122 } else {
123 TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
124 TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
125 utimes(cfile, times);
126 }
127 }
128 free(cfile);
129 }
130}
131
132void tns_clean_cache(tns_t *tns) {
133 int dirlen, delete;
134 char *cfile, *filename, *tpos;
135 r_dir_t dir;
136
137 if (!cache_dir)
138 return;
139
140 if (r_opendir(&dir, cache_dir)) {
141 warn("could not open thumbnail cache directory: %s", cache_dir);
142 return;
143 }
144
145 dirlen = strlen(cache_dir);
146
147 while ((cfile = r_readdir(&dir))) {
148 filename = cfile + dirlen;
149 delete = 0;
150
151 if ((tpos = strrchr(filename, '.'))) {
152 *tpos = '\0';
153 delete = access(filename, F_OK);
154 *tpos = '.';
155 }
156
157 if (delete && unlink(cfile))
158 warn("could not delete cache file: %s", cfile);
159
160 free(cfile);
161 }
162
163 r_closedir(&dir);
164}
165
166
167void tns_init(tns_t *tns, int cnt) {
168 int len;
169 char *homedir;
170
171 if (!tns)
172 return;
173
174 if (cnt) {
175 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
176 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
177 } else {
178 tns->thumbs = NULL;
179 }
180
181 tns->cnt = tns->first = tns->sel = 0;
182 tns->cap = cnt;
183 tns->alpha = 1;
184 tns->dirty = 0;
185
186 if ((homedir = getenv("HOME"))) {
187 if (cache_dir)
188 free(cache_dir);
189 len = strlen(homedir) + 10;
190 cache_dir = (char*) s_malloc(len * sizeof(char));
191 snprintf(cache_dir, len, "%s/.sxiv", homedir);
192 } else {
193 warn("could not locate thumbnail cache directory");
194 }
195}
196
197void tns_free(tns_t *tns) {
198 int i;
199
200 if (!tns)
201 return;
202
203 if (tns->thumbs) {
204 for (i = 0; i < tns->cnt; i++) {
205 if (tns->thumbs[i].im) {
206 imlib_context_set_image(tns->thumbs[i].im);
207 imlib_free_image();
208 }
209 }
210 free(tns->thumbs);
211 tns->thumbs = NULL;
212 }
213
214 if (cache_dir) {
215 free(cache_dir);
216 cache_dir = NULL;
217 }
218}
219
220int tns_load(tns_t *tns, int n, const fileinfo_t *file,
221 Bool force, Bool silent)
222{
223 int w, h;
224 int use_cache, cache_hit = 0;
225 float z, zw, zh;
226 thumb_t *t;
227 Imlib_Image *im;
228 const char *fmt;
229
230 if (!tns || !tns->thumbs || !file || !file->name || !file->path)
231 return 0;
232
233 if (n < 0 || n >= tns->cap)
234 return 0;
235
236 t = &tns->thumbs[n];
237 t->file = file;
238
239 if (t->im) {
240 imlib_context_set_image(t->im);
241 imlib_free_image();
242 }
243
244 if ((use_cache = tns_cache_enabled())) {
245 if (!force && (im = tns_cache_load(file->path)))
246 cache_hit = 1;
247 }
248
249 if (!cache_hit &&
250 (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
251 {
252 if (!silent)
253 warn("could not open image: %s", file->name);
254 return 0;
255 }
256
257 imlib_context_set_image(im);
258 imlib_context_set_anti_alias(1);
259
260 fmt = imlib_image_format();
261 /* avoid unused-but-set-variable warning */
262 (void) fmt;
263
264#ifdef EXIF_SUPPORT
265 if (!cache_hit && !strcmp(fmt, "jpeg"))
266 exif_auto_orientate(file);
267#endif
268
269 w = imlib_image_get_width();
270 h = imlib_image_get_height();
271 zw = (float) THUMB_SIZE / (float) w;
272 zh = (float) THUMB_SIZE / (float) h;
273 z = MIN(zw, zh);
274 t->w = z * w;
275 t->h = z * h;
276
277 if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
278 die("could not allocate memory");
279
280 imlib_free_image_and_decache();
281
282 if (use_cache && !cache_hit)
283 tns_cache_write(t, False);
284
285 tns->dirty = 1;
286 return 1;
287}
288
289void tns_check_view(tns_t *tns, Bool scrolled) {
290 int r;
291
292 if (!tns)
293 return;
294
295 tns->first -= tns->first % tns->cols;
296 r = tns->sel % tns->cols;
297
298 if (scrolled) {
299 /* move selection into visible area */
300 if (tns->sel >= tns->first + tns->cols * tns->rows)
301 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
302 else if (tns->sel < tns->first)
303 tns->sel = tns->first + r;
304 } else {
305 /* scroll to selection */
306 if (tns->first + tns->cols * tns->rows <= tns->sel) {
307 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
308 tns->dirty = 1;
309 } else if (tns->first > tns->sel) {
310 tns->first = tns->sel - r;
311 tns->dirty = 1;
312 }
313 }
314}
315
316void tns_render(tns_t *tns, win_t *win) {
317 int i, cnt, r, x, y;
318 thumb_t *t;
319
320 if (!tns || !tns->thumbs || !win)
321 return;
322
323 if (!tns->dirty)
324 return;
325
326 win_clear(win);
327 imlib_context_set_drawable(win->pm);
328
329 tns->cols = MAX(1, win->w / thumb_dim);
330 tns->rows = MAX(1, win->h / thumb_dim);
331
332 if (tns->cnt < tns->cols * tns->rows) {
333 tns->first = 0;
334 cnt = tns->cnt;
335 } else {
336 tns_check_view(tns, False);
337 cnt = tns->cols * tns->rows;
338 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
339 tns->first -= r - r % tns->cols;
340 if (r > 0)
341 cnt -= r % tns->cols;
342 }
343
344 r = cnt % tns->cols ? 1 : 0;
345 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
346 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
347
348 for (i = 0; i < cnt; i++) {
349 t = &tns->thumbs[tns->first + i];
350 t->x = x + (THUMB_SIZE - t->w) / 2;
351 t->y = y + (THUMB_SIZE - t->h) / 2;
352 imlib_context_set_image(t->im);
353
354 if (imlib_image_has_alpha() && !tns->alpha)
355 win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, True, 0, win->white);
356
357 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
358 t->x, t->y, t->w, t->h);
359 if ((i + 1) % tns->cols == 0) {
360 x = tns->x;
361 y += thumb_dim;
362 } else {
363 x += thumb_dim;
364 }
365 }
366
367 tns->dirty = 0;
368 tns_highlight(tns, win, tns->sel, True);
369}
370
371void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
372 thumb_t *t;
373 int x, y;
374 unsigned long col;
375
376 if (!tns || !tns->thumbs || !win)
377 return;
378
379 if (n >= 0 && n < tns->cnt) {
380 t = &tns->thumbs[n];
381
382 if (hl)
383 col = win->selcol;
384 else if (win->fullscreen)
385 col = win->black;
386 else
387 col = win->bgcol;
388
389 x = t->x - (THUMB_SIZE - t->w) / 2;
390 y = t->y - (THUMB_SIZE - t->h) / 2;
391 win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
392 False, 2, col);
393 }
394
395 win_draw(win);
396}
397
398int tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
399 int old;
400
401 if (!tns || !tns->thumbs || !win)
402 return 0;
403
404 old = tns->sel;
405
406 switch (dir) {
407 case DIR_LEFT:
408 if (tns->sel > 0)
409 tns->sel--;
410 break;
411 case DIR_RIGHT:
412 if (tns->sel < tns->cnt - 1)
413 tns->sel++;
414 break;
415 case DIR_UP:
416 if (tns->sel >= tns->cols)
417 tns->sel -= tns->cols;
418 break;
419 case DIR_DOWN:
420 if (tns->sel + tns->cols < tns->cnt)
421 tns->sel += tns->cols;
422 break;
423 }
424
425 if (tns->sel != old) {
426 tns_highlight(tns, win, old, False);
427 tns_check_view(tns, False);
428 if (!tns->dirty)
429 tns_highlight(tns, win, tns->sel, True);
430 }
431
432 return tns->sel != old;
433}
434
435int tns_scroll(tns_t *tns, direction_t dir) {
436 int old;
437
438 if (!tns)
439 return 0;
440
441 old = tns->first;
442
443 if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
444 tns->first += tns->cols;
445 tns_check_view(tns, True);
446 tns->dirty = 1;
447 } else if (dir == DIR_UP && tns->first >= tns->cols) {
448 tns->first -= tns->cols;
449 tns_check_view(tns, True);
450 tns->dirty = 1;
451 }
452
453 return tns->first != old;
454}
455
456int tns_translate(tns_t *tns, int x, int y) {
457 int n;
458
459 if (!tns || !tns->thumbs)
460 return -1;
461
462 if (x < tns->x || y < tns->y)
463 return -1;
464
465 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
466 (x - tns->x) / thumb_dim;
467 if (n >= tns->cnt)
468 n = -1;
469
470 return n;
471}