A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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