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.
 
 
 
 
 
 

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