A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

541 linhas
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. if (cache_dir != NULL)
  155. free(cache_dir);
  156. len = strlen(homedir) + strlen(dsuffix) + 6;
  157. cache_dir = (char*) s_malloc(len);
  158. snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
  159. } else {
  160. warn("could not locate thumbnail cache directory");
  161. }
  162. }
  163. void tns_free(tns_t *tns)
  164. {
  165. int i;
  166. if (tns == NULL)
  167. return;
  168. if (tns->thumbs != NULL) {
  169. for (i = 0; i < tns->cnt; i++) {
  170. if (tns->thumbs[i].im != NULL) {
  171. imlib_context_set_image(tns->thumbs[i].im);
  172. imlib_free_image();
  173. }
  174. }
  175. free(tns->thumbs);
  176. tns->thumbs = NULL;
  177. }
  178. if (cache_dir != NULL) {
  179. free(cache_dir);
  180. cache_dir = NULL;
  181. }
  182. }
  183. bool tns_load(tns_t *tns, int n, bool force)
  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. const fileinfo_t *file;
  191. if (tns == NULL || tns->thumbs == NULL)
  192. return false;
  193. if (n < 0 || n >= tns->cap)
  194. return false;
  195. file = &tns->files[n];
  196. if (file->name == NULL || file->path == NULL)
  197. return false;
  198. t = &tns->thumbs[n];
  199. if (t->im != NULL) {
  200. imlib_context_set_image(t->im);
  201. imlib_free_image();
  202. }
  203. if (!force && (im = tns_cache_load(file->path)) != NULL) {
  204. cache_hit = true;
  205. } else {
  206. #if HAVE_LIBEXIF
  207. if (!force) {
  208. int pw = 0, ph = 0, x = 0, y = 0;
  209. bool err;
  210. ExifData *ed;
  211. ExifEntry *entry;
  212. ExifContent *ifd;
  213. ExifByteOrder byte_order;
  214. int tmpfd;
  215. char tmppath[] = "/tmp/sxiv-XXXXXX";
  216. Imlib_Image tmpim;
  217. if ((ed = exif_data_new_from_file(file->path)) != NULL &&
  218. ed->data != NULL && ed->size > 0)
  219. {
  220. if ((tmpfd = mkstemp(tmppath)) >= 0) {
  221. err = write(tmpfd, ed->data, ed->size) != ed->size;
  222. close(tmpfd);
  223. if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
  224. byte_order = exif_data_get_byte_order(ed);
  225. ifd = ed->ifd[EXIF_IFD_EXIF];
  226. entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
  227. if (entry != NULL)
  228. pw = exif_get_long(entry->data, byte_order);
  229. entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
  230. if (entry != NULL)
  231. ph = exif_get_long(entry->data, byte_order);
  232. imlib_context_set_image(tmpim);
  233. w = imlib_image_get_width();
  234. h = imlib_image_get_height();
  235. if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
  236. zw = (float) pw / (float) w;
  237. zh = (float) ph / (float) h;
  238. if (zw < zh) {
  239. pw /= zh;
  240. x = (w - pw) / 2;
  241. w = pw;
  242. } else if (zw > zh) {
  243. ph /= zw;
  244. y = (h - ph) / 2;
  245. h = ph;
  246. }
  247. }
  248. if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
  249. die("could not allocate memory");
  250. imlib_free_image_and_decache();
  251. }
  252. unlink(tmppath);
  253. }
  254. exif_data_unref(ed);
  255. }
  256. }
  257. #endif
  258. if (im == NULL && (access(file->path, R_OK) < 0 ||
  259. (im = imlib_load_image(file->path)) == NULL))
  260. {
  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, file, 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 (tns->files[tns->first + i].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. win_t *win = tns->win;
  366. thumb_t *t = &tns->thumbs[n];
  367. unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
  368. int x = t->x + t->w, y = t->y + t->h;
  369. win_draw_rect(win, x - 2, y + 1, 1, 2, true, 1, col);
  370. win_draw_rect(win, x + 1, y - 2, 2, 1, true, 1, col);
  371. if (mark)
  372. col = win->selcol;
  373. win_draw_rect(win, x - 1, y + 1, 6, 2, true, 1, col);
  374. win_draw_rect(win, x + 1, y - 1, 2, 6, true, 1, col);
  375. if (!mark && n == *tns->sel)
  376. tns_highlight(tns, n, true);
  377. }
  378. }
  379. void tns_highlight(tns_t *tns, int n, bool hl)
  380. {
  381. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  382. return;
  383. if (n >= 0 && n < tns->cnt) {
  384. win_t *win = tns->win;
  385. thumb_t *t = &tns->thumbs[n];
  386. unsigned long col;
  387. if (hl)
  388. col = win->selcol;
  389. else
  390. col = win->fullscreen ? win->fscol : win->bgcol;
  391. win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, false, 2, col);
  392. if (tns->files[n].marked)
  393. tns_mark(tns, n, true);
  394. }
  395. }
  396. bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
  397. {
  398. int old, max;
  399. if (tns == NULL || tns->thumbs == NULL)
  400. return false;
  401. old = *tns->sel;
  402. cnt = cnt > 1 ? cnt : 1;
  403. switch (dir) {
  404. case DIR_UP:
  405. *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
  406. break;
  407. case DIR_DOWN:
  408. max = tns->cols * ((tns->cnt - 1) / tns->cols) +
  409. MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
  410. *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
  411. break;
  412. case DIR_LEFT:
  413. *tns->sel = MAX(*tns->sel - cnt, 0);
  414. break;
  415. case DIR_RIGHT:
  416. *tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1);
  417. break;
  418. }
  419. if (*tns->sel != old) {
  420. tns_highlight(tns, old, false);
  421. tns_check_view(tns, false);
  422. if (!tns->dirty)
  423. tns_highlight(tns, *tns->sel, true);
  424. }
  425. return *tns->sel != old;
  426. }
  427. bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
  428. {
  429. int d, max, old;
  430. if (tns == NULL)
  431. return false;
  432. old = tns->first;
  433. d = tns->cols * (screen ? tns->rows : 1);
  434. if (dir == DIR_DOWN) {
  435. max = tns->cnt - tns->cols * tns->rows;
  436. if (tns->cnt % tns->cols != 0)
  437. max += tns->cols - tns->cnt % tns->cols;
  438. tns->first = MIN(tns->first + d, max);
  439. } else if (dir == DIR_UP) {
  440. tns->first = MAX(tns->first - d, 0);
  441. }
  442. if (tns->first != old) {
  443. tns_check_view(tns, true);
  444. tns->dirty = true;
  445. }
  446. return tns->first != old;
  447. }
  448. int tns_translate(tns_t *tns, int x, int y)
  449. {
  450. int n;
  451. if (tns == NULL || tns->thumbs == NULL)
  452. return -1;
  453. if (x < tns->x || y < tns->y)
  454. return -1;
  455. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  456. (x - tns->x) / thumb_dim;
  457. if (n >= tns->cnt)
  458. n = -1;
  459. return n;
  460. }