A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* sxiv: thumbs.c
  2. * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  19. #define _THUMBS_CONFIG
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <utime.h>
  26. #include "thumbs.h"
  27. #include "util.h"
  28. #include "config.h"
  29. #ifdef EXIF_SUPPORT
  30. void exif_auto_orientate(const fileinfo_t*);
  31. #endif
  32. const int thumb_dim = THUMB_SIZE + 10;
  33. char *cache_dir = NULL;
  34. bool tns_cache_enabled() {
  35. struct stat stats;
  36. return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) &&
  37. !access(cache_dir, W_OK);
  38. }
  39. char* tns_cache_filepath(const char *filepath) {
  40. size_t len;
  41. char *cfile = NULL;
  42. if (!cache_dir || !filepath || *filepath != '/')
  43. return NULL;
  44. if (strncmp(filepath, cache_dir, strlen(cache_dir))) {
  45. len = strlen(cache_dir) + strlen(filepath) + 6;
  46. cfile = (char*) s_malloc(len);
  47. snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
  48. }
  49. return cfile;
  50. }
  51. Imlib_Image* tns_cache_load(const char *filepath) {
  52. char *cfile;
  53. struct stat cstats, fstats;
  54. Imlib_Image *im = NULL;
  55. if (!filepath)
  56. return NULL;
  57. if (stat(filepath, &fstats))
  58. return NULL;
  59. if ((cfile = tns_cache_filepath(filepath))) {
  60. if (!stat(cfile, &cstats) && 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. char *cfile, *dirend;
  68. struct stat cstats, fstats;
  69. struct utimbuf times;
  70. Imlib_Load_Error err = 0;
  71. if (!t || !t->im || !t->file || !t->file->name || !t->file->path)
  72. return;
  73. if (stat(t->file->path, &fstats))
  74. return;
  75. if ((cfile = tns_cache_filepath(t->file->path))) {
  76. if (force || stat(cfile, &cstats) || cstats.st_mtime != fstats.st_mtime) {
  77. if ((dirend = strrchr(cfile, '/'))) {
  78. *dirend = '\0';
  79. err = r_mkdir(cfile);
  80. *dirend = '/';
  81. }
  82. if (!err) {
  83. imlib_context_set_image(t->im);
  84. imlib_image_set_format("png");
  85. imlib_save_image_with_error_return(cfile, &err);
  86. }
  87. if (err) {
  88. warn("could not cache thumbnail: %s", t->file->name);
  89. } else {
  90. times.actime = fstats.st_atime;
  91. times.modtime = fstats.st_mtime;
  92. utime(cfile, &times);
  93. }
  94. }
  95. free(cfile);
  96. }
  97. }
  98. void tns_clean_cache(tns_t *tns) {
  99. int dirlen;
  100. bool delete;
  101. char *cfile, *filename, *tpos;
  102. r_dir_t dir;
  103. if (!cache_dir)
  104. return;
  105. if (r_opendir(&dir, cache_dir)) {
  106. warn("could not open thumbnail cache directory: %s", cache_dir);
  107. return;
  108. }
  109. dirlen = strlen(cache_dir);
  110. while ((cfile = r_readdir(&dir))) {
  111. filename = cfile + dirlen;
  112. delete = false;
  113. if ((tpos = strrchr(filename, '.'))) {
  114. *tpos = '\0';
  115. if (access(filename, F_OK))
  116. delete = true;
  117. *tpos = '.';
  118. }
  119. if (delete && unlink(cfile))
  120. warn("could not delete cache file: %s", cfile);
  121. free(cfile);
  122. }
  123. r_closedir(&dir);
  124. }
  125. void tns_init(tns_t *tns, int cnt) {
  126. int len;
  127. char *homedir;
  128. if (!tns)
  129. return;
  130. if (cnt) {
  131. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  132. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  133. } else {
  134. tns->thumbs = NULL;
  135. }
  136. tns->cnt = tns->first = tns->sel = 0;
  137. tns->cap = cnt;
  138. tns->alpha = true;
  139. tns->dirty = false;
  140. if ((homedir = getenv("HOME"))) {
  141. if (cache_dir)
  142. free(cache_dir);
  143. len = strlen(homedir) + 10;
  144. cache_dir = (char*) s_malloc(len * sizeof(char));
  145. snprintf(cache_dir, len, "%s/.sxiv", homedir);
  146. } else {
  147. warn("could not locate thumbnail cache directory");
  148. }
  149. }
  150. void tns_free(tns_t *tns) {
  151. int i;
  152. if (!tns)
  153. return;
  154. if (tns->thumbs) {
  155. for (i = 0; i < tns->cnt; i++) {
  156. if (tns->thumbs[i].im) {
  157. imlib_context_set_image(tns->thumbs[i].im);
  158. imlib_free_image();
  159. }
  160. }
  161. free(tns->thumbs);
  162. tns->thumbs = NULL;
  163. }
  164. if (cache_dir) {
  165. free(cache_dir);
  166. cache_dir = NULL;
  167. }
  168. }
  169. bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
  170. bool force, bool silent)
  171. {
  172. int w, h;
  173. bool use_cache, cache_hit = false;
  174. float z, zw, zh;
  175. thumb_t *t;
  176. Imlib_Image *im;
  177. const char *fmt;
  178. if (!tns || !tns->thumbs || !file || !file->name || !file->path)
  179. return false;
  180. if (n < 0 || n >= tns->cap)
  181. return false;
  182. t = &tns->thumbs[n];
  183. t->file = file;
  184. if (t->im) {
  185. imlib_context_set_image(t->im);
  186. imlib_free_image();
  187. }
  188. if ((use_cache = tns_cache_enabled())) {
  189. if (!force && (im = tns_cache_load(file->path)))
  190. cache_hit = true;
  191. }
  192. if (!cache_hit &&
  193. (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
  194. {
  195. if (!silent)
  196. warn("could not open image: %s", file->name);
  197. return false;
  198. }
  199. imlib_context_set_image(im);
  200. imlib_context_set_anti_alias(1);
  201. fmt = imlib_image_format();
  202. /* avoid unused-but-set-variable warning */
  203. (void) fmt;
  204. #ifdef EXIF_SUPPORT
  205. if (!cache_hit && !strcmp(fmt, "jpeg"))
  206. exif_auto_orientate(file);
  207. #endif
  208. w = imlib_image_get_width();
  209. h = imlib_image_get_height();
  210. zw = (float) THUMB_SIZE / (float) w;
  211. zh = (float) THUMB_SIZE / (float) h;
  212. z = MIN(zw, zh);
  213. t->w = z * w;
  214. t->h = z * h;
  215. if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
  216. die("could not allocate memory");
  217. imlib_free_image_and_decache();
  218. if (use_cache && !cache_hit)
  219. tns_cache_write(t, false);
  220. tns->dirty = true;
  221. return true;
  222. }
  223. void tns_check_view(tns_t *tns, bool scrolled) {
  224. int r;
  225. if (!tns)
  226. return;
  227. tns->first -= tns->first % tns->cols;
  228. r = tns->sel % tns->cols;
  229. if (scrolled) {
  230. /* move selection into visible area */
  231. if (tns->sel >= tns->first + tns->cols * tns->rows)
  232. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  233. else if (tns->sel < tns->first)
  234. tns->sel = tns->first + r;
  235. } else {
  236. /* scroll to selection */
  237. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  238. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  239. tns->dirty = true;
  240. } else if (tns->first > tns->sel) {
  241. tns->first = tns->sel - r;
  242. tns->dirty = true;
  243. }
  244. }
  245. }
  246. void tns_render(tns_t *tns, win_t *win) {
  247. int i, cnt, r, x, y;
  248. thumb_t *t;
  249. if (!tns || !tns->thumbs || !win)
  250. return;
  251. if (!tns->dirty)
  252. return;
  253. win_clear(win);
  254. imlib_context_set_drawable(win->pm);
  255. tns->cols = MAX(1, win->w / thumb_dim);
  256. tns->rows = MAX(1, win->h / thumb_dim);
  257. if (tns->cnt < tns->cols * tns->rows) {
  258. tns->first = 0;
  259. cnt = tns->cnt;
  260. } else {
  261. tns_check_view(tns, false);
  262. cnt = tns->cols * tns->rows;
  263. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  264. tns->first -= r - r % tns->cols;
  265. if (r > 0)
  266. cnt -= r % tns->cols;
  267. }
  268. r = cnt % tns->cols ? 1 : 0;
  269. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  270. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  271. for (i = 0; i < cnt; i++) {
  272. t = &tns->thumbs[tns->first + i];
  273. t->x = x + (THUMB_SIZE - t->w) / 2;
  274. t->y = y + (THUMB_SIZE - t->h) / 2;
  275. imlib_context_set_image(t->im);
  276. if (imlib_image_has_alpha() && !tns->alpha)
  277. win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
  278. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  279. t->x, t->y, t->w, t->h);
  280. if ((i + 1) % tns->cols == 0) {
  281. x = tns->x;
  282. y += thumb_dim;
  283. } else {
  284. x += thumb_dim;
  285. }
  286. }
  287. tns->dirty = false;
  288. tns_highlight(tns, win, tns->sel, true);
  289. }
  290. void tns_highlight(tns_t *tns, win_t *win, int n, bool hl) {
  291. thumb_t *t;
  292. int x, y;
  293. unsigned long col;
  294. if (!tns || !tns->thumbs || !win)
  295. return;
  296. if (n >= 0 && n < tns->cnt) {
  297. t = &tns->thumbs[n];
  298. if (hl)
  299. col = win->selcol;
  300. else if (win->fullscreen)
  301. col = win->black;
  302. else
  303. col = win->bgcol;
  304. x = t->x - (THUMB_SIZE - t->w) / 2;
  305. y = t->y - (THUMB_SIZE - t->h) / 2;
  306. win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
  307. false, 2, col);
  308. }
  309. win_draw(win);
  310. }
  311. bool tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
  312. int old;
  313. if (!tns || !tns->thumbs || !win)
  314. return false;
  315. old = tns->sel;
  316. switch (dir) {
  317. case DIR_LEFT:
  318. if (tns->sel > 0)
  319. tns->sel--;
  320. break;
  321. case DIR_RIGHT:
  322. if (tns->sel < tns->cnt - 1)
  323. tns->sel++;
  324. break;
  325. case DIR_UP:
  326. if (tns->sel >= tns->cols)
  327. tns->sel -= tns->cols;
  328. break;
  329. case DIR_DOWN:
  330. if (tns->sel + tns->cols < tns->cnt)
  331. tns->sel += tns->cols;
  332. break;
  333. }
  334. if (tns->sel != old) {
  335. tns_highlight(tns, win, old, false);
  336. tns_check_view(tns, false);
  337. if (!tns->dirty)
  338. tns_highlight(tns, win, tns->sel, true);
  339. }
  340. return tns->sel != old;
  341. }
  342. bool tns_scroll(tns_t *tns, direction_t dir) {
  343. int old;
  344. if (!tns)
  345. return false;
  346. old = tns->first;
  347. if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  348. tns->first += tns->cols;
  349. tns_check_view(tns, true);
  350. tns->dirty = true;
  351. } else if (dir == DIR_UP && tns->first >= tns->cols) {
  352. tns->first -= tns->cols;
  353. tns_check_view(tns, true);
  354. tns->dirty = true;
  355. }
  356. return tns->first != old;
  357. }
  358. int tns_translate(tns_t *tns, int x, int y) {
  359. int n;
  360. if (!tns || !tns->thumbs)
  361. return -1;
  362. if (x < tns->x || y < tns->y)
  363. return -1;
  364. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  365. (x - tns->x) / thumb_dim;
  366. if (n >= tns->cnt)
  367. n = -1;
  368. return n;
  369. }