Krolyxon
Commit bfab4dc

Branches

Refs →

nsxivthumbs.c

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