Krolyxon
Commit b6a6c26

Branches

Refs →

nsxivthumbs.c

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