Krolyxon
Commit d721d84

Branches

Refs →

nsxivthumbs.c

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