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