Krolyxon
Commit dd9e5da

Branches

Refs →

nsxivthumbs.c

460 lines · 9.4 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 im = imlib_load_image(cfile);
86 }
87 free(cfile);
88 }
89
90 return im;
91}
92
93void tns_cache_write(thumb_t *t, Bool force) {
94 char *cfile, *dirend;
95 struct stat cstats, fstats;
96 struct timeval times[2];
97 Imlib_Load_Error err = 0;
98
99 if (!t || !t->im || !t->filename)
100 return;
101
102 if (stat(t->filename, &fstats))
103 return;
104
105 if ((cfile = tns_cache_filename(t->filename))) {
106 if (force || stat(cfile, &cstats) ||
107 cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
108 cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec)
109 {
110 if ((dirend = strrchr(cfile, '/'))) {
111 *dirend = '\0';
112 err = r_mkdir(cfile);
113 *dirend = '/';
114 }
115
116 if (!err) {
117 imlib_context_set_image(t->im);
118 imlib_image_set_format("png");
119 imlib_save_image_with_error_return(cfile, &err);
120 }
121
122 if (err) {
123 warn("could not cache thumbnail: %s", t->filename);
124 } else {
125 TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
126 TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
127 utimes(cfile, times);
128 }
129 }
130 free(cfile);
131 }
132}
133
134void tns_clear_cache(tns_t *tns) {
135 int dirlen, delete;
136 char *cfile, *filename, *tpos;
137 r_dir_t dir;
138
139 if (!cache_dir)
140 return;
141
142 if (r_opendir(&dir, cache_dir)) {
143 warn("could not open thumbnail cache directory: %s", cache_dir);
144 return;
145 }
146
147 dirlen = strlen(cache_dir);
148
149 while ((cfile = r_readdir(&dir))) {
150 filename = cfile + dirlen;
151 delete = 0;
152
153 if ((tpos = strrchr(filename, '.'))) {
154 *tpos = '\0';
155 delete = access(filename, F_OK);
156 *tpos = '.';
157 }
158
159 if (delete && unlink(cfile))
160 warn("could not delete cache file: %s", cfile);
161
162 free(cfile);
163 }
164
165 r_closedir(&dir);
166}
167
168
169void tns_init(tns_t *tns, int cnt) {
170 int len;
171 char *homedir;
172
173 if (!tns)
174 return;
175
176 if (cnt) {
177 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
178 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
179 } else {
180 tns->thumbs = NULL;
181 }
182
183 tns->cnt = tns->first = tns->sel = 0;
184 tns->cap = cnt;
185 tns->dirty = 0;
186
187 if ((homedir = getenv("HOME"))) {
188 if (cache_dir)
189 free(cache_dir);
190 len = strlen(homedir) + 10;
191 cache_dir = (char*) s_malloc(len * sizeof(char));
192 snprintf(cache_dir, len, "%s/.sxiv", homedir);
193 } else {
194 warn("could not locate thumbnail cache directory");
195 }
196}
197
198void tns_free(tns_t *tns, win_t *win) {
199 int i;
200
201 if (!tns)
202 return;
203
204 if (tns->thumbs) {
205 for (i = 0; i < tns->cnt; ++i) {
206 if (tns->thumbs[i].im) {
207 imlib_context_set_image(tns->thumbs[i].im);
208 imlib_free_image();
209 }
210 }
211 free(tns->thumbs);
212 tns->thumbs = NULL;
213 }
214
215 if (cache_dir) {
216 free(cache_dir);
217 cache_dir = NULL;
218 }
219}
220
221void tns_load(tns_t *tns, win_t *win, int n, const char *filename) {
222 int w, h;
223 int use_cache, cached = 0;
224 float z, zw, zh;
225 thumb_t *t;
226 Imlib_Image *im;
227
228 if (!tns || !tns->thumbs || !win || !filename)
229 return;
230
231 if (n >= tns->cap)
232 return;
233 else if (n >= tns->cnt)
234 tns->cnt = n + 1;
235
236 t = &tns->thumbs[n];
237
238 if (t->im) {
239 imlib_context_set_image(t->im);
240 imlib_free_image();
241 }
242
243 if ((use_cache = tns_cache_enabled())) {
244 if ((im = tns_cache_load(filename)))
245 cached = 1;
246 }
247
248 if (cached || (im = imlib_load_image(filename)))
249 imlib_context_set_image(im);
250 else
251 imlib_context_set_image(im_invalid);
252
253 w = imlib_image_get_width();
254 h = imlib_image_get_height();
255
256 if (im) {
257 t->filename = filename;
258 zw = (float) THUMB_SIZE / (float) w;
259 zh = (float) THUMB_SIZE / (float) h;
260 z = MIN(zw, zh);
261 } else {
262 t->filename = NULL;
263 z = 1.0;
264 }
265
266 t->w = z * w;
267 t->h = z * h;
268
269 imlib_context_set_anti_alias(1);
270 if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
271 die("could not allocate memory");
272 if (im)
273 imlib_free_image_and_decache();
274 if (use_cache && !cached)
275 tns_cache_write(t, False);
276
277 tns->dirty = 1;
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 = 1;
300 } else if (tns->first > tns->sel) {
301 tns->first = tns->sel - r;
302 tns->dirty = 1;
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 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
345 t->x, t->y, t->w, t->h);
346 if ((i + 1) % tns->cols == 0) {
347 x = tns->x;
348 y += thumb_dim;
349 } else {
350 x += thumb_dim;
351 }
352 }
353
354 tns->dirty = 0;
355 tns_highlight(tns, win, tns->sel, True);
356}
357
358void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
359 thumb_t *t;
360 unsigned long col;
361
362 if (!tns || !tns->thumbs || !win)
363 return;
364
365 if (n >= 0 && n < tns->cnt) {
366 t = &tns->thumbs[n];
367
368 if (hl)
369 col = win->selcol;
370 else if (win->fullscreen)
371 col = win->black;
372 else
373 col = win->bgcol;
374
375 win_draw_rect(win, win->pm, t->x - 2, t->y - 2, t->w + 4, t->h + 4,
376 False, 2, col);
377 }
378
379 win_draw(win);
380}
381
382int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
383 int old;
384
385 if (!tns || !tns->thumbs || !win)
386 return 0;
387
388 old = tns->sel;
389
390 switch (dir) {
391 case TNS_LEFT:
392 if (tns->sel > 0)
393 --tns->sel;
394 break;
395 case TNS_RIGHT:
396 if (tns->sel < tns->cnt - 1)
397 ++tns->sel;
398 break;
399 case TNS_UP:
400 if (tns->sel >= tns->cols)
401 tns->sel -= tns->cols;
402 break;
403 case TNS_DOWN:
404 if (tns->sel + tns->cols < tns->cnt)
405 tns->sel += tns->cols;
406 break;
407 }
408
409 if (tns->sel != old) {
410 tns_highlight(tns, win, old, False);
411 tns_check_view(tns, False);
412 if (!tns->dirty)
413 tns_highlight(tns, win, tns->sel, True);
414 }
415
416 return tns->sel != old;
417}
418
419int tns_scroll(tns_t *tns, tnsdir_t dir) {
420 int old;
421
422 if (!tns)
423 return 0;
424
425 old = tns->first;
426
427 if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
428 tns->first += tns->cols;
429 tns_check_view(tns, True);
430 tns->dirty = 1;
431 } else if (dir == TNS_UP && tns->first >= tns->cols) {
432 tns->first -= tns->cols;
433 tns_check_view(tns, True);
434 tns->dirty = 1;
435 }
436
437 return tns->first != old;
438}
439
440int tns_translate(tns_t *tns, int x, int y) {
441 int n;
442 thumb_t *t;
443
444 if (!tns || !tns->thumbs)
445 return -1;
446
447 if (x < tns->x || y < tns->y)
448 return -1;
449
450 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
451 (x - tns->x) / thumb_dim;
452
453 if (n < tns->cnt) {
454 t = &tns->thumbs[n];
455 if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
456 return n;
457 }
458
459 return -1;
460}