Krolyxon
Commit 1d0f51f

Branches

Refs →

nsxivthumbs.c

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