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