| 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 | |
| 22 | #include <stdio.h> |
| 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 "thumbs.h" |
| 31 | #include "util.h" |
| 32 | #include "config.h" |
| 33 | |
| 34 | #if HAVE_LIBEXIF |
| 35 | #include <libexif/exif-data.h> |
| 36 | void exif_auto_orientate(const fileinfo_t*); |
| 37 | #endif |
| 38 | |
| 39 | static const int thumb_dim = THUMB_SIZE + 10; |
| 40 | |
| 41 | static char *cache_dir = NULL; |
| 42 | |
| 43 | bool tns_cache_enabled(void) |
| 44 | { |
| 45 | struct stat stats; |
| 46 | |
| 47 | return cache_dir != NULL && stat(cache_dir, &stats) == 0 && |
| 48 | S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0; |
| 49 | } |
| 50 | |
| 51 | char* tns_cache_filepath(const char *filepath) |
| 52 | { |
| 53 | size_t len; |
| 54 | char *cfile = NULL; |
| 55 | |
| 56 | if (cache_dir == NULL || filepath == NULL || *filepath != '/') |
| 57 | return NULL; |
| 58 | |
| 59 | if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) { |
| 60 | /* don't cache images inside the cache directory! */ |
| 61 | len = strlen(cache_dir) + strlen(filepath) + 6; |
| 62 | cfile = (char*) s_malloc(len); |
| 63 | snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1); |
| 64 | } |
| 65 | return cfile; |
| 66 | } |
| 67 | |
| 68 | Imlib_Image tns_cache_load(const char *filepath) |
| 69 | { |
| 70 | char *cfile; |
| 71 | struct stat cstats, fstats; |
| 72 | Imlib_Image im = NULL; |
| 73 | |
| 74 | if (filepath == NULL) |
| 75 | return NULL; |
| 76 | if (stat(filepath, &fstats) < 0) |
| 77 | return NULL; |
| 78 | |
| 79 | if ((cfile = tns_cache_filepath(filepath)) != NULL) { |
| 80 | if (stat(cfile, &cstats) == 0 && cstats.st_mtime == fstats.st_mtime) |
| 81 | im = imlib_load_image(cfile); |
| 82 | free(cfile); |
| 83 | } |
| 84 | return im; |
| 85 | } |
| 86 | |
| 87 | void tns_cache_write(thumb_t *t, bool force) |
| 88 | { |
| 89 | char *cfile, *dirend; |
| 90 | struct stat cstats, fstats; |
| 91 | struct utimbuf times; |
| 92 | Imlib_Load_Error err = 0; |
| 93 | |
| 94 | if (t == NULL || t->im == NULL) |
| 95 | return; |
| 96 | if (t->file == NULL || t->file->name == NULL || t->file->path == NULL) |
| 97 | return; |
| 98 | if (stat(t->file->path, &fstats) < 0) |
| 99 | return; |
| 100 | |
| 101 | if ((cfile = tns_cache_filepath(t->file->path)) != NULL) { |
| 102 | if (force || stat(cfile, &cstats) < 0 || |
| 103 | cstats.st_mtime != fstats.st_mtime) |
| 104 | { |
| 105 | if ((dirend = strrchr(cfile, '/')) != NULL) { |
| 106 | *dirend = '\0'; |
| 107 | err = r_mkdir(cfile); |
| 108 | *dirend = '/'; |
| 109 | } |
| 110 | |
| 111 | if (err == 0) { |
| 112 | imlib_context_set_image(t->im); |
| 113 | imlib_image_set_format("png"); |
| 114 | imlib_save_image_with_error_return(cfile, &err); |
| 115 | } |
| 116 | |
| 117 | if (err == 0) { |
| 118 | times.actime = fstats.st_atime; |
| 119 | times.modtime = fstats.st_mtime; |
| 120 | utime(cfile, ×); |
| 121 | } else { |
| 122 | warn("could not cache thumbnail: %s", t->file->name); |
| 123 | } |
| 124 | } |
| 125 | free(cfile); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void tns_clean_cache(tns_t *tns) |
| 130 | { |
| 131 | int dirlen; |
| 132 | bool delete; |
| 133 | char *cfile, *filename, *tpos; |
| 134 | r_dir_t dir; |
| 135 | |
| 136 | if (cache_dir == NULL) |
| 137 | return; |
| 138 | |
| 139 | if (r_opendir(&dir, cache_dir) < 0) { |
| 140 | warn("could not open thumbnail cache directory: %s", cache_dir); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | dirlen = strlen(cache_dir); |
| 145 | |
| 146 | while ((cfile = r_readdir(&dir)) != NULL) { |
| 147 | filename = cfile + dirlen; |
| 148 | delete = false; |
| 149 | |
| 150 | if ((tpos = strrchr(filename, '.')) != NULL) { |
| 151 | *tpos = '\0'; |
| 152 | if (access(filename, F_OK) < 0) |
| 153 | delete = true; |
| 154 | *tpos = '.'; |
| 155 | } |
| 156 | if (delete) { |
| 157 | if (unlink(cfile) < 0) |
| 158 | warn("could not delete cache file: %s", cfile); |
| 159 | } |
| 160 | free(cfile); |
| 161 | } |
| 162 | r_closedir(&dir); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | void tns_init(tns_t *tns, int cnt, win_t *win) |
| 167 | { |
| 168 | int len; |
| 169 | const char *homedir, *dsuffix = ""; |
| 170 | |
| 171 | if (tns == NULL) |
| 172 | return; |
| 173 | |
| 174 | if (cnt > 0) { |
| 175 | tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t)); |
| 176 | memset(tns->thumbs, 0, cnt * sizeof(thumb_t)); |
| 177 | } else { |
| 178 | tns->thumbs = NULL; |
| 179 | } |
| 180 | |
| 181 | tns->cap = cnt; |
| 182 | tns->cnt = tns->first = tns->sel = 0; |
| 183 | tns->win = win; |
| 184 | tns->dirty = false; |
| 185 | |
| 186 | if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') { |
| 187 | homedir = getenv("HOME"); |
| 188 | dsuffix = "/.cache"; |
| 189 | } |
| 190 | if (homedir != NULL) { |
| 191 | if (cache_dir != NULL) |
| 192 | free(cache_dir); |
| 193 | len = strlen(homedir) + strlen(dsuffix) + 6; |
| 194 | cache_dir = (char*) s_malloc(len); |
| 195 | snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix); |
| 196 | } else { |
| 197 | warn("could not locate thumbnail cache directory"); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void tns_free(tns_t *tns) |
| 202 | { |
| 203 | int i; |
| 204 | |
| 205 | if (tns == NULL) |
| 206 | return; |
| 207 | |
| 208 | if (tns->thumbs != NULL) { |
| 209 | for (i = 0; i < tns->cnt; i++) { |
| 210 | if (tns->thumbs[i].im != NULL) { |
| 211 | imlib_context_set_image(tns->thumbs[i].im); |
| 212 | imlib_free_image(); |
| 213 | } |
| 214 | } |
| 215 | free(tns->thumbs); |
| 216 | tns->thumbs = NULL; |
| 217 | } |
| 218 | |
| 219 | if (cache_dir != NULL) { |
| 220 | free(cache_dir); |
| 221 | cache_dir = NULL; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | bool tns_load(tns_t *tns, int n, const fileinfo_t *file, |
| 226 | bool force, bool silent) |
| 227 | { |
| 228 | int w, h; |
| 229 | bool use_cache, cache_hit = false; |
| 230 | float z, zw, zh; |
| 231 | thumb_t *t; |
| 232 | Imlib_Image im = NULL; |
| 233 | |
| 234 | if (tns == NULL || tns->thumbs == NULL) |
| 235 | return false; |
| 236 | if (file == NULL || file->name == NULL || file->path == NULL) |
| 237 | return false; |
| 238 | if (n < 0 || n >= tns->cap) |
| 239 | return false; |
| 240 | |
| 241 | t = &tns->thumbs[n]; |
| 242 | t->file = file; |
| 243 | |
| 244 | if (t->im != NULL) { |
| 245 | imlib_context_set_image(t->im); |
| 246 | imlib_free_image(); |
| 247 | } |
| 248 | |
| 249 | if ((use_cache = tns_cache_enabled())) { |
| 250 | if (!force && (im = tns_cache_load(file->path)) != NULL) |
| 251 | cache_hit = true; |
| 252 | } |
| 253 | |
| 254 | if (!cache_hit) { |
| 255 | #if HAVE_LIBEXIF |
| 256 | int pw = 0, ph = 0, x = 0, y = 0; |
| 257 | bool err; |
| 258 | ExifData *ed; |
| 259 | ExifEntry *entry; |
| 260 | ExifContent *ifd; |
| 261 | ExifByteOrder byte_order; |
| 262 | int tmpfd; |
| 263 | char tmppath[] = "/tmp/sxiv-XXXXXX"; |
| 264 | Imlib_Image tmpim; |
| 265 | |
| 266 | if ((ed = exif_data_new_from_file(file->path)) != NULL && |
| 267 | ed->data != NULL && ed->size > 0) |
| 268 | { |
| 269 | if ((tmpfd = mkstemp(tmppath)) >= 0) { |
| 270 | err = write(tmpfd, ed->data, ed->size) != ed->size; |
| 271 | close(tmpfd); |
| 272 | |
| 273 | if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) { |
| 274 | byte_order = exif_data_get_byte_order(ed); |
| 275 | ifd = ed->ifd[EXIF_IFD_EXIF]; |
| 276 | entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION); |
| 277 | if (entry != NULL) |
| 278 | pw = exif_get_long(entry->data, byte_order); |
| 279 | entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION); |
| 280 | if (entry != NULL) |
| 281 | ph = exif_get_long(entry->data, byte_order); |
| 282 | |
| 283 | imlib_context_set_image(tmpim); |
| 284 | w = imlib_image_get_width(); |
| 285 | h = imlib_image_get_height(); |
| 286 | |
| 287 | if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) { |
| 288 | zw = (float) pw / (float) w; |
| 289 | zh = (float) ph / (float) h; |
| 290 | if (zw < zh) { |
| 291 | pw /= zh; |
| 292 | x = (w - pw) / 2; |
| 293 | w = pw; |
| 294 | } else if (zw > zh) { |
| 295 | ph /= zw; |
| 296 | y = (h - ph) / 2; |
| 297 | h = ph; |
| 298 | } |
| 299 | } |
| 300 | if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL) |
| 301 | die("could not allocate memory"); |
| 302 | imlib_free_image_and_decache(); |
| 303 | } |
| 304 | unlink(tmppath); |
| 305 | } |
| 306 | exif_data_unref(ed); |
| 307 | } |
| 308 | #endif |
| 309 | if (im == NULL && (access(file->path, R_OK) < 0 || |
| 310 | (im = imlib_load_image(file->path)) == NULL)) |
| 311 | { |
| 312 | if (!silent) |
| 313 | warn("could not open image: %s", file->name); |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | imlib_context_set_image(im); |
| 318 | imlib_context_set_anti_alias(1); |
| 319 | |
| 320 | #if HAVE_LIBEXIF |
| 321 | if (!cache_hit) |
| 322 | exif_auto_orientate(file); |
| 323 | #endif |
| 324 | |
| 325 | w = imlib_image_get_width(); |
| 326 | h = imlib_image_get_height(); |
| 327 | zw = (float) THUMB_SIZE / (float) w; |
| 328 | zh = (float) THUMB_SIZE / (float) h; |
| 329 | z = MIN(zw, zh); |
| 330 | z = MIN(z, 1.0); |
| 331 | t->w = z * w; |
| 332 | t->h = z * h; |
| 333 | |
| 334 | t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h); |
| 335 | if (t->im == NULL) |
| 336 | die("could not allocate memory"); |
| 337 | |
| 338 | imlib_free_image_and_decache(); |
| 339 | |
| 340 | if (use_cache && !cache_hit) |
| 341 | tns_cache_write(t, true); |
| 342 | |
| 343 | tns->dirty = true; |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | void tns_check_view(tns_t *tns, bool scrolled) |
| 348 | { |
| 349 | int r; |
| 350 | |
| 351 | if (tns == NULL) |
| 352 | return; |
| 353 | |
| 354 | tns->first -= tns->first % tns->cols; |
| 355 | r = tns->sel % tns->cols; |
| 356 | |
| 357 | if (scrolled) { |
| 358 | /* move selection into visible area */ |
| 359 | if (tns->sel >= tns->first + tns->cols * tns->rows) |
| 360 | tns->sel = tns->first + r + tns->cols * (tns->rows - 1); |
| 361 | else if (tns->sel < tns->first) |
| 362 | tns->sel = tns->first + r; |
| 363 | } else { |
| 364 | /* scroll to selection */ |
| 365 | if (tns->first + tns->cols * tns->rows <= tns->sel) { |
| 366 | tns->first = tns->sel - r - tns->cols * (tns->rows - 1); |
| 367 | tns->dirty = true; |
| 368 | } else if (tns->first > tns->sel) { |
| 369 | tns->first = tns->sel - r; |
| 370 | tns->dirty = true; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | void tns_render(tns_t *tns) |
| 376 | { |
| 377 | thumb_t *t; |
| 378 | win_t *win; |
| 379 | int i, cnt, r, x, y; |
| 380 | |
| 381 | if (tns == NULL || tns->thumbs == NULL || tns->win == NULL) |
| 382 | return; |
| 383 | if (!tns->dirty) |
| 384 | return; |
| 385 | |
| 386 | win = tns->win; |
| 387 | win_clear(win); |
| 388 | imlib_context_set_drawable(win->pm); |
| 389 | |
| 390 | tns->cols = MAX(1, win->w / thumb_dim); |
| 391 | tns->rows = MAX(1, win->h / thumb_dim); |
| 392 | |
| 393 | if (tns->cnt < tns->cols * tns->rows) { |
| 394 | tns->first = 0; |
| 395 | cnt = tns->cnt; |
| 396 | } else { |
| 397 | tns_check_view(tns, false); |
| 398 | cnt = tns->cols * tns->rows; |
| 399 | if ((r = tns->first + cnt - tns->cnt) >= tns->cols) |
| 400 | tns->first -= r - r % tns->cols; |
| 401 | if (r > 0) |
| 402 | cnt -= r % tns->cols; |
| 403 | } |
| 404 | |
| 405 | r = cnt % tns->cols ? 1 : 0; |
| 406 | tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5; |
| 407 | tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5; |
| 408 | |
| 409 | for (i = 0; i < cnt; i++) { |
| 410 | t = &tns->thumbs[tns->first + i]; |
| 411 | t->x = x + (THUMB_SIZE - t->w) / 2; |
| 412 | t->y = y + (THUMB_SIZE - t->h) / 2; |
| 413 | imlib_context_set_image(t->im); |
| 414 | imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h, |
| 415 | t->x, t->y, t->w, t->h); |
| 416 | if (t->file->marked) |
| 417 | tns_mark(tns, tns->first + i, true); |
| 418 | if ((i + 1) % tns->cols == 0) { |
| 419 | x = tns->x; |
| 420 | y += thumb_dim; |
| 421 | } else { |
| 422 | x += thumb_dim; |
| 423 | } |
| 424 | } |
| 425 | tns->dirty = false; |
| 426 | tns_highlight(tns, tns->sel, true); |
| 427 | } |
| 428 | |
| 429 | void tns_mark(tns_t *tns, int n, bool mark) |
| 430 | { |
| 431 | if (tns == NULL || tns->thumbs == NULL || tns->win == NULL) |
| 432 | return; |
| 433 | |
| 434 | if (n >= 0 && n < tns->cnt) { |
| 435 | unsigned long col; |
| 436 | thumb_t *t = &tns->thumbs[n]; |
| 437 | win_t *win = tns->win; |
| 438 | int x = t->x, y = t->y, w = t->w, h = t->h; |
| 439 | |
| 440 | if (mark || n == tns->sel) |
| 441 | col = win->selcol; |
| 442 | else if (win->fullscreen) |
| 443 | col = win->fscol; |
| 444 | else |
| 445 | col = win->bgcol; |
| 446 | |
| 447 | win_draw_rect(win, win->pm, x - 4, y - 4, 8, 2, true, 0, col); |
| 448 | win_draw_rect(win, win->pm, x - 4, y - 4, 2, 8, true, 0, col); |
| 449 | win_draw_rect(win, win->pm, x + w - 4, y - 4, 8, 2, true, 0, col); |
| 450 | win_draw_rect(win, win->pm, x + w + 2, y - 4, 2, 8, true, 0, col); |
| 451 | win_draw_rect(win, win->pm, x - 4, y + h + 2, 8, 2, true, 0, col); |
| 452 | win_draw_rect(win, win->pm, x - 4, y + h - 4, 2, 8, true, 0, col); |
| 453 | win_draw_rect(win, win->pm, x + w - 4, y + h + 2, 8, 2, true, 0, col); |
| 454 | win_draw_rect(win, win->pm, x + w + 2, y + h - 4, 2, 8, true, 0, col); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void tns_highlight(tns_t *tns, int n, bool hl) |
| 459 | { |
| 460 | if (tns == NULL || tns->thumbs == NULL || tns->win == NULL) |
| 461 | return; |
| 462 | |
| 463 | if (n >= 0 && n < tns->cnt) { |
| 464 | unsigned long col; |
| 465 | thumb_t *t = &tns->thumbs[n]; |
| 466 | win_t *win = tns->win; |
| 467 | |
| 468 | if (hl) |
| 469 | col = win->selcol; |
| 470 | else if (win->fullscreen) |
| 471 | col = win->fscol; |
| 472 | else |
| 473 | col = win->bgcol; |
| 474 | |
| 475 | win_draw_rect(win, win->pm, t->x - 3, t->y - 3, t->w + 6, t->h + 6, |
| 476 | false, 2, col); |
| 477 | |
| 478 | if (!hl && t->file->marked) |
| 479 | tns_mark(tns, n, true); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | bool tns_move_selection(tns_t *tns, direction_t dir, int cnt) |
| 484 | { |
| 485 | int old, max; |
| 486 | |
| 487 | if (tns == NULL || tns->thumbs == NULL) |
| 488 | return false; |
| 489 | |
| 490 | old = tns->sel; |
| 491 | cnt = cnt > 1 ? cnt : 1; |
| 492 | |
| 493 | switch (dir) { |
| 494 | case DIR_UP: |
| 495 | tns->sel = MAX(tns->sel - cnt * tns->cols, tns->sel % tns->cols); |
| 496 | break; |
| 497 | case DIR_DOWN: |
| 498 | max = tns->cols * ((tns->cnt - 1) / tns->cols) + |
| 499 | MIN((tns->cnt - 1) % tns->cols, tns->sel % tns->cols); |
| 500 | tns->sel = MIN(tns->sel + cnt * tns->cols, max); |
| 501 | break; |
| 502 | case DIR_LEFT: |
| 503 | tns->sel = MAX(tns->sel - cnt, 0); |
| 504 | break; |
| 505 | case DIR_RIGHT: |
| 506 | tns->sel = MIN(tns->sel + cnt, tns->cnt - 1); |
| 507 | break; |
| 508 | } |
| 509 | |
| 510 | if (tns->sel != old) { |
| 511 | tns_highlight(tns, old, false); |
| 512 | tns_check_view(tns, false); |
| 513 | if (!tns->dirty) |
| 514 | tns_highlight(tns, tns->sel, true); |
| 515 | } |
| 516 | return tns->sel != old; |
| 517 | } |
| 518 | |
| 519 | bool tns_scroll(tns_t *tns, direction_t dir, bool screen) |
| 520 | { |
| 521 | int d, max, old; |
| 522 | |
| 523 | if (tns == NULL) |
| 524 | return false; |
| 525 | |
| 526 | old = tns->first; |
| 527 | d = tns->cols * (screen ? tns->rows : 1); |
| 528 | |
| 529 | if (dir == DIR_DOWN) { |
| 530 | max = tns->cnt - tns->cols * tns->rows; |
| 531 | if (tns->cnt % tns->cols != 0) |
| 532 | max += tns->cols - tns->cnt % tns->cols; |
| 533 | tns->first = MIN(tns->first + d, max); |
| 534 | } else if (dir == DIR_UP) { |
| 535 | tns->first = MAX(tns->first - d, 0); |
| 536 | } |
| 537 | |
| 538 | if (tns->first != old) { |
| 539 | tns_check_view(tns, true); |
| 540 | tns->dirty = true; |
| 541 | } |
| 542 | return tns->first != old; |
| 543 | } |
| 544 | |
| 545 | int tns_translate(tns_t *tns, int x, int y) |
| 546 | { |
| 547 | int n; |
| 548 | |
| 549 | if (tns == NULL || tns->thumbs == NULL) |
| 550 | return -1; |
| 551 | if (x < tns->x || y < tns->y) |
| 552 | return -1; |
| 553 | |
| 554 | n = tns->first + (y - tns->y) / thumb_dim * tns->cols + |
| 555 | (x - tns->x) / thumb_dim; |
| 556 | if (n >= tns->cnt) |
| 557 | n = -1; |
| 558 | |
| 559 | return n; |
| 560 | } |