Krolyxon
Commit 524d9de

Branches

Refs →

nsxivthumbs.c

513 lines · 11.5 KB · C

1/* Copyright 2011 Bert Muennich
2 *
3 * This file is part of sxiv.
4 *
5 * sxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * sxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#define _POSIX_C_SOURCE 200112L
20#define _THUMBS_CONFIG
21#define _RENDER_CONFIG
22
23#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28#include <utime.h>
29
30#include "exif.h"
31#include "thumbs.h"
32#include "util.h"
33#include "config.h"
34
35static const int thumb_dim = THUMB_SIZE + 10;
36
37static char *cache_dir = NULL;
38
39bool tns_cache_enabled(void)
40{
41 struct stat stats;
42
43 return cache_dir != NULL && stat(cache_dir, &stats) == 0 &&
44 S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0;
45}
46
47char* tns_cache_filepath(const char *filepath)
48{
49 size_t len;
50 char *cfile = NULL;
51
52 if (cache_dir == NULL || filepath == NULL || *filepath != '/')
53 return NULL;
54
55 if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
56 /* don't cache images inside the cache directory! */
57 len = strlen(cache_dir) + strlen(filepath) + 6;
58 cfile = (char*) s_malloc(len);
59 snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
60 }
61 return cfile;
62}
63
64Imlib_Image tns_cache_load(const char *filepath)
65{
66 char *cfile;
67 struct stat cstats, fstats;
68 Imlib_Image im = NULL;
69
70 if (filepath == NULL)
71 return NULL;
72 if (stat(filepath, &fstats) < 0)
73 return NULL;
74
75 if ((cfile = tns_cache_filepath(filepath)) != NULL) {
76 if (stat(cfile, &cstats) == 0 && cstats.st_mtime == fstats.st_mtime)
77 im = imlib_load_image(cfile);
78 free(cfile);
79 }
80 return im;
81}
82
83void tns_cache_write(thumb_t *t, bool force)
84{
85 char *cfile, *dirend;
86 struct stat cstats, fstats;
87 struct utimbuf times;
88 Imlib_Load_Error err = 0;
89
90 if (t == NULL || t->im == NULL)
91 return;
92 if (t->file == NULL || t->file->name == NULL || t->file->path == NULL)
93 return;
94 if (stat(t->file->path, &fstats) < 0)
95 return;
96
97 if ((cfile = tns_cache_filepath(t->file->path)) != NULL) {
98 if (force || stat(cfile, &cstats) < 0 ||
99 cstats.st_mtime != fstats.st_mtime)
100 {
101 if ((dirend = strrchr(cfile, '/')) != NULL) {
102 *dirend = '\0';
103 err = r_mkdir(cfile);
104 *dirend = '/';
105 }
106
107 if (err == 0) {
108 imlib_context_set_image(t->im);
109 imlib_image_set_format("png");
110 imlib_save_image_with_error_return(cfile, &err);
111 }
112
113 if (err == 0) {
114 times.actime = fstats.st_atime;
115 times.modtime = fstats.st_mtime;
116 utime(cfile, &times);
117 } else {
118 warn("could not cache thumbnail: %s", t->file->name);
119 }
120 }
121 free(cfile);
122 }
123}
124
125void tns_clean_cache(tns_t *tns)
126{
127 int dirlen;
128 bool delete;
129 char *cfile, *filename, *tpos;
130 r_dir_t dir;
131
132 if (cache_dir == NULL)
133 return;
134
135 if (r_opendir(&dir, cache_dir) < 0) {
136 warn("could not open thumbnail cache directory: %s", cache_dir);
137 return;
138 }
139
140 dirlen = strlen(cache_dir);
141
142 while ((cfile = r_readdir(&dir)) != NULL) {
143 filename = cfile + dirlen;
144 delete = false;
145
146 if ((tpos = strrchr(filename, '.')) != NULL) {
147 *tpos = '\0';
148 if (access(filename, F_OK) < 0)
149 delete = true;
150 *tpos = '.';
151 }
152 if (delete) {
153 if (unlink(cfile) < 0)
154 warn("could not delete cache file: %s", cfile);
155 }
156 free(cfile);
157 }
158 r_closedir(&dir);
159}
160
161
162void tns_init(tns_t *tns, int cnt, win_t *win)
163{
164 int len;
165 const char *homedir, *dsuffix = "";
166
167 if (tns == NULL)
168 return;
169
170 if (cnt > 0) {
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->cap = cnt;
178 tns->cnt = tns->first = tns->sel = 0;
179 tns->win = win;
180 tns->alpha = !RENDER_WHITE_ALPHA;
181 tns->dirty = false;
182
183 if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
184 homedir = getenv("HOME");
185 dsuffix = "/.cache";
186 }
187 if (homedir != NULL) {
188 if (cache_dir != NULL)
189 free(cache_dir);
190 len = strlen(homedir) + strlen(dsuffix) + 6;
191 cache_dir = (char*) s_malloc(len);
192 snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
193 } else {
194 warn("could not locate thumbnail cache directory");
195 }
196}
197
198void tns_free(tns_t *tns)
199{
200 int i;
201
202 if (tns == NULL)
203 return;
204
205 if (tns->thumbs != NULL) {
206 for (i = 0; i < tns->cnt; i++) {
207 if (tns->thumbs[i].im != NULL) {
208 imlib_context_set_image(tns->thumbs[i].im);
209 imlib_free_image();
210 }
211 }
212 free(tns->thumbs);
213 tns->thumbs = NULL;
214 }
215
216 if (cache_dir != NULL) {
217 free(cache_dir);
218 cache_dir = NULL;
219 }
220}
221
222bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
223 bool force, bool silent)
224{
225 int w, h;
226 bool use_cache, cache_hit = false;
227 float z, zw, zh;
228 thumb_t *t;
229 Imlib_Image im;
230 const char *fmt;
231
232 if (tns == NULL || tns->thumbs == NULL)
233 return false;
234 if (file == NULL || file->name == NULL || file->path == NULL)
235 return false;
236 if (n < 0 || n >= tns->cap)
237 return false;
238
239 t = &tns->thumbs[n];
240 t->file = file;
241
242 if (t->im != NULL) {
243 imlib_context_set_image(t->im);
244 imlib_free_image();
245 }
246
247 if ((use_cache = tns_cache_enabled())) {
248 if (!force && (im = tns_cache_load(file->path)) != NULL)
249 cache_hit = true;
250 }
251
252 if (!cache_hit) {
253 if (access(file->path, R_OK) < 0 ||
254 (im = imlib_load_image(file->path)) == NULL)
255 {
256 if (!silent)
257 warn("could not open image: %s", file->name);
258 return false;
259 }
260 }
261
262 imlib_context_set_image(im);
263 imlib_context_set_anti_alias(1);
264
265 if ((fmt = imlib_image_format()) == NULL) {
266 if (!silent)
267 warn("could not open image: %s", file->name);
268 imlib_free_image_and_decache();
269 return false;
270 }
271 if (STREQ(fmt, "jpeg"))
272 exif_auto_orientate(file);
273
274 w = imlib_image_get_width();
275 h = imlib_image_get_height();
276 zw = (float) THUMB_SIZE / (float) w;
277 zh = (float) THUMB_SIZE / (float) h;
278 z = MIN(zw, zh);
279 z = MIN(z, 1.0);
280 t->w = z * w;
281 t->h = z * h;
282
283 t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
284 if (t->im == NULL)
285 die("could not allocate memory");
286
287 imlib_free_image_and_decache();
288
289 if (use_cache && !cache_hit)
290 tns_cache_write(t, true);
291
292 tns->dirty = true;
293 return true;
294}
295
296void tns_check_view(tns_t *tns, bool scrolled)
297{
298 int r;
299
300 if (tns == NULL)
301 return;
302
303 tns->first -= tns->first % tns->cols;
304 r = tns->sel % tns->cols;
305
306 if (scrolled) {
307 /* move selection into visible area */
308 if (tns->sel >= tns->first + tns->cols * tns->rows)
309 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
310 else if (tns->sel < tns->first)
311 tns->sel = tns->first + r;
312 } else {
313 /* scroll to selection */
314 if (tns->first + tns->cols * tns->rows <= tns->sel) {
315 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
316 tns->dirty = true;
317 } else if (tns->first > tns->sel) {
318 tns->first = tns->sel - r;
319 tns->dirty = true;
320 }
321 }
322}
323
324void tns_render(tns_t *tns)
325{
326 thumb_t *t;
327 win_t *win;
328 int i, cnt, r, x, y;
329
330 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
331 return;
332 if (!tns->dirty)
333 return;
334
335 win = tns->win;
336 win_clear(win);
337 imlib_context_set_drawable(win->pm);
338
339 tns->cols = MAX(1, win->w / thumb_dim);
340 tns->rows = MAX(1, win->h / thumb_dim);
341
342 if (tns->cnt < tns->cols * tns->rows) {
343 tns->first = 0;
344 cnt = tns->cnt;
345 } else {
346 tns_check_view(tns, false);
347 cnt = tns->cols * tns->rows;
348 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
349 tns->first -= r - r % tns->cols;
350 if (r > 0)
351 cnt -= r % tns->cols;
352 }
353
354 r = cnt % tns->cols ? 1 : 0;
355 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
356 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
357
358 for (i = 0; i < cnt; i++) {
359 t = &tns->thumbs[tns->first + i];
360 t->x = x + (THUMB_SIZE - t->w) / 2;
361 t->y = y + (THUMB_SIZE - t->h) / 2;
362 imlib_context_set_image(t->im);
363
364 if (!tns->alpha && imlib_image_has_alpha())
365 win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
366
367 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
368 t->x, t->y, t->w, t->h);
369 if (t->file->marked)
370 tns_mark(tns, tns->first + i, true);
371 if ((i + 1) % tns->cols == 0) {
372 x = tns->x;
373 y += thumb_dim;
374 } else {
375 x += thumb_dim;
376 }
377 }
378 tns->dirty = false;
379 tns_highlight(tns, tns->sel, true);
380}
381
382void tns_mark(tns_t *tns, int n, bool mark)
383{
384 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
385 return;
386
387 if (n >= 0 && n < tns->cnt) {
388 unsigned long col;
389 thumb_t *t = &tns->thumbs[n];
390 win_t *win = tns->win;
391 int x = t->x, y = t->y, w = t->w, h = t->h;
392
393 if (mark || n == tns->sel)
394 col = win->selcol;
395 else if (win->fullscreen)
396 col = win->fscol;
397 else
398 col = win->bgcol;
399
400 win_draw_rect(win, win->pm, x - 4, y - 4, 8, 2, true, 0, col);
401 win_draw_rect(win, win->pm, x - 4, y - 4, 2, 8, true, 0, col);
402 win_draw_rect(win, win->pm, x + w - 4, y - 4, 8, 2, true, 0, col);
403 win_draw_rect(win, win->pm, x + w + 2, y - 4, 2, 8, true, 0, col);
404 win_draw_rect(win, win->pm, x - 4, y + h + 2, 8, 2, true, 0, col);
405 win_draw_rect(win, win->pm, x - 4, y + h - 4, 2, 8, true, 0, col);
406 win_draw_rect(win, win->pm, x + w - 4, y + h + 2, 8, 2, true, 0, col);
407 win_draw_rect(win, win->pm, x + w + 2, y + h - 4, 2, 8, true, 0, col);
408 }
409}
410
411void tns_highlight(tns_t *tns, int n, bool hl)
412{
413 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
414 return;
415
416 if (n >= 0 && n < tns->cnt) {
417 unsigned long col;
418 thumb_t *t = &tns->thumbs[n];
419 win_t *win = tns->win;
420
421 if (hl)
422 col = win->selcol;
423 else if (win->fullscreen)
424 col = win->fscol;
425 else
426 col = win->bgcol;
427
428 win_draw_rect(win, win->pm, t->x - 3, t->y - 3, t->w + 6, t->h + 6,
429 false, 2, col);
430
431 if (!hl && t->file->marked)
432 tns_mark(tns, n, true);
433 }
434}
435
436bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
437{
438 int old, max;
439
440 if (tns == NULL || tns->thumbs == NULL)
441 return false;
442
443 old = tns->sel;
444 cnt = cnt > 1 ? cnt : 1;
445
446 switch (dir) {
447 case DIR_UP:
448 tns->sel = MAX(tns->sel - cnt * tns->cols, tns->sel % tns->cols);
449 break;
450 case DIR_DOWN:
451 max = tns->cols * ((tns->cnt - 1) / tns->cols) +
452 MIN((tns->cnt - 1) % tns->cols, tns->sel % tns->cols);
453 tns->sel = MIN(tns->sel + cnt * tns->cols, max);
454 break;
455 case DIR_LEFT:
456 tns->sel = MAX(tns->sel - cnt, 0);
457 break;
458 case DIR_RIGHT:
459 tns->sel = MIN(tns->sel + cnt, tns->cnt - 1);
460 break;
461 }
462
463 if (tns->sel != old) {
464 tns_highlight(tns, old, false);
465 tns_check_view(tns, false);
466 if (!tns->dirty)
467 tns_highlight(tns, tns->sel, true);
468 }
469 return tns->sel != old;
470}
471
472bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
473{
474 int d, max, old;
475
476 if (tns == NULL)
477 return false;
478
479 old = tns->first;
480 d = tns->cols * (screen ? tns->rows : 1);
481
482 if (dir == DIR_DOWN) {
483 max = tns->cnt - tns->cols * tns->rows;
484 if (tns->cnt % tns->cols != 0)
485 max += tns->cols - tns->cnt % tns->cols;
486 tns->first = MIN(tns->first + d, max);
487 } else if (dir == DIR_UP) {
488 tns->first = MAX(tns->first - d, 0);
489 }
490
491 if (tns->first != old) {
492 tns_check_view(tns, true);
493 tns->dirty = true;
494 }
495 return tns->first != old;
496}
497
498int tns_translate(tns_t *tns, int x, int y)
499{
500 int n;
501
502 if (tns == NULL || tns->thumbs == NULL)
503 return -1;
504 if (x < tns->x || y < tns->y)
505 return -1;
506
507 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
508 (x - tns->x) / thumb_dim;
509 if (n >= tns->cnt)
510 n = -1;
511
512 return n;
513}