Krolyxon
Commit b845827

Branches

Refs →

nsxivthumbs.c

465 lines · 10.2 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 _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 "thumbs.h"
30#include "util.h"
31#include "config.h"
32
33const int thumb_dim = THUMB_SIZE + 10;
34char *cache_dir = NULL;
35
36bool tns_cache_enabled(void) {
37 struct stat stats;
38
39 return cache_dir != NULL && stat(cache_dir, &stats) == 0 &&
40 S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0;
41}
42
43char* tns_cache_filepath(const char *filepath) {
44 size_t len;
45 char *cfile = NULL;
46
47 if (cache_dir == NULL || filepath == NULL || *filepath != '/')
48 return NULL;
49
50 if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
51 /* don't cache images inside the cache directory! */
52 len = strlen(cache_dir) + strlen(filepath) + 6;
53 cfile = (char*) s_malloc(len);
54 snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
55 }
56 return cfile;
57}
58
59Imlib_Image* tns_cache_load(const char *filepath) {
60 char *cfile;
61 struct stat cstats, fstats;
62 Imlib_Image *im = NULL;
63
64 if (filepath == NULL)
65 return NULL;
66 if (stat(filepath, &fstats) < 0)
67 return NULL;
68
69 if ((cfile = tns_cache_filepath(filepath)) != NULL) {
70 if (stat(cfile, &cstats) == 0 && cstats.st_mtime == fstats.st_mtime)
71 im = imlib_load_image(cfile);
72 free(cfile);
73 }
74 return im;
75}
76
77void tns_cache_write(thumb_t *t, bool force) {
78 char *cfile, *dirend;
79 struct stat cstats, fstats;
80 struct utimbuf times;
81 Imlib_Load_Error err = 0;
82
83 if (t == NULL || t->im == NULL)
84 return;
85 if (t->file == NULL || t->file->name == NULL || t->file->path == NULL)
86 return;
87 if (stat(t->file->path, &fstats) < 0)
88 return;
89
90 if ((cfile = tns_cache_filepath(t->file->path)) != NULL) {
91 if (force || stat(cfile, &cstats) < 0 ||
92 cstats.st_mtime != fstats.st_mtime)
93 {
94 if ((dirend = strrchr(cfile, '/')) != NULL) {
95 *dirend = '\0';
96 err = r_mkdir(cfile);
97 *dirend = '/';
98 }
99
100 if (err == 0) {
101 imlib_context_set_image(t->im);
102 imlib_image_set_format("png");
103 imlib_save_image_with_error_return(cfile, &err);
104 }
105
106 if (err == 0) {
107 times.actime = fstats.st_atime;
108 times.modtime = fstats.st_mtime;
109 utime(cfile, &times);
110 } else {
111 warn("could not cache thumbnail: %s", t->file->name);
112 }
113 }
114 free(cfile);
115 }
116}
117
118void tns_clean_cache(tns_t *tns) {
119 int dirlen;
120 bool delete;
121 char *cfile, *filename, *tpos;
122 r_dir_t dir;
123
124 if (cache_dir == NULL)
125 return;
126
127 if (r_opendir(&dir, cache_dir) < 0) {
128 warn("could not open thumbnail cache directory: %s", cache_dir);
129 return;
130 }
131
132 dirlen = strlen(cache_dir);
133
134 while ((cfile = r_readdir(&dir)) != NULL) {
135 filename = cfile + dirlen;
136 delete = false;
137
138 if ((tpos = strrchr(filename, '.')) != NULL) {
139 *tpos = '\0';
140 if (access(filename, F_OK) < 0)
141 delete = true;
142 *tpos = '.';
143 }
144 if (delete) {
145 if (unlink(cfile) < 0)
146 warn("could not delete cache file: %s", cfile);
147 }
148 free(cfile);
149 }
150 r_closedir(&dir);
151}
152
153
154void tns_init(tns_t *tns, int cnt, win_t *win) {
155 int len;
156 char *homedir;
157
158 if (tns == NULL)
159 return;
160
161 if (cnt > 0) {
162 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
163 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
164 } else {
165 tns->thumbs = NULL;
166 }
167
168 tns->cap = cnt;
169 tns->cnt = tns->first = tns->sel = 0;
170 tns->win = win;
171 tns->alpha = true;
172 tns->dirty = false;
173
174 if ((homedir = getenv("HOME")) != NULL) {
175 if (cache_dir != NULL)
176 free(cache_dir);
177 len = strlen(homedir) + 10;
178 cache_dir = (char*) s_malloc(len * sizeof(char));
179 snprintf(cache_dir, len, "%s/.sxiv", homedir);
180 } else {
181 warn("could not locate thumbnail cache directory");
182 }
183}
184
185void tns_free(tns_t *tns) {
186 int i;
187
188 if (tns != NULL)
189 return;
190
191 if (tns->thumbs != NULL) {
192 for (i = 0; i < tns->cnt; i++) {
193 if (tns->thumbs[i].im != NULL) {
194 imlib_context_set_image(tns->thumbs[i].im);
195 imlib_free_image();
196 }
197 }
198 free(tns->thumbs);
199 tns->thumbs = NULL;
200 }
201
202 if (cache_dir != NULL) {
203 free(cache_dir);
204 cache_dir = NULL;
205 }
206}
207
208bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
209 bool force, bool silent)
210{
211 int w, h;
212 bool use_cache, cache_hit = false;
213 float z, zw, zh;
214 thumb_t *t;
215 Imlib_Image *im;
216 const char *fmt;
217
218 if (tns == NULL || tns->thumbs == NULL)
219 return false;
220 if (file == NULL || file->name == NULL || file->path == NULL)
221 return false;
222 if (n < 0 || n >= tns->cap)
223 return false;
224
225 t = &tns->thumbs[n];
226 t->file = file;
227
228 if (t->im != NULL) {
229 imlib_context_set_image(t->im);
230 imlib_free_image();
231 }
232
233 if ((use_cache = tns_cache_enabled())) {
234 if (!force && (im = tns_cache_load(file->path)) != NULL)
235 cache_hit = true;
236 }
237
238 if (!cache_hit) {
239 if (access(file->path, R_OK) < 0 ||
240 (im = imlib_load_image(file->path)) == NULL)
241 {
242 if (!silent)
243 warn("could not open image: %s", file->name);
244 return false;
245 }
246 }
247
248 imlib_context_set_image(im);
249 imlib_context_set_anti_alias(1);
250
251 if ((fmt = imlib_image_format()) == NULL) {
252 if (!silent)
253 warn("could not open image: %s", file->name);
254 imlib_free_image_and_decache();
255 return false;
256 }
257
258 w = imlib_image_get_width();
259 h = imlib_image_get_height();
260 zw = (float) THUMB_SIZE / (float) w;
261 zh = (float) THUMB_SIZE / (float) h;
262 z = MIN(zw, zh);
263 z = MIN(z, 1.0);
264 t->w = z * w;
265 t->h = z * h;
266
267 t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
268 if (t->im == NULL)
269 die("could not allocate memory");
270
271 imlib_free_image_and_decache();
272
273 if (use_cache && !cache_hit)
274 tns_cache_write(t, false);
275
276 tns->dirty = true;
277 return true;
278}
279
280void tns_check_view(tns_t *tns, bool scrolled) {
281 int r;
282
283 if (tns == NULL)
284 return;
285
286 tns->first -= tns->first % tns->cols;
287 r = tns->sel % tns->cols;
288
289 if (scrolled) {
290 /* move selection into visible area */
291 if (tns->sel >= tns->first + tns->cols * tns->rows)
292 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
293 else if (tns->sel < tns->first)
294 tns->sel = tns->first + r;
295 } else {
296 /* scroll to selection */
297 if (tns->first + tns->cols * tns->rows <= tns->sel) {
298 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
299 tns->dirty = true;
300 } else if (tns->first > tns->sel) {
301 tns->first = tns->sel - r;
302 tns->dirty = true;
303 }
304 }
305}
306
307void tns_render(tns_t *tns) {
308 thumb_t *t;
309 win_t *win;
310 int i, cnt, r, x, y;
311
312 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
313 return;
314 if (!tns->dirty)
315 return;
316
317 win = tns->win;
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 (!tns->alpha && imlib_image_has_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 tns->dirty = false;
359 tns_highlight(tns, tns->sel, true);
360}
361
362void tns_highlight(tns_t *tns, int n, bool hl) {
363 thumb_t *t;
364 win_t *win;
365 int x, y;
366 unsigned long col;
367
368 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
369 return;
370
371 win = tns->win;
372
373 if (n >= 0 && n < tns->cnt) {
374 t = &tns->thumbs[n];
375
376 if (hl)
377 col = win->selcol;
378 else if (win->fullscreen)
379 col = win->black;
380 else
381 col = win->bgcol;
382
383 x = t->x - (THUMB_SIZE - t->w) / 2;
384 y = t->y - (THUMB_SIZE - t->h) / 2;
385 win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
386 false, 2, col);
387 }
388}
389
390bool tns_move_selection(tns_t *tns, direction_t dir) {
391 int old;
392
393 if (tns == NULL || tns->thumbs == NULL)
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, old, false);
419 tns_check_view(tns, false);
420 if (!tns->dirty)
421 tns_highlight(tns, tns->sel, true);
422 }
423 return tns->sel != old;
424}
425
426bool tns_scroll(tns_t *tns, direction_t dir, bool screen) {
427 int d, max, old;
428
429 if (tns == NULL)
430 return false;
431
432 old = tns->first;
433 d = tns->cols * (screen ? tns->rows : 1);
434
435 if (dir == DIR_DOWN) {
436 max = tns->cnt - tns->cols * tns->rows;
437 if (tns->cnt % tns->cols != 0)
438 max += tns->cols - tns->cnt % tns->cols;
439 tns->first = MIN(tns->first + d, max);
440 } else if (dir == DIR_UP) {
441 tns->first = MAX(tns->first - d, 0);
442 }
443
444 if (tns->first != old) {
445 tns_check_view(tns, true);
446 tns->dirty = true;
447 }
448 return tns->first != old;
449}
450
451int tns_translate(tns_t *tns, int x, int y) {
452 int n;
453
454 if (tns == NULL || tns->thumbs == NULL)
455 return -1;
456 if (x < tns->x || y < tns->y)
457 return -1;
458
459 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
460 (x - tns->x) / thumb_dim;
461 if (n >= tns->cnt)
462 n = -1;
463
464 return n;
465}