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.
 
 
 
 
 
 

464 line
9.6 KiB

  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. extern Imlib_Image *im_invalid;
  28. const int thumb_dim = THUMB_SIZE + 10;
  29. char *cache_dir = NULL;
  30. int tns_cache_enabled() {
  31. struct stat stats;
  32. return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) &&
  33. !access(cache_dir, W_OK);
  34. }
  35. char* tns_cache_filename(const char *filename) {
  36. size_t len;
  37. char *cfile = NULL;
  38. const char *abspath;
  39. if (!cache_dir || !filename)
  40. return NULL;
  41. if (*filename != '/') {
  42. if (!(abspath = absolute_path(filename)))
  43. return NULL;
  44. } else {
  45. abspath = filename;
  46. }
  47. if (strncmp(abspath, cache_dir, strlen(cache_dir))) {
  48. len = strlen(cache_dir) + strlen(abspath) + 6;
  49. cfile = (char*) s_malloc(len);
  50. snprintf(cfile, len, "%s/%s.png", cache_dir, abspath + 1);
  51. }
  52. if (abspath != filename)
  53. free((void*) abspath);
  54. return cfile;
  55. }
  56. Imlib_Image* tns_cache_load(const char *filename) {
  57. char *cfile;
  58. struct stat cstats, fstats;
  59. Imlib_Image *im = NULL;
  60. if (!filename)
  61. return NULL;
  62. if (stat(filename, &fstats))
  63. return NULL;
  64. if ((cfile = tns_cache_filename(filename))) {
  65. if (!stat(cfile, &cstats) &&
  66. cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
  67. cstats.st_mtim.tv_nsec == fstats.st_mtim.tv_nsec)
  68. {
  69. printf("cache hit: %s\n", filename);
  70. im = imlib_load_image(cfile);
  71. } else
  72. printf("cache MISS: %s\n", filename);
  73. free(cfile);
  74. }
  75. return im;
  76. }
  77. void tns_cache_write(thumb_t *t, Bool force) {
  78. char *cfile, *dirend;
  79. struct stat cstats, fstats;
  80. struct timeval times[2];
  81. Imlib_Load_Error err = 0;
  82. if (!t || !t->im || !t->filename)
  83. return;
  84. if (stat(t->filename, &fstats))
  85. return;
  86. if ((cfile = tns_cache_filename(t->filename))) {
  87. if (force || stat(cfile, &cstats) ||
  88. cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
  89. cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec)
  90. {
  91. if ((dirend = strrchr(cfile, '/'))) {
  92. *dirend = '\0';
  93. err = r_mkdir(cfile);
  94. *dirend = '/';
  95. }
  96. if (!err) {
  97. imlib_context_set_image(t->im);
  98. imlib_image_set_format("png");
  99. imlib_save_image_with_error_return(cfile, &err);
  100. }
  101. if (err) {
  102. warn("could not cache thumbnail: %s", t->filename);
  103. } else {
  104. TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
  105. TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
  106. utimes(cfile, times);
  107. printf("thumbnail cache file written: %s\n", t->filename);
  108. }
  109. }
  110. free(cfile);
  111. }
  112. }
  113. void tns_clear_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, win_t *win) {
  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. void tns_load(tns_t *tns, win_t *win, int n, const char *filename) {
  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 || !win || !filename)
  188. return;
  189. if (n >= tns->cap)
  190. return;
  191. else if (n >= tns->cnt)
  192. tns->cnt = n + 1;
  193. t = &tns->thumbs[n];
  194. if (t->im) {
  195. imlib_context_set_image(t->im);
  196. imlib_free_image();
  197. }
  198. if ((use_cache = tns_cache_enabled())) {
  199. if ((im = tns_cache_load(filename)))
  200. cached = 1;
  201. }
  202. if (cached || (im = imlib_load_image(filename)))
  203. imlib_context_set_image(im);
  204. else
  205. imlib_context_set_image(im_invalid);
  206. w = imlib_image_get_width();
  207. h = imlib_image_get_height();
  208. if (im) {
  209. t->filename = filename;
  210. zw = (float) THUMB_SIZE / (float) w;
  211. zh = (float) THUMB_SIZE / (float) h;
  212. z = MIN(zw, zh);
  213. } else {
  214. t->filename = NULL;
  215. z = 1.0;
  216. }
  217. t->w = z * w;
  218. t->h = z * h;
  219. imlib_context_set_anti_alias(1);
  220. if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
  221. die("could not allocate memory");
  222. if (im)
  223. imlib_free_image_and_decache();
  224. if (use_cache && !cached)
  225. tns_cache_write(t, False);
  226. tns->dirty = 1;
  227. }
  228. void tns_check_view(tns_t *tns, Bool scrolled) {
  229. int r;
  230. if (!tns)
  231. return;
  232. tns->first -= tns->first % tns->cols;
  233. r = tns->sel % tns->cols;
  234. if (scrolled) {
  235. /* move selection into visible area */
  236. if (tns->sel >= tns->first + tns->cols * tns->rows)
  237. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  238. else if (tns->sel < tns->first)
  239. tns->sel = tns->first + r;
  240. } else {
  241. /* scroll to selection */
  242. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  243. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  244. tns->dirty = 1;
  245. } else if (tns->first > tns->sel) {
  246. tns->first = tns->sel - r;
  247. tns->dirty = 1;
  248. }
  249. }
  250. }
  251. void tns_render(tns_t *tns, win_t *win) {
  252. int i, cnt, r, x, y;
  253. thumb_t *t;
  254. if (!tns || !tns->thumbs || !win)
  255. return;
  256. if (!tns->dirty)
  257. return;
  258. win_clear(win);
  259. imlib_context_set_drawable(win->pm);
  260. tns->cols = MAX(1, win->w / thumb_dim);
  261. tns->rows = MAX(1, win->h / thumb_dim);
  262. if (tns->cnt < tns->cols * tns->rows) {
  263. tns->first = 0;
  264. cnt = tns->cnt;
  265. } else {
  266. tns_check_view(tns, False);
  267. cnt = tns->cols * tns->rows;
  268. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  269. tns->first -= r - r % tns->cols;
  270. if (r > 0)
  271. cnt -= r % tns->cols;
  272. }
  273. r = cnt % tns->cols ? 1 : 0;
  274. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  275. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  276. for (i = 0; i < cnt; ++i) {
  277. t = &tns->thumbs[tns->first + i];
  278. t->x = x + (THUMB_SIZE - t->w) / 2;
  279. t->y = y + (THUMB_SIZE - t->h) / 2;
  280. imlib_context_set_image(t->im);
  281. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  282. t->x, t->y, t->w, t->h);
  283. if ((i + 1) % tns->cols == 0) {
  284. x = tns->x;
  285. y += thumb_dim;
  286. } else {
  287. x += thumb_dim;
  288. }
  289. }
  290. tns->dirty = 0;
  291. tns_highlight(tns, win, tns->sel, True);
  292. }
  293. void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
  294. thumb_t *t;
  295. unsigned long col;
  296. if (!tns || !tns->thumbs || !win)
  297. return;
  298. if (n >= 0 && n < tns->cnt) {
  299. t = &tns->thumbs[n];
  300. if (hl)
  301. col = win->selcol;
  302. else if (win->fullscreen)
  303. col = win->black;
  304. else
  305. col = win->bgcol;
  306. win_draw_rect(win, win->pm, t->x - 2, t->y - 2, t->w + 4, t->h + 4,
  307. False, 2, col);
  308. }
  309. win_draw(win);
  310. }
  311. int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
  312. int old;
  313. if (!tns || !tns->thumbs || !win)
  314. return 0;
  315. old = tns->sel;
  316. switch (dir) {
  317. case TNS_LEFT:
  318. if (tns->sel > 0)
  319. --tns->sel;
  320. break;
  321. case TNS_RIGHT:
  322. if (tns->sel < tns->cnt - 1)
  323. ++tns->sel;
  324. break;
  325. case TNS_UP:
  326. if (tns->sel >= tns->cols)
  327. tns->sel -= tns->cols;
  328. break;
  329. case TNS_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. int tns_scroll(tns_t *tns, tnsdir_t dir) {
  343. int old;
  344. if (!tns)
  345. return 0;
  346. old = tns->first;
  347. if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  348. tns->first += tns->cols;
  349. tns_check_view(tns, True);
  350. tns->dirty = 1;
  351. } else if (dir == TNS_UP && tns->first >= tns->cols) {
  352. tns->first -= tns->cols;
  353. tns_check_view(tns, True);
  354. tns->dirty = 1;
  355. }
  356. return tns->first != old;
  357. }
  358. int tns_translate(tns_t *tns, int x, int y) {
  359. int n;
  360. thumb_t *t;
  361. if (!tns || !tns->thumbs)
  362. return -1;
  363. if (x < tns->x || y < tns->y)
  364. return -1;
  365. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  366. (x - tns->x) / thumb_dim;
  367. if (n < tns->cnt) {
  368. t = &tns->thumbs[n];
  369. if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
  370. return n;
  371. }
  372. return -1;
  373. }