A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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