A Simple X Image Viewer
 
 
 
 
 
 

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