Krolyxon
Commit 7e51c35

Branches

Refs →

nsxivthumbs.c

482 lines · 10.4 KB · C

1/* Copyright 2011 Bert Muennich
2 *
3 * This file is part of sxiv.
4 *
5 * sxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * sxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
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{
41 struct stat stats;
42
43 return cache_dir != NULL && stat(cache_dir, &stats) == 0 &&
44 S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0;
45}
46
47char* tns_cache_filepath(const char *filepath)
48{
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{
66 char *cfile;
67 struct stat cstats, fstats;
68 Imlib_Image *im = NULL;
69
70 if (filepath == NULL)
71 return NULL;
72 if (stat(filepath, &fstats) < 0)
73 return NULL;
74
75 if ((cfile = tns_cache_filepath(filepath)) != NULL) {
76 if (stat(cfile, &cstats) == 0 && cstats.st_mtime == fstats.st_mtime)
77 im = imlib_load_image(cfile);
78 free(cfile);
79 }
80 return im;
81}
82
83void tns_cache_write(thumb_t *t, bool force)
84{
85 char *cfile, *dirend;
86 struct stat cstats, fstats;
87 struct utimbuf times;
88 Imlib_Load_Error err = 0;
89
90 if (t == NULL || t->im == NULL)
91 return;
92 if (t->file == NULL || t->file->name == NULL || t->file->path == NULL)
93 return;
94 if (stat(t->file->path, &fstats) < 0)
95 return;
96
97 if ((cfile = tns_cache_filepath(t->file->path)) != NULL) {
98 if (force || stat(cfile, &cstats) < 0 ||
99 cstats.st_mtime != fstats.st_mtime)
100 {
101 if ((dirend = strrchr(cfile, '/')) != NULL) {
102 *dirend = '\0';
103 err = r_mkdir(cfile);
104 *dirend = '/';
105 }
106
107 if (err == 0) {
108 imlib_context_set_image(t->im);
109 imlib_image_set_format("png");
110 imlib_save_image_with_error_return(cfile, &err);
111 }
112
113 if (err == 0) {
114 times.actime = fstats.st_atime;
115 times.modtime = fstats.st_mtime;
116 utime(cfile, &times);
117 } else {
118 warn("could not cache thumbnail: %s", t->file->name);
119 }
120 }
121 free(cfile);
122 }
123}
124
125void tns_clean_cache(tns_t *tns)
126{
127 int dirlen;
128 bool delete;
129 char *cfile, *filename, *tpos;
130 r_dir_t dir;
131
132 if (cache_dir == NULL)
133 return;
134
135 if (r_opendir(&dir, cache_dir) < 0) {
136 warn("could not open thumbnail cache directory: %s", cache_dir);
137 return;
138 }
139
140 dirlen = strlen(cache_dir);
141
142 while ((cfile = r_readdir(&dir)) != NULL) {
143 filename = cfile + dirlen;
144 delete = false;
145
146 if ((tpos = strrchr(filename, '.')) != NULL) {
147 *tpos = '\0';
148 if (access(filename, F_OK) < 0)
149 delete = true;
150 *tpos = '.';
151 }
152 if (delete) {
153 if (unlink(cfile) < 0)
154 warn("could not delete cache file: %s", cfile);
155 }
156 free(cfile);
157 }
158 r_closedir(&dir);
159}
160
161
162void tns_init(tns_t *tns, int cnt, win_t *win)
163{
164 int len;
165 char *homedir;
166
167 if (tns == NULL)
168 return;
169
170 if (cnt > 0) {
171 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
172 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
173 } else {
174 tns->thumbs = NULL;
175 }
176
177 tns->cap = cnt;
178 tns->cnt = tns->first = tns->sel = 0;
179 tns->win = win;
180 tns->alpha = true;
181 tns->dirty = false;
182
183 if ((homedir = getenv("HOME")) != NULL) {
184 if (cache_dir != NULL)
185 free(cache_dir);
186 len = strlen(homedir) + strlen(CACHE_DIR) + 2;
187 cache_dir = (char*) s_malloc(len);
188 snprintf(cache_dir, len, "%s/%s", homedir, CACHE_DIR);
189 } else {
190 warn("could not locate thumbnail cache directory");
191 }
192}
193
194void tns_free(tns_t *tns)
195{
196 int i;
197
198 if (tns == NULL)
199 return;
200
201 if (tns->thumbs != NULL) {
202 for (i = 0; i < tns->cnt; i++) {
203 if (tns->thumbs[i].im != NULL) {
204 imlib_context_set_image(tns->thumbs[i].im);
205 imlib_free_image();
206 }
207 }
208 free(tns->thumbs);
209 tns->thumbs = NULL;
210 }
211
212 if (cache_dir != NULL) {
213 free(cache_dir);
214 cache_dir = NULL;
215 }
216}
217
218bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
219 bool force, bool silent)
220{
221 int w, h;
222 bool use_cache, cache_hit = false;
223 float z, zw, zh;
224 thumb_t *t;
225 Imlib_Image *im;
226 const char *fmt;
227
228 if (tns == NULL || tns->thumbs == NULL)
229 return false;
230 if (file == NULL || file->name == NULL || file->path == NULL)
231 return false;
232 if (n < 0 || n >= tns->cap)
233 return false;
234
235 t = &tns->thumbs[n];
236 t->file = file;
237
238 if (t->im != NULL) {
239 imlib_context_set_image(t->im);
240 imlib_free_image();
241 }
242
243 if ((use_cache = tns_cache_enabled())) {
244 if (!force && (im = tns_cache_load(file->path)) != NULL)
245 cache_hit = true;
246 }
247
248 if (!cache_hit) {
249 if (access(file->path, R_OK) < 0 ||
250 (im = imlib_load_image(file->path)) == NULL)
251 {
252 if (!silent)
253 warn("could not open image: %s", file->name);
254 return false;
255 }
256 }
257
258 imlib_context_set_image(im);
259 imlib_context_set_anti_alias(1);
260
261 if ((fmt = imlib_image_format()) == NULL) {
262 if (!silent)
263 warn("could not open image: %s", file->name);
264 imlib_free_image_and_decache();
265 return false;
266 }
267 if (STREQ(fmt, "jpeg"))
268 exif_auto_orientate(file);
269
270 w = imlib_image_get_width();
271 h = imlib_image_get_height();
272 zw = (float) THUMB_SIZE / (float) w;
273 zh = (float) THUMB_SIZE / (float) h;
274 z = MIN(zw, zh);
275 z = MIN(z, 1.0);
276 t->w = z * w;
277 t->h = z * h;
278
279 t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
280 if (t->im == NULL)
281 die("could not allocate memory");
282
283 imlib_free_image_and_decache();
284
285 if (use_cache && !cache_hit)
286 tns_cache_write(t, true);
287
288 tns->dirty = true;
289 return true;
290}
291
292void tns_check_view(tns_t *tns, bool scrolled)
293{
294 int r;
295
296 if (tns == NULL)
297 return;
298
299 tns->first -= tns->first % tns->cols;
300 r = tns->sel % tns->cols;
301
302 if (scrolled) {
303 /* move selection into visible area */
304 if (tns->sel >= tns->first + tns->cols * tns->rows)
305 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
306 else if (tns->sel < tns->first)
307 tns->sel = tns->first + r;
308 } else {
309 /* scroll to selection */
310 if (tns->first + tns->cols * tns->rows <= tns->sel) {
311 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
312 tns->dirty = true;
313 } else if (tns->first > tns->sel) {
314 tns->first = tns->sel - r;
315 tns->dirty = true;
316 }
317 }
318}
319
320void tns_render(tns_t *tns)
321{
322 thumb_t *t;
323 win_t *win;
324 int i, cnt, r, x, y;
325
326 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
327 return;
328 if (!tns->dirty)
329 return;
330
331 win = tns->win;
332 win_clear(win);
333 imlib_context_set_drawable(win->pm);
334
335 tns->cols = MAX(1, win->w / thumb_dim);
336 tns->rows = MAX(1, win->h / thumb_dim);
337
338 if (tns->cnt < tns->cols * tns->rows) {
339 tns->first = 0;
340 cnt = tns->cnt;
341 } else {
342 tns_check_view(tns, false);
343 cnt = tns->cols * tns->rows;
344 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
345 tns->first -= r - r % tns->cols;
346 if (r > 0)
347 cnt -= r % tns->cols;
348 }
349
350 r = cnt % tns->cols ? 1 : 0;
351 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
352 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
353
354 for (i = 0; i < cnt; i++) {
355 t = &tns->thumbs[tns->first + i];
356 t->x = x + (THUMB_SIZE - t->w) / 2;
357 t->y = y + (THUMB_SIZE - t->h) / 2;
358 imlib_context_set_image(t->im);
359
360 if (!tns->alpha && imlib_image_has_alpha())
361 win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
362
363 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
364 t->x, t->y, t->w, t->h);
365 if ((i + 1) % tns->cols == 0) {
366 x = tns->x;
367 y += thumb_dim;
368 } else {
369 x += thumb_dim;
370 }
371 }
372 tns->dirty = false;
373 tns_highlight(tns, tns->sel, true);
374}
375
376void tns_highlight(tns_t *tns, int n, bool hl)
377{
378 thumb_t *t;
379 win_t *win;
380 int x, y;
381 unsigned long col;
382
383 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
384 return;
385
386 win = tns->win;
387
388 if (n >= 0 && n < tns->cnt) {
389 t = &tns->thumbs[n];
390
391 if (hl)
392 col = win->selcol;
393 else if (win->fullscreen)
394 col = win->fscol;
395 else
396 col = win->bgcol;
397
398 x = t->x - (THUMB_SIZE - t->w) / 2;
399 y = t->y - (THUMB_SIZE - t->h) / 2;
400 win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
401 false, 2, col);
402 }
403}
404
405bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
406{
407 int old, max;
408
409 if (tns == NULL || tns->thumbs == NULL)
410 return false;
411
412 old = tns->sel;
413 cnt = cnt > 1 ? cnt : 1;
414
415 switch (dir) {
416 case DIR_UP:
417 tns->sel = MAX(tns->sel - cnt * tns->cols, tns->sel % tns->cols);
418 break;
419 case DIR_DOWN:
420 max = tns->cols * ((tns->cnt - 1) / tns->cols) +
421 MIN((tns->cnt - 1) % tns->cols, tns->sel % tns->cols);
422 tns->sel = MIN(tns->sel + cnt * tns->cols, max);
423 break;
424 case DIR_LEFT:
425 tns->sel = MAX(tns->sel - cnt, 0);
426 break;
427 case DIR_RIGHT:
428 tns->sel = MIN(tns->sel + cnt, tns->cnt - 1);
429 break;
430 }
431
432 if (tns->sel != old) {
433 tns_highlight(tns, old, false);
434 tns_check_view(tns, false);
435 if (!tns->dirty)
436 tns_highlight(tns, tns->sel, true);
437 }
438 return tns->sel != old;
439}
440
441bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
442{
443 int d, max, old;
444
445 if (tns == NULL)
446 return false;
447
448 old = tns->first;
449 d = tns->cols * (screen ? tns->rows : 1);
450
451 if (dir == DIR_DOWN) {
452 max = tns->cnt - tns->cols * tns->rows;
453 if (tns->cnt % tns->cols != 0)
454 max += tns->cols - tns->cnt % tns->cols;
455 tns->first = MIN(tns->first + d, max);
456 } else if (dir == DIR_UP) {
457 tns->first = MAX(tns->first - d, 0);
458 }
459
460 if (tns->first != old) {
461 tns_check_view(tns, true);
462 tns->dirty = true;
463 }
464 return tns->first != old;
465}
466
467int tns_translate(tns_t *tns, int x, int y)
468{
469 int n;
470
471 if (tns == NULL || tns->thumbs == NULL)
472 return -1;
473 if (x < tns->x || y < tns->y)
474 return -1;
475
476 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
477 (x - tns->x) / thumb_dim;
478 if (n >= tns->cnt)
479 n = -1;
480
481 return n;
482}