Krolyxon
Commit 255dd5b

Branches

Refs →

nsxivthumbs.c

463 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) {
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->cnt = tns->first = tns->sel = 0;
174 tns->cap = cnt;
175 tns->alpha = true;
176 tns->dirty = false;
177
178 if ((homedir = getenv("HOME"))) {
179 if (cache_dir)
180 free(cache_dir);
181 len = strlen(homedir) + 10;
182 cache_dir = (char*) s_malloc(len * sizeof(char));
183 snprintf(cache_dir, len, "%s/.sxiv", homedir);
184 } else {
185 warn("could not locate thumbnail cache directory");
186 }
187}
188
189void tns_free(tns_t *tns) {
190 int i;
191
192 if (!tns)
193 return;
194
195 if (tns->thumbs) {
196 for (i = 0; i < tns->cnt; i++) {
197 if (tns->thumbs[i].im) {
198 imlib_context_set_image(tns->thumbs[i].im);
199 imlib_free_image();
200 }
201 }
202 free(tns->thumbs);
203 tns->thumbs = NULL;
204 }
205
206 if (cache_dir) {
207 free(cache_dir);
208 cache_dir = NULL;
209 }
210}
211
212bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
213 bool force, bool silent)
214{
215 int w, h;
216 bool use_cache, cache_hit = false;
217 float z, zw, zh;
218 thumb_t *t;
219 Imlib_Image *im;
220 const char *fmt;
221
222 if (!tns || !tns->thumbs || !file || !file->name || !file->path)
223 return false;
224
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) {
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)))
238 cache_hit = true;
239 }
240
241 if (!cache_hit &&
242 (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
243 {
244 if (!silent)
245 warn("could not open image: %s", file->name);
246 return false;
247 }
248
249 imlib_context_set_image(im);
250 imlib_context_set_anti_alias(1);
251
252 fmt = imlib_image_format();
253 /* avoid unused-but-set-variable warning */
254 (void) fmt;
255
256#if EXIF_SUPPORT
257 if (!cache_hit && !strcmp(fmt, "jpeg"))
258 exif_auto_orientate(file);
259#endif
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 t->w = z * w;
267 t->h = z * h;
268
269 if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
270 die("could not allocate memory");
271
272 imlib_free_image_and_decache();
273
274 if (use_cache && !cache_hit)
275 tns_cache_write(t, false);
276
277 tns->dirty = true;
278 return true;
279}
280
281void tns_check_view(tns_t *tns, bool scrolled) {
282 int r;
283
284 if (!tns)
285 return;
286
287 tns->first -= tns->first % tns->cols;
288 r = tns->sel % tns->cols;
289
290 if (scrolled) {
291 /* move selection into visible area */
292 if (tns->sel >= tns->first + tns->cols * tns->rows)
293 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
294 else if (tns->sel < tns->first)
295 tns->sel = tns->first + r;
296 } else {
297 /* scroll to selection */
298 if (tns->first + tns->cols * tns->rows <= tns->sel) {
299 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
300 tns->dirty = true;
301 } else if (tns->first > tns->sel) {
302 tns->first = tns->sel - r;
303 tns->dirty = true;
304 }
305 }
306}
307
308void tns_render(tns_t *tns, win_t *win) {
309 int i, cnt, r, x, y;
310 thumb_t *t;
311
312 if (!tns || !tns->thumbs || !win)
313 return;
314
315 if (!tns->dirty)
316 return;
317
318 win_clear(win);
319 imlib_context_set_drawable(win->pm);
320
321 tns->cols = MAX(1, win->w / thumb_dim);
322 tns->rows = MAX(1, win->h / thumb_dim);
323
324 if (tns->cnt < tns->cols * tns->rows) {
325 tns->first = 0;
326 cnt = tns->cnt;
327 } else {
328 tns_check_view(tns, false);
329 cnt = tns->cols * tns->rows;
330 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
331 tns->first -= r - r % tns->cols;
332 if (r > 0)
333 cnt -= r % tns->cols;
334 }
335
336 r = cnt % tns->cols ? 1 : 0;
337 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
338 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
339
340 for (i = 0; i < cnt; i++) {
341 t = &tns->thumbs[tns->first + i];
342 t->x = x + (THUMB_SIZE - t->w) / 2;
343 t->y = y + (THUMB_SIZE - t->h) / 2;
344 imlib_context_set_image(t->im);
345
346 if (imlib_image_has_alpha() && !tns->alpha)
347 win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
348
349 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
350 t->x, t->y, t->w, t->h);
351 if ((i + 1) % tns->cols == 0) {
352 x = tns->x;
353 y += thumb_dim;
354 } else {
355 x += thumb_dim;
356 }
357 }
358
359 tns->dirty = false;
360 tns_highlight(tns, win, tns->sel, true);
361}
362
363void tns_highlight(tns_t *tns, win_t *win, int n, bool hl) {
364 thumb_t *t;
365 int x, y;
366 unsigned long col;
367
368 if (!tns || !tns->thumbs || !win)
369 return;
370
371 if (n >= 0 && n < tns->cnt) {
372 t = &tns->thumbs[n];
373
374 if (hl)
375 col = win->selcol;
376 else if (win->fullscreen)
377 col = win->black;
378 else
379 col = win->bgcol;
380
381 x = t->x - (THUMB_SIZE - t->w) / 2;
382 y = t->y - (THUMB_SIZE - t->h) / 2;
383 win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
384 false, 2, col);
385 }
386
387 win_draw(win);
388}
389
390bool tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
391 int old;
392
393 if (!tns || !tns->thumbs || !win)
394 return false;
395
396 old = tns->sel;
397
398 switch (dir) {
399 case DIR_LEFT:
400 if (tns->sel > 0)
401 tns->sel--;
402 break;
403 case DIR_RIGHT:
404 if (tns->sel < tns->cnt - 1)
405 tns->sel++;
406 break;
407 case DIR_UP:
408 if (tns->sel >= tns->cols)
409 tns->sel -= tns->cols;
410 break;
411 case DIR_DOWN:
412 if (tns->sel + tns->cols < tns->cnt)
413 tns->sel += tns->cols;
414 break;
415 }
416
417 if (tns->sel != old) {
418 tns_highlight(tns, win, old, false);
419 tns_check_view(tns, false);
420 if (!tns->dirty)
421 tns_highlight(tns, win, tns->sel, true);
422 }
423
424 return tns->sel != old;
425}
426
427bool tns_scroll(tns_t *tns, direction_t dir) {
428 int old;
429
430 if (!tns)
431 return false;
432
433 old = tns->first;
434
435 if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
436 tns->first += tns->cols;
437 tns_check_view(tns, true);
438 tns->dirty = true;
439 } else if (dir == DIR_UP && tns->first >= tns->cols) {
440 tns->first -= tns->cols;
441 tns_check_view(tns, true);
442 tns->dirty = true;
443 }
444
445 return tns->first != old;
446}
447
448int tns_translate(tns_t *tns, int x, int y) {
449 int n;
450
451 if (!tns || !tns->thumbs)
452 return -1;
453
454 if (x < tns->x || y < tns->y)
455 return -1;
456
457 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
458 (x - tns->x) / thumb_dim;
459 if (n >= tns->cnt)
460 n = -1;
461
462 return n;
463}