Krolyxon
Commit 391e6e7

Branches

Refs →

nsxivthumbs.c

452 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,
216 Bool force, Bool silent)
217{
218 int w, h;
219 int use_cache, cache_hit = 0;
220 float z, zw, zh;
221 thumb_t *t;
222 Imlib_Image *im;
223
224 if (!tns || !tns->thumbs || !file || !file->name || !file->path)
225 return 0;
226
227 if (n < 0 || n >= tns->cap)
228 return 0;
229
230 t = &tns->thumbs[n];
231 t->file = file;
232
233 if (t->im) {
234 imlib_context_set_image(t->im);
235 imlib_free_image();
236 }
237
238 if ((use_cache = tns_cache_enabled())) {
239 if (!force && (im = tns_cache_load(file->path)))
240 cache_hit = 1;
241 }
242
243 if (!cache_hit &&
244 (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
245 {
246 if (!silent)
247 warn("could not open image: %s", file->name);
248 return 0;
249 }
250
251 imlib_context_set_image(im);
252 imlib_context_set_anti_alias(1);
253
254 w = imlib_image_get_width();
255 h = imlib_image_get_height();
256 zw = (float) THUMB_SIZE / (float) w;
257 zh = (float) THUMB_SIZE / (float) h;
258 z = MIN(zw, zh);
259 t->w = z * w;
260 t->h = z * h;
261
262 if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
263 die("could not allocate memory");
264
265 imlib_free_image_and_decache();
266
267 if (use_cache && !cache_hit)
268 tns_cache_write(t, False);
269
270 tns->dirty = 1;
271 return 1;
272}
273
274void tns_check_view(tns_t *tns, Bool scrolled) {
275 int r;
276
277 if (!tns)
278 return;
279
280 tns->first -= tns->first % tns->cols;
281 r = tns->sel % tns->cols;
282
283 if (scrolled) {
284 /* move selection into visible area */
285 if (tns->sel >= tns->first + tns->cols * tns->rows)
286 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
287 else if (tns->sel < tns->first)
288 tns->sel = tns->first + r;
289 } else {
290 /* scroll to selection */
291 if (tns->first + tns->cols * tns->rows <= tns->sel) {
292 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
293 tns->dirty = 1;
294 } else if (tns->first > tns->sel) {
295 tns->first = tns->sel - r;
296 tns->dirty = 1;
297 }
298 }
299}
300
301void tns_render(tns_t *tns, win_t *win) {
302 int i, cnt, r, x, y;
303 thumb_t *t;
304
305 if (!tns || !tns->thumbs || !win)
306 return;
307
308 if (!tns->dirty)
309 return;
310
311 win_clear(win);
312 imlib_context_set_drawable(win->pm);
313
314 tns->cols = MAX(1, win->w / thumb_dim);
315 tns->rows = MAX(1, win->h / thumb_dim);
316
317 if (tns->cnt < tns->cols * tns->rows) {
318 tns->first = 0;
319 cnt = tns->cnt;
320 } else {
321 tns_check_view(tns, False);
322 cnt = tns->cols * tns->rows;
323 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
324 tns->first -= r - r % tns->cols;
325 if (r > 0)
326 cnt -= r % tns->cols;
327 }
328
329 r = cnt % tns->cols ? 1 : 0;
330 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
331 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
332
333 for (i = 0; i < cnt; i++) {
334 t = &tns->thumbs[tns->first + i];
335 t->x = x + (THUMB_SIZE - t->w) / 2;
336 t->y = y + (THUMB_SIZE - t->h) / 2;
337 imlib_context_set_image(t->im);
338 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
339 t->x, t->y, t->w, t->h);
340 if ((i + 1) % tns->cols == 0) {
341 x = tns->x;
342 y += thumb_dim;
343 } else {
344 x += thumb_dim;
345 }
346 }
347
348 tns->dirty = 0;
349 tns_highlight(tns, win, tns->sel, True);
350}
351
352void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
353 thumb_t *t;
354 int x, y;
355 unsigned long col;
356
357 if (!tns || !tns->thumbs || !win)
358 return;
359
360 if (n >= 0 && n < tns->cnt) {
361 t = &tns->thumbs[n];
362
363 if (hl)
364 col = win->selcol;
365 else if (win->fullscreen)
366 col = win->black;
367 else
368 col = win->bgcol;
369
370 x = t->x - (THUMB_SIZE - t->w) / 2;
371 y = t->y - (THUMB_SIZE - t->h) / 2;
372 win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
373 False, 2, col);
374 }
375
376 win_draw(win);
377}
378
379int tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
380 int old;
381
382 if (!tns || !tns->thumbs || !win)
383 return 0;
384
385 old = tns->sel;
386
387 switch (dir) {
388 case DIR_LEFT:
389 if (tns->sel > 0)
390 tns->sel--;
391 break;
392 case DIR_RIGHT:
393 if (tns->sel < tns->cnt - 1)
394 tns->sel++;
395 break;
396 case DIR_UP:
397 if (tns->sel >= tns->cols)
398 tns->sel -= tns->cols;
399 break;
400 case DIR_DOWN:
401 if (tns->sel + tns->cols < tns->cnt)
402 tns->sel += tns->cols;
403 break;
404 }
405
406 if (tns->sel != old) {
407 tns_highlight(tns, win, old, False);
408 tns_check_view(tns, False);
409 if (!tns->dirty)
410 tns_highlight(tns, win, tns->sel, True);
411 }
412
413 return tns->sel != old;
414}
415
416int tns_scroll(tns_t *tns, direction_t dir) {
417 int old;
418
419 if (!tns)
420 return 0;
421
422 old = tns->first;
423
424 if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
425 tns->first += tns->cols;
426 tns_check_view(tns, True);
427 tns->dirty = 1;
428 } else if (dir == DIR_UP && tns->first >= tns->cols) {
429 tns->first -= tns->cols;
430 tns_check_view(tns, True);
431 tns->dirty = 1;
432 }
433
434 return tns->first != old;
435}
436
437int tns_translate(tns_t *tns, int x, int y) {
438 int n;
439
440 if (!tns || !tns->thumbs)
441 return -1;
442
443 if (x < tns->x || y < tns->y)
444 return -1;
445
446 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
447 (x - tns->x) / thumb_dim;
448 if (n >= tns->cnt)
449 n = -1;
450
451 return n;
452}