A Simple X Image Viewer
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

570 lines
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. #define _THUMBS_CONFIG
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <utime.h>
  27. #include "thumbs.h"
  28. #include "util.h"
  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. static const int thumb_dim = THUMB_SIZE + 10;
  36. char* tns_cache_filepath(const char *filepath)
  37. {
  38. size_t len;
  39. char *cfile = NULL;
  40. if (cache_dir == NULL || filepath == NULL || *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) + 6;
  45. cfile = (char*) s_malloc(len);
  46. snprintf(cfile, len, "%s/%s.png", 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 (filepath == NULL)
  56. return NULL;
  57. if (stat(filepath, &fstats) < 0)
  58. return NULL;
  59. if ((cfile = tns_cache_filepath(filepath)) != NULL) {
  60. if (stat(cfile, &cstats) == 0) {
  61. if (cstats.st_mtime == fstats.st_mtime)
  62. im = imlib_load_image(cfile);
  63. else
  64. *outdated = true;
  65. }
  66. free(cfile);
  67. }
  68. return im;
  69. }
  70. void tns_cache_write(thumb_t *t, const fileinfo_t *file, bool force)
  71. {
  72. char *cfile, *dirend;
  73. struct stat cstats, fstats;
  74. struct utimbuf times;
  75. Imlib_Load_Error err = 0;
  76. if (t == NULL || t->im == NULL)
  77. return;
  78. if (file == NULL || file->name == NULL || file->path == NULL)
  79. return;
  80. if (stat(file->path, &fstats) < 0)
  81. return;
  82. if ((cfile = tns_cache_filepath(file->path)) != NULL) {
  83. if (force || stat(cfile, &cstats) < 0 ||
  84. cstats.st_mtime != fstats.st_mtime)
  85. {
  86. if ((dirend = strrchr(cfile, '/')) != NULL) {
  87. *dirend = '\0';
  88. err = r_mkdir(cfile);
  89. *dirend = '/';
  90. }
  91. if (err == 0) {
  92. imlib_context_set_image(t->im);
  93. imlib_image_set_format("png");
  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. bool delete;
  109. char *cfile, *filename, *tpos;
  110. r_dir_t dir;
  111. if (cache_dir == NULL)
  112. return;
  113. if (r_opendir(&dir, cache_dir) < 0) {
  114. warn("could not open thumbnail cache directory: %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. warn("could not delete cache file: %s", cfile);
  130. }
  131. free(cfile);
  132. }
  133. r_closedir(&dir);
  134. }
  135. void tns_init(tns_t *tns, const fileinfo_t *files, int cnt, int *sel, win_t *win)
  136. {
  137. int len;
  138. const char *homedir, *dsuffix = "";
  139. if (tns == NULL)
  140. return;
  141. if (cnt > 0) {
  142. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  143. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  144. } else {
  145. tns->thumbs = NULL;
  146. }
  147. tns->files = files;
  148. tns->cnt = cnt;
  149. tns->loadnext = tns->first = tns->end = tns->r_first = tns->r_end = 0;
  150. tns->sel = sel;
  151. tns->win = win;
  152. tns->dirty = false;
  153. if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
  154. homedir = getenv("HOME");
  155. dsuffix = "/.cache";
  156. }
  157. if (homedir != NULL) {
  158. free(cache_dir);
  159. len = strlen(homedir) + strlen(dsuffix) + 6;
  160. cache_dir = (char*) s_malloc(len);
  161. snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
  162. } else {
  163. warn("could not locate thumbnail cache directory");
  164. }
  165. }
  166. void tns_free(tns_t *tns)
  167. {
  168. int i;
  169. if (tns == NULL)
  170. return;
  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. bool tns_load(tns_t *tns, int n, bool force)
  185. {
  186. int w, h;
  187. bool cache_hit = false;
  188. float z, zw, zh;
  189. thumb_t *t;
  190. Imlib_Image im = NULL;
  191. const fileinfo_t *file;
  192. if (tns == NULL || tns->thumbs == NULL)
  193. return false;
  194. if (n < 0 || n >= tns->cnt)
  195. return false;
  196. file = &tns->files[n];
  197. if (file->name == NULL || file->path == NULL)
  198. return false;
  199. t = &tns->thumbs[n];
  200. if (t->im != NULL) {
  201. imlib_context_set_image(t->im);
  202. imlib_free_image();
  203. }
  204. if (!force && (im = tns_cache_load(file->path, &force)) != NULL) {
  205. cache_hit = true;
  206. } else {
  207. #if HAVE_LIBEXIF
  208. if (!force) {
  209. int pw = 0, ph = 0, x = 0, y = 0;
  210. bool err;
  211. ExifData *ed;
  212. ExifEntry *entry;
  213. ExifContent *ifd;
  214. ExifByteOrder byte_order;
  215. int tmpfd;
  216. char tmppath[] = "/tmp/sxiv-XXXXXX";
  217. Imlib_Image tmpim;
  218. if ((ed = exif_data_new_from_file(file->path)) != NULL &&
  219. ed->data != NULL && ed->size > 0)
  220. {
  221. if ((tmpfd = mkstemp(tmppath)) >= 0) {
  222. err = write(tmpfd, ed->data, ed->size) != ed->size;
  223. close(tmpfd);
  224. if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
  225. byte_order = exif_data_get_byte_order(ed);
  226. ifd = ed->ifd[EXIF_IFD_EXIF];
  227. entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
  228. if (entry != NULL)
  229. pw = exif_get_long(entry->data, byte_order);
  230. entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
  231. if (entry != NULL)
  232. ph = exif_get_long(entry->data, byte_order);
  233. imlib_context_set_image(tmpim);
  234. w = imlib_image_get_width();
  235. h = imlib_image_get_height();
  236. if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
  237. zw = (float) pw / (float) w;
  238. zh = (float) ph / (float) h;
  239. if (zw < zh) {
  240. pw /= zh;
  241. x = (w - pw) / 2;
  242. w = pw;
  243. } else if (zw > zh) {
  244. ph /= zw;
  245. y = (h - ph) / 2;
  246. h = ph;
  247. }
  248. }
  249. if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
  250. die("could not allocate memory");
  251. imlib_free_image_and_decache();
  252. }
  253. unlink(tmppath);
  254. }
  255. exif_data_unref(ed);
  256. }
  257. }
  258. #endif
  259. if (im == NULL && (access(file->path, R_OK) < 0 ||
  260. (im = imlib_load_image(file->path)) == NULL))
  261. {
  262. warn("could not open image: %s", file->name);
  263. return false;
  264. }
  265. }
  266. imlib_context_set_image(im);
  267. imlib_context_set_anti_alias(1);
  268. #if HAVE_LIBEXIF
  269. if (!cache_hit)
  270. exif_auto_orientate(file);
  271. #endif
  272. w = imlib_image_get_width();
  273. h = imlib_image_get_height();
  274. zw = (float) THUMB_SIZE / (float) w;
  275. zh = (float) THUMB_SIZE / (float) h;
  276. z = MIN(zw, zh);
  277. z = MIN(z, 1.0);
  278. t->w = z * w;
  279. t->h = z * h;
  280. t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
  281. if (t->im == NULL)
  282. die("could not allocate memory");
  283. imlib_free_image_and_decache();
  284. if (!cache_hit)
  285. tns_cache_write(t, file, true);
  286. tns->dirty = true;
  287. return true;
  288. }
  289. void tns_unload(tns_t *tns, int n)
  290. {
  291. thumb_t *t;
  292. if (tns == NULL || tns->thumbs == NULL)
  293. return;
  294. if (n < 0 || n >= tns->cnt)
  295. return;
  296. t = &tns->thumbs[n];
  297. if (t->im != NULL) {
  298. imlib_context_set_image(t->im);
  299. imlib_free_image();
  300. t->im = NULL;
  301. }
  302. }
  303. void tns_check_view(tns_t *tns, bool scrolled)
  304. {
  305. int r;
  306. if (tns == NULL)
  307. return;
  308. tns->first -= tns->first % tns->cols;
  309. r = *tns->sel % tns->cols;
  310. if (scrolled) {
  311. /* move selection into visible area */
  312. if (*tns->sel >= tns->first + tns->cols * tns->rows)
  313. *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  314. else if (*tns->sel < tns->first)
  315. *tns->sel = tns->first + r;
  316. } else {
  317. /* scroll to selection */
  318. if (tns->first + tns->cols * tns->rows <= *tns->sel) {
  319. tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
  320. tns->dirty = true;
  321. } else if (tns->first > *tns->sel) {
  322. tns->first = *tns->sel - r;
  323. tns->dirty = true;
  324. }
  325. }
  326. }
  327. void tns_render(tns_t *tns)
  328. {
  329. thumb_t *t;
  330. win_t *win;
  331. int i, cnt, r, x, y;
  332. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  333. return;
  334. if (!tns->dirty)
  335. return;
  336. win = tns->win;
  337. win_clear(win);
  338. imlib_context_set_drawable(win->buf.pm);
  339. tns->cols = MAX(1, win->w / thumb_dim);
  340. tns->rows = MAX(1, win->h / thumb_dim);
  341. if (tns->cnt < tns->cols * tns->rows) {
  342. tns->first = 0;
  343. cnt = tns->cnt;
  344. } else {
  345. tns_check_view(tns, false);
  346. cnt = tns->cols * tns->rows;
  347. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  348. tns->first -= r - r % tns->cols;
  349. if (r > 0)
  350. cnt -= r % tns->cols;
  351. }
  352. r = cnt % tns->cols ? 1 : 0;
  353. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  354. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  355. tns->loadnext = tns->cnt;
  356. tns->end = tns->first + cnt;
  357. for (i = tns->r_first; i < tns->r_end; i++) {
  358. if ((i < tns->first || i >= tns->end) && tns->thumbs[i].im != NULL)
  359. tns_unload(tns, i);
  360. }
  361. tns->r_first = tns->first;
  362. tns->r_end = tns->end;
  363. for (i = tns->first; i < tns->end; i++) {
  364. t = &tns->thumbs[i];
  365. if (t->im != NULL) {
  366. t->x = x + (THUMB_SIZE - t->w) / 2;
  367. t->y = y + (THUMB_SIZE - t->h) / 2;
  368. imlib_context_set_image(t->im);
  369. imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h);
  370. if (tns->files[i].marked)
  371. tns_mark(tns, i, true);
  372. } else {
  373. tns->loadnext = MIN(tns->loadnext, i);
  374. }
  375. if ((i + 1) % tns->cols == 0) {
  376. x = tns->x;
  377. y += thumb_dim;
  378. } else {
  379. x += thumb_dim;
  380. }
  381. }
  382. tns->dirty = false;
  383. tns_highlight(tns, *tns->sel, true);
  384. }
  385. void tns_mark(tns_t *tns, int n, bool mark)
  386. {
  387. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  388. return;
  389. if (n >= 0 && n < tns->cnt && tns->thumbs[n].im != NULL) {
  390. win_t *win = tns->win;
  391. thumb_t *t = &tns->thumbs[n];
  392. unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
  393. int x = t->x + t->w, y = t->y + t->h;
  394. win_draw_rect(win, x - 2, y + 1, 1, 2, true, 1, col);
  395. win_draw_rect(win, x + 1, y - 2, 2, 1, true, 1, col);
  396. if (mark)
  397. col = win->selcol;
  398. win_draw_rect(win, x - 1, y + 1, 6, 2, true, 1, col);
  399. win_draw_rect(win, x + 1, y - 1, 2, 6, true, 1, col);
  400. if (!mark && n == *tns->sel)
  401. tns_highlight(tns, n, true);
  402. }
  403. }
  404. void tns_highlight(tns_t *tns, int n, bool hl)
  405. {
  406. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  407. return;
  408. if (n >= 0 && n < tns->cnt && tns->thumbs[n].im != NULL) {
  409. win_t *win = tns->win;
  410. thumb_t *t = &tns->thumbs[n];
  411. unsigned long col;
  412. if (hl)
  413. col = win->selcol;
  414. else
  415. col = win->fullscreen ? win->fscol : win->bgcol;
  416. win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, false, 2, col);
  417. if (tns->files[n].marked)
  418. tns_mark(tns, n, true);
  419. }
  420. }
  421. bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
  422. {
  423. int old, max;
  424. if (tns == NULL || tns->thumbs == NULL)
  425. return false;
  426. old = *tns->sel;
  427. cnt = cnt > 1 ? cnt : 1;
  428. switch (dir) {
  429. case DIR_UP:
  430. *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
  431. break;
  432. case DIR_DOWN:
  433. max = tns->cols * ((tns->cnt - 1) / tns->cols) +
  434. MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
  435. *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
  436. break;
  437. case DIR_LEFT:
  438. *tns->sel = MAX(*tns->sel - cnt, 0);
  439. break;
  440. case DIR_RIGHT:
  441. *tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1);
  442. break;
  443. }
  444. if (*tns->sel != old) {
  445. tns_highlight(tns, old, false);
  446. tns_check_view(tns, false);
  447. if (!tns->dirty)
  448. tns_highlight(tns, *tns->sel, true);
  449. }
  450. return *tns->sel != old;
  451. }
  452. bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
  453. {
  454. int d, max, old;
  455. if (tns == NULL)
  456. return false;
  457. old = tns->first;
  458. d = tns->cols * (screen ? tns->rows : 1);
  459. if (dir == DIR_DOWN) {
  460. max = tns->cnt - tns->cols * tns->rows;
  461. if (tns->cnt % tns->cols != 0)
  462. max += tns->cols - tns->cnt % tns->cols;
  463. tns->first = MIN(tns->first + d, max);
  464. } else if (dir == DIR_UP) {
  465. tns->first = MAX(tns->first - d, 0);
  466. }
  467. if (tns->first != old) {
  468. tns_check_view(tns, true);
  469. tns->dirty = true;
  470. }
  471. return tns->first != old;
  472. }
  473. int tns_translate(tns_t *tns, int x, int y)
  474. {
  475. int n;
  476. if (tns == NULL || tns->thumbs == NULL)
  477. return -1;
  478. if (x < tns->x || y < tns->y)
  479. return -1;
  480. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  481. (x - tns->x) / thumb_dim;
  482. if (n >= tns->cnt)
  483. n = -1;
  484. return n;
  485. }