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ů.

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