A Simple X Image Viewer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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