A Simple X Image Viewer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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