nsxiv/thumbs.c

462 lines
9.6 KiB
C
Raw Normal View History

/* sxiv: thumbs.c
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
2011-09-03 21:11:45 +00:00
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
2011-09-03 21:11:45 +00:00
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
2011-09-03 21:11:45 +00:00
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _POSIX_C_SOURCE 200112L
#include <stdlib.h>
2011-02-16 22:03:42 +00:00
#include <string.h>
2011-04-06 23:26:08 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
#include "thumbs.h"
#include "util.h"
#define _THUMBS_CONFIG
2011-07-22 12:49:06 +00:00
#include "config.h"
#ifdef EXIF_SUPPORT
2011-09-06 09:09:57 +00:00
void exif_auto_orientate(const fileinfo_t*);
#endif
2011-09-06 09:09:57 +00:00
const int thumb_dim = THUMB_SIZE + 10;
2011-04-07 15:23:47 +00:00
char *cache_dir = NULL;
2011-04-08 08:24:10 +00:00
int tns_cache_enabled() {
struct stat stats;
return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) &&
!access(cache_dir, W_OK);
}
2011-08-17 22:38:55 +00:00
char* tns_cache_filepath(const char *filepath) {
2011-04-08 08:24:10 +00:00
size_t len;
char *cfile = NULL;
2011-08-17 22:38:55 +00:00
if (!cache_dir || !filepath || *filepath != '/')
2011-04-08 08:24:10 +00:00
return NULL;
2011-08-17 22:38:55 +00:00
if (strncmp(filepath, cache_dir, strlen(cache_dir))) {
len = strlen(cache_dir) + strlen(filepath) + 6;
2011-04-08 08:24:10 +00:00
cfile = (char*) s_malloc(len);
2011-08-17 22:38:55 +00:00
snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
2011-04-08 08:24:10 +00:00
}
return cfile;
}
2011-08-17 22:38:55 +00:00
Imlib_Image* tns_cache_load(const char *filepath) {
2011-04-08 08:24:10 +00:00
char *cfile;
struct stat cstats, fstats;
Imlib_Image *im = NULL;
2011-08-17 22:38:55 +00:00
if (!filepath)
2011-04-08 12:44:00 +00:00
return NULL;
2011-08-17 22:38:55 +00:00
if (stat(filepath, &fstats))
2011-04-08 08:24:10 +00:00
return NULL;
2011-08-17 22:38:55 +00:00
if ((cfile = tns_cache_filepath(filepath))) {
if (!stat(cfile, &cstats) && cstats.st_mtime == fstats.st_mtime)
2011-04-08 08:24:10 +00:00
im = imlib_load_image(cfile);
free(cfile);
}
return im;
}
void tns_cache_write(thumb_t *t, Bool force) {
char *cfile, *dirend;
struct stat cstats, fstats;
struct utimbuf times;
2011-04-08 08:24:10 +00:00
Imlib_Load_Error err = 0;
2011-08-17 22:38:55 +00:00
if (!t || !t->im || !t->file || !t->file->name || !t->file->path)
2011-04-08 08:24:10 +00:00
return;
2011-04-08 12:44:00 +00:00
2011-08-17 22:38:55 +00:00
if (stat(t->file->path, &fstats))
2011-04-08 08:24:10 +00:00
return;
2011-08-17 22:38:55 +00:00
if ((cfile = tns_cache_filepath(t->file->path))) {
if (force || stat(cfile, &cstats) || cstats.st_mtime != fstats.st_mtime) {
2011-04-08 08:24:10 +00:00
if ((dirend = strrchr(cfile, '/'))) {
*dirend = '\0';
err = r_mkdir(cfile);
*dirend = '/';
}
if (!err) {
imlib_context_set_image(t->im);
imlib_image_set_format("png");
imlib_save_image_with_error_return(cfile, &err);
}
if (err) {
2011-08-17 22:38:55 +00:00
warn("could not cache thumbnail: %s", t->file->name);
2011-04-08 08:24:10 +00:00
} else {
times.actime = fstats.st_atime;
times.modtime = fstats.st_mtime;
utime(cfile, &times);
2011-04-08 08:24:10 +00:00
}
}
free(cfile);
}
}
void tns_clean_cache(tns_t *tns) {
2011-04-08 12:44:00 +00:00
int dirlen, delete;
char *cfile, *filename, *tpos;
r_dir_t dir;
if (!cache_dir)
return;
if (r_opendir(&dir, cache_dir)) {
warn("could not open thumbnail cache directory: %s", cache_dir);
return;
}
dirlen = strlen(cache_dir);
while ((cfile = r_readdir(&dir))) {
filename = cfile + dirlen;
delete = 0;
if ((tpos = strrchr(filename, '.'))) {
*tpos = '\0';
delete = access(filename, F_OK);
*tpos = '.';
}
if (delete && unlink(cfile))
warn("could not delete cache file: %s", cfile);
free(cfile);
}
r_closedir(&dir);
}
2011-04-06 23:26:08 +00:00
2011-02-17 10:04:58 +00:00
void tns_init(tns_t *tns, int cnt) {
2011-04-07 15:23:47 +00:00
int len;
char *homedir;
2011-02-17 10:04:58 +00:00
if (!tns)
2011-02-16 22:03:42 +00:00
return;
2011-04-08 12:44:00 +00:00
if (cnt) {
tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
} else {
tns->thumbs = NULL;
}
tns->cnt = tns->first = tns->sel = 0;
tns->cap = cnt;
tns->alpha = 1;
2011-02-19 08:47:53 +00:00
tns->dirty = 0;
2011-04-07 15:23:47 +00:00
if ((homedir = getenv("HOME"))) {
if (cache_dir)
free(cache_dir);
len = strlen(homedir) + 10;
cache_dir = (char*) s_malloc(len * sizeof(char));
snprintf(cache_dir, len, "%s/.sxiv", homedir);
2011-04-08 12:44:00 +00:00
} else {
warn("could not locate thumbnail cache directory");
2011-04-07 15:23:47 +00:00
}
}
void tns_free(tns_t *tns) {
int i;
2011-04-08 12:44:00 +00:00
if (!tns)
return;
2011-04-08 12:44:00 +00:00
if (tns->thumbs) {
for (i = 0; i < tns->cnt; i++) {
2011-04-08 12:44:00 +00:00
if (tns->thumbs[i].im) {
imlib_context_set_image(tns->thumbs[i].im);
imlib_free_image();
}
}
2011-04-08 12:44:00 +00:00
free(tns->thumbs);
tns->thumbs = NULL;
}
2011-04-07 15:23:47 +00:00
if (cache_dir) {
free(cache_dir);
cache_dir = NULL;
}
}
int tns_load(tns_t *tns, int n, const fileinfo_t *file,
Bool force, Bool silent)
{
2011-02-17 10:04:58 +00:00
int w, h;
int use_cache, cache_hit = 0;
2011-02-17 10:04:58 +00:00
float z, zw, zh;
thumb_t *t;
Imlib_Image *im;
2011-09-06 09:09:57 +00:00
const char *fmt;
2011-02-17 10:04:58 +00:00
2011-08-17 22:38:55 +00:00
if (!tns || !tns->thumbs || !file || !file->name || !file->path)
return 0;
2011-02-17 10:04:58 +00:00
if (n < 0 || n >= tns->cap)
return 0;
t = &tns->thumbs[n];
2011-08-17 22:38:55 +00:00
t->file = file;
if (t->im) {
imlib_context_set_image(t->im);
imlib_free_image();
}
if ((use_cache = tns_cache_enabled())) {
if (!force && (im = tns_cache_load(file->path)))
cache_hit = 1;
}
if (!cache_hit &&
2011-08-17 22:38:55 +00:00
(access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
{
if (!silent)
2011-08-17 22:38:55 +00:00
warn("could not open image: %s", file->name);
return 0;
}
imlib_context_set_image(im);
imlib_context_set_anti_alias(1);
2011-02-17 10:04:58 +00:00
fmt = imlib_image_format();
/* avoid unused-but-set-variable warning */
(void) fmt;
#ifdef EXIF_SUPPORT
2011-09-08 14:19:30 +00:00
if (!cache_hit && !strcmp(fmt, "jpeg"))
exif_auto_orientate(file);
#endif
2011-09-06 09:09:57 +00:00
2011-02-17 10:04:58 +00:00
w = imlib_image_get_width();
h = imlib_image_get_height();
zw = (float) THUMB_SIZE / (float) w;
zh = (float) THUMB_SIZE / (float) h;
z = MIN(zw, zh);
2011-02-17 10:04:58 +00:00
t->w = z * w;
t->h = z * h;
if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
die("could not allocate memory");
imlib_free_image_and_decache();
if (use_cache && !cache_hit)
tns_cache_write(t, False);
tns->dirty = 1;
return 1;
2011-02-17 10:04:58 +00:00
}
void tns_check_view(tns_t *tns, Bool scrolled) {
2011-02-19 23:08:17 +00:00
int r;
if (!tns)
return;
tns->first -= tns->first % tns->cols;
r = tns->sel % tns->cols;
if (scrolled) {
/* move selection into visible area */
if (tns->sel >= tns->first + tns->cols * tns->rows)
tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
else if (tns->sel < tns->first)
tns->sel = tns->first + r;
} else {
/* scroll to selection */
if (tns->first + tns->cols * tns->rows <= tns->sel) {
tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
tns->dirty = 1;
} else if (tns->first > tns->sel) {
tns->first = tns->sel - r;
tns->dirty = 1;
}
}
}
void tns_render(tns_t *tns, win_t *win) {
int i, cnt, r, x, y;
thumb_t *t;
2011-04-08 12:44:00 +00:00
if (!tns || !tns->thumbs || !win)
return;
if (!tns->dirty)
return;
win_clear(win);
imlib_context_set_drawable(win->pm);
2011-02-17 15:22:54 +00:00
tns->cols = MAX(1, win->w / thumb_dim);
tns->rows = MAX(1, win->h / thumb_dim);
if (tns->cnt < tns->cols * tns->rows) {
tns->first = 0;
cnt = tns->cnt;
} else {
tns_check_view(tns, False);
cnt = tns->cols * tns->rows;
if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
tns->first -= r - r % tns->cols;
if (r > 0)
cnt -= r % tns->cols;
}
r = cnt % tns->cols ? 1 : 0;
2011-02-18 13:57:24 +00:00
tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
for (i = 0; i < cnt; i++) {
t = &tns->thumbs[tns->first + i];
t->x = x + (THUMB_SIZE - t->w) / 2;
t->y = y + (THUMB_SIZE - t->h) / 2;
imlib_context_set_image(t->im);
if (imlib_image_has_alpha() && !tns->alpha)
win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, True, 0, win->white);
imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
t->x, t->y, t->w, t->h);
if ((i + 1) % tns->cols == 0) {
2011-02-18 13:57:24 +00:00
x = tns->x;
y += thumb_dim;
} else {
x += thumb_dim;
}
}
2011-02-19 08:47:53 +00:00
tns->dirty = 0;
tns_highlight(tns, win, tns->sel, True);
}
void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
2011-02-17 15:22:54 +00:00
thumb_t *t;
int x, y;
2011-03-09 10:37:49 +00:00
unsigned long col;
2011-02-17 15:22:54 +00:00
2011-04-08 12:44:00 +00:00
if (!tns || !tns->thumbs || !win)
2011-02-17 15:22:54 +00:00
return;
if (n >= 0 && n < tns->cnt) {
t = &tns->thumbs[n];
2011-03-09 10:37:49 +00:00
if (hl)
col = win->selcol;
else if (win->fullscreen)
col = win->black;
else
col = win->bgcol;
x = t->x - (THUMB_SIZE - t->w) / 2;
y = t->y - (THUMB_SIZE - t->h) / 2;
win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
2011-07-26 22:20:27 +00:00
False, 2, col);
2011-02-17 15:22:54 +00:00
}
2011-02-17 15:57:55 +00:00
win_draw(win);
}
2011-07-22 12:49:06 +00:00
int tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
int old;
2011-02-17 15:57:55 +00:00
2011-04-08 12:44:00 +00:00
if (!tns || !tns->thumbs || !win)
2011-02-19 08:47:53 +00:00
return 0;
2011-02-17 15:57:55 +00:00
old = tns->sel;
2011-02-17 15:57:55 +00:00
switch (dir) {
2011-07-22 12:49:06 +00:00
case DIR_LEFT:
if (tns->sel > 0)
tns->sel--;
2011-02-17 15:57:55 +00:00
break;
2011-07-22 12:49:06 +00:00
case DIR_RIGHT:
if (tns->sel < tns->cnt - 1)
tns->sel++;
2011-02-17 15:57:55 +00:00
break;
2011-07-22 12:49:06 +00:00
case DIR_UP:
if (tns->sel >= tns->cols)
tns->sel -= tns->cols;
2011-02-17 15:57:55 +00:00
break;
2011-07-22 12:49:06 +00:00
case DIR_DOWN:
if (tns->sel + tns->cols < tns->cnt)
tns->sel += tns->cols;
2011-02-17 15:57:55 +00:00
break;
}
2011-02-18 14:33:52 +00:00
if (tns->sel != old) {
tns_highlight(tns, win, old, False);
tns_check_view(tns, False);
if (!tns->dirty)
tns_highlight(tns, win, tns->sel, True);
2011-02-18 14:33:52 +00:00
}
2011-02-19 08:47:53 +00:00
return tns->sel != old;
2011-02-17 15:22:54 +00:00
}
2011-02-17 16:28:13 +00:00
2011-07-22 12:49:06 +00:00
int tns_scroll(tns_t *tns, direction_t dir) {
2011-02-19 23:08:17 +00:00
int old;
if (!tns)
return 0;
old = tns->first;
2011-07-22 12:49:06 +00:00
if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
2011-02-19 23:08:17 +00:00
tns->first += tns->cols;
tns_check_view(tns, True);
tns->dirty = 1;
2011-07-22 12:49:06 +00:00
} else if (dir == DIR_UP && tns->first >= tns->cols) {
2011-02-19 23:08:17 +00:00
tns->first -= tns->cols;
tns_check_view(tns, True);
tns->dirty = 1;
}
return tns->first != old;
2011-02-19 23:08:17 +00:00
}
2011-02-17 16:28:13 +00:00
int tns_translate(tns_t *tns, int x, int y) {
int n;
2011-04-08 12:44:00 +00:00
if (!tns || !tns->thumbs)
return -1;
if (x < tns->x || y < tns->y)
2011-02-17 16:28:13 +00:00
return -1;
n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
(x - tns->x) / thumb_dim;
if (n >= tns->cnt)
n = -1;
2011-02-18 14:02:49 +00:00
return n;
2011-02-17 16:28:13 +00:00
}