A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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