Krolyxon
Commit 0155223

Branches

Refs →

nsxivthumbs.c

467 lines · 10.4 KB · C

1/* sxiv: thumbs.c
2 * Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com>
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 _THUMBS_CONFIG
21
22#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27#include <utime.h>
28
29#include "exif.h"
30#include "thumbs.h"
31#include "util.h"
32#include "config.h"
33
34const int thumb_dim = THUMB_SIZE + 10;
35char *cache_dir = NULL;
36
37bool tns_cache_enabled(void) {
38 struct stat stats;
39
40 return cache_dir != NULL && stat(cache_dir, &stats) == 0 &&
41 S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0;
42}
43
44char* tns_cache_filepath(const char *filepath) {
45 size_t len;
46 char *cfile = NULL;
47
48 if (cache_dir == NULL || filepath == NULL || *filepath != '/')
49 return NULL;
50
51 if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
52 /* don't cache images inside the cache directory! */
53 len = strlen(cache_dir) + strlen(filepath) + 6;
54 cfile = (char*) s_malloc(len);
55 snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
56 }
57 return cfile;
58}
59
60Imlib_Image* tns_cache_load(const char *filepath) {
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 char *cfile, *dirend;
80 struct stat cstats, fstats;
81 struct utimbuf times;
82 Imlib_Load_Error err = 0;
83
84 if (t == NULL || t->im == NULL)
85 return;
86 if (t->file == NULL || t->file->name == NULL || t->file->path == NULL)
87 return;
88 if (stat(t->file->path, &fstats) < 0)
89 return;
90
91 if ((cfile = tns_cache_filepath(t->file->path)) != NULL) {
92 if (force || stat(cfile, &cstats) < 0 ||
93 cstats.st_mtime != fstats.st_mtime)
94 {
95 if ((dirend = strrchr(cfile, '/')) != NULL) {
96 *dirend = '\0';
97 err = r_mkdir(cfile);
98 *dirend = '/';
99 }
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
107 if (err == 0) {
108 times.actime = fstats.st_atime;
109 times.modtime = fstats.st_mtime;
110 utime(cfile, &times);
111 } else {
112 warn("could not cache thumbnail: %s", t->file->name);
113 }
114 }
115 free(cfile);
116 }
117}
118
119void tns_clean_cache(tns_t *tns) {
120 int dirlen;
121 bool delete;
122 char *cfile, *filename, *tpos;
123 r_dir_t dir;
124
125 if (cache_dir == NULL)
126 return;
127
128 if (r_opendir(&dir, cache_dir) < 0) {
129 warn("could not open thumbnail cache directory: %s", cache_dir);
130 return;
131 }
132
133 dirlen = strlen(cache_dir);
134
135 while ((cfile = r_readdir(&dir)) != NULL) {
136 filename = cfile + dirlen;
137 delete = false;
138
139 if ((tpos = strrchr(filename, '.')) != NULL) {
140 *tpos = '\0';
141 if (access(filename, F_OK) < 0)
142 delete = true;
143 *tpos = '.';
144 }
145 if (delete) {
146 if (unlink(cfile) < 0)
147 warn("could not delete cache file: %s", cfile);
148 }
149 free(cfile);
150 }
151 r_closedir(&dir);
152}
153
154
155void tns_init(tns_t *tns, int cnt, win_t *win) {
156 int len;
157 char *homedir;
158
159 if (tns == NULL)
160 return;
161
162 if (cnt > 0) {
163 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
164 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
165 } else {
166 tns->thumbs = NULL;
167 }
168
169 tns->cap = cnt;
170 tns->cnt = tns->first = tns->sel = 0;
171 tns->win = win;
172 tns->alpha = true;
173 tns->dirty = false;
174
175 if ((homedir = getenv("HOME")) != NULL) {
176 if (cache_dir != NULL)
177 free(cache_dir);
178 len = strlen(homedir) + 10;
179 cache_dir = (char*) s_malloc(len * sizeof(char));
180 snprintf(cache_dir, len, "%s/.sxiv", homedir);
181 } else {
182 warn("could not locate thumbnail cache directory");
183 }
184}
185
186void tns_free(tns_t *tns) {
187 int i;
188
189 if (tns == NULL)
190 return;
191
192 if (tns->thumbs != NULL) {
193 for (i = 0; i < tns->cnt; i++) {
194 if (tns->thumbs[i].im != NULL) {
195 imlib_context_set_image(tns->thumbs[i].im);
196 imlib_free_image();
197 }
198 }
199 free(tns->thumbs);
200 tns->thumbs = NULL;
201 }
202
203 if (cache_dir != NULL) {
204 free(cache_dir);
205 cache_dir = NULL;
206 }
207}
208
209bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
210 bool force, bool silent)
211{
212 int w, h;
213 bool use_cache, cache_hit = false;
214 float z, zw, zh;
215 thumb_t *t;
216 Imlib_Image *im;
217 const char *fmt;
218
219 if (tns == NULL || tns->thumbs == NULL)
220 return false;
221 if (file == NULL || file->name == NULL || file->path == NULL)
222 return false;
223 if (n < 0 || n >= tns->cap)
224 return false;
225
226 t = &tns->thumbs[n];
227 t->file = file;
228
229 if (t->im != NULL) {
230 imlib_context_set_image(t->im);
231 imlib_free_image();
232 }
233
234 if ((use_cache = tns_cache_enabled())) {
235 if (!force && (im = tns_cache_load(file->path)) != NULL)
236 cache_hit = true;
237 }
238
239 if (!cache_hit) {
240 if (access(file->path, R_OK) < 0 ||
241 (im = imlib_load_image(file->path)) == NULL)
242 {
243 if (!silent)
244 warn("could not open image: %s", file->name);
245 return false;
246 }
247 }
248
249 imlib_context_set_image(im);
250 imlib_context_set_anti_alias(1);
251
252 if ((fmt = imlib_image_format()) == NULL) {
253 if (!silent)
254 warn("could not open image: %s", file->name);
255 imlib_free_image_and_decache();
256 return false;
257 }
258 if (STREQ(fmt, "jpeg"))
259 exif_auto_orientate(file);
260
261 w = imlib_image_get_width();
262 h = imlib_image_get_height();
263 zw = (float) THUMB_SIZE / (float) w;
264 zh = (float) THUMB_SIZE / (float) h;
265 z = MIN(zw, zh);
266 z = MIN(z, 1.0);
267 t->w = z * w;
268 t->h = z * h;
269
270 t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
271 if (t->im == NULL)
272 die("could not allocate memory");
273
274 imlib_free_image_and_decache();
275
276 if (use_cache && !cache_hit)
277 tns_cache_write(t, true);
278
279 tns->dirty = true;
280 return true;
281}
282
283void tns_check_view(tns_t *tns, bool scrolled) {
284 int r;
285
286 if (tns == NULL)
287 return;
288
289 tns->first -= tns->first % tns->cols;
290 r = tns->sel % tns->cols;
291
292 if (scrolled) {
293 /* move selection into visible area */
294 if (tns->sel >= tns->first + tns->cols * tns->rows)
295 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
296 else if (tns->sel < tns->first)
297 tns->sel = tns->first + r;
298 } else {
299 /* scroll to selection */
300 if (tns->first + tns->cols * tns->rows <= tns->sel) {
301 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
302 tns->dirty = true;
303 } else if (tns->first > tns->sel) {
304 tns->first = tns->sel - r;
305 tns->dirty = true;
306 }
307 }
308}
309
310void tns_render(tns_t *tns) {
311 thumb_t *t;
312 win_t *win;
313 int i, cnt, r, x, y;
314
315 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
316 return;
317 if (!tns->dirty)
318 return;
319
320 win = tns->win;
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 (!tns->alpha && imlib_image_has_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 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 == NULL || tns->thumbs == NULL || tns->win == NULL)
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->fscol;
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
393bool tns_move_selection(tns_t *tns, direction_t dir, int cnt) {
394 int old, max;
395
396 if (tns == NULL || tns->thumbs == NULL)
397 return false;
398
399 old = tns->sel;
400 cnt = cnt > 1 ? cnt : 1;
401
402 switch (dir) {
403 case DIR_UP:
404 tns->sel = MAX(tns->sel - cnt * tns->cols, tns->sel % tns->cols);
405 break;
406 case DIR_DOWN:
407 max = tns->cols * ((tns->cnt - 1) / tns->cols) +
408 MIN((tns->cnt - 1) % tns->cols, tns->sel % tns->cols);
409 tns->sel = MIN(tns->sel + cnt * tns->cols, max);
410 break;
411 case DIR_LEFT:
412 tns->sel = MAX(tns->sel - cnt, 0);
413 break;
414 case DIR_RIGHT:
415 tns->sel = MIN(tns->sel + cnt, tns->cnt - 1);
416 break;
417 }
418
419 if (tns->sel != old) {
420 tns_highlight(tns, old, false);
421 tns_check_view(tns, false);
422 if (!tns->dirty)
423 tns_highlight(tns, tns->sel, true);
424 }
425 return tns->sel != old;
426}
427
428bool tns_scroll(tns_t *tns, direction_t dir, bool screen) {
429 int d, max, old;
430
431 if (tns == NULL)
432 return false;
433
434 old = tns->first;
435 d = tns->cols * (screen ? tns->rows : 1);
436
437 if (dir == DIR_DOWN) {
438 max = tns->cnt - tns->cols * tns->rows;
439 if (tns->cnt % tns->cols != 0)
440 max += tns->cols - tns->cnt % tns->cols;
441 tns->first = MIN(tns->first + d, max);
442 } else if (dir == DIR_UP) {
443 tns->first = MAX(tns->first - d, 0);
444 }
445
446 if (tns->first != old) {
447 tns_check_view(tns, true);
448 tns->dirty = true;
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 == NULL || tns->thumbs == NULL)
457 return -1;
458 if (x < tns->x || y < tns->y)
459 return -1;
460
461 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
462 (x - tns->x) / thumb_dim;
463 if (n >= tns->cnt)
464 n = -1;
465
466 return n;
467}