Krolyxon
Commit 2bad53c

Branches

Refs →

nsxivthumbs.c

463 lines · 9.5 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#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_filename(const char *filename) {
48 size_t len;
49 char *cfile = NULL;
50 const char *abspath;
51
52 if (!cache_dir || !filename)
53 return NULL;
54
55 if (*filename != '/') {
56 if (!(abspath = absolute_path(filename)))
57 return NULL;
58 } else {
59 abspath = filename;
60 }
61
62 if (strncmp(abspath, cache_dir, strlen(cache_dir))) {
63 len = strlen(cache_dir) + strlen(abspath) + 6;
64 cfile = (char*) s_malloc(len);
65 snprintf(cfile, len, "%s/%s.png", cache_dir, abspath + 1);
66 }
67
68 if (abspath != filename)
69 free((void*) abspath);
70
71 return cfile;
72}
73
74Imlib_Image* tns_cache_load(const char *filename) {
75 char *cfile;
76 struct stat cstats, fstats;
77 Imlib_Image *im = NULL;
78
79 if (!filename)
80 return NULL;
81
82 if (stat(filename, &fstats))
83 return NULL;
84
85 if ((cfile = tns_cache_filename(filename))) {
86 if (!stat(cfile, &cstats) &&
87 cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
88 cstats.st_mtim.tv_nsec / 1000 == fstats.st_mtim.tv_nsec / 1000)
89 {
90 im = imlib_load_image(cfile);
91 }
92 free(cfile);
93 }
94
95 return im;
96}
97
98void tns_cache_write(thumb_t *t, Bool force) {
99 char *cfile, *dirend;
100 struct stat cstats, fstats;
101 struct timeval times[2];
102 Imlib_Load_Error err = 0;
103
104 if (!t || !t->im || !t->filename)
105 return;
106
107 if (stat(t->filename, &fstats))
108 return;
109
110 if ((cfile = tns_cache_filename(t->filename))) {
111 if (force || stat(cfile, &cstats) ||
112 cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
113 cstats.st_mtim.tv_nsec / 1000 != fstats.st_mtim.tv_nsec / 1000)
114 {
115 if ((dirend = strrchr(cfile, '/'))) {
116 *dirend = '\0';
117 err = r_mkdir(cfile);
118 *dirend = '/';
119 }
120
121 if (!err) {
122 imlib_context_set_image(t->im);
123 imlib_image_set_format("png");
124 imlib_save_image_with_error_return(cfile, &err);
125 }
126
127 if (err) {
128 warn("could not cache thumbnail: %s", t->filename);
129 } else {
130 TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
131 TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
132 utimes(cfile, times);
133 }
134 }
135 free(cfile);
136 }
137}
138
139void tns_clean_cache(tns_t *tns) {
140 int dirlen, delete;
141 char *cfile, *filename, *tpos;
142 r_dir_t dir;
143
144 if (!cache_dir)
145 return;
146
147 if (r_opendir(&dir, cache_dir)) {
148 warn("could not open thumbnail cache directory: %s", cache_dir);
149 return;
150 }
151
152 dirlen = strlen(cache_dir);
153
154 while ((cfile = r_readdir(&dir))) {
155 filename = cfile + dirlen;
156 delete = 0;
157
158 if ((tpos = strrchr(filename, '.'))) {
159 *tpos = '\0';
160 delete = access(filename, F_OK);
161 *tpos = '.';
162 }
163
164 if (delete && unlink(cfile))
165 warn("could not delete cache file: %s", cfile);
166
167 free(cfile);
168 }
169
170 r_closedir(&dir);
171}
172
173
174void tns_init(tns_t *tns, int cnt) {
175 int len;
176 char *homedir;
177
178 if (!tns)
179 return;
180
181 if (cnt) {
182 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
183 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
184 } else {
185 tns->thumbs = NULL;
186 }
187
188 tns->cnt = tns->first = tns->sel = 0;
189 tns->cap = cnt;
190 tns->dirty = 0;
191
192 if ((homedir = getenv("HOME"))) {
193 if (cache_dir)
194 free(cache_dir);
195 len = strlen(homedir) + 10;
196 cache_dir = (char*) s_malloc(len * sizeof(char));
197 snprintf(cache_dir, len, "%s/.sxiv", homedir);
198 } else {
199 warn("could not locate thumbnail cache directory");
200 }
201}
202
203void tns_free(tns_t *tns) {
204 int i;
205
206 if (!tns)
207 return;
208
209 if (tns->thumbs) {
210 for (i = 0; i < tns->cnt; i++) {
211 if (tns->thumbs[i].im) {
212 imlib_context_set_image(tns->thumbs[i].im);
213 imlib_free_image();
214 }
215 }
216 free(tns->thumbs);
217 tns->thumbs = NULL;
218 }
219
220 if (cache_dir) {
221 free(cache_dir);
222 cache_dir = NULL;
223 }
224}
225
226int tns_load(tns_t *tns, int n, const char *filename, unsigned char silent) {
227 int w, h;
228 int use_cache, cached = 0;
229 float z, zw, zh;
230 thumb_t *t;
231 Imlib_Image *im;
232
233 if (!tns || !tns->thumbs || !filename)
234 return 0;
235
236 if (n < 0 || n >= tns->cap)
237 return 0;
238
239 t = &tns->thumbs[n];
240 t->filename = filename;
241
242 if (t->im) {
243 imlib_context_set_image(t->im);
244 imlib_free_image();
245 }
246
247 if ((use_cache = tns_cache_enabled())) {
248 if ((im = tns_cache_load(filename)))
249 cached = 1;
250 }
251
252 if (!cached &&
253 (access(filename, R_OK) || !(im = imlib_load_image(filename))))
254 {
255 if (!silent)
256 warn("could not open image: %s", filename);
257 return 0;
258 }
259
260 imlib_context_set_image(im);
261 imlib_context_set_anti_alias(1);
262
263 w = imlib_image_get_width();
264 h = imlib_image_get_height();
265 zw = (float) THUMB_SIZE / (float) w;
266 zh = (float) THUMB_SIZE / (float) h;
267 z = MIN(zw, zh);
268 t->w = z * w;
269 t->h = z * h;
270
271 if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
272 die("could not allocate memory");
273
274 imlib_free_image_and_decache();
275
276 if (use_cache && !cached)
277 tns_cache_write(t, False);
278
279 tns->dirty = 1;
280 return 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 - 4, t->y - 4, t->w + 8, t->h + 8,
379 False, 2, col);
380 }
381
382 win_draw(win);
383}
384
385int tns_move_selection(tns_t *tns, win_t *win, direction_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 DIR_LEFT:
395 if (tns->sel > 0)
396 tns->sel--;
397 break;
398 case DIR_RIGHT:
399 if (tns->sel < tns->cnt - 1)
400 tns->sel++;
401 break;
402 case DIR_UP:
403 if (tns->sel >= tns->cols)
404 tns->sel -= tns->cols;
405 break;
406 case DIR_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, direction_t dir) {
423 int old;
424
425 if (!tns)
426 return 0;
427
428 old = tns->first;
429
430 if (dir == DIR_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 == DIR_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}