Krolyxon
Commit 964bf13

Branches

Refs →

nsxivthumbs.c

472 lines · 10.3 KB · C

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