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

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