| 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 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/time.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "thumbs.h" |
| 28 | #include "util.h" |
| 29 | |
| 30 | extern Imlib_Image *im_invalid; |
| 31 | |
| 32 | const int thumb_dim = THUMB_SIZE + 10; |
| 33 | char *cache_dir = NULL; |
| 34 | |
| 35 | int tns_cache_enabled(); |
| 36 | Imlib_Image* tns_cache_load(const char*); |
| 37 | void tns_cache_write(thumb_t*, Bool); |
| 38 | |
| 39 | void tns_init(tns_t *tns, int cnt) { |
| 40 | int len; |
| 41 | char *homedir; |
| 42 | |
| 43 | if (!tns) |
| 44 | return; |
| 45 | |
| 46 | tns->cnt = tns->first = tns->sel = 0; |
| 47 | tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t)); |
| 48 | memset(tns->thumbs, 0, cnt * sizeof(thumb_t)); |
| 49 | tns->cap = cnt; |
| 50 | tns->dirty = 0; |
| 51 | |
| 52 | if ((homedir = getenv("HOME"))) { |
| 53 | if (cache_dir) |
| 54 | free(cache_dir); |
| 55 | len = strlen(homedir) + 10; |
| 56 | cache_dir = (char*) s_malloc(len * sizeof(char)); |
| 57 | snprintf(cache_dir, len, "%s/.sxiv", homedir); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void tns_free(tns_t *tns, win_t *win) { |
| 62 | int i; |
| 63 | |
| 64 | if (!tns || !tns->thumbs) |
| 65 | return; |
| 66 | |
| 67 | for (i = 0; i < tns->cnt; ++i) { |
| 68 | if (tns->thumbs[i].im) { |
| 69 | imlib_context_set_image(tns->thumbs[i].im); |
| 70 | imlib_free_image(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | free(tns->thumbs); |
| 75 | tns->thumbs = NULL; |
| 76 | |
| 77 | if (cache_dir) { |
| 78 | free(cache_dir); |
| 79 | cache_dir = NULL; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void tns_load(tns_t *tns, win_t *win, int n, const char *filename) { |
| 84 | int w, h; |
| 85 | int use_cache, cached = 0; |
| 86 | float z, zw, zh; |
| 87 | thumb_t *t; |
| 88 | Imlib_Image *im; |
| 89 | |
| 90 | if (!tns || !win || !filename) |
| 91 | return; |
| 92 | |
| 93 | if (n >= tns->cap) |
| 94 | return; |
| 95 | else if (n >= tns->cnt) |
| 96 | tns->cnt = n + 1; |
| 97 | |
| 98 | t = &tns->thumbs[n]; |
| 99 | |
| 100 | if (t->im) { |
| 101 | imlib_context_set_image(t->im); |
| 102 | imlib_free_image(); |
| 103 | } |
| 104 | |
| 105 | if ((use_cache = tns_cache_enabled())) { |
| 106 | if ((im = tns_cache_load(filename))) |
| 107 | cached = 1; |
| 108 | } |
| 109 | |
| 110 | if (cached || (im = imlib_load_image(filename))) |
| 111 | imlib_context_set_image(im); |
| 112 | else |
| 113 | imlib_context_set_image(im_invalid); |
| 114 | |
| 115 | w = imlib_image_get_width(); |
| 116 | h = imlib_image_get_height(); |
| 117 | |
| 118 | if (im) { |
| 119 | t->filename = filename; |
| 120 | zw = (float) THUMB_SIZE / (float) w; |
| 121 | zh = (float) THUMB_SIZE / (float) h; |
| 122 | z = MIN(zw, zh); |
| 123 | } else { |
| 124 | t->filename = NULL; |
| 125 | z = 1.0; |
| 126 | } |
| 127 | |
| 128 | t->w = z * w; |
| 129 | t->h = z * h; |
| 130 | |
| 131 | imlib_context_set_anti_alias(1); |
| 132 | if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h))) |
| 133 | die("could not allocate memory"); |
| 134 | if (im) |
| 135 | imlib_free_image_and_decache(); |
| 136 | if (use_cache && !cached) |
| 137 | tns_cache_write(t, False); |
| 138 | |
| 139 | tns->dirty = 1; |
| 140 | } |
| 141 | |
| 142 | void tns_check_view(tns_t *tns, Bool scrolled) { |
| 143 | int r; |
| 144 | |
| 145 | if (!tns) |
| 146 | return; |
| 147 | |
| 148 | tns->first -= tns->first % tns->cols; |
| 149 | r = tns->sel % tns->cols; |
| 150 | |
| 151 | if (scrolled) { |
| 152 | /* move selection into visible area */ |
| 153 | if (tns->sel >= tns->first + tns->cols * tns->rows) |
| 154 | tns->sel = tns->first + r + tns->cols * (tns->rows - 1); |
| 155 | else if (tns->sel < tns->first) |
| 156 | tns->sel = tns->first + r; |
| 157 | } else { |
| 158 | /* scroll to selection */ |
| 159 | if (tns->first + tns->cols * tns->rows <= tns->sel) { |
| 160 | tns->first = tns->sel - r - tns->cols * (tns->rows - 1); |
| 161 | tns->dirty = 1; |
| 162 | } else if (tns->first > tns->sel) { |
| 163 | tns->first = tns->sel - r; |
| 164 | tns->dirty = 1; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void tns_render(tns_t *tns, win_t *win) { |
| 170 | int i, cnt, r, x, y; |
| 171 | thumb_t *t; |
| 172 | |
| 173 | if (!tns || !tns->dirty || !win) |
| 174 | return; |
| 175 | |
| 176 | win_clear(win); |
| 177 | imlib_context_set_drawable(win->pm); |
| 178 | |
| 179 | tns->cols = MAX(1, win->w / thumb_dim); |
| 180 | tns->rows = MAX(1, win->h / thumb_dim); |
| 181 | |
| 182 | if (tns->cnt < tns->cols * tns->rows) { |
| 183 | tns->first = 0; |
| 184 | cnt = tns->cnt; |
| 185 | } else { |
| 186 | tns_check_view(tns, False); |
| 187 | cnt = tns->cols * tns->rows; |
| 188 | if ((r = tns->first + cnt - tns->cnt) >= tns->cols) |
| 189 | tns->first -= r - r % tns->cols; |
| 190 | if (r > 0) |
| 191 | cnt -= r % tns->cols; |
| 192 | } |
| 193 | |
| 194 | r = cnt % tns->cols ? 1 : 0; |
| 195 | tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5; |
| 196 | tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5; |
| 197 | |
| 198 | for (i = 0; i < cnt; ++i) { |
| 199 | t = &tns->thumbs[tns->first + i]; |
| 200 | t->x = x + (THUMB_SIZE - t->w) / 2; |
| 201 | t->y = y + (THUMB_SIZE - t->h) / 2; |
| 202 | imlib_context_set_image(t->im); |
| 203 | imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h, |
| 204 | t->x, t->y, t->w, t->h); |
| 205 | if ((i + 1) % tns->cols == 0) { |
| 206 | x = tns->x; |
| 207 | y += thumb_dim; |
| 208 | } else { |
| 209 | x += thumb_dim; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | tns->dirty = 0; |
| 214 | tns_highlight(tns, win, tns->sel, True); |
| 215 | } |
| 216 | |
| 217 | void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) { |
| 218 | thumb_t *t; |
| 219 | unsigned long col; |
| 220 | |
| 221 | if (!tns || !win) |
| 222 | return; |
| 223 | |
| 224 | if (n >= 0 && n < tns->cnt) { |
| 225 | t = &tns->thumbs[n]; |
| 226 | |
| 227 | if (hl) |
| 228 | col = win->selcol; |
| 229 | else if (win->fullscreen) |
| 230 | col = win->black; |
| 231 | else |
| 232 | col = win->bgcol; |
| 233 | |
| 234 | win_draw_rect(win, win->pm, t->x - 2, t->y - 2, t->w + 4, t->h + 4, |
| 235 | False, 2, col); |
| 236 | } |
| 237 | |
| 238 | win_draw(win); |
| 239 | } |
| 240 | |
| 241 | int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) { |
| 242 | int old; |
| 243 | |
| 244 | if (!tns || !win) |
| 245 | return 0; |
| 246 | |
| 247 | old = tns->sel; |
| 248 | |
| 249 | switch (dir) { |
| 250 | case TNS_LEFT: |
| 251 | if (tns->sel > 0) |
| 252 | --tns->sel; |
| 253 | break; |
| 254 | case TNS_RIGHT: |
| 255 | if (tns->sel < tns->cnt - 1) |
| 256 | ++tns->sel; |
| 257 | break; |
| 258 | case TNS_UP: |
| 259 | if (tns->sel >= tns->cols) |
| 260 | tns->sel -= tns->cols; |
| 261 | break; |
| 262 | case TNS_DOWN: |
| 263 | if (tns->sel + tns->cols < tns->cnt) |
| 264 | tns->sel += tns->cols; |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | if (tns->sel != old) { |
| 269 | tns_highlight(tns, win, old, False); |
| 270 | tns_check_view(tns, False); |
| 271 | if (!tns->dirty) |
| 272 | tns_highlight(tns, win, tns->sel, True); |
| 273 | } |
| 274 | |
| 275 | return tns->sel != old; |
| 276 | } |
| 277 | |
| 278 | int tns_scroll(tns_t *tns, tnsdir_t dir) { |
| 279 | int old; |
| 280 | |
| 281 | if (!tns) |
| 282 | return 0; |
| 283 | |
| 284 | old = tns->first; |
| 285 | |
| 286 | if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) { |
| 287 | tns->first += tns->cols; |
| 288 | tns_check_view(tns, True); |
| 289 | tns->dirty = 1; |
| 290 | } else if (dir == TNS_UP && tns->first >= tns->cols) { |
| 291 | tns->first -= tns->cols; |
| 292 | tns_check_view(tns, True); |
| 293 | tns->dirty = 1; |
| 294 | } |
| 295 | |
| 296 | return tns->first != old; |
| 297 | } |
| 298 | |
| 299 | int tns_translate(tns_t *tns, int x, int y) { |
| 300 | int n; |
| 301 | thumb_t *t; |
| 302 | |
| 303 | if (!tns || x < tns->x || y < tns->y) |
| 304 | return -1; |
| 305 | |
| 306 | n = tns->first + (y - tns->y) / thumb_dim * tns->cols + |
| 307 | (x - tns->x) / thumb_dim; |
| 308 | |
| 309 | if (n < tns->cnt) { |
| 310 | t = &tns->thumbs[n]; |
| 311 | if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h) |
| 312 | return n; |
| 313 | } |
| 314 | |
| 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | /* thumbnail caching */ |
| 320 | |
| 321 | int tns_cache_enabled() { |
| 322 | struct stat stats; |
| 323 | |
| 324 | return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) && |
| 325 | !access(cache_dir, W_OK); |
| 326 | } |
| 327 | |
| 328 | char* tns_cache_filename(const char *filename) { |
| 329 | size_t len; |
| 330 | int i; |
| 331 | char *cfile, *abspath; |
| 332 | |
| 333 | if (!cache_dir || !filename) |
| 334 | return NULL; |
| 335 | |
| 336 | if (*filename != '/') { |
| 337 | if (!(abspath = absolute_path(filename))) |
| 338 | return NULL; |
| 339 | } else { |
| 340 | abspath = (char*) s_malloc(strlen(filename) + 1); |
| 341 | strcpy(abspath, filename); |
| 342 | } |
| 343 | |
| 344 | len = strlen(abspath); |
| 345 | for (i = 0; i < len; ++i) { |
| 346 | if (abspath[i] == '/') |
| 347 | abspath[i] = '%'; |
| 348 | } |
| 349 | |
| 350 | len += strlen(cache_dir) + 6; |
| 351 | cfile = (char*) s_malloc(len); |
| 352 | snprintf(cfile, len, "%s/%s.png", cache_dir, abspath + 1); |
| 353 | |
| 354 | free(abspath); |
| 355 | |
| 356 | return cfile; |
| 357 | } |
| 358 | |
| 359 | Imlib_Image* tns_cache_load(const char *filename) { |
| 360 | char *cfile; |
| 361 | struct stat cstats, fstats; |
| 362 | Imlib_Image *im = NULL; |
| 363 | |
| 364 | if (!filename || stat(filename, &fstats)) |
| 365 | return NULL; |
| 366 | |
| 367 | if ((cfile = tns_cache_filename(filename))) { |
| 368 | if (!stat(cfile, &cstats) && |
| 369 | cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec && |
| 370 | cstats.st_mtim.tv_nsec == fstats.st_mtim.tv_nsec) |
| 371 | { |
| 372 | printf("cache hit: %s\n", filename); |
| 373 | im = imlib_load_image(cfile); |
| 374 | } else |
| 375 | printf("cache MISS: %s\n", filename); |
| 376 | free(cfile); |
| 377 | } |
| 378 | |
| 379 | return im; |
| 380 | } |
| 381 | |
| 382 | void tns_cache_write(thumb_t *t, Bool force) { |
| 383 | char *cfile; |
| 384 | struct stat cstats, fstats; |
| 385 | struct timeval times[2]; |
| 386 | Imlib_Load_Error err; |
| 387 | |
| 388 | if (!t || !t->im || !t->filename) |
| 389 | return; |
| 390 | if (stat(t->filename, &fstats)) |
| 391 | return; |
| 392 | |
| 393 | if ((cfile = tns_cache_filename(t->filename))) { |
| 394 | if (force || stat(cfile, &cstats) || |
| 395 | cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec || |
| 396 | cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec) |
| 397 | { |
| 398 | imlib_context_set_image(t->im); |
| 399 | imlib_image_set_format("png"); |
| 400 | imlib_save_image_with_error_return(cfile, &err); |
| 401 | |
| 402 | if (err) { |
| 403 | warn("could not cache thumbnail: %s", t->filename); |
| 404 | } else { |
| 405 | TIMESPEC_TO_TIMEVAL(×[0], &fstats.st_atim); |
| 406 | TIMESPEC_TO_TIMEVAL(×[1], &fstats.st_mtim); |
| 407 | utimes(cfile, times); |
| 408 | printf("thumbnail cache file written: %s\n", t->filename); |
| 409 | } |
| 410 | } |
| 411 | free(cfile); |
| 412 | } |
| 413 | } |