A Simple X Image Viewer
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.
 
 
 
 
 
 

606 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. #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) + 6;
  44. cfile = (char*) s_malloc(len);
  45. snprintf(cfile, len, "%s/%s.jpg", 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. imlib_image_set_format("jpg");
  91. imlib_save_image_with_error_return(cfile, &err);
  92. }
  93. if (err == 0) {
  94. times.actime = fstats.st_atime;
  95. times.modtime = fstats.st_mtime;
  96. utime(cfile, &times);
  97. }
  98. }
  99. free(cfile);
  100. }
  101. }
  102. void tns_clean_cache(tns_t *tns)
  103. {
  104. int dirlen;
  105. bool delete;
  106. char *cfile, *filename, *tpos;
  107. r_dir_t dir;
  108. if (cache_dir == NULL)
  109. return;
  110. if (r_opendir(&dir, cache_dir) < 0) {
  111. warn("could not open thumbnail cache directory: %s", cache_dir);
  112. return;
  113. }
  114. dirlen = strlen(cache_dir);
  115. while ((cfile = r_readdir(&dir)) != NULL) {
  116. filename = cfile + dirlen;
  117. delete = false;
  118. if ((tpos = strrchr(filename, '.')) != NULL) {
  119. *tpos = '\0';
  120. if (access(filename, F_OK) < 0)
  121. delete = true;
  122. *tpos = '.';
  123. }
  124. if (delete) {
  125. if (unlink(cfile) < 0)
  126. warn("could not delete cache file: %s", cfile);
  127. }
  128. free(cfile);
  129. }
  130. r_closedir(&dir);
  131. }
  132. void tns_init(tns_t *tns, const fileinfo_t *files, int cnt, int *sel, win_t *win)
  133. {
  134. int len;
  135. const char *homedir, *dsuffix = "";
  136. if (tns == NULL)
  137. return;
  138. if (cnt > 0) {
  139. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  140. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  141. } else {
  142. tns->thumbs = NULL;
  143. }
  144. tns->files = files;
  145. tns->cnt = cnt;
  146. tns->loadnext = tns->first = tns->end = tns->r_first = tns->r_end = 0;
  147. tns->sel = sel;
  148. tns->win = win;
  149. tns->dirty = false;
  150. tns->zl = 0;
  151. tns_zoom(tns, 1);
  152. if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
  153. homedir = getenv("HOME");
  154. dsuffix = "/.cache";
  155. }
  156. if (homedir != NULL) {
  157. free(cache_dir);
  158. len = strlen(homedir) + strlen(dsuffix) + 6;
  159. cache_dir = (char*) s_malloc(len);
  160. snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
  161. } else {
  162. warn("could not locate thumbnail cache directory");
  163. }
  164. }
  165. void tns_free(tns_t *tns)
  166. {
  167. int i;
  168. if (tns == NULL)
  169. return;
  170. if (tns->thumbs != NULL) {
  171. for (i = 0; i < tns->cnt; i++) {
  172. if (tns->thumbs[i].im != NULL) {
  173. imlib_context_set_image(tns->thumbs[i].im);
  174. imlib_free_image();
  175. }
  176. }
  177. free(tns->thumbs);
  178. tns->thumbs = NULL;
  179. }
  180. free(cache_dir);
  181. cache_dir = NULL;
  182. }
  183. Imlib_Image tns_scale_down(Imlib_Image im, int dim)
  184. {
  185. int w, h;
  186. float z, zw, zh;
  187. imlib_context_set_image(im);
  188. w = imlib_image_get_width();
  189. h = imlib_image_get_height();
  190. zw = (float) dim / (float) w;
  191. zh = (float) dim / (float) h;
  192. z = MIN(zw, zh);
  193. z = MIN(z, 1.0);
  194. if (z < 1.0) {
  195. imlib_context_set_anti_alias(1);
  196. im = imlib_create_cropped_scaled_image(0, 0, w, h, z * w, z * h);
  197. if (im == NULL)
  198. die("could not allocate memory");
  199. imlib_free_image_and_decache();
  200. }
  201. return im;
  202. }
  203. bool tns_load(tns_t *tns, int n, bool force)
  204. {
  205. int w, h;
  206. bool cache_hit = false;
  207. float zw, zh;
  208. thumb_t *t;
  209. Imlib_Image im = NULL;
  210. const fileinfo_t *file;
  211. if (tns == NULL || tns->thumbs == NULL)
  212. return false;
  213. if (n < 0 || n >= tns->cnt)
  214. return false;
  215. file = &tns->files[n];
  216. if (file->name == NULL || file->path == NULL)
  217. return false;
  218. t = &tns->thumbs[n];
  219. if (t->im != NULL) {
  220. imlib_context_set_image(t->im);
  221. imlib_free_image();
  222. }
  223. if (!force && (im = tns_cache_load(file->path, &force)) != NULL) {
  224. cache_hit = true;
  225. } else {
  226. #if HAVE_LIBEXIF
  227. if (!force) {
  228. int pw = 0, ph = 0, x = 0, y = 0;
  229. bool err;
  230. ExifData *ed;
  231. ExifEntry *entry;
  232. ExifContent *ifd;
  233. ExifByteOrder byte_order;
  234. int tmpfd;
  235. char tmppath[] = "/tmp/sxiv-XXXXXX";
  236. Imlib_Image tmpim;
  237. if ((ed = exif_data_new_from_file(file->path)) != NULL &&
  238. ed->data != NULL && ed->size > 0)
  239. {
  240. if ((tmpfd = mkstemp(tmppath)) >= 0) {
  241. err = write(tmpfd, ed->data, ed->size) != ed->size;
  242. close(tmpfd);
  243. if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
  244. byte_order = exif_data_get_byte_order(ed);
  245. ifd = ed->ifd[EXIF_IFD_EXIF];
  246. entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
  247. if (entry != NULL)
  248. pw = exif_get_long(entry->data, byte_order);
  249. entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
  250. if (entry != NULL)
  251. ph = exif_get_long(entry->data, byte_order);
  252. imlib_context_set_image(tmpim);
  253. w = imlib_image_get_width();
  254. h = imlib_image_get_height();
  255. if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
  256. zw = (float) pw / (float) w;
  257. zh = (float) ph / (float) h;
  258. if (zw < zh) {
  259. pw /= zh;
  260. x = (w - pw) / 2;
  261. w = pw;
  262. } else if (zw > zh) {
  263. ph /= zw;
  264. y = (h - ph) / 2;
  265. h = ph;
  266. }
  267. }
  268. if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
  269. die("could not allocate memory");
  270. imlib_free_image_and_decache();
  271. }
  272. unlink(tmppath);
  273. }
  274. exif_data_unref(ed);
  275. }
  276. }
  277. #endif
  278. if (im == NULL && (access(file->path, R_OK) < 0 ||
  279. (im = imlib_load_image(file->path)) == NULL))
  280. {
  281. warn("could not open image: %s", file->name);
  282. return false;
  283. }
  284. }
  285. if (!cache_hit) {
  286. #if HAVE_LIBEXIF
  287. imlib_context_set_image(im);
  288. exif_auto_orientate(file);
  289. #endif
  290. im = tns_scale_down(im, thumb_sizes[ARRLEN(thumb_sizes)-1]);
  291. tns_cache_write(im, file->path, true);
  292. }
  293. t->im = tns_scale_down(im, thumb_sizes[tns->zl]);
  294. imlib_context_set_image(t->im);
  295. t->w = imlib_image_get_width();
  296. t->h = imlib_image_get_height();
  297. tns->dirty = true;
  298. return true;
  299. }
  300. void tns_unload(tns_t *tns, int n)
  301. {
  302. thumb_t *t;
  303. if (tns == NULL || tns->thumbs == NULL)
  304. return;
  305. if (n < 0 || n >= tns->cnt)
  306. return;
  307. t = &tns->thumbs[n];
  308. if (t->im != NULL) {
  309. imlib_context_set_image(t->im);
  310. imlib_free_image();
  311. t->im = NULL;
  312. }
  313. }
  314. void tns_check_view(tns_t *tns, bool scrolled)
  315. {
  316. int r;
  317. if (tns == NULL)
  318. return;
  319. tns->first -= tns->first % tns->cols;
  320. r = *tns->sel % tns->cols;
  321. if (scrolled) {
  322. /* move selection into visible area */
  323. if (*tns->sel >= tns->first + tns->cols * tns->rows)
  324. *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  325. else if (*tns->sel < tns->first)
  326. *tns->sel = tns->first + r;
  327. } else {
  328. /* scroll to selection */
  329. if (tns->first + tns->cols * tns->rows <= *tns->sel) {
  330. tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
  331. tns->dirty = true;
  332. } else if (tns->first > *tns->sel) {
  333. tns->first = *tns->sel - r;
  334. tns->dirty = true;
  335. }
  336. }
  337. }
  338. void tns_render(tns_t *tns)
  339. {
  340. thumb_t *t;
  341. win_t *win;
  342. int i, cnt, r, x, y;
  343. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  344. return;
  345. if (!tns->dirty)
  346. return;
  347. win = tns->win;
  348. win_clear(win);
  349. imlib_context_set_drawable(win->buf.pm);
  350. tns->cols = MAX(1, win->w / tns->dim);
  351. tns->rows = MAX(1, win->h / tns->dim);
  352. if (tns->cnt < tns->cols * tns->rows) {
  353. tns->first = 0;
  354. cnt = tns->cnt;
  355. } else {
  356. tns_check_view(tns, false);
  357. cnt = tns->cols * tns->rows;
  358. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  359. tns->first -= r - r % tns->cols;
  360. if (r > 0)
  361. cnt -= r % tns->cols;
  362. }
  363. r = cnt % tns->cols ? 1 : 0;
  364. tns->x = x = (win->w - MIN(cnt, tns->cols) * tns->dim) / 2 + tns->bw + 3;
  365. tns->y = y = (win->h - (cnt / tns->cols + r) * tns->dim) / 2 + tns->bw + 3;
  366. tns->loadnext = tns->cnt;
  367. tns->end = tns->first + cnt;
  368. for (i = tns->r_first; i < tns->r_end; i++) {
  369. if ((i < tns->first || i >= tns->end) && tns->thumbs[i].im != NULL)
  370. tns_unload(tns, i);
  371. }
  372. tns->r_first = tns->first;
  373. tns->r_end = tns->end;
  374. for (i = tns->first; i < tns->end; i++) {
  375. t = &tns->thumbs[i];
  376. if (t->im != NULL) {
  377. t->x = x + (thumb_sizes[tns->zl] - t->w) / 2;
  378. t->y = y + (thumb_sizes[tns->zl] - t->h) / 2;
  379. imlib_context_set_image(t->im);
  380. imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h);
  381. if (tns->files[i].marked)
  382. tns_mark(tns, i, true);
  383. } else {
  384. tns->loadnext = MIN(tns->loadnext, i);
  385. }
  386. if ((i + 1) % tns->cols == 0) {
  387. x = tns->x;
  388. y += tns->dim;
  389. } else {
  390. x += tns->dim;
  391. }
  392. }
  393. tns->dirty = false;
  394. tns_highlight(tns, *tns->sel, true);
  395. }
  396. void tns_mark(tns_t *tns, int n, bool mark)
  397. {
  398. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  399. return;
  400. if (n >= 0 && n < tns->cnt && tns->thumbs[n].im != NULL) {
  401. win_t *win = tns->win;
  402. thumb_t *t = &tns->thumbs[n];
  403. unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
  404. int x = t->x + t->w, y = t->y + t->h;
  405. win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
  406. win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col);
  407. if (mark)
  408. col = win->selcol;
  409. win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col);
  410. if (!mark && n == *tns->sel)
  411. tns_highlight(tns, n, true);
  412. }
  413. }
  414. void tns_highlight(tns_t *tns, int n, bool hl)
  415. {
  416. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  417. return;
  418. if (n >= 0 && n < tns->cnt && tns->thumbs[n].im != NULL) {
  419. win_t *win = tns->win;
  420. thumb_t *t = &tns->thumbs[n];
  421. unsigned long col;
  422. int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
  423. if (hl)
  424. col = win->selcol;
  425. else
  426. col = win->fullscreen ? win->fscol : win->bgcol;
  427. win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
  428. false, tns->bw, col);
  429. if (tns->files[n].marked)
  430. tns_mark(tns, n, true);
  431. }
  432. }
  433. bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
  434. {
  435. int old, max;
  436. if (tns == NULL || tns->thumbs == NULL)
  437. return false;
  438. old = *tns->sel;
  439. cnt = cnt > 1 ? cnt : 1;
  440. switch (dir) {
  441. case DIR_UP:
  442. *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
  443. break;
  444. case DIR_DOWN:
  445. max = tns->cols * ((tns->cnt - 1) / tns->cols) +
  446. MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
  447. *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
  448. break;
  449. case DIR_LEFT:
  450. *tns->sel = MAX(*tns->sel - cnt, 0);
  451. break;
  452. case DIR_RIGHT:
  453. *tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1);
  454. break;
  455. }
  456. if (*tns->sel != old) {
  457. tns_highlight(tns, old, false);
  458. tns_check_view(tns, false);
  459. if (!tns->dirty)
  460. tns_highlight(tns, *tns->sel, true);
  461. }
  462. return *tns->sel != old;
  463. }
  464. bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
  465. {
  466. int d, max, old;
  467. if (tns == NULL)
  468. return false;
  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. if (tns == NULL || tns->thumbs == NULL)
  489. return false;
  490. oldzl = tns->zl;
  491. tns->zl += -(d < 0) + (d > 0);
  492. tns->zl = MAX(tns->zl, 0);
  493. tns->zl = MIN(tns->zl, ARRLEN(thumb_sizes)-1);
  494. tns->bw = ((thumb_sizes[tns->zl] - 1) >> 5) + 1;
  495. tns->dim = thumb_sizes[tns->zl] + 2 * tns->bw + 6;
  496. if (tns->zl != oldzl) {
  497. for (i = 0; i < tns->cnt; i++)
  498. tns_unload(tns, i);
  499. tns->dirty = true;
  500. }
  501. return tns->zl != oldzl;
  502. }
  503. int tns_translate(tns_t *tns, int x, int y)
  504. {
  505. int n;
  506. if (tns == NULL || tns->thumbs == NULL)
  507. return -1;
  508. if (x < tns->x || y < tns->y)
  509. return -1;
  510. n = tns->first + (y - tns->y) / tns->dim * tns->cols +
  511. (x - tns->x) / tns->dim;
  512. if (n >= tns->cnt)
  513. n = -1;
  514. return n;
  515. }