Krolyxon
Commit 078136d

Branches

Refs →

nsxivthumbs.c

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