Krolyxon
Commit 8f99adb

Branches

Refs →

nsxivthumbs.c

462 lines · 9.6 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 _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
33#ifdef EXIF_SUPPORT
34void exif_auto_orientate(const fileinfo_t*);
35#endif
36
37const int thumb_dim = THUMB_SIZE + 10;
38char *cache_dir = NULL;
39
40bool tns_cache_enabled() {
41 struct stat stats;
42
43 return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) &&
44 !access(cache_dir, W_OK);
45}
46
47char* tns_cache_filepath(const char *filepath) {
48 size_t len;
49 char *cfile = NULL;
50
51 if (!cache_dir || !filepath || *filepath != '/')
52 return NULL;
53
54 if (strncmp(filepath, cache_dir, strlen(cache_dir))) {
55 len = strlen(cache_dir) + strlen(filepath) + 6;
56 cfile = (char*) s_malloc(len);
57 snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
58 }
59
60 return cfile;
61}
62
63Imlib_Image* tns_cache_load(const char *filepath) {
64 char *cfile;
65 struct stat cstats, fstats;
66 Imlib_Image *im = NULL;
67
68 if (!filepath)
69 return NULL;
70
71 if (stat(filepath, &fstats))
72 return NULL;
73
74 if ((cfile = tns_cache_filepath(filepath))) {
75 if (!stat(cfile, &cstats) && cstats.st_mtime == fstats.st_mtime)
76 im = imlib_load_image(cfile);
77 free(cfile);
78 }
79
80 return im;
81}
82
83void tns_cache_write(thumb_t *t, bool force) {
84 char *cfile, *dirend;
85 struct stat cstats, fstats;
86 struct utimbuf times;
87 Imlib_Load_Error err = 0;
88
89 if (!t || !t->im || !t->file || !t->file->name || !t->file->path)
90 return;
91
92 if (stat(t->file->path, &fstats))
93 return;
94
95 if ((cfile = tns_cache_filepath(t->file->path))) {
96 if (force || stat(cfile, &cstats) || cstats.st_mtime != fstats.st_mtime) {
97 if ((dirend = strrchr(cfile, '/'))) {
98 *dirend = '\0';
99 err = r_mkdir(cfile);
100 *dirend = '/';
101 }
102
103 if (!err) {
104 imlib_context_set_image(t->im);
105 imlib_image_set_format("png");
106 imlib_save_image_with_error_return(cfile, &err);
107 }
108
109 if (err) {
110 warn("could not cache thumbnail: %s", t->file->name);
111 } else {
112 times.actime = fstats.st_atime;
113 times.modtime = fstats.st_mtime;
114 utime(cfile, &times);
115 }
116 }
117 free(cfile);
118 }
119}
120
121void tns_clean_cache(tns_t *tns) {
122 int dirlen;
123 bool delete;
124 char *cfile, *filename, *tpos;
125 r_dir_t dir;
126
127 if (!cache_dir)
128 return;
129
130 if (r_opendir(&dir, cache_dir)) {
131 warn("could not open thumbnail cache directory: %s", cache_dir);
132 return;
133 }
134
135 dirlen = strlen(cache_dir);
136
137 while ((cfile = r_readdir(&dir))) {
138 filename = cfile + dirlen;
139 delete = false;
140
141 if ((tpos = strrchr(filename, '.'))) {
142 *tpos = '\0';
143 if (access(filename, F_OK))
144 delete = true;
145 *tpos = '.';
146 }
147
148 if (delete && unlink(cfile))
149 warn("could not delete cache file: %s", cfile);
150
151 free(cfile);
152 }
153
154 r_closedir(&dir);
155}
156
157
158void tns_init(tns_t *tns, int cnt) {
159 int len;
160 char *homedir;
161
162 if (!tns)
163 return;
164
165 if (cnt) {
166 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
167 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
168 } else {
169 tns->thumbs = NULL;
170 }
171
172 tns->cnt = tns->first = tns->sel = 0;
173 tns->cap = cnt;
174 tns->alpha = true;
175 tns->dirty = false;
176
177 if ((homedir = getenv("HOME"))) {
178 if (cache_dir)
179 free(cache_dir);
180 len = strlen(homedir) + 10;
181 cache_dir = (char*) s_malloc(len * sizeof(char));
182 snprintf(cache_dir, len, "%s/.sxiv", homedir);
183 } else {
184 warn("could not locate thumbnail cache directory");
185 }
186}
187
188void tns_free(tns_t *tns) {
189 int i;
190
191 if (!tns)
192 return;
193
194 if (tns->thumbs) {
195 for (i = 0; i < tns->cnt; i++) {
196 if (tns->thumbs[i].im) {
197 imlib_context_set_image(tns->thumbs[i].im);
198 imlib_free_image();
199 }
200 }
201 free(tns->thumbs);
202 tns->thumbs = NULL;
203 }
204
205 if (cache_dir) {
206 free(cache_dir);
207 cache_dir = NULL;
208 }
209}
210
211bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
212 bool force, bool silent)
213{
214 int w, h;
215 bool use_cache, cache_hit = false;
216 float z, zw, zh;
217 thumb_t *t;
218 Imlib_Image *im;
219 const char *fmt;
220
221 if (!tns || !tns->thumbs || !file || !file->name || !file->path)
222 return false;
223
224 if (n < 0 || n >= tns->cap)
225 return false;
226
227 t = &tns->thumbs[n];
228 t->file = file;
229
230 if (t->im) {
231 imlib_context_set_image(t->im);
232 imlib_free_image();
233 }
234
235 if ((use_cache = tns_cache_enabled())) {
236 if (!force && (im = tns_cache_load(file->path)))
237 cache_hit = true;
238 }
239
240 if (!cache_hit &&
241 (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
242 {
243 if (!silent)
244 warn("could not open image: %s", file->name);
245 return false;
246 }
247
248 imlib_context_set_image(im);
249 imlib_context_set_anti_alias(1);
250
251 fmt = imlib_image_format();
252 /* avoid unused-but-set-variable warning */
253 (void) fmt;
254
255#ifdef EXIF_SUPPORT
256 if (!cache_hit && !strcmp(fmt, "jpeg"))
257 exif_auto_orientate(file);
258#endif
259
260 w = imlib_image_get_width();
261 h = imlib_image_get_height();
262 zw = (float) THUMB_SIZE / (float) w;
263 zh = (float) THUMB_SIZE / (float) h;
264 z = MIN(zw, zh);
265 t->w = z * w;
266 t->h = z * h;
267
268 if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
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)
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, win_t *win) {
308 int i, cnt, r, x, y;
309 thumb_t *t;
310
311 if (!tns || !tns->thumbs || !win)
312 return;
313
314 if (!tns->dirty)
315 return;
316
317 win_clear(win);
318 imlib_context_set_drawable(win->pm);
319
320 tns->cols = MAX(1, win->w / thumb_dim);
321 tns->rows = MAX(1, win->h / thumb_dim);
322
323 if (tns->cnt < tns->cols * tns->rows) {
324 tns->first = 0;
325 cnt = tns->cnt;
326 } else {
327 tns_check_view(tns, false);
328 cnt = tns->cols * tns->rows;
329 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
330 tns->first -= r - r % tns->cols;
331 if (r > 0)
332 cnt -= r % tns->cols;
333 }
334
335 r = cnt % tns->cols ? 1 : 0;
336 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
337 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
338
339 for (i = 0; i < cnt; i++) {
340 t = &tns->thumbs[tns->first + i];
341 t->x = x + (THUMB_SIZE - t->w) / 2;
342 t->y = y + (THUMB_SIZE - t->h) / 2;
343 imlib_context_set_image(t->im);
344
345 if (imlib_image_has_alpha() && !tns->alpha)
346 win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
347
348 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
349 t->x, t->y, t->w, t->h);
350 if ((i + 1) % tns->cols == 0) {
351 x = tns->x;
352 y += thumb_dim;
353 } else {
354 x += thumb_dim;
355 }
356 }
357
358 tns->dirty = false;
359 tns_highlight(tns, win, tns->sel, true);
360}
361
362void tns_highlight(tns_t *tns, win_t *win, int n, bool hl) {
363 thumb_t *t;
364 int x, y;
365 unsigned long col;
366
367 if (!tns || !tns->thumbs || !win)
368 return;
369
370 if (n >= 0 && n < tns->cnt) {
371 t = &tns->thumbs[n];
372
373 if (hl)
374 col = win->selcol;
375 else if (win->fullscreen)
376 col = win->black;
377 else
378 col = win->bgcol;
379
380 x = t->x - (THUMB_SIZE - t->w) / 2;
381 y = t->y - (THUMB_SIZE - t->h) / 2;
382 win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
383 false, 2, col);
384 }
385
386 win_draw(win);
387}
388
389bool tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
390 int old;
391
392 if (!tns || !tns->thumbs || !win)
393 return false;
394
395 old = tns->sel;
396
397 switch (dir) {
398 case DIR_LEFT:
399 if (tns->sel > 0)
400 tns->sel--;
401 break;
402 case DIR_RIGHT:
403 if (tns->sel < tns->cnt - 1)
404 tns->sel++;
405 break;
406 case DIR_UP:
407 if (tns->sel >= tns->cols)
408 tns->sel -= tns->cols;
409 break;
410 case DIR_DOWN:
411 if (tns->sel + tns->cols < tns->cnt)
412 tns->sel += tns->cols;
413 break;
414 }
415
416 if (tns->sel != old) {
417 tns_highlight(tns, win, old, false);
418 tns_check_view(tns, false);
419 if (!tns->dirty)
420 tns_highlight(tns, win, tns->sel, true);
421 }
422
423 return tns->sel != old;
424}
425
426bool tns_scroll(tns_t *tns, direction_t dir) {
427 int old;
428
429 if (!tns)
430 return false;
431
432 old = tns->first;
433
434 if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
435 tns->first += tns->cols;
436 tns_check_view(tns, true);
437 tns->dirty = true;
438 } else if (dir == DIR_UP && tns->first >= tns->cols) {
439 tns->first -= tns->cols;
440 tns_check_view(tns, true);
441 tns->dirty = true;
442 }
443
444 return tns->first != old;
445}
446
447int tns_translate(tns_t *tns, int x, int y) {
448 int n;
449
450 if (!tns || !tns->thumbs)
451 return -1;
452
453 if (x < tns->x || y < tns->y)
454 return -1;
455
456 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
457 (x - tns->x) / thumb_dim;
458 if (n >= tns->cnt)
459 n = -1;
460
461 return n;
462}