A Simple X Image Viewer
 
 
 
 
 
 

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