A Simple X Image Viewer
 
 
 
 
 
 

601 rinda
13 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. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <utime.h>
  26. #include "thumbs.h"
  27. #include "util.h"
  28. #include "config.h"
  29. #if HAVE_LIBEXIF
  30. #include <libexif/exif-data.h>
  31. void exif_auto_orientate(const fileinfo_t*);
  32. #endif
  33. static char *cache_dir;
  34. static const int thumb_size[] = { 32, 64, 96, 128, 160 };
  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->zl = 1;
  150. tns->dirty = false;
  151. if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
  152. homedir = getenv("HOME");
  153. dsuffix = "/.cache";
  154. }
  155. if (homedir != NULL) {
  156. free(cache_dir);
  157. len = strlen(homedir) + strlen(dsuffix) + 6;
  158. cache_dir = (char*) s_malloc(len);
  159. snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
  160. } else {
  161. warn("could not locate thumbnail cache directory");
  162. }
  163. }
  164. void tns_free(tns_t *tns)
  165. {
  166. int i;
  167. if (tns == NULL)
  168. return;
  169. if (tns->thumbs != NULL) {
  170. for (i = 0; i < tns->cnt; i++) {
  171. if (tns->thumbs[i].im != NULL) {
  172. imlib_context_set_image(tns->thumbs[i].im);
  173. imlib_free_image();
  174. }
  175. }
  176. free(tns->thumbs);
  177. tns->thumbs = NULL;
  178. }
  179. free(cache_dir);
  180. cache_dir = NULL;
  181. }
  182. Imlib_Image tns_scale_down(Imlib_Image im, int dim)
  183. {
  184. int w, h;
  185. float z, zw, zh;
  186. imlib_context_set_image(im);
  187. w = imlib_image_get_width();
  188. h = imlib_image_get_height();
  189. zw = (float) dim / (float) w;
  190. zh = (float) dim / (float) h;
  191. z = MIN(zw, zh);
  192. z = MIN(z, 1.0);
  193. if (z < 1.0) {
  194. imlib_context_set_anti_alias(1);
  195. im = imlib_create_cropped_scaled_image(0, 0, w, h, z * w, z * h);
  196. if (im == NULL)
  197. die("could not allocate memory");
  198. imlib_free_image_and_decache();
  199. }
  200. return im;
  201. }
  202. bool tns_load(tns_t *tns, int n, bool force)
  203. {
  204. int w, h;
  205. bool cache_hit = false;
  206. float zw, zh;
  207. thumb_t *t;
  208. Imlib_Image im = NULL;
  209. const fileinfo_t *file;
  210. if (tns == NULL || tns->thumbs == NULL)
  211. return false;
  212. if (n < 0 || n >= tns->cnt)
  213. return false;
  214. file = &tns->files[n];
  215. if (file->name == NULL || file->path == NULL)
  216. return false;
  217. t = &tns->thumbs[n];
  218. if (t->im != NULL) {
  219. imlib_context_set_image(t->im);
  220. imlib_free_image();
  221. }
  222. if (!force && (im = tns_cache_load(file->path, &force)) != NULL) {
  223. cache_hit = true;
  224. } else {
  225. #if HAVE_LIBEXIF
  226. if (!force) {
  227. int pw = 0, ph = 0, x = 0, y = 0;
  228. bool err;
  229. ExifData *ed;
  230. ExifEntry *entry;
  231. ExifContent *ifd;
  232. ExifByteOrder byte_order;
  233. int tmpfd;
  234. char tmppath[] = "/tmp/sxiv-XXXXXX";
  235. Imlib_Image tmpim;
  236. if ((ed = exif_data_new_from_file(file->path)) != NULL &&
  237. ed->data != NULL && ed->size > 0)
  238. {
  239. if ((tmpfd = mkstemp(tmppath)) >= 0) {
  240. err = write(tmpfd, ed->data, ed->size) != ed->size;
  241. close(tmpfd);
  242. if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
  243. byte_order = exif_data_get_byte_order(ed);
  244. ifd = ed->ifd[EXIF_IFD_EXIF];
  245. entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
  246. if (entry != NULL)
  247. pw = exif_get_long(entry->data, byte_order);
  248. entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
  249. if (entry != NULL)
  250. ph = exif_get_long(entry->data, byte_order);
  251. imlib_context_set_image(tmpim);
  252. w = imlib_image_get_width();
  253. h = imlib_image_get_height();
  254. if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
  255. zw = (float) pw / (float) w;
  256. zh = (float) ph / (float) h;
  257. if (zw < zh) {
  258. pw /= zh;
  259. x = (w - pw) / 2;
  260. w = pw;
  261. } else if (zw > zh) {
  262. ph /= zw;
  263. y = (h - ph) / 2;
  264. h = ph;
  265. }
  266. }
  267. if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
  268. die("could not allocate memory");
  269. imlib_free_image_and_decache();
  270. }
  271. unlink(tmppath);
  272. }
  273. exif_data_unref(ed);
  274. }
  275. }
  276. #endif
  277. if (im == NULL && (access(file->path, R_OK) < 0 ||
  278. (im = imlib_load_image(file->path)) == NULL))
  279. {
  280. warn("could not open image: %s", file->name);
  281. return false;
  282. }
  283. }
  284. if (!cache_hit) {
  285. #if HAVE_LIBEXIF
  286. imlib_context_set_image(im);
  287. exif_auto_orientate(file);
  288. #endif
  289. im = tns_scale_down(im, thumb_size[ARRLEN(thumb_size)-1]);
  290. tns_cache_write(im, file->path, true);
  291. }
  292. t->im = tns_scale_down(im, thumb_size[tns->zl]);
  293. imlib_context_set_image(t->im);
  294. t->w = imlib_image_get_width();
  295. t->h = imlib_image_get_height();
  296. tns->dirty = true;
  297. return true;
  298. }
  299. void tns_unload(tns_t *tns, int n)
  300. {
  301. thumb_t *t;
  302. if (tns == NULL || tns->thumbs == NULL)
  303. return;
  304. if (n < 0 || n >= tns->cnt)
  305. return;
  306. t = &tns->thumbs[n];
  307. if (t->im != NULL) {
  308. imlib_context_set_image(t->im);
  309. imlib_free_image();
  310. t->im = NULL;
  311. }
  312. }
  313. void tns_check_view(tns_t *tns, bool scrolled)
  314. {
  315. int r;
  316. if (tns == NULL)
  317. return;
  318. tns->first -= tns->first % tns->cols;
  319. r = *tns->sel % tns->cols;
  320. if (scrolled) {
  321. /* move selection into visible area */
  322. if (*tns->sel >= tns->first + tns->cols * tns->rows)
  323. *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  324. else if (*tns->sel < tns->first)
  325. *tns->sel = tns->first + r;
  326. } else {
  327. /* scroll to selection */
  328. if (tns->first + tns->cols * tns->rows <= *tns->sel) {
  329. tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
  330. tns->dirty = true;
  331. } else if (tns->first > *tns->sel) {
  332. tns->first = *tns->sel - r;
  333. tns->dirty = true;
  334. }
  335. }
  336. }
  337. void tns_render(tns_t *tns)
  338. {
  339. thumb_t *t;
  340. win_t *win;
  341. int i, cnt, r, x, y;
  342. int thumb_dim;
  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. thumb_dim = thumb_size[tns->zl] + 10;
  351. tns->cols = MAX(1, win->w / thumb_dim);
  352. tns->rows = MAX(1, win->h / thumb_dim);
  353. if (tns->cnt < tns->cols * tns->rows) {
  354. tns->first = 0;
  355. cnt = tns->cnt;
  356. } else {
  357. tns_check_view(tns, false);
  358. cnt = tns->cols * tns->rows;
  359. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  360. tns->first -= r - r % tns->cols;
  361. if (r > 0)
  362. cnt -= r % tns->cols;
  363. }
  364. r = cnt % tns->cols ? 1 : 0;
  365. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  366. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  367. tns->loadnext = tns->cnt;
  368. tns->end = tns->first + cnt;
  369. for (i = tns->r_first; i < tns->r_end; i++) {
  370. if ((i < tns->first || i >= tns->end) && tns->thumbs[i].im != NULL)
  371. tns_unload(tns, i);
  372. }
  373. tns->r_first = tns->first;
  374. tns->r_end = tns->end;
  375. for (i = tns->first; i < tns->end; i++) {
  376. t = &tns->thumbs[i];
  377. if (t->im != NULL) {
  378. t->x = x + (thumb_size[tns->zl] - t->w) / 2;
  379. t->y = y + (thumb_size[tns->zl] - t->h) / 2;
  380. imlib_context_set_image(t->im);
  381. imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h);
  382. if (tns->files[i].marked)
  383. tns_mark(tns, i, true);
  384. } else {
  385. tns->loadnext = MIN(tns->loadnext, i);
  386. }
  387. if ((i + 1) % tns->cols == 0) {
  388. x = tns->x;
  389. y += thumb_dim;
  390. } else {
  391. x += thumb_dim;
  392. }
  393. }
  394. tns->dirty = false;
  395. tns_highlight(tns, *tns->sel, true);
  396. }
  397. void tns_mark(tns_t *tns, int n, bool mark)
  398. {
  399. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  400. return;
  401. if (n >= 0 && n < tns->cnt && tns->thumbs[n].im != NULL) {
  402. win_t *win = tns->win;
  403. thumb_t *t = &tns->thumbs[n];
  404. unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
  405. int x = t->x + t->w, y = t->y + t->h;
  406. win_draw_rect(win, x - 1, y + 1, 1, 2, true, 1, col);
  407. win_draw_rect(win, x + 1, y - 1, 2, 1, true, 1, col);
  408. if (mark)
  409. col = win->selcol;
  410. win_draw_rect(win, x, y, 4, 4, true, 1, col);
  411. if (!mark && n == *tns->sel)
  412. tns_highlight(tns, n, true);
  413. }
  414. }
  415. void tns_highlight(tns_t *tns, int n, bool hl)
  416. {
  417. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  418. return;
  419. if (n >= 0 && n < tns->cnt && tns->thumbs[n].im != NULL) {
  420. win_t *win = tns->win;
  421. thumb_t *t = &tns->thumbs[n];
  422. unsigned long col;
  423. if (hl)
  424. col = win->selcol;
  425. else
  426. col = win->fullscreen ? win->fscol : win->bgcol;
  427. win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, false, 2, col);
  428. if (tns->files[n].marked)
  429. tns_mark(tns, n, true);
  430. }
  431. }
  432. bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
  433. {
  434. int old, max;
  435. if (tns == NULL || tns->thumbs == NULL)
  436. return false;
  437. old = *tns->sel;
  438. cnt = cnt > 1 ? cnt : 1;
  439. switch (dir) {
  440. case DIR_UP:
  441. *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
  442. break;
  443. case DIR_DOWN:
  444. max = tns->cols * ((tns->cnt - 1) / tns->cols) +
  445. MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
  446. *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
  447. break;
  448. case DIR_LEFT:
  449. *tns->sel = MAX(*tns->sel - cnt, 0);
  450. break;
  451. case DIR_RIGHT:
  452. *tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1);
  453. break;
  454. }
  455. if (*tns->sel != old) {
  456. tns_highlight(tns, old, false);
  457. tns_check_view(tns, false);
  458. if (!tns->dirty)
  459. tns_highlight(tns, *tns->sel, true);
  460. }
  461. return *tns->sel != old;
  462. }
  463. bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
  464. {
  465. int d, max, old;
  466. if (tns == NULL)
  467. return false;
  468. old = tns->first;
  469. d = tns->cols * (screen ? tns->rows : 1);
  470. if (dir == DIR_DOWN) {
  471. max = tns->cnt - tns->cols * tns->rows;
  472. if (tns->cnt % tns->cols != 0)
  473. max += tns->cols - tns->cnt % tns->cols;
  474. tns->first = MIN(tns->first + d, max);
  475. } else if (dir == DIR_UP) {
  476. tns->first = MAX(tns->first - d, 0);
  477. }
  478. if (tns->first != old) {
  479. tns_check_view(tns, true);
  480. tns->dirty = true;
  481. }
  482. return tns->first != old;
  483. }
  484. bool tns_zoom(tns_t *tns, int d)
  485. {
  486. int i, oldzl;
  487. if (tns == NULL || tns->thumbs == NULL)
  488. return false;
  489. oldzl = tns->zl;
  490. tns->zl += -(d < 0) + (d > 0);
  491. tns->zl = MAX(tns->zl, 0);
  492. tns->zl = MIN(tns->zl, ARRLEN(thumb_size)-1);
  493. if (tns->zl != oldzl) {
  494. for (i = 0; i < tns->cnt; i++)
  495. tns_unload(tns, i);
  496. tns->dirty = true;
  497. }
  498. return tns->zl != oldzl;
  499. }
  500. int tns_translate(tns_t *tns, int x, int y)
  501. {
  502. int n;
  503. if (tns == NULL || tns->thumbs == NULL)
  504. return -1;
  505. if (x < tns->x || y < tns->y)
  506. return -1;
  507. n = tns->first + (y - tns->y) / (thumb_size[tns->zl] + 10) * tns->cols +
  508. (x - tns->x) / (thumb_size[tns->zl] + 10);
  509. if (n >= tns->cnt)
  510. n = -1;
  511. return n;
  512. }