Krolyxon
Commit 421f012

Branches

Refs →

nsxivthumbs.c

450 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
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (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., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
17 */
18
19#define _THUMBS_CONFIG
20
21#include <stdlib.h>
22#include <string.h>
23#include <sys/time.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27
28#include "thumbs.h"
29#include "util.h"
30#include "config.h"
31
32#ifdef __NetBSD__
33#define st_mtim st_mtimespec
34#define st_atim st_atimespec
35#endif
36
37const int thumb_dim = THUMB_SIZE + 10;
38char *cache_dir = NULL;
39
40int 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) &&
76 cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
77 cstats.st_mtim.tv_nsec / 1000 == fstats.st_mtim.tv_nsec / 1000)
78 {
79 im = imlib_load_image(cfile);
80 }
81 free(cfile);
82 }
83
84 return im;
85}
86
87void tns_cache_write(thumb_t *t, Bool force) {
88 char *cfile, *dirend;
89 struct stat cstats, fstats;
90 struct timeval times[2];
91 Imlib_Load_Error err = 0;
92
93 if (!t || !t->im || !t->file || !t->file->name || !t->file->path)
94 return;
95
96 if (stat(t->file->path, &fstats))
97 return;
98
99 if ((cfile = tns_cache_filepath(t->file->path))) {
100 if (force || stat(cfile, &cstats) ||
101 cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
102 cstats.st_mtim.tv_nsec / 1000 != fstats.st_mtim.tv_nsec / 1000)
103 {
104 if ((dirend = strrchr(cfile, '/'))) {
105 *dirend = '\0';
106 err = r_mkdir(cfile);
107 *dirend = '/';
108 }
109
110 if (!err) {
111 imlib_context_set_image(t->im);
112 imlib_image_set_format("png");
113 imlib_save_image_with_error_return(cfile, &err);
114 }
115
116 if (err) {
117 warn("could not cache thumbnail: %s", t->file->name);
118 } else {
119 TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
120 TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
121 utimes(cfile, times);
122 }
123 }
124 free(cfile);
125 }
126}
127
128void tns_clean_cache(tns_t *tns) {
129 int dirlen, delete;
130 char *cfile, *filename, *tpos;
131 r_dir_t dir;
132
133 if (!cache_dir)
134 return;
135
136 if (r_opendir(&dir, cache_dir)) {
137 warn("could not open thumbnail cache directory: %s", cache_dir);
138 return;
139 }
140
141 dirlen = strlen(cache_dir);
142
143 while ((cfile = r_readdir(&dir))) {
144 filename = cfile + dirlen;
145 delete = 0;
146
147 if ((tpos = strrchr(filename, '.'))) {
148 *tpos = '\0';
149 delete = access(filename, F_OK);
150 *tpos = '.';
151 }
152
153 if (delete && unlink(cfile))
154 warn("could not delete cache file: %s", cfile);
155
156 free(cfile);
157 }
158
159 r_closedir(&dir);
160}
161
162
163void tns_init(tns_t *tns, int cnt) {
164 int len;
165 char *homedir;
166
167 if (!tns)
168 return;
169
170 if (cnt) {
171 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
172 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
173 } else {
174 tns->thumbs = NULL;
175 }
176
177 tns->cnt = tns->first = tns->sel = 0;
178 tns->cap = cnt;
179 tns->dirty = 0;
180
181 if ((homedir = getenv("HOME"))) {
182 if (cache_dir)
183 free(cache_dir);
184 len = strlen(homedir) + 10;
185 cache_dir = (char*) s_malloc(len * sizeof(char));
186 snprintf(cache_dir, len, "%s/.sxiv", homedir);
187 } else {
188 warn("could not locate thumbnail cache directory");
189 }
190}
191
192void tns_free(tns_t *tns) {
193 int i;
194
195 if (!tns)
196 return;
197
198 if (tns->thumbs) {
199 for (i = 0; i < tns->cnt; i++) {
200 if (tns->thumbs[i].im) {
201 imlib_context_set_image(tns->thumbs[i].im);
202 imlib_free_image();
203 }
204 }
205 free(tns->thumbs);
206 tns->thumbs = NULL;
207 }
208
209 if (cache_dir) {
210 free(cache_dir);
211 cache_dir = NULL;
212 }
213}
214
215int tns_load(tns_t *tns, int n, const fileinfo_t *file, unsigned char silent) {
216 int w, h;
217 int use_cache, cached = 0;
218 float z, zw, zh;
219 thumb_t *t;
220 Imlib_Image *im;
221
222 if (!tns || !tns->thumbs || !file || !file->name || !file->path)
223 return 0;
224
225 if (n < 0 || n >= tns->cap)
226 return 0;
227
228 t = &tns->thumbs[n];
229 t->file = file;
230
231 if (t->im) {
232 imlib_context_set_image(t->im);
233 imlib_free_image();
234 }
235
236 if ((use_cache = tns_cache_enabled())) {
237 if ((im = tns_cache_load(file->path)))
238 cached = 1;
239 }
240
241 if (!cached &&
242 (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
243 {
244 if (!silent)
245 warn("could not open image: %s", file->name);
246 return 0;
247 }
248
249 imlib_context_set_image(im);
250 imlib_context_set_anti_alias(1);
251
252 w = imlib_image_get_width();
253 h = imlib_image_get_height();
254 zw = (float) THUMB_SIZE / (float) w;
255 zh = (float) THUMB_SIZE / (float) h;
256 z = MIN(zw, zh);
257 t->w = z * w;
258 t->h = z * h;
259
260 if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
261 die("could not allocate memory");
262
263 imlib_free_image_and_decache();
264
265 if (use_cache && !cached)
266 tns_cache_write(t, False);
267
268 tns->dirty = 1;
269 return 1;
270}
271
272void tns_check_view(tns_t *tns, Bool scrolled) {
273 int r;
274
275 if (!tns)
276 return;
277
278 tns->first -= tns->first % tns->cols;
279 r = tns->sel % tns->cols;
280
281 if (scrolled) {
282 /* move selection into visible area */
283 if (tns->sel >= tns->first + tns->cols * tns->rows)
284 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
285 else if (tns->sel < tns->first)
286 tns->sel = tns->first + r;
287 } else {
288 /* scroll to selection */
289 if (tns->first + tns->cols * tns->rows <= tns->sel) {
290 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
291 tns->dirty = 1;
292 } else if (tns->first > tns->sel) {
293 tns->first = tns->sel - r;
294 tns->dirty = 1;
295 }
296 }
297}
298
299void tns_render(tns_t *tns, win_t *win) {
300 int i, cnt, r, x, y;
301 thumb_t *t;
302
303 if (!tns || !tns->thumbs || !win)
304 return;
305
306 if (!tns->dirty)
307 return;
308
309 win_clear(win);
310 imlib_context_set_drawable(win->pm);
311
312 tns->cols = MAX(1, win->w / thumb_dim);
313 tns->rows = MAX(1, win->h / thumb_dim);
314
315 if (tns->cnt < tns->cols * tns->rows) {
316 tns->first = 0;
317 cnt = tns->cnt;
318 } else {
319 tns_check_view(tns, False);
320 cnt = tns->cols * tns->rows;
321 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
322 tns->first -= r - r % tns->cols;
323 if (r > 0)
324 cnt -= r % tns->cols;
325 }
326
327 r = cnt % tns->cols ? 1 : 0;
328 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
329 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
330
331 for (i = 0; i < cnt; i++) {
332 t = &tns->thumbs[tns->first + i];
333 t->x = x + (THUMB_SIZE - t->w) / 2;
334 t->y = y + (THUMB_SIZE - t->h) / 2;
335 imlib_context_set_image(t->im);
336 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
337 t->x, t->y, t->w, t->h);
338 if ((i + 1) % tns->cols == 0) {
339 x = tns->x;
340 y += thumb_dim;
341 } else {
342 x += thumb_dim;
343 }
344 }
345
346 tns->dirty = 0;
347 tns_highlight(tns, win, tns->sel, True);
348}
349
350void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
351 thumb_t *t;
352 int x, y;
353 unsigned long col;
354
355 if (!tns || !tns->thumbs || !win)
356 return;
357
358 if (n >= 0 && n < tns->cnt) {
359 t = &tns->thumbs[n];
360
361 if (hl)
362 col = win->selcol;
363 else if (win->fullscreen)
364 col = win->black;
365 else
366 col = win->bgcol;
367
368 x = t->x - (THUMB_SIZE - t->w) / 2;
369 y = t->y - (THUMB_SIZE - t->h) / 2;
370 win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
371 False, 2, col);
372 }
373
374 win_draw(win);
375}
376
377int tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
378 int old;
379
380 if (!tns || !tns->thumbs || !win)
381 return 0;
382
383 old = tns->sel;
384
385 switch (dir) {
386 case DIR_LEFT:
387 if (tns->sel > 0)
388 tns->sel--;
389 break;
390 case DIR_RIGHT:
391 if (tns->sel < tns->cnt - 1)
392 tns->sel++;
393 break;
394 case DIR_UP:
395 if (tns->sel >= tns->cols)
396 tns->sel -= tns->cols;
397 break;
398 case DIR_DOWN:
399 if (tns->sel + tns->cols < tns->cnt)
400 tns->sel += tns->cols;
401 break;
402 }
403
404 if (tns->sel != old) {
405 tns_highlight(tns, win, old, False);
406 tns_check_view(tns, False);
407 if (!tns->dirty)
408 tns_highlight(tns, win, tns->sel, True);
409 }
410
411 return tns->sel != old;
412}
413
414int tns_scroll(tns_t *tns, direction_t dir) {
415 int old;
416
417 if (!tns)
418 return 0;
419
420 old = tns->first;
421
422 if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
423 tns->first += tns->cols;
424 tns_check_view(tns, True);
425 tns->dirty = 1;
426 } else if (dir == DIR_UP && tns->first >= tns->cols) {
427 tns->first -= tns->cols;
428 tns_check_view(tns, True);
429 tns->dirty = 1;
430 }
431
432 return tns->first != old;
433}
434
435int tns_translate(tns_t *tns, int x, int y) {
436 int n;
437
438 if (!tns || !tns->thumbs)
439 return -1;
440
441 if (x < tns->x || y < tns->y)
442 return -1;
443
444 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
445 (x - tns->x) / thumb_dim;
446 if (n >= tns->cnt)
447 n = -1;
448
449 return n;
450}