Krolyxon
Commit dc54981

Branches

Refs →

nsxivthumbs.c

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