Krolyxon
Commit e999688

Branches

Refs →

nsxivthumbs.c

417 lines · 8.9 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
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stdlib.h>
20#include <string.h>
21#include <sys/time.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25
26#include "config.h"
27#include "thumbs.h"
28#include "util.h"
29
30extern Imlib_Image *im_invalid;
31
32const int thumb_dim = THUMB_SIZE + 10;
33char *cache_dir = NULL;
34
35int tns_cache_enabled();
36Imlib_Image* tns_cache_load(const char*);
37void tns_cache_write(thumb_t*, Bool);
38
39void tns_init(tns_t *tns, int cnt) {
40 int len;
41 char *homedir;
42
43 if (!tns)
44 return;
45
46 tns->cnt = tns->first = tns->sel = 0;
47 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
48 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
49 tns->cap = cnt;
50 tns->dirty = 0;
51
52 if ((homedir = getenv("HOME"))) {
53 if (cache_dir)
54 free(cache_dir);
55 len = strlen(homedir) + 10;
56 cache_dir = (char*) s_malloc(len * sizeof(char));
57 snprintf(cache_dir, len, "%s/.sxiv", homedir);
58 }
59}
60
61void tns_free(tns_t *tns, win_t *win) {
62 int i;
63
64 if (!tns || !tns->thumbs)
65 return;
66
67 for (i = 0; i < tns->cnt; ++i) {
68 if (tns->thumbs[i].im) {
69 imlib_context_set_image(tns->thumbs[i].im);
70 imlib_free_image();
71 }
72 }
73
74 free(tns->thumbs);
75 tns->thumbs = NULL;
76
77 if (cache_dir) {
78 free(cache_dir);
79 cache_dir = NULL;
80 }
81}
82
83void tns_load(tns_t *tns, win_t *win, int n, const char *filename) {
84 int w, h;
85 int use_cache, cached = 0;
86 float z, zw, zh;
87 thumb_t *t;
88 Imlib_Image *im;
89
90 if (!tns || !win || !filename)
91 return;
92
93 if (n >= tns->cap)
94 return;
95 else if (n >= tns->cnt)
96 tns->cnt = n + 1;
97
98 t = &tns->thumbs[n];
99
100 if (t->im) {
101 imlib_context_set_image(t->im);
102 imlib_free_image();
103 }
104
105 if ((use_cache = tns_cache_enabled())) {
106 if ((im = tns_cache_load(filename)))
107 cached = 1;
108 }
109
110 if (cached || (im = imlib_load_image(filename)))
111 imlib_context_set_image(im);
112 else
113 imlib_context_set_image(im_invalid);
114
115 w = imlib_image_get_width();
116 h = imlib_image_get_height();
117
118 if (im) {
119 t->filename = filename;
120 zw = (float) THUMB_SIZE / (float) w;
121 zh = (float) THUMB_SIZE / (float) h;
122 z = MIN(zw, zh);
123 } else {
124 t->filename = NULL;
125 z = 1.0;
126 }
127
128 t->w = z * w;
129 t->h = z * h;
130
131 imlib_context_set_anti_alias(1);
132 if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
133 die("could not allocate memory");
134 if (im)
135 imlib_free_image_and_decache();
136 if (use_cache && !cached)
137 tns_cache_write(t, False);
138
139 tns->dirty = 1;
140}
141
142void tns_check_view(tns_t *tns, Bool scrolled) {
143 int r;
144
145 if (!tns)
146 return;
147
148 tns->first -= tns->first % tns->cols;
149 r = tns->sel % tns->cols;
150
151 if (scrolled) {
152 /* move selection into visible area */
153 if (tns->sel >= tns->first + tns->cols * tns->rows)
154 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
155 else if (tns->sel < tns->first)
156 tns->sel = tns->first + r;
157 } else {
158 /* scroll to selection */
159 if (tns->first + tns->cols * tns->rows <= tns->sel) {
160 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
161 tns->dirty = 1;
162 } else if (tns->first > tns->sel) {
163 tns->first = tns->sel - r;
164 tns->dirty = 1;
165 }
166 }
167}
168
169void tns_render(tns_t *tns, win_t *win) {
170 int i, cnt, r, x, y;
171 thumb_t *t;
172
173 if (!tns || !tns->dirty || !win)
174 return;
175
176 win_clear(win);
177 imlib_context_set_drawable(win->pm);
178
179 tns->cols = MAX(1, win->w / thumb_dim);
180 tns->rows = MAX(1, win->h / thumb_dim);
181
182 if (tns->cnt < tns->cols * tns->rows) {
183 tns->first = 0;
184 cnt = tns->cnt;
185 } else {
186 tns_check_view(tns, False);
187 cnt = tns->cols * tns->rows;
188 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
189 tns->first -= r - r % tns->cols;
190 if (r > 0)
191 cnt -= r % tns->cols;
192 }
193
194 r = cnt % tns->cols ? 1 : 0;
195 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
196 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
197
198 for (i = 0; i < cnt; ++i) {
199 t = &tns->thumbs[tns->first + i];
200 t->x = x + (THUMB_SIZE - t->w) / 2;
201 t->y = y + (THUMB_SIZE - t->h) / 2;
202 imlib_context_set_image(t->im);
203 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
204 t->x, t->y, t->w, t->h);
205 if ((i + 1) % tns->cols == 0) {
206 x = tns->x;
207 y += thumb_dim;
208 } else {
209 x += thumb_dim;
210 }
211 }
212
213 tns->dirty = 0;
214 tns_highlight(tns, win, tns->sel, True);
215}
216
217void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
218 thumb_t *t;
219 unsigned long col;
220
221 if (!tns || !win)
222 return;
223
224 if (n >= 0 && n < tns->cnt) {
225 t = &tns->thumbs[n];
226
227 if (hl)
228 col = win->selcol;
229 else if (win->fullscreen)
230 col = win->black;
231 else
232 col = win->bgcol;
233
234 win_draw_rect(win, win->pm, t->x - 2, t->y - 2, t->w + 4, t->h + 4,
235 False, 2, col);
236 }
237
238 win_draw(win);
239}
240
241int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
242 int old;
243
244 if (!tns || !win)
245 return 0;
246
247 old = tns->sel;
248
249 switch (dir) {
250 case TNS_LEFT:
251 if (tns->sel > 0)
252 --tns->sel;
253 break;
254 case TNS_RIGHT:
255 if (tns->sel < tns->cnt - 1)
256 ++tns->sel;
257 break;
258 case TNS_UP:
259 if (tns->sel >= tns->cols)
260 tns->sel -= tns->cols;
261 break;
262 case TNS_DOWN:
263 if (tns->sel + tns->cols < tns->cnt)
264 tns->sel += tns->cols;
265 break;
266 }
267
268 if (tns->sel != old) {
269 tns_highlight(tns, win, old, False);
270 tns_check_view(tns, False);
271 if (!tns->dirty)
272 tns_highlight(tns, win, tns->sel, True);
273 }
274
275 return tns->sel != old;
276}
277
278int tns_scroll(tns_t *tns, tnsdir_t dir) {
279 int old;
280
281 if (!tns)
282 return 0;
283
284 old = tns->first;
285
286 if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
287 tns->first += tns->cols;
288 tns_check_view(tns, True);
289 tns->dirty = 1;
290 } else if (dir == TNS_UP && tns->first >= tns->cols) {
291 tns->first -= tns->cols;
292 tns_check_view(tns, True);
293 tns->dirty = 1;
294 }
295
296 return tns->first != old;
297}
298
299int tns_translate(tns_t *tns, int x, int y) {
300 int n;
301 thumb_t *t;
302
303 if (!tns || x < tns->x || y < tns->y)
304 return -1;
305
306 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
307 (x - tns->x) / thumb_dim;
308
309 if (n < tns->cnt) {
310 t = &tns->thumbs[n];
311 if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
312 return n;
313 }
314
315 return -1;
316}
317
318
319/* thumbnail caching */
320
321int tns_cache_enabled() {
322 struct stat stats;
323
324 return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) &&
325 !access(cache_dir, W_OK);
326}
327
328char* tns_cache_filename(const char *filename) {
329 size_t len;
330 char *cfile = NULL;
331 const char *abspath;
332
333 if (!cache_dir || !filename)
334 return NULL;
335
336 if (*filename != '/') {
337 if (!(abspath = absolute_path(filename)))
338 return NULL;
339 } else {
340 abspath = filename;
341 }
342
343 if (strncmp(abspath, cache_dir, strlen(cache_dir))) {
344 len = strlen(cache_dir) + strlen(abspath) + 6;
345 cfile = (char*) s_malloc(len);
346 snprintf(cfile, len, "%s/%s.png", cache_dir, abspath + 1);
347 }
348
349 if (abspath != filename)
350 free((void*) abspath);
351
352 return cfile;
353}
354
355Imlib_Image* tns_cache_load(const char *filename) {
356 char *cfile;
357 struct stat cstats, fstats;
358 Imlib_Image *im = NULL;
359
360 if (!filename || stat(filename, &fstats))
361 return NULL;
362
363 if ((cfile = tns_cache_filename(filename))) {
364 if (!stat(cfile, &cstats) &&
365 cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
366 cstats.st_mtim.tv_nsec == fstats.st_mtim.tv_nsec)
367 {
368 printf("cache hit: %s\n", filename);
369 im = imlib_load_image(cfile);
370 } else
371 printf("cache MISS: %s\n", filename);
372 free(cfile);
373 }
374
375 return im;
376}
377
378void tns_cache_write(thumb_t *t, Bool force) {
379 char *cfile, *dirend;
380 struct stat cstats, fstats;
381 struct timeval times[2];
382 Imlib_Load_Error err = 0;
383
384 if (!t || !t->im || !t->filename)
385 return;
386 if (stat(t->filename, &fstats))
387 return;
388
389 if ((cfile = tns_cache_filename(t->filename))) {
390 if (force || stat(cfile, &cstats) ||
391 cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
392 cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec)
393 {
394 if ((dirend = strrchr(cfile, '/'))) {
395 *dirend = '\0';
396 err = create_dir_rec(cfile);
397 *dirend = '/';
398 }
399
400 if (!err) {
401 imlib_context_set_image(t->im);
402 imlib_image_set_format("png");
403 imlib_save_image_with_error_return(cfile, &err);
404 }
405
406 if (err) {
407 warn("could not cache thumbnail: %s", t->filename);
408 } else {
409 TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
410 TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
411 utimes(cfile, times);
412 printf("thumbnail cache file written: %s\n", t->filename);
413 }
414 }
415 free(cfile);
416 }
417}