You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

595 lines
14 KiB

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