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.
 
 
 
 
 
 

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