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.
 
 
 
 
 
 

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