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.

пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. #define _RENDER_CONFIG
  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 "exif.h"
  28. #include "thumbs.h"
  29. #include "util.h"
  30. #include "config.h"
  31. static const int thumb_dim = THUMB_SIZE + 10;
  32. static const char * const CACHE_DIR = ".sxiv/cache";
  33. static char *cache_dir = NULL;
  34. bool tns_cache_enabled(void)
  35. {
  36. struct stat stats;
  37. return cache_dir != NULL && stat(cache_dir, &stats) == 0 &&
  38. S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0;
  39. }
  40. char* tns_cache_filepath(const char *filepath)
  41. {
  42. size_t len;
  43. char *cfile = NULL;
  44. if (cache_dir == NULL || filepath == NULL || *filepath != '/')
  45. return NULL;
  46. if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
  47. /* don't cache images inside the cache directory! */
  48. len = strlen(cache_dir) + strlen(filepath) + 6;
  49. cfile = (char*) s_malloc(len);
  50. snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
  51. }
  52. return cfile;
  53. }
  54. Imlib_Image* tns_cache_load(const char *filepath)
  55. {
  56. char *cfile;
  57. struct stat cstats, fstats;
  58. Imlib_Image *im = NULL;
  59. if (filepath == NULL)
  60. return NULL;
  61. if (stat(filepath, &fstats) < 0)
  62. return NULL;
  63. if ((cfile = tns_cache_filepath(filepath)) != NULL) {
  64. if (stat(cfile, &cstats) == 0 && cstats.st_mtime == fstats.st_mtime)
  65. im = imlib_load_image(cfile);
  66. free(cfile);
  67. }
  68. return im;
  69. }
  70. void tns_cache_write(thumb_t *t, bool force)
  71. {
  72. char *cfile, *dirend;
  73. struct stat cstats, fstats;
  74. struct utimbuf times;
  75. Imlib_Load_Error err = 0;
  76. if (t == NULL || t->im == NULL)
  77. return;
  78. if (t->file == NULL || t->file->name == NULL || t->file->path == NULL)
  79. return;
  80. if (stat(t->file->path, &fstats) < 0)
  81. return;
  82. if ((cfile = tns_cache_filepath(t->file->path)) != NULL) {
  83. if (force || stat(cfile, &cstats) < 0 ||
  84. cstats.st_mtime != fstats.st_mtime)
  85. {
  86. if ((dirend = strrchr(cfile, '/')) != NULL) {
  87. *dirend = '\0';
  88. err = r_mkdir(cfile);
  89. *dirend = '/';
  90. }
  91. if (err == 0) {
  92. imlib_context_set_image(t->im);
  93. imlib_image_set_format("png");
  94. imlib_save_image_with_error_return(cfile, &err);
  95. }
  96. if (err == 0) {
  97. times.actime = fstats.st_atime;
  98. times.modtime = fstats.st_mtime;
  99. utime(cfile, &times);
  100. } else {
  101. warn("could not cache thumbnail: %s", t->file->name);
  102. }
  103. }
  104. free(cfile);
  105. }
  106. }
  107. void tns_clean_cache(tns_t *tns)
  108. {
  109. int dirlen;
  110. bool delete;
  111. char *cfile, *filename, *tpos;
  112. r_dir_t dir;
  113. if (cache_dir == NULL)
  114. return;
  115. if (r_opendir(&dir, cache_dir) < 0) {
  116. warn("could not open thumbnail cache directory: %s", cache_dir);
  117. return;
  118. }
  119. dirlen = strlen(cache_dir);
  120. while ((cfile = r_readdir(&dir)) != NULL) {
  121. filename = cfile + dirlen;
  122. delete = false;
  123. if ((tpos = strrchr(filename, '.')) != NULL) {
  124. *tpos = '\0';
  125. if (access(filename, F_OK) < 0)
  126. delete = true;
  127. *tpos = '.';
  128. }
  129. if (delete) {
  130. if (unlink(cfile) < 0)
  131. warn("could not delete cache file: %s", cfile);
  132. }
  133. free(cfile);
  134. }
  135. r_closedir(&dir);
  136. }
  137. void tns_init(tns_t *tns, int cnt, win_t *win)
  138. {
  139. int len;
  140. char *homedir;
  141. if (tns == NULL)
  142. return;
  143. if (cnt > 0) {
  144. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  145. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  146. } else {
  147. tns->thumbs = NULL;
  148. }
  149. tns->cap = cnt;
  150. tns->cnt = tns->first = tns->sel = 0;
  151. tns->win = win;
  152. tns->alpha = !RENDER_WHITE_ALPHA;
  153. tns->dirty = false;
  154. if ((homedir = getenv("HOME")) != NULL) {
  155. if (cache_dir != NULL)
  156. free(cache_dir);
  157. len = strlen(homedir) + strlen(CACHE_DIR) + 2;
  158. cache_dir = (char*) s_malloc(len);
  159. snprintf(cache_dir, len, "%s/%s", homedir, CACHE_DIR);
  160. } else {
  161. warn("could not locate thumbnail cache directory");
  162. }
  163. }
  164. void tns_free(tns_t *tns)
  165. {
  166. int i;
  167. if (tns == NULL)
  168. return;
  169. if (tns->thumbs != NULL) {
  170. for (i = 0; i < tns->cnt; i++) {
  171. if (tns->thumbs[i].im != NULL) {
  172. imlib_context_set_image(tns->thumbs[i].im);
  173. imlib_free_image();
  174. }
  175. }
  176. free(tns->thumbs);
  177. tns->thumbs = NULL;
  178. }
  179. if (cache_dir != NULL) {
  180. free(cache_dir);
  181. cache_dir = NULL;
  182. }
  183. }
  184. bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
  185. bool force, bool silent)
  186. {
  187. int w, h;
  188. bool use_cache, cache_hit = false;
  189. float z, zw, zh;
  190. thumb_t *t;
  191. Imlib_Image *im;
  192. const char *fmt;
  193. if (tns == NULL || tns->thumbs == NULL)
  194. return false;
  195. if (file == NULL || file->name == NULL || file->path == NULL)
  196. return false;
  197. if (n < 0 || n >= tns->cap)
  198. return false;
  199. t = &tns->thumbs[n];
  200. t->file = file;
  201. if (t->im != NULL) {
  202. imlib_context_set_image(t->im);
  203. imlib_free_image();
  204. }
  205. if ((use_cache = tns_cache_enabled())) {
  206. if (!force && (im = tns_cache_load(file->path)) != NULL)
  207. cache_hit = true;
  208. }
  209. if (!cache_hit) {
  210. if (access(file->path, R_OK) < 0 ||
  211. (im = imlib_load_image(file->path)) == NULL)
  212. {
  213. if (!silent)
  214. warn("could not open image: %s", file->name);
  215. return false;
  216. }
  217. }
  218. imlib_context_set_image(im);
  219. imlib_context_set_anti_alias(1);
  220. if ((fmt = imlib_image_format()) == NULL) {
  221. if (!silent)
  222. warn("could not open image: %s", file->name);
  223. imlib_free_image_and_decache();
  224. return false;
  225. }
  226. if (STREQ(fmt, "jpeg"))
  227. exif_auto_orientate(file);
  228. w = imlib_image_get_width();
  229. h = imlib_image_get_height();
  230. zw = (float) THUMB_SIZE / (float) w;
  231. zh = (float) THUMB_SIZE / (float) h;
  232. z = MIN(zw, zh);
  233. z = MIN(z, 1.0);
  234. t->w = z * w;
  235. t->h = z * h;
  236. t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
  237. if (t->im == NULL)
  238. die("could not allocate memory");
  239. imlib_free_image_and_decache();
  240. if (use_cache && !cache_hit)
  241. tns_cache_write(t, true);
  242. tns->dirty = true;
  243. return true;
  244. }
  245. void tns_check_view(tns_t *tns, bool scrolled)
  246. {
  247. int r;
  248. if (tns == NULL)
  249. return;
  250. tns->first -= tns->first % tns->cols;
  251. r = tns->sel % tns->cols;
  252. if (scrolled) {
  253. /* move selection into visible area */
  254. if (tns->sel >= tns->first + tns->cols * tns->rows)
  255. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  256. else if (tns->sel < tns->first)
  257. tns->sel = tns->first + r;
  258. } else {
  259. /* scroll to selection */
  260. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  261. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  262. tns->dirty = true;
  263. } else if (tns->first > tns->sel) {
  264. tns->first = tns->sel - r;
  265. tns->dirty = true;
  266. }
  267. }
  268. }
  269. void tns_render(tns_t *tns)
  270. {
  271. thumb_t *t;
  272. win_t *win;
  273. int i, cnt, r, x, y;
  274. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  275. return;
  276. if (!tns->dirty)
  277. return;
  278. win = tns->win;
  279. win_clear(win);
  280. imlib_context_set_drawable(win->pm);
  281. tns->cols = MAX(1, win->w / thumb_dim);
  282. tns->rows = MAX(1, win->h / thumb_dim);
  283. if (tns->cnt < tns->cols * tns->rows) {
  284. tns->first = 0;
  285. cnt = tns->cnt;
  286. } else {
  287. tns_check_view(tns, false);
  288. cnt = tns->cols * tns->rows;
  289. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  290. tns->first -= r - r % tns->cols;
  291. if (r > 0)
  292. cnt -= r % tns->cols;
  293. }
  294. r = cnt % tns->cols ? 1 : 0;
  295. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  296. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  297. for (i = 0; i < cnt; i++) {
  298. t = &tns->thumbs[tns->first + i];
  299. t->x = x + (THUMB_SIZE - t->w) / 2;
  300. t->y = y + (THUMB_SIZE - t->h) / 2;
  301. imlib_context_set_image(t->im);
  302. if (!tns->alpha && imlib_image_has_alpha())
  303. win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
  304. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  305. t->x, t->y, t->w, t->h);
  306. if ((i + 1) % tns->cols == 0) {
  307. x = tns->x;
  308. y += thumb_dim;
  309. } else {
  310. x += thumb_dim;
  311. }
  312. }
  313. tns->dirty = false;
  314. tns_highlight(tns, tns->sel, true);
  315. }
  316. void tns_highlight(tns_t *tns, int n, bool hl)
  317. {
  318. thumb_t *t;
  319. win_t *win;
  320. int x, y;
  321. unsigned long col;
  322. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  323. return;
  324. win = tns->win;
  325. if (n >= 0 && n < tns->cnt) {
  326. t = &tns->thumbs[n];
  327. if (hl)
  328. col = win->selcol;
  329. else if (win->fullscreen)
  330. col = win->fscol;
  331. else
  332. col = win->bgcol;
  333. x = t->x - (THUMB_SIZE - t->w) / 2;
  334. y = t->y - (THUMB_SIZE - t->h) / 2;
  335. win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
  336. false, 2, col);
  337. }
  338. }
  339. bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
  340. {
  341. int old, max;
  342. if (tns == NULL || tns->thumbs == NULL)
  343. return false;
  344. old = tns->sel;
  345. cnt = cnt > 1 ? cnt : 1;
  346. switch (dir) {
  347. case DIR_UP:
  348. tns->sel = MAX(tns->sel - cnt * tns->cols, tns->sel % tns->cols);
  349. break;
  350. case DIR_DOWN:
  351. max = tns->cols * ((tns->cnt - 1) / tns->cols) +
  352. MIN((tns->cnt - 1) % tns->cols, tns->sel % tns->cols);
  353. tns->sel = MIN(tns->sel + cnt * tns->cols, max);
  354. break;
  355. case DIR_LEFT:
  356. tns->sel = MAX(tns->sel - cnt, 0);
  357. break;
  358. case DIR_RIGHT:
  359. tns->sel = MIN(tns->sel + cnt, tns->cnt - 1);
  360. break;
  361. }
  362. if (tns->sel != old) {
  363. tns_highlight(tns, old, false);
  364. tns_check_view(tns, false);
  365. if (!tns->dirty)
  366. tns_highlight(tns, tns->sel, true);
  367. }
  368. return tns->sel != old;
  369. }
  370. bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
  371. {
  372. int d, max, old;
  373. if (tns == NULL)
  374. return false;
  375. old = tns->first;
  376. d = tns->cols * (screen ? tns->rows : 1);
  377. if (dir == DIR_DOWN) {
  378. max = tns->cnt - tns->cols * tns->rows;
  379. if (tns->cnt % tns->cols != 0)
  380. max += tns->cols - tns->cnt % tns->cols;
  381. tns->first = MIN(tns->first + d, max);
  382. } else if (dir == DIR_UP) {
  383. tns->first = MAX(tns->first - d, 0);
  384. }
  385. if (tns->first != old) {
  386. tns_check_view(tns, true);
  387. tns->dirty = true;
  388. }
  389. return tns->first != old;
  390. }
  391. int tns_translate(tns_t *tns, int x, int y)
  392. {
  393. int n;
  394. if (tns == NULL || tns->thumbs == NULL)
  395. return -1;
  396. if (x < tns->x || y < tns->y)
  397. return -1;
  398. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  399. (x - tns->x) / thumb_dim;
  400. if (n >= tns->cnt)
  401. n = -1;
  402. return n;
  403. }