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.
 
 
 
 
 
 

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