Krolyxon
Commit ac4eb53

Branches

Refs →

nsxivthumbs.c

475 lines · 10.4 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 if ((fmt = imlib_image_format()) == NULL) {
257 if (!silent)
258 warn("could not open image: %s", file->name);
259 imlib_free_image_and_decache();
260 return false;
261 }
262#if EXIF_SUPPORT
263 if (!cache_hit && STREQ(fmt, "jpeg"))
264 exif_auto_orientate(file);
265#endif
266
267 w = imlib_image_get_width();
268 h = imlib_image_get_height();
269 zw = (float) THUMB_SIZE / (float) w;
270 zh = (float) THUMB_SIZE / (float) h;
271 z = MIN(zw, zh);
272 z = MIN(z, 1.0);
273 t->w = z * w;
274 t->h = z * h;
275
276 t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
277 if (t->im == NULL)
278 die("could not allocate memory");
279
280 imlib_free_image_and_decache();
281
282 if (use_cache && !cache_hit)
283 tns_cache_write(t, false);
284
285 tns->dirty = true;
286 return true;
287}
288
289void tns_check_view(tns_t *tns, bool scrolled) {
290 int r;
291
292 if (tns == NULL)
293 return;
294
295 tns->first -= tns->first % tns->cols;
296 r = tns->sel % tns->cols;
297
298 if (scrolled) {
299 /* move selection into visible area */
300 if (tns->sel >= tns->first + tns->cols * tns->rows)
301 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
302 else if (tns->sel < tns->first)
303 tns->sel = tns->first + r;
304 } else {
305 /* scroll to selection */
306 if (tns->first + tns->cols * tns->rows <= tns->sel) {
307 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
308 tns->dirty = true;
309 } else if (tns->first > tns->sel) {
310 tns->first = tns->sel - r;
311 tns->dirty = true;
312 }
313 }
314}
315
316void tns_render(tns_t *tns) {
317 thumb_t *t;
318 win_t *win;
319 int i, cnt, r, x, y;
320
321 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
322 return;
323 if (!tns->dirty)
324 return;
325
326 win = tns->win;
327 win_clear(win);
328 imlib_context_set_drawable(win->pm);
329
330 tns->cols = MAX(1, win->w / thumb_dim);
331 tns->rows = MAX(1, win->h / thumb_dim);
332
333 if (tns->cnt < tns->cols * tns->rows) {
334 tns->first = 0;
335 cnt = tns->cnt;
336 } else {
337 tns_check_view(tns, false);
338 cnt = tns->cols * tns->rows;
339 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
340 tns->first -= r - r % tns->cols;
341 if (r > 0)
342 cnt -= r % tns->cols;
343 }
344
345 r = cnt % tns->cols ? 1 : 0;
346 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
347 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
348
349 for (i = 0; i < cnt; i++) {
350 t = &tns->thumbs[tns->first + i];
351 t->x = x + (THUMB_SIZE - t->w) / 2;
352 t->y = y + (THUMB_SIZE - t->h) / 2;
353 imlib_context_set_image(t->im);
354
355 if (!tns->alpha && imlib_image_has_alpha())
356 win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
357
358 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
359 t->x, t->y, t->w, t->h);
360 if ((i + 1) % tns->cols == 0) {
361 x = tns->x;
362 y += thumb_dim;
363 } else {
364 x += thumb_dim;
365 }
366 }
367 tns->dirty = false;
368 tns_highlight(tns, tns->sel, true);
369}
370
371void tns_highlight(tns_t *tns, int n, bool hl) {
372 thumb_t *t;
373 win_t *win;
374 int x, y;
375 unsigned long col;
376
377 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
378 return;
379
380 win = tns->win;
381
382 if (n >= 0 && n < tns->cnt) {
383 t = &tns->thumbs[n];
384
385 if (hl)
386 col = win->selcol;
387 else if (win->fullscreen)
388 col = win->black;
389 else
390 col = win->bgcol;
391
392 x = t->x - (THUMB_SIZE - t->w) / 2;
393 y = t->y - (THUMB_SIZE - t->h) / 2;
394 win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
395 false, 2, col);
396 }
397 win_draw(win);
398}
399
400bool tns_move_selection(tns_t *tns, direction_t dir) {
401 int old;
402
403 if (tns == NULL || tns->thumbs == NULL)
404 return false;
405
406 old = tns->sel;
407
408 switch (dir) {
409 case DIR_LEFT:
410 if (tns->sel > 0)
411 tns->sel--;
412 break;
413 case DIR_RIGHT:
414 if (tns->sel < tns->cnt - 1)
415 tns->sel++;
416 break;
417 case DIR_UP:
418 if (tns->sel >= tns->cols)
419 tns->sel -= tns->cols;
420 break;
421 case DIR_DOWN:
422 if (tns->sel + tns->cols < tns->cnt)
423 tns->sel += tns->cols;
424 break;
425 }
426
427 if (tns->sel != old) {
428 tns_highlight(tns, old, false);
429 tns_check_view(tns, false);
430 if (!tns->dirty)
431 tns_highlight(tns, tns->sel, true);
432 }
433 return tns->sel != old;
434}
435
436bool tns_scroll(tns_t *tns, direction_t dir, bool screen) {
437 int d, max, old;
438
439 if (tns == NULL)
440 return false;
441
442 old = tns->first;
443 d = tns->cols * (screen ? tns->rows : 1);
444
445 if (dir == DIR_DOWN) {
446 max = tns->cnt - tns->cols * tns->rows;
447 if (tns->cnt % tns->cols != 0)
448 max += tns->cols - tns->cnt % tns->cols;
449 tns->first = MIN(tns->first + d, max);
450 } else if (dir == DIR_UP) {
451 tns->first = MAX(tns->first - d, 0);
452 }
453
454 if (tns->first != old) {
455 tns_check_view(tns, true);
456 tns->dirty = true;
457 }
458 return tns->first != old;
459}
460
461int tns_translate(tns_t *tns, int x, int y) {
462 int n;
463
464 if (tns == NULL || tns->thumbs == NULL)
465 return -1;
466 if (x < tns->x || y < tns->y)
467 return -1;
468
469 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
470 (x - tns->x) / thumb_dim;
471 if (n >= tns->cnt)
472 n = -1;
473
474 return n;
475}